2 * linux/fs/ext4/crypto_fname.c
4 * Copyright (C) 2015, Google, Inc.
6 * This contains functions for filename crypto management in ext4
8 * Written by Uday Savagaonkar, 2014.
10 * This has not yet undergone a rigorous security audit.
14 #include <crypto/skcipher.h>
15 #include <keys/encrypted-type.h>
16 #include <keys/user-type.h>
17 #include <linux/gfp.h>
18 #include <linux/kernel.h>
19 #include <linux/key.h>
20 #include <linux/list.h>
21 #include <linux/mempool.h>
22 #include <linux/random.h>
23 #include <linux/scatterlist.h>
24 #include <linux/spinlock_types.h>
27 #include "ext4_crypto.h"
31 * ext4_dir_crypt_complete() -
33 static void ext4_dir_crypt_complete(struct crypto_async_request
*req
, int res
)
35 struct ext4_completion_result
*ecr
= req
->data
;
37 if (res
== -EINPROGRESS
)
40 complete(&ecr
->completion
);
43 bool ext4_valid_filenames_enc_mode(uint32_t mode
)
45 return (mode
== EXT4_ENCRYPTION_MODE_AES_256_CTS
);
48 static unsigned max_name_len(struct inode
*inode
)
50 return S_ISLNK(inode
->i_mode
) ? inode
->i_sb
->s_blocksize
:
55 * ext4_fname_encrypt() -
57 * This function encrypts the input filename, and returns the length of the
58 * ciphertext. Errors are returned as negative numbers. We trust the caller to
59 * allocate sufficient memory to oname string.
61 static int ext4_fname_encrypt(struct inode
*inode
,
62 const struct qstr
*iname
,
63 struct ext4_str
*oname
)
66 struct skcipher_request
*req
= NULL
;
67 DECLARE_EXT4_COMPLETION_RESULT(ecr
);
68 struct ext4_crypt_info
*ci
= EXT4_I(inode
)->i_crypt_info
;
69 struct crypto_skcipher
*tfm
= ci
->ci_ctfm
;
71 char iv
[EXT4_CRYPTO_BLOCK_SIZE
];
72 struct scatterlist src_sg
, dst_sg
;
73 int padding
= 4 << (ci
->ci_flags
& EXT4_POLICY_FLAGS_PAD_MASK
);
74 char *workbuf
, buf
[32], *alloc_buf
= NULL
;
75 unsigned lim
= max_name_len(inode
);
77 if (iname
->len
<= 0 || iname
->len
> lim
)
80 ciphertext_len
= (iname
->len
< EXT4_CRYPTO_BLOCK_SIZE
) ?
81 EXT4_CRYPTO_BLOCK_SIZE
: iname
->len
;
82 ciphertext_len
= ext4_fname_crypto_round_up(ciphertext_len
, padding
);
83 ciphertext_len
= (ciphertext_len
> lim
)
84 ? lim
: ciphertext_len
;
86 if (ciphertext_len
<= sizeof(buf
)) {
89 alloc_buf
= kmalloc(ciphertext_len
, GFP_NOFS
);
95 /* Allocate request */
96 req
= skcipher_request_alloc(tfm
, GFP_NOFS
);
99 KERN_ERR
"%s: crypto_request_alloc() failed\n", __func__
);
103 skcipher_request_set_callback(req
,
104 CRYPTO_TFM_REQ_MAY_BACKLOG
| CRYPTO_TFM_REQ_MAY_SLEEP
,
105 ext4_dir_crypt_complete
, &ecr
);
108 memcpy(workbuf
, iname
->name
, iname
->len
);
109 if (iname
->len
< ciphertext_len
)
110 memset(workbuf
+ iname
->len
, 0, ciphertext_len
- iname
->len
);
113 memset(iv
, 0, EXT4_CRYPTO_BLOCK_SIZE
);
115 /* Create encryption request */
116 sg_init_one(&src_sg
, workbuf
, ciphertext_len
);
117 sg_init_one(&dst_sg
, oname
->name
, ciphertext_len
);
118 skcipher_request_set_crypt(req
, &src_sg
, &dst_sg
, ciphertext_len
, iv
);
119 res
= crypto_skcipher_encrypt(req
);
120 if (res
== -EINPROGRESS
|| res
== -EBUSY
) {
121 wait_for_completion(&ecr
.completion
);
125 skcipher_request_free(req
);
128 KERN_ERR
"%s: Error (error code %d)\n", __func__
, res
);
130 oname
->len
= ciphertext_len
;
135 * ext4_fname_decrypt()
136 * This function decrypts the input filename, and returns
137 * the length of the plaintext.
138 * Errors are returned as negative numbers.
139 * We trust the caller to allocate sufficient memory to oname string.
141 static int ext4_fname_decrypt(struct inode
*inode
,
142 const struct ext4_str
*iname
,
143 struct ext4_str
*oname
)
145 struct ext4_str tmp_in
[2], tmp_out
[1];
146 struct skcipher_request
*req
= NULL
;
147 DECLARE_EXT4_COMPLETION_RESULT(ecr
);
148 struct scatterlist src_sg
, dst_sg
;
149 struct ext4_crypt_info
*ci
= EXT4_I(inode
)->i_crypt_info
;
150 struct crypto_skcipher
*tfm
= ci
->ci_ctfm
;
152 char iv
[EXT4_CRYPTO_BLOCK_SIZE
];
153 unsigned lim
= max_name_len(inode
);
155 if (iname
->len
<= 0 || iname
->len
> lim
)
158 tmp_in
[0].name
= iname
->name
;
159 tmp_in
[0].len
= iname
->len
;
160 tmp_out
[0].name
= oname
->name
;
162 /* Allocate request */
163 req
= skcipher_request_alloc(tfm
, GFP_NOFS
);
166 KERN_ERR
"%s: crypto_request_alloc() failed\n", __func__
);
169 skcipher_request_set_callback(req
,
170 CRYPTO_TFM_REQ_MAY_BACKLOG
| CRYPTO_TFM_REQ_MAY_SLEEP
,
171 ext4_dir_crypt_complete
, &ecr
);
174 memset(iv
, 0, EXT4_CRYPTO_BLOCK_SIZE
);
176 /* Create encryption request */
177 sg_init_one(&src_sg
, iname
->name
, iname
->len
);
178 sg_init_one(&dst_sg
, oname
->name
, oname
->len
);
179 skcipher_request_set_crypt(req
, &src_sg
, &dst_sg
, iname
->len
, iv
);
180 res
= crypto_skcipher_decrypt(req
);
181 if (res
== -EINPROGRESS
|| res
== -EBUSY
) {
182 wait_for_completion(&ecr
.completion
);
185 skcipher_request_free(req
);
188 KERN_ERR
"%s: Error in ext4_fname_encrypt (error code %d)\n",
193 oname
->len
= strnlen(oname
->name
, iname
->len
);
197 static const char *lookup_table
=
198 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
201 * ext4_fname_encode_digest() -
203 * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
204 * The encoded string is roughly 4/3 times the size of the input string.
206 static int digest_encode(const char *src
, int len
, char *dst
)
208 int i
= 0, bits
= 0, ac
= 0;
212 ac
+= (((unsigned char) src
[i
]) << bits
);
215 *cp
++ = lookup_table
[ac
& 0x3f];
222 *cp
++ = lookup_table
[ac
& 0x3f];
226 static int digest_decode(const char *src
, int len
, char *dst
)
228 int i
= 0, bits
= 0, ac
= 0;
233 p
= strchr(lookup_table
, src
[i
]);
234 if (p
== NULL
|| src
[i
] == 0)
236 ac
+= (p
- lookup_table
) << bits
;
251 * ext4_fname_crypto_round_up() -
253 * Return: The next multiple of block size
255 u32
ext4_fname_crypto_round_up(u32 size
, u32 blksize
)
257 return ((size
+blksize
-1)/blksize
)*blksize
;
260 unsigned ext4_fname_encrypted_size(struct inode
*inode
, u32 ilen
)
262 struct ext4_crypt_info
*ci
= EXT4_I(inode
)->i_crypt_info
;
266 padding
= 4 << (ci
->ci_flags
& EXT4_POLICY_FLAGS_PAD_MASK
);
267 if (ilen
< EXT4_CRYPTO_BLOCK_SIZE
)
268 ilen
= EXT4_CRYPTO_BLOCK_SIZE
;
269 return ext4_fname_crypto_round_up(ilen
, padding
);
273 * ext4_fname_crypto_alloc_buffer() -
275 * Allocates an output buffer that is sufficient for the crypto operation
276 * specified by the context and the direction.
278 int ext4_fname_crypto_alloc_buffer(struct inode
*inode
,
279 u32 ilen
, struct ext4_str
*crypto_str
)
281 unsigned int olen
= ext4_fname_encrypted_size(inode
, ilen
);
283 crypto_str
->len
= olen
;
284 if (olen
< EXT4_FNAME_CRYPTO_DIGEST_SIZE
*2)
285 olen
= EXT4_FNAME_CRYPTO_DIGEST_SIZE
*2;
286 /* Allocated buffer can hold one more character to null-terminate the
288 crypto_str
->name
= kmalloc(olen
+1, GFP_NOFS
);
289 if (!(crypto_str
->name
))
295 * ext4_fname_crypto_free_buffer() -
297 * Frees the buffer allocated for crypto operation.
299 void ext4_fname_crypto_free_buffer(struct ext4_str
*crypto_str
)
303 kfree(crypto_str
->name
);
304 crypto_str
->name
= NULL
;
308 * ext4_fname_disk_to_usr() - converts a filename from disk space to user space
310 int _ext4_fname_disk_to_usr(struct inode
*inode
,
311 struct dx_hash_info
*hinfo
,
312 const struct ext4_str
*iname
,
313 struct ext4_str
*oname
)
318 if (iname
->len
< 3) {
319 /*Check for . and .. */
320 if (iname
->name
[0] == '.' && iname
->name
[iname
->len
-1] == '.') {
321 oname
->name
[0] = '.';
322 oname
->name
[iname
->len
-1] = '.';
323 oname
->len
= iname
->len
;
327 if (iname
->len
< EXT4_CRYPTO_BLOCK_SIZE
) {
328 EXT4_ERROR_INODE(inode
, "encrypted inode too small");
331 if (EXT4_I(inode
)->i_crypt_info
)
332 return ext4_fname_decrypt(inode
, iname
, oname
);
334 if (iname
->len
<= EXT4_FNAME_CRYPTO_DIGEST_SIZE
) {
335 ret
= digest_encode(iname
->name
, iname
->len
, oname
->name
);
340 memcpy(buf
, &hinfo
->hash
, 4);
341 memcpy(buf
+4, &hinfo
->minor_hash
, 4);
344 memcpy(buf
+ 8, iname
->name
+ iname
->len
- 16, 16);
345 oname
->name
[0] = '_';
346 ret
= digest_encode(buf
, 24, oname
->name
+1);
347 oname
->len
= ret
+ 1;
351 int ext4_fname_disk_to_usr(struct inode
*inode
,
352 struct dx_hash_info
*hinfo
,
353 const struct ext4_dir_entry_2
*de
,
354 struct ext4_str
*oname
)
356 struct ext4_str iname
= {.name
= (unsigned char *) de
->name
,
357 .len
= de
->name_len
};
359 return _ext4_fname_disk_to_usr(inode
, hinfo
, &iname
, oname
);
364 * ext4_fname_usr_to_disk() - converts a filename from user space to disk space
366 int ext4_fname_usr_to_disk(struct inode
*inode
,
367 const struct qstr
*iname
,
368 struct ext4_str
*oname
)
371 struct ext4_crypt_info
*ci
= EXT4_I(inode
)->i_crypt_info
;
373 if (iname
->len
< 3) {
374 /*Check for . and .. */
375 if (iname
->name
[0] == '.' &&
376 iname
->name
[iname
->len
-1] == '.') {
377 oname
->name
[0] = '.';
378 oname
->name
[iname
->len
-1] = '.';
379 oname
->len
= iname
->len
;
384 res
= ext4_fname_encrypt(inode
, iname
, oname
);
387 /* Without a proper key, a user is not allowed to modify the filenames
388 * in a directory. Consequently, a user space name cannot be mapped to
389 * a disk-space name */
393 int ext4_fname_setup_filename(struct inode
*dir
, const struct qstr
*iname
,
394 int lookup
, struct ext4_filename
*fname
)
396 struct ext4_crypt_info
*ci
;
397 int ret
= 0, bigname
= 0;
399 memset(fname
, 0, sizeof(struct ext4_filename
));
400 fname
->usr_fname
= iname
;
402 if (!ext4_encrypted_inode(dir
) ||
403 ((iname
->name
[0] == '.') &&
404 ((iname
->len
== 1) ||
405 ((iname
->name
[1] == '.') && (iname
->len
== 2))))) {
406 fname
->disk_name
.name
= (unsigned char *) iname
->name
;
407 fname
->disk_name
.len
= iname
->len
;
410 ret
= ext4_get_encryption_info(dir
);
413 ci
= EXT4_I(dir
)->i_crypt_info
;
415 ret
= ext4_fname_crypto_alloc_buffer(dir
, iname
->len
,
419 ret
= ext4_fname_encrypt(dir
, iname
, &fname
->crypto_buf
);
422 fname
->disk_name
.name
= fname
->crypto_buf
.name
;
423 fname
->disk_name
.len
= fname
->crypto_buf
.len
;
429 /* We don't have the key and we are doing a lookup; decode the
432 if (iname
->name
[0] == '_')
434 if ((bigname
&& (iname
->len
!= 33)) ||
435 (!bigname
&& (iname
->len
> 43)))
438 fname
->crypto_buf
.name
= kmalloc(32, GFP_KERNEL
);
439 if (fname
->crypto_buf
.name
== NULL
)
441 ret
= digest_decode(iname
->name
+ bigname
, iname
->len
- bigname
,
442 fname
->crypto_buf
.name
);
447 fname
->crypto_buf
.len
= ret
;
449 memcpy(&fname
->hinfo
.hash
, fname
->crypto_buf
.name
, 4);
450 memcpy(&fname
->hinfo
.minor_hash
, fname
->crypto_buf
.name
+ 4, 4);
452 fname
->disk_name
.name
= fname
->crypto_buf
.name
;
453 fname
->disk_name
.len
= fname
->crypto_buf
.len
;
457 kfree(fname
->crypto_buf
.name
);
458 fname
->crypto_buf
.name
= NULL
;
462 void ext4_fname_free_filename(struct ext4_filename
*fname
)
464 kfree(fname
->crypto_buf
.name
);
465 fname
->crypto_buf
.name
= NULL
;
466 fname
->usr_fname
= NULL
;
467 fname
->disk_name
.name
= NULL
;