crypto_fname.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * linux/fs/ext4/crypto_fname.c
  3. *
  4. * Copyright (C) 2015, Google, Inc.
  5. *
  6. * This contains functions for filename crypto management in ext4
  7. *
  8. * Written by Uday Savagaonkar, 2014.
  9. *
  10. * This has not yet undergone a rigorous security audit.
  11. *
  12. */
  13. #include <crypto/hash.h>
  14. #include <crypto/sha.h>
  15. #include <keys/encrypted-type.h>
  16. #include <keys/user-type.h>
  17. #include <linux/crypto.h>
  18. #include <linux/gfp.h>
  19. #include <linux/kernel.h>
  20. #include <linux/key.h>
  21. #include <linux/list.h>
  22. #include <linux/mempool.h>
  23. #include <linux/random.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/spinlock_types.h>
  26. #include "ext4.h"
  27. #include "ext4_crypto.h"
  28. #include "xattr.h"
  29. /**
  30. * ext4_dir_crypt_complete() -
  31. */
  32. static void ext4_dir_crypt_complete(struct crypto_async_request *req, int res)
  33. {
  34. struct ext4_completion_result *ecr = req->data;
  35. if (res == -EINPROGRESS)
  36. return;
  37. ecr->res = res;
  38. complete(&ecr->completion);
  39. }
  40. bool ext4_valid_filenames_enc_mode(uint32_t mode)
  41. {
  42. return (mode == EXT4_ENCRYPTION_MODE_AES_256_CTS);
  43. }
  44. static unsigned max_name_len(struct inode *inode)
  45. {
  46. return S_ISLNK(inode->i_mode) ? inode->i_sb->s_blocksize :
  47. EXT4_NAME_LEN;
  48. }
  49. /**
  50. * ext4_fname_encrypt() -
  51. *
  52. * This function encrypts the input filename, and returns the length of the
  53. * ciphertext. Errors are returned as negative numbers. We trust the caller to
  54. * allocate sufficient memory to oname string.
  55. */
  56. static int ext4_fname_encrypt(struct inode *inode,
  57. const struct qstr *iname,
  58. struct ext4_str *oname)
  59. {
  60. u32 ciphertext_len;
  61. struct ablkcipher_request *req = NULL;
  62. DECLARE_EXT4_COMPLETION_RESULT(ecr);
  63. struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
  64. struct crypto_ablkcipher *tfm = ci->ci_ctfm;
  65. int res = 0;
  66. char iv[EXT4_CRYPTO_BLOCK_SIZE];
  67. struct scatterlist src_sg, dst_sg;
  68. int padding = 4 << (ci->ci_flags & EXT4_POLICY_FLAGS_PAD_MASK);
  69. char *workbuf, buf[32], *alloc_buf = NULL;
  70. unsigned lim = max_name_len(inode);
  71. if (iname->len <= 0 || iname->len > lim)
  72. return -EIO;
  73. ciphertext_len = (iname->len < EXT4_CRYPTO_BLOCK_SIZE) ?
  74. EXT4_CRYPTO_BLOCK_SIZE : iname->len;
  75. ciphertext_len = ext4_fname_crypto_round_up(ciphertext_len, padding);
  76. ciphertext_len = (ciphertext_len > lim)
  77. ? lim : ciphertext_len;
  78. if (ciphertext_len <= sizeof(buf)) {
  79. workbuf = buf;
  80. } else {
  81. alloc_buf = kmalloc(ciphertext_len, GFP_NOFS);
  82. if (!alloc_buf)
  83. return -ENOMEM;
  84. workbuf = alloc_buf;
  85. }
  86. /* Allocate request */
  87. req = ablkcipher_request_alloc(tfm, GFP_NOFS);
  88. if (!req) {
  89. printk_ratelimited(
  90. KERN_ERR "%s: crypto_request_alloc() failed\n", __func__);
  91. kfree(alloc_buf);
  92. return -ENOMEM;
  93. }
  94. ablkcipher_request_set_callback(req,
  95. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  96. ext4_dir_crypt_complete, &ecr);
  97. /* Copy the input */
  98. memcpy(workbuf, iname->name, iname->len);
  99. if (iname->len < ciphertext_len)
  100. memset(workbuf + iname->len, 0, ciphertext_len - iname->len);
  101. /* Initialize IV */
  102. memset(iv, 0, EXT4_CRYPTO_BLOCK_SIZE);
  103. /* Create encryption request */
  104. sg_init_one(&src_sg, workbuf, ciphertext_len);
  105. sg_init_one(&dst_sg, oname->name, ciphertext_len);
  106. ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, ciphertext_len, iv);
  107. res = crypto_ablkcipher_encrypt(req);
  108. if (res == -EINPROGRESS || res == -EBUSY) {
  109. BUG_ON(req->base.data != &ecr);
  110. wait_for_completion(&ecr.completion);
  111. res = ecr.res;
  112. }
  113. kfree(alloc_buf);
  114. ablkcipher_request_free(req);
  115. if (res < 0) {
  116. printk_ratelimited(
  117. KERN_ERR "%s: Error (error code %d)\n", __func__, res);
  118. }
  119. oname->len = ciphertext_len;
  120. return res;
  121. }
  122. /*
  123. * ext4_fname_decrypt()
  124. * This function decrypts the input filename, and returns
  125. * the length of the plaintext.
  126. * Errors are returned as negative numbers.
  127. * We trust the caller to allocate sufficient memory to oname string.
  128. */
  129. static int ext4_fname_decrypt(struct inode *inode,
  130. const struct ext4_str *iname,
  131. struct ext4_str *oname)
  132. {
  133. struct ext4_str tmp_in[2], tmp_out[1];
  134. struct ablkcipher_request *req = NULL;
  135. DECLARE_EXT4_COMPLETION_RESULT(ecr);
  136. struct scatterlist src_sg, dst_sg;
  137. struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
  138. struct crypto_ablkcipher *tfm = ci->ci_ctfm;
  139. int res = 0;
  140. char iv[EXT4_CRYPTO_BLOCK_SIZE];
  141. unsigned lim = max_name_len(inode);
  142. if (iname->len <= 0 || iname->len > lim)
  143. return -EIO;
  144. tmp_in[0].name = iname->name;
  145. tmp_in[0].len = iname->len;
  146. tmp_out[0].name = oname->name;
  147. /* Allocate request */
  148. req = ablkcipher_request_alloc(tfm, GFP_NOFS);
  149. if (!req) {
  150. printk_ratelimited(
  151. KERN_ERR "%s: crypto_request_alloc() failed\n", __func__);
  152. return -ENOMEM;
  153. }
  154. ablkcipher_request_set_callback(req,
  155. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  156. ext4_dir_crypt_complete, &ecr);
  157. /* Initialize IV */
  158. memset(iv, 0, EXT4_CRYPTO_BLOCK_SIZE);
  159. /* Create encryption request */
  160. sg_init_one(&src_sg, iname->name, iname->len);
  161. sg_init_one(&dst_sg, oname->name, oname->len);
  162. ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
  163. res = crypto_ablkcipher_decrypt(req);
  164. if (res == -EINPROGRESS || res == -EBUSY) {
  165. BUG_ON(req->base.data != &ecr);
  166. wait_for_completion(&ecr.completion);
  167. res = ecr.res;
  168. }
  169. ablkcipher_request_free(req);
  170. if (res < 0) {
  171. printk_ratelimited(
  172. KERN_ERR "%s: Error in ext4_fname_encrypt (error code %d)\n",
  173. __func__, res);
  174. return res;
  175. }
  176. oname->len = strnlen(oname->name, iname->len);
  177. return oname->len;
  178. }
  179. static const char *lookup_table =
  180. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
  181. /**
  182. * ext4_fname_encode_digest() -
  183. *
  184. * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
  185. * The encoded string is roughly 4/3 times the size of the input string.
  186. */
  187. static int digest_encode(const char *src, int len, char *dst)
  188. {
  189. int i = 0, bits = 0, ac = 0;
  190. char *cp = dst;
  191. while (i < len) {
  192. ac += (((unsigned char) src[i]) << bits);
  193. bits += 8;
  194. do {
  195. *cp++ = lookup_table[ac & 0x3f];
  196. ac >>= 6;
  197. bits -= 6;
  198. } while (bits >= 6);
  199. i++;
  200. }
  201. if (bits)
  202. *cp++ = lookup_table[ac & 0x3f];
  203. return cp - dst;
  204. }
  205. static int digest_decode(const char *src, int len, char *dst)
  206. {
  207. int i = 0, bits = 0, ac = 0;
  208. const char *p;
  209. char *cp = dst;
  210. while (i < len) {
  211. p = strchr(lookup_table, src[i]);
  212. if (p == NULL || src[i] == 0)
  213. return -2;
  214. ac += (p - lookup_table) << bits;
  215. bits += 6;
  216. if (bits >= 8) {
  217. *cp++ = ac & 0xff;
  218. ac >>= 8;
  219. bits -= 8;
  220. }
  221. i++;
  222. }
  223. if (ac)
  224. return -1;
  225. return cp - dst;
  226. }
  227. /**
  228. * ext4_fname_crypto_round_up() -
  229. *
  230. * Return: The next multiple of block size
  231. */
  232. u32 ext4_fname_crypto_round_up(u32 size, u32 blksize)
  233. {
  234. return ((size+blksize-1)/blksize)*blksize;
  235. }
  236. unsigned ext4_fname_encrypted_size(struct inode *inode, u32 ilen)
  237. {
  238. struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
  239. int padding = 32;
  240. if (ci)
  241. padding = 4 << (ci->ci_flags & EXT4_POLICY_FLAGS_PAD_MASK);
  242. if (ilen < EXT4_CRYPTO_BLOCK_SIZE)
  243. ilen = EXT4_CRYPTO_BLOCK_SIZE;
  244. return ext4_fname_crypto_round_up(ilen, padding);
  245. }
  246. /*
  247. * ext4_fname_crypto_alloc_buffer() -
  248. *
  249. * Allocates an output buffer that is sufficient for the crypto operation
  250. * specified by the context and the direction.
  251. */
  252. int ext4_fname_crypto_alloc_buffer(struct inode *inode,
  253. u32 ilen, struct ext4_str *crypto_str)
  254. {
  255. unsigned int olen = ext4_fname_encrypted_size(inode, ilen);
  256. crypto_str->len = olen;
  257. if (olen < EXT4_FNAME_CRYPTO_DIGEST_SIZE*2)
  258. olen = EXT4_FNAME_CRYPTO_DIGEST_SIZE*2;
  259. /* Allocated buffer can hold one more character to null-terminate the
  260. * string */
  261. crypto_str->name = kmalloc(olen+1, GFP_NOFS);
  262. if (!(crypto_str->name))
  263. return -ENOMEM;
  264. return 0;
  265. }
  266. /**
  267. * ext4_fname_crypto_free_buffer() -
  268. *
  269. * Frees the buffer allocated for crypto operation.
  270. */
  271. void ext4_fname_crypto_free_buffer(struct ext4_str *crypto_str)
  272. {
  273. if (!crypto_str)
  274. return;
  275. kfree(crypto_str->name);
  276. crypto_str->name = NULL;
  277. }
  278. /**
  279. * ext4_fname_disk_to_usr() - converts a filename from disk space to user space
  280. */
  281. int _ext4_fname_disk_to_usr(struct inode *inode,
  282. struct dx_hash_info *hinfo,
  283. const struct ext4_str *iname,
  284. struct ext4_str *oname)
  285. {
  286. char buf[24];
  287. int ret;
  288. if (iname->len < 3) {
  289. /*Check for . and .. */
  290. if (iname->name[0] == '.' && iname->name[iname->len-1] == '.') {
  291. oname->name[0] = '.';
  292. oname->name[iname->len-1] = '.';
  293. oname->len = iname->len;
  294. return oname->len;
  295. }
  296. }
  297. if (iname->len < EXT4_CRYPTO_BLOCK_SIZE) {
  298. EXT4_ERROR_INODE(inode, "encrypted inode too small");
  299. return -EUCLEAN;
  300. }
  301. if (EXT4_I(inode)->i_crypt_info)
  302. return ext4_fname_decrypt(inode, iname, oname);
  303. if (iname->len <= EXT4_FNAME_CRYPTO_DIGEST_SIZE) {
  304. ret = digest_encode(iname->name, iname->len, oname->name);
  305. oname->len = ret;
  306. return ret;
  307. }
  308. if (hinfo) {
  309. memcpy(buf, &hinfo->hash, 4);
  310. memcpy(buf+4, &hinfo->minor_hash, 4);
  311. } else
  312. memset(buf, 0, 8);
  313. memcpy(buf + 8, iname->name + iname->len - 16, 16);
  314. oname->name[0] = '_';
  315. ret = digest_encode(buf, 24, oname->name+1);
  316. oname->len = ret + 1;
  317. return ret + 1;
  318. }
  319. int ext4_fname_disk_to_usr(struct inode *inode,
  320. struct dx_hash_info *hinfo,
  321. const struct ext4_dir_entry_2 *de,
  322. struct ext4_str *oname)
  323. {
  324. struct ext4_str iname = {.name = (unsigned char *) de->name,
  325. .len = de->name_len };
  326. return _ext4_fname_disk_to_usr(inode, hinfo, &iname, oname);
  327. }
  328. /**
  329. * ext4_fname_usr_to_disk() - converts a filename from user space to disk space
  330. */
  331. int ext4_fname_usr_to_disk(struct inode *inode,
  332. const struct qstr *iname,
  333. struct ext4_str *oname)
  334. {
  335. int res;
  336. struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
  337. if (iname->len < 3) {
  338. /*Check for . and .. */
  339. if (iname->name[0] == '.' &&
  340. iname->name[iname->len-1] == '.') {
  341. oname->name[0] = '.';
  342. oname->name[iname->len-1] = '.';
  343. oname->len = iname->len;
  344. return oname->len;
  345. }
  346. }
  347. if (ci) {
  348. res = ext4_fname_encrypt(inode, iname, oname);
  349. return res;
  350. }
  351. /* Without a proper key, a user is not allowed to modify the filenames
  352. * in a directory. Consequently, a user space name cannot be mapped to
  353. * a disk-space name */
  354. return -EACCES;
  355. }
  356. int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
  357. int lookup, struct ext4_filename *fname)
  358. {
  359. struct ext4_crypt_info *ci;
  360. int ret = 0, bigname = 0;
  361. memset(fname, 0, sizeof(struct ext4_filename));
  362. fname->usr_fname = iname;
  363. if (!ext4_encrypted_inode(dir) ||
  364. ((iname->name[0] == '.') &&
  365. ((iname->len == 1) ||
  366. ((iname->name[1] == '.') && (iname->len == 2))))) {
  367. fname->disk_name.name = (unsigned char *) iname->name;
  368. fname->disk_name.len = iname->len;
  369. return 0;
  370. }
  371. ret = ext4_get_encryption_info(dir);
  372. if (ret)
  373. return ret;
  374. ci = EXT4_I(dir)->i_crypt_info;
  375. if (ci) {
  376. ret = ext4_fname_crypto_alloc_buffer(dir, iname->len,
  377. &fname->crypto_buf);
  378. if (ret < 0)
  379. return ret;
  380. ret = ext4_fname_encrypt(dir, iname, &fname->crypto_buf);
  381. if (ret < 0)
  382. goto errout;
  383. fname->disk_name.name = fname->crypto_buf.name;
  384. fname->disk_name.len = fname->crypto_buf.len;
  385. return 0;
  386. }
  387. if (!lookup)
  388. return -EACCES;
  389. /* We don't have the key and we are doing a lookup; decode the
  390. * user-supplied name
  391. */
  392. if (iname->name[0] == '_')
  393. bigname = 1;
  394. if ((bigname && (iname->len != 33)) ||
  395. (!bigname && (iname->len > 43)))
  396. return -ENOENT;
  397. fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
  398. if (fname->crypto_buf.name == NULL)
  399. return -ENOMEM;
  400. ret = digest_decode(iname->name + bigname, iname->len - bigname,
  401. fname->crypto_buf.name);
  402. if (ret < 0) {
  403. ret = -ENOENT;
  404. goto errout;
  405. }
  406. fname->crypto_buf.len = ret;
  407. if (bigname) {
  408. memcpy(&fname->hinfo.hash, fname->crypto_buf.name, 4);
  409. memcpy(&fname->hinfo.minor_hash, fname->crypto_buf.name + 4, 4);
  410. } else {
  411. fname->disk_name.name = fname->crypto_buf.name;
  412. fname->disk_name.len = fname->crypto_buf.len;
  413. }
  414. return 0;
  415. errout:
  416. kfree(fname->crypto_buf.name);
  417. fname->crypto_buf.name = NULL;
  418. return ret;
  419. }
  420. void ext4_fname_free_filename(struct ext4_filename *fname)
  421. {
  422. kfree(fname->crypto_buf.name);
  423. fname->crypto_buf.name = NULL;
  424. fname->usr_fname = NULL;
  425. fname->disk_name.name = NULL;
  426. }