1 /*
2  * linux/fs/f2fs/crypto_fname.c
3  *
4  * Copied from linux/fs/ext4/crypto.c
5  *
6  * Copyright (C) 2015, Google, Inc.
7  * Copyright (C) 2015, Motorola Mobility
8  *
9  * This contains functions for filename crypto management in f2fs
10  *
11  * Written by Uday Savagaonkar, 2014.
12  *
13  * Adjust f2fs dentry structure
14  *	Jaegeuk Kim, 2015.
15  *
16  * This has not yet undergone a rigorous security audit.
17  */
18 #include <crypto/hash.h>
19 #include <crypto/sha.h>
20 #include <keys/encrypted-type.h>
21 #include <keys/user-type.h>
22 #include <linux/crypto.h>
23 #include <linux/gfp.h>
24 #include <linux/kernel.h>
25 #include <linux/key.h>
26 #include <linux/list.h>
27 #include <linux/mempool.h>
28 #include <linux/random.h>
29 #include <linux/scatterlist.h>
30 #include <linux/spinlock_types.h>
31 #include <linux/f2fs_fs.h>
32 #include <linux/ratelimit.h>
33 
34 #include "f2fs.h"
35 #include "f2fs_crypto.h"
36 #include "xattr.h"
37 
38 /**
39  * f2fs_dir_crypt_complete() -
40  */
f2fs_dir_crypt_complete(struct crypto_async_request * req,int res)41 static void f2fs_dir_crypt_complete(struct crypto_async_request *req, int res)
42 {
43 	struct f2fs_completion_result *ecr = req->data;
44 
45 	if (res == -EINPROGRESS)
46 		return;
47 	ecr->res = res;
48 	complete(&ecr->completion);
49 }
50 
f2fs_valid_filenames_enc_mode(uint32_t mode)51 bool f2fs_valid_filenames_enc_mode(uint32_t mode)
52 {
53 	return (mode == F2FS_ENCRYPTION_MODE_AES_256_CTS);
54 }
55 
max_name_len(struct inode * inode)56 static unsigned max_name_len(struct inode *inode)
57 {
58 	return S_ISLNK(inode->i_mode) ? inode->i_sb->s_blocksize :
59 					F2FS_NAME_LEN;
60 }
61 
62 /**
63  * f2fs_fname_encrypt() -
64  *
65  * This function encrypts the input filename, and returns the length of the
66  * ciphertext. Errors are returned as negative numbers.  We trust the caller to
67  * allocate sufficient memory to oname string.
68  */
f2fs_fname_encrypt(struct inode * inode,const struct qstr * iname,struct f2fs_str * oname)69 static int f2fs_fname_encrypt(struct inode *inode,
70 			const struct qstr *iname, struct f2fs_str *oname)
71 {
72 	u32 ciphertext_len;
73 	struct ablkcipher_request *req = NULL;
74 	DECLARE_F2FS_COMPLETION_RESULT(ecr);
75 	struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
76 	struct crypto_ablkcipher *tfm = ci->ci_ctfm;
77 	int res = 0;
78 	char iv[F2FS_CRYPTO_BLOCK_SIZE];
79 	struct scatterlist src_sg, dst_sg;
80 	int padding = 4 << (ci->ci_flags & F2FS_POLICY_FLAGS_PAD_MASK);
81 	char *workbuf, buf[32], *alloc_buf = NULL;
82 	unsigned lim = max_name_len(inode);
83 
84 	if (iname->len <= 0 || iname->len > lim)
85 		return -EIO;
86 
87 	ciphertext_len = (iname->len < F2FS_CRYPTO_BLOCK_SIZE) ?
88 		F2FS_CRYPTO_BLOCK_SIZE : iname->len;
89 	ciphertext_len = f2fs_fname_crypto_round_up(ciphertext_len, padding);
90 	ciphertext_len = (ciphertext_len > lim) ? lim : ciphertext_len;
91 
92 	if (ciphertext_len <= sizeof(buf)) {
93 		workbuf = buf;
94 	} else {
95 		alloc_buf = kmalloc(ciphertext_len, GFP_NOFS);
96 		if (!alloc_buf)
97 			return -ENOMEM;
98 		workbuf = alloc_buf;
99 	}
100 
101 	/* Allocate request */
102 	req = ablkcipher_request_alloc(tfm, GFP_NOFS);
103 	if (!req) {
104 		printk_ratelimited(KERN_ERR
105 			"%s: crypto_request_alloc() failed\n", __func__);
106 		kfree(alloc_buf);
107 		return -ENOMEM;
108 	}
109 	ablkcipher_request_set_callback(req,
110 			CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
111 			f2fs_dir_crypt_complete, &ecr);
112 
113 	/* Copy the input */
114 	memcpy(workbuf, iname->name, iname->len);
115 	if (iname->len < ciphertext_len)
116 		memset(workbuf + iname->len, 0, ciphertext_len - iname->len);
117 
118 	/* Initialize IV */
119 	memset(iv, 0, F2FS_CRYPTO_BLOCK_SIZE);
120 
121 	/* Create encryption request */
122 	sg_init_one(&src_sg, workbuf, ciphertext_len);
123 	sg_init_one(&dst_sg, oname->name, ciphertext_len);
124 	ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, ciphertext_len, iv);
125 	res = crypto_ablkcipher_encrypt(req);
126 	if (res == -EINPROGRESS || res == -EBUSY) {
127 		BUG_ON(req->base.data != &ecr);
128 		wait_for_completion(&ecr.completion);
129 		res = ecr.res;
130 	}
131 	kfree(alloc_buf);
132 	ablkcipher_request_free(req);
133 	if (res < 0) {
134 		printk_ratelimited(KERN_ERR
135 				"%s: Error (error code %d)\n", __func__, res);
136 	}
137 	oname->len = ciphertext_len;
138 	return res;
139 }
140 
141 /*
142  * f2fs_fname_decrypt()
143  *	This function decrypts the input filename, and returns
144  *	the length of the plaintext.
145  *	Errors are returned as negative numbers.
146  *	We trust the caller to allocate sufficient memory to oname string.
147  */
f2fs_fname_decrypt(struct inode * inode,const struct f2fs_str * iname,struct f2fs_str * oname)148 static int f2fs_fname_decrypt(struct inode *inode,
149 			const struct f2fs_str *iname, struct f2fs_str *oname)
150 {
151 	struct ablkcipher_request *req = NULL;
152 	DECLARE_F2FS_COMPLETION_RESULT(ecr);
153 	struct scatterlist src_sg, dst_sg;
154 	struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
155 	struct crypto_ablkcipher *tfm = ci->ci_ctfm;
156 	int res = 0;
157 	char iv[F2FS_CRYPTO_BLOCK_SIZE];
158 	unsigned lim = max_name_len(inode);
159 
160 	if (iname->len <= 0 || iname->len > lim)
161 		return -EIO;
162 
163 	/* Allocate request */
164 	req = ablkcipher_request_alloc(tfm, GFP_NOFS);
165 	if (!req) {
166 		printk_ratelimited(KERN_ERR
167 			"%s: crypto_request_alloc() failed\n",  __func__);
168 		return -ENOMEM;
169 	}
170 	ablkcipher_request_set_callback(req,
171 		CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
172 		f2fs_dir_crypt_complete, &ecr);
173 
174 	/* Initialize IV */
175 	memset(iv, 0, F2FS_CRYPTO_BLOCK_SIZE);
176 
177 	/* Create decryption request */
178 	sg_init_one(&src_sg, iname->name, iname->len);
179 	sg_init_one(&dst_sg, oname->name, oname->len);
180 	ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
181 	res = crypto_ablkcipher_decrypt(req);
182 	if (res == -EINPROGRESS || res == -EBUSY) {
183 		BUG_ON(req->base.data != &ecr);
184 		wait_for_completion(&ecr.completion);
185 		res = ecr.res;
186 	}
187 	ablkcipher_request_free(req);
188 	if (res < 0) {
189 		printk_ratelimited(KERN_ERR
190 			"%s: Error in f2fs_fname_decrypt (error code %d)\n",
191 			__func__, res);
192 		return res;
193 	}
194 
195 	oname->len = strnlen(oname->name, iname->len);
196 	return oname->len;
197 }
198 
199 static const char *lookup_table =
200 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
201 
202 /**
203  * f2fs_fname_encode_digest() -
204  *
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.
207  */
digest_encode(const char * src,int len,char * dst)208 static int digest_encode(const char *src, int len, char *dst)
209 {
210 	int i = 0, bits = 0, ac = 0;
211 	char *cp = dst;
212 
213 	while (i < len) {
214 		ac += (((unsigned char) src[i]) << bits);
215 		bits += 8;
216 		do {
217 			*cp++ = lookup_table[ac & 0x3f];
218 			ac >>= 6;
219 			bits -= 6;
220 		} while (bits >= 6);
221 		i++;
222 	}
223 	if (bits)
224 		*cp++ = lookup_table[ac & 0x3f];
225 	return cp - dst;
226 }
227 
digest_decode(const char * src,int len,char * dst)228 static int digest_decode(const char *src, int len, char *dst)
229 {
230 	int i = 0, bits = 0, ac = 0;
231 	const char *p;
232 	char *cp = dst;
233 
234 	while (i < len) {
235 		p = strchr(lookup_table, src[i]);
236 		if (p == NULL || src[i] == 0)
237 			return -2;
238 		ac += (p - lookup_table) << bits;
239 		bits += 6;
240 		if (bits >= 8) {
241 			*cp++ = ac & 0xff;
242 			ac >>= 8;
243 			bits -= 8;
244 		}
245 		i++;
246 	}
247 	if (ac)
248 		return -1;
249 	return cp - dst;
250 }
251 
252 /**
253  * f2fs_fname_crypto_round_up() -
254  *
255  * Return: The next multiple of block size
256  */
f2fs_fname_crypto_round_up(u32 size,u32 blksize)257 u32 f2fs_fname_crypto_round_up(u32 size, u32 blksize)
258 {
259 	return ((size + blksize - 1) / blksize) * blksize;
260 }
261 
262 /**
263  * f2fs_fname_crypto_alloc_obuff() -
264  *
265  * Allocates an output buffer that is sufficient for the crypto operation
266  * specified by the context and the direction.
267  */
f2fs_fname_crypto_alloc_buffer(struct inode * inode,u32 ilen,struct f2fs_str * crypto_str)268 int f2fs_fname_crypto_alloc_buffer(struct inode *inode,
269 				   u32 ilen, struct f2fs_str *crypto_str)
270 {
271 	unsigned int olen;
272 	int padding = 16;
273 	struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
274 
275 	if (ci)
276 		padding = 4 << (ci->ci_flags & F2FS_POLICY_FLAGS_PAD_MASK);
277 	if (padding < F2FS_CRYPTO_BLOCK_SIZE)
278 		padding = F2FS_CRYPTO_BLOCK_SIZE;
279 	olen = f2fs_fname_crypto_round_up(ilen, padding);
280 	crypto_str->len = olen;
281 	if (olen < F2FS_FNAME_CRYPTO_DIGEST_SIZE * 2)
282 		olen = F2FS_FNAME_CRYPTO_DIGEST_SIZE * 2;
283 	/* Allocated buffer can hold one more character to null-terminate the
284 	 * string */
285 	crypto_str->name = kmalloc(olen + 1, GFP_NOFS);
286 	if (!(crypto_str->name))
287 		return -ENOMEM;
288 	return 0;
289 }
290 
291 /**
292  * f2fs_fname_crypto_free_buffer() -
293  *
294  * Frees the buffer allocated for crypto operation.
295  */
f2fs_fname_crypto_free_buffer(struct f2fs_str * crypto_str)296 void f2fs_fname_crypto_free_buffer(struct f2fs_str *crypto_str)
297 {
298 	if (!crypto_str)
299 		return;
300 	kfree(crypto_str->name);
301 	crypto_str->name = NULL;
302 }
303 
304 /**
305  * f2fs_fname_disk_to_usr() - converts a filename from disk space to user space
306  */
f2fs_fname_disk_to_usr(struct inode * inode,f2fs_hash_t * hash,const struct f2fs_str * iname,struct f2fs_str * oname)307 int f2fs_fname_disk_to_usr(struct inode *inode,
308 			f2fs_hash_t *hash,
309 			const struct f2fs_str *iname,
310 			struct f2fs_str *oname)
311 {
312 	const struct qstr qname = FSTR_TO_QSTR(iname);
313 	char buf[24];
314 	int ret;
315 
316 	if (is_dot_dotdot(&qname)) {
317 		oname->name[0] = '.';
318 		oname->name[iname->len - 1] = '.';
319 		oname->len = iname->len;
320 		return oname->len;
321 	}
322 
323 	if (F2FS_I(inode)->i_crypt_info)
324 		return f2fs_fname_decrypt(inode, iname, oname);
325 
326 	if (iname->len <= F2FS_FNAME_CRYPTO_DIGEST_SIZE) {
327 		ret = digest_encode(iname->name, iname->len, oname->name);
328 		oname->len = ret;
329 		return ret;
330 	}
331 	if (hash) {
332 		memcpy(buf, hash, 4);
333 		memset(buf + 4, 0, 4);
334 	} else
335 		memset(buf, 0, 8);
336 	memcpy(buf + 8, iname->name + iname->len - 16, 16);
337 	oname->name[0] = '_';
338 	ret = digest_encode(buf, 24, oname->name + 1);
339 	oname->len = ret + 1;
340 	return ret + 1;
341 }
342 
343 /**
344  * f2fs_fname_usr_to_disk() - converts a filename from user space to disk space
345  */
f2fs_fname_usr_to_disk(struct inode * inode,const struct qstr * iname,struct f2fs_str * oname)346 int f2fs_fname_usr_to_disk(struct inode *inode,
347 			const struct qstr *iname,
348 			struct f2fs_str *oname)
349 {
350 	int res;
351 	struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
352 
353 	if (is_dot_dotdot(iname)) {
354 		oname->name[0] = '.';
355 		oname->name[iname->len - 1] = '.';
356 		oname->len = iname->len;
357 		return oname->len;
358 	}
359 
360 	if (ci) {
361 		res = f2fs_fname_encrypt(inode, iname, oname);
362 		return res;
363 	}
364 	/* Without a proper key, a user is not allowed to modify the filenames
365 	 * in a directory. Consequently, a user space name cannot be mapped to
366 	 * a disk-space name */
367 	return -EACCES;
368 }
369 
f2fs_fname_setup_filename(struct inode * dir,const struct qstr * iname,int lookup,struct f2fs_filename * fname)370 int f2fs_fname_setup_filename(struct inode *dir, const struct qstr *iname,
371 			      int lookup, struct f2fs_filename *fname)
372 {
373 	struct f2fs_crypt_info *ci;
374 	int ret = 0, bigname = 0;
375 
376 	memset(fname, 0, sizeof(struct f2fs_filename));
377 	fname->usr_fname = iname;
378 
379 	if (!f2fs_encrypted_inode(dir) || is_dot_dotdot(iname)) {
380 		fname->disk_name.name = (unsigned char *)iname->name;
381 		fname->disk_name.len = iname->len;
382 		return 0;
383 	}
384 	ret = f2fs_get_encryption_info(dir);
385 	if (ret)
386 		return ret;
387 	ci = F2FS_I(dir)->i_crypt_info;
388 	if (ci) {
389 		ret = f2fs_fname_crypto_alloc_buffer(dir, iname->len,
390 						     &fname->crypto_buf);
391 		if (ret < 0)
392 			return ret;
393 		ret = f2fs_fname_encrypt(dir, iname, &fname->crypto_buf);
394 		if (ret < 0)
395 			goto errout;
396 		fname->disk_name.name = fname->crypto_buf.name;
397 		fname->disk_name.len = fname->crypto_buf.len;
398 		return 0;
399 	}
400 	if (!lookup)
401 		return -EACCES;
402 
403 	/* We don't have the key and we are doing a lookup; decode the
404 	 * user-supplied name
405 	 */
406 	if (iname->name[0] == '_')
407 		bigname = 1;
408 	if ((bigname && (iname->len != 33)) ||
409 	    (!bigname && (iname->len > 43)))
410 		return -ENOENT;
411 
412 	fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
413 	if (fname->crypto_buf.name == NULL)
414 		return -ENOMEM;
415 	ret = digest_decode(iname->name + bigname, iname->len - bigname,
416 				fname->crypto_buf.name);
417 	if (ret < 0) {
418 		ret = -ENOENT;
419 		goto errout;
420 	}
421 	fname->crypto_buf.len = ret;
422 	if (bigname) {
423 		memcpy(&fname->hash, fname->crypto_buf.name, 4);
424 	} else {
425 		fname->disk_name.name = fname->crypto_buf.name;
426 		fname->disk_name.len = fname->crypto_buf.len;
427 	}
428 	return 0;
429 errout:
430 	f2fs_fname_crypto_free_buffer(&fname->crypto_buf);
431 	return ret;
432 }
433 
f2fs_fname_free_filename(struct f2fs_filename * fname)434 void f2fs_fname_free_filename(struct f2fs_filename *fname)
435 {
436 	kfree(fname->crypto_buf.name);
437 	fname->crypto_buf.name = NULL;
438 	fname->usr_fname = NULL;
439 	fname->disk_name.name = NULL;
440 }
441