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 | |
2ebdef6d | 14 | #include <linux/namei.h> |
6b3bd08f | 15 | #include <linux/scatterlist.h> |
edc440e3 DR |
16 | #include <crypto/hash.h> |
17 | #include <crypto/sha.h> | |
a575784c | 18 | #include <crypto/skcipher.h> |
3325bea5 | 19 | #include "fscrypt_private.h" |
6b3bd08f | 20 | |
edc440e3 DR |
21 | /** |
22 | * struct fscrypt_nokey_name - identifier for directory entry when key is absent | |
23 | * | |
24 | * When userspace lists an encrypted directory without access to the key, the | |
25 | * filesystem must present a unique "no-key name" for each filename that allows | |
26 | * it to find the directory entry again if requested. Naively, that would just | |
27 | * mean using the ciphertext filenames. However, since the ciphertext filenames | |
28 | * can contain illegal characters ('\0' and '/'), they must be encoded in some | |
29 | * way. We use base64. But that can cause names to exceed NAME_MAX (255 | |
30 | * bytes), so we also need to use a strong hash to abbreviate long names. | |
31 | * | |
32 | * The filesystem may also need another kind of hash, the "dirhash", to quickly | |
33 | * find the directory entry. Since filesystems normally compute the dirhash | |
34 | * over the on-disk filename (i.e. the ciphertext), it's not computable from | |
35 | * no-key names that abbreviate the ciphertext using the strong hash to fit in | |
36 | * NAME_MAX. It's also not computable if it's a keyed hash taken over the | |
37 | * plaintext (but it may still be available in the on-disk directory entry); | |
38 | * casefolded directories use this type of dirhash. At least in these cases, | |
39 | * each no-key name must include the name's dirhash too. | |
40 | * | |
41 | * To meet all these requirements, we base64-encode the following | |
42 | * variable-length structure. It contains the dirhash, or 0's if the filesystem | |
43 | * didn't provide one; up to 149 bytes of the ciphertext name; and for | |
44 | * ciphertexts longer than 149 bytes, also the SHA-256 of the remaining bytes. | |
45 | * | |
46 | * This ensures that each no-key name contains everything needed to find the | |
47 | * directory entry again, contains only legal characters, doesn't exceed | |
48 | * NAME_MAX, is unambiguous unless there's a SHA-256 collision, and that we only | |
49 | * take the performance hit of SHA-256 on very long filenames (which are rare). | |
50 | */ | |
51 | struct fscrypt_nokey_name { | |
52 | u32 dirhash[2]; | |
53 | u8 bytes[149]; | |
54 | u8 sha256[SHA256_DIGEST_SIZE]; | |
55 | }; /* 189 bytes => 252 bytes base64-encoded, which is <= NAME_MAX (255) */ | |
56 | ||
57 | /* | |
58 | * Decoded size of max-size nokey name, i.e. a name that was abbreviated using | |
59 | * the strong hash and thus includes the 'sha256' field. This isn't simply | |
60 | * sizeof(struct fscrypt_nokey_name), as the padding at the end isn't included. | |
61 | */ | |
62 | #define FSCRYPT_NOKEY_NAME_MAX offsetofend(struct fscrypt_nokey_name, sha256) | |
63 | ||
64 | static struct crypto_shash *sha256_hash_tfm; | |
65 | ||
66 | static int fscrypt_do_sha256(const u8 *data, unsigned int data_len, u8 *result) | |
67 | { | |
68 | struct crypto_shash *tfm = READ_ONCE(sha256_hash_tfm); | |
69 | ||
70 | if (unlikely(!tfm)) { | |
71 | struct crypto_shash *prev_tfm; | |
72 | ||
73 | tfm = crypto_alloc_shash("sha256", 0, 0); | |
74 | if (IS_ERR(tfm)) { | |
75 | fscrypt_err(NULL, | |
76 | "Error allocating SHA-256 transform: %ld", | |
77 | PTR_ERR(tfm)); | |
78 | return PTR_ERR(tfm); | |
79 | } | |
80 | prev_tfm = cmpxchg(&sha256_hash_tfm, NULL, tfm); | |
81 | if (prev_tfm) { | |
82 | crypto_free_shash(tfm); | |
83 | tfm = prev_tfm; | |
84 | } | |
85 | } | |
86 | { | |
87 | SHASH_DESC_ON_STACK(desc, tfm); | |
88 | ||
89 | desc->tfm = tfm; | |
90 | ||
91 | return crypto_shash_digest(desc, data, data_len, result); | |
92 | } | |
93 | } | |
94 | ||
dcf0db9e EB |
95 | static inline bool fscrypt_is_dot_dotdot(const struct qstr *str) |
96 | { | |
97 | if (str->len == 1 && str->name[0] == '.') | |
98 | return true; | |
99 | ||
100 | if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.') | |
101 | return true; | |
102 | ||
103 | return false; | |
104 | } | |
105 | ||
6b3bd08f | 106 | /** |
1b3b827e | 107 | * fscrypt_fname_encrypt() - encrypt a filename |
6b3bd08f | 108 | * |
50c961de EB |
109 | * The output buffer must be at least as large as the input buffer. |
110 | * Any extra space is filled with NUL padding before encryption. | |
ef1eb3aa EB |
111 | * |
112 | * Return: 0 on success, -errno on failure | |
6b3bd08f | 113 | */ |
1b3b827e EB |
114 | int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname, |
115 | u8 *out, unsigned int olen) | |
6b3bd08f | 116 | { |
2731a944 | 117 | struct skcipher_request *req = NULL; |
d0082e1a | 118 | DECLARE_CRYPTO_WAIT(wait); |
8a4ab0b8 | 119 | const struct fscrypt_info *ci = inode->i_crypt_info; |
8094c3ce EB |
120 | struct crypto_skcipher *tfm = ci->ci_ctfm; |
121 | union fscrypt_iv iv; | |
08ae877f | 122 | struct scatterlist sg; |
8094c3ce | 123 | int res; |
6b3bd08f | 124 | |
08ae877f EB |
125 | /* |
126 | * Copy the filename to the output buffer for encrypting in-place and | |
127 | * pad it with the needed number of NUL bytes. | |
128 | */ | |
50c961de | 129 | if (WARN_ON(olen < iname->len)) |
76e81d6d | 130 | return -ENOBUFS; |
50c961de EB |
131 | memcpy(out, iname->name, iname->len); |
132 | memset(out + iname->len, 0, olen - iname->len); | |
6b3bd08f | 133 | |
08ae877f | 134 | /* Initialize the IV */ |
8094c3ce | 135 | fscrypt_generate_iv(&iv, 0, ci); |
6b3bd08f | 136 | |
08ae877f | 137 | /* Set up the encryption request */ |
2731a944 | 138 | req = skcipher_request_alloc(tfm, GFP_NOFS); |
c90fd775 | 139 | if (!req) |
6b3bd08f | 140 | return -ENOMEM; |
2731a944 | 141 | skcipher_request_set_callback(req, |
6b3bd08f | 142 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
d0082e1a | 143 | crypto_req_done, &wait); |
50c961de | 144 | sg_init_one(&sg, out, olen); |
8094c3ce | 145 | skcipher_request_set_crypt(req, &sg, &sg, olen, &iv); |
6b3bd08f | 146 | |
08ae877f | 147 | /* Do the encryption */ |
d0082e1a | 148 | res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); |
2731a944 | 149 | skcipher_request_free(req); |
ef1eb3aa | 150 | if (res < 0) { |
886da8b3 | 151 | fscrypt_err(inode, "Filename encryption failed: %d", res); |
ef1eb3aa EB |
152 | return res; |
153 | } | |
0b81d077 | 154 | |
ef1eb3aa | 155 | return 0; |
6b3bd08f JK |
156 | } |
157 | ||
ef1eb3aa EB |
158 | /** |
159 | * fname_decrypt() - decrypt a filename | |
160 | * | |
161 | * The caller must have allocated sufficient memory for the @oname string. | |
162 | * | |
163 | * Return: 0 on success, -errno on failure | |
6b3bd08f | 164 | */ |
8a4ab0b8 EB |
165 | static int fname_decrypt(const struct inode *inode, |
166 | const struct fscrypt_str *iname, | |
167 | struct fscrypt_str *oname) | |
6b3bd08f | 168 | { |
2731a944 | 169 | struct skcipher_request *req = NULL; |
d0082e1a | 170 | DECLARE_CRYPTO_WAIT(wait); |
6b3bd08f | 171 | struct scatterlist src_sg, dst_sg; |
8a4ab0b8 | 172 | const struct fscrypt_info *ci = inode->i_crypt_info; |
8094c3ce EB |
173 | struct crypto_skcipher *tfm = ci->ci_ctfm; |
174 | union fscrypt_iv iv; | |
175 | int res; | |
6b3bd08f | 176 | |
6b3bd08f | 177 | /* Allocate request */ |
2731a944 | 178 | req = skcipher_request_alloc(tfm, GFP_NOFS); |
c90fd775 | 179 | if (!req) |
6b3bd08f | 180 | return -ENOMEM; |
2731a944 | 181 | skcipher_request_set_callback(req, |
6b3bd08f | 182 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
d0082e1a | 183 | crypto_req_done, &wait); |
6b3bd08f JK |
184 | |
185 | /* Initialize IV */ | |
8094c3ce | 186 | fscrypt_generate_iv(&iv, 0, ci); |
6b3bd08f JK |
187 | |
188 | /* Create decryption request */ | |
189 | sg_init_one(&src_sg, iname->name, iname->len); | |
190 | sg_init_one(&dst_sg, oname->name, oname->len); | |
8094c3ce | 191 | skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, &iv); |
d0082e1a | 192 | res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait); |
2731a944 | 193 | skcipher_request_free(req); |
6b3bd08f | 194 | if (res < 0) { |
886da8b3 | 195 | fscrypt_err(inode, "Filename decryption failed: %d", res); |
6b3bd08f JK |
196 | return res; |
197 | } | |
198 | ||
199 | oname->len = strnlen(oname->name, iname->len); | |
ef1eb3aa | 200 | return 0; |
6b3bd08f JK |
201 | } |
202 | ||
1c5100a2 | 203 | static const char lookup_table[65] = |
6b3bd08f JK |
204 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,"; |
205 | ||
17159420 EB |
206 | #define BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3) |
207 | ||
6b3bd08f | 208 | /** |
1c5100a2 | 209 | * base64_encode() - |
6b3bd08f | 210 | * |
1c5100a2 | 211 | * Encodes the input string using characters from the set [A-Za-z0-9+,]. |
6b3bd08f | 212 | * The encoded string is roughly 4/3 times the size of the input string. |
1c5100a2 EB |
213 | * |
214 | * Return: length of the encoded string | |
6b3bd08f | 215 | */ |
1c5100a2 | 216 | static int base64_encode(const u8 *src, int len, char *dst) |
6b3bd08f | 217 | { |
1c5100a2 | 218 | int i, bits = 0, ac = 0; |
6b3bd08f JK |
219 | char *cp = dst; |
220 | ||
1c5100a2 EB |
221 | for (i = 0; i < len; i++) { |
222 | ac += src[i] << bits; | |
6b3bd08f JK |
223 | bits += 8; |
224 | do { | |
225 | *cp++ = lookup_table[ac & 0x3f]; | |
226 | ac >>= 6; | |
227 | bits -= 6; | |
228 | } while (bits >= 6); | |
6b3bd08f JK |
229 | } |
230 | if (bits) | |
231 | *cp++ = lookup_table[ac & 0x3f]; | |
232 | return cp - dst; | |
233 | } | |
234 | ||
1c5100a2 | 235 | static int base64_decode(const char *src, int len, u8 *dst) |
6b3bd08f | 236 | { |
1c5100a2 | 237 | int i, bits = 0, ac = 0; |
6b3bd08f | 238 | const char *p; |
1c5100a2 | 239 | u8 *cp = dst; |
6b3bd08f | 240 | |
1c5100a2 | 241 | for (i = 0; i < len; i++) { |
6b3bd08f JK |
242 | p = strchr(lookup_table, src[i]); |
243 | if (p == NULL || src[i] == 0) | |
244 | return -2; | |
245 | ac += (p - lookup_table) << bits; | |
246 | bits += 6; | |
247 | if (bits >= 8) { | |
248 | *cp++ = ac & 0xff; | |
249 | ac >>= 8; | |
250 | bits -= 8; | |
251 | } | |
6b3bd08f JK |
252 | } |
253 | if (ac) | |
254 | return -1; | |
255 | return cp - dst; | |
256 | } | |
257 | ||
b9db0b4a EB |
258 | bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len, |
259 | u32 max_len, u32 *encrypted_len_ret) | |
6b3bd08f | 260 | { |
5dae460c EB |
261 | const struct fscrypt_info *ci = inode->i_crypt_info; |
262 | int padding = 4 << (fscrypt_policy_flags(&ci->ci_policy) & | |
3b6df59b | 263 | FSCRYPT_POLICY_FLAGS_PAD_MASK); |
b9db0b4a EB |
264 | u32 encrypted_len; |
265 | ||
266 | if (orig_len > max_len) | |
267 | return false; | |
268 | encrypted_len = max(orig_len, (u32)FS_CRYPTO_BLOCK_SIZE); | |
269 | encrypted_len = round_up(encrypted_len, padding); | |
270 | *encrypted_len_ret = min(encrypted_len, max_len); | |
271 | return true; | |
6b3bd08f JK |
272 | } |
273 | ||
274 | /** | |
2cbadadc | 275 | * fscrypt_fname_alloc_buffer - allocate a buffer for presented filenames |
6b3bd08f | 276 | * |
2cbadadc EB |
277 | * Allocate a buffer that is large enough to hold any decrypted or encoded |
278 | * filename (null-terminated), for the given maximum encrypted filename length. | |
279 | * | |
280 | * Return: 0 on success, -errno on failure | |
6b3bd08f | 281 | */ |
0b93e1b9 | 282 | int fscrypt_fname_alloc_buffer(const struct inode *inode, |
2cbadadc EB |
283 | u32 max_encrypted_len, |
284 | struct fscrypt_str *crypto_str) | |
6b3bd08f | 285 | { |
edc440e3 | 286 | const u32 max_encoded_len = BASE64_CHARS(FSCRYPT_NOKEY_NAME_MAX); |
2cbadadc | 287 | u32 max_presented_len; |
6b3bd08f | 288 | |
2cbadadc | 289 | max_presented_len = max(max_encoded_len, max_encrypted_len); |
17159420 | 290 | |
2cbadadc EB |
291 | crypto_str->name = kmalloc(max_presented_len + 1, GFP_NOFS); |
292 | if (!crypto_str->name) | |
6b3bd08f | 293 | return -ENOMEM; |
2cbadadc | 294 | crypto_str->len = max_presented_len; |
6b3bd08f JK |
295 | return 0; |
296 | } | |
0b81d077 | 297 | EXPORT_SYMBOL(fscrypt_fname_alloc_buffer); |
6b3bd08f JK |
298 | |
299 | /** | |
2cbadadc | 300 | * fscrypt_fname_free_buffer - free the buffer for presented filenames |
6b3bd08f | 301 | * |
2cbadadc | 302 | * Free the buffer allocated by fscrypt_fname_alloc_buffer(). |
6b3bd08f | 303 | */ |
0b81d077 | 304 | void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str) |
6b3bd08f JK |
305 | { |
306 | if (!crypto_str) | |
307 | return; | |
308 | kfree(crypto_str->name); | |
309 | crypto_str->name = NULL; | |
310 | } | |
0b81d077 | 311 | EXPORT_SYMBOL(fscrypt_fname_free_buffer); |
6b3bd08f JK |
312 | |
313 | /** | |
0b81d077 JK |
314 | * fscrypt_fname_disk_to_usr() - converts a filename from disk space to user |
315 | * space | |
ef1eb3aa EB |
316 | * |
317 | * The caller must have allocated sufficient memory for the @oname string. | |
318 | * | |
edc440e3 DR |
319 | * If the key is available, we'll decrypt the disk name. Otherwise, we'll |
320 | * encode it for presentation in fscrypt_nokey_name format. | |
321 | * See struct fscrypt_nokey_name for details. | |
17159420 | 322 | * |
ef1eb3aa | 323 | * Return: 0 on success, -errno on failure |
6b3bd08f | 324 | */ |
8a4ab0b8 EB |
325 | int fscrypt_fname_disk_to_usr(const struct inode *inode, |
326 | u32 hash, u32 minor_hash, | |
327 | const struct fscrypt_str *iname, | |
328 | struct fscrypt_str *oname) | |
6b3bd08f JK |
329 | { |
330 | const struct qstr qname = FSTR_TO_QSTR(iname); | |
edc440e3 DR |
331 | struct fscrypt_nokey_name nokey_name; |
332 | u32 size; /* size of the unencoded no-key name */ | |
333 | int err; | |
6b3bd08f | 334 | |
0b81d077 | 335 | if (fscrypt_is_dot_dotdot(&qname)) { |
6b3bd08f JK |
336 | oname->name[0] = '.'; |
337 | oname->name[iname->len - 1] = '.'; | |
338 | oname->len = iname->len; | |
ef1eb3aa | 339 | return 0; |
6b3bd08f JK |
340 | } |
341 | ||
0b81d077 | 342 | if (iname->len < FS_CRYPTO_BLOCK_SIZE) |
1dafa51d | 343 | return -EUCLEAN; |
6b3bd08f | 344 | |
e37a784d | 345 | if (fscrypt_has_encryption_key(inode)) |
0b81d077 JK |
346 | return fname_decrypt(inode, iname, oname); |
347 | ||
edc440e3 DR |
348 | /* |
349 | * Sanity check that struct fscrypt_nokey_name doesn't have padding | |
350 | * between fields and that its encoded size never exceeds NAME_MAX. | |
351 | */ | |
352 | BUILD_BUG_ON(offsetofend(struct fscrypt_nokey_name, dirhash) != | |
353 | offsetof(struct fscrypt_nokey_name, bytes)); | |
354 | BUILD_BUG_ON(offsetofend(struct fscrypt_nokey_name, bytes) != | |
355 | offsetof(struct fscrypt_nokey_name, sha256)); | |
356 | BUILD_BUG_ON(BASE64_CHARS(FSCRYPT_NOKEY_NAME_MAX) > NAME_MAX); | |
357 | ||
6b3bd08f | 358 | if (hash) { |
edc440e3 DR |
359 | nokey_name.dirhash[0] = hash; |
360 | nokey_name.dirhash[1] = minor_hash; | |
0b81d077 | 361 | } else { |
edc440e3 DR |
362 | nokey_name.dirhash[0] = 0; |
363 | nokey_name.dirhash[1] = 0; | |
0b81d077 | 364 | } |
edc440e3 DR |
365 | if (iname->len <= sizeof(nokey_name.bytes)) { |
366 | memcpy(nokey_name.bytes, iname->name, iname->len); | |
367 | size = offsetof(struct fscrypt_nokey_name, bytes[iname->len]); | |
368 | } else { | |
369 | memcpy(nokey_name.bytes, iname->name, sizeof(nokey_name.bytes)); | |
370 | /* Compute strong hash of remaining part of name. */ | |
371 | err = fscrypt_do_sha256(&iname->name[sizeof(nokey_name.bytes)], | |
372 | iname->len - sizeof(nokey_name.bytes), | |
373 | nokey_name.sha256); | |
374 | if (err) | |
375 | return err; | |
376 | size = FSCRYPT_NOKEY_NAME_MAX; | |
377 | } | |
378 | oname->len = base64_encode((const u8 *)&nokey_name, size, oname->name); | |
ef1eb3aa | 379 | return 0; |
6b3bd08f | 380 | } |
0b81d077 | 381 | EXPORT_SYMBOL(fscrypt_fname_disk_to_usr); |
6b3bd08f | 382 | |
17159420 EB |
383 | /** |
384 | * fscrypt_setup_filename() - prepare to search a possibly encrypted directory | |
385 | * @dir: the directory that will be searched | |
386 | * @iname: the user-provided filename being searched for | |
387 | * @lookup: 1 if we're allowed to proceed without the key because it's | |
388 | * ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot | |
389 | * proceed without the key because we're going to create the dir_entry. | |
390 | * @fname: the filename information to be filled in | |
391 | * | |
392 | * Given a user-provided filename @iname, this function sets @fname->disk_name | |
393 | * to the name that would be stored in the on-disk directory entry, if possible. | |
394 | * If the directory is unencrypted this is simply @iname. Else, if we have the | |
395 | * directory's encryption key, then @iname is the plaintext, so we encrypt it to | |
396 | * get the disk_name. | |
397 | * | |
398 | * Else, for keyless @lookup operations, @iname is the presented ciphertext, so | |
edc440e3 | 399 | * we decode it to get the fscrypt_nokey_name. Non-@lookup operations will be |
17159420 EB |
400 | * impossible in this case, so we fail them with ENOKEY. |
401 | * | |
402 | * If successful, fscrypt_free_filename() must be called later to clean up. | |
403 | * | |
404 | * Return: 0 on success, -errno on failure | |
405 | */ | |
0b81d077 JK |
406 | int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname, |
407 | int lookup, struct fscrypt_name *fname) | |
6b3bd08f | 408 | { |
edc440e3 | 409 | struct fscrypt_nokey_name *nokey_name; |
17159420 | 410 | int ret; |
6b3bd08f | 411 | |
0b81d077 | 412 | memset(fname, 0, sizeof(struct fscrypt_name)); |
6b3bd08f JK |
413 | fname->usr_fname = iname; |
414 | ||
e0428a26 | 415 | if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) { |
6b3bd08f JK |
416 | fname->disk_name.name = (unsigned char *)iname->name; |
417 | fname->disk_name.len = iname->len; | |
7bf4b557 | 418 | return 0; |
6b3bd08f | 419 | } |
1b53cf98 | 420 | ret = fscrypt_get_encryption_info(dir); |
17bfde60 | 421 | if (ret) |
6b3bd08f | 422 | return ret; |
0b81d077 | 423 | |
e37a784d | 424 | if (fscrypt_has_encryption_key(dir)) { |
b9db0b4a | 425 | if (!fscrypt_fname_encrypted_size(dir, iname->len, |
e12ee683 | 426 | dir->i_sb->s_cop->max_namelen, |
b9db0b4a | 427 | &fname->crypto_buf.len)) |
50c961de | 428 | return -ENAMETOOLONG; |
50c961de EB |
429 | fname->crypto_buf.name = kmalloc(fname->crypto_buf.len, |
430 | GFP_NOFS); | |
431 | if (!fname->crypto_buf.name) | |
432 | return -ENOMEM; | |
433 | ||
1b3b827e EB |
434 | ret = fscrypt_fname_encrypt(dir, iname, fname->crypto_buf.name, |
435 | fname->crypto_buf.len); | |
ef1eb3aa | 436 | if (ret) |
e5e0906b | 437 | goto errout; |
6b3bd08f JK |
438 | fname->disk_name.name = fname->crypto_buf.name; |
439 | fname->disk_name.len = fname->crypto_buf.len; | |
7bf4b557 | 440 | return 0; |
6b3bd08f | 441 | } |
e5e0906b | 442 | if (!lookup) |
54475f53 | 443 | return -ENOKEY; |
b01531db | 444 | fname->is_ciphertext_name = true; |
6b3bd08f | 445 | |
0b81d077 JK |
446 | /* |
447 | * We don't have the key and we are doing a lookup; decode the | |
6b3bd08f JK |
448 | * user-supplied name |
449 | */ | |
e5e0906b | 450 | |
edc440e3 DR |
451 | if (iname->len > BASE64_CHARS(FSCRYPT_NOKEY_NAME_MAX)) |
452 | return -ENOENT; | |
453 | ||
454 | fname->crypto_buf.name = kmalloc(FSCRYPT_NOKEY_NAME_MAX, GFP_KERNEL); | |
e5e0906b JK |
455 | if (fname->crypto_buf.name == NULL) |
456 | return -ENOMEM; | |
0b81d077 | 457 | |
edc440e3 DR |
458 | ret = base64_decode(iname->name, iname->len, fname->crypto_buf.name); |
459 | if (ret < (int)offsetof(struct fscrypt_nokey_name, bytes[1]) || | |
460 | (ret > offsetof(struct fscrypt_nokey_name, sha256) && | |
461 | ret != FSCRYPT_NOKEY_NAME_MAX)) { | |
6b3bd08f | 462 | ret = -ENOENT; |
e5e0906b | 463 | goto errout; |
6b3bd08f JK |
464 | } |
465 | fname->crypto_buf.len = ret; | |
edc440e3 DR |
466 | |
467 | nokey_name = (void *)fname->crypto_buf.name; | |
468 | fname->hash = nokey_name->dirhash[0]; | |
469 | fname->minor_hash = nokey_name->dirhash[1]; | |
470 | if (ret != FSCRYPT_NOKEY_NAME_MAX) { | |
471 | /* The full ciphertext filename is available. */ | |
472 | fname->disk_name.name = nokey_name->bytes; | |
473 | fname->disk_name.len = | |
474 | ret - offsetof(struct fscrypt_nokey_name, bytes); | |
6b3bd08f | 475 | } |
7bf4b557 | 476 | return 0; |
0b81d077 | 477 | |
e5e0906b | 478 | errout: |
50c961de | 479 | kfree(fname->crypto_buf.name); |
6b3bd08f JK |
480 | return ret; |
481 | } | |
0b81d077 | 482 | EXPORT_SYMBOL(fscrypt_setup_filename); |
2ebdef6d | 483 | |
edc440e3 DR |
484 | /** |
485 | * fscrypt_match_name() - test whether the given name matches a directory entry | |
486 | * @fname: the name being searched for | |
487 | * @de_name: the name from the directory entry | |
488 | * @de_name_len: the length of @de_name in bytes | |
489 | * | |
490 | * Normally @fname->disk_name will be set, and in that case we simply compare | |
491 | * that to the name stored in the directory entry. The only exception is that | |
492 | * if we don't have the key for an encrypted directory and the name we're | |
493 | * looking for is very long, then we won't have the full disk_name and instead | |
494 | * we'll need to match against a fscrypt_nokey_name that includes a strong hash. | |
495 | * | |
496 | * Return: %true if the name matches, otherwise %false. | |
497 | */ | |
498 | bool fscrypt_match_name(const struct fscrypt_name *fname, | |
499 | const u8 *de_name, u32 de_name_len) | |
500 | { | |
501 | const struct fscrypt_nokey_name *nokey_name = | |
502 | (const void *)fname->crypto_buf.name; | |
503 | u8 sha256[SHA256_DIGEST_SIZE]; | |
504 | ||
505 | if (likely(fname->disk_name.name)) { | |
506 | if (de_name_len != fname->disk_name.len) | |
507 | return false; | |
508 | return !memcmp(de_name, fname->disk_name.name, de_name_len); | |
509 | } | |
510 | if (de_name_len <= sizeof(nokey_name->bytes)) | |
511 | return false; | |
512 | if (memcmp(de_name, nokey_name->bytes, sizeof(nokey_name->bytes))) | |
513 | return false; | |
514 | if (fscrypt_do_sha256(&de_name[sizeof(nokey_name->bytes)], | |
515 | de_name_len - sizeof(nokey_name->bytes), sha256)) | |
516 | return false; | |
517 | return !memcmp(sha256, nokey_name->sha256, sizeof(sha256)); | |
518 | } | |
519 | EXPORT_SYMBOL_GPL(fscrypt_match_name); | |
520 | ||
aa408f83 DR |
521 | /** |
522 | * fscrypt_fname_siphash() - calculate the SipHash of a filename | |
523 | * @dir: the parent directory | |
524 | * @name: the filename to calculate the SipHash of | |
525 | * | |
526 | * Given a plaintext filename @name and a directory @dir which uses SipHash as | |
527 | * its dirhash method and has had its fscrypt key set up, this function | |
528 | * calculates the SipHash of that name using the directory's secret dirhash key. | |
529 | * | |
530 | * Return: the SipHash of @name using the hash key of @dir | |
531 | */ | |
532 | u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name) | |
533 | { | |
534 | const struct fscrypt_info *ci = dir->i_crypt_info; | |
535 | ||
536 | WARN_ON(!ci->ci_dirhash_key_initialized); | |
537 | ||
538 | return siphash(name->name, name->len, &ci->ci_dirhash_key); | |
539 | } | |
540 | EXPORT_SYMBOL_GPL(fscrypt_fname_siphash); | |
541 | ||
2ebdef6d EB |
542 | /* |
543 | * Validate dentries in encrypted directories to make sure we aren't potentially | |
544 | * caching stale dentries after a key has been added. | |
545 | */ | |
546 | static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags) | |
547 | { | |
548 | struct dentry *dir; | |
549 | int err; | |
550 | int valid; | |
551 | ||
552 | /* | |
553 | * Plaintext names are always valid, since fscrypt doesn't support | |
554 | * reverting to ciphertext names without evicting the directory's inode | |
555 | * -- which implies eviction of the dentries in the directory. | |
556 | */ | |
557 | if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME)) | |
558 | return 1; | |
559 | ||
560 | /* | |
561 | * Ciphertext name; valid if the directory's key is still unavailable. | |
562 | * | |
563 | * Although fscrypt forbids rename() on ciphertext names, we still must | |
564 | * use dget_parent() here rather than use ->d_parent directly. That's | |
565 | * because a corrupted fs image may contain directory hard links, which | |
566 | * the VFS handles by moving the directory's dentry tree in the dcache | |
567 | * each time ->lookup() finds the directory and it already has a dentry | |
568 | * elsewhere. Thus ->d_parent can be changing, and we must safely grab | |
569 | * a reference to some ->d_parent to prevent it from being freed. | |
570 | */ | |
571 | ||
572 | if (flags & LOOKUP_RCU) | |
573 | return -ECHILD; | |
574 | ||
575 | dir = dget_parent(dentry); | |
576 | err = fscrypt_get_encryption_info(d_inode(dir)); | |
577 | valid = !fscrypt_has_encryption_key(d_inode(dir)); | |
578 | dput(dir); | |
579 | ||
580 | if (err < 0) | |
581 | return err; | |
582 | ||
583 | return valid; | |
584 | } | |
585 | ||
586 | const struct dentry_operations fscrypt_d_ops = { | |
587 | .d_revalidate = fscrypt_d_revalidate, | |
588 | }; |