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