crypto_fname.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. wait_for_completion(&ecr.completion);
  110. res = ecr.res;
  111. }
  112. kfree(alloc_buf);
  113. ablkcipher_request_free(req);
  114. if (res < 0) {
  115. printk_ratelimited(
  116. KERN_ERR "%s: Error (error code %d)\n", __func__, res);
  117. }
  118. oname->len = ciphertext_len;
  119. return res;
  120. }
  121. /*
  122. * ext4_fname_decrypt()
  123. * This function decrypts the input filename, and returns
  124. * the length of the plaintext.
  125. * Errors are returned as negative numbers.
  126. * We trust the caller to allocate sufficient memory to oname string.
  127. */
  128. static int ext4_fname_decrypt(struct inode *inode,
  129. const struct ext4_str *iname,
  130. struct ext4_str *oname)
  131. {
  132. struct ext4_str tmp_in[2], tmp_out[1];
  133. struct ablkcipher_request *req = NULL;
  134. DECLARE_EXT4_COMPLETION_RESULT(ecr);
  135. struct scatterlist src_sg, dst_sg;
  136. struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
  137. struct crypto_ablkcipher *tfm = ci->ci_ctfm;
  138. int res = 0;
  139. char iv[EXT4_CRYPTO_BLOCK_SIZE];
  140. unsigned lim = max_name_len(inode);
  141. if (iname->len <= 0 || iname->len > lim)
  142. return -EIO;
  143. tmp_in[0].name = iname->name;
  144. tmp_in[0].len = iname->len;
  145. tmp_out[0].name = oname->name;
  146. /* Allocate request */
  147. req = ablkcipher_request_alloc(tfm, GFP_NOFS);
  148. if (!req) {
  149. printk_ratelimited(
  150. KERN_ERR "%s: crypto_request_alloc() failed\n", __func__);
  151. return -ENOMEM;
  152. }
  153. ablkcipher_request_set_callback(req,
  154. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  155. ext4_dir_crypt_complete, &ecr);
  156. /* Initialize IV */
  157. memset(iv, 0, EXT4_CRYPTO_BLOCK_SIZE);
  158. /* Create encryption request */
  159. sg_init_one(&src_sg, iname->name, iname->len);
  160. sg_init_one(&dst_sg, oname->name, oname->len);
  161. ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
  162. res = crypto_ablkcipher_decrypt(req);
  163. if (res == -EINPROGRESS || res == -EBUSY) {
  164. wait_for_completion(&ecr.completion);
  165. res = ecr.res;
  166. }
  167. ablkcipher_request_free(req);
  168. if (res < 0) {
  169. printk_ratelimited(
  170. KERN_ERR "%s: Error in ext4_fname_encrypt (error code %d)\n",
  171. __func__, res);
  172. return res;
  173. }
  174. oname->len = strnlen(oname->name, iname->len);
  175. return oname->len;
  176. }
  177. static const char *lookup_table =
  178. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
  179. /**
  180. * ext4_fname_encode_digest() -
  181. *
  182. * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
  183. * The encoded string is roughly 4/3 times the size of the input string.
  184. */
  185. static int digest_encode(const char *src, int len, char *dst)
  186. {
  187. int i = 0, bits = 0, ac = 0;
  188. char *cp = dst;
  189. while (i < len) {
  190. ac += (((unsigned char) src[i]) << bits);
  191. bits += 8;
  192. do {
  193. *cp++ = lookup_table[ac & 0x3f];
  194. ac >>= 6;
  195. bits -= 6;
  196. } while (bits >= 6);
  197. i++;
  198. }
  199. if (bits)
  200. *cp++ = lookup_table[ac & 0x3f];
  201. return cp - dst;
  202. }
  203. static int digest_decode(const char *src, int len, char *dst)
  204. {
  205. int i = 0, bits = 0, ac = 0;
  206. const char *p;
  207. char *cp = dst;
  208. while (i < len) {
  209. p = strchr(lookup_table, src[i]);
  210. if (p == NULL || src[i] == 0)
  211. return -2;
  212. ac += (p - lookup_table) << bits;
  213. bits += 6;
  214. if (bits >= 8) {
  215. *cp++ = ac & 0xff;
  216. ac >>= 8;
  217. bits -= 8;
  218. }
  219. i++;
  220. }
  221. if (ac)
  222. return -1;
  223. return cp - dst;
  224. }
  225. /**
  226. * ext4_fname_crypto_round_up() -
  227. *
  228. * Return: The next multiple of block size
  229. */
  230. u32 ext4_fname_crypto_round_up(u32 size, u32 blksize)
  231. {
  232. return ((size+blksize-1)/blksize)*blksize;
  233. }
  234. unsigned ext4_fname_encrypted_size(struct inode *inode, u32 ilen)
  235. {
  236. struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
  237. int padding = 32;
  238. if (ci)
  239. padding = 4 << (ci->ci_flags & EXT4_POLICY_FLAGS_PAD_MASK);
  240. if (ilen < EXT4_CRYPTO_BLOCK_SIZE)
  241. ilen = EXT4_CRYPTO_BLOCK_SIZE;
  242. return ext4_fname_crypto_round_up(ilen, padding);
  243. }
  244. /*
  245. * ext4_fname_crypto_alloc_buffer() -
  246. *
  247. * Allocates an output buffer that is sufficient for the crypto operation
  248. * specified by the context and the direction.
  249. */
  250. int ext4_fname_crypto_alloc_buffer(struct inode *inode,
  251. u32 ilen, struct ext4_str *crypto_str)
  252. {
  253. unsigned int olen = ext4_fname_encrypted_size(inode, ilen);
  254. crypto_str->len = olen;
  255. if (olen < EXT4_FNAME_CRYPTO_DIGEST_SIZE*2)
  256. olen = EXT4_FNAME_CRYPTO_DIGEST_SIZE*2;
  257. /* Allocated buffer can hold one more character to null-terminate the
  258. * string */
  259. crypto_str->name = kmalloc(olen+1, GFP_NOFS);
  260. if (!(crypto_str->name))
  261. return -ENOMEM;
  262. return 0;
  263. }
  264. /**
  265. * ext4_fname_crypto_free_buffer() -
  266. *
  267. * Frees the buffer allocated for crypto operation.
  268. */
  269. void ext4_fname_crypto_free_buffer(struct ext4_str *crypto_str)
  270. {
  271. if (!crypto_str)
  272. return;
  273. kfree(crypto_str->name);
  274. crypto_str->name = NULL;
  275. }
  276. /**
  277. * ext4_fname_disk_to_usr() - converts a filename from disk space to user space
  278. */
  279. int _ext4_fname_disk_to_usr(struct inode *inode,
  280. struct dx_hash_info *hinfo,
  281. const struct ext4_str *iname,
  282. struct ext4_str *oname)
  283. {
  284. char buf[24];
  285. int ret;
  286. if (iname->len < 3) {
  287. /*Check for . and .. */
  288. if (iname->name[0] == '.' && iname->name[iname->len-1] == '.') {
  289. oname->name[0] = '.';
  290. oname->name[iname->len-1] = '.';
  291. oname->len = iname->len;
  292. return oname->len;
  293. }
  294. }
  295. if (iname->len < EXT4_CRYPTO_BLOCK_SIZE) {
  296. EXT4_ERROR_INODE(inode, "encrypted inode too small");
  297. return -EUCLEAN;
  298. }
  299. if (EXT4_I(inode)->i_crypt_info)
  300. return ext4_fname_decrypt(inode, iname, oname);
  301. if (iname->len <= EXT4_FNAME_CRYPTO_DIGEST_SIZE) {
  302. ret = digest_encode(iname->name, iname->len, oname->name);
  303. oname->len = ret;
  304. return ret;
  305. }
  306. if (hinfo) {
  307. memcpy(buf, &hinfo->hash, 4);
  308. memcpy(buf+4, &hinfo->minor_hash, 4);
  309. } else
  310. memset(buf, 0, 8);
  311. memcpy(buf + 8, iname->name + ((iname->len - 17) & ~15), 16);
  312. oname->name[0] = '_';
  313. ret = digest_encode(buf, 24, oname->name+1);
  314. oname->len = ret + 1;
  315. return ret + 1;
  316. }
  317. int ext4_fname_disk_to_usr(struct inode *inode,
  318. struct dx_hash_info *hinfo,
  319. const struct ext4_dir_entry_2 *de,
  320. struct ext4_str *oname)
  321. {
  322. struct ext4_str iname = {.name = (unsigned char *) de->name,
  323. .len = de->name_len };
  324. return _ext4_fname_disk_to_usr(inode, hinfo, &iname, oname);
  325. }
  326. /**
  327. * ext4_fname_usr_to_disk() - converts a filename from user space to disk space
  328. */
  329. int ext4_fname_usr_to_disk(struct inode *inode,
  330. const struct qstr *iname,
  331. struct ext4_str *oname)
  332. {
  333. int res;
  334. struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
  335. if (iname->len < 3) {
  336. /*Check for . and .. */
  337. if (iname->name[0] == '.' &&
  338. iname->name[iname->len-1] == '.') {
  339. oname->name[0] = '.';
  340. oname->name[iname->len-1] = '.';
  341. oname->len = iname->len;
  342. return oname->len;
  343. }
  344. }
  345. if (ci) {
  346. res = ext4_fname_encrypt(inode, iname, oname);
  347. return res;
  348. }
  349. /* Without a proper key, a user is not allowed to modify the filenames
  350. * in a directory. Consequently, a user space name cannot be mapped to
  351. * a disk-space name */
  352. return -EACCES;
  353. }
  354. int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
  355. int lookup, struct ext4_filename *fname)
  356. {
  357. struct ext4_crypt_info *ci;
  358. int ret = 0, bigname = 0;
  359. memset(fname, 0, sizeof(struct ext4_filename));
  360. fname->usr_fname = iname;
  361. if (!ext4_encrypted_inode(dir) ||
  362. ((iname->name[0] == '.') &&
  363. ((iname->len == 1) ||
  364. ((iname->name[1] == '.') && (iname->len == 2))))) {
  365. fname->disk_name.name = (unsigned char *) iname->name;
  366. fname->disk_name.len = iname->len;
  367. return 0;
  368. }
  369. ret = ext4_get_encryption_info(dir);
  370. if (ret)
  371. return ret;
  372. ci = EXT4_I(dir)->i_crypt_info;
  373. if (ci) {
  374. ret = ext4_fname_crypto_alloc_buffer(dir, iname->len,
  375. &fname->crypto_buf);
  376. if (ret < 0)
  377. return ret;
  378. ret = ext4_fname_encrypt(dir, iname, &fname->crypto_buf);
  379. if (ret < 0)
  380. goto errout;
  381. fname->disk_name.name = fname->crypto_buf.name;
  382. fname->disk_name.len = fname->crypto_buf.len;
  383. return 0;
  384. }
  385. if (!lookup)
  386. return -EACCES;
  387. /* We don't have the key and we are doing a lookup; decode the
  388. * user-supplied name
  389. */
  390. if (iname->name[0] == '_')
  391. bigname = 1;
  392. if ((bigname && (iname->len != 33)) ||
  393. (!bigname && (iname->len > 43)))
  394. return -ENOENT;
  395. fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
  396. if (fname->crypto_buf.name == NULL)
  397. return -ENOMEM;
  398. ret = digest_decode(iname->name + bigname, iname->len - bigname,
  399. fname->crypto_buf.name);
  400. if (ret < 0) {
  401. ret = -ENOENT;
  402. goto errout;
  403. }
  404. fname->crypto_buf.len = ret;
  405. if (bigname) {
  406. memcpy(&fname->hinfo.hash, fname->crypto_buf.name, 4);
  407. memcpy(&fname->hinfo.minor_hash, fname->crypto_buf.name + 4, 4);
  408. } else {
  409. fname->disk_name.name = fname->crypto_buf.name;
  410. fname->disk_name.len = fname->crypto_buf.len;
  411. }
  412. return 0;
  413. errout:
  414. kfree(fname->crypto_buf.name);
  415. fname->crypto_buf.name = NULL;
  416. return ret;
  417. }
  418. void ext4_fname_free_filename(struct ext4_filename *fname)
  419. {
  420. kfree(fname->crypto_buf.name);
  421. fname->crypto_buf.name = NULL;
  422. fname->usr_fname = NULL;
  423. fname->disk_name.name = NULL;
  424. }