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