1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * This contains functions for filename crypto management
4 *
5 * Copyright (C) 2015, Google, Inc.
6 * Copyright (C) 2015, Motorola Mobility
7 *
8 * Written by Uday Savagaonkar, 2014.
9 * Modified by Jaegeuk Kim, 2015.
10 *
11 * This has not yet undergone a rigorous security audit.
12 */
13
14 #include <linux/namei.h>
15 #include <linux/scatterlist.h>
16 #include <crypto/hash.h>
17 #include <crypto/sha.h>
18 #include <crypto/skcipher.h>
19 #include "fscrypt_private.h"
20
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
fscrypt_do_sha256(const u8 * data,unsigned int data_len,u8 * result)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
fscrypt_is_dot_dotdot(const struct qstr * str)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
106 /**
107 * fscrypt_fname_encrypt() - encrypt a filename
108 * @inode: inode of the parent directory (for regular filenames)
109 * or of the symlink (for symlink targets)
110 * @iname: the filename to encrypt
111 * @out: (output) the encrypted filename
112 * @olen: size of the encrypted filename. It must be at least @iname->len.
113 * Any extra space is filled with NUL padding before encryption.
114 *
115 * Return: 0 on success, -errno on failure
116 */
fscrypt_fname_encrypt(const struct inode * inode,const struct qstr * iname,u8 * out,unsigned int olen)117 int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
118 u8 *out, unsigned int olen)
119 {
120 struct skcipher_request *req = NULL;
121 DECLARE_CRYPTO_WAIT(wait);
122 const struct fscrypt_info *ci = inode->i_crypt_info;
123 struct crypto_skcipher *tfm = ci->ci_key.tfm;
124 union fscrypt_iv iv;
125 struct scatterlist sg;
126 int res;
127
128 /*
129 * Copy the filename to the output buffer for encrypting in-place and
130 * pad it with the needed number of NUL bytes.
131 */
132 if (WARN_ON(olen < iname->len))
133 return -ENOBUFS;
134 memcpy(out, iname->name, iname->len);
135 memset(out + iname->len, 0, olen - iname->len);
136
137 /* Initialize the IV */
138 fscrypt_generate_iv(&iv, 0, ci);
139
140 /* Set up the encryption request */
141 req = skcipher_request_alloc(tfm, GFP_NOFS);
142 if (!req)
143 return -ENOMEM;
144 skcipher_request_set_callback(req,
145 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
146 crypto_req_done, &wait);
147 sg_init_one(&sg, out, olen);
148 skcipher_request_set_crypt(req, &sg, &sg, olen, &iv);
149
150 /* Do the encryption */
151 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
152 skcipher_request_free(req);
153 if (res < 0) {
154 fscrypt_err(inode, "Filename encryption failed: %d", res);
155 return res;
156 }
157
158 return 0;
159 }
160
161 /**
162 * fname_decrypt() - decrypt a filename
163 * @inode: inode of the parent directory (for regular filenames)
164 * or of the symlink (for symlink targets)
165 * @iname: the encrypted filename to decrypt
166 * @oname: (output) the decrypted filename. The caller must have allocated
167 * enough space for this, e.g. using fscrypt_fname_alloc_buffer().
168 *
169 * Return: 0 on success, -errno on failure
170 */
fname_decrypt(const struct inode * inode,const struct fscrypt_str * iname,struct fscrypt_str * oname)171 static int fname_decrypt(const struct inode *inode,
172 const struct fscrypt_str *iname,
173 struct fscrypt_str *oname)
174 {
175 struct skcipher_request *req = NULL;
176 DECLARE_CRYPTO_WAIT(wait);
177 struct scatterlist src_sg, dst_sg;
178 const struct fscrypt_info *ci = inode->i_crypt_info;
179 struct crypto_skcipher *tfm = ci->ci_key.tfm;
180 union fscrypt_iv iv;
181 int res;
182
183 /* Allocate request */
184 req = skcipher_request_alloc(tfm, GFP_NOFS);
185 if (!req)
186 return -ENOMEM;
187 skcipher_request_set_callback(req,
188 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
189 crypto_req_done, &wait);
190
191 /* Initialize IV */
192 fscrypt_generate_iv(&iv, 0, ci);
193
194 /* Create decryption request */
195 sg_init_one(&src_sg, iname->name, iname->len);
196 sg_init_one(&dst_sg, oname->name, oname->len);
197 skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, &iv);
198 res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
199 skcipher_request_free(req);
200 if (res < 0) {
201 fscrypt_err(inode, "Filename decryption failed: %d", res);
202 return res;
203 }
204
205 oname->len = strnlen(oname->name, iname->len);
206 return 0;
207 }
208
209 static const char lookup_table[65] =
210 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
211
212 #define BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3)
213
214 /**
215 * base64_encode() - base64-encode some bytes
216 * @src: the bytes to encode
217 * @len: number of bytes to encode
218 * @dst: (output) the base64-encoded string. Not NUL-terminated.
219 *
220 * Encodes the input string using characters from the set [A-Za-z0-9+,].
221 * The encoded string is roughly 4/3 times the size of the input string.
222 *
223 * Return: length of the encoded string
224 */
base64_encode(const u8 * src,int len,char * dst)225 static int base64_encode(const u8 *src, int len, char *dst)
226 {
227 int i, bits = 0, ac = 0;
228 char *cp = dst;
229
230 for (i = 0; i < len; i++) {
231 ac += src[i] << bits;
232 bits += 8;
233 do {
234 *cp++ = lookup_table[ac & 0x3f];
235 ac >>= 6;
236 bits -= 6;
237 } while (bits >= 6);
238 }
239 if (bits)
240 *cp++ = lookup_table[ac & 0x3f];
241 return cp - dst;
242 }
243
base64_decode(const char * src,int len,u8 * dst)244 static int base64_decode(const char *src, int len, u8 *dst)
245 {
246 int i, bits = 0, ac = 0;
247 const char *p;
248 u8 *cp = dst;
249
250 for (i = 0; i < len; i++) {
251 p = strchr(lookup_table, src[i]);
252 if (p == NULL || src[i] == 0)
253 return -2;
254 ac += (p - lookup_table) << bits;
255 bits += 6;
256 if (bits >= 8) {
257 *cp++ = ac & 0xff;
258 ac >>= 8;
259 bits -= 8;
260 }
261 }
262 if (ac)
263 return -1;
264 return cp - dst;
265 }
266
fscrypt_fname_encrypted_size(const struct inode * inode,u32 orig_len,u32 max_len,u32 * encrypted_len_ret)267 bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
268 u32 max_len, u32 *encrypted_len_ret)
269 {
270 const struct fscrypt_info *ci = inode->i_crypt_info;
271 int padding = 4 << (fscrypt_policy_flags(&ci->ci_policy) &
272 FSCRYPT_POLICY_FLAGS_PAD_MASK);
273 u32 encrypted_len;
274
275 if (orig_len > max_len)
276 return false;
277 encrypted_len = max(orig_len, (u32)FS_CRYPTO_BLOCK_SIZE);
278 encrypted_len = round_up(encrypted_len, padding);
279 *encrypted_len_ret = min(encrypted_len, max_len);
280 return true;
281 }
282
283 /**
284 * fscrypt_fname_alloc_buffer() - allocate a buffer for presented filenames
285 * @inode: inode of the parent directory (for regular filenames)
286 * or of the symlink (for symlink targets)
287 * @max_encrypted_len: maximum length of encrypted filenames the buffer will be
288 * used to present
289 * @crypto_str: (output) buffer to allocate
290 *
291 * Allocate a buffer that is large enough to hold any decrypted or encoded
292 * filename (null-terminated), for the given maximum encrypted filename length.
293 *
294 * Return: 0 on success, -errno on failure
295 */
fscrypt_fname_alloc_buffer(const struct inode * inode,u32 max_encrypted_len,struct fscrypt_str * crypto_str)296 int fscrypt_fname_alloc_buffer(const struct inode *inode,
297 u32 max_encrypted_len,
298 struct fscrypt_str *crypto_str)
299 {
300 const u32 max_encoded_len = BASE64_CHARS(FSCRYPT_NOKEY_NAME_MAX);
301 u32 max_presented_len;
302
303 max_presented_len = max(max_encoded_len, max_encrypted_len);
304
305 crypto_str->name = kmalloc(max_presented_len + 1, GFP_NOFS);
306 if (!crypto_str->name)
307 return -ENOMEM;
308 crypto_str->len = max_presented_len;
309 return 0;
310 }
311 EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
312
313 /**
314 * fscrypt_fname_free_buffer() - free a buffer for presented filenames
315 * @crypto_str: the buffer to free
316 *
317 * Free a buffer that was allocated by fscrypt_fname_alloc_buffer().
318 */
fscrypt_fname_free_buffer(struct fscrypt_str * crypto_str)319 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
320 {
321 if (!crypto_str)
322 return;
323 kfree(crypto_str->name);
324 crypto_str->name = NULL;
325 }
326 EXPORT_SYMBOL(fscrypt_fname_free_buffer);
327
328 /**
329 * fscrypt_fname_disk_to_usr() - convert an encrypted filename to
330 * user-presentable form
331 * @inode: inode of the parent directory (for regular filenames)
332 * or of the symlink (for symlink targets)
333 * @hash: first part of the name's dirhash, if applicable. This only needs to
334 * be provided if the filename is located in an indexed directory whose
335 * encryption key may be unavailable. Not needed for symlink targets.
336 * @minor_hash: second part of the name's dirhash, if applicable
337 * @iname: encrypted filename to convert. May also be "." or "..", which
338 * aren't actually encrypted.
339 * @oname: output buffer for the user-presentable filename. The caller must
340 * have allocated enough space for this, e.g. using
341 * fscrypt_fname_alloc_buffer().
342 *
343 * If the key is available, we'll decrypt the disk name. Otherwise, we'll
344 * encode it for presentation in fscrypt_nokey_name format.
345 * See struct fscrypt_nokey_name for details.
346 *
347 * Return: 0 on success, -errno on failure
348 */
fscrypt_fname_disk_to_usr(const struct inode * inode,u32 hash,u32 minor_hash,const struct fscrypt_str * iname,struct fscrypt_str * oname)349 int fscrypt_fname_disk_to_usr(const struct inode *inode,
350 u32 hash, u32 minor_hash,
351 const struct fscrypt_str *iname,
352 struct fscrypt_str *oname)
353 {
354 const struct qstr qname = FSTR_TO_QSTR(iname);
355 struct fscrypt_nokey_name nokey_name;
356 u32 size; /* size of the unencoded no-key name */
357 int err;
358
359 if (fscrypt_is_dot_dotdot(&qname)) {
360 oname->name[0] = '.';
361 oname->name[iname->len - 1] = '.';
362 oname->len = iname->len;
363 return 0;
364 }
365
366 if (iname->len < FS_CRYPTO_BLOCK_SIZE)
367 return -EUCLEAN;
368
369 if (fscrypt_has_encryption_key(inode))
370 return fname_decrypt(inode, iname, oname);
371
372 /*
373 * Sanity check that struct fscrypt_nokey_name doesn't have padding
374 * between fields and that its encoded size never exceeds NAME_MAX.
375 */
376 BUILD_BUG_ON(offsetofend(struct fscrypt_nokey_name, dirhash) !=
377 offsetof(struct fscrypt_nokey_name, bytes));
378 BUILD_BUG_ON(offsetofend(struct fscrypt_nokey_name, bytes) !=
379 offsetof(struct fscrypt_nokey_name, sha256));
380 BUILD_BUG_ON(BASE64_CHARS(FSCRYPT_NOKEY_NAME_MAX) > NAME_MAX);
381
382 nokey_name.dirhash[0] = hash;
383 nokey_name.dirhash[1] = minor_hash;
384 if (iname->len <= sizeof(nokey_name.bytes)) {
385 memcpy(nokey_name.bytes, iname->name, iname->len);
386 size = offsetof(struct fscrypt_nokey_name, bytes[iname->len]);
387 } else {
388 memcpy(nokey_name.bytes, iname->name, sizeof(nokey_name.bytes));
389 /* Compute strong hash of remaining part of name. */
390 err = fscrypt_do_sha256(&iname->name[sizeof(nokey_name.bytes)],
391 iname->len - sizeof(nokey_name.bytes),
392 nokey_name.sha256);
393 if (err)
394 return err;
395 size = FSCRYPT_NOKEY_NAME_MAX;
396 }
397 oname->len = base64_encode((const u8 *)&nokey_name, size, oname->name);
398 return 0;
399 }
400 EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
401
402 /**
403 * fscrypt_setup_filename() - prepare to search a possibly encrypted directory
404 * @dir: the directory that will be searched
405 * @iname: the user-provided filename being searched for
406 * @lookup: 1 if we're allowed to proceed without the key because it's
407 * ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
408 * proceed without the key because we're going to create the dir_entry.
409 * @fname: the filename information to be filled in
410 *
411 * Given a user-provided filename @iname, this function sets @fname->disk_name
412 * to the name that would be stored in the on-disk directory entry, if possible.
413 * If the directory is unencrypted this is simply @iname. Else, if we have the
414 * directory's encryption key, then @iname is the plaintext, so we encrypt it to
415 * get the disk_name.
416 *
417 * Else, for keyless @lookup operations, @iname is the presented ciphertext, so
418 * we decode it to get the fscrypt_nokey_name. Non-@lookup operations will be
419 * impossible in this case, so we fail them with ENOKEY.
420 *
421 * If successful, fscrypt_free_filename() must be called later to clean up.
422 *
423 * Return: 0 on success, -errno on failure
424 */
fscrypt_setup_filename(struct inode * dir,const struct qstr * iname,int lookup,struct fscrypt_name * fname)425 int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
426 int lookup, struct fscrypt_name *fname)
427 {
428 struct fscrypt_nokey_name *nokey_name;
429 int ret;
430
431 memset(fname, 0, sizeof(struct fscrypt_name));
432 fname->usr_fname = iname;
433
434 if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) {
435 fname->disk_name.name = (unsigned char *)iname->name;
436 fname->disk_name.len = iname->len;
437 return 0;
438 }
439 ret = fscrypt_get_encryption_info(dir);
440 if (ret)
441 return ret;
442
443 if (fscrypt_has_encryption_key(dir)) {
444 if (!fscrypt_fname_encrypted_size(dir, iname->len,
445 dir->i_sb->s_cop->max_namelen,
446 &fname->crypto_buf.len))
447 return -ENAMETOOLONG;
448 fname->crypto_buf.name = kmalloc(fname->crypto_buf.len,
449 GFP_NOFS);
450 if (!fname->crypto_buf.name)
451 return -ENOMEM;
452
453 ret = fscrypt_fname_encrypt(dir, iname, fname->crypto_buf.name,
454 fname->crypto_buf.len);
455 if (ret)
456 goto errout;
457 fname->disk_name.name = fname->crypto_buf.name;
458 fname->disk_name.len = fname->crypto_buf.len;
459 return 0;
460 }
461 if (!lookup)
462 return -ENOKEY;
463 fname->is_ciphertext_name = true;
464
465 /*
466 * We don't have the key and we are doing a lookup; decode the
467 * user-supplied name
468 */
469
470 if (iname->len > BASE64_CHARS(FSCRYPT_NOKEY_NAME_MAX))
471 return -ENOENT;
472
473 fname->crypto_buf.name = kmalloc(FSCRYPT_NOKEY_NAME_MAX, GFP_KERNEL);
474 if (fname->crypto_buf.name == NULL)
475 return -ENOMEM;
476
477 ret = base64_decode(iname->name, iname->len, fname->crypto_buf.name);
478 if (ret < (int)offsetof(struct fscrypt_nokey_name, bytes[1]) ||
479 (ret > offsetof(struct fscrypt_nokey_name, sha256) &&
480 ret != FSCRYPT_NOKEY_NAME_MAX)) {
481 ret = -ENOENT;
482 goto errout;
483 }
484 fname->crypto_buf.len = ret;
485
486 nokey_name = (void *)fname->crypto_buf.name;
487 fname->hash = nokey_name->dirhash[0];
488 fname->minor_hash = nokey_name->dirhash[1];
489 if (ret != FSCRYPT_NOKEY_NAME_MAX) {
490 /* The full ciphertext filename is available. */
491 fname->disk_name.name = nokey_name->bytes;
492 fname->disk_name.len =
493 ret - offsetof(struct fscrypt_nokey_name, bytes);
494 }
495 return 0;
496
497 errout:
498 kfree(fname->crypto_buf.name);
499 return ret;
500 }
501 EXPORT_SYMBOL(fscrypt_setup_filename);
502
503 /**
504 * fscrypt_match_name() - test whether the given name matches a directory entry
505 * @fname: the name being searched for
506 * @de_name: the name from the directory entry
507 * @de_name_len: the length of @de_name in bytes
508 *
509 * Normally @fname->disk_name will be set, and in that case we simply compare
510 * that to the name stored in the directory entry. The only exception is that
511 * if we don't have the key for an encrypted directory and the name we're
512 * looking for is very long, then we won't have the full disk_name and instead
513 * we'll need to match against a fscrypt_nokey_name that includes a strong hash.
514 *
515 * Return: %true if the name matches, otherwise %false.
516 */
fscrypt_match_name(const struct fscrypt_name * fname,const u8 * de_name,u32 de_name_len)517 bool fscrypt_match_name(const struct fscrypt_name *fname,
518 const u8 *de_name, u32 de_name_len)
519 {
520 const struct fscrypt_nokey_name *nokey_name =
521 (const void *)fname->crypto_buf.name;
522 u8 sha256[SHA256_DIGEST_SIZE];
523
524 if (likely(fname->disk_name.name)) {
525 if (de_name_len != fname->disk_name.len)
526 return false;
527 return !memcmp(de_name, fname->disk_name.name, de_name_len);
528 }
529 if (de_name_len <= sizeof(nokey_name->bytes))
530 return false;
531 if (memcmp(de_name, nokey_name->bytes, sizeof(nokey_name->bytes)))
532 return false;
533 if (fscrypt_do_sha256(&de_name[sizeof(nokey_name->bytes)],
534 de_name_len - sizeof(nokey_name->bytes), sha256))
535 return false;
536 return !memcmp(sha256, nokey_name->sha256, sizeof(sha256));
537 }
538 EXPORT_SYMBOL_GPL(fscrypt_match_name);
539
540 /**
541 * fscrypt_fname_siphash() - calculate the SipHash of a filename
542 * @dir: the parent directory
543 * @name: the filename to calculate the SipHash of
544 *
545 * Given a plaintext filename @name and a directory @dir which uses SipHash as
546 * its dirhash method and has had its fscrypt key set up, this function
547 * calculates the SipHash of that name using the directory's secret dirhash key.
548 *
549 * Return: the SipHash of @name using the hash key of @dir
550 */
fscrypt_fname_siphash(const struct inode * dir,const struct qstr * name)551 u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name)
552 {
553 const struct fscrypt_info *ci = dir->i_crypt_info;
554
555 WARN_ON(!ci->ci_dirhash_key_initialized);
556
557 return siphash(name->name, name->len, &ci->ci_dirhash_key);
558 }
559 EXPORT_SYMBOL_GPL(fscrypt_fname_siphash);
560
561 /*
562 * Validate dentries in encrypted directories to make sure we aren't potentially
563 * caching stale dentries after a key has been added.
564 */
fscrypt_d_revalidate(struct dentry * dentry,unsigned int flags)565 int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)
566 {
567 struct dentry *dir;
568 int err;
569 int valid;
570
571 /*
572 * Plaintext names are always valid, since fscrypt doesn't support
573 * reverting to ciphertext names without evicting the directory's inode
574 * -- which implies eviction of the dentries in the directory.
575 */
576 if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME))
577 return 1;
578
579 /*
580 * Ciphertext name; valid if the directory's key is still unavailable.
581 *
582 * Although fscrypt forbids rename() on ciphertext names, we still must
583 * use dget_parent() here rather than use ->d_parent directly. That's
584 * because a corrupted fs image may contain directory hard links, which
585 * the VFS handles by moving the directory's dentry tree in the dcache
586 * each time ->lookup() finds the directory and it already has a dentry
587 * elsewhere. Thus ->d_parent can be changing, and we must safely grab
588 * a reference to some ->d_parent to prevent it from being freed.
589 */
590
591 if (flags & LOOKUP_RCU)
592 return -ECHILD;
593
594 dir = dget_parent(dentry);
595 err = fscrypt_get_encryption_info(d_inode(dir));
596 valid = !fscrypt_has_encryption_key(d_inode(dir));
597 dput(dir);
598
599 if (err < 0)
600 return err;
601
602 return valid;
603 }
604 EXPORT_SYMBOL_GPL(fscrypt_d_revalidate);
605