crypto_fname.c 12 KB

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