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