fname.c 11 KB

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