fname.c 11 KB

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