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