fname.c 10 KB

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