Commit | Line | Data |
---|---|---|
6b3bd08f JK |
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 | */ | |
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 | ||
51 | bool f2fs_valid_filenames_enc_mode(uint32_t mode) | |
52 | { | |
53 | return (mode == F2FS_ENCRYPTION_MODE_AES_256_CTS); | |
54 | } | |
55 | ||
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 | */ | |
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) { | |
6b3bd08f JK |
127 | wait_for_completion(&ecr.completion); |
128 | res = ecr.res; | |
129 | } | |
130 | kfree(alloc_buf); | |
131 | ablkcipher_request_free(req); | |
132 | if (res < 0) { | |
133 | printk_ratelimited(KERN_ERR | |
134 | "%s: Error (error code %d)\n", __func__, res); | |
135 | } | |
136 | oname->len = ciphertext_len; | |
137 | return res; | |
138 | } | |
139 | ||
140 | /* | |
141 | * f2fs_fname_decrypt() | |
142 | * This function decrypts the input filename, and returns | |
143 | * the length of the plaintext. | |
144 | * Errors are returned as negative numbers. | |
145 | * We trust the caller to allocate sufficient memory to oname string. | |
146 | */ | |
147 | static int f2fs_fname_decrypt(struct inode *inode, | |
148 | const struct f2fs_str *iname, struct f2fs_str *oname) | |
149 | { | |
150 | struct ablkcipher_request *req = NULL; | |
151 | DECLARE_F2FS_COMPLETION_RESULT(ecr); | |
152 | struct scatterlist src_sg, dst_sg; | |
153 | struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info; | |
154 | struct crypto_ablkcipher *tfm = ci->ci_ctfm; | |
155 | int res = 0; | |
156 | char iv[F2FS_CRYPTO_BLOCK_SIZE]; | |
157 | unsigned lim = max_name_len(inode); | |
158 | ||
159 | if (iname->len <= 0 || iname->len > lim) | |
160 | return -EIO; | |
161 | ||
162 | /* Allocate request */ | |
163 | req = ablkcipher_request_alloc(tfm, GFP_NOFS); | |
164 | if (!req) { | |
165 | printk_ratelimited(KERN_ERR | |
166 | "%s: crypto_request_alloc() failed\n", __func__); | |
167 | return -ENOMEM; | |
168 | } | |
169 | ablkcipher_request_set_callback(req, | |
170 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, | |
171 | f2fs_dir_crypt_complete, &ecr); | |
172 | ||
173 | /* Initialize IV */ | |
174 | memset(iv, 0, F2FS_CRYPTO_BLOCK_SIZE); | |
175 | ||
176 | /* Create decryption request */ | |
177 | sg_init_one(&src_sg, iname->name, iname->len); | |
178 | sg_init_one(&dst_sg, oname->name, oname->len); | |
179 | ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv); | |
180 | res = crypto_ablkcipher_decrypt(req); | |
181 | if (res == -EINPROGRESS || res == -EBUSY) { | |
6b3bd08f JK |
182 | wait_for_completion(&ecr.completion); |
183 | res = ecr.res; | |
184 | } | |
185 | ablkcipher_request_free(req); | |
186 | if (res < 0) { | |
187 | printk_ratelimited(KERN_ERR | |
188 | "%s: Error in f2fs_fname_decrypt (error code %d)\n", | |
189 | __func__, res); | |
190 | return res; | |
191 | } | |
192 | ||
193 | oname->len = strnlen(oname->name, iname->len); | |
194 | return oname->len; | |
195 | } | |
196 | ||
197 | static const char *lookup_table = | |
198 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,"; | |
199 | ||
200 | /** | |
201 | * f2fs_fname_encode_digest() - | |
202 | * | |
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. | |
205 | */ | |
206 | static int digest_encode(const char *src, int len, char *dst) | |
207 | { | |
208 | int i = 0, bits = 0, ac = 0; | |
209 | char *cp = dst; | |
210 | ||
211 | while (i < len) { | |
212 | ac += (((unsigned char) src[i]) << bits); | |
213 | bits += 8; | |
214 | do { | |
215 | *cp++ = lookup_table[ac & 0x3f]; | |
216 | ac >>= 6; | |
217 | bits -= 6; | |
218 | } while (bits >= 6); | |
219 | i++; | |
220 | } | |
221 | if (bits) | |
222 | *cp++ = lookup_table[ac & 0x3f]; | |
223 | return cp - dst; | |
224 | } | |
225 | ||
226 | static int digest_decode(const char *src, int len, char *dst) | |
227 | { | |
228 | int i = 0, bits = 0, ac = 0; | |
229 | const char *p; | |
230 | char *cp = dst; | |
231 | ||
232 | while (i < len) { | |
233 | p = strchr(lookup_table, src[i]); | |
234 | if (p == NULL || src[i] == 0) | |
235 | return -2; | |
236 | ac += (p - lookup_table) << bits; | |
237 | bits += 6; | |
238 | if (bits >= 8) { | |
239 | *cp++ = ac & 0xff; | |
240 | ac >>= 8; | |
241 | bits -= 8; | |
242 | } | |
243 | i++; | |
244 | } | |
245 | if (ac) | |
246 | return -1; | |
247 | return cp - dst; | |
248 | } | |
249 | ||
6b3bd08f JK |
250 | /** |
251 | * f2fs_fname_crypto_round_up() - | |
252 | * | |
253 | * Return: The next multiple of block size | |
254 | */ | |
255 | u32 f2fs_fname_crypto_round_up(u32 size, u32 blksize) | |
256 | { | |
257 | return ((size + blksize - 1) / blksize) * blksize; | |
258 | } | |
259 | ||
922ec355 CY |
260 | unsigned f2fs_fname_encrypted_size(struct inode *inode, u32 ilen) |
261 | { | |
262 | struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info; | |
263 | int padding = 32; | |
264 | ||
265 | if (ci) | |
266 | padding = 4 << (ci->ci_flags & F2FS_POLICY_FLAGS_PAD_MASK); | |
267 | if (ilen < F2FS_CRYPTO_BLOCK_SIZE) | |
268 | ilen = F2FS_CRYPTO_BLOCK_SIZE; | |
269 | return f2fs_fname_crypto_round_up(ilen, padding); | |
270 | } | |
271 | ||
6b3bd08f JK |
272 | /** |
273 | * f2fs_fname_crypto_alloc_obuff() - | |
274 | * | |
275 | * Allocates an output buffer that is sufficient for the crypto operation | |
276 | * specified by the context and the direction. | |
277 | */ | |
278 | int f2fs_fname_crypto_alloc_buffer(struct inode *inode, | |
279 | u32 ilen, struct f2fs_str *crypto_str) | |
280 | { | |
922ec355 | 281 | unsigned int olen = f2fs_fname_encrypted_size(inode, ilen); |
6b3bd08f | 282 | |
6b3bd08f JK |
283 | crypto_str->len = olen; |
284 | if (olen < F2FS_FNAME_CRYPTO_DIGEST_SIZE * 2) | |
285 | olen = F2FS_FNAME_CRYPTO_DIGEST_SIZE * 2; | |
286 | /* Allocated buffer can hold one more character to null-terminate the | |
287 | * string */ | |
288 | crypto_str->name = kmalloc(olen + 1, GFP_NOFS); | |
289 | if (!(crypto_str->name)) | |
290 | return -ENOMEM; | |
291 | return 0; | |
292 | } | |
293 | ||
294 | /** | |
295 | * f2fs_fname_crypto_free_buffer() - | |
296 | * | |
297 | * Frees the buffer allocated for crypto operation. | |
298 | */ | |
299 | void f2fs_fname_crypto_free_buffer(struct f2fs_str *crypto_str) | |
300 | { | |
301 | if (!crypto_str) | |
302 | return; | |
303 | kfree(crypto_str->name); | |
304 | crypto_str->name = NULL; | |
305 | } | |
306 | ||
307 | /** | |
308 | * f2fs_fname_disk_to_usr() - converts a filename from disk space to user space | |
309 | */ | |
310 | int f2fs_fname_disk_to_usr(struct inode *inode, | |
311 | f2fs_hash_t *hash, | |
312 | const struct f2fs_str *iname, | |
313 | struct f2fs_str *oname) | |
314 | { | |
315 | const struct qstr qname = FSTR_TO_QSTR(iname); | |
316 | char buf[24]; | |
317 | int ret; | |
318 | ||
319 | if (is_dot_dotdot(&qname)) { | |
320 | oname->name[0] = '.'; | |
321 | oname->name[iname->len - 1] = '.'; | |
322 | oname->len = iname->len; | |
323 | return oname->len; | |
324 | } | |
1dafa51d JK |
325 | if (iname->len < F2FS_CRYPTO_BLOCK_SIZE) { |
326 | printk("encrypted inode too small"); | |
327 | return -EUCLEAN; | |
328 | } | |
6b3bd08f JK |
329 | if (F2FS_I(inode)->i_crypt_info) |
330 | return f2fs_fname_decrypt(inode, iname, oname); | |
331 | ||
332 | if (iname->len <= F2FS_FNAME_CRYPTO_DIGEST_SIZE) { | |
333 | ret = digest_encode(iname->name, iname->len, oname->name); | |
334 | oname->len = ret; | |
335 | return ret; | |
336 | } | |
337 | if (hash) { | |
338 | memcpy(buf, hash, 4); | |
339 | memset(buf + 4, 0, 4); | |
340 | } else | |
341 | memset(buf, 0, 8); | |
342 | memcpy(buf + 8, iname->name + iname->len - 16, 16); | |
343 | oname->name[0] = '_'; | |
344 | ret = digest_encode(buf, 24, oname->name + 1); | |
345 | oname->len = ret + 1; | |
346 | return ret + 1; | |
347 | } | |
348 | ||
349 | /** | |
350 | * f2fs_fname_usr_to_disk() - converts a filename from user space to disk space | |
351 | */ | |
352 | int f2fs_fname_usr_to_disk(struct inode *inode, | |
353 | const struct qstr *iname, | |
354 | struct f2fs_str *oname) | |
355 | { | |
356 | int res; | |
357 | struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info; | |
358 | ||
359 | if (is_dot_dotdot(iname)) { | |
360 | oname->name[0] = '.'; | |
361 | oname->name[iname->len - 1] = '.'; | |
362 | oname->len = iname->len; | |
363 | return oname->len; | |
364 | } | |
365 | ||
366 | if (ci) { | |
367 | res = f2fs_fname_encrypt(inode, iname, oname); | |
368 | return res; | |
369 | } | |
370 | /* Without a proper key, a user is not allowed to modify the filenames | |
371 | * in a directory. Consequently, a user space name cannot be mapped to | |
372 | * a disk-space name */ | |
373 | return -EACCES; | |
374 | } | |
375 | ||
376 | int f2fs_fname_setup_filename(struct inode *dir, const struct qstr *iname, | |
377 | int lookup, struct f2fs_filename *fname) | |
378 | { | |
379 | struct f2fs_crypt_info *ci; | |
380 | int ret = 0, bigname = 0; | |
381 | ||
382 | memset(fname, 0, sizeof(struct f2fs_filename)); | |
383 | fname->usr_fname = iname; | |
384 | ||
385 | if (!f2fs_encrypted_inode(dir) || is_dot_dotdot(iname)) { | |
386 | fname->disk_name.name = (unsigned char *)iname->name; | |
387 | fname->disk_name.len = iname->len; | |
7bf4b557 | 388 | return 0; |
6b3bd08f | 389 | } |
26bf3dc7 | 390 | ret = f2fs_get_encryption_info(dir); |
6b3bd08f JK |
391 | if (ret) |
392 | return ret; | |
393 | ci = F2FS_I(dir)->i_crypt_info; | |
394 | if (ci) { | |
395 | ret = f2fs_fname_crypto_alloc_buffer(dir, iname->len, | |
396 | &fname->crypto_buf); | |
397 | if (ret < 0) | |
7bf4b557 | 398 | return ret; |
6b3bd08f JK |
399 | ret = f2fs_fname_encrypt(dir, iname, &fname->crypto_buf); |
400 | if (ret < 0) | |
e5e0906b | 401 | goto errout; |
6b3bd08f JK |
402 | fname->disk_name.name = fname->crypto_buf.name; |
403 | fname->disk_name.len = fname->crypto_buf.len; | |
7bf4b557 | 404 | return 0; |
6b3bd08f | 405 | } |
e5e0906b JK |
406 | if (!lookup) |
407 | return -EACCES; | |
6b3bd08f JK |
408 | |
409 | /* We don't have the key and we are doing a lookup; decode the | |
410 | * user-supplied name | |
411 | */ | |
412 | if (iname->name[0] == '_') | |
413 | bigname = 1; | |
414 | if ((bigname && (iname->len != 33)) || | |
e5e0906b JK |
415 | (!bigname && (iname->len > 43))) |
416 | return -ENOENT; | |
417 | ||
6b3bd08f | 418 | fname->crypto_buf.name = kmalloc(32, GFP_KERNEL); |
e5e0906b JK |
419 | if (fname->crypto_buf.name == NULL) |
420 | return -ENOMEM; | |
6b3bd08f JK |
421 | ret = digest_decode(iname->name + bigname, iname->len - bigname, |
422 | fname->crypto_buf.name); | |
423 | if (ret < 0) { | |
424 | ret = -ENOENT; | |
e5e0906b | 425 | goto errout; |
6b3bd08f JK |
426 | } |
427 | fname->crypto_buf.len = ret; | |
428 | if (bigname) { | |
429 | memcpy(&fname->hash, fname->crypto_buf.name, 4); | |
430 | } else { | |
431 | fname->disk_name.name = fname->crypto_buf.name; | |
432 | fname->disk_name.len = fname->crypto_buf.len; | |
433 | } | |
7bf4b557 | 434 | return 0; |
e5e0906b | 435 | errout: |
7bf4b557 | 436 | f2fs_fname_crypto_free_buffer(&fname->crypto_buf); |
6b3bd08f JK |
437 | return ret; |
438 | } | |
439 | ||
440 | void f2fs_fname_free_filename(struct f2fs_filename *fname) | |
441 | { | |
442 | kfree(fname->crypto_buf.name); | |
443 | fname->crypto_buf.name = NULL; | |
444 | fname->usr_fname = NULL; | |
445 | fname->disk_name.name = NULL; | |
446 | } |