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