]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/crypto/fname.c
fscrypto: make filename crypto functions return 0 on success
[karo-tx-linux.git] / fs / crypto / fname.c
1 /*
2  * This contains functions for filename crypto management
3  *
4  * Copyright (C) 2015, Google, Inc.
5  * Copyright (C) 2015, Motorola Mobility
6  *
7  * Written by Uday Savagaonkar, 2014.
8  * Modified by Jaegeuk Kim, 2015.
9  *
10  * This has not yet undergone a rigorous security audit.
11  */
12
13 #include <linux/scatterlist.h>
14 #include <linux/ratelimit.h>
15 #include <linux/fscrypto.h>
16
17 static u32 size_round_up(size_t size, size_t blksize)
18 {
19         return ((size + blksize - 1) / blksize) * blksize;
20 }
21
22 /**
23  * fname_crypt_complete() - completion callback for filename crypto
24  * @req: The asynchronous cipher request context
25  * @res: The result of the cipher operation
26  */
27 static void fname_crypt_complete(struct crypto_async_request *req, int res)
28 {
29         struct fscrypt_completion_result *ecr = req->data;
30
31         if (res == -EINPROGRESS)
32                 return;
33         ecr->res = res;
34         complete(&ecr->completion);
35 }
36
37 /**
38  * fname_encrypt() - encrypt a filename
39  *
40  * The caller must have allocated sufficient memory for the @oname string.
41  *
42  * Return: 0 on success, -errno on failure
43  */
44 static int fname_encrypt(struct inode *inode,
45                         const struct qstr *iname, struct fscrypt_str *oname)
46 {
47         u32 ciphertext_len;
48         struct skcipher_request *req = NULL;
49         DECLARE_FS_COMPLETION_RESULT(ecr);
50         struct fscrypt_info *ci = inode->i_crypt_info;
51         struct crypto_skcipher *tfm = ci->ci_ctfm;
52         int res = 0;
53         char iv[FS_CRYPTO_BLOCK_SIZE];
54         struct scatterlist src_sg, dst_sg;
55         int padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
56         char *workbuf, buf[32], *alloc_buf = NULL;
57         unsigned lim;
58
59         lim = inode->i_sb->s_cop->max_namelen(inode);
60         if (iname->len <= 0 || iname->len > lim)
61                 return -EIO;
62
63         ciphertext_len = (iname->len < FS_CRYPTO_BLOCK_SIZE) ?
64                                         FS_CRYPTO_BLOCK_SIZE : iname->len;
65         ciphertext_len = size_round_up(ciphertext_len, padding);
66         ciphertext_len = (ciphertext_len > lim) ? lim : ciphertext_len;
67
68         if (ciphertext_len <= sizeof(buf)) {
69                 workbuf = buf;
70         } else {
71                 alloc_buf = kmalloc(ciphertext_len, GFP_NOFS);
72                 if (!alloc_buf)
73                         return -ENOMEM;
74                 workbuf = alloc_buf;
75         }
76
77         /* Allocate request */
78         req = skcipher_request_alloc(tfm, GFP_NOFS);
79         if (!req) {
80                 printk_ratelimited(KERN_ERR
81                         "%s: crypto_request_alloc() failed\n", __func__);
82                 kfree(alloc_buf);
83                 return -ENOMEM;
84         }
85         skcipher_request_set_callback(req,
86                         CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
87                         fname_crypt_complete, &ecr);
88
89         /* Copy the input */
90         memcpy(workbuf, iname->name, iname->len);
91         if (iname->len < ciphertext_len)
92                 memset(workbuf + iname->len, 0, ciphertext_len - iname->len);
93
94         /* Initialize IV */
95         memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
96
97         /* Create encryption request */
98         sg_init_one(&src_sg, workbuf, ciphertext_len);
99         sg_init_one(&dst_sg, oname->name, ciphertext_len);
100         skcipher_request_set_crypt(req, &src_sg, &dst_sg, ciphertext_len, iv);
101         res = crypto_skcipher_encrypt(req);
102         if (res == -EINPROGRESS || res == -EBUSY) {
103                 wait_for_completion(&ecr.completion);
104                 res = ecr.res;
105         }
106         kfree(alloc_buf);
107         skcipher_request_free(req);
108         if (res < 0) {
109                 printk_ratelimited(KERN_ERR
110                                 "%s: Error (error code %d)\n", __func__, res);
111                 return res;
112         }
113
114         oname->len = ciphertext_len;
115         return 0;
116 }
117
118 /**
119  * fname_decrypt() - decrypt a filename
120  *
121  * The caller must have allocated sufficient memory for the @oname string.
122  *
123  * Return: 0 on success, -errno on failure
124  */
125 static int fname_decrypt(struct inode *inode,
126                                 const struct fscrypt_str *iname,
127                                 struct fscrypt_str *oname)
128 {
129         struct skcipher_request *req = NULL;
130         DECLARE_FS_COMPLETION_RESULT(ecr);
131         struct scatterlist src_sg, dst_sg;
132         struct fscrypt_info *ci = inode->i_crypt_info;
133         struct crypto_skcipher *tfm = ci->ci_ctfm;
134         int res = 0;
135         char iv[FS_CRYPTO_BLOCK_SIZE];
136         unsigned lim;
137
138         lim = inode->i_sb->s_cop->max_namelen(inode);
139         if (iname->len <= 0 || iname->len > lim)
140                 return -EIO;
141
142         /* Allocate request */
143         req = skcipher_request_alloc(tfm, GFP_NOFS);
144         if (!req) {
145                 printk_ratelimited(KERN_ERR
146                         "%s: crypto_request_alloc() failed\n",  __func__);
147                 return -ENOMEM;
148         }
149         skcipher_request_set_callback(req,
150                 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
151                 fname_crypt_complete, &ecr);
152
153         /* Initialize IV */
154         memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
155
156         /* Create decryption request */
157         sg_init_one(&src_sg, iname->name, iname->len);
158         sg_init_one(&dst_sg, oname->name, oname->len);
159         skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
160         res = crypto_skcipher_decrypt(req);
161         if (res == -EINPROGRESS || res == -EBUSY) {
162                 wait_for_completion(&ecr.completion);
163                 res = ecr.res;
164         }
165         skcipher_request_free(req);
166         if (res < 0) {
167                 printk_ratelimited(KERN_ERR
168                                 "%s: Error (error code %d)\n", __func__, res);
169                 return res;
170         }
171
172         oname->len = strnlen(oname->name, iname->len);
173         return 0;
174 }
175
176 static const char *lookup_table =
177         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
178
179 /**
180  * digest_encode() -
181  *
182  * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
183  * The encoded string is roughly 4/3 times the size of the input string.
184  */
185 static int digest_encode(const char *src, int len, char *dst)
186 {
187         int i = 0, bits = 0, ac = 0;
188         char *cp = dst;
189
190         while (i < len) {
191                 ac += (((unsigned char) src[i]) << bits);
192                 bits += 8;
193                 do {
194                         *cp++ = lookup_table[ac & 0x3f];
195                         ac >>= 6;
196                         bits -= 6;
197                 } while (bits >= 6);
198                 i++;
199         }
200         if (bits)
201                 *cp++ = lookup_table[ac & 0x3f];
202         return cp - dst;
203 }
204
205 static int digest_decode(const char *src, int len, char *dst)
206 {
207         int i = 0, bits = 0, ac = 0;
208         const char *p;
209         char *cp = dst;
210
211         while (i < len) {
212                 p = strchr(lookup_table, src[i]);
213                 if (p == NULL || src[i] == 0)
214                         return -2;
215                 ac += (p - lookup_table) << bits;
216                 bits += 6;
217                 if (bits >= 8) {
218                         *cp++ = ac & 0xff;
219                         ac >>= 8;
220                         bits -= 8;
221                 }
222                 i++;
223         }
224         if (ac)
225                 return -1;
226         return cp - dst;
227 }
228
229 u32 fscrypt_fname_encrypted_size(struct inode *inode, u32 ilen)
230 {
231         int padding = 32;
232         struct fscrypt_info *ci = inode->i_crypt_info;
233
234         if (ci)
235                 padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
236         if (ilen < FS_CRYPTO_BLOCK_SIZE)
237                 ilen = FS_CRYPTO_BLOCK_SIZE;
238         return size_round_up(ilen, padding);
239 }
240 EXPORT_SYMBOL(fscrypt_fname_encrypted_size);
241
242 /**
243  * fscrypt_fname_crypto_alloc_obuff() -
244  *
245  * Allocates an output buffer that is sufficient for the crypto operation
246  * specified by the context and the direction.
247  */
248 int fscrypt_fname_alloc_buffer(struct inode *inode,
249                                 u32 ilen, struct fscrypt_str *crypto_str)
250 {
251         unsigned int olen = fscrypt_fname_encrypted_size(inode, ilen);
252
253         crypto_str->len = olen;
254         if (olen < FS_FNAME_CRYPTO_DIGEST_SIZE * 2)
255                 olen = FS_FNAME_CRYPTO_DIGEST_SIZE * 2;
256         /*
257          * Allocated buffer can hold one more character to null-terminate the
258          * string
259          */
260         crypto_str->name = kmalloc(olen + 1, GFP_NOFS);
261         if (!(crypto_str->name))
262                 return -ENOMEM;
263         return 0;
264 }
265 EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
266
267 /**
268  * fscrypt_fname_crypto_free_buffer() -
269  *
270  * Frees the buffer allocated for crypto operation.
271  */
272 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
273 {
274         if (!crypto_str)
275                 return;
276         kfree(crypto_str->name);
277         crypto_str->name = NULL;
278 }
279 EXPORT_SYMBOL(fscrypt_fname_free_buffer);
280
281 /**
282  * fscrypt_fname_disk_to_usr() - converts a filename from disk space to user
283  * space
284  *
285  * The caller must have allocated sufficient memory for the @oname string.
286  *
287  * Return: 0 on success, -errno on failure
288  */
289 int fscrypt_fname_disk_to_usr(struct inode *inode,
290                         u32 hash, u32 minor_hash,
291                         const struct fscrypt_str *iname,
292                         struct fscrypt_str *oname)
293 {
294         const struct qstr qname = FSTR_TO_QSTR(iname);
295         char buf[24];
296
297         if (fscrypt_is_dot_dotdot(&qname)) {
298                 oname->name[0] = '.';
299                 oname->name[iname->len - 1] = '.';
300                 oname->len = iname->len;
301                 return 0;
302         }
303
304         if (iname->len < FS_CRYPTO_BLOCK_SIZE)
305                 return -EUCLEAN;
306
307         if (inode->i_crypt_info)
308                 return fname_decrypt(inode, iname, oname);
309
310         if (iname->len <= FS_FNAME_CRYPTO_DIGEST_SIZE) {
311                 oname->len = digest_encode(iname->name, iname->len,
312                                            oname->name);
313                 return 0;
314         }
315         if (hash) {
316                 memcpy(buf, &hash, 4);
317                 memcpy(buf + 4, &minor_hash, 4);
318         } else {
319                 memset(buf, 0, 8);
320         }
321         memcpy(buf + 8, iname->name + iname->len - 16, 16);
322         oname->name[0] = '_';
323         oname->len = 1 + digest_encode(buf, 24, oname->name + 1);
324         return 0;
325 }
326 EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
327
328 /**
329  * fscrypt_fname_usr_to_disk() - converts a filename from user space to disk
330  * space
331  *
332  * The caller must have allocated sufficient memory for the @oname string.
333  *
334  * Return: 0 on success, -errno on failure
335  */
336 int fscrypt_fname_usr_to_disk(struct inode *inode,
337                         const struct qstr *iname,
338                         struct fscrypt_str *oname)
339 {
340         if (fscrypt_is_dot_dotdot(iname)) {
341                 oname->name[0] = '.';
342                 oname->name[iname->len - 1] = '.';
343                 oname->len = iname->len;
344                 return 0;
345         }
346         if (inode->i_crypt_info)
347                 return fname_encrypt(inode, iname, oname);
348         /*
349          * Without a proper key, a user is not allowed to modify the filenames
350          * in a directory. Consequently, a user space name cannot be mapped to
351          * a disk-space name
352          */
353         return -EACCES;
354 }
355 EXPORT_SYMBOL(fscrypt_fname_usr_to_disk);
356
357 int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
358                               int lookup, struct fscrypt_name *fname)
359 {
360         int ret = 0, bigname = 0;
361
362         memset(fname, 0, sizeof(struct fscrypt_name));
363         fname->usr_fname = iname;
364
365         if (!dir->i_sb->s_cop->is_encrypted(dir) ||
366                                 fscrypt_is_dot_dotdot(iname)) {
367                 fname->disk_name.name = (unsigned char *)iname->name;
368                 fname->disk_name.len = iname->len;
369                 return 0;
370         }
371         ret = get_crypt_info(dir);
372         if (ret && ret != -EOPNOTSUPP)
373                 return ret;
374
375         if (dir->i_crypt_info) {
376                 ret = fscrypt_fname_alloc_buffer(dir, iname->len,
377                                                         &fname->crypto_buf);
378                 if (ret)
379                         return ret;
380                 ret = fname_encrypt(dir, iname, &fname->crypto_buf);
381                 if (ret)
382                         goto errout;
383                 fname->disk_name.name = fname->crypto_buf.name;
384                 fname->disk_name.len = fname->crypto_buf.len;
385                 return 0;
386         }
387         if (!lookup)
388                 return -EACCES;
389
390         /*
391          * We don't have the key and we are doing a lookup; decode the
392          * user-supplied name
393          */
394         if (iname->name[0] == '_')
395                 bigname = 1;
396         if ((bigname && (iname->len != 33)) || (!bigname && (iname->len > 43)))
397                 return -ENOENT;
398
399         fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
400         if (fname->crypto_buf.name == NULL)
401                 return -ENOMEM;
402
403         ret = digest_decode(iname->name + bigname, iname->len - bigname,
404                                 fname->crypto_buf.name);
405         if (ret < 0) {
406                 ret = -ENOENT;
407                 goto errout;
408         }
409         fname->crypto_buf.len = ret;
410         if (bigname) {
411                 memcpy(&fname->hash, fname->crypto_buf.name, 4);
412                 memcpy(&fname->minor_hash, fname->crypto_buf.name + 4, 4);
413         } else {
414                 fname->disk_name.name = fname->crypto_buf.name;
415                 fname->disk_name.len = fname->crypto_buf.len;
416         }
417         return 0;
418
419 errout:
420         fscrypt_fname_free_buffer(&fname->crypto_buf);
421         return ret;
422 }
423 EXPORT_SYMBOL(fscrypt_setup_filename);
424
425 void fscrypt_free_filename(struct fscrypt_name *fname)
426 {
427         kfree(fname->crypto_buf.name);
428         fname->crypto_buf.name = NULL;
429         fname->usr_fname = NULL;
430         fname->disk_name.name = NULL;
431 }
432 EXPORT_SYMBOL(fscrypt_free_filename);