]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/f2fs/crypto_fname.c
Merge branch 'akpm-current/current'
[karo-tx-linux.git] / fs / f2fs / crypto_fname.c
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/skcipher.h>
19 #include <keys/encrypted-type.h>
20 #include <keys/user-type.h>
21 #include <linux/gfp.h>
22 #include <linux/kernel.h>
23 #include <linux/key.h>
24 #include <linux/list.h>
25 #include <linux/mempool.h>
26 #include <linux/random.h>
27 #include <linux/scatterlist.h>
28 #include <linux/spinlock_types.h>
29 #include <linux/f2fs_fs.h>
30 #include <linux/ratelimit.h>
31
32 #include "f2fs.h"
33 #include "f2fs_crypto.h"
34 #include "xattr.h"
35
36 /**
37  * f2fs_dir_crypt_complete() -
38  */
39 static void f2fs_dir_crypt_complete(struct crypto_async_request *req, int res)
40 {
41         struct f2fs_completion_result *ecr = req->data;
42
43         if (res == -EINPROGRESS)
44                 return;
45         ecr->res = res;
46         complete(&ecr->completion);
47 }
48
49 bool f2fs_valid_filenames_enc_mode(uint32_t mode)
50 {
51         return (mode == F2FS_ENCRYPTION_MODE_AES_256_CTS);
52 }
53
54 static unsigned max_name_len(struct inode *inode)
55 {
56         return S_ISLNK(inode->i_mode) ? inode->i_sb->s_blocksize :
57                                         F2FS_NAME_LEN;
58 }
59
60 /**
61  * f2fs_fname_encrypt() -
62  *
63  * This function encrypts the input filename, and returns the length of the
64  * ciphertext. Errors are returned as negative numbers.  We trust the caller to
65  * allocate sufficient memory to oname string.
66  */
67 static int f2fs_fname_encrypt(struct inode *inode,
68                         const struct qstr *iname, struct f2fs_str *oname)
69 {
70         u32 ciphertext_len;
71         struct skcipher_request *req = NULL;
72         DECLARE_F2FS_COMPLETION_RESULT(ecr);
73         struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
74         struct crypto_skcipher *tfm = ci->ci_ctfm;
75         int res = 0;
76         char iv[F2FS_CRYPTO_BLOCK_SIZE];
77         struct scatterlist src_sg, dst_sg;
78         int padding = 4 << (ci->ci_flags & F2FS_POLICY_FLAGS_PAD_MASK);
79         char *workbuf, buf[32], *alloc_buf = NULL;
80         unsigned lim = max_name_len(inode);
81
82         if (iname->len <= 0 || iname->len > lim)
83                 return -EIO;
84
85         ciphertext_len = (iname->len < F2FS_CRYPTO_BLOCK_SIZE) ?
86                 F2FS_CRYPTO_BLOCK_SIZE : iname->len;
87         ciphertext_len = f2fs_fname_crypto_round_up(ciphertext_len, padding);
88         ciphertext_len = (ciphertext_len > lim) ? lim : ciphertext_len;
89
90         if (ciphertext_len <= sizeof(buf)) {
91                 workbuf = buf;
92         } else {
93                 alloc_buf = kmalloc(ciphertext_len, GFP_NOFS);
94                 if (!alloc_buf)
95                         return -ENOMEM;
96                 workbuf = alloc_buf;
97         }
98
99         /* Allocate request */
100         req = skcipher_request_alloc(tfm, GFP_NOFS);
101         if (!req) {
102                 printk_ratelimited(KERN_ERR
103                         "%s: crypto_request_alloc() failed\n", __func__);
104                 kfree(alloc_buf);
105                 return -ENOMEM;
106         }
107         skcipher_request_set_callback(req,
108                         CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
109                         f2fs_dir_crypt_complete, &ecr);
110
111         /* Copy the input */
112         memcpy(workbuf, iname->name, iname->len);
113         if (iname->len < ciphertext_len)
114                 memset(workbuf + iname->len, 0, ciphertext_len - iname->len);
115
116         /* Initialize IV */
117         memset(iv, 0, F2FS_CRYPTO_BLOCK_SIZE);
118
119         /* Create encryption request */
120         sg_init_one(&src_sg, workbuf, ciphertext_len);
121         sg_init_one(&dst_sg, oname->name, ciphertext_len);
122         skcipher_request_set_crypt(req, &src_sg, &dst_sg, ciphertext_len, iv);
123         res = crypto_skcipher_encrypt(req);
124         if (res == -EINPROGRESS || res == -EBUSY) {
125                 BUG_ON(req->base.data != &ecr);
126                 wait_for_completion(&ecr.completion);
127                 res = ecr.res;
128         }
129         kfree(alloc_buf);
130         skcipher_request_free(req);
131         if (res < 0) {
132                 printk_ratelimited(KERN_ERR
133                                 "%s: Error (error code %d)\n", __func__, res);
134         }
135         oname->len = ciphertext_len;
136         return res;
137 }
138
139 /*
140  * f2fs_fname_decrypt()
141  *      This function decrypts the input filename, and returns
142  *      the length of the plaintext.
143  *      Errors are returned as negative numbers.
144  *      We trust the caller to allocate sufficient memory to oname string.
145  */
146 static int f2fs_fname_decrypt(struct inode *inode,
147                         const struct f2fs_str *iname, struct f2fs_str *oname)
148 {
149         struct skcipher_request *req = NULL;
150         DECLARE_F2FS_COMPLETION_RESULT(ecr);
151         struct scatterlist src_sg, dst_sg;
152         struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
153         struct crypto_skcipher *tfm = ci->ci_ctfm;
154         int res = 0;
155         char iv[F2FS_CRYPTO_BLOCK_SIZE];
156         unsigned lim = max_name_len(inode);
157
158         if (iname->len <= 0 || iname->len > lim)
159                 return -EIO;
160
161         /* Allocate request */
162         req = skcipher_request_alloc(tfm, GFP_NOFS);
163         if (!req) {
164                 printk_ratelimited(KERN_ERR
165                         "%s: crypto_request_alloc() failed\n",  __func__);
166                 return -ENOMEM;
167         }
168         skcipher_request_set_callback(req,
169                 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
170                 f2fs_dir_crypt_complete, &ecr);
171
172         /* Initialize IV */
173         memset(iv, 0, F2FS_CRYPTO_BLOCK_SIZE);
174
175         /* Create decryption request */
176         sg_init_one(&src_sg, iname->name, iname->len);
177         sg_init_one(&dst_sg, oname->name, oname->len);
178         skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
179         res = crypto_skcipher_decrypt(req);
180         if (res == -EINPROGRESS || res == -EBUSY) {
181                 BUG_ON(req->base.data != &ecr);
182                 wait_for_completion(&ecr.completion);
183                 res = ecr.res;
184         }
185         skcipher_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
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
260 /**
261  * f2fs_fname_crypto_alloc_obuff() -
262  *
263  * Allocates an output buffer that is sufficient for the crypto operation
264  * specified by the context and the direction.
265  */
266 int f2fs_fname_crypto_alloc_buffer(struct inode *inode,
267                                    u32 ilen, struct f2fs_str *crypto_str)
268 {
269         unsigned int olen;
270         int padding = 16;
271         struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
272
273         if (ci)
274                 padding = 4 << (ci->ci_flags & F2FS_POLICY_FLAGS_PAD_MASK);
275         if (padding < F2FS_CRYPTO_BLOCK_SIZE)
276                 padding = F2FS_CRYPTO_BLOCK_SIZE;
277         olen = f2fs_fname_crypto_round_up(ilen, padding);
278         crypto_str->len = olen;
279         if (olen < F2FS_FNAME_CRYPTO_DIGEST_SIZE * 2)
280                 olen = F2FS_FNAME_CRYPTO_DIGEST_SIZE * 2;
281         /* Allocated buffer can hold one more character to null-terminate the
282          * string */
283         crypto_str->name = kmalloc(olen + 1, GFP_NOFS);
284         if (!(crypto_str->name))
285                 return -ENOMEM;
286         return 0;
287 }
288
289 /**
290  * f2fs_fname_crypto_free_buffer() -
291  *
292  * Frees the buffer allocated for crypto operation.
293  */
294 void f2fs_fname_crypto_free_buffer(struct f2fs_str *crypto_str)
295 {
296         if (!crypto_str)
297                 return;
298         kfree(crypto_str->name);
299         crypto_str->name = NULL;
300 }
301
302 /**
303  * f2fs_fname_disk_to_usr() - converts a filename from disk space to user space
304  */
305 int f2fs_fname_disk_to_usr(struct inode *inode,
306                         f2fs_hash_t *hash,
307                         const struct f2fs_str *iname,
308                         struct f2fs_str *oname)
309 {
310         const struct qstr qname = FSTR_TO_QSTR(iname);
311         char buf[24];
312         int ret;
313
314         if (is_dot_dotdot(&qname)) {
315                 oname->name[0] = '.';
316                 oname->name[iname->len - 1] = '.';
317                 oname->len = iname->len;
318                 return oname->len;
319         }
320
321         if (F2FS_I(inode)->i_crypt_info)
322                 return f2fs_fname_decrypt(inode, iname, oname);
323
324         if (iname->len <= F2FS_FNAME_CRYPTO_DIGEST_SIZE) {
325                 ret = digest_encode(iname->name, iname->len, oname->name);
326                 oname->len = ret;
327                 return ret;
328         }
329         if (hash) {
330                 memcpy(buf, hash, 4);
331                 memset(buf + 4, 0, 4);
332         } else
333                 memset(buf, 0, 8);
334         memcpy(buf + 8, iname->name + iname->len - 16, 16);
335         oname->name[0] = '_';
336         ret = digest_encode(buf, 24, oname->name + 1);
337         oname->len = ret + 1;
338         return ret + 1;
339 }
340
341 /**
342  * f2fs_fname_usr_to_disk() - converts a filename from user space to disk space
343  */
344 int f2fs_fname_usr_to_disk(struct inode *inode,
345                         const struct qstr *iname,
346                         struct f2fs_str *oname)
347 {
348         int res;
349         struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
350
351         if (is_dot_dotdot(iname)) {
352                 oname->name[0] = '.';
353                 oname->name[iname->len - 1] = '.';
354                 oname->len = iname->len;
355                 return oname->len;
356         }
357
358         if (ci) {
359                 res = f2fs_fname_encrypt(inode, iname, oname);
360                 return res;
361         }
362         /* Without a proper key, a user is not allowed to modify the filenames
363          * in a directory. Consequently, a user space name cannot be mapped to
364          * a disk-space name */
365         return -EACCES;
366 }
367
368 int f2fs_fname_setup_filename(struct inode *dir, const struct qstr *iname,
369                               int lookup, struct f2fs_filename *fname)
370 {
371         struct f2fs_crypt_info *ci;
372         int ret = 0, bigname = 0;
373
374         memset(fname, 0, sizeof(struct f2fs_filename));
375         fname->usr_fname = iname;
376
377         if (!f2fs_encrypted_inode(dir) || is_dot_dotdot(iname)) {
378                 fname->disk_name.name = (unsigned char *)iname->name;
379                 fname->disk_name.len = iname->len;
380                 return 0;
381         }
382         ret = f2fs_get_encryption_info(dir);
383         if (ret)
384                 return ret;
385         ci = F2FS_I(dir)->i_crypt_info;
386         if (ci) {
387                 ret = f2fs_fname_crypto_alloc_buffer(dir, iname->len,
388                                                      &fname->crypto_buf);
389                 if (ret < 0)
390                         return ret;
391                 ret = f2fs_fname_encrypt(dir, iname, &fname->crypto_buf);
392                 if (ret < 0)
393                         goto errout;
394                 fname->disk_name.name = fname->crypto_buf.name;
395                 fname->disk_name.len = fname->crypto_buf.len;
396                 return 0;
397         }
398         if (!lookup)
399                 return -EACCES;
400
401         /* We don't have the key and we are doing a lookup; decode the
402          * user-supplied name
403          */
404         if (iname->name[0] == '_')
405                 bigname = 1;
406         if ((bigname && (iname->len != 33)) ||
407             (!bigname && (iname->len > 43)))
408                 return -ENOENT;
409
410         fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
411         if (fname->crypto_buf.name == NULL)
412                 return -ENOMEM;
413         ret = digest_decode(iname->name + bigname, iname->len - bigname,
414                                 fname->crypto_buf.name);
415         if (ret < 0) {
416                 ret = -ENOENT;
417                 goto errout;
418         }
419         fname->crypto_buf.len = ret;
420         if (bigname) {
421                 memcpy(&fname->hash, fname->crypto_buf.name, 4);
422         } else {
423                 fname->disk_name.name = fname->crypto_buf.name;
424                 fname->disk_name.len = fname->crypto_buf.len;
425         }
426         return 0;
427 errout:
428         f2fs_fname_crypto_free_buffer(&fname->crypto_buf);
429         return ret;
430 }
431
432 void f2fs_fname_free_filename(struct f2fs_filename *fname)
433 {
434         kfree(fname->crypto_buf.name);
435         fname->crypto_buf.name = NULL;
436         fname->usr_fname = NULL;
437         fname->disk_name.name = NULL;
438 }