fname.c 12 KB

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