fname.c 18 KB

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