crypto_fname.c 18 KB

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