fname.c 21 KB

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