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/hash.h>
15 #include <crypto/sha.h>
16 #include <keys/encrypted-type.h>
17 #include <keys/user-type.h>
18 #include <linux/crypto.h>
19 #include <linux/gfp.h>
20 #include <linux/kernel.h>
21 #include <linux/key.h>
22 #include <linux/key.h>
23 #include <linux/list.h>
24 #include <linux/mempool.h>
25 #include <linux/random.h>
26 #include <linux/scatterlist.h>
27 #include <linux/spinlock_types.h>
30 #include "ext4_crypto.h"
34 * ext4_dir_crypt_complete() -
36 static void ext4_dir_crypt_complete(struct crypto_async_request
*req
, int res
)
38 struct ext4_completion_result
*ecr
= req
->data
;
40 if (res
== -EINPROGRESS
)
43 complete(&ecr
->completion
);
46 bool ext4_valid_filenames_enc_mode(uint32_t mode
)
48 return (mode
== EXT4_ENCRYPTION_MODE_AES_256_CTS
);
52 * ext4_fname_encrypt() -
54 * This function encrypts the input filename, and returns the length of the
55 * ciphertext. Errors are returned as negative numbers. We trust the caller to
56 * allocate sufficient memory to oname string.
58 static int ext4_fname_encrypt(struct ext4_fname_crypto_ctx
*ctx
,
59 const struct qstr
*iname
,
60 struct ext4_str
*oname
)
63 struct ablkcipher_request
*req
= NULL
;
64 DECLARE_EXT4_COMPLETION_RESULT(ecr
);
65 struct crypto_ablkcipher
*tfm
= ctx
->ctfm
;
67 char iv
[EXT4_CRYPTO_BLOCK_SIZE
];
68 struct scatterlist sg
[1];
71 if (iname
->len
<= 0 || iname
->len
> ctx
->lim
)
74 ciphertext_len
= (iname
->len
< EXT4_CRYPTO_BLOCK_SIZE
) ?
75 EXT4_CRYPTO_BLOCK_SIZE
: iname
->len
;
76 ciphertext_len
= (ciphertext_len
> ctx
->lim
)
77 ? ctx
->lim
: ciphertext_len
;
79 /* Allocate request */
80 req
= ablkcipher_request_alloc(tfm
, GFP_NOFS
);
83 KERN_ERR
"%s: crypto_request_alloc() failed\n", __func__
);
86 ablkcipher_request_set_callback(req
,
87 CRYPTO_TFM_REQ_MAY_BACKLOG
| CRYPTO_TFM_REQ_MAY_SLEEP
,
88 ext4_dir_crypt_complete
, &ecr
);
90 /* Map the workpage */
91 workbuf
= kmap(ctx
->workpage
);
94 memcpy(workbuf
, iname
->name
, iname
->len
);
95 if (iname
->len
< ciphertext_len
)
96 memset(workbuf
+ iname
->len
, 0, ciphertext_len
- iname
->len
);
99 memset(iv
, 0, EXT4_CRYPTO_BLOCK_SIZE
);
101 /* Create encryption request */
102 sg_init_table(sg
, 1);
103 sg_set_page(sg
, ctx
->workpage
, PAGE_SIZE
, 0);
104 ablkcipher_request_set_crypt(req
, sg
, sg
, iname
->len
, iv
);
105 res
= crypto_ablkcipher_encrypt(req
);
106 if (res
== -EINPROGRESS
|| res
== -EBUSY
) {
107 BUG_ON(req
->base
.data
!= &ecr
);
108 wait_for_completion(&ecr
.completion
);
112 /* Copy the result to output */
113 memcpy(oname
->name
, workbuf
, ciphertext_len
);
114 res
= ciphertext_len
;
116 kunmap(ctx
->workpage
);
117 ablkcipher_request_free(req
);
120 KERN_ERR
"%s: Error (error code %d)\n", __func__
, res
);
122 oname
->len
= ciphertext_len
;
127 * ext4_fname_decrypt()
128 * This function decrypts the input filename, and returns
129 * the length of the plaintext.
130 * Errors are returned as negative numbers.
131 * We trust the caller to allocate sufficient memory to oname string.
133 static int ext4_fname_decrypt(struct ext4_fname_crypto_ctx
*ctx
,
134 const struct ext4_str
*iname
,
135 struct ext4_str
*oname
)
137 struct ext4_str tmp_in
[2], tmp_out
[1];
138 struct ablkcipher_request
*req
= NULL
;
139 DECLARE_EXT4_COMPLETION_RESULT(ecr
);
140 struct scatterlist sg
[1];
141 struct crypto_ablkcipher
*tfm
= ctx
->ctfm
;
143 char iv
[EXT4_CRYPTO_BLOCK_SIZE
];
146 if (iname
->len
<= 0 || iname
->len
> ctx
->lim
)
149 tmp_in
[0].name
= iname
->name
;
150 tmp_in
[0].len
= iname
->len
;
151 tmp_out
[0].name
= oname
->name
;
153 /* Allocate request */
154 req
= ablkcipher_request_alloc(tfm
, GFP_NOFS
);
157 KERN_ERR
"%s: crypto_request_alloc() failed\n", __func__
);
160 ablkcipher_request_set_callback(req
,
161 CRYPTO_TFM_REQ_MAY_BACKLOG
| CRYPTO_TFM_REQ_MAY_SLEEP
,
162 ext4_dir_crypt_complete
, &ecr
);
164 /* Map the workpage */
165 workbuf
= kmap(ctx
->workpage
);
168 memcpy(workbuf
, iname
->name
, iname
->len
);
171 memset(iv
, 0, EXT4_CRYPTO_BLOCK_SIZE
);
173 /* Create encryption request */
174 sg_init_table(sg
, 1);
175 sg_set_page(sg
, ctx
->workpage
, PAGE_SIZE
, 0);
176 ablkcipher_request_set_crypt(req
, sg
, sg
, iname
->len
, iv
);
177 res
= crypto_ablkcipher_decrypt(req
);
178 if (res
== -EINPROGRESS
|| res
== -EBUSY
) {
179 BUG_ON(req
->base
.data
!= &ecr
);
180 wait_for_completion(&ecr
.completion
);
184 /* Copy the result to output */
185 memcpy(oname
->name
, workbuf
, iname
->len
);
188 kunmap(ctx
->workpage
);
189 ablkcipher_request_free(req
);
192 KERN_ERR
"%s: Error in ext4_fname_encrypt (error code %d)\n",
197 oname
->len
= strnlen(oname
->name
, iname
->len
);
202 * ext4_fname_encode_digest() -
204 * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
205 * The encoded string is roughly 4/3 times the size of the input string.
207 int ext4_fname_encode_digest(char *dst
, char *src
, u32 len
)
209 static const char *lookup_table
=
210 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+";
211 u32 current_chunk
, num_chunks
, i
;
217 for (i
= 0; i
< num_chunks
; i
++) {
218 c0
= src
[3*i
] & 0x3f;
219 c1
= (((src
[3*i
]>>6)&0x3) | ((src
[3*i
+1] & 0xf)<<2)) & 0x3f;
220 c2
= (((src
[3*i
+1]>>4)&0xf) | ((src
[3*i
+2] & 0x3)<<4)) & 0x3f;
221 c3
= (src
[3*i
+2]>>2) & 0x3f;
222 dst
[4*i
] = lookup_table
[c0
];
223 dst
[4*i
+1] = lookup_table
[c1
];
224 dst
[4*i
+2] = lookup_table
[c2
];
225 dst
[4*i
+3] = lookup_table
[c3
];
228 memset(tmp_buf
, 0, 3);
229 memcpy(tmp_buf
, &src
[3*i
], len
-3*i
);
230 c0
= tmp_buf
[0] & 0x3f;
231 c1
= (((tmp_buf
[0]>>6)&0x3) | ((tmp_buf
[1] & 0xf)<<2)) & 0x3f;
232 c2
= (((tmp_buf
[1]>>4)&0xf) | ((tmp_buf
[2] & 0x3)<<4)) & 0x3f;
233 c3
= (tmp_buf
[2]>>2) & 0x3f;
234 dst
[4*i
] = lookup_table
[c0
];
235 dst
[4*i
+1] = lookup_table
[c1
];
236 dst
[4*i
+2] = lookup_table
[c2
];
237 dst
[4*i
+3] = lookup_table
[c3
];
244 * ext4_fname_hash() -
246 * This function computes the hash of the input filename, and sets the output
247 * buffer to the *encoded* digest. It returns the length of the digest as its
248 * return value. Errors are returned as negative numbers. We trust the caller
249 * to allocate sufficient memory to oname string.
251 static int ext4_fname_hash(struct ext4_fname_crypto_ctx
*ctx
,
252 const struct ext4_str
*iname
,
253 struct ext4_str
*oname
)
255 struct scatterlist sg
;
256 struct hash_desc desc
= {
257 .tfm
= (struct crypto_hash
*)ctx
->htfm
,
258 .flags
= CRYPTO_TFM_REQ_MAY_SLEEP
262 if (iname
->len
<= EXT4_FNAME_CRYPTO_DIGEST_SIZE
) {
263 res
= ext4_fname_encode_digest(oname
->name
, iname
->name
,
269 sg_init_one(&sg
, iname
->name
, iname
->len
);
270 res
= crypto_hash_init(&desc
);
273 "%s: Error initializing crypto hash; res = [%d]\n",
277 res
= crypto_hash_update(&desc
, &sg
, iname
->len
);
280 "%s: Error updating crypto hash; res = [%d]\n",
284 res
= crypto_hash_final(&desc
,
285 &oname
->name
[EXT4_FNAME_CRYPTO_DIGEST_SIZE
]);
288 "%s: Error finalizing crypto hash; res = [%d]\n",
292 /* Encode the digest as a printable string--this will increase the
293 * size of the digest */
294 oname
->name
[0] = 'I';
295 res
= ext4_fname_encode_digest(oname
->name
+1,
296 &oname
->name
[EXT4_FNAME_CRYPTO_DIGEST_SIZE
],
297 EXT4_FNAME_CRYPTO_DIGEST_SIZE
) + 1;
304 * ext4_free_fname_crypto_ctx() -
306 * Frees up a crypto context.
308 void ext4_free_fname_crypto_ctx(struct ext4_fname_crypto_ctx
*ctx
)
310 if (ctx
== NULL
|| IS_ERR(ctx
))
313 if (ctx
->ctfm
&& !IS_ERR(ctx
->ctfm
))
314 crypto_free_ablkcipher(ctx
->ctfm
);
315 if (ctx
->htfm
&& !IS_ERR(ctx
->htfm
))
316 crypto_free_hash(ctx
->htfm
);
317 if (ctx
->workpage
&& !IS_ERR(ctx
->workpage
))
318 __free_page(ctx
->workpage
);
323 * ext4_put_fname_crypto_ctx() -
325 * Return: The crypto context onto free list. If the free list is above a
326 * threshold, completely frees up the context, and returns the memory.
328 * TODO: Currently we directly free the crypto context. Eventually we should
329 * add code it to return to free list. Such an approach will increase
330 * efficiency of directory lookup.
332 void ext4_put_fname_crypto_ctx(struct ext4_fname_crypto_ctx
**ctx
)
334 if (*ctx
== NULL
|| IS_ERR(*ctx
))
336 ext4_free_fname_crypto_ctx(*ctx
);
341 * ext4_search_fname_crypto_ctx() -
343 static struct ext4_fname_crypto_ctx
*ext4_search_fname_crypto_ctx(
344 const struct ext4_encryption_key
*key
)
350 * ext4_alloc_fname_crypto_ctx() -
352 struct ext4_fname_crypto_ctx
*ext4_alloc_fname_crypto_ctx(
353 const struct ext4_encryption_key
*key
)
355 struct ext4_fname_crypto_ctx
*ctx
;
357 ctx
= kmalloc(sizeof(struct ext4_fname_crypto_ctx
), GFP_NOFS
);
359 return ERR_PTR(-ENOMEM
);
360 if (key
->mode
== EXT4_ENCRYPTION_MODE_INVALID
) {
361 /* This will automatically set key mode to invalid
362 * As enum for ENCRYPTION_MODE_INVALID is zero */
363 memset(&ctx
->key
, 0, sizeof(ctx
->key
));
365 memcpy(&ctx
->key
, key
, sizeof(struct ext4_encryption_key
));
367 ctx
->has_valid_key
= (EXT4_ENCRYPTION_MODE_INVALID
== key
->mode
)
369 ctx
->ctfm_key_is_ready
= 0;
372 ctx
->workpage
= NULL
;
377 * ext4_get_fname_crypto_ctx() -
379 * Allocates a free crypto context and initializes it to hold
380 * the crypto material for the inode.
382 * Return: NULL if not encrypted. Error value on error. Valid pointer otherwise.
384 struct ext4_fname_crypto_ctx
*ext4_get_fname_crypto_ctx(
385 struct inode
*inode
, u32 max_ciphertext_len
)
387 struct ext4_fname_crypto_ctx
*ctx
;
388 struct ext4_inode_info
*ei
= EXT4_I(inode
);
391 /* Check if the crypto policy is set on the inode */
392 res
= ext4_encrypted_inode(inode
);
396 if (!ext4_has_encryption_key(inode
))
397 ext4_generate_encryption_key(inode
);
399 /* Get a crypto context based on the key.
400 * A new context is allocated if no context matches the requested key.
402 ctx
= ext4_search_fname_crypto_ctx(&(ei
->i_encryption_key
));
404 ctx
= ext4_alloc_fname_crypto_ctx(&(ei
->i_encryption_key
));
408 if (ctx
->has_valid_key
) {
409 if (ctx
->key
.mode
!= EXT4_ENCRYPTION_MODE_AES_256_CTS
) {
410 printk_once(KERN_WARNING
411 "ext4: unsupported key mode %d\n",
413 return ERR_PTR(-ENOKEY
);
416 /* As a first cut, we will allocate new tfm in every call.
417 * later, we will keep the tfm around, in case the key gets
419 if (ctx
->ctfm
== NULL
) {
420 ctx
->ctfm
= crypto_alloc_ablkcipher("cts(cbc(aes))",
423 if (IS_ERR(ctx
->ctfm
)) {
424 res
= PTR_ERR(ctx
->ctfm
);
426 KERN_DEBUG
"%s: error (%d) allocating crypto tfm\n",
429 ext4_put_fname_crypto_ctx(&ctx
);
432 if (ctx
->ctfm
== NULL
) {
434 KERN_DEBUG
"%s: could not allocate crypto tfm\n",
436 ext4_put_fname_crypto_ctx(&ctx
);
437 return ERR_PTR(-ENOMEM
);
439 if (ctx
->workpage
== NULL
)
440 ctx
->workpage
= alloc_page(GFP_NOFS
);
441 if (IS_ERR(ctx
->workpage
)) {
442 res
= PTR_ERR(ctx
->workpage
);
444 KERN_DEBUG
"%s: error (%d) allocating work page\n",
446 ctx
->workpage
= NULL
;
447 ext4_put_fname_crypto_ctx(&ctx
);
450 if (ctx
->workpage
== NULL
) {
452 KERN_DEBUG
"%s: could not allocate work page\n",
454 ext4_put_fname_crypto_ctx(&ctx
);
455 return ERR_PTR(-ENOMEM
);
457 ctx
->lim
= max_ciphertext_len
;
458 crypto_ablkcipher_clear_flags(ctx
->ctfm
, ~0);
459 crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctx
->ctfm
),
460 CRYPTO_TFM_REQ_WEAK_KEY
);
462 /* If we are lucky, we will get a context that is already
463 * set up with the right key. Else, we will have to
465 if (!ctx
->ctfm_key_is_ready
) {
466 /* Since our crypto objectives for filename encryption
468 * we directly use the inode master key */
469 res
= crypto_ablkcipher_setkey(ctx
->ctfm
,
470 ctx
->key
.raw
, ctx
->key
.size
);
472 ext4_put_fname_crypto_ctx(&ctx
);
473 return ERR_PTR(-EIO
);
475 ctx
->ctfm_key_is_ready
= 1;
477 /* In the current implementation, key should never be
478 * marked "ready" for a context that has just been
479 * allocated. So we should never reach here */
483 if (ctx
->htfm
== NULL
)
484 ctx
->htfm
= crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC
);
485 if (IS_ERR(ctx
->htfm
)) {
486 res
= PTR_ERR(ctx
->htfm
);
487 printk(KERN_DEBUG
"%s: error (%d) allocating hash tfm\n",
490 ext4_put_fname_crypto_ctx(&ctx
);
493 if (ctx
->htfm
== NULL
) {
494 printk(KERN_DEBUG
"%s: could not allocate hash tfm\n",
496 ext4_put_fname_crypto_ctx(&ctx
);
497 return ERR_PTR(-ENOMEM
);
504 * ext4_fname_crypto_round_up() -
506 * Return: The next multiple of block size
508 u32
ext4_fname_crypto_round_up(u32 size
, u32 blksize
)
510 return ((size
+blksize
-1)/blksize
)*blksize
;
514 * ext4_fname_crypto_namelen_on_disk() -
516 int ext4_fname_crypto_namelen_on_disk(struct ext4_fname_crypto_ctx
*ctx
,
523 if (!(ctx
->has_valid_key
))
525 ciphertext_len
= (namelen
< EXT4_CRYPTO_BLOCK_SIZE
) ?
526 EXT4_CRYPTO_BLOCK_SIZE
: namelen
;
527 ciphertext_len
= (ciphertext_len
> ctx
->lim
)
528 ? ctx
->lim
: ciphertext_len
;
529 return (int) ciphertext_len
;
533 * ext4_fname_crypto_alloc_obuff() -
535 * Allocates an output buffer that is sufficient for the crypto operation
536 * specified by the context and the direction.
538 int ext4_fname_crypto_alloc_buffer(struct ext4_fname_crypto_ctx
*ctx
,
539 u32 ilen
, struct ext4_str
*crypto_str
)
545 olen
= ext4_fname_crypto_round_up(ilen
, EXT4_CRYPTO_BLOCK_SIZE
);
546 crypto_str
->len
= olen
;
547 if (olen
< EXT4_FNAME_CRYPTO_DIGEST_SIZE
*2)
548 olen
= EXT4_FNAME_CRYPTO_DIGEST_SIZE
*2;
549 /* Allocated buffer can hold one more character to null-terminate the
551 crypto_str
->name
= kmalloc(olen
+1, GFP_NOFS
);
552 if (!(crypto_str
->name
))
558 * ext4_fname_crypto_free_buffer() -
560 * Frees the buffer allocated for crypto operation.
562 void ext4_fname_crypto_free_buffer(struct ext4_str
*crypto_str
)
566 kfree(crypto_str
->name
);
567 crypto_str
->name
= NULL
;
571 * ext4_fname_disk_to_usr() - converts a filename from disk space to user space
573 int _ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx
*ctx
,
574 const struct ext4_str
*iname
,
575 struct ext4_str
*oname
)
579 if (iname
->len
< 3) {
580 /*Check for . and .. */
581 if (iname
->name
[0] == '.' && iname
->name
[iname
->len
-1] == '.') {
582 oname
->name
[0] = '.';
583 oname
->name
[iname
->len
-1] = '.';
584 oname
->len
= iname
->len
;
588 if (ctx
->has_valid_key
)
589 return ext4_fname_decrypt(ctx
, iname
, oname
);
591 return ext4_fname_hash(ctx
, iname
, oname
);
594 int ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx
*ctx
,
595 const struct ext4_dir_entry_2
*de
,
596 struct ext4_str
*oname
)
598 struct ext4_str iname
= {.name
= (unsigned char *) de
->name
,
599 .len
= de
->name_len
};
601 return _ext4_fname_disk_to_usr(ctx
, &iname
, oname
);
606 * ext4_fname_usr_to_disk() - converts a filename from user space to disk space
608 int ext4_fname_usr_to_disk(struct ext4_fname_crypto_ctx
*ctx
,
609 const struct qstr
*iname
,
610 struct ext4_str
*oname
)
616 if (iname
->len
< 3) {
617 /*Check for . and .. */
618 if (iname
->name
[0] == '.' &&
619 iname
->name
[iname
->len
-1] == '.') {
620 oname
->name
[0] = '.';
621 oname
->name
[iname
->len
-1] = '.';
622 oname
->len
= iname
->len
;
626 if (ctx
->has_valid_key
) {
627 res
= ext4_fname_encrypt(ctx
, iname
, oname
);
630 /* Without a proper key, a user is not allowed to modify the filenames
631 * in a directory. Consequently, a user space name cannot be mapped to
632 * a disk-space name */
637 * Calculate the htree hash from a filename from user space
639 int ext4_fname_usr_to_hash(struct ext4_fname_crypto_ctx
*ctx
,
640 const struct qstr
*iname
,
641 struct dx_hash_info
*hinfo
)
643 struct ext4_str tmp
, tmp2
;
646 if (!ctx
|| !ctx
->has_valid_key
||
647 ((iname
->name
[0] == '.') &&
648 ((iname
->len
== 1) ||
649 ((iname
->name
[1] == '.') && (iname
->len
== 2))))) {
650 ext4fs_dirhash(iname
->name
, iname
->len
, hinfo
);
654 /* First encrypt the plaintext name */
655 ret
= ext4_fname_crypto_alloc_buffer(ctx
, iname
->len
, &tmp
);
659 ret
= ext4_fname_encrypt(ctx
, iname
, &tmp
);
663 tmp2
.len
= (4 * ((EXT4_FNAME_CRYPTO_DIGEST_SIZE
+ 2) / 3)) + 1;
664 tmp2
.name
= kmalloc(tmp2
.len
+ 1, GFP_KERNEL
);
665 if (tmp2
.name
== NULL
) {
670 ret
= ext4_fname_hash(ctx
, &tmp
, &tmp2
);
672 ext4fs_dirhash(tmp2
.name
, tmp2
.len
, hinfo
);
673 ext4_fname_crypto_free_buffer(&tmp2
);
675 ext4_fname_crypto_free_buffer(&tmp
);
680 * ext4_fname_disk_to_htree() - converts a filename from disk space to htree-access string
682 int ext4_fname_disk_to_hash(struct ext4_fname_crypto_ctx
*ctx
,
683 const struct ext4_dir_entry_2
*de
,
684 struct dx_hash_info
*hinfo
)
686 struct ext4_str iname
= {.name
= (unsigned char *) de
->name
,
687 .len
= de
->name_len
};
692 ((iname
.name
[0] == '.') &&
694 ((iname
.name
[1] == '.') && (iname
.len
== 2))))) {
695 ext4fs_dirhash(iname
.name
, iname
.len
, hinfo
);
699 tmp
.len
= (4 * ((EXT4_FNAME_CRYPTO_DIGEST_SIZE
+ 2) / 3)) + 1;
700 tmp
.name
= kmalloc(tmp
.len
+ 1, GFP_KERNEL
);
701 if (tmp
.name
== NULL
)
704 ret
= ext4_fname_hash(ctx
, &iname
, &tmp
);
706 ext4fs_dirhash(tmp
.name
, tmp
.len
, hinfo
);
707 ext4_fname_crypto_free_buffer(&tmp
);