Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 2 | /* |
| 3 | * This contains encryption functions for per-file encryption. |
| 4 | * |
| 5 | * Copyright (C) 2015, Google, Inc. |
| 6 | * Copyright (C) 2015, Motorola Mobility |
| 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 | * Add fscrypt_pullback_bio_page() |
| 15 | * Jaegeuk Kim, 2015. |
| 16 | * |
| 17 | * This has not yet undergone a rigorous security audit. |
| 18 | * |
| 19 | * The usage of AES-XTS should conform to recommendations in NIST |
| 20 | * Special Publication 800-38E and IEEE P1619/D16. |
| 21 | */ |
| 22 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 23 | #include <linux/pagemap.h> |
| 24 | #include <linux/mempool.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/scatterlist.h> |
| 27 | #include <linux/ratelimit.h> |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 28 | #include <linux/dcache.h> |
Jaegeuk Kim | 03a8bb0 | 2016-04-12 16:05:36 -0700 | [diff] [blame] | 29 | #include <linux/namei.h> |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 30 | #include <crypto/aes.h> |
Eric Biggers | a575784 | 2018-01-05 10:45:00 -0800 | [diff] [blame] | 31 | #include <crypto/skcipher.h> |
Theodore Ts'o | cc4e0df | 2016-11-26 22:05:18 -0500 | [diff] [blame] | 32 | #include "fscrypt_private.h" |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 33 | |
| 34 | static unsigned int num_prealloc_crypto_pages = 32; |
| 35 | static unsigned int num_prealloc_crypto_ctxs = 128; |
| 36 | |
| 37 | module_param(num_prealloc_crypto_pages, uint, 0444); |
| 38 | MODULE_PARM_DESC(num_prealloc_crypto_pages, |
| 39 | "Number of crypto pages to preallocate"); |
| 40 | module_param(num_prealloc_crypto_ctxs, uint, 0444); |
| 41 | MODULE_PARM_DESC(num_prealloc_crypto_ctxs, |
| 42 | "Number of crypto contexts to preallocate"); |
| 43 | |
| 44 | static mempool_t *fscrypt_bounce_page_pool = NULL; |
| 45 | |
| 46 | static LIST_HEAD(fscrypt_free_ctxs); |
| 47 | static DEFINE_SPINLOCK(fscrypt_ctx_lock); |
| 48 | |
Eric Biggers | 0cb8dae | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 49 | static struct workqueue_struct *fscrypt_read_workqueue; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 50 | static DEFINE_MUTEX(fscrypt_init_mutex); |
| 51 | |
| 52 | static struct kmem_cache *fscrypt_ctx_cachep; |
| 53 | struct kmem_cache *fscrypt_info_cachep; |
| 54 | |
Eric Biggers | 0cb8dae | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 55 | void fscrypt_enqueue_decrypt_work(struct work_struct *work) |
| 56 | { |
| 57 | queue_work(fscrypt_read_workqueue, work); |
| 58 | } |
| 59 | EXPORT_SYMBOL(fscrypt_enqueue_decrypt_work); |
| 60 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 61 | /** |
Eric Biggers | 2a415a0 | 2019-05-20 09:29:40 -0700 | [diff] [blame] | 62 | * fscrypt_release_ctx() - Release a decryption context |
| 63 | * @ctx: The decryption context to release. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 64 | * |
Eric Biggers | 2a415a0 | 2019-05-20 09:29:40 -0700 | [diff] [blame] | 65 | * If the decryption context was allocated from the pre-allocated pool, return |
| 66 | * it to that pool. Else, free it. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 67 | */ |
| 68 | void fscrypt_release_ctx(struct fscrypt_ctx *ctx) |
| 69 | { |
| 70 | unsigned long flags; |
| 71 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 72 | if (ctx->flags & FS_CTX_REQUIRES_FREE_ENCRYPT_FL) { |
| 73 | kmem_cache_free(fscrypt_ctx_cachep, ctx); |
| 74 | } else { |
| 75 | spin_lock_irqsave(&fscrypt_ctx_lock, flags); |
| 76 | list_add(&ctx->free_list, &fscrypt_free_ctxs); |
| 77 | spin_unlock_irqrestore(&fscrypt_ctx_lock, flags); |
| 78 | } |
| 79 | } |
| 80 | EXPORT_SYMBOL(fscrypt_release_ctx); |
| 81 | |
| 82 | /** |
Eric Biggers | 2a415a0 | 2019-05-20 09:29:40 -0700 | [diff] [blame] | 83 | * fscrypt_get_ctx() - Get a decryption context |
Jaegeuk Kim | b32e4482 | 2016-04-11 15:51:57 -0700 | [diff] [blame] | 84 | * @gfp_flags: The gfp flag for memory allocation |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 85 | * |
Eric Biggers | 2a415a0 | 2019-05-20 09:29:40 -0700 | [diff] [blame] | 86 | * Allocate and initialize a decryption context. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 87 | * |
Eric Biggers | 2a415a0 | 2019-05-20 09:29:40 -0700 | [diff] [blame] | 88 | * Return: A new decryption context on success; an ERR_PTR() otherwise. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 89 | */ |
Eric Biggers | cd0265f | 2019-03-18 10:23:33 -0700 | [diff] [blame] | 90 | struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 91 | { |
Eric Biggers | cd0265f | 2019-03-18 10:23:33 -0700 | [diff] [blame] | 92 | struct fscrypt_ctx *ctx; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 93 | unsigned long flags; |
| 94 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 95 | /* |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 96 | * First try getting a ctx from the free list so that we don't have to |
| 97 | * call into the slab allocator. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 98 | */ |
| 99 | spin_lock_irqsave(&fscrypt_ctx_lock, flags); |
| 100 | ctx = list_first_entry_or_null(&fscrypt_free_ctxs, |
| 101 | struct fscrypt_ctx, free_list); |
| 102 | if (ctx) |
| 103 | list_del(&ctx->free_list); |
| 104 | spin_unlock_irqrestore(&fscrypt_ctx_lock, flags); |
| 105 | if (!ctx) { |
Jaegeuk Kim | b32e4482 | 2016-04-11 15:51:57 -0700 | [diff] [blame] | 106 | ctx = kmem_cache_zalloc(fscrypt_ctx_cachep, gfp_flags); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 107 | if (!ctx) |
| 108 | return ERR_PTR(-ENOMEM); |
| 109 | ctx->flags |= FS_CTX_REQUIRES_FREE_ENCRYPT_FL; |
| 110 | } else { |
| 111 | ctx->flags &= ~FS_CTX_REQUIRES_FREE_ENCRYPT_FL; |
| 112 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 113 | return ctx; |
| 114 | } |
| 115 | EXPORT_SYMBOL(fscrypt_get_ctx); |
| 116 | |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 117 | struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags) |
| 118 | { |
| 119 | return mempool_alloc(fscrypt_bounce_page_pool, gfp_flags); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * fscrypt_free_bounce_page() - free a ciphertext bounce page |
| 124 | * |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 125 | * Free a bounce page that was allocated by fscrypt_encrypt_pagecache_blocks(), |
| 126 | * or by fscrypt_alloc_bounce_page() directly. |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 127 | */ |
| 128 | void fscrypt_free_bounce_page(struct page *bounce_page) |
| 129 | { |
| 130 | if (!bounce_page) |
| 131 | return; |
| 132 | set_page_private(bounce_page, (unsigned long)NULL); |
| 133 | ClearPagePrivate(bounce_page); |
| 134 | mempool_free(bounce_page, fscrypt_bounce_page_pool); |
| 135 | } |
| 136 | EXPORT_SYMBOL(fscrypt_free_bounce_page); |
| 137 | |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 138 | void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num, |
| 139 | const struct fscrypt_info *ci) |
| 140 | { |
| 141 | memset(iv, 0, ci->ci_mode->ivsize); |
| 142 | iv->lblk_num = cpu_to_le64(lblk_num); |
| 143 | |
| 144 | if (ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY) |
| 145 | memcpy(iv->nonce, ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE); |
| 146 | |
| 147 | if (ci->ci_essiv_tfm != NULL) |
| 148 | crypto_cipher_encrypt_one(ci->ci_essiv_tfm, iv->raw, iv->raw); |
| 149 | } |
| 150 | |
Eric Biggers | f47fcbb | 2019-05-20 09:29:41 -0700 | [diff] [blame] | 151 | /* Encrypt or decrypt a single filesystem block of file contents */ |
| 152 | int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw, |
| 153 | u64 lblk_num, struct page *src_page, |
| 154 | struct page *dest_page, unsigned int len, |
| 155 | unsigned int offs, gfp_t gfp_flags) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 156 | { |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 157 | union fscrypt_iv iv; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 158 | struct skcipher_request *req = NULL; |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 159 | DECLARE_CRYPTO_WAIT(wait); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 160 | struct scatterlist dst, src; |
| 161 | struct fscrypt_info *ci = inode->i_crypt_info; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 162 | struct crypto_skcipher *tfm = ci->ci_ctfm; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 163 | int res = 0; |
| 164 | |
Eric Biggers | eeacfdc | 2019-05-20 09:29:42 -0700 | [diff] [blame] | 165 | if (WARN_ON_ONCE(len <= 0)) |
| 166 | return -EINVAL; |
| 167 | if (WARN_ON_ONCE(len % FS_CRYPTO_BLOCK_SIZE != 0)) |
| 168 | return -EINVAL; |
David Gstir | 1400451 | 2016-12-06 23:53:55 +0100 | [diff] [blame] | 169 | |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 170 | fscrypt_generate_iv(&iv, lblk_num, ci); |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 171 | |
Jaegeuk Kim | b32e4482 | 2016-04-11 15:51:57 -0700 | [diff] [blame] | 172 | req = skcipher_request_alloc(tfm, gfp_flags); |
Eric Biggers | c90fd775 | 2018-04-30 15:51:38 -0700 | [diff] [blame] | 173 | if (!req) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 174 | return -ENOMEM; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 175 | |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 176 | skcipher_request_set_callback( |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 177 | req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 178 | crypto_req_done, &wait); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 179 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 180 | sg_init_table(&dst, 1); |
David Gstir | 1400451 | 2016-12-06 23:53:55 +0100 | [diff] [blame] | 181 | sg_set_page(&dst, dest_page, len, offs); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 182 | sg_init_table(&src, 1); |
David Gstir | 1400451 | 2016-12-06 23:53:55 +0100 | [diff] [blame] | 183 | sg_set_page(&src, src_page, len, offs); |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 184 | skcipher_request_set_crypt(req, &src, &dst, len, &iv); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 185 | if (rw == FS_DECRYPT) |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 186 | res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 187 | else |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 188 | res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 189 | skcipher_request_free(req); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 190 | if (res) { |
Eric Biggers | 544d08f | 2018-04-30 15:51:47 -0700 | [diff] [blame] | 191 | fscrypt_err(inode->i_sb, |
| 192 | "%scryption failed for inode %lu, block %llu: %d", |
| 193 | (rw == FS_DECRYPT ? "de" : "en"), |
| 194 | inode->i_ino, lblk_num, res); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 195 | return res; |
| 196 | } |
| 197 | return 0; |
| 198 | } |
| 199 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 200 | /** |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 201 | * fscrypt_encrypt_pagecache_blocks() - Encrypt filesystem blocks from a pagecache page |
| 202 | * @page: The locked pagecache page containing the block(s) to encrypt |
| 203 | * @len: Total size of the block(s) to encrypt. Must be a nonzero |
| 204 | * multiple of the filesystem's block size. |
| 205 | * @offs: Byte offset within @page of the first block to encrypt. Must be |
| 206 | * a multiple of the filesystem's block size. |
| 207 | * @gfp_flags: Memory allocation flags |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 208 | * |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 209 | * A new bounce page is allocated, and the specified block(s) are encrypted into |
| 210 | * it. In the bounce page, the ciphertext block(s) will be located at the same |
| 211 | * offsets at which the plaintext block(s) were located in the source page; any |
| 212 | * other parts of the bounce page will be left uninitialized. However, normally |
| 213 | * blocksize == PAGE_SIZE and the whole page is encrypted at once. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 214 | * |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 215 | * This is for use by the filesystem's ->writepages() method. |
| 216 | * |
| 217 | * Return: the new encrypted bounce page on success; an ERR_PTR() on failure |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 218 | */ |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 219 | struct page *fscrypt_encrypt_pagecache_blocks(struct page *page, |
| 220 | unsigned int len, |
| 221 | unsigned int offs, |
| 222 | gfp_t gfp_flags) |
David Gstir | 7821d4d | 2016-11-13 22:20:46 +0100 | [diff] [blame] | 223 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 224 | { |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 225 | const struct inode *inode = page->mapping->host; |
| 226 | const unsigned int blockbits = inode->i_blkbits; |
| 227 | const unsigned int blocksize = 1 << blockbits; |
Eric Biggers | 03569f2 | 2019-05-20 09:29:43 -0700 | [diff] [blame] | 228 | struct page *ciphertext_page; |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 229 | u64 lblk_num = ((u64)page->index << (PAGE_SHIFT - blockbits)) + |
| 230 | (offs >> blockbits); |
| 231 | unsigned int i; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 232 | int err; |
| 233 | |
Eric Biggers | eeacfdc | 2019-05-20 09:29:42 -0700 | [diff] [blame] | 234 | if (WARN_ON_ONCE(!PageLocked(page))) |
| 235 | return ERR_PTR(-EINVAL); |
David Gstir | bd7b829 | 2016-12-06 23:53:56 +0100 | [diff] [blame] | 236 | |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 237 | if (WARN_ON_ONCE(len <= 0 || !IS_ALIGNED(len | offs, blocksize))) |
| 238 | return ERR_PTR(-EINVAL); |
| 239 | |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 240 | ciphertext_page = fscrypt_alloc_bounce_page(gfp_flags); |
| 241 | if (!ciphertext_page) |
| 242 | return ERR_PTR(-ENOMEM); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 243 | |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 244 | for (i = offs; i < offs + len; i += blocksize, lblk_num++) { |
| 245 | err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk_num, |
| 246 | page, ciphertext_page, |
| 247 | blocksize, i, gfp_flags); |
| 248 | if (err) { |
| 249 | fscrypt_free_bounce_page(ciphertext_page); |
| 250 | return ERR_PTR(err); |
| 251 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 252 | } |
David Gstir | 9e53277 | 2016-12-06 23:53:54 +0100 | [diff] [blame] | 253 | SetPagePrivate(ciphertext_page); |
Eric Biggers | d2d0727 | 2019-05-20 09:29:39 -0700 | [diff] [blame] | 254 | set_page_private(ciphertext_page, (unsigned long)page); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 255 | return ciphertext_page; |
| 256 | } |
Eric Biggers | 53bc1d85 | 2019-05-20 09:29:44 -0700 | [diff] [blame] | 257 | EXPORT_SYMBOL(fscrypt_encrypt_pagecache_blocks); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 258 | |
| 259 | /** |
Eric Biggers | 03569f2 | 2019-05-20 09:29:43 -0700 | [diff] [blame] | 260 | * fscrypt_encrypt_block_inplace() - Encrypt a filesystem block in-place |
| 261 | * @inode: The inode to which this block belongs |
| 262 | * @page: The page containing the block to encrypt |
| 263 | * @len: Size of block to encrypt. Doesn't need to be a multiple of the |
| 264 | * fs block size, but must be a multiple of FS_CRYPTO_BLOCK_SIZE. |
| 265 | * @offs: Byte offset within @page at which the block to encrypt begins |
| 266 | * @lblk_num: Filesystem logical block number of the block, i.e. the 0-based |
| 267 | * number of the block within the file |
| 268 | * @gfp_flags: Memory allocation flags |
| 269 | * |
| 270 | * Encrypt a possibly-compressed filesystem block that is located in an |
| 271 | * arbitrary page, not necessarily in the original pagecache page. The @inode |
| 272 | * and @lblk_num must be specified, as they can't be determined from @page. |
| 273 | * |
| 274 | * Return: 0 on success; -errno on failure |
| 275 | */ |
| 276 | int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page, |
| 277 | unsigned int len, unsigned int offs, |
| 278 | u64 lblk_num, gfp_t gfp_flags) |
| 279 | { |
| 280 | return fscrypt_crypt_block(inode, FS_ENCRYPT, lblk_num, page, page, |
| 281 | len, offs, gfp_flags); |
| 282 | } |
| 283 | EXPORT_SYMBOL(fscrypt_encrypt_block_inplace); |
| 284 | |
| 285 | /** |
David Gstir | 7821d4d | 2016-11-13 22:20:46 +0100 | [diff] [blame] | 286 | * fscrypt_decrypt_page() - Decrypts a page in-place |
David Gstir | 1400451 | 2016-12-06 23:53:55 +0100 | [diff] [blame] | 287 | * @inode: The corresponding inode for the page to decrypt. |
Eric Biggers | 41adbcb | 2019-05-20 09:29:46 -0700 | [diff] [blame^] | 288 | * @page: The page to decrypt. Must be locked. |
David Gstir | 1400451 | 2016-12-06 23:53:55 +0100 | [diff] [blame] | 289 | * @len: Number of bytes in @page to be decrypted. |
| 290 | * @offs: Start of data in @page. |
| 291 | * @lblk_num: Logical block number. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 292 | * |
| 293 | * Decrypts page in-place using the ctx encryption context. |
| 294 | * |
| 295 | * Called from the read completion callback. |
| 296 | * |
| 297 | * Return: Zero on success, non-zero otherwise. |
| 298 | */ |
David Gstir | 0b93e1b | 2016-11-13 22:20:47 +0100 | [diff] [blame] | 299 | int fscrypt_decrypt_page(const struct inode *inode, struct page *page, |
David Gstir | 1400451 | 2016-12-06 23:53:55 +0100 | [diff] [blame] | 300 | unsigned int len, unsigned int offs, u64 lblk_num) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 301 | { |
Eric Biggers | 41adbcb | 2019-05-20 09:29:46 -0700 | [diff] [blame^] | 302 | if (WARN_ON_ONCE(!PageLocked(page))) |
Eric Biggers | eeacfdc | 2019-05-20 09:29:42 -0700 | [diff] [blame] | 303 | return -EINVAL; |
David Gstir | bd7b829 | 2016-12-06 23:53:56 +0100 | [diff] [blame] | 304 | |
Eric Biggers | f47fcbb | 2019-05-20 09:29:41 -0700 | [diff] [blame] | 305 | return fscrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page, page, |
| 306 | len, offs, GFP_NOFS); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 307 | } |
| 308 | EXPORT_SYMBOL(fscrypt_decrypt_page); |
| 309 | |
Eric Biggers | 41adbcb | 2019-05-20 09:29:46 -0700 | [diff] [blame^] | 310 | /** |
| 311 | * fscrypt_decrypt_block_inplace() - Decrypt a filesystem block in-place |
| 312 | * @inode: The inode to which this block belongs |
| 313 | * @page: The page containing the block to decrypt |
| 314 | * @len: Size of block to decrypt. Doesn't need to be a multiple of the |
| 315 | * fs block size, but must be a multiple of FS_CRYPTO_BLOCK_SIZE. |
| 316 | * @offs: Byte offset within @page at which the block to decrypt begins |
| 317 | * @lblk_num: Filesystem logical block number of the block, i.e. the 0-based |
| 318 | * number of the block within the file |
| 319 | * |
| 320 | * Decrypt a possibly-compressed filesystem block that is located in an |
| 321 | * arbitrary page, not necessarily in the original pagecache page. The @inode |
| 322 | * and @lblk_num must be specified, as they can't be determined from @page. |
| 323 | * |
| 324 | * Return: 0 on success; -errno on failure |
| 325 | */ |
| 326 | int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page, |
| 327 | unsigned int len, unsigned int offs, |
| 328 | u64 lblk_num) |
| 329 | { |
| 330 | return fscrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page, page, |
| 331 | len, offs, GFP_NOFS); |
| 332 | } |
| 333 | EXPORT_SYMBOL(fscrypt_decrypt_block_inplace); |
| 334 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 335 | /* |
Eric Biggers | 6cc2486 | 2019-03-20 11:39:09 -0700 | [diff] [blame] | 336 | * Validate dentries in encrypted directories to make sure we aren't potentially |
| 337 | * caching stale dentries after a key has been added. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 338 | */ |
| 339 | static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags) |
| 340 | { |
Jaegeuk Kim | d7d7535 | 2016-04-11 15:10:11 -0700 | [diff] [blame] | 341 | struct dentry *dir; |
Eric Biggers | 6cc2486 | 2019-03-20 11:39:09 -0700 | [diff] [blame] | 342 | int err; |
| 343 | int valid; |
| 344 | |
| 345 | /* |
| 346 | * Plaintext names are always valid, since fscrypt doesn't support |
| 347 | * reverting to ciphertext names without evicting the directory's inode |
| 348 | * -- which implies eviction of the dentries in the directory. |
| 349 | */ |
| 350 | if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME)) |
| 351 | return 1; |
| 352 | |
| 353 | /* |
| 354 | * Ciphertext name; valid if the directory's key is still unavailable. |
| 355 | * |
| 356 | * Although fscrypt forbids rename() on ciphertext names, we still must |
| 357 | * use dget_parent() here rather than use ->d_parent directly. That's |
| 358 | * because a corrupted fs image may contain directory hard links, which |
| 359 | * the VFS handles by moving the directory's dentry tree in the dcache |
| 360 | * each time ->lookup() finds the directory and it already has a dentry |
| 361 | * elsewhere. Thus ->d_parent can be changing, and we must safely grab |
| 362 | * a reference to some ->d_parent to prevent it from being freed. |
| 363 | */ |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 364 | |
Jaegeuk Kim | 03a8bb0 | 2016-04-12 16:05:36 -0700 | [diff] [blame] | 365 | if (flags & LOOKUP_RCU) |
| 366 | return -ECHILD; |
| 367 | |
Jaegeuk Kim | d7d7535 | 2016-04-11 15:10:11 -0700 | [diff] [blame] | 368 | dir = dget_parent(dentry); |
Eric Biggers | 6cc2486 | 2019-03-20 11:39:09 -0700 | [diff] [blame] | 369 | err = fscrypt_get_encryption_info(d_inode(dir)); |
| 370 | valid = !fscrypt_has_encryption_key(d_inode(dir)); |
Jaegeuk Kim | d7d7535 | 2016-04-11 15:10:11 -0700 | [diff] [blame] | 371 | dput(dir); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 372 | |
Eric Biggers | 6cc2486 | 2019-03-20 11:39:09 -0700 | [diff] [blame] | 373 | if (err < 0) |
| 374 | return err; |
| 375 | |
| 376 | return valid; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | const struct dentry_operations fscrypt_d_ops = { |
| 380 | .d_revalidate = fscrypt_d_revalidate, |
| 381 | }; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 382 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 383 | static void fscrypt_destroy(void) |
| 384 | { |
| 385 | struct fscrypt_ctx *pos, *n; |
| 386 | |
| 387 | list_for_each_entry_safe(pos, n, &fscrypt_free_ctxs, free_list) |
| 388 | kmem_cache_free(fscrypt_ctx_cachep, pos); |
| 389 | INIT_LIST_HEAD(&fscrypt_free_ctxs); |
| 390 | mempool_destroy(fscrypt_bounce_page_pool); |
| 391 | fscrypt_bounce_page_pool = NULL; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * fscrypt_initialize() - allocate major buffers for fs encryption. |
David Gstir | f32d7ac | 2016-12-06 23:53:57 +0100 | [diff] [blame] | 396 | * @cop_flags: fscrypt operations flags |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 397 | * |
| 398 | * We only call this when we start accessing encrypted files, since it |
| 399 | * results in memory getting allocated that wouldn't otherwise be used. |
| 400 | * |
| 401 | * Return: Zero on success, non-zero otherwise. |
| 402 | */ |
David Gstir | f32d7ac | 2016-12-06 23:53:57 +0100 | [diff] [blame] | 403 | int fscrypt_initialize(unsigned int cop_flags) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 404 | { |
| 405 | int i, res = -ENOMEM; |
| 406 | |
Eric Biggers | a0b3bc8 | 2017-10-29 06:30:19 -0400 | [diff] [blame] | 407 | /* No need to allocate a bounce page pool if this FS won't use it. */ |
| 408 | if (cop_flags & FS_CFLG_OWN_PAGES) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 409 | return 0; |
| 410 | |
| 411 | mutex_lock(&fscrypt_init_mutex); |
| 412 | if (fscrypt_bounce_page_pool) |
| 413 | goto already_initialized; |
| 414 | |
| 415 | for (i = 0; i < num_prealloc_crypto_ctxs; i++) { |
| 416 | struct fscrypt_ctx *ctx; |
| 417 | |
| 418 | ctx = kmem_cache_zalloc(fscrypt_ctx_cachep, GFP_NOFS); |
| 419 | if (!ctx) |
| 420 | goto fail; |
| 421 | list_add(&ctx->free_list, &fscrypt_free_ctxs); |
| 422 | } |
| 423 | |
| 424 | fscrypt_bounce_page_pool = |
| 425 | mempool_create_page_pool(num_prealloc_crypto_pages, 0); |
| 426 | if (!fscrypt_bounce_page_pool) |
| 427 | goto fail; |
| 428 | |
| 429 | already_initialized: |
| 430 | mutex_unlock(&fscrypt_init_mutex); |
| 431 | return 0; |
| 432 | fail: |
| 433 | fscrypt_destroy(); |
| 434 | mutex_unlock(&fscrypt_init_mutex); |
| 435 | return res; |
| 436 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 437 | |
Eric Biggers | 544d08f | 2018-04-30 15:51:47 -0700 | [diff] [blame] | 438 | void fscrypt_msg(struct super_block *sb, const char *level, |
| 439 | const char *fmt, ...) |
| 440 | { |
| 441 | static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, |
| 442 | DEFAULT_RATELIMIT_BURST); |
| 443 | struct va_format vaf; |
| 444 | va_list args; |
| 445 | |
| 446 | if (!__ratelimit(&rs)) |
| 447 | return; |
| 448 | |
| 449 | va_start(args, fmt); |
| 450 | vaf.fmt = fmt; |
| 451 | vaf.va = &args; |
| 452 | if (sb) |
| 453 | printk("%sfscrypt (%s): %pV\n", level, sb->s_id, &vaf); |
| 454 | else |
| 455 | printk("%sfscrypt: %pV\n", level, &vaf); |
| 456 | va_end(args); |
| 457 | } |
| 458 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 459 | /** |
| 460 | * fscrypt_init() - Set up for fs encryption. |
| 461 | */ |
| 462 | static int __init fscrypt_init(void) |
| 463 | { |
Eric Biggers | 36dd26e | 2018-04-20 16:30:02 -0700 | [diff] [blame] | 464 | /* |
| 465 | * Use an unbound workqueue to allow bios to be decrypted in parallel |
| 466 | * even when they happen to complete on the same CPU. This sacrifices |
| 467 | * locality, but it's worthwhile since decryption is CPU-intensive. |
| 468 | * |
| 469 | * Also use a high-priority workqueue to prioritize decryption work, |
| 470 | * which blocks reads from completing, over regular application tasks. |
| 471 | */ |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 472 | fscrypt_read_workqueue = alloc_workqueue("fscrypt_read_queue", |
Eric Biggers | 36dd26e | 2018-04-20 16:30:02 -0700 | [diff] [blame] | 473 | WQ_UNBOUND | WQ_HIGHPRI, |
| 474 | num_online_cpus()); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 475 | if (!fscrypt_read_workqueue) |
| 476 | goto fail; |
| 477 | |
| 478 | fscrypt_ctx_cachep = KMEM_CACHE(fscrypt_ctx, SLAB_RECLAIM_ACCOUNT); |
| 479 | if (!fscrypt_ctx_cachep) |
| 480 | goto fail_free_queue; |
| 481 | |
| 482 | fscrypt_info_cachep = KMEM_CACHE(fscrypt_info, SLAB_RECLAIM_ACCOUNT); |
| 483 | if (!fscrypt_info_cachep) |
| 484 | goto fail_free_ctx; |
| 485 | |
| 486 | return 0; |
| 487 | |
| 488 | fail_free_ctx: |
| 489 | kmem_cache_destroy(fscrypt_ctx_cachep); |
| 490 | fail_free_queue: |
| 491 | destroy_workqueue(fscrypt_read_workqueue); |
| 492 | fail: |
| 493 | return -ENOMEM; |
| 494 | } |
| 495 | module_init(fscrypt_init) |
| 496 | |
| 497 | /** |
| 498 | * fscrypt_exit() - Shutdown the fs encryption system |
| 499 | */ |
| 500 | static void __exit fscrypt_exit(void) |
| 501 | { |
| 502 | fscrypt_destroy(); |
| 503 | |
| 504 | if (fscrypt_read_workqueue) |
| 505 | destroy_workqueue(fscrypt_read_workqueue); |
| 506 | kmem_cache_destroy(fscrypt_ctx_cachep); |
| 507 | kmem_cache_destroy(fscrypt_info_cachep); |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 508 | |
| 509 | fscrypt_essiv_cleanup(); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 510 | } |
| 511 | module_exit(fscrypt_exit); |
| 512 | |
| 513 | MODULE_LICENSE("GPL"); |