crypto.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * linux/fs/ext4/crypto.c
  3. *
  4. * Copyright (C) 2015, Google, Inc.
  5. *
  6. * This contains encryption functions for ext4
  7. *
  8. * Written by Michael Halcrow, 2014.
  9. *
  10. * Filename encryption additions
  11. * Uday Savagaonkar, 2014
  12. * Encryption policy handling additions
  13. * Ildar Muslukhov, 2014
  14. *
  15. * This has not yet undergone a rigorous security audit.
  16. *
  17. * The usage of AES-XTS should conform to recommendations in NIST
  18. * Special Publication 800-38E and IEEE P1619/D16.
  19. */
  20. #include <crypto/hash.h>
  21. #include <crypto/sha.h>
  22. #include <keys/user-type.h>
  23. #include <keys/encrypted-type.h>
  24. #include <linux/crypto.h>
  25. #include <linux/ecryptfs.h>
  26. #include <linux/gfp.h>
  27. #include <linux/kernel.h>
  28. #include <linux/key.h>
  29. #include <linux/list.h>
  30. #include <linux/mempool.h>
  31. #include <linux/module.h>
  32. #include <linux/mutex.h>
  33. #include <linux/random.h>
  34. #include <linux/scatterlist.h>
  35. #include <linux/spinlock_types.h>
  36. #include "ext4_extents.h"
  37. #include "xattr.h"
  38. /* Encryption added and removed here! (L: */
  39. static unsigned int num_prealloc_crypto_pages = 32;
  40. static unsigned int num_prealloc_crypto_ctxs = 128;
  41. module_param(num_prealloc_crypto_pages, uint, 0444);
  42. MODULE_PARM_DESC(num_prealloc_crypto_pages,
  43. "Number of crypto pages to preallocate");
  44. module_param(num_prealloc_crypto_ctxs, uint, 0444);
  45. MODULE_PARM_DESC(num_prealloc_crypto_ctxs,
  46. "Number of crypto contexts to preallocate");
  47. static mempool_t *ext4_bounce_page_pool;
  48. static LIST_HEAD(ext4_free_crypto_ctxs);
  49. static DEFINE_SPINLOCK(ext4_crypto_ctx_lock);
  50. /**
  51. * ext4_release_crypto_ctx() - Releases an encryption context
  52. * @ctx: The encryption context to release.
  53. *
  54. * If the encryption context was allocated from the pre-allocated pool, returns
  55. * it to that pool. Else, frees it.
  56. *
  57. * If there's a bounce page in the context, this frees that.
  58. */
  59. void ext4_release_crypto_ctx(struct ext4_crypto_ctx *ctx)
  60. {
  61. unsigned long flags;
  62. if (ctx->bounce_page) {
  63. if (ctx->flags & EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL)
  64. __free_page(ctx->bounce_page);
  65. else
  66. mempool_free(ctx->bounce_page, ext4_bounce_page_pool);
  67. ctx->bounce_page = NULL;
  68. }
  69. ctx->control_page = NULL;
  70. if (ctx->flags & EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL) {
  71. if (ctx->tfm)
  72. crypto_free_tfm(ctx->tfm);
  73. kfree(ctx);
  74. } else {
  75. spin_lock_irqsave(&ext4_crypto_ctx_lock, flags);
  76. list_add(&ctx->free_list, &ext4_free_crypto_ctxs);
  77. spin_unlock_irqrestore(&ext4_crypto_ctx_lock, flags);
  78. }
  79. }
  80. /**
  81. * ext4_alloc_and_init_crypto_ctx() - Allocates and inits an encryption context
  82. * @mask: The allocation mask.
  83. *
  84. * Return: An allocated and initialized encryption context on success. An error
  85. * value or NULL otherwise.
  86. */
  87. static struct ext4_crypto_ctx *ext4_alloc_and_init_crypto_ctx(gfp_t mask)
  88. {
  89. struct ext4_crypto_ctx *ctx = kzalloc(sizeof(struct ext4_crypto_ctx),
  90. mask);
  91. if (!ctx)
  92. return ERR_PTR(-ENOMEM);
  93. return ctx;
  94. }
  95. /**
  96. * ext4_get_crypto_ctx() - Gets an encryption context
  97. * @inode: The inode for which we are doing the crypto
  98. *
  99. * Allocates and initializes an encryption context.
  100. *
  101. * Return: An allocated and initialized encryption context on success; error
  102. * value or NULL otherwise.
  103. */
  104. struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode)
  105. {
  106. struct ext4_crypto_ctx *ctx = NULL;
  107. int res = 0;
  108. unsigned long flags;
  109. struct ext4_encryption_key *key = &EXT4_I(inode)->i_encryption_key;
  110. if (!ext4_read_workqueue)
  111. ext4_init_crypto();
  112. /*
  113. * We first try getting the ctx from a free list because in
  114. * the common case the ctx will have an allocated and
  115. * initialized crypto tfm, so it's probably a worthwhile
  116. * optimization. For the bounce page, we first try getting it
  117. * from the kernel allocator because that's just about as fast
  118. * as getting it from a list and because a cache of free pages
  119. * should generally be a "last resort" option for a filesystem
  120. * to be able to do its job.
  121. */
  122. spin_lock_irqsave(&ext4_crypto_ctx_lock, flags);
  123. ctx = list_first_entry_or_null(&ext4_free_crypto_ctxs,
  124. struct ext4_crypto_ctx, free_list);
  125. if (ctx)
  126. list_del(&ctx->free_list);
  127. spin_unlock_irqrestore(&ext4_crypto_ctx_lock, flags);
  128. if (!ctx) {
  129. ctx = ext4_alloc_and_init_crypto_ctx(GFP_NOFS);
  130. if (IS_ERR(ctx)) {
  131. res = PTR_ERR(ctx);
  132. goto out;
  133. }
  134. ctx->flags |= EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL;
  135. } else {
  136. ctx->flags &= ~EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL;
  137. }
  138. /* Allocate a new Crypto API context if we don't already have
  139. * one or if it isn't the right mode. */
  140. BUG_ON(key->mode == EXT4_ENCRYPTION_MODE_INVALID);
  141. if (ctx->tfm && (ctx->mode != key->mode)) {
  142. crypto_free_tfm(ctx->tfm);
  143. ctx->tfm = NULL;
  144. ctx->mode = EXT4_ENCRYPTION_MODE_INVALID;
  145. }
  146. if (!ctx->tfm) {
  147. switch (key->mode) {
  148. case EXT4_ENCRYPTION_MODE_AES_256_XTS:
  149. ctx->tfm = crypto_ablkcipher_tfm(
  150. crypto_alloc_ablkcipher("xts(aes)", 0, 0));
  151. break;
  152. case EXT4_ENCRYPTION_MODE_AES_256_GCM:
  153. /* TODO(mhalcrow): AEAD w/ gcm(aes);
  154. * crypto_aead_setauthsize() */
  155. ctx->tfm = ERR_PTR(-ENOTSUPP);
  156. break;
  157. default:
  158. BUG();
  159. }
  160. if (IS_ERR_OR_NULL(ctx->tfm)) {
  161. res = PTR_ERR(ctx->tfm);
  162. ctx->tfm = NULL;
  163. goto out;
  164. }
  165. ctx->mode = key->mode;
  166. }
  167. BUG_ON(key->size != ext4_encryption_key_size(key->mode));
  168. /* There shouldn't be a bounce page attached to the crypto
  169. * context at this point. */
  170. BUG_ON(ctx->bounce_page);
  171. out:
  172. if (res) {
  173. if (!IS_ERR_OR_NULL(ctx))
  174. ext4_release_crypto_ctx(ctx);
  175. ctx = ERR_PTR(res);
  176. }
  177. return ctx;
  178. }
  179. struct workqueue_struct *ext4_read_workqueue;
  180. static DEFINE_MUTEX(crypto_init);
  181. /**
  182. * ext4_exit_crypto() - Shutdown the ext4 encryption system
  183. */
  184. void ext4_exit_crypto(void)
  185. {
  186. struct ext4_crypto_ctx *pos, *n;
  187. list_for_each_entry_safe(pos, n, &ext4_free_crypto_ctxs, free_list) {
  188. if (pos->bounce_page) {
  189. if (pos->flags &
  190. EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL) {
  191. __free_page(pos->bounce_page);
  192. } else {
  193. mempool_free(pos->bounce_page,
  194. ext4_bounce_page_pool);
  195. }
  196. }
  197. if (pos->tfm)
  198. crypto_free_tfm(pos->tfm);
  199. kfree(pos);
  200. }
  201. INIT_LIST_HEAD(&ext4_free_crypto_ctxs);
  202. if (ext4_bounce_page_pool)
  203. mempool_destroy(ext4_bounce_page_pool);
  204. ext4_bounce_page_pool = NULL;
  205. if (ext4_read_workqueue)
  206. destroy_workqueue(ext4_read_workqueue);
  207. ext4_read_workqueue = NULL;
  208. }
  209. /**
  210. * ext4_init_crypto() - Set up for ext4 encryption.
  211. *
  212. * We only call this when we start accessing encrypted files, since it
  213. * results in memory getting allocated that wouldn't otherwise be used.
  214. *
  215. * Return: Zero on success, non-zero otherwise.
  216. */
  217. int ext4_init_crypto(void)
  218. {
  219. int i, res;
  220. mutex_lock(&crypto_init);
  221. if (ext4_read_workqueue)
  222. goto already_initialized;
  223. ext4_read_workqueue = alloc_workqueue("ext4_crypto", WQ_HIGHPRI, 0);
  224. if (!ext4_read_workqueue) {
  225. res = -ENOMEM;
  226. goto fail;
  227. }
  228. for (i = 0; i < num_prealloc_crypto_ctxs; i++) {
  229. struct ext4_crypto_ctx *ctx;
  230. ctx = ext4_alloc_and_init_crypto_ctx(GFP_KERNEL);
  231. if (IS_ERR(ctx)) {
  232. res = PTR_ERR(ctx);
  233. goto fail;
  234. }
  235. list_add(&ctx->free_list, &ext4_free_crypto_ctxs);
  236. }
  237. ext4_bounce_page_pool =
  238. mempool_create_page_pool(num_prealloc_crypto_pages, 0);
  239. if (!ext4_bounce_page_pool) {
  240. res = -ENOMEM;
  241. goto fail;
  242. }
  243. already_initialized:
  244. mutex_unlock(&crypto_init);
  245. return 0;
  246. fail:
  247. ext4_exit_crypto();
  248. mutex_unlock(&crypto_init);
  249. return res;
  250. }
  251. void ext4_restore_control_page(struct page *data_page)
  252. {
  253. struct ext4_crypto_ctx *ctx =
  254. (struct ext4_crypto_ctx *)page_private(data_page);
  255. set_page_private(data_page, (unsigned long)NULL);
  256. ClearPagePrivate(data_page);
  257. unlock_page(data_page);
  258. ext4_release_crypto_ctx(ctx);
  259. }
  260. /**
  261. * ext4_crypt_complete() - The completion callback for page encryption
  262. * @req: The asynchronous encryption request context
  263. * @res: The result of the encryption operation
  264. */
  265. static void ext4_crypt_complete(struct crypto_async_request *req, int res)
  266. {
  267. struct ext4_completion_result *ecr = req->data;
  268. if (res == -EINPROGRESS)
  269. return;
  270. ecr->res = res;
  271. complete(&ecr->completion);
  272. }
  273. typedef enum {
  274. EXT4_DECRYPT = 0,
  275. EXT4_ENCRYPT,
  276. } ext4_direction_t;
  277. static int ext4_page_crypto(struct ext4_crypto_ctx *ctx,
  278. struct inode *inode,
  279. ext4_direction_t rw,
  280. pgoff_t index,
  281. struct page *src_page,
  282. struct page *dest_page)
  283. {
  284. u8 xts_tweak[EXT4_XTS_TWEAK_SIZE];
  285. struct ablkcipher_request *req = NULL;
  286. DECLARE_EXT4_COMPLETION_RESULT(ecr);
  287. struct scatterlist dst, src;
  288. struct ext4_inode_info *ei = EXT4_I(inode);
  289. struct crypto_ablkcipher *atfm = __crypto_ablkcipher_cast(ctx->tfm);
  290. int res = 0;
  291. BUG_ON(!ctx->tfm);
  292. BUG_ON(ctx->mode != ei->i_encryption_key.mode);
  293. if (ctx->mode != EXT4_ENCRYPTION_MODE_AES_256_XTS) {
  294. printk_ratelimited(KERN_ERR
  295. "%s: unsupported crypto algorithm: %d\n",
  296. __func__, ctx->mode);
  297. return -ENOTSUPP;
  298. }
  299. crypto_ablkcipher_clear_flags(atfm, ~0);
  300. crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  301. res = crypto_ablkcipher_setkey(atfm, ei->i_encryption_key.raw,
  302. ei->i_encryption_key.size);
  303. if (res) {
  304. printk_ratelimited(KERN_ERR
  305. "%s: crypto_ablkcipher_setkey() failed\n",
  306. __func__);
  307. return res;
  308. }
  309. req = ablkcipher_request_alloc(atfm, GFP_NOFS);
  310. if (!req) {
  311. printk_ratelimited(KERN_ERR
  312. "%s: crypto_request_alloc() failed\n",
  313. __func__);
  314. return -ENOMEM;
  315. }
  316. ablkcipher_request_set_callback(
  317. req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  318. ext4_crypt_complete, &ecr);
  319. BUILD_BUG_ON(EXT4_XTS_TWEAK_SIZE < sizeof(index));
  320. memcpy(xts_tweak, &index, sizeof(index));
  321. memset(&xts_tweak[sizeof(index)], 0,
  322. EXT4_XTS_TWEAK_SIZE - sizeof(index));
  323. sg_init_table(&dst, 1);
  324. sg_set_page(&dst, dest_page, PAGE_CACHE_SIZE, 0);
  325. sg_init_table(&src, 1);
  326. sg_set_page(&src, src_page, PAGE_CACHE_SIZE, 0);
  327. ablkcipher_request_set_crypt(req, &src, &dst, PAGE_CACHE_SIZE,
  328. xts_tweak);
  329. if (rw == EXT4_DECRYPT)
  330. res = crypto_ablkcipher_decrypt(req);
  331. else
  332. res = crypto_ablkcipher_encrypt(req);
  333. if (res == -EINPROGRESS || res == -EBUSY) {
  334. BUG_ON(req->base.data != &ecr);
  335. wait_for_completion(&ecr.completion);
  336. res = ecr.res;
  337. }
  338. ablkcipher_request_free(req);
  339. if (res) {
  340. printk_ratelimited(
  341. KERN_ERR
  342. "%s: crypto_ablkcipher_encrypt() returned %d\n",
  343. __func__, res);
  344. return res;
  345. }
  346. return 0;
  347. }
  348. /**
  349. * ext4_encrypt() - Encrypts a page
  350. * @inode: The inode for which the encryption should take place
  351. * @plaintext_page: The page to encrypt. Must be locked.
  352. *
  353. * Allocates a ciphertext page and encrypts plaintext_page into it using the ctx
  354. * encryption context.
  355. *
  356. * Called on the page write path. The caller must call
  357. * ext4_restore_control_page() on the returned ciphertext page to
  358. * release the bounce buffer and the encryption context.
  359. *
  360. * Return: An allocated page with the encrypted content on success. Else, an
  361. * error value or NULL.
  362. */
  363. struct page *ext4_encrypt(struct inode *inode,
  364. struct page *plaintext_page)
  365. {
  366. struct ext4_crypto_ctx *ctx;
  367. struct page *ciphertext_page = NULL;
  368. int err;
  369. BUG_ON(!PageLocked(plaintext_page));
  370. ctx = ext4_get_crypto_ctx(inode);
  371. if (IS_ERR(ctx))
  372. return (struct page *) ctx;
  373. /* The encryption operation will require a bounce page. */
  374. ciphertext_page = alloc_page(GFP_NOFS);
  375. if (!ciphertext_page) {
  376. /* This is a potential bottleneck, but at least we'll have
  377. * forward progress. */
  378. ciphertext_page = mempool_alloc(ext4_bounce_page_pool,
  379. GFP_NOFS);
  380. if (WARN_ON_ONCE(!ciphertext_page)) {
  381. ciphertext_page = mempool_alloc(ext4_bounce_page_pool,
  382. GFP_NOFS | __GFP_WAIT);
  383. }
  384. ctx->flags &= ~EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
  385. } else {
  386. ctx->flags |= EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
  387. }
  388. ctx->bounce_page = ciphertext_page;
  389. ctx->control_page = plaintext_page;
  390. err = ext4_page_crypto(ctx, inode, EXT4_ENCRYPT, plaintext_page->index,
  391. plaintext_page, ciphertext_page);
  392. if (err) {
  393. ext4_release_crypto_ctx(ctx);
  394. return ERR_PTR(err);
  395. }
  396. SetPagePrivate(ciphertext_page);
  397. set_page_private(ciphertext_page, (unsigned long)ctx);
  398. lock_page(ciphertext_page);
  399. return ciphertext_page;
  400. }
  401. /**
  402. * ext4_decrypt() - Decrypts a page in-place
  403. * @ctx: The encryption context.
  404. * @page: The page to decrypt. Must be locked.
  405. *
  406. * Decrypts page in-place using the ctx encryption context.
  407. *
  408. * Called from the read completion callback.
  409. *
  410. * Return: Zero on success, non-zero otherwise.
  411. */
  412. int ext4_decrypt(struct ext4_crypto_ctx *ctx, struct page *page)
  413. {
  414. BUG_ON(!PageLocked(page));
  415. return ext4_page_crypto(ctx, page->mapping->host,
  416. EXT4_DECRYPT, page->index, page, page);
  417. }
  418. /*
  419. * Convenience function which takes care of allocating and
  420. * deallocating the encryption context
  421. */
  422. int ext4_decrypt_one(struct inode *inode, struct page *page)
  423. {
  424. int ret;
  425. struct ext4_crypto_ctx *ctx = ext4_get_crypto_ctx(inode);
  426. if (!ctx)
  427. return -ENOMEM;
  428. ret = ext4_decrypt(ctx, page);
  429. ext4_release_crypto_ctx(ctx);
  430. return ret;
  431. }
  432. int ext4_encrypted_zeroout(struct inode *inode, struct ext4_extent *ex)
  433. {
  434. struct ext4_crypto_ctx *ctx;
  435. struct page *ciphertext_page = NULL;
  436. struct bio *bio;
  437. ext4_lblk_t lblk = ex->ee_block;
  438. ext4_fsblk_t pblk = ext4_ext_pblock(ex);
  439. unsigned int len = ext4_ext_get_actual_len(ex);
  440. int err = 0;
  441. BUG_ON(inode->i_sb->s_blocksize != PAGE_CACHE_SIZE);
  442. ctx = ext4_get_crypto_ctx(inode);
  443. if (IS_ERR(ctx))
  444. return PTR_ERR(ctx);
  445. ciphertext_page = alloc_page(GFP_NOFS);
  446. if (!ciphertext_page) {
  447. /* This is a potential bottleneck, but at least we'll have
  448. * forward progress. */
  449. ciphertext_page = mempool_alloc(ext4_bounce_page_pool,
  450. GFP_NOFS);
  451. if (WARN_ON_ONCE(!ciphertext_page)) {
  452. ciphertext_page = mempool_alloc(ext4_bounce_page_pool,
  453. GFP_NOFS | __GFP_WAIT);
  454. }
  455. ctx->flags &= ~EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
  456. } else {
  457. ctx->flags |= EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
  458. }
  459. ctx->bounce_page = ciphertext_page;
  460. while (len--) {
  461. err = ext4_page_crypto(ctx, inode, EXT4_ENCRYPT, lblk,
  462. ZERO_PAGE(0), ciphertext_page);
  463. if (err)
  464. goto errout;
  465. bio = bio_alloc(GFP_KERNEL, 1);
  466. if (!bio) {
  467. err = -ENOMEM;
  468. goto errout;
  469. }
  470. bio->bi_bdev = inode->i_sb->s_bdev;
  471. bio->bi_iter.bi_sector = pblk;
  472. err = bio_add_page(bio, ciphertext_page,
  473. inode->i_sb->s_blocksize, 0);
  474. if (err) {
  475. bio_put(bio);
  476. goto errout;
  477. }
  478. err = submit_bio_wait(WRITE, bio);
  479. if (err)
  480. goto errout;
  481. }
  482. err = 0;
  483. errout:
  484. ext4_release_crypto_ctx(ctx);
  485. return err;
  486. }
  487. bool ext4_valid_contents_enc_mode(uint32_t mode)
  488. {
  489. return (mode == EXT4_ENCRYPTION_MODE_AES_256_XTS);
  490. }
  491. /**
  492. * ext4_validate_encryption_key_size() - Validate the encryption key size
  493. * @mode: The key mode.
  494. * @size: The key size to validate.
  495. *
  496. * Return: The validated key size for @mode. Zero if invalid.
  497. */
  498. uint32_t ext4_validate_encryption_key_size(uint32_t mode, uint32_t size)
  499. {
  500. if (size == ext4_encryption_key_size(mode))
  501. return size;
  502. return 0;
  503. }