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