Whamcloud - gitweb
LU-12275 sec: add llcrypt as file encryption library
[fs/lustre-release.git] / libcfs / libcfs / crypto / crypto.c
1 // SPDX-License-Identifier: GPL-2.0-only
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 llcrypt_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 /*
23  * Linux commit 219d54332a09
24  * tags/v5.4
25  */
26
27 #include <linux/pagemap.h>
28 #include <linux/mempool.h>
29 #include <linux/module.h>
30 #include <linux/scatterlist.h>
31 #include <linux/ratelimit.h>
32 #include <linux/dcache.h>
33 #include <linux/namei.h>
34 #include <crypto/aes.h>
35 #include <crypto/skcipher.h>
36 #include "llcrypt_private.h"
37
38 static unsigned int num_prealloc_crypto_pages = 32;
39 static unsigned int num_prealloc_crypto_ctxs = 128;
40
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
48 static mempool_t *llcrypt_bounce_page_pool = NULL;
49
50 static LIST_HEAD(llcrypt_free_ctxs);
51 static DEFINE_SPINLOCK(llcrypt_ctx_lock);
52
53 static struct workqueue_struct *llcrypt_read_workqueue;
54 static DEFINE_MUTEX(llcrypt_init_mutex);
55
56 static struct kmem_cache *llcrypt_ctx_cachep;
57 struct kmem_cache *llcrypt_info_cachep;
58
59 void llcrypt_enqueue_decrypt_work(struct work_struct *work)
60 {
61         queue_work(llcrypt_read_workqueue, work);
62 }
63 EXPORT_SYMBOL(llcrypt_enqueue_decrypt_work);
64
65 /**
66  * llcrypt_release_ctx() - Release a decryption context
67  * @ctx: The decryption context to release.
68  *
69  * If the decryption context was allocated from the pre-allocated pool, return
70  * it to that pool.  Else, free it.
71  */
72 void llcrypt_release_ctx(struct llcrypt_ctx *ctx)
73 {
74         unsigned long flags;
75
76         if (ctx->flags & FS_CTX_REQUIRES_FREE_ENCRYPT_FL) {
77                 kmem_cache_free(llcrypt_ctx_cachep, ctx);
78         } else {
79                 spin_lock_irqsave(&llcrypt_ctx_lock, flags);
80                 list_add(&ctx->free_list, &llcrypt_free_ctxs);
81                 spin_unlock_irqrestore(&llcrypt_ctx_lock, flags);
82         }
83 }
84 EXPORT_SYMBOL(llcrypt_release_ctx);
85
86 /**
87  * llcrypt_get_ctx() - Get a decryption context
88  * @gfp_flags:   The gfp flag for memory allocation
89  *
90  * Allocate and initialize a decryption context.
91  *
92  * Return: A new decryption context on success; an ERR_PTR() otherwise.
93  */
94 struct llcrypt_ctx *llcrypt_get_ctx(gfp_t gfp_flags)
95 {
96         struct llcrypt_ctx *ctx;
97         unsigned long flags;
98
99         /*
100          * First try getting a ctx from the free list so that we don't have to
101          * call into the slab allocator.
102          */
103         spin_lock_irqsave(&llcrypt_ctx_lock, flags);
104         ctx = list_first_entry_or_null(&llcrypt_free_ctxs,
105                                         struct llcrypt_ctx, free_list);
106         if (ctx)
107                 list_del(&ctx->free_list);
108         spin_unlock_irqrestore(&llcrypt_ctx_lock, flags);
109         if (!ctx) {
110                 ctx = kmem_cache_zalloc(llcrypt_ctx_cachep, gfp_flags);
111                 if (!ctx)
112                         return ERR_PTR(-ENOMEM);
113                 ctx->flags |= FS_CTX_REQUIRES_FREE_ENCRYPT_FL;
114         } else {
115                 ctx->flags &= ~FS_CTX_REQUIRES_FREE_ENCRYPT_FL;
116         }
117         return ctx;
118 }
119 EXPORT_SYMBOL(llcrypt_get_ctx);
120
121 struct page *llcrypt_alloc_bounce_page(gfp_t gfp_flags)
122 {
123         return mempool_alloc(llcrypt_bounce_page_pool, gfp_flags);
124 }
125
126 /**
127  * llcrypt_free_bounce_page() - free a ciphertext bounce page
128  *
129  * Free a bounce page that was allocated by llcrypt_encrypt_pagecache_blocks(),
130  * or by llcrypt_alloc_bounce_page() directly.
131  */
132 void llcrypt_free_bounce_page(struct page *bounce_page)
133 {
134         if (!bounce_page)
135                 return;
136         set_page_private(bounce_page, (unsigned long)NULL);
137         ClearPagePrivate(bounce_page);
138         mempool_free(bounce_page, llcrypt_bounce_page_pool);
139 }
140 EXPORT_SYMBOL(llcrypt_free_bounce_page);
141
142 void llcrypt_generate_iv(union llcrypt_iv *iv, u64 lblk_num,
143                          const struct llcrypt_info *ci)
144 {
145         memset(iv, 0, ci->ci_mode->ivsize);
146         iv->lblk_num = cpu_to_le64(lblk_num);
147
148         if (llcrypt_is_direct_key_policy(&ci->ci_policy))
149                 memcpy(iv->nonce, ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE);
150
151         if (ci->ci_essiv_tfm != NULL)
152                 crypto_cipher_encrypt_one(ci->ci_essiv_tfm, iv->raw, iv->raw);
153 }
154
155 /* Encrypt or decrypt a single filesystem block of file contents */
156 int llcrypt_crypt_block(const struct inode *inode, llcrypt_direction_t rw,
157                         u64 lblk_num, struct page *src_page,
158                         struct page *dest_page, unsigned int len,
159                         unsigned int offs, gfp_t gfp_flags)
160 {
161         union llcrypt_iv iv;
162         struct skcipher_request *req = NULL;
163         DECLARE_CRYPTO_WAIT(wait);
164         struct scatterlist dst, src;
165         struct llcrypt_info *ci = llcrypt_info(inode);
166         struct crypto_skcipher *tfm = ci->ci_ctfm;
167         int res = 0;
168
169         if (WARN_ON_ONCE(len <= 0))
170                 return -EINVAL;
171         if (WARN_ON_ONCE(len % LL_CRYPTO_BLOCK_SIZE != 0))
172                 return -EINVAL;
173
174         llcrypt_generate_iv(&iv, lblk_num, ci);
175
176         req = skcipher_request_alloc(tfm, gfp_flags);
177         if (!req)
178                 return -ENOMEM;
179
180         skcipher_request_set_callback(
181                 req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
182                 crypto_req_done, &wait);
183
184         sg_init_table(&dst, 1);
185         sg_set_page(&dst, dest_page, len, offs);
186         sg_init_table(&src, 1);
187         sg_set_page(&src, src_page, len, offs);
188         skcipher_request_set_crypt(req, &src, &dst, len, &iv);
189         if (rw == FS_DECRYPT)
190                 res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
191         else
192                 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
193         skcipher_request_free(req);
194         if (res) {
195                 llcrypt_err(inode, "%scryption failed for block %llu: %d",
196                             (rw == FS_DECRYPT ? "De" : "En"), lblk_num, res);
197                 return res;
198         }
199         return 0;
200 }
201
202 /**
203  * llcrypt_encrypt_pagecache_blocks() - Encrypt filesystem blocks from a pagecache page
204  * @page:      The locked pagecache page containing the block(s) to encrypt
205  * @len:       Total size of the block(s) to encrypt.  Must be a nonzero
206  *              multiple of the filesystem's block size.
207  * @offs:      Byte offset within @page of the first block to encrypt.  Must be
208  *              a multiple of the filesystem's block size.
209  * @gfp_flags: Memory allocation flags
210  *
211  * A new bounce page is allocated, and the specified block(s) are encrypted into
212  * it.  In the bounce page, the ciphertext block(s) will be located at the same
213  * offsets at which the plaintext block(s) were located in the source page; any
214  * other parts of the bounce page will be left uninitialized.  However, normally
215  * blocksize == PAGE_SIZE and the whole page is encrypted at once.
216  *
217  * This is for use by the filesystem's ->writepages() method.
218  *
219  * Return: the new encrypted bounce page on success; an ERR_PTR() on failure
220  */
221 struct page *llcrypt_encrypt_pagecache_blocks(struct page *page,
222                                               unsigned int len,
223                                               unsigned int offs,
224                                               gfp_t gfp_flags)
225
226 {
227         const struct inode *inode = page->mapping->host;
228         const unsigned int blockbits = inode->i_blkbits;
229         const unsigned int blocksize = 1 << blockbits;
230         struct page *ciphertext_page;
231         u64 lblk_num = ((u64)page->index << (PAGE_SHIFT - blockbits)) +
232                        (offs >> blockbits);
233         unsigned int i;
234         int err;
235
236         if (WARN_ON_ONCE(!PageLocked(page)))
237                 return ERR_PTR(-EINVAL);
238
239         if (WARN_ON_ONCE(len <= 0 || !IS_ALIGNED(len | offs, blocksize)))
240                 return ERR_PTR(-EINVAL);
241
242         ciphertext_page = llcrypt_alloc_bounce_page(gfp_flags);
243         if (!ciphertext_page)
244                 return ERR_PTR(-ENOMEM);
245
246         for (i = offs; i < offs + len; i += blocksize, lblk_num++) {
247                 err = llcrypt_crypt_block(inode, FS_ENCRYPT, lblk_num,
248                                           page, ciphertext_page,
249                                           blocksize, i, gfp_flags);
250                 if (err) {
251                         llcrypt_free_bounce_page(ciphertext_page);
252                         return ERR_PTR(err);
253                 }
254         }
255         SetPagePrivate(ciphertext_page);
256         set_page_private(ciphertext_page, (unsigned long)page);
257         return ciphertext_page;
258 }
259 EXPORT_SYMBOL(llcrypt_encrypt_pagecache_blocks);
260
261 /**
262  * llcrypt_encrypt_block_inplace() - Encrypt a filesystem block in-place
263  * @inode:     The inode to which this block belongs
264  * @page:      The page containing the block to encrypt
265  * @len:       Size of block to encrypt.  Doesn't need to be a multiple of the
266  *              fs block size, but must be a multiple of LL_CRYPTO_BLOCK_SIZE.
267  * @offs:      Byte offset within @page at which the block to encrypt begins
268  * @lblk_num:  Filesystem logical block number of the block, i.e. the 0-based
269  *              number of the block within the file
270  * @gfp_flags: Memory allocation flags
271  *
272  * Encrypt a possibly-compressed filesystem block that is located in an
273  * arbitrary page, not necessarily in the original pagecache page.  The @inode
274  * and @lblk_num must be specified, as they can't be determined from @page.
275  *
276  * Return: 0 on success; -errno on failure
277  */
278 int llcrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
279                                   unsigned int len, unsigned int offs,
280                                   u64 lblk_num, gfp_t gfp_flags)
281 {
282         return llcrypt_crypt_block(inode, FS_ENCRYPT, lblk_num, page, page,
283                                    len, offs, gfp_flags);
284 }
285 EXPORT_SYMBOL(llcrypt_encrypt_block_inplace);
286
287 /**
288  * llcrypt_decrypt_pagecache_blocks() - Decrypt filesystem blocks in a pagecache page
289  * @page:      The locked pagecache page containing the block(s) to decrypt
290  * @len:       Total size of the block(s) to decrypt.  Must be a nonzero
291  *              multiple of the filesystem's block size.
292  * @offs:      Byte offset within @page of the first block to decrypt.  Must be
293  *              a multiple of the filesystem's block size.
294  *
295  * The specified block(s) are decrypted in-place within the pagecache page,
296  * which must still be locked and not uptodate.  Normally, blocksize ==
297  * PAGE_SIZE and the whole page is decrypted at once.
298  *
299  * This is for use by the filesystem's ->readpages() method.
300  *
301  * Return: 0 on success; -errno on failure
302  */
303 int llcrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
304                                      unsigned int offs)
305 {
306         const struct inode *inode = page->mapping->host;
307         const unsigned int blockbits = inode->i_blkbits;
308         const unsigned int blocksize = 1 << blockbits;
309         u64 lblk_num = ((u64)page->index << (PAGE_SHIFT - blockbits)) +
310                        (offs >> blockbits);
311         unsigned int i;
312         int err;
313
314         if (WARN_ON_ONCE(!PageLocked(page)))
315                 return -EINVAL;
316
317         if (WARN_ON_ONCE(len <= 0 || !IS_ALIGNED(len | offs, blocksize)))
318                 return -EINVAL;
319
320         for (i = offs; i < offs + len; i += blocksize, lblk_num++) {
321                 err = llcrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page,
322                                           page, blocksize, i, GFP_NOFS);
323                 if (err)
324                         return err;
325         }
326         return 0;
327 }
328 EXPORT_SYMBOL(llcrypt_decrypt_pagecache_blocks);
329
330 /**
331  * llcrypt_decrypt_block_inplace() - Decrypt a filesystem block in-place
332  * @inode:     The inode to which this block belongs
333  * @page:      The page containing the block to decrypt
334  * @len:       Size of block to decrypt.  Doesn't need to be a multiple of the
335  *              fs block size, but must be a multiple of LL_CRYPTO_BLOCK_SIZE.
336  * @offs:      Byte offset within @page at which the block to decrypt begins
337  * @lblk_num:  Filesystem logical block number of the block, i.e. the 0-based
338  *              number of the block within the file
339  *
340  * Decrypt a possibly-compressed filesystem block that is located in an
341  * arbitrary page, not necessarily in the original pagecache page.  The @inode
342  * and @lblk_num must be specified, as they can't be determined from @page.
343  *
344  * Return: 0 on success; -errno on failure
345  */
346 int llcrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
347                                   unsigned int len, unsigned int offs,
348                                   u64 lblk_num)
349 {
350         return llcrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page, page,
351                                    len, offs, GFP_NOFS);
352 }
353 EXPORT_SYMBOL(llcrypt_decrypt_block_inplace);
354
355 /*
356  * Validate dentries in encrypted directories to make sure we aren't potentially
357  * caching stale dentries after a key has been added.
358  */
359 static int llcrypt_d_revalidate(struct dentry *dentry, unsigned int flags)
360 {
361         struct dentry *dir;
362         int err;
363         int valid;
364
365         /*
366          * Plaintext names are always valid, since llcrypt doesn't support
367          * reverting to ciphertext names without evicting the directory's inode
368          * -- which implies eviction of the dentries in the directory.
369          */
370         if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME))
371                 return 1;
372
373         /*
374          * Ciphertext name; valid if the directory's key is still unavailable.
375          *
376          * Although llcrypt forbids rename() on ciphertext names, we still must
377          * use dget_parent() here rather than use ->d_parent directly.  That's
378          * because a corrupted fs image may contain directory hard links, which
379          * the VFS handles by moving the directory's dentry tree in the dcache
380          * each time ->lookup() finds the directory and it already has a dentry
381          * elsewhere.  Thus ->d_parent can be changing, and we must safely grab
382          * a reference to some ->d_parent to prevent it from being freed.
383          */
384
385         if (flags & LOOKUP_RCU)
386                 return -ECHILD;
387
388         dir = dget_parent(dentry);
389         err = llcrypt_get_encryption_info(d_inode(dir));
390         valid = !llcrypt_has_encryption_key(d_inode(dir));
391         dput(dir);
392
393         if (err < 0)
394                 return err;
395
396         return valid;
397 }
398
399 const struct dentry_operations llcrypt_d_ops = {
400         .d_revalidate = llcrypt_d_revalidate,
401 };
402
403 static void llcrypt_destroy(void)
404 {
405         struct llcrypt_ctx *pos, *n;
406
407         list_for_each_entry_safe(pos, n, &llcrypt_free_ctxs, free_list)
408                 kmem_cache_free(llcrypt_ctx_cachep, pos);
409         INIT_LIST_HEAD(&llcrypt_free_ctxs);
410         mempool_destroy(llcrypt_bounce_page_pool);
411         llcrypt_bounce_page_pool = NULL;
412 }
413
414 /**
415  * llcrypt_initialize() - allocate major buffers for fs encryption.
416  * @cop_flags:  llcrypt operations flags
417  *
418  * We only call this when we start accessing encrypted files, since it
419  * results in memory getting allocated that wouldn't otherwise be used.
420  *
421  * Return: Zero on success, non-zero otherwise.
422  */
423 int llcrypt_initialize(unsigned int cop_flags)
424 {
425         int i, res = -ENOMEM;
426
427         /* No need to allocate a bounce page pool if this FS won't use it. */
428         if (cop_flags & LL_CFLG_OWN_PAGES)
429                 return 0;
430
431         mutex_lock(&llcrypt_init_mutex);
432         if (llcrypt_bounce_page_pool)
433                 goto already_initialized;
434
435         for (i = 0; i < num_prealloc_crypto_ctxs; i++) {
436                 struct llcrypt_ctx *ctx;
437
438                 ctx = kmem_cache_zalloc(llcrypt_ctx_cachep, GFP_NOFS);
439                 if (!ctx)
440                         goto fail;
441                 list_add(&ctx->free_list, &llcrypt_free_ctxs);
442         }
443
444         llcrypt_bounce_page_pool =
445                 mempool_create_page_pool(num_prealloc_crypto_pages, 0);
446         if (!llcrypt_bounce_page_pool)
447                 goto fail;
448
449 already_initialized:
450         mutex_unlock(&llcrypt_init_mutex);
451         return 0;
452 fail:
453         llcrypt_destroy();
454         mutex_unlock(&llcrypt_init_mutex);
455         return res;
456 }
457
458 void llcrypt_msg(const struct inode *inode, int mask,
459                  const char *fmt, ...)
460 {
461         static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
462                                       DEFAULT_RATELIMIT_BURST);
463         struct va_format vaf;
464         va_list args;
465
466         if (!__ratelimit(&rs))
467                 return;
468
469         va_start(args, fmt);
470         vaf.fmt = fmt;
471         vaf.va = &args;
472         if (inode)
473                 CDEBUG(mask, "llcrypt (%s, inode %lu): %pV\n",
474                        inode->i_sb->s_id, inode->i_ino, &vaf);
475         else
476                 CDEBUG(mask, "llcrypt: %pV\n", &vaf);
477         va_end(args);
478 }
479
480 /**
481  * llcrypt_init() - Set up for fs encryption.
482  */
483 int __init llcrypt_init(void)
484 {
485         int err = -ENOMEM;
486
487         /*
488          * Use an unbound workqueue to allow bios to be decrypted in parallel
489          * even when they happen to complete on the same CPU.  This sacrifices
490          * locality, but it's worthwhile since decryption is CPU-intensive.
491          *
492          * Also use a high-priority workqueue to prioritize decryption work,
493          * which blocks reads from completing, over regular application tasks.
494          */
495         llcrypt_read_workqueue = alloc_workqueue("llcrypt_read_queue",
496                                                  WQ_UNBOUND | WQ_HIGHPRI,
497                                                  num_online_cpus());
498         if (!llcrypt_read_workqueue)
499                 goto fail;
500
501         llcrypt_ctx_cachep = KMEM_CACHE(llcrypt_ctx, SLAB_RECLAIM_ACCOUNT);
502         if (!llcrypt_ctx_cachep)
503                 goto fail_free_queue;
504
505         llcrypt_info_cachep = KMEM_CACHE(llcrypt_info, SLAB_RECLAIM_ACCOUNT);
506         if (!llcrypt_info_cachep)
507                 goto fail_free_ctx;
508
509         err = llcrypt_init_keyring();
510         if (err)
511                 goto fail_free_info;
512
513         return 0;
514
515 fail_free_info:
516         kmem_cache_destroy(llcrypt_info_cachep);
517 fail_free_ctx:
518         kmem_cache_destroy(llcrypt_ctx_cachep);
519 fail_free_queue:
520         destroy_workqueue(llcrypt_read_workqueue);
521 fail:
522         return err;
523 }
524
525 /**
526  * llcrypt_exit() - Clean up for fs encryption.
527  */
528 void __exit llcrypt_exit(void)
529 {
530         llcrypt_exit_keyring();
531
532         llcrypt_destroy();
533         /*
534          * Make sure all delayed rcu free inodes are flushed before we
535          * destroy cache.
536          */
537         rcu_barrier();
538
539         kmem_cache_destroy(llcrypt_info_cachep);
540         kmem_cache_destroy(llcrypt_ctx_cachep);
541         destroy_workqueue(llcrypt_read_workqueue);
542 }