fname.c 11 KB

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