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/list.h>
23 #include <linux/mempool.h>
24 #include <linux/random.h>
25 #include <linux/scatterlist.h>
26 #include <linux/spinlock_types.h>
29 #include "ext4_crypto.h"
33 * ext4_dir_crypt_complete() -
35 static void ext4_dir_crypt_complete(struct crypto_async_request *req, int res)
37 struct ext4_completion_result *ecr = req->data;
39 if (res == -EINPROGRESS)
42 complete(&ecr->completion);
45 bool ext4_valid_filenames_enc_mode(uint32_t mode)
47 return (mode == EXT4_ENCRYPTION_MODE_AES_256_CTS);
50 static unsigned max_name_len(struct inode *inode)
52 return S_ISLNK(inode->i_mode) ? inode->i_sb->s_blocksize :
57 * ext4_fname_encrypt() -
59 * This function encrypts the input filename, and returns the length of the
60 * ciphertext. Errors are returned as negative numbers. We trust the caller to
61 * allocate sufficient memory to oname string.
63 static int ext4_fname_encrypt(struct inode *inode,
64 const struct qstr *iname,
65 struct ext4_str *oname)
68 struct ablkcipher_request *req = NULL;
69 DECLARE_EXT4_COMPLETION_RESULT(ecr);
70 struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
71 struct crypto_ablkcipher *tfm = ci->ci_ctfm;
73 char iv[EXT4_CRYPTO_BLOCK_SIZE];
74 struct scatterlist src_sg, dst_sg;
75 int padding = 4 << (ci->ci_flags & EXT4_POLICY_FLAGS_PAD_MASK);
76 char *workbuf, buf[32], *alloc_buf = NULL;
77 unsigned lim = max_name_len(inode);
79 if (iname->len <= 0 || iname->len > lim)
82 ciphertext_len = (iname->len < EXT4_CRYPTO_BLOCK_SIZE) ?
83 EXT4_CRYPTO_BLOCK_SIZE : iname->len;
84 ciphertext_len = ext4_fname_crypto_round_up(ciphertext_len, padding);
85 ciphertext_len = (ciphertext_len > lim)
86 ? lim : ciphertext_len;
88 if (ciphertext_len <= sizeof(buf)) {
91 alloc_buf = kmalloc(ciphertext_len, GFP_NOFS);
97 /* Allocate request */
98 req = ablkcipher_request_alloc(tfm, GFP_NOFS);
101 KERN_ERR "%s: crypto_request_alloc() failed\n", __func__);
105 ablkcipher_request_set_callback(req,
106 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
107 ext4_dir_crypt_complete, &ecr);
110 memcpy(workbuf, iname->name, iname->len);
111 if (iname->len < ciphertext_len)
112 memset(workbuf + iname->len, 0, ciphertext_len - iname->len);
115 memset(iv, 0, EXT4_CRYPTO_BLOCK_SIZE);
117 /* Create encryption request */
118 sg_init_one(&src_sg, workbuf, ciphertext_len);
119 sg_init_one(&dst_sg, oname->name, ciphertext_len);
120 ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, ciphertext_len, iv);
121 res = crypto_ablkcipher_encrypt(req);
122 if (res == -EINPROGRESS || res == -EBUSY) {
123 wait_for_completion(&ecr.completion);
127 ablkcipher_request_free(req);
130 KERN_ERR "%s: Error (error code %d)\n", __func__, res);
132 oname->len = ciphertext_len;
137 * ext4_fname_decrypt()
138 * This function decrypts the input filename, and returns
139 * the length of the plaintext.
140 * Errors are returned as negative numbers.
141 * We trust the caller to allocate sufficient memory to oname string.
143 static int ext4_fname_decrypt(struct inode *inode,
144 const struct ext4_str *iname,
145 struct ext4_str *oname)
147 struct ext4_str tmp_in[2], tmp_out[1];
148 struct ablkcipher_request *req = NULL;
149 DECLARE_EXT4_COMPLETION_RESULT(ecr);
150 struct scatterlist src_sg, dst_sg;
151 struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
152 struct crypto_ablkcipher *tfm = ci->ci_ctfm;
154 char iv[EXT4_CRYPTO_BLOCK_SIZE];
155 unsigned lim = max_name_len(inode);
157 if (iname->len <= 0 || iname->len > lim)
160 tmp_in[0].name = iname->name;
161 tmp_in[0].len = iname->len;
162 tmp_out[0].name = oname->name;
164 /* Allocate request */
165 req = ablkcipher_request_alloc(tfm, GFP_NOFS);
168 KERN_ERR "%s: crypto_request_alloc() failed\n", __func__);
171 ablkcipher_request_set_callback(req,
172 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
173 ext4_dir_crypt_complete, &ecr);
176 memset(iv, 0, EXT4_CRYPTO_BLOCK_SIZE);
178 /* Create encryption request */
179 sg_init_one(&src_sg, iname->name, iname->len);
180 sg_init_one(&dst_sg, oname->name, oname->len);
181 ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
182 res = crypto_ablkcipher_decrypt(req);
183 if (res == -EINPROGRESS || res == -EBUSY) {
184 wait_for_completion(&ecr.completion);
187 ablkcipher_request_free(req);
190 KERN_ERR "%s: Error in ext4_fname_encrypt (error code %d)\n",
195 oname->len = strnlen(oname->name, iname->len);
199 static const char *lookup_table =
200 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
203 * ext4_fname_encode_digest() -
205 * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
206 * The encoded string is roughly 4/3 times the size of the input string.
208 static int digest_encode(const char *src, int len, char *dst)
210 int i = 0, bits = 0, ac = 0;
214 ac += (((unsigned char) src[i]) << bits);
217 *cp++ = lookup_table[ac & 0x3f];
224 *cp++ = lookup_table[ac & 0x3f];
228 static int digest_decode(const char *src, int len, char *dst)
230 int i = 0, bits = 0, ac = 0;
235 p = strchr(lookup_table, src[i]);
236 if (p == NULL || src[i] == 0)
238 ac += (p - lookup_table) << bits;
253 * ext4_fname_crypto_round_up() -
255 * Return: The next multiple of block size
257 u32 ext4_fname_crypto_round_up(u32 size, u32 blksize)
259 return ((size+blksize-1)/blksize)*blksize;
262 unsigned ext4_fname_encrypted_size(struct inode *inode, u32 ilen)
264 struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
268 padding = 4 << (ci->ci_flags & EXT4_POLICY_FLAGS_PAD_MASK);
269 if (ilen < EXT4_CRYPTO_BLOCK_SIZE)
270 ilen = EXT4_CRYPTO_BLOCK_SIZE;
271 return ext4_fname_crypto_round_up(ilen, padding);
275 * ext4_fname_crypto_alloc_buffer() -
277 * Allocates an output buffer that is sufficient for the crypto operation
278 * specified by the context and the direction.
280 int ext4_fname_crypto_alloc_buffer(struct inode *inode,
281 u32 ilen, struct ext4_str *crypto_str)
283 unsigned int olen = ext4_fname_encrypted_size(inode, ilen);
285 crypto_str->len = olen;
286 if (olen < EXT4_FNAME_CRYPTO_DIGEST_SIZE*2)
287 olen = EXT4_FNAME_CRYPTO_DIGEST_SIZE*2;
288 /* Allocated buffer can hold one more character to null-terminate the
290 crypto_str->name = kmalloc(olen+1, GFP_NOFS);
291 if (!(crypto_str->name))
297 * ext4_fname_crypto_free_buffer() -
299 * Frees the buffer allocated for crypto operation.
301 void ext4_fname_crypto_free_buffer(struct ext4_str *crypto_str)
305 kfree(crypto_str->name);
306 crypto_str->name = NULL;
310 * ext4_fname_disk_to_usr() - converts a filename from disk space to user space
312 int _ext4_fname_disk_to_usr(struct inode *inode,
313 struct dx_hash_info *hinfo,
314 const struct ext4_str *iname,
315 struct ext4_str *oname)
320 if (iname->len < 3) {
321 /*Check for . and .. */
322 if (iname->name[0] == '.' && iname->name[iname->len-1] == '.') {
323 oname->name[0] = '.';
324 oname->name[iname->len-1] = '.';
325 oname->len = iname->len;
329 if (iname->len < EXT4_CRYPTO_BLOCK_SIZE) {
330 EXT4_ERROR_INODE(inode, "encrypted inode too small");
333 if (EXT4_I(inode)->i_crypt_info)
334 return ext4_fname_decrypt(inode, iname, oname);
336 if (iname->len <= EXT4_FNAME_CRYPTO_DIGEST_SIZE) {
337 ret = digest_encode(iname->name, iname->len, oname->name);
342 memcpy(buf, &hinfo->hash, 4);
343 memcpy(buf+4, &hinfo->minor_hash, 4);
346 memcpy(buf + 8, iname->name + iname->len - 16, 16);
347 oname->name[0] = '_';
348 ret = digest_encode(buf, 24, oname->name+1);
349 oname->len = ret + 1;
353 int ext4_fname_disk_to_usr(struct inode *inode,
354 struct dx_hash_info *hinfo,
355 const struct ext4_dir_entry_2 *de,
356 struct ext4_str *oname)
358 struct ext4_str iname = {.name = (unsigned char *) de->name,
359 .len = de->name_len };
361 return _ext4_fname_disk_to_usr(inode, hinfo, &iname, oname);
366 * ext4_fname_usr_to_disk() - converts a filename from user space to disk space
368 int ext4_fname_usr_to_disk(struct inode *inode,
369 const struct qstr *iname,
370 struct ext4_str *oname)
373 struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
375 if (iname->len < 3) {
376 /*Check for . and .. */
377 if (iname->name[0] == '.' &&
378 iname->name[iname->len-1] == '.') {
379 oname->name[0] = '.';
380 oname->name[iname->len-1] = '.';
381 oname->len = iname->len;
386 res = ext4_fname_encrypt(inode, iname, oname);
389 /* Without a proper key, a user is not allowed to modify the filenames
390 * in a directory. Consequently, a user space name cannot be mapped to
391 * a disk-space name */
395 int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
396 int lookup, struct ext4_filename *fname)
398 struct ext4_crypt_info *ci;
399 int ret = 0, bigname = 0;
401 memset(fname, 0, sizeof(struct ext4_filename));
402 fname->usr_fname = iname;
404 if (!ext4_encrypted_inode(dir) ||
405 ((iname->name[0] == '.') &&
406 ((iname->len == 1) ||
407 ((iname->name[1] == '.') && (iname->len == 2))))) {
408 fname->disk_name.name = (unsigned char *) iname->name;
409 fname->disk_name.len = iname->len;
412 ret = ext4_get_encryption_info(dir);
415 ci = EXT4_I(dir)->i_crypt_info;
417 ret = ext4_fname_crypto_alloc_buffer(dir, iname->len,
421 ret = ext4_fname_encrypt(dir, iname, &fname->crypto_buf);
424 fname->disk_name.name = fname->crypto_buf.name;
425 fname->disk_name.len = fname->crypto_buf.len;
431 /* We don't have the key and we are doing a lookup; decode the
434 if (iname->name[0] == '_')
436 if ((bigname && (iname->len != 33)) ||
437 (!bigname && (iname->len > 43)))
440 fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
441 if (fname->crypto_buf.name == NULL)
443 ret = digest_decode(iname->name + bigname, iname->len - bigname,
444 fname->crypto_buf.name);
449 fname->crypto_buf.len = ret;
451 memcpy(&fname->hinfo.hash, fname->crypto_buf.name, 4);
452 memcpy(&fname->hinfo.minor_hash, fname->crypto_buf.name + 4, 4);
454 fname->disk_name.name = fname->crypto_buf.name;
455 fname->disk_name.len = fname->crypto_buf.len;
459 kfree(fname->crypto_buf.name);
460 fname->crypto_buf.name = NULL;
464 void ext4_fname_free_filename(struct ext4_filename *fname)
466 kfree(fname->crypto_buf.name);
467 fname->crypto_buf.name = NULL;
468 fname->usr_fname = NULL;
469 fname->disk_name.name = NULL;