blob: 016c4b63b53d879a3537e7542c44b04818df315a [file] [log] [blame]
Jaegeuk Kim6b3bd082015-04-26 00:12:50 -07001/*
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 */
41static 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
51bool f2fs_valid_filenames_enc_mode(uint32_t mode)
52{
53 return (mode == F2FS_ENCRYPTION_MODE_AES_256_CTS);
54}
55
56static 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 */
69static 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 */
148static 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
199static 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 */
208static 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
228static 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
252int f2fs_setup_fname_crypto(struct inode *inode)
253{
254 struct f2fs_inode_info *fi = F2FS_I(inode);
255 struct f2fs_crypt_info *ci = fi->i_crypt_info;
256 struct crypto_ablkcipher *ctfm;
257 int res;
258
259 /* Check if the crypto policy is set on the inode */
260 res = f2fs_encrypted_inode(inode);
261 if (res == 0)
262 return 0;
263
264 res = f2fs_get_encryption_info(inode);
265 if (res < 0)
266 return res;
267 ci = fi->i_crypt_info;
268
269 if (!ci || ci->ci_ctfm)
270 return 0;
271
Jaegeuk Kim640778f2015-05-12 13:33:00 -0700272 if (ci->ci_filename_mode != F2FS_ENCRYPTION_MODE_AES_256_CTS) {
Jaegeuk Kim6b3bd082015-04-26 00:12:50 -0700273 printk_once(KERN_WARNING "f2fs: unsupported key mode %d\n",
Jaegeuk Kim640778f2015-05-12 13:33:00 -0700274 ci->ci_filename_mode);
Jaegeuk Kim6b3bd082015-04-26 00:12:50 -0700275 return -ENOKEY;
276 }
277
278 ctfm = crypto_alloc_ablkcipher("cts(cbc(aes))", 0, 0);
279 if (!ctfm || IS_ERR(ctfm)) {
280 res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
281 printk(KERN_DEBUG "%s: error (%d) allocating crypto tfm\n",
282 __func__, res);
283 return res;
284 }
285 crypto_ablkcipher_clear_flags(ctfm, ~0);
286 crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm),
287 CRYPTO_TFM_REQ_WEAK_KEY);
288
289 res = crypto_ablkcipher_setkey(ctfm, ci->ci_raw, ci->ci_size);
290 if (res) {
291 crypto_free_ablkcipher(ctfm);
292 return -EIO;
293 }
294 ci->ci_ctfm = ctfm;
295 return 0;
296}
297
298/**
299 * f2fs_fname_crypto_round_up() -
300 *
301 * Return: The next multiple of block size
302 */
303u32 f2fs_fname_crypto_round_up(u32 size, u32 blksize)
304{
305 return ((size + blksize - 1) / blksize) * blksize;
306}
307
308/**
309 * f2fs_fname_crypto_alloc_obuff() -
310 *
311 * Allocates an output buffer that is sufficient for the crypto operation
312 * specified by the context and the direction.
313 */
314int f2fs_fname_crypto_alloc_buffer(struct inode *inode,
315 u32 ilen, struct f2fs_str *crypto_str)
316{
317 unsigned int olen;
318 int padding = 16;
319 struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
320
321 if (ci)
322 padding = 4 << (ci->ci_flags & F2FS_POLICY_FLAGS_PAD_MASK);
323 if (padding < F2FS_CRYPTO_BLOCK_SIZE)
324 padding = F2FS_CRYPTO_BLOCK_SIZE;
325 olen = f2fs_fname_crypto_round_up(ilen, padding);
326 crypto_str->len = olen;
327 if (olen < F2FS_FNAME_CRYPTO_DIGEST_SIZE * 2)
328 olen = F2FS_FNAME_CRYPTO_DIGEST_SIZE * 2;
329 /* Allocated buffer can hold one more character to null-terminate the
330 * string */
331 crypto_str->name = kmalloc(olen + 1, GFP_NOFS);
332 if (!(crypto_str->name))
333 return -ENOMEM;
334 return 0;
335}
336
337/**
338 * f2fs_fname_crypto_free_buffer() -
339 *
340 * Frees the buffer allocated for crypto operation.
341 */
342void f2fs_fname_crypto_free_buffer(struct f2fs_str *crypto_str)
343{
344 if (!crypto_str)
345 return;
346 kfree(crypto_str->name);
347 crypto_str->name = NULL;
348}
349
350/**
351 * f2fs_fname_disk_to_usr() - converts a filename from disk space to user space
352 */
353int f2fs_fname_disk_to_usr(struct inode *inode,
354 f2fs_hash_t *hash,
355 const struct f2fs_str *iname,
356 struct f2fs_str *oname)
357{
358 const struct qstr qname = FSTR_TO_QSTR(iname);
359 char buf[24];
360 int ret;
361
362 if (is_dot_dotdot(&qname)) {
363 oname->name[0] = '.';
364 oname->name[iname->len - 1] = '.';
365 oname->len = iname->len;
366 return oname->len;
367 }
368
369 if (F2FS_I(inode)->i_crypt_info)
370 return f2fs_fname_decrypt(inode, iname, oname);
371
372 if (iname->len <= F2FS_FNAME_CRYPTO_DIGEST_SIZE) {
373 ret = digest_encode(iname->name, iname->len, oname->name);
374 oname->len = ret;
375 return ret;
376 }
377 if (hash) {
378 memcpy(buf, hash, 4);
379 memset(buf + 4, 0, 4);
380 } else
381 memset(buf, 0, 8);
382 memcpy(buf + 8, iname->name + iname->len - 16, 16);
383 oname->name[0] = '_';
384 ret = digest_encode(buf, 24, oname->name + 1);
385 oname->len = ret + 1;
386 return ret + 1;
387}
388
389/**
390 * f2fs_fname_usr_to_disk() - converts a filename from user space to disk space
391 */
392int f2fs_fname_usr_to_disk(struct inode *inode,
393 const struct qstr *iname,
394 struct f2fs_str *oname)
395{
396 int res;
397 struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
398
399 if (is_dot_dotdot(iname)) {
400 oname->name[0] = '.';
401 oname->name[iname->len - 1] = '.';
402 oname->len = iname->len;
403 return oname->len;
404 }
405
406 if (ci) {
407 res = f2fs_fname_encrypt(inode, iname, oname);
408 return res;
409 }
410 /* Without a proper key, a user is not allowed to modify the filenames
411 * in a directory. Consequently, a user space name cannot be mapped to
412 * a disk-space name */
413 return -EACCES;
414}
415
416int f2fs_fname_setup_filename(struct inode *dir, const struct qstr *iname,
417 int lookup, struct f2fs_filename *fname)
418{
419 struct f2fs_crypt_info *ci;
420 int ret = 0, bigname = 0;
421
422 memset(fname, 0, sizeof(struct f2fs_filename));
423 fname->usr_fname = iname;
424
425 if (!f2fs_encrypted_inode(dir) || is_dot_dotdot(iname)) {
426 fname->disk_name.name = (unsigned char *)iname->name;
427 fname->disk_name.len = iname->len;
Chao Yu7bf4b552015-05-13 18:20:54 +0800428 return 0;
Jaegeuk Kim6b3bd082015-04-26 00:12:50 -0700429 }
430 ret = f2fs_setup_fname_crypto(dir);
431 if (ret)
432 return ret;
433 ci = F2FS_I(dir)->i_crypt_info;
434 if (ci) {
435 ret = f2fs_fname_crypto_alloc_buffer(dir, iname->len,
436 &fname->crypto_buf);
437 if (ret < 0)
Chao Yu7bf4b552015-05-13 18:20:54 +0800438 return ret;
Jaegeuk Kim6b3bd082015-04-26 00:12:50 -0700439 ret = f2fs_fname_encrypt(dir, iname, &fname->crypto_buf);
440 if (ret < 0)
441 goto out;
442 fname->disk_name.name = fname->crypto_buf.name;
443 fname->disk_name.len = fname->crypto_buf.len;
Chao Yu7bf4b552015-05-13 18:20:54 +0800444 return 0;
Jaegeuk Kim6b3bd082015-04-26 00:12:50 -0700445 }
446 if (!lookup) {
447 ret = -EACCES;
448 goto out;
449 }
450
451 /* We don't have the key and we are doing a lookup; decode the
452 * user-supplied name
453 */
454 if (iname->name[0] == '_')
455 bigname = 1;
456 if ((bigname && (iname->len != 33)) ||
457 (!bigname && (iname->len > 43))) {
458 ret = -ENOENT;
459 }
460 fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
461 if (fname->crypto_buf.name == NULL) {
462 ret = -ENOMEM;
463 goto out;
464 }
465 ret = digest_decode(iname->name + bigname, iname->len - bigname,
466 fname->crypto_buf.name);
467 if (ret < 0) {
468 ret = -ENOENT;
469 goto out;
470 }
471 fname->crypto_buf.len = ret;
472 if (bigname) {
473 memcpy(&fname->hash, fname->crypto_buf.name, 4);
474 } else {
475 fname->disk_name.name = fname->crypto_buf.name;
476 fname->disk_name.len = fname->crypto_buf.len;
477 }
Chao Yu7bf4b552015-05-13 18:20:54 +0800478 return 0;
Jaegeuk Kim6b3bd082015-04-26 00:12:50 -0700479out:
Chao Yu7bf4b552015-05-13 18:20:54 +0800480 f2fs_fname_crypto_free_buffer(&fname->crypto_buf);
Jaegeuk Kim6b3bd082015-04-26 00:12:50 -0700481 return ret;
482}
483
484void f2fs_fname_free_filename(struct f2fs_filename *fname)
485{
486 kfree(fname->crypto_buf.name);
487 fname->crypto_buf.name = NULL;
488 fname->usr_fname = NULL;
489 fname->disk_name.name = NULL;
490}