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);
51 static unsigned max_name_len(struct inode *inode)
53 return S_ISLNK(inode->i_mode) ? inode->i_sb->s_blocksize :
58 * ext4_fname_encrypt() -
60 * This function encrypts the input filename, and returns the length of the
61 * ciphertext. Errors are returned as negative numbers. We trust the caller to
62 * allocate sufficient memory to oname string.
64 static int ext4_fname_encrypt(struct inode *inode,
65 const struct qstr *iname,
66 struct ext4_str *oname)
69 struct ablkcipher_request *req = NULL;
70 DECLARE_EXT4_COMPLETION_RESULT(ecr);
71 struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
72 struct crypto_ablkcipher *tfm = ci->ci_ctfm;
74 char iv[EXT4_CRYPTO_BLOCK_SIZE];
75 struct scatterlist src_sg, dst_sg;
76 int padding = 4 << (ci->ci_flags & EXT4_POLICY_FLAGS_PAD_MASK);
77 char *workbuf, buf[32], *alloc_buf = NULL;
78 unsigned lim = max_name_len(inode);
80 if (iname->len <= 0 || iname->len > lim)
83 ciphertext_len = (iname->len < EXT4_CRYPTO_BLOCK_SIZE) ?
84 EXT4_CRYPTO_BLOCK_SIZE : iname->len;
85 ciphertext_len = ext4_fname_crypto_round_up(ciphertext_len, padding);
86 ciphertext_len = (ciphertext_len > lim)
87 ? lim : ciphertext_len;
89 if (ciphertext_len <= sizeof(buf)) {
92 alloc_buf = kmalloc(ciphertext_len, GFP_NOFS);
98 /* Allocate request */
99 req = ablkcipher_request_alloc(tfm, GFP_NOFS);
102 KERN_ERR "%s: crypto_request_alloc() failed\n", __func__);
106 ablkcipher_request_set_callback(req,
107 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
108 ext4_dir_crypt_complete, &ecr);
111 memcpy(workbuf, iname->name, iname->len);
112 if (iname->len < ciphertext_len)
113 memset(workbuf + iname->len, 0, ciphertext_len - iname->len);
116 memset(iv, 0, EXT4_CRYPTO_BLOCK_SIZE);
118 /* Create encryption request */
119 sg_init_one(&src_sg, workbuf, ciphertext_len);
120 sg_init_one(&dst_sg, oname->name, ciphertext_len);
121 ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, ciphertext_len, iv);
122 res = crypto_ablkcipher_encrypt(req);
123 if (res == -EINPROGRESS || res == -EBUSY) {
124 BUG_ON(req->base.data != &ecr);
125 wait_for_completion(&ecr.completion);
129 ablkcipher_request_free(req);
132 KERN_ERR "%s: Error (error code %d)\n", __func__, res);
134 oname->len = ciphertext_len;
139 * ext4_fname_decrypt()
140 * This function decrypts the input filename, and returns
141 * the length of the plaintext.
142 * Errors are returned as negative numbers.
143 * We trust the caller to allocate sufficient memory to oname string.
145 static int ext4_fname_decrypt(struct inode *inode,
146 const struct ext4_str *iname,
147 struct ext4_str *oname)
149 struct ext4_str tmp_in[2], tmp_out[1];
150 struct ablkcipher_request *req = NULL;
151 DECLARE_EXT4_COMPLETION_RESULT(ecr);
152 struct scatterlist src_sg, dst_sg;
153 struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
154 struct crypto_ablkcipher *tfm = ci->ci_ctfm;
156 char iv[EXT4_CRYPTO_BLOCK_SIZE];
157 unsigned lim = max_name_len(inode);
159 if (iname->len <= 0 || iname->len > lim)
162 tmp_in[0].name = iname->name;
163 tmp_in[0].len = iname->len;
164 tmp_out[0].name = oname->name;
166 /* Allocate request */
167 req = ablkcipher_request_alloc(tfm, GFP_NOFS);
170 KERN_ERR "%s: crypto_request_alloc() failed\n", __func__);
173 ablkcipher_request_set_callback(req,
174 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
175 ext4_dir_crypt_complete, &ecr);
178 memset(iv, 0, EXT4_CRYPTO_BLOCK_SIZE);
180 /* Create encryption request */
181 sg_init_one(&src_sg, iname->name, iname->len);
182 sg_init_one(&dst_sg, oname->name, oname->len);
183 ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
184 res = crypto_ablkcipher_decrypt(req);
185 if (res == -EINPROGRESS || res == -EBUSY) {
186 BUG_ON(req->base.data != &ecr);
187 wait_for_completion(&ecr.completion);
190 ablkcipher_request_free(req);
193 KERN_ERR "%s: Error in ext4_fname_encrypt (error code %d)\n",
198 oname->len = strnlen(oname->name, iname->len);
202 static const char *lookup_table =
203 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
206 * ext4_fname_encode_digest() -
208 * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
209 * The encoded string is roughly 4/3 times the size of the input string.
211 static int digest_encode(const char *src, int len, char *dst)
213 int i = 0, bits = 0, ac = 0;
217 ac += (((unsigned char) src[i]) << bits);
220 *cp++ = lookup_table[ac & 0x3f];
227 *cp++ = lookup_table[ac & 0x3f];
231 static int digest_decode(const char *src, int len, char *dst)
233 int i = 0, bits = 0, ac = 0;
238 p = strchr(lookup_table, src[i]);
239 if (p == NULL || src[i] == 0)
241 ac += (p - lookup_table) << bits;
256 * ext4_fname_crypto_round_up() -
258 * Return: The next multiple of block size
260 u32 ext4_fname_crypto_round_up(u32 size, u32 blksize)
262 return ((size+blksize-1)/blksize)*blksize;
266 * ext4_fname_crypto_alloc_obuff() -
268 * Allocates an output buffer that is sufficient for the crypto operation
269 * specified by the context and the direction.
271 int ext4_fname_crypto_alloc_buffer(struct inode *inode,
272 u32 ilen, struct ext4_str *crypto_str)
276 struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
279 padding = 4 << (ci->ci_flags & EXT4_POLICY_FLAGS_PAD_MASK);
280 if (padding < EXT4_CRYPTO_BLOCK_SIZE)
281 padding = EXT4_CRYPTO_BLOCK_SIZE;
282 olen = ext4_fname_crypto_round_up(ilen, padding);
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 (EXT4_I(inode)->i_crypt_info)
328 return ext4_fname_decrypt(inode, iname, oname);
330 if (iname->len <= EXT4_FNAME_CRYPTO_DIGEST_SIZE) {
331 ret = digest_encode(iname->name, iname->len, oname->name);
336 memcpy(buf, &hinfo->hash, 4);
337 memcpy(buf+4, &hinfo->minor_hash, 4);
340 memcpy(buf + 8, iname->name + iname->len - 16, 16);
341 oname->name[0] = '_';
342 ret = digest_encode(buf, 24, oname->name+1);
343 oname->len = ret + 1;
347 int ext4_fname_disk_to_usr(struct inode *inode,
348 struct dx_hash_info *hinfo,
349 const struct ext4_dir_entry_2 *de,
350 struct ext4_str *oname)
352 struct ext4_str iname = {.name = (unsigned char *) de->name,
353 .len = de->name_len };
355 return _ext4_fname_disk_to_usr(inode, hinfo, &iname, oname);
360 * ext4_fname_usr_to_disk() - converts a filename from user space to disk space
362 int ext4_fname_usr_to_disk(struct inode *inode,
363 const struct qstr *iname,
364 struct ext4_str *oname)
367 struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
369 if (iname->len < 3) {
370 /*Check for . and .. */
371 if (iname->name[0] == '.' &&
372 iname->name[iname->len-1] == '.') {
373 oname->name[0] = '.';
374 oname->name[iname->len-1] = '.';
375 oname->len = iname->len;
380 res = ext4_fname_encrypt(inode, iname, oname);
383 /* Without a proper key, a user is not allowed to modify the filenames
384 * in a directory. Consequently, a user space name cannot be mapped to
385 * a disk-space name */
389 int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
390 int lookup, struct ext4_filename *fname)
392 struct ext4_crypt_info *ci;
393 int ret = 0, bigname = 0;
395 memset(fname, 0, sizeof(struct ext4_filename));
396 fname->usr_fname = iname;
398 if (!ext4_encrypted_inode(dir) ||
399 ((iname->name[0] == '.') &&
400 ((iname->len == 1) ||
401 ((iname->name[1] == '.') && (iname->len == 2))))) {
402 fname->disk_name.name = (unsigned char *) iname->name;
403 fname->disk_name.len = iname->len;
406 ret = ext4_get_encryption_info(dir);
409 ci = EXT4_I(dir)->i_crypt_info;
411 ret = ext4_fname_crypto_alloc_buffer(dir, iname->len,
415 ret = ext4_fname_encrypt(dir, iname, &fname->crypto_buf);
418 fname->disk_name.name = fname->crypto_buf.name;
419 fname->disk_name.len = fname->crypto_buf.len;
425 /* We don't have the key and we are doing a lookup; decode the
428 if (iname->name[0] == '_')
430 if ((bigname && (iname->len != 33)) ||
431 (!bigname && (iname->len > 43)))
434 fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
435 if (fname->crypto_buf.name == NULL)
437 ret = digest_decode(iname->name + bigname, iname->len - bigname,
438 fname->crypto_buf.name);
443 fname->crypto_buf.len = ret;
445 memcpy(&fname->hinfo.hash, fname->crypto_buf.name, 4);
446 memcpy(&fname->hinfo.minor_hash, fname->crypto_buf.name + 4, 4);
448 fname->disk_name.name = fname->crypto_buf.name;
449 fname->disk_name.len = fname->crypto_buf.len;
453 kfree(fname->crypto_buf.name);
454 fname->crypto_buf.name = NULL;
458 void ext4_fname_free_filename(struct ext4_filename *fname)
460 kfree(fname->crypto_buf.name);
461 fname->crypto_buf.name = NULL;
462 fname->usr_fname = NULL;
463 fname->disk_name.name = NULL;