crypto.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * linux/fs/f2fs/crypto.c
  3. *
  4. * Copied from linux/fs/ext4/crypto.c
  5. *
  6. * Copyright (C) 2015, Google, Inc.
  7. * Copyright (C) 2015, Motorola Mobility
  8. *
  9. * This contains encryption functions for f2fs
  10. *
  11. * Written by Michael Halcrow, 2014.
  12. *
  13. * Filename encryption additions
  14. * Uday Savagaonkar, 2014
  15. * Encryption policy handling additions
  16. * Ildar Muslukhov, 2014
  17. * Remove ext4_encrypted_zeroout(),
  18. * add f2fs_restore_and_release_control_page()
  19. * Jaegeuk Kim, 2015.
  20. *
  21. * This has not yet undergone a rigorous security audit.
  22. *
  23. * The usage of AES-XTS should conform to recommendations in NIST
  24. * Special Publication 800-38E and IEEE P1619/D16.
  25. */
  26. #include <crypto/hash.h>
  27. #include <crypto/sha.h>
  28. #include <keys/user-type.h>
  29. #include <keys/encrypted-type.h>
  30. #include <linux/crypto.h>
  31. #include <linux/ecryptfs.h>
  32. #include <linux/gfp.h>
  33. #include <linux/kernel.h>
  34. #include <linux/key.h>
  35. #include <linux/list.h>
  36. #include <linux/mempool.h>
  37. #include <linux/module.h>
  38. #include <linux/mutex.h>
  39. #include <linux/random.h>
  40. #include <linux/scatterlist.h>
  41. #include <linux/spinlock_types.h>
  42. #include <linux/f2fs_fs.h>
  43. #include <linux/ratelimit.h>
  44. #include <linux/bio.h>
  45. #include "f2fs.h"
  46. #include "xattr.h"
  47. /* Encryption added and removed here! (L: */
  48. static unsigned int num_prealloc_crypto_pages = 32;
  49. static unsigned int num_prealloc_crypto_ctxs = 128;
  50. module_param(num_prealloc_crypto_pages, uint, 0444);
  51. MODULE_PARM_DESC(num_prealloc_crypto_pages,
  52. "Number of crypto pages to preallocate");
  53. module_param(num_prealloc_crypto_ctxs, uint, 0444);
  54. MODULE_PARM_DESC(num_prealloc_crypto_ctxs,
  55. "Number of crypto contexts to preallocate");
  56. static mempool_t *f2fs_bounce_page_pool;
  57. static LIST_HEAD(f2fs_free_crypto_ctxs);
  58. static DEFINE_SPINLOCK(f2fs_crypto_ctx_lock);
  59. static struct workqueue_struct *f2fs_read_workqueue;
  60. static DEFINE_MUTEX(crypto_init);
  61. static struct kmem_cache *f2fs_crypto_ctx_cachep;
  62. struct kmem_cache *f2fs_crypt_info_cachep;
  63. /**
  64. * f2fs_release_crypto_ctx() - Releases an encryption context
  65. * @ctx: The encryption context to release.
  66. *
  67. * If the encryption context was allocated from the pre-allocated pool, returns
  68. * it to that pool. Else, frees it.
  69. *
  70. * If there's a bounce page in the context, this frees that.
  71. */
  72. void f2fs_release_crypto_ctx(struct f2fs_crypto_ctx *ctx)
  73. {
  74. unsigned long flags;
  75. if (ctx->flags & F2FS_WRITE_PATH_FL && ctx->w.bounce_page) {
  76. mempool_free(ctx->w.bounce_page, f2fs_bounce_page_pool);
  77. ctx->w.bounce_page = NULL;
  78. }
  79. ctx->w.control_page = NULL;
  80. if (ctx->flags & F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL) {
  81. kmem_cache_free(f2fs_crypto_ctx_cachep, ctx);
  82. } else {
  83. spin_lock_irqsave(&f2fs_crypto_ctx_lock, flags);
  84. list_add(&ctx->free_list, &f2fs_free_crypto_ctxs);
  85. spin_unlock_irqrestore(&f2fs_crypto_ctx_lock, flags);
  86. }
  87. }
  88. /**
  89. * f2fs_get_crypto_ctx() - Gets an encryption context
  90. * @inode: The inode for which we are doing the crypto
  91. *
  92. * Allocates and initializes an encryption context.
  93. *
  94. * Return: An allocated and initialized encryption context on success; error
  95. * value or NULL otherwise.
  96. */
  97. struct f2fs_crypto_ctx *f2fs_get_crypto_ctx(struct inode *inode)
  98. {
  99. struct f2fs_crypto_ctx *ctx = NULL;
  100. unsigned long flags;
  101. struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
  102. if (ci == NULL)
  103. return ERR_PTR(-ENOKEY);
  104. /*
  105. * We first try getting the ctx from a free list because in
  106. * the common case the ctx will have an allocated and
  107. * initialized crypto tfm, so it's probably a worthwhile
  108. * optimization. For the bounce page, we first try getting it
  109. * from the kernel allocator because that's just about as fast
  110. * as getting it from a list and because a cache of free pages
  111. * should generally be a "last resort" option for a filesystem
  112. * to be able to do its job.
  113. */
  114. spin_lock_irqsave(&f2fs_crypto_ctx_lock, flags);
  115. ctx = list_first_entry_or_null(&f2fs_free_crypto_ctxs,
  116. struct f2fs_crypto_ctx, free_list);
  117. if (ctx)
  118. list_del(&ctx->free_list);
  119. spin_unlock_irqrestore(&f2fs_crypto_ctx_lock, flags);
  120. if (!ctx) {
  121. ctx = kmem_cache_zalloc(f2fs_crypto_ctx_cachep, GFP_NOFS);
  122. if (!ctx)
  123. return ERR_PTR(-ENOMEM);
  124. ctx->flags |= F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL;
  125. } else {
  126. ctx->flags &= ~F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL;
  127. }
  128. ctx->flags &= ~F2FS_WRITE_PATH_FL;
  129. return ctx;
  130. }
  131. /*
  132. * Call f2fs_decrypt on every single page, reusing the encryption
  133. * context.
  134. */
  135. static void completion_pages(struct work_struct *work)
  136. {
  137. struct f2fs_crypto_ctx *ctx =
  138. container_of(work, struct f2fs_crypto_ctx, r.work);
  139. struct bio *bio = ctx->r.bio;
  140. struct bio_vec *bv;
  141. int i;
  142. bio_for_each_segment_all(bv, bio, i) {
  143. struct page *page = bv->bv_page;
  144. int ret = f2fs_decrypt(ctx, page);
  145. if (ret) {
  146. WARN_ON_ONCE(1);
  147. SetPageError(page);
  148. } else
  149. SetPageUptodate(page);
  150. unlock_page(page);
  151. }
  152. f2fs_release_crypto_ctx(ctx);
  153. bio_put(bio);
  154. }
  155. void f2fs_end_io_crypto_work(struct f2fs_crypto_ctx *ctx, struct bio *bio)
  156. {
  157. INIT_WORK(&ctx->r.work, completion_pages);
  158. ctx->r.bio = bio;
  159. queue_work(f2fs_read_workqueue, &ctx->r.work);
  160. }
  161. static void f2fs_crypto_destroy(void)
  162. {
  163. struct f2fs_crypto_ctx *pos, *n;
  164. list_for_each_entry_safe(pos, n, &f2fs_free_crypto_ctxs, free_list)
  165. kmem_cache_free(f2fs_crypto_ctx_cachep, pos);
  166. INIT_LIST_HEAD(&f2fs_free_crypto_ctxs);
  167. if (f2fs_bounce_page_pool)
  168. mempool_destroy(f2fs_bounce_page_pool);
  169. f2fs_bounce_page_pool = NULL;
  170. }
  171. /**
  172. * f2fs_crypto_initialize() - Set up for f2fs encryption.
  173. *
  174. * We only call this when we start accessing encrypted files, since it
  175. * results in memory getting allocated that wouldn't otherwise be used.
  176. *
  177. * Return: Zero on success, non-zero otherwise.
  178. */
  179. int f2fs_crypto_initialize(void)
  180. {
  181. int i, res = -ENOMEM;
  182. if (f2fs_bounce_page_pool)
  183. return 0;
  184. mutex_lock(&crypto_init);
  185. if (f2fs_bounce_page_pool)
  186. goto already_initialized;
  187. for (i = 0; i < num_prealloc_crypto_ctxs; i++) {
  188. struct f2fs_crypto_ctx *ctx;
  189. ctx = kmem_cache_zalloc(f2fs_crypto_ctx_cachep, GFP_KERNEL);
  190. if (!ctx)
  191. goto fail;
  192. list_add(&ctx->free_list, &f2fs_free_crypto_ctxs);
  193. }
  194. /* must be allocated at the last step to avoid race condition above */
  195. f2fs_bounce_page_pool =
  196. mempool_create_page_pool(num_prealloc_crypto_pages, 0);
  197. if (!f2fs_bounce_page_pool)
  198. goto fail;
  199. already_initialized:
  200. mutex_unlock(&crypto_init);
  201. return 0;
  202. fail:
  203. f2fs_crypto_destroy();
  204. mutex_unlock(&crypto_init);
  205. return res;
  206. }
  207. /**
  208. * f2fs_exit_crypto() - Shutdown the f2fs encryption system
  209. */
  210. void f2fs_exit_crypto(void)
  211. {
  212. f2fs_crypto_destroy();
  213. if (f2fs_read_workqueue)
  214. destroy_workqueue(f2fs_read_workqueue);
  215. if (f2fs_crypto_ctx_cachep)
  216. kmem_cache_destroy(f2fs_crypto_ctx_cachep);
  217. if (f2fs_crypt_info_cachep)
  218. kmem_cache_destroy(f2fs_crypt_info_cachep);
  219. }
  220. int __init f2fs_init_crypto(void)
  221. {
  222. int res = -ENOMEM;
  223. f2fs_read_workqueue = alloc_workqueue("f2fs_crypto", WQ_HIGHPRI, 0);
  224. if (!f2fs_read_workqueue)
  225. goto fail;
  226. f2fs_crypto_ctx_cachep = KMEM_CACHE(f2fs_crypto_ctx,
  227. SLAB_RECLAIM_ACCOUNT);
  228. if (!f2fs_crypto_ctx_cachep)
  229. goto fail;
  230. f2fs_crypt_info_cachep = KMEM_CACHE(f2fs_crypt_info,
  231. SLAB_RECLAIM_ACCOUNT);
  232. if (!f2fs_crypt_info_cachep)
  233. goto fail;
  234. return 0;
  235. fail:
  236. f2fs_exit_crypto();
  237. return res;
  238. }
  239. void f2fs_restore_and_release_control_page(struct page **page)
  240. {
  241. struct f2fs_crypto_ctx *ctx;
  242. struct page *bounce_page;
  243. /* The bounce data pages are unmapped. */
  244. if ((*page)->mapping)
  245. return;
  246. /* The bounce data page is unmapped. */
  247. bounce_page = *page;
  248. ctx = (struct f2fs_crypto_ctx *)page_private(bounce_page);
  249. /* restore control page */
  250. *page = ctx->w.control_page;
  251. f2fs_restore_control_page(bounce_page);
  252. }
  253. void f2fs_restore_control_page(struct page *data_page)
  254. {
  255. struct f2fs_crypto_ctx *ctx =
  256. (struct f2fs_crypto_ctx *)page_private(data_page);
  257. set_page_private(data_page, (unsigned long)NULL);
  258. ClearPagePrivate(data_page);
  259. unlock_page(data_page);
  260. f2fs_release_crypto_ctx(ctx);
  261. }
  262. /**
  263. * f2fs_crypt_complete() - The completion callback for page encryption
  264. * @req: The asynchronous encryption request context
  265. * @res: The result of the encryption operation
  266. */
  267. static void f2fs_crypt_complete(struct crypto_async_request *req, int res)
  268. {
  269. struct f2fs_completion_result *ecr = req->data;
  270. if (res == -EINPROGRESS)
  271. return;
  272. ecr->res = res;
  273. complete(&ecr->completion);
  274. }
  275. typedef enum {
  276. F2FS_DECRYPT = 0,
  277. F2FS_ENCRYPT,
  278. } f2fs_direction_t;
  279. static int f2fs_page_crypto(struct f2fs_crypto_ctx *ctx,
  280. struct inode *inode,
  281. f2fs_direction_t rw,
  282. pgoff_t index,
  283. struct page *src_page,
  284. struct page *dest_page)
  285. {
  286. u8 xts_tweak[F2FS_XTS_TWEAK_SIZE];
  287. struct ablkcipher_request *req = NULL;
  288. DECLARE_F2FS_COMPLETION_RESULT(ecr);
  289. struct scatterlist dst, src;
  290. struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
  291. struct crypto_ablkcipher *tfm = ci->ci_ctfm;
  292. int res = 0;
  293. req = ablkcipher_request_alloc(tfm, GFP_NOFS);
  294. if (!req) {
  295. printk_ratelimited(KERN_ERR
  296. "%s: crypto_request_alloc() failed\n",
  297. __func__);
  298. return -ENOMEM;
  299. }
  300. ablkcipher_request_set_callback(
  301. req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  302. f2fs_crypt_complete, &ecr);
  303. BUILD_BUG_ON(F2FS_XTS_TWEAK_SIZE < sizeof(index));
  304. memcpy(xts_tweak, &index, sizeof(index));
  305. memset(&xts_tweak[sizeof(index)], 0,
  306. F2FS_XTS_TWEAK_SIZE - sizeof(index));
  307. sg_init_table(&dst, 1);
  308. sg_set_page(&dst, dest_page, PAGE_CACHE_SIZE, 0);
  309. sg_init_table(&src, 1);
  310. sg_set_page(&src, src_page, PAGE_CACHE_SIZE, 0);
  311. ablkcipher_request_set_crypt(req, &src, &dst, PAGE_CACHE_SIZE,
  312. xts_tweak);
  313. if (rw == F2FS_DECRYPT)
  314. res = crypto_ablkcipher_decrypt(req);
  315. else
  316. res = crypto_ablkcipher_encrypt(req);
  317. if (res == -EINPROGRESS || res == -EBUSY) {
  318. wait_for_completion(&ecr.completion);
  319. res = ecr.res;
  320. }
  321. ablkcipher_request_free(req);
  322. if (res) {
  323. printk_ratelimited(KERN_ERR
  324. "%s: crypto_ablkcipher_encrypt() returned %d\n",
  325. __func__, res);
  326. return res;
  327. }
  328. return 0;
  329. }
  330. static struct page *alloc_bounce_page(struct f2fs_crypto_ctx *ctx)
  331. {
  332. ctx->w.bounce_page = mempool_alloc(f2fs_bounce_page_pool, GFP_NOWAIT);
  333. if (ctx->w.bounce_page == NULL)
  334. return ERR_PTR(-ENOMEM);
  335. ctx->flags |= F2FS_WRITE_PATH_FL;
  336. return ctx->w.bounce_page;
  337. }
  338. /**
  339. * f2fs_encrypt() - Encrypts a page
  340. * @inode: The inode for which the encryption should take place
  341. * @plaintext_page: The page to encrypt. Must be locked.
  342. *
  343. * Allocates a ciphertext page and encrypts plaintext_page into it using the ctx
  344. * encryption context.
  345. *
  346. * Called on the page write path. The caller must call
  347. * f2fs_restore_control_page() on the returned ciphertext page to
  348. * release the bounce buffer and the encryption context.
  349. *
  350. * Return: An allocated page with the encrypted content on success. Else, an
  351. * error value or NULL.
  352. */
  353. struct page *f2fs_encrypt(struct inode *inode,
  354. struct page *plaintext_page)
  355. {
  356. struct f2fs_crypto_ctx *ctx;
  357. struct page *ciphertext_page = NULL;
  358. int err;
  359. BUG_ON(!PageLocked(plaintext_page));
  360. ctx = f2fs_get_crypto_ctx(inode);
  361. if (IS_ERR(ctx))
  362. return (struct page *)ctx;
  363. /* The encryption operation will require a bounce page. */
  364. ciphertext_page = alloc_bounce_page(ctx);
  365. if (IS_ERR(ciphertext_page))
  366. goto err_out;
  367. ctx->w.control_page = plaintext_page;
  368. err = f2fs_page_crypto(ctx, inode, F2FS_ENCRYPT, plaintext_page->index,
  369. plaintext_page, ciphertext_page);
  370. if (err) {
  371. ciphertext_page = ERR_PTR(err);
  372. goto err_out;
  373. }
  374. SetPagePrivate(ciphertext_page);
  375. set_page_private(ciphertext_page, (unsigned long)ctx);
  376. lock_page(ciphertext_page);
  377. return ciphertext_page;
  378. err_out:
  379. f2fs_release_crypto_ctx(ctx);
  380. return ciphertext_page;
  381. }
  382. /**
  383. * f2fs_decrypt() - Decrypts a page in-place
  384. * @ctx: The encryption context.
  385. * @page: The page to decrypt. Must be locked.
  386. *
  387. * Decrypts page in-place using the ctx encryption context.
  388. *
  389. * Called from the read completion callback.
  390. *
  391. * Return: Zero on success, non-zero otherwise.
  392. */
  393. int f2fs_decrypt(struct f2fs_crypto_ctx *ctx, struct page *page)
  394. {
  395. BUG_ON(!PageLocked(page));
  396. return f2fs_page_crypto(ctx, page->mapping->host,
  397. F2FS_DECRYPT, page->index, page, page);
  398. }
  399. /*
  400. * Convenience function which takes care of allocating and
  401. * deallocating the encryption context
  402. */
  403. int f2fs_decrypt_one(struct inode *inode, struct page *page)
  404. {
  405. struct f2fs_crypto_ctx *ctx = f2fs_get_crypto_ctx(inode);
  406. int ret;
  407. if (IS_ERR(ctx))
  408. return PTR_ERR(ctx);
  409. ret = f2fs_decrypt(ctx, page);
  410. f2fs_release_crypto_ctx(ctx);
  411. return ret;
  412. }
  413. bool f2fs_valid_contents_enc_mode(uint32_t mode)
  414. {
  415. return (mode == F2FS_ENCRYPTION_MODE_AES_256_XTS);
  416. }
  417. /**
  418. * f2fs_validate_encryption_key_size() - Validate the encryption key size
  419. * @mode: The key mode.
  420. * @size: The key size to validate.
  421. *
  422. * Return: The validated key size for @mode. Zero if invalid.
  423. */
  424. uint32_t f2fs_validate_encryption_key_size(uint32_t mode, uint32_t size)
  425. {
  426. if (size == f2fs_encryption_key_size(mode))
  427. return size;
  428. return 0;
  429. }