crypto_fname.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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/hash.h>
  14. #include <crypto/sha.h>
  15. #include <keys/encrypted-type.h>
  16. #include <keys/user-type.h>
  17. #include <linux/crypto.h>
  18. #include <linux/gfp.h>
  19. #include <linux/kernel.h>
  20. #include <linux/key.h>
  21. #include <linux/key.h>
  22. #include <linux/list.h>
  23. #include <linux/mempool.h>
  24. #include <linux/random.h>
  25. #include <linux/scatterlist.h>
  26. #include <linux/spinlock_types.h>
  27. #include "ext4.h"
  28. #include "ext4_crypto.h"
  29. #include "xattr.h"
  30. /**
  31. * ext4_dir_crypt_complete() -
  32. */
  33. static void ext4_dir_crypt_complete(struct crypto_async_request *req, int res)
  34. {
  35. struct ext4_completion_result *ecr = req->data;
  36. if (res == -EINPROGRESS)
  37. return;
  38. ecr->res = res;
  39. complete(&ecr->completion);
  40. }
  41. bool ext4_valid_filenames_enc_mode(uint32_t mode)
  42. {
  43. return (mode == EXT4_ENCRYPTION_MODE_AES_256_CTS);
  44. }
  45. /**
  46. * ext4_fname_encrypt() -
  47. *
  48. * This function encrypts the input filename, and returns the length of the
  49. * ciphertext. Errors are returned as negative numbers. We trust the caller to
  50. * allocate sufficient memory to oname string.
  51. */
  52. static int ext4_fname_encrypt(struct ext4_fname_crypto_ctx *ctx,
  53. const struct qstr *iname,
  54. struct ext4_str *oname)
  55. {
  56. u32 ciphertext_len;
  57. struct ablkcipher_request *req = NULL;
  58. DECLARE_EXT4_COMPLETION_RESULT(ecr);
  59. struct crypto_ablkcipher *tfm = ctx->ctfm;
  60. int res = 0;
  61. char iv[EXT4_CRYPTO_BLOCK_SIZE];
  62. struct scatterlist sg[1];
  63. char *workbuf;
  64. if (iname->len <= 0 || iname->len > ctx->lim)
  65. return -EIO;
  66. ciphertext_len = (iname->len < EXT4_CRYPTO_BLOCK_SIZE) ?
  67. EXT4_CRYPTO_BLOCK_SIZE : iname->len;
  68. ciphertext_len = (ciphertext_len > ctx->lim)
  69. ? ctx->lim : ciphertext_len;
  70. /* Allocate request */
  71. req = ablkcipher_request_alloc(tfm, GFP_NOFS);
  72. if (!req) {
  73. printk_ratelimited(
  74. KERN_ERR "%s: crypto_request_alloc() failed\n", __func__);
  75. return -ENOMEM;
  76. }
  77. ablkcipher_request_set_callback(req,
  78. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  79. ext4_dir_crypt_complete, &ecr);
  80. /* Map the workpage */
  81. workbuf = kmap(ctx->workpage);
  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, EXT4_CRYPTO_BLOCK_SIZE);
  88. /* Create encryption request */
  89. sg_init_table(sg, 1);
  90. sg_set_page(sg, ctx->workpage, PAGE_SIZE, 0);
  91. ablkcipher_request_set_crypt(req, sg, sg, iname->len, iv);
  92. res = crypto_ablkcipher_encrypt(req);
  93. if (res == -EINPROGRESS || res == -EBUSY) {
  94. BUG_ON(req->base.data != &ecr);
  95. wait_for_completion(&ecr.completion);
  96. res = ecr.res;
  97. }
  98. if (res >= 0) {
  99. /* Copy the result to output */
  100. memcpy(oname->name, workbuf, ciphertext_len);
  101. res = ciphertext_len;
  102. }
  103. kunmap(ctx->workpage);
  104. ablkcipher_request_free(req);
  105. if (res < 0) {
  106. printk_ratelimited(
  107. KERN_ERR "%s: Error (error code %d)\n", __func__, res);
  108. }
  109. oname->len = ciphertext_len;
  110. return res;
  111. }
  112. /*
  113. * ext4_fname_decrypt()
  114. * This function decrypts the input filename, and returns
  115. * the length of the plaintext.
  116. * Errors are returned as negative numbers.
  117. * We trust the caller to allocate sufficient memory to oname string.
  118. */
  119. static int ext4_fname_decrypt(struct ext4_fname_crypto_ctx *ctx,
  120. const struct ext4_str *iname,
  121. struct ext4_str *oname)
  122. {
  123. struct ext4_str tmp_in[2], tmp_out[1];
  124. struct ablkcipher_request *req = NULL;
  125. DECLARE_EXT4_COMPLETION_RESULT(ecr);
  126. struct scatterlist sg[1];
  127. struct crypto_ablkcipher *tfm = ctx->ctfm;
  128. int res = 0;
  129. char iv[EXT4_CRYPTO_BLOCK_SIZE];
  130. char *workbuf;
  131. if (iname->len <= 0 || iname->len > ctx->lim)
  132. return -EIO;
  133. tmp_in[0].name = iname->name;
  134. tmp_in[0].len = iname->len;
  135. tmp_out[0].name = oname->name;
  136. /* Allocate request */
  137. req = ablkcipher_request_alloc(tfm, GFP_NOFS);
  138. if (!req) {
  139. printk_ratelimited(
  140. KERN_ERR "%s: crypto_request_alloc() failed\n", __func__);
  141. return -ENOMEM;
  142. }
  143. ablkcipher_request_set_callback(req,
  144. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  145. ext4_dir_crypt_complete, &ecr);
  146. /* Map the workpage */
  147. workbuf = kmap(ctx->workpage);
  148. /* Copy the input */
  149. memcpy(workbuf, iname->name, iname->len);
  150. /* Initialize IV */
  151. memset(iv, 0, EXT4_CRYPTO_BLOCK_SIZE);
  152. /* Create encryption request */
  153. sg_init_table(sg, 1);
  154. sg_set_page(sg, ctx->workpage, PAGE_SIZE, 0);
  155. ablkcipher_request_set_crypt(req, sg, sg, iname->len, iv);
  156. res = crypto_ablkcipher_decrypt(req);
  157. if (res == -EINPROGRESS || res == -EBUSY) {
  158. BUG_ON(req->base.data != &ecr);
  159. wait_for_completion(&ecr.completion);
  160. res = ecr.res;
  161. }
  162. if (res >= 0) {
  163. /* Copy the result to output */
  164. memcpy(oname->name, workbuf, iname->len);
  165. res = iname->len;
  166. }
  167. kunmap(ctx->workpage);
  168. ablkcipher_request_free(req);
  169. if (res < 0) {
  170. printk_ratelimited(
  171. KERN_ERR "%s: Error in ext4_fname_encrypt (error code %d)\n",
  172. __func__, res);
  173. return res;
  174. }
  175. oname->len = strnlen(oname->name, iname->len);
  176. return oname->len;
  177. }
  178. /**
  179. * ext4_fname_encode_digest() -
  180. *
  181. * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
  182. * The encoded string is roughly 4/3 times the size of the input string.
  183. */
  184. int ext4_fname_encode_digest(char *dst, char *src, u32 len)
  185. {
  186. static const char *lookup_table =
  187. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+";
  188. u32 current_chunk, num_chunks, i;
  189. char tmp_buf[3];
  190. u32 c0, c1, c2, c3;
  191. current_chunk = 0;
  192. num_chunks = len/3;
  193. for (i = 0; i < num_chunks; i++) {
  194. c0 = src[3*i] & 0x3f;
  195. c1 = (((src[3*i]>>6)&0x3) | ((src[3*i+1] & 0xf)<<2)) & 0x3f;
  196. c2 = (((src[3*i+1]>>4)&0xf) | ((src[3*i+2] & 0x3)<<4)) & 0x3f;
  197. c3 = (src[3*i+2]>>2) & 0x3f;
  198. dst[4*i] = lookup_table[c0];
  199. dst[4*i+1] = lookup_table[c1];
  200. dst[4*i+2] = lookup_table[c2];
  201. dst[4*i+3] = lookup_table[c3];
  202. }
  203. if (i*3 < len) {
  204. memset(tmp_buf, 0, 3);
  205. memcpy(tmp_buf, &src[3*i], len-3*i);
  206. c0 = tmp_buf[0] & 0x3f;
  207. c1 = (((tmp_buf[0]>>6)&0x3) | ((tmp_buf[1] & 0xf)<<2)) & 0x3f;
  208. c2 = (((tmp_buf[1]>>4)&0xf) | ((tmp_buf[2] & 0x3)<<4)) & 0x3f;
  209. c3 = (tmp_buf[2]>>2) & 0x3f;
  210. dst[4*i] = lookup_table[c0];
  211. dst[4*i+1] = lookup_table[c1];
  212. dst[4*i+2] = lookup_table[c2];
  213. dst[4*i+3] = lookup_table[c3];
  214. i++;
  215. }
  216. return (i * 4);
  217. }
  218. /**
  219. * ext4_fname_hash() -
  220. *
  221. * This function computes the hash of the input filename, and sets the output
  222. * buffer to the *encoded* digest. It returns the length of the digest as its
  223. * return value. Errors are returned as negative numbers. We trust the caller
  224. * to allocate sufficient memory to oname string.
  225. */
  226. static int ext4_fname_hash(struct ext4_fname_crypto_ctx *ctx,
  227. const struct ext4_str *iname,
  228. struct ext4_str *oname)
  229. {
  230. struct scatterlist sg;
  231. struct hash_desc desc = {
  232. .tfm = (struct crypto_hash *)ctx->htfm,
  233. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  234. };
  235. int res = 0;
  236. if (iname->len <= EXT4_FNAME_CRYPTO_DIGEST_SIZE) {
  237. res = ext4_fname_encode_digest(oname->name, iname->name,
  238. iname->len);
  239. oname->len = res;
  240. return res;
  241. }
  242. sg_init_one(&sg, iname->name, iname->len);
  243. res = crypto_hash_init(&desc);
  244. if (res) {
  245. printk(KERN_ERR
  246. "%s: Error initializing crypto hash; res = [%d]\n",
  247. __func__, res);
  248. goto out;
  249. }
  250. res = crypto_hash_update(&desc, &sg, iname->len);
  251. if (res) {
  252. printk(KERN_ERR
  253. "%s: Error updating crypto hash; res = [%d]\n",
  254. __func__, res);
  255. goto out;
  256. }
  257. res = crypto_hash_final(&desc,
  258. &oname->name[EXT4_FNAME_CRYPTO_DIGEST_SIZE]);
  259. if (res) {
  260. printk(KERN_ERR
  261. "%s: Error finalizing crypto hash; res = [%d]\n",
  262. __func__, res);
  263. goto out;
  264. }
  265. /* Encode the digest as a printable string--this will increase the
  266. * size of the digest */
  267. oname->name[0] = 'I';
  268. res = ext4_fname_encode_digest(oname->name+1,
  269. &oname->name[EXT4_FNAME_CRYPTO_DIGEST_SIZE],
  270. EXT4_FNAME_CRYPTO_DIGEST_SIZE) + 1;
  271. oname->len = res;
  272. out:
  273. return res;
  274. }
  275. /**
  276. * ext4_free_fname_crypto_ctx() -
  277. *
  278. * Frees up a crypto context.
  279. */
  280. void ext4_free_fname_crypto_ctx(struct ext4_fname_crypto_ctx *ctx)
  281. {
  282. if (ctx == NULL || IS_ERR(ctx))
  283. return;
  284. if (ctx->ctfm && !IS_ERR(ctx->ctfm))
  285. crypto_free_ablkcipher(ctx->ctfm);
  286. if (ctx->htfm && !IS_ERR(ctx->htfm))
  287. crypto_free_hash(ctx->htfm);
  288. if (ctx->workpage && !IS_ERR(ctx->workpage))
  289. __free_page(ctx->workpage);
  290. kfree(ctx);
  291. }
  292. /**
  293. * ext4_put_fname_crypto_ctx() -
  294. *
  295. * Return: The crypto context onto free list. If the free list is above a
  296. * threshold, completely frees up the context, and returns the memory.
  297. *
  298. * TODO: Currently we directly free the crypto context. Eventually we should
  299. * add code it to return to free list. Such an approach will increase
  300. * efficiency of directory lookup.
  301. */
  302. void ext4_put_fname_crypto_ctx(struct ext4_fname_crypto_ctx **ctx)
  303. {
  304. if (*ctx == NULL || IS_ERR(*ctx))
  305. return;
  306. ext4_free_fname_crypto_ctx(*ctx);
  307. *ctx = NULL;
  308. }
  309. /**
  310. * ext4_search_fname_crypto_ctx() -
  311. */
  312. static struct ext4_fname_crypto_ctx *ext4_search_fname_crypto_ctx(
  313. const struct ext4_encryption_key *key)
  314. {
  315. return NULL;
  316. }
  317. /**
  318. * ext4_alloc_fname_crypto_ctx() -
  319. */
  320. struct ext4_fname_crypto_ctx *ext4_alloc_fname_crypto_ctx(
  321. const struct ext4_encryption_key *key)
  322. {
  323. struct ext4_fname_crypto_ctx *ctx;
  324. ctx = kmalloc(sizeof(struct ext4_fname_crypto_ctx), GFP_NOFS);
  325. if (ctx == NULL)
  326. return ERR_PTR(-ENOMEM);
  327. if (key->mode == EXT4_ENCRYPTION_MODE_INVALID) {
  328. /* This will automatically set key mode to invalid
  329. * As enum for ENCRYPTION_MODE_INVALID is zero */
  330. memset(&ctx->key, 0, sizeof(ctx->key));
  331. } else {
  332. memcpy(&ctx->key, key, sizeof(struct ext4_encryption_key));
  333. }
  334. ctx->has_valid_key = (EXT4_ENCRYPTION_MODE_INVALID == key->mode)
  335. ? 0 : 1;
  336. ctx->ctfm_key_is_ready = 0;
  337. ctx->ctfm = NULL;
  338. ctx->htfm = NULL;
  339. ctx->workpage = NULL;
  340. return ctx;
  341. }
  342. /**
  343. * ext4_get_fname_crypto_ctx() -
  344. *
  345. * Allocates a free crypto context and initializes it to hold
  346. * the crypto material for the inode.
  347. *
  348. * Return: NULL if not encrypted. Error value on error. Valid pointer otherwise.
  349. */
  350. struct ext4_fname_crypto_ctx *ext4_get_fname_crypto_ctx(
  351. struct inode *inode, u32 max_ciphertext_len)
  352. {
  353. struct ext4_fname_crypto_ctx *ctx;
  354. struct ext4_inode_info *ei = EXT4_I(inode);
  355. int res;
  356. /* Check if the crypto policy is set on the inode */
  357. res = ext4_encrypted_inode(inode);
  358. if (res == 0)
  359. return NULL;
  360. if (!ext4_has_encryption_key(inode))
  361. ext4_generate_encryption_key(inode);
  362. /* Get a crypto context based on the key.
  363. * A new context is allocated if no context matches the requested key.
  364. */
  365. ctx = ext4_search_fname_crypto_ctx(&(ei->i_encryption_key));
  366. if (ctx == NULL)
  367. ctx = ext4_alloc_fname_crypto_ctx(&(ei->i_encryption_key));
  368. if (IS_ERR(ctx))
  369. return ctx;
  370. if (ctx->has_valid_key) {
  371. if (ctx->key.mode != EXT4_ENCRYPTION_MODE_AES_256_CTS) {
  372. printk_once(KERN_WARNING
  373. "ext4: unsupported key mode %d\n",
  374. ctx->key.mode);
  375. return ERR_PTR(-ENOKEY);
  376. }
  377. /* As a first cut, we will allocate new tfm in every call.
  378. * later, we will keep the tfm around, in case the key gets
  379. * re-used */
  380. if (ctx->ctfm == NULL) {
  381. ctx->ctfm = crypto_alloc_ablkcipher("cts(cbc(aes))",
  382. 0, 0);
  383. }
  384. if (IS_ERR(ctx->ctfm)) {
  385. res = PTR_ERR(ctx->ctfm);
  386. printk(
  387. KERN_DEBUG "%s: error (%d) allocating crypto tfm\n",
  388. __func__, res);
  389. ctx->ctfm = NULL;
  390. ext4_put_fname_crypto_ctx(&ctx);
  391. return ERR_PTR(res);
  392. }
  393. if (ctx->ctfm == NULL) {
  394. printk(
  395. KERN_DEBUG "%s: could not allocate crypto tfm\n",
  396. __func__);
  397. ext4_put_fname_crypto_ctx(&ctx);
  398. return ERR_PTR(-ENOMEM);
  399. }
  400. if (ctx->workpage == NULL)
  401. ctx->workpage = alloc_page(GFP_NOFS);
  402. if (IS_ERR(ctx->workpage)) {
  403. res = PTR_ERR(ctx->workpage);
  404. printk(
  405. KERN_DEBUG "%s: error (%d) allocating work page\n",
  406. __func__, res);
  407. ctx->workpage = NULL;
  408. ext4_put_fname_crypto_ctx(&ctx);
  409. return ERR_PTR(res);
  410. }
  411. if (ctx->workpage == NULL) {
  412. printk(
  413. KERN_DEBUG "%s: could not allocate work page\n",
  414. __func__);
  415. ext4_put_fname_crypto_ctx(&ctx);
  416. return ERR_PTR(-ENOMEM);
  417. }
  418. ctx->lim = max_ciphertext_len;
  419. crypto_ablkcipher_clear_flags(ctx->ctfm, ~0);
  420. crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctx->ctfm),
  421. CRYPTO_TFM_REQ_WEAK_KEY);
  422. /* If we are lucky, we will get a context that is already
  423. * set up with the right key. Else, we will have to
  424. * set the key */
  425. if (!ctx->ctfm_key_is_ready) {
  426. /* Since our crypto objectives for filename encryption
  427. * are pretty weak,
  428. * we directly use the inode master key */
  429. res = crypto_ablkcipher_setkey(ctx->ctfm,
  430. ctx->key.raw, ctx->key.size);
  431. if (res) {
  432. ext4_put_fname_crypto_ctx(&ctx);
  433. return ERR_PTR(-EIO);
  434. }
  435. ctx->ctfm_key_is_ready = 1;
  436. } else {
  437. /* In the current implementation, key should never be
  438. * marked "ready" for a context that has just been
  439. * allocated. So we should never reach here */
  440. BUG();
  441. }
  442. }
  443. if (ctx->htfm == NULL)
  444. ctx->htfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  445. if (IS_ERR(ctx->htfm)) {
  446. res = PTR_ERR(ctx->htfm);
  447. printk(KERN_DEBUG "%s: error (%d) allocating hash tfm\n",
  448. __func__, res);
  449. ctx->htfm = NULL;
  450. ext4_put_fname_crypto_ctx(&ctx);
  451. return ERR_PTR(res);
  452. }
  453. if (ctx->htfm == NULL) {
  454. printk(KERN_DEBUG "%s: could not allocate hash tfm\n",
  455. __func__);
  456. ext4_put_fname_crypto_ctx(&ctx);
  457. return ERR_PTR(-ENOMEM);
  458. }
  459. return ctx;
  460. }
  461. /**
  462. * ext4_fname_crypto_round_up() -
  463. *
  464. * Return: The next multiple of block size
  465. */
  466. u32 ext4_fname_crypto_round_up(u32 size, u32 blksize)
  467. {
  468. return ((size+blksize-1)/blksize)*blksize;
  469. }
  470. /**
  471. * ext4_fname_crypto_namelen_on_disk() -
  472. */
  473. int ext4_fname_crypto_namelen_on_disk(struct ext4_fname_crypto_ctx *ctx,
  474. u32 namelen)
  475. {
  476. u32 ciphertext_len;
  477. if (ctx == NULL)
  478. return -EIO;
  479. if (!(ctx->has_valid_key))
  480. return -EACCES;
  481. ciphertext_len = (namelen < EXT4_CRYPTO_BLOCK_SIZE) ?
  482. EXT4_CRYPTO_BLOCK_SIZE : namelen;
  483. ciphertext_len = (ciphertext_len > ctx->lim)
  484. ? ctx->lim : ciphertext_len;
  485. return (int) ciphertext_len;
  486. }
  487. /**
  488. * ext4_fname_crypto_alloc_obuff() -
  489. *
  490. * Allocates an output buffer that is sufficient for the crypto operation
  491. * specified by the context and the direction.
  492. */
  493. int ext4_fname_crypto_alloc_buffer(struct ext4_fname_crypto_ctx *ctx,
  494. u32 ilen, struct ext4_str *crypto_str)
  495. {
  496. unsigned int olen;
  497. if (!ctx)
  498. return -EIO;
  499. olen = ext4_fname_crypto_round_up(ilen, EXT4_CRYPTO_BLOCK_SIZE);
  500. crypto_str->len = olen;
  501. if (olen < EXT4_FNAME_CRYPTO_DIGEST_SIZE*2)
  502. olen = EXT4_FNAME_CRYPTO_DIGEST_SIZE*2;
  503. /* Allocated buffer can hold one more character to null-terminate the
  504. * string */
  505. crypto_str->name = kmalloc(olen+1, GFP_NOFS);
  506. if (!(crypto_str->name))
  507. return -ENOMEM;
  508. return 0;
  509. }
  510. /**
  511. * ext4_fname_crypto_free_buffer() -
  512. *
  513. * Frees the buffer allocated for crypto operation.
  514. */
  515. void ext4_fname_crypto_free_buffer(struct ext4_str *crypto_str)
  516. {
  517. if (!crypto_str)
  518. return;
  519. kfree(crypto_str->name);
  520. crypto_str->name = NULL;
  521. }
  522. /**
  523. * ext4_fname_disk_to_usr() - converts a filename from disk space to user space
  524. */
  525. int _ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx,
  526. const struct ext4_str *iname,
  527. struct ext4_str *oname)
  528. {
  529. if (ctx == NULL)
  530. return -EIO;
  531. if (iname->len < 3) {
  532. /*Check for . and .. */
  533. if (iname->name[0] == '.' && iname->name[iname->len-1] == '.') {
  534. oname->name[0] = '.';
  535. oname->name[iname->len-1] = '.';
  536. oname->len = iname->len;
  537. return oname->len;
  538. }
  539. }
  540. if (ctx->has_valid_key)
  541. return ext4_fname_decrypt(ctx, iname, oname);
  542. else
  543. return ext4_fname_hash(ctx, iname, oname);
  544. }
  545. int ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx,
  546. const struct ext4_dir_entry_2 *de,
  547. struct ext4_str *oname)
  548. {
  549. struct ext4_str iname = {.name = (unsigned char *) de->name,
  550. .len = de->name_len };
  551. return _ext4_fname_disk_to_usr(ctx, &iname, oname);
  552. }
  553. /**
  554. * ext4_fname_usr_to_disk() - converts a filename from user space to disk space
  555. */
  556. int ext4_fname_usr_to_disk(struct ext4_fname_crypto_ctx *ctx,
  557. const struct qstr *iname,
  558. struct ext4_str *oname)
  559. {
  560. int res;
  561. if (ctx == NULL)
  562. return -EIO;
  563. if (iname->len < 3) {
  564. /*Check for . and .. */
  565. if (iname->name[0] == '.' &&
  566. iname->name[iname->len-1] == '.') {
  567. oname->name[0] = '.';
  568. oname->name[iname->len-1] = '.';
  569. oname->len = iname->len;
  570. return oname->len;
  571. }
  572. }
  573. if (ctx->has_valid_key) {
  574. res = ext4_fname_encrypt(ctx, iname, oname);
  575. return res;
  576. }
  577. /* Without a proper key, a user is not allowed to modify the filenames
  578. * in a directory. Consequently, a user space name cannot be mapped to
  579. * a disk-space name */
  580. return -EACCES;
  581. }
  582. /*
  583. * Calculate the htree hash from a filename from user space
  584. */
  585. int ext4_fname_usr_to_hash(struct ext4_fname_crypto_ctx *ctx,
  586. const struct qstr *iname,
  587. struct dx_hash_info *hinfo)
  588. {
  589. struct ext4_str tmp, tmp2;
  590. int ret = 0;
  591. if (!ctx || !ctx->has_valid_key ||
  592. ((iname->name[0] == '.') &&
  593. ((iname->len == 1) ||
  594. ((iname->name[1] == '.') && (iname->len == 2))))) {
  595. ext4fs_dirhash(iname->name, iname->len, hinfo);
  596. return 0;
  597. }
  598. /* First encrypt the plaintext name */
  599. ret = ext4_fname_crypto_alloc_buffer(ctx, iname->len, &tmp);
  600. if (ret < 0)
  601. return ret;
  602. ret = ext4_fname_encrypt(ctx, iname, &tmp);
  603. if (ret < 0)
  604. goto out;
  605. tmp2.len = (4 * ((EXT4_FNAME_CRYPTO_DIGEST_SIZE + 2) / 3)) + 1;
  606. tmp2.name = kmalloc(tmp2.len + 1, GFP_KERNEL);
  607. if (tmp2.name == NULL) {
  608. ret = -ENOMEM;
  609. goto out;
  610. }
  611. ret = ext4_fname_hash(ctx, &tmp, &tmp2);
  612. if (ret > 0)
  613. ext4fs_dirhash(tmp2.name, tmp2.len, hinfo);
  614. ext4_fname_crypto_free_buffer(&tmp2);
  615. out:
  616. ext4_fname_crypto_free_buffer(&tmp);
  617. return ret;
  618. }
  619. /**
  620. * ext4_fname_disk_to_htree() - converts a filename from disk space to htree-access string
  621. */
  622. int ext4_fname_disk_to_hash(struct ext4_fname_crypto_ctx *ctx,
  623. const struct ext4_dir_entry_2 *de,
  624. struct dx_hash_info *hinfo)
  625. {
  626. struct ext4_str iname = {.name = (unsigned char *) de->name,
  627. .len = de->name_len};
  628. struct ext4_str tmp;
  629. int ret;
  630. if (!ctx ||
  631. ((iname.name[0] == '.') &&
  632. ((iname.len == 1) ||
  633. ((iname.name[1] == '.') && (iname.len == 2))))) {
  634. ext4fs_dirhash(iname.name, iname.len, hinfo);
  635. return 0;
  636. }
  637. tmp.len = (4 * ((EXT4_FNAME_CRYPTO_DIGEST_SIZE + 2) / 3)) + 1;
  638. tmp.name = kmalloc(tmp.len + 1, GFP_KERNEL);
  639. if (tmp.name == NULL)
  640. return -ENOMEM;
  641. ret = ext4_fname_hash(ctx, &iname, &tmp);
  642. if (ret > 0)
  643. ext4fs_dirhash(tmp.name, tmp.len, hinfo);
  644. ext4_fname_crypto_free_buffer(&tmp);
  645. return ret;
  646. }