crypto_fname.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * linux/fs/f2fs/crypto_fname.c
  3. *
  4. * Copied from linux/fs/ext4/crypto.c
  5. *
  6. * Copyright (C) 2015, Google, Inc.
  7. * Copyright (C) 2015, Motorola Mobility
  8. *
  9. * This contains functions for filename crypto management in f2fs
  10. *
  11. * Written by Uday Savagaonkar, 2014.
  12. *
  13. * Adjust f2fs dentry structure
  14. * Jaegeuk Kim, 2015.
  15. *
  16. * This has not yet undergone a rigorous security audit.
  17. */
  18. #include <crypto/hash.h>
  19. #include <crypto/sha.h>
  20. #include <keys/encrypted-type.h>
  21. #include <keys/user-type.h>
  22. #include <linux/crypto.h>
  23. #include <linux/gfp.h>
  24. #include <linux/kernel.h>
  25. #include <linux/key.h>
  26. #include <linux/list.h>
  27. #include <linux/mempool.h>
  28. #include <linux/random.h>
  29. #include <linux/scatterlist.h>
  30. #include <linux/spinlock_types.h>
  31. #include <linux/f2fs_fs.h>
  32. #include <linux/ratelimit.h>
  33. #include "f2fs.h"
  34. #include "f2fs_crypto.h"
  35. #include "xattr.h"
  36. /**
  37. * f2fs_dir_crypt_complete() -
  38. */
  39. static void f2fs_dir_crypt_complete(struct crypto_async_request *req, int res)
  40. {
  41. struct f2fs_completion_result *ecr = req->data;
  42. if (res == -EINPROGRESS)
  43. return;
  44. ecr->res = res;
  45. complete(&ecr->completion);
  46. }
  47. bool f2fs_valid_filenames_enc_mode(uint32_t mode)
  48. {
  49. return (mode == F2FS_ENCRYPTION_MODE_AES_256_CTS);
  50. }
  51. static unsigned max_name_len(struct inode *inode)
  52. {
  53. return S_ISLNK(inode->i_mode) ? inode->i_sb->s_blocksize :
  54. F2FS_NAME_LEN;
  55. }
  56. /**
  57. * f2fs_fname_encrypt() -
  58. *
  59. * This function encrypts the input filename, and returns the length of the
  60. * ciphertext. Errors are returned as negative numbers. We trust the caller to
  61. * allocate sufficient memory to oname string.
  62. */
  63. static int f2fs_fname_encrypt(struct inode *inode,
  64. const struct qstr *iname, struct f2fs_str *oname)
  65. {
  66. u32 ciphertext_len;
  67. struct ablkcipher_request *req = NULL;
  68. DECLARE_F2FS_COMPLETION_RESULT(ecr);
  69. struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
  70. struct crypto_ablkcipher *tfm = ci->ci_ctfm;
  71. int res = 0;
  72. char iv[F2FS_CRYPTO_BLOCK_SIZE];
  73. struct scatterlist src_sg, dst_sg;
  74. int padding = 4 << (ci->ci_flags & F2FS_POLICY_FLAGS_PAD_MASK);
  75. char *workbuf, buf[32], *alloc_buf = NULL;
  76. unsigned lim = max_name_len(inode);
  77. if (iname->len <= 0 || iname->len > lim)
  78. return -EIO;
  79. ciphertext_len = (iname->len < F2FS_CRYPTO_BLOCK_SIZE) ?
  80. F2FS_CRYPTO_BLOCK_SIZE : iname->len;
  81. ciphertext_len = f2fs_fname_crypto_round_up(ciphertext_len, padding);
  82. ciphertext_len = (ciphertext_len > lim) ? lim : ciphertext_len;
  83. if (ciphertext_len <= sizeof(buf)) {
  84. workbuf = buf;
  85. } else {
  86. alloc_buf = kmalloc(ciphertext_len, GFP_NOFS);
  87. if (!alloc_buf)
  88. return -ENOMEM;
  89. workbuf = alloc_buf;
  90. }
  91. /* Allocate request */
  92. req = ablkcipher_request_alloc(tfm, GFP_NOFS);
  93. if (!req) {
  94. printk_ratelimited(KERN_ERR
  95. "%s: crypto_request_alloc() failed\n", __func__);
  96. kfree(alloc_buf);
  97. return -ENOMEM;
  98. }
  99. ablkcipher_request_set_callback(req,
  100. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  101. f2fs_dir_crypt_complete, &ecr);
  102. /* Copy the input */
  103. memcpy(workbuf, iname->name, iname->len);
  104. if (iname->len < ciphertext_len)
  105. memset(workbuf + iname->len, 0, ciphertext_len - iname->len);
  106. /* Initialize IV */
  107. memset(iv, 0, F2FS_CRYPTO_BLOCK_SIZE);
  108. /* Create encryption request */
  109. sg_init_one(&src_sg, workbuf, ciphertext_len);
  110. sg_init_one(&dst_sg, oname->name, ciphertext_len);
  111. ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, ciphertext_len, iv);
  112. res = crypto_ablkcipher_encrypt(req);
  113. if (res == -EINPROGRESS || res == -EBUSY) {
  114. BUG_ON(req->base.data != &ecr);
  115. wait_for_completion(&ecr.completion);
  116. res = ecr.res;
  117. }
  118. kfree(alloc_buf);
  119. ablkcipher_request_free(req);
  120. if (res < 0) {
  121. printk_ratelimited(KERN_ERR
  122. "%s: Error (error code %d)\n", __func__, res);
  123. }
  124. oname->len = ciphertext_len;
  125. return res;
  126. }
  127. /*
  128. * f2fs_fname_decrypt()
  129. * This function decrypts the input filename, and returns
  130. * the length of the plaintext.
  131. * Errors are returned as negative numbers.
  132. * We trust the caller to allocate sufficient memory to oname string.
  133. */
  134. static int f2fs_fname_decrypt(struct inode *inode,
  135. const struct f2fs_str *iname, struct f2fs_str *oname)
  136. {
  137. struct ablkcipher_request *req = NULL;
  138. DECLARE_F2FS_COMPLETION_RESULT(ecr);
  139. struct scatterlist src_sg, dst_sg;
  140. struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
  141. struct crypto_ablkcipher *tfm = ci->ci_ctfm;
  142. int res = 0;
  143. char iv[F2FS_CRYPTO_BLOCK_SIZE];
  144. unsigned lim = max_name_len(inode);
  145. if (iname->len <= 0 || iname->len > lim)
  146. return -EIO;
  147. /* Allocate request */
  148. req = ablkcipher_request_alloc(tfm, GFP_NOFS);
  149. if (!req) {
  150. printk_ratelimited(KERN_ERR
  151. "%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. f2fs_dir_crypt_complete, &ecr);
  157. /* Initialize IV */
  158. memset(iv, 0, F2FS_CRYPTO_BLOCK_SIZE);
  159. /* Create decryption 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(KERN_ERR
  172. "%s: Error in f2fs_fname_decrypt (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. * f2fs_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. * f2fs_fname_crypto_round_up() -
  229. *
  230. * Return: The next multiple of block size
  231. */
  232. u32 f2fs_fname_crypto_round_up(u32 size, u32 blksize)
  233. {
  234. return ((size + blksize - 1) / blksize) * blksize;
  235. }
  236. /**
  237. * f2fs_fname_crypto_alloc_obuff() -
  238. *
  239. * Allocates an output buffer that is sufficient for the crypto operation
  240. * specified by the context and the direction.
  241. */
  242. int f2fs_fname_crypto_alloc_buffer(struct inode *inode,
  243. u32 ilen, struct f2fs_str *crypto_str)
  244. {
  245. unsigned int olen;
  246. int padding = 16;
  247. struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
  248. if (ci)
  249. padding = 4 << (ci->ci_flags & F2FS_POLICY_FLAGS_PAD_MASK);
  250. if (padding < F2FS_CRYPTO_BLOCK_SIZE)
  251. padding = F2FS_CRYPTO_BLOCK_SIZE;
  252. olen = f2fs_fname_crypto_round_up(ilen, padding);
  253. crypto_str->len = olen;
  254. if (olen < F2FS_FNAME_CRYPTO_DIGEST_SIZE * 2)
  255. olen = F2FS_FNAME_CRYPTO_DIGEST_SIZE * 2;
  256. /* Allocated buffer can hold one more character to null-terminate the
  257. * string */
  258. crypto_str->name = kmalloc(olen + 1, GFP_NOFS);
  259. if (!(crypto_str->name))
  260. return -ENOMEM;
  261. return 0;
  262. }
  263. /**
  264. * f2fs_fname_crypto_free_buffer() -
  265. *
  266. * Frees the buffer allocated for crypto operation.
  267. */
  268. void f2fs_fname_crypto_free_buffer(struct f2fs_str *crypto_str)
  269. {
  270. if (!crypto_str)
  271. return;
  272. kfree(crypto_str->name);
  273. crypto_str->name = NULL;
  274. }
  275. /**
  276. * f2fs_fname_disk_to_usr() - converts a filename from disk space to user space
  277. */
  278. int f2fs_fname_disk_to_usr(struct inode *inode,
  279. f2fs_hash_t *hash,
  280. const struct f2fs_str *iname,
  281. struct f2fs_str *oname)
  282. {
  283. const struct qstr qname = FSTR_TO_QSTR(iname);
  284. char buf[24];
  285. int ret;
  286. if (is_dot_dotdot(&qname)) {
  287. oname->name[0] = '.';
  288. oname->name[iname->len - 1] = '.';
  289. oname->len = iname->len;
  290. return oname->len;
  291. }
  292. if (F2FS_I(inode)->i_crypt_info)
  293. return f2fs_fname_decrypt(inode, iname, oname);
  294. if (iname->len <= F2FS_FNAME_CRYPTO_DIGEST_SIZE) {
  295. ret = digest_encode(iname->name, iname->len, oname->name);
  296. oname->len = ret;
  297. return ret;
  298. }
  299. if (hash) {
  300. memcpy(buf, hash, 4);
  301. memset(buf + 4, 0, 4);
  302. } else
  303. memset(buf, 0, 8);
  304. memcpy(buf + 8, iname->name + iname->len - 16, 16);
  305. oname->name[0] = '_';
  306. ret = digest_encode(buf, 24, oname->name + 1);
  307. oname->len = ret + 1;
  308. return ret + 1;
  309. }
  310. /**
  311. * f2fs_fname_usr_to_disk() - converts a filename from user space to disk space
  312. */
  313. int f2fs_fname_usr_to_disk(struct inode *inode,
  314. const struct qstr *iname,
  315. struct f2fs_str *oname)
  316. {
  317. int res;
  318. struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
  319. if (is_dot_dotdot(iname)) {
  320. oname->name[0] = '.';
  321. oname->name[iname->len - 1] = '.';
  322. oname->len = iname->len;
  323. return oname->len;
  324. }
  325. if (ci) {
  326. res = f2fs_fname_encrypt(inode, iname, oname);
  327. return res;
  328. }
  329. /* Without a proper key, a user is not allowed to modify the filenames
  330. * in a directory. Consequently, a user space name cannot be mapped to
  331. * a disk-space name */
  332. return -EACCES;
  333. }
  334. int f2fs_fname_setup_filename(struct inode *dir, const struct qstr *iname,
  335. int lookup, struct f2fs_filename *fname)
  336. {
  337. struct f2fs_crypt_info *ci;
  338. int ret = 0, bigname = 0;
  339. memset(fname, 0, sizeof(struct f2fs_filename));
  340. fname->usr_fname = iname;
  341. if (!f2fs_encrypted_inode(dir) || is_dot_dotdot(iname)) {
  342. fname->disk_name.name = (unsigned char *)iname->name;
  343. fname->disk_name.len = iname->len;
  344. return 0;
  345. }
  346. ret = f2fs_get_encryption_info(dir);
  347. if (ret)
  348. return ret;
  349. ci = F2FS_I(dir)->i_crypt_info;
  350. if (ci) {
  351. ret = f2fs_fname_crypto_alloc_buffer(dir, iname->len,
  352. &fname->crypto_buf);
  353. if (ret < 0)
  354. return ret;
  355. ret = f2fs_fname_encrypt(dir, iname, &fname->crypto_buf);
  356. if (ret < 0)
  357. goto errout;
  358. fname->disk_name.name = fname->crypto_buf.name;
  359. fname->disk_name.len = fname->crypto_buf.len;
  360. return 0;
  361. }
  362. if (!lookup)
  363. return -EACCES;
  364. /* We don't have the key and we are doing a lookup; decode the
  365. * user-supplied name
  366. */
  367. if (iname->name[0] == '_')
  368. bigname = 1;
  369. if ((bigname && (iname->len != 33)) ||
  370. (!bigname && (iname->len > 43)))
  371. return -ENOENT;
  372. fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
  373. if (fname->crypto_buf.name == NULL)
  374. return -ENOMEM;
  375. ret = digest_decode(iname->name + bigname, iname->len - bigname,
  376. fname->crypto_buf.name);
  377. if (ret < 0) {
  378. ret = -ENOENT;
  379. goto errout;
  380. }
  381. fname->crypto_buf.len = ret;
  382. if (bigname) {
  383. memcpy(&fname->hash, fname->crypto_buf.name, 4);
  384. } else {
  385. fname->disk_name.name = fname->crypto_buf.name;
  386. fname->disk_name.len = fname->crypto_buf.len;
  387. }
  388. return 0;
  389. errout:
  390. f2fs_fname_crypto_free_buffer(&fname->crypto_buf);
  391. return ret;
  392. }
  393. void f2fs_fname_free_filename(struct f2fs_filename *fname)
  394. {
  395. kfree(fname->crypto_buf.name);
  396. fname->crypto_buf.name = NULL;
  397. fname->usr_fname = NULL;
  398. fname->disk_name.name = NULL;
  399. }