fname.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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/scatterlist.h>
  14. #include <linux/ratelimit.h>
  15. #include <crypto/skcipher.h>
  16. #include "fscrypt_private.h"
  17. static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
  18. {
  19. if (str->len == 1 && str->name[0] == '.')
  20. return true;
  21. if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
  22. return true;
  23. return false;
  24. }
  25. /**
  26. * fname_encrypt() - encrypt a filename
  27. *
  28. * The output buffer must be at least as large as the input buffer.
  29. * Any extra space is filled with NUL padding before encryption.
  30. *
  31. * Return: 0 on success, -errno on failure
  32. */
  33. int fname_encrypt(struct inode *inode, const struct qstr *iname,
  34. u8 *out, unsigned int olen)
  35. {
  36. struct skcipher_request *req = NULL;
  37. DECLARE_CRYPTO_WAIT(wait);
  38. struct crypto_skcipher *tfm = inode->i_crypt_info->ci_ctfm;
  39. int res = 0;
  40. char iv[FS_CRYPTO_BLOCK_SIZE];
  41. struct scatterlist sg;
  42. /*
  43. * Copy the filename to the output buffer for encrypting in-place and
  44. * pad it with the needed number of NUL bytes.
  45. */
  46. if (WARN_ON(olen < iname->len))
  47. return -ENOBUFS;
  48. memcpy(out, iname->name, iname->len);
  49. memset(out + iname->len, 0, olen - iname->len);
  50. /* Initialize the IV */
  51. memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
  52. /* Set up the encryption request */
  53. req = skcipher_request_alloc(tfm, GFP_NOFS);
  54. if (!req)
  55. return -ENOMEM;
  56. skcipher_request_set_callback(req,
  57. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  58. crypto_req_done, &wait);
  59. sg_init_one(&sg, out, olen);
  60. skcipher_request_set_crypt(req, &sg, &sg, olen, iv);
  61. /* Do the encryption */
  62. res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
  63. skcipher_request_free(req);
  64. if (res < 0) {
  65. fscrypt_err(inode->i_sb,
  66. "Filename encryption failed for inode %lu: %d",
  67. inode->i_ino, res);
  68. return res;
  69. }
  70. return 0;
  71. }
  72. /**
  73. * fname_decrypt() - decrypt a filename
  74. *
  75. * The caller must have allocated sufficient memory for the @oname string.
  76. *
  77. * Return: 0 on success, -errno on failure
  78. */
  79. static int fname_decrypt(struct inode *inode,
  80. const struct fscrypt_str *iname,
  81. struct fscrypt_str *oname)
  82. {
  83. struct skcipher_request *req = NULL;
  84. DECLARE_CRYPTO_WAIT(wait);
  85. struct scatterlist src_sg, dst_sg;
  86. struct crypto_skcipher *tfm = inode->i_crypt_info->ci_ctfm;
  87. int res = 0;
  88. char iv[FS_CRYPTO_BLOCK_SIZE];
  89. /* Allocate request */
  90. req = skcipher_request_alloc(tfm, GFP_NOFS);
  91. if (!req)
  92. return -ENOMEM;
  93. skcipher_request_set_callback(req,
  94. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  95. crypto_req_done, &wait);
  96. /* Initialize IV */
  97. memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
  98. /* Create decryption request */
  99. sg_init_one(&src_sg, iname->name, iname->len);
  100. sg_init_one(&dst_sg, oname->name, oname->len);
  101. skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
  102. res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
  103. skcipher_request_free(req);
  104. if (res < 0) {
  105. fscrypt_err(inode->i_sb,
  106. "Filename decryption failed for inode %lu: %d",
  107. inode->i_ino, res);
  108. return res;
  109. }
  110. oname->len = strnlen(oname->name, iname->len);
  111. return 0;
  112. }
  113. static const char *lookup_table =
  114. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
  115. #define BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3)
  116. /**
  117. * digest_encode() -
  118. *
  119. * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
  120. * The encoded string is roughly 4/3 times the size of the input string.
  121. */
  122. static int digest_encode(const char *src, int len, char *dst)
  123. {
  124. int i = 0, bits = 0, ac = 0;
  125. char *cp = dst;
  126. while (i < len) {
  127. ac += (((unsigned char) src[i]) << bits);
  128. bits += 8;
  129. do {
  130. *cp++ = lookup_table[ac & 0x3f];
  131. ac >>= 6;
  132. bits -= 6;
  133. } while (bits >= 6);
  134. i++;
  135. }
  136. if (bits)
  137. *cp++ = lookup_table[ac & 0x3f];
  138. return cp - dst;
  139. }
  140. static int digest_decode(const char *src, int len, char *dst)
  141. {
  142. int i = 0, bits = 0, ac = 0;
  143. const char *p;
  144. char *cp = dst;
  145. while (i < len) {
  146. p = strchr(lookup_table, src[i]);
  147. if (p == NULL || src[i] == 0)
  148. return -2;
  149. ac += (p - lookup_table) << bits;
  150. bits += 6;
  151. if (bits >= 8) {
  152. *cp++ = ac & 0xff;
  153. ac >>= 8;
  154. bits -= 8;
  155. }
  156. i++;
  157. }
  158. if (ac)
  159. return -1;
  160. return cp - dst;
  161. }
  162. bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
  163. u32 max_len, u32 *encrypted_len_ret)
  164. {
  165. int padding = 4 << (inode->i_crypt_info->ci_flags &
  166. FS_POLICY_FLAGS_PAD_MASK);
  167. u32 encrypted_len;
  168. if (orig_len > max_len)
  169. return false;
  170. encrypted_len = max(orig_len, (u32)FS_CRYPTO_BLOCK_SIZE);
  171. encrypted_len = round_up(encrypted_len, padding);
  172. *encrypted_len_ret = min(encrypted_len, max_len);
  173. return true;
  174. }
  175. /**
  176. * fscrypt_fname_alloc_buffer - allocate a buffer for presented filenames
  177. *
  178. * Allocate a buffer that is large enough to hold any decrypted or encoded
  179. * filename (null-terminated), for the given maximum encrypted filename length.
  180. *
  181. * Return: 0 on success, -errno on failure
  182. */
  183. int fscrypt_fname_alloc_buffer(const struct inode *inode,
  184. u32 max_encrypted_len,
  185. struct fscrypt_str *crypto_str)
  186. {
  187. const u32 max_encoded_len =
  188. max_t(u32, BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE),
  189. 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name)));
  190. u32 max_presented_len;
  191. max_presented_len = max(max_encoded_len, max_encrypted_len);
  192. crypto_str->name = kmalloc(max_presented_len + 1, GFP_NOFS);
  193. if (!crypto_str->name)
  194. return -ENOMEM;
  195. crypto_str->len = max_presented_len;
  196. return 0;
  197. }
  198. EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
  199. /**
  200. * fscrypt_fname_free_buffer - free the buffer for presented filenames
  201. *
  202. * Free the buffer allocated by fscrypt_fname_alloc_buffer().
  203. */
  204. void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
  205. {
  206. if (!crypto_str)
  207. return;
  208. kfree(crypto_str->name);
  209. crypto_str->name = NULL;
  210. }
  211. EXPORT_SYMBOL(fscrypt_fname_free_buffer);
  212. /**
  213. * fscrypt_fname_disk_to_usr() - converts a filename from disk space to user
  214. * space
  215. *
  216. * The caller must have allocated sufficient memory for the @oname string.
  217. *
  218. * If the key is available, we'll decrypt the disk name; otherwise, we'll encode
  219. * it for presentation. Short names are directly base64-encoded, while long
  220. * names are encoded in fscrypt_digested_name format.
  221. *
  222. * Return: 0 on success, -errno on failure
  223. */
  224. int fscrypt_fname_disk_to_usr(struct inode *inode,
  225. u32 hash, u32 minor_hash,
  226. const struct fscrypt_str *iname,
  227. struct fscrypt_str *oname)
  228. {
  229. const struct qstr qname = FSTR_TO_QSTR(iname);
  230. struct fscrypt_digested_name digested_name;
  231. if (fscrypt_is_dot_dotdot(&qname)) {
  232. oname->name[0] = '.';
  233. oname->name[iname->len - 1] = '.';
  234. oname->len = iname->len;
  235. return 0;
  236. }
  237. if (iname->len < FS_CRYPTO_BLOCK_SIZE)
  238. return -EUCLEAN;
  239. if (inode->i_crypt_info)
  240. return fname_decrypt(inode, iname, oname);
  241. if (iname->len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE) {
  242. oname->len = digest_encode(iname->name, iname->len,
  243. oname->name);
  244. return 0;
  245. }
  246. if (hash) {
  247. digested_name.hash = hash;
  248. digested_name.minor_hash = minor_hash;
  249. } else {
  250. digested_name.hash = 0;
  251. digested_name.minor_hash = 0;
  252. }
  253. memcpy(digested_name.digest,
  254. FSCRYPT_FNAME_DIGEST(iname->name, iname->len),
  255. FSCRYPT_FNAME_DIGEST_SIZE);
  256. oname->name[0] = '_';
  257. oname->len = 1 + digest_encode((const char *)&digested_name,
  258. sizeof(digested_name), oname->name + 1);
  259. return 0;
  260. }
  261. EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
  262. /**
  263. * fscrypt_setup_filename() - prepare to search a possibly encrypted directory
  264. * @dir: the directory that will be searched
  265. * @iname: the user-provided filename being searched for
  266. * @lookup: 1 if we're allowed to proceed without the key because it's
  267. * ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
  268. * proceed without the key because we're going to create the dir_entry.
  269. * @fname: the filename information to be filled in
  270. *
  271. * Given a user-provided filename @iname, this function sets @fname->disk_name
  272. * to the name that would be stored in the on-disk directory entry, if possible.
  273. * If the directory is unencrypted this is simply @iname. Else, if we have the
  274. * directory's encryption key, then @iname is the plaintext, so we encrypt it to
  275. * get the disk_name.
  276. *
  277. * Else, for keyless @lookup operations, @iname is the presented ciphertext, so
  278. * we decode it to get either the ciphertext disk_name (for short names) or the
  279. * fscrypt_digested_name (for long names). Non-@lookup operations will be
  280. * impossible in this case, so we fail them with ENOKEY.
  281. *
  282. * If successful, fscrypt_free_filename() must be called later to clean up.
  283. *
  284. * Return: 0 on success, -errno on failure
  285. */
  286. int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
  287. int lookup, struct fscrypt_name *fname)
  288. {
  289. int ret;
  290. int digested;
  291. memset(fname, 0, sizeof(struct fscrypt_name));
  292. fname->usr_fname = iname;
  293. if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) {
  294. fname->disk_name.name = (unsigned char *)iname->name;
  295. fname->disk_name.len = iname->len;
  296. return 0;
  297. }
  298. ret = fscrypt_get_encryption_info(dir);
  299. if (ret)
  300. return ret;
  301. if (dir->i_crypt_info) {
  302. if (!fscrypt_fname_encrypted_size(dir, iname->len,
  303. dir->i_sb->s_cop->max_namelen,
  304. &fname->crypto_buf.len))
  305. return -ENAMETOOLONG;
  306. fname->crypto_buf.name = kmalloc(fname->crypto_buf.len,
  307. GFP_NOFS);
  308. if (!fname->crypto_buf.name)
  309. return -ENOMEM;
  310. ret = fname_encrypt(dir, iname, fname->crypto_buf.name,
  311. fname->crypto_buf.len);
  312. if (ret)
  313. goto errout;
  314. fname->disk_name.name = fname->crypto_buf.name;
  315. fname->disk_name.len = fname->crypto_buf.len;
  316. return 0;
  317. }
  318. if (!lookup)
  319. return -ENOKEY;
  320. /*
  321. * We don't have the key and we are doing a lookup; decode the
  322. * user-supplied name
  323. */
  324. if (iname->name[0] == '_') {
  325. if (iname->len !=
  326. 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name)))
  327. return -ENOENT;
  328. digested = 1;
  329. } else {
  330. if (iname->len >
  331. BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE))
  332. return -ENOENT;
  333. digested = 0;
  334. }
  335. fname->crypto_buf.name =
  336. kmalloc(max_t(size_t, FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE,
  337. sizeof(struct fscrypt_digested_name)),
  338. GFP_KERNEL);
  339. if (fname->crypto_buf.name == NULL)
  340. return -ENOMEM;
  341. ret = digest_decode(iname->name + digested, iname->len - digested,
  342. fname->crypto_buf.name);
  343. if (ret < 0) {
  344. ret = -ENOENT;
  345. goto errout;
  346. }
  347. fname->crypto_buf.len = ret;
  348. if (digested) {
  349. const struct fscrypt_digested_name *n =
  350. (const void *)fname->crypto_buf.name;
  351. fname->hash = n->hash;
  352. fname->minor_hash = n->minor_hash;
  353. } else {
  354. fname->disk_name.name = fname->crypto_buf.name;
  355. fname->disk_name.len = fname->crypto_buf.len;
  356. }
  357. return 0;
  358. errout:
  359. kfree(fname->crypto_buf.name);
  360. return ret;
  361. }
  362. EXPORT_SYMBOL(fscrypt_setup_filename);