ext4: Optimize ext4 DIO overwrites
[linux/fpc-iii.git] / fs / crypto / crypto.c
blob3719efa546c6579b38dbd6fcf2a8b49b5ceb32d5
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * This contains encryption functions for per-file encryption.
5 * Copyright (C) 2015, Google, Inc.
6 * Copyright (C) 2015, Motorola Mobility
8 * Written by Michael Halcrow, 2014.
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.
17 * This has not yet undergone a rigorous security audit.
19 * The usage of AES-XTS should conform to recommendations in NIST
20 * Special Publication 800-38E and IEEE P1619/D16.
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>
28 #include <linux/dcache.h>
29 #include <linux/namei.h>
30 #include <crypto/skcipher.h>
31 #include "fscrypt_private.h"
33 static unsigned int num_prealloc_crypto_pages = 32;
35 module_param(num_prealloc_crypto_pages, uint, 0444);
36 MODULE_PARM_DESC(num_prealloc_crypto_pages,
37 "Number of crypto pages to preallocate");
39 static mempool_t *fscrypt_bounce_page_pool = NULL;
41 static struct workqueue_struct *fscrypt_read_workqueue;
42 static DEFINE_MUTEX(fscrypt_init_mutex);
44 struct kmem_cache *fscrypt_info_cachep;
46 void fscrypt_enqueue_decrypt_work(struct work_struct *work)
48 queue_work(fscrypt_read_workqueue, work);
50 EXPORT_SYMBOL(fscrypt_enqueue_decrypt_work);
52 struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags)
54 return mempool_alloc(fscrypt_bounce_page_pool, gfp_flags);
57 /**
58 * fscrypt_free_bounce_page() - free a ciphertext bounce page
60 * Free a bounce page that was allocated by fscrypt_encrypt_pagecache_blocks(),
61 * or by fscrypt_alloc_bounce_page() directly.
63 void fscrypt_free_bounce_page(struct page *bounce_page)
65 if (!bounce_page)
66 return;
67 set_page_private(bounce_page, (unsigned long)NULL);
68 ClearPagePrivate(bounce_page);
69 mempool_free(bounce_page, fscrypt_bounce_page_pool);
71 EXPORT_SYMBOL(fscrypt_free_bounce_page);
73 void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
74 const struct fscrypt_info *ci)
76 u8 flags = fscrypt_policy_flags(&ci->ci_policy);
78 memset(iv, 0, ci->ci_mode->ivsize);
80 if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) {
81 WARN_ON_ONCE((u32)lblk_num != lblk_num);
82 lblk_num |= (u64)ci->ci_inode->i_ino << 32;
83 } else if (flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) {
84 memcpy(iv->nonce, ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE);
86 iv->lblk_num = cpu_to_le64(lblk_num);
89 /* Encrypt or decrypt a single filesystem block of file contents */
90 int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw,
91 u64 lblk_num, struct page *src_page,
92 struct page *dest_page, unsigned int len,
93 unsigned int offs, gfp_t gfp_flags)
95 union fscrypt_iv iv;
96 struct skcipher_request *req = NULL;
97 DECLARE_CRYPTO_WAIT(wait);
98 struct scatterlist dst, src;
99 struct fscrypt_info *ci = inode->i_crypt_info;
100 struct crypto_skcipher *tfm = ci->ci_ctfm;
101 int res = 0;
103 if (WARN_ON_ONCE(len <= 0))
104 return -EINVAL;
105 if (WARN_ON_ONCE(len % FS_CRYPTO_BLOCK_SIZE != 0))
106 return -EINVAL;
108 fscrypt_generate_iv(&iv, lblk_num, ci);
110 req = skcipher_request_alloc(tfm, gfp_flags);
111 if (!req)
112 return -ENOMEM;
114 skcipher_request_set_callback(
115 req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
116 crypto_req_done, &wait);
118 sg_init_table(&dst, 1);
119 sg_set_page(&dst, dest_page, len, offs);
120 sg_init_table(&src, 1);
121 sg_set_page(&src, src_page, len, offs);
122 skcipher_request_set_crypt(req, &src, &dst, len, &iv);
123 if (rw == FS_DECRYPT)
124 res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
125 else
126 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
127 skcipher_request_free(req);
128 if (res) {
129 fscrypt_err(inode, "%scryption failed for block %llu: %d",
130 (rw == FS_DECRYPT ? "De" : "En"), lblk_num, res);
131 return res;
133 return 0;
137 * fscrypt_encrypt_pagecache_blocks() - Encrypt filesystem blocks from a pagecache page
138 * @page: The locked pagecache page containing the block(s) to encrypt
139 * @len: Total size of the block(s) to encrypt. Must be a nonzero
140 * multiple of the filesystem's block size.
141 * @offs: Byte offset within @page of the first block to encrypt. Must be
142 * a multiple of the filesystem's block size.
143 * @gfp_flags: Memory allocation flags
145 * A new bounce page is allocated, and the specified block(s) are encrypted into
146 * it. In the bounce page, the ciphertext block(s) will be located at the same
147 * offsets at which the plaintext block(s) were located in the source page; any
148 * other parts of the bounce page will be left uninitialized. However, normally
149 * blocksize == PAGE_SIZE and the whole page is encrypted at once.
151 * This is for use by the filesystem's ->writepages() method.
153 * Return: the new encrypted bounce page on success; an ERR_PTR() on failure
155 struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
156 unsigned int len,
157 unsigned int offs,
158 gfp_t gfp_flags)
161 const struct inode *inode = page->mapping->host;
162 const unsigned int blockbits = inode->i_blkbits;
163 const unsigned int blocksize = 1 << blockbits;
164 struct page *ciphertext_page;
165 u64 lblk_num = ((u64)page->index << (PAGE_SHIFT - blockbits)) +
166 (offs >> blockbits);
167 unsigned int i;
168 int err;
170 if (WARN_ON_ONCE(!PageLocked(page)))
171 return ERR_PTR(-EINVAL);
173 if (WARN_ON_ONCE(len <= 0 || !IS_ALIGNED(len | offs, blocksize)))
174 return ERR_PTR(-EINVAL);
176 ciphertext_page = fscrypt_alloc_bounce_page(gfp_flags);
177 if (!ciphertext_page)
178 return ERR_PTR(-ENOMEM);
180 for (i = offs; i < offs + len; i += blocksize, lblk_num++) {
181 err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk_num,
182 page, ciphertext_page,
183 blocksize, i, gfp_flags);
184 if (err) {
185 fscrypt_free_bounce_page(ciphertext_page);
186 return ERR_PTR(err);
189 SetPagePrivate(ciphertext_page);
190 set_page_private(ciphertext_page, (unsigned long)page);
191 return ciphertext_page;
193 EXPORT_SYMBOL(fscrypt_encrypt_pagecache_blocks);
196 * fscrypt_encrypt_block_inplace() - Encrypt a filesystem block in-place
197 * @inode: The inode to which this block belongs
198 * @page: The page containing the block to encrypt
199 * @len: Size of block to encrypt. Doesn't need to be a multiple of the
200 * fs block size, but must be a multiple of FS_CRYPTO_BLOCK_SIZE.
201 * @offs: Byte offset within @page at which the block to encrypt begins
202 * @lblk_num: Filesystem logical block number of the block, i.e. the 0-based
203 * number of the block within the file
204 * @gfp_flags: Memory allocation flags
206 * Encrypt a possibly-compressed filesystem block that is located in an
207 * arbitrary page, not necessarily in the original pagecache page. The @inode
208 * and @lblk_num must be specified, as they can't be determined from @page.
210 * Return: 0 on success; -errno on failure
212 int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
213 unsigned int len, unsigned int offs,
214 u64 lblk_num, gfp_t gfp_flags)
216 return fscrypt_crypt_block(inode, FS_ENCRYPT, lblk_num, page, page,
217 len, offs, gfp_flags);
219 EXPORT_SYMBOL(fscrypt_encrypt_block_inplace);
222 * fscrypt_decrypt_pagecache_blocks() - Decrypt filesystem blocks in a pagecache page
223 * @page: The locked pagecache page containing the block(s) to decrypt
224 * @len: Total size of the block(s) to decrypt. Must be a nonzero
225 * multiple of the filesystem's block size.
226 * @offs: Byte offset within @page of the first block to decrypt. Must be
227 * a multiple of the filesystem's block size.
229 * The specified block(s) are decrypted in-place within the pagecache page,
230 * which must still be locked and not uptodate. Normally, blocksize ==
231 * PAGE_SIZE and the whole page is decrypted at once.
233 * This is for use by the filesystem's ->readpages() method.
235 * Return: 0 on success; -errno on failure
237 int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
238 unsigned int offs)
240 const struct inode *inode = page->mapping->host;
241 const unsigned int blockbits = inode->i_blkbits;
242 const unsigned int blocksize = 1 << blockbits;
243 u64 lblk_num = ((u64)page->index << (PAGE_SHIFT - blockbits)) +
244 (offs >> blockbits);
245 unsigned int i;
246 int err;
248 if (WARN_ON_ONCE(!PageLocked(page)))
249 return -EINVAL;
251 if (WARN_ON_ONCE(len <= 0 || !IS_ALIGNED(len | offs, blocksize)))
252 return -EINVAL;
254 for (i = offs; i < offs + len; i += blocksize, lblk_num++) {
255 err = fscrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page,
256 page, blocksize, i, GFP_NOFS);
257 if (err)
258 return err;
260 return 0;
262 EXPORT_SYMBOL(fscrypt_decrypt_pagecache_blocks);
265 * fscrypt_decrypt_block_inplace() - Decrypt a filesystem block in-place
266 * @inode: The inode to which this block belongs
267 * @page: The page containing the block to decrypt
268 * @len: Size of block to decrypt. Doesn't need to be a multiple of the
269 * fs block size, but must be a multiple of FS_CRYPTO_BLOCK_SIZE.
270 * @offs: Byte offset within @page at which the block to decrypt begins
271 * @lblk_num: Filesystem logical block number of the block, i.e. the 0-based
272 * number of the block within the file
274 * Decrypt a possibly-compressed filesystem block that is located in an
275 * arbitrary page, not necessarily in the original pagecache page. The @inode
276 * and @lblk_num must be specified, as they can't be determined from @page.
278 * Return: 0 on success; -errno on failure
280 int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
281 unsigned int len, unsigned int offs,
282 u64 lblk_num)
284 return fscrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page, page,
285 len, offs, GFP_NOFS);
287 EXPORT_SYMBOL(fscrypt_decrypt_block_inplace);
290 * Validate dentries in encrypted directories to make sure we aren't potentially
291 * caching stale dentries after a key has been added.
293 static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)
295 struct dentry *dir;
296 int err;
297 int valid;
300 * Plaintext names are always valid, since fscrypt doesn't support
301 * reverting to ciphertext names without evicting the directory's inode
302 * -- which implies eviction of the dentries in the directory.
304 if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME))
305 return 1;
308 * Ciphertext name; valid if the directory's key is still unavailable.
310 * Although fscrypt forbids rename() on ciphertext names, we still must
311 * use dget_parent() here rather than use ->d_parent directly. That's
312 * because a corrupted fs image may contain directory hard links, which
313 * the VFS handles by moving the directory's dentry tree in the dcache
314 * each time ->lookup() finds the directory and it already has a dentry
315 * elsewhere. Thus ->d_parent can be changing, and we must safely grab
316 * a reference to some ->d_parent to prevent it from being freed.
319 if (flags & LOOKUP_RCU)
320 return -ECHILD;
322 dir = dget_parent(dentry);
323 err = fscrypt_get_encryption_info(d_inode(dir));
324 valid = !fscrypt_has_encryption_key(d_inode(dir));
325 dput(dir);
327 if (err < 0)
328 return err;
330 return valid;
333 const struct dentry_operations fscrypt_d_ops = {
334 .d_revalidate = fscrypt_d_revalidate,
338 * fscrypt_initialize() - allocate major buffers for fs encryption.
339 * @cop_flags: fscrypt operations flags
341 * We only call this when we start accessing encrypted files, since it
342 * results in memory getting allocated that wouldn't otherwise be used.
344 * Return: 0 on success; -errno on failure
346 int fscrypt_initialize(unsigned int cop_flags)
348 int err = 0;
350 /* No need to allocate a bounce page pool if this FS won't use it. */
351 if (cop_flags & FS_CFLG_OWN_PAGES)
352 return 0;
354 mutex_lock(&fscrypt_init_mutex);
355 if (fscrypt_bounce_page_pool)
356 goto out_unlock;
358 err = -ENOMEM;
359 fscrypt_bounce_page_pool =
360 mempool_create_page_pool(num_prealloc_crypto_pages, 0);
361 if (!fscrypt_bounce_page_pool)
362 goto out_unlock;
364 err = 0;
365 out_unlock:
366 mutex_unlock(&fscrypt_init_mutex);
367 return err;
370 void fscrypt_msg(const struct inode *inode, const char *level,
371 const char *fmt, ...)
373 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
374 DEFAULT_RATELIMIT_BURST);
375 struct va_format vaf;
376 va_list args;
378 if (!__ratelimit(&rs))
379 return;
381 va_start(args, fmt);
382 vaf.fmt = fmt;
383 vaf.va = &args;
384 if (inode)
385 printk("%sfscrypt (%s, inode %lu): %pV\n",
386 level, inode->i_sb->s_id, inode->i_ino, &vaf);
387 else
388 printk("%sfscrypt: %pV\n", level, &vaf);
389 va_end(args);
393 * fscrypt_init() - Set up for fs encryption.
395 static int __init fscrypt_init(void)
397 int err = -ENOMEM;
400 * Use an unbound workqueue to allow bios to be decrypted in parallel
401 * even when they happen to complete on the same CPU. This sacrifices
402 * locality, but it's worthwhile since decryption is CPU-intensive.
404 * Also use a high-priority workqueue to prioritize decryption work,
405 * which blocks reads from completing, over regular application tasks.
407 fscrypt_read_workqueue = alloc_workqueue("fscrypt_read_queue",
408 WQ_UNBOUND | WQ_HIGHPRI,
409 num_online_cpus());
410 if (!fscrypt_read_workqueue)
411 goto fail;
413 fscrypt_info_cachep = KMEM_CACHE(fscrypt_info, SLAB_RECLAIM_ACCOUNT);
414 if (!fscrypt_info_cachep)
415 goto fail_free_queue;
417 err = fscrypt_init_keyring();
418 if (err)
419 goto fail_free_info;
421 return 0;
423 fail_free_info:
424 kmem_cache_destroy(fscrypt_info_cachep);
425 fail_free_queue:
426 destroy_workqueue(fscrypt_read_workqueue);
427 fail:
428 return err;
430 late_initcall(fscrypt_init)