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
fscrypt_is_dot_dotdot(const struct qstr * str)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 */
fscrypt_fname_encrypt(const struct inode * inode,const struct qstr * iname,u8 * out,unsigned int olen)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 = inode->i_crypt_info;
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 */
fname_decrypt(const struct inode * inode,const struct fscrypt_str * iname,struct fscrypt_str * oname)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 = inode->i_crypt_info;
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 */
fscrypt_base64url_encode(const u8 * src,int srclen,char * dst)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 */
fscrypt_base64url_decode(const char * src,int srclen,u8 * dst)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
__fscrypt_fname_encrypted_size(const union fscrypt_policy * policy,u32 orig_len,u32 max_len,u32 * encrypted_len_ret)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 */
fscrypt_fname_encrypted_size(const struct inode * inode,u32 orig_len,u32 max_len,u32 * encrypted_len_ret)274 bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
275 u32 max_len, u32 *encrypted_len_ret)
276 {
277 return __fscrypt_fname_encrypted_size(&inode->i_crypt_info->ci_policy,
278 orig_len, max_len,
279 encrypted_len_ret);
280 }
281 EXPORT_SYMBOL_GPL(fscrypt_fname_encrypted_size);
282
283 /**
284 * fscrypt_fname_alloc_buffer() - allocate a buffer for presented filenames
285 * @max_encrypted_len: maximum length of encrypted filenames the buffer will be
286 * used to present
287 * @crypto_str: (output) buffer to allocate
288 *
289 * Allocate a buffer that is large enough to hold any decrypted or encoded
290 * filename (null-terminated), for the given maximum encrypted filename length.
291 *
292 * Return: 0 on success, -errno on failure
293 */
fscrypt_fname_alloc_buffer(u32 max_encrypted_len,struct fscrypt_str * crypto_str)294 int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
295 struct fscrypt_str *crypto_str)
296 {
297 u32 max_presented_len = max_t(u32, FSCRYPT_NOKEY_NAME_MAX_ENCODED,
298 max_encrypted_len);
299
300 crypto_str->name = kmalloc(max_presented_len + 1, GFP_NOFS);
301 if (!crypto_str->name)
302 return -ENOMEM;
303 crypto_str->len = max_presented_len;
304 return 0;
305 }
306 EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
307
308 /**
309 * fscrypt_fname_free_buffer() - free a buffer for presented filenames
310 * @crypto_str: the buffer to free
311 *
312 * Free a buffer that was allocated by fscrypt_fname_alloc_buffer().
313 */
fscrypt_fname_free_buffer(struct fscrypt_str * crypto_str)314 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
315 {
316 if (!crypto_str)
317 return;
318 kfree(crypto_str->name);
319 crypto_str->name = NULL;
320 }
321 EXPORT_SYMBOL(fscrypt_fname_free_buffer);
322
323 /**
324 * fscrypt_fname_disk_to_usr() - convert an encrypted filename to
325 * user-presentable form
326 * @inode: inode of the parent directory (for regular filenames)
327 * or of the symlink (for symlink targets)
328 * @hash: first part of the name's dirhash, if applicable. This only needs to
329 * be provided if the filename is located in an indexed directory whose
330 * encryption key may be unavailable. Not needed for symlink targets.
331 * @minor_hash: second part of the name's dirhash, if applicable
332 * @iname: encrypted filename to convert. May also be "." or "..", which
333 * aren't actually encrypted.
334 * @oname: output buffer for the user-presentable filename. The caller must
335 * have allocated enough space for this, e.g. using
336 * fscrypt_fname_alloc_buffer().
337 *
338 * If the key is available, we'll decrypt the disk name. Otherwise, we'll
339 * encode it for presentation in fscrypt_nokey_name format.
340 * See struct fscrypt_nokey_name for details.
341 *
342 * Return: 0 on success, -errno on failure
343 */
fscrypt_fname_disk_to_usr(const struct inode * inode,u32 hash,u32 minor_hash,const struct fscrypt_str * iname,struct fscrypt_str * oname)344 int fscrypt_fname_disk_to_usr(const struct inode *inode,
345 u32 hash, u32 minor_hash,
346 const struct fscrypt_str *iname,
347 struct fscrypt_str *oname)
348 {
349 const struct qstr qname = FSTR_TO_QSTR(iname);
350 struct fscrypt_nokey_name nokey_name;
351 u32 size; /* size of the unencoded no-key name */
352
353 if (fscrypt_is_dot_dotdot(&qname)) {
354 oname->name[0] = '.';
355 oname->name[iname->len - 1] = '.';
356 oname->len = iname->len;
357 return 0;
358 }
359
360 if (iname->len < FSCRYPT_FNAME_MIN_MSG_LEN)
361 return -EUCLEAN;
362
363 if (fscrypt_has_encryption_key(inode))
364 return fname_decrypt(inode, iname, oname);
365
366 /*
367 * Sanity check that struct fscrypt_nokey_name doesn't have padding
368 * between fields and that its encoded size never exceeds NAME_MAX.
369 */
370 BUILD_BUG_ON(offsetofend(struct fscrypt_nokey_name, dirhash) !=
371 offsetof(struct fscrypt_nokey_name, bytes));
372 BUILD_BUG_ON(offsetofend(struct fscrypt_nokey_name, bytes) !=
373 offsetof(struct fscrypt_nokey_name, sha256));
374 BUILD_BUG_ON(FSCRYPT_NOKEY_NAME_MAX_ENCODED > NAME_MAX);
375
376 nokey_name.dirhash[0] = hash;
377 nokey_name.dirhash[1] = minor_hash;
378
379 if (iname->len <= sizeof(nokey_name.bytes)) {
380 memcpy(nokey_name.bytes, iname->name, iname->len);
381 size = offsetof(struct fscrypt_nokey_name, bytes[iname->len]);
382 } else {
383 memcpy(nokey_name.bytes, iname->name, sizeof(nokey_name.bytes));
384 /* Compute strong hash of remaining part of name. */
385 sha256(&iname->name[sizeof(nokey_name.bytes)],
386 iname->len - sizeof(nokey_name.bytes),
387 nokey_name.sha256);
388 size = FSCRYPT_NOKEY_NAME_MAX;
389 }
390 oname->len = fscrypt_base64url_encode((const u8 *)&nokey_name, size,
391 oname->name);
392 return 0;
393 }
394 EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
395
396 /**
397 * fscrypt_setup_filename() - prepare to search a possibly encrypted directory
398 * @dir: the directory that will be searched
399 * @iname: the user-provided filename being searched for
400 * @lookup: 1 if we're allowed to proceed without the key because it's
401 * ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
402 * proceed without the key because we're going to create the dir_entry.
403 * @fname: the filename information to be filled in
404 *
405 * Given a user-provided filename @iname, this function sets @fname->disk_name
406 * to the name that would be stored in the on-disk directory entry, if possible.
407 * If the directory is unencrypted this is simply @iname. Else, if we have the
408 * directory's encryption key, then @iname is the plaintext, so we encrypt it to
409 * get the disk_name.
410 *
411 * Else, for keyless @lookup operations, @iname should be a no-key name, so we
412 * decode it to get the struct fscrypt_nokey_name. Non-@lookup operations will
413 * be impossible in this case, so we fail them with ENOKEY.
414 *
415 * If successful, fscrypt_free_filename() must be called later to clean up.
416 *
417 * Return: 0 on success, -errno on failure
418 */
fscrypt_setup_filename(struct inode * dir,const struct qstr * iname,int lookup,struct fscrypt_name * fname)419 int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
420 int lookup, struct fscrypt_name *fname)
421 {
422 struct fscrypt_nokey_name *nokey_name;
423 int ret;
424
425 memset(fname, 0, sizeof(struct fscrypt_name));
426 fname->usr_fname = iname;
427
428 if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) {
429 fname->disk_name.name = (unsigned char *)iname->name;
430 fname->disk_name.len = iname->len;
431 return 0;
432 }
433 ret = fscrypt_get_encryption_info(dir, lookup);
434 if (ret)
435 return ret;
436
437 if (fscrypt_has_encryption_key(dir)) {
438 if (!fscrypt_fname_encrypted_size(dir, iname->len, NAME_MAX,
439 &fname->crypto_buf.len))
440 return -ENAMETOOLONG;
441 fname->crypto_buf.name = kmalloc(fname->crypto_buf.len,
442 GFP_NOFS);
443 if (!fname->crypto_buf.name)
444 return -ENOMEM;
445
446 ret = fscrypt_fname_encrypt(dir, iname, fname->crypto_buf.name,
447 fname->crypto_buf.len);
448 if (ret)
449 goto errout;
450 fname->disk_name.name = fname->crypto_buf.name;
451 fname->disk_name.len = fname->crypto_buf.len;
452 return 0;
453 }
454 if (!lookup)
455 return -ENOKEY;
456 fname->is_nokey_name = true;
457
458 /*
459 * We don't have the key and we are doing a lookup; decode the
460 * user-supplied name
461 */
462
463 if (iname->len > FSCRYPT_NOKEY_NAME_MAX_ENCODED)
464 return -ENOENT;
465
466 fname->crypto_buf.name = kmalloc(FSCRYPT_NOKEY_NAME_MAX, GFP_KERNEL);
467 if (fname->crypto_buf.name == NULL)
468 return -ENOMEM;
469
470 ret = fscrypt_base64url_decode(iname->name, iname->len,
471 fname->crypto_buf.name);
472 if (ret < (int)offsetof(struct fscrypt_nokey_name, bytes[1]) ||
473 (ret > offsetof(struct fscrypt_nokey_name, sha256) &&
474 ret != FSCRYPT_NOKEY_NAME_MAX)) {
475 ret = -ENOENT;
476 goto errout;
477 }
478 fname->crypto_buf.len = ret;
479
480 nokey_name = (void *)fname->crypto_buf.name;
481 fname->hash = nokey_name->dirhash[0];
482 fname->minor_hash = nokey_name->dirhash[1];
483 if (ret != FSCRYPT_NOKEY_NAME_MAX) {
484 /* The full ciphertext filename is available. */
485 fname->disk_name.name = nokey_name->bytes;
486 fname->disk_name.len =
487 ret - offsetof(struct fscrypt_nokey_name, bytes);
488 }
489 return 0;
490
491 errout:
492 kfree(fname->crypto_buf.name);
493 return ret;
494 }
495 EXPORT_SYMBOL(fscrypt_setup_filename);
496
497 /**
498 * fscrypt_match_name() - test whether the given name matches a directory entry
499 * @fname: the name being searched for
500 * @de_name: the name from the directory entry
501 * @de_name_len: the length of @de_name in bytes
502 *
503 * Normally @fname->disk_name will be set, and in that case we simply compare
504 * that to the name stored in the directory entry. The only exception is that
505 * if we don't have the key for an encrypted directory and the name we're
506 * looking for is very long, then we won't have the full disk_name and instead
507 * we'll need to match against a fscrypt_nokey_name that includes a strong hash.
508 *
509 * Return: %true if the name matches, otherwise %false.
510 */
fscrypt_match_name(const struct fscrypt_name * fname,const u8 * de_name,u32 de_name_len)511 bool fscrypt_match_name(const struct fscrypt_name *fname,
512 const u8 *de_name, u32 de_name_len)
513 {
514 const struct fscrypt_nokey_name *nokey_name =
515 (const void *)fname->crypto_buf.name;
516 u8 digest[SHA256_DIGEST_SIZE];
517
518 if (likely(fname->disk_name.name)) {
519 if (de_name_len != fname->disk_name.len)
520 return false;
521 return !memcmp(de_name, fname->disk_name.name, de_name_len);
522 }
523 if (de_name_len <= sizeof(nokey_name->bytes))
524 return false;
525 if (memcmp(de_name, nokey_name->bytes, sizeof(nokey_name->bytes)))
526 return false;
527 sha256(&de_name[sizeof(nokey_name->bytes)],
528 de_name_len - sizeof(nokey_name->bytes), digest);
529 return !memcmp(digest, nokey_name->sha256, sizeof(digest));
530 }
531 EXPORT_SYMBOL_GPL(fscrypt_match_name);
532
533 /**
534 * fscrypt_fname_siphash() - calculate the SipHash of a filename
535 * @dir: the parent directory
536 * @name: the filename to calculate the SipHash of
537 *
538 * Given a plaintext filename @name and a directory @dir which uses SipHash as
539 * its dirhash method and has had its fscrypt key set up, this function
540 * calculates the SipHash of that name using the directory's secret dirhash key.
541 *
542 * Return: the SipHash of @name using the hash key of @dir
543 */
fscrypt_fname_siphash(const struct inode * dir,const struct qstr * name)544 u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name)
545 {
546 const struct fscrypt_inode_info *ci = dir->i_crypt_info;
547
548 WARN_ON_ONCE(!ci->ci_dirhash_key_initialized);
549
550 return siphash(name->name, name->len, &ci->ci_dirhash_key);
551 }
552 EXPORT_SYMBOL_GPL(fscrypt_fname_siphash);
553
554 /*
555 * Validate dentries in encrypted directories to make sure we aren't potentially
556 * caching stale dentries after a key has been added.
557 */
fscrypt_d_revalidate(struct inode * dir,const struct qstr * name,struct dentry * dentry,unsigned int flags)558 int fscrypt_d_revalidate(struct inode *dir, const struct qstr *name,
559 struct dentry *dentry, unsigned int flags)
560 {
561 int err;
562
563 /*
564 * Plaintext names are always valid, since fscrypt doesn't support
565 * reverting to no-key names without evicting the directory's inode
566 * -- which implies eviction of the dentries in the directory.
567 */
568 if (!(dentry->d_flags & DCACHE_NOKEY_NAME))
569 return 1;
570
571 /*
572 * No-key name; valid if the directory's key is still unavailable.
573 *
574 * Note in RCU mode we have to bail if we get here -
575 * fscrypt_get_encryption_info() may block.
576 */
577
578 if (flags & LOOKUP_RCU)
579 return -ECHILD;
580
581 /*
582 * Pass allow_unsupported=true, so that files with an unsupported
583 * encryption policy can be deleted.
584 */
585 err = fscrypt_get_encryption_info(dir, true);
586 if (err < 0)
587 return err;
588
589 return !fscrypt_has_encryption_key(dir);
590 }
591 EXPORT_SYMBOL_GPL(fscrypt_d_revalidate);
592