Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This contains encryption functions for per-file encryption. |
| 3 | * |
| 4 | * Copyright (C) 2015, Google, Inc. |
| 5 | * Copyright (C) 2015, Motorola Mobility |
| 6 | * |
| 7 | * Written by Michael Halcrow, 2014. |
| 8 | * |
| 9 | * Filename encryption additions |
| 10 | * Uday Savagaonkar, 2014 |
| 11 | * Encryption policy handling additions |
| 12 | * Ildar Muslukhov, 2014 |
| 13 | * Add fscrypt_pullback_bio_page() |
| 14 | * Jaegeuk Kim, 2015. |
| 15 | * |
| 16 | * This has not yet undergone a rigorous security audit. |
| 17 | * |
| 18 | * The usage of AES-XTS should conform to recommendations in NIST |
| 19 | * Special Publication 800-38E and IEEE P1619/D16. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/pagemap.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/bio.h> |
| 25 | #include <linux/namei.h> |
| 26 | #include "fscrypt_private.h" |
| 27 | |
Eric Biggers | 0509923 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 28 | static void __fscrypt_decrypt_bio(struct bio *bio, bool done) |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 29 | { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 30 | struct bio_vec *bv; |
| 31 | int i; |
| 32 | |
| 33 | bio_for_each_segment_all(bv, bio, i) { |
| 34 | struct page *page = bv->bv_page; |
| 35 | int ret = fscrypt_decrypt_page(page->mapping->host, page, |
| 36 | PAGE_SIZE, 0, page->index); |
| 37 | |
| 38 | if (ret) { |
| 39 | WARN_ON_ONCE(1); |
| 40 | SetPageError(page); |
Eric Biggers | 0509923 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 41 | } else if (done) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 42 | SetPageUptodate(page); |
| 43 | } |
Eric Biggers | 0509923 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 44 | if (done) |
| 45 | unlock_page(page); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 46 | } |
Eric Biggers | 0509923 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void fscrypt_decrypt_bio(struct bio *bio) |
| 50 | { |
| 51 | __fscrypt_decrypt_bio(bio, false); |
| 52 | } |
| 53 | EXPORT_SYMBOL(fscrypt_decrypt_bio); |
| 54 | |
| 55 | static void completion_pages(struct work_struct *work) |
| 56 | { |
| 57 | struct fscrypt_ctx *ctx = |
| 58 | container_of(work, struct fscrypt_ctx, r.work); |
| 59 | struct bio *bio = ctx->r.bio; |
| 60 | |
| 61 | __fscrypt_decrypt_bio(bio, true); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 62 | fscrypt_release_ctx(ctx); |
| 63 | bio_put(bio); |
| 64 | } |
| 65 | |
Eric Biggers | 0509923 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 66 | void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx, struct bio *bio) |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 67 | { |
| 68 | INIT_WORK(&ctx->r.work, completion_pages); |
| 69 | ctx->r.bio = bio; |
Eric Biggers | 0509923 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 70 | fscrypt_enqueue_decrypt_work(&ctx->r.work); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 71 | } |
Eric Biggers | 0509923 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 72 | EXPORT_SYMBOL(fscrypt_enqueue_decrypt_bio); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 73 | |
| 74 | void fscrypt_pullback_bio_page(struct page **page, bool restore) |
| 75 | { |
| 76 | struct fscrypt_ctx *ctx; |
| 77 | struct page *bounce_page; |
| 78 | |
| 79 | /* The bounce data pages are unmapped. */ |
| 80 | if ((*page)->mapping) |
| 81 | return; |
| 82 | |
| 83 | /* The bounce data page is unmapped. */ |
| 84 | bounce_page = *page; |
| 85 | ctx = (struct fscrypt_ctx *)page_private(bounce_page); |
| 86 | |
| 87 | /* restore control page */ |
| 88 | *page = ctx->w.control_page; |
| 89 | |
| 90 | if (restore) |
| 91 | fscrypt_restore_control_page(bounce_page); |
| 92 | } |
| 93 | EXPORT_SYMBOL(fscrypt_pullback_bio_page); |
| 94 | |
| 95 | int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, |
| 96 | sector_t pblk, unsigned int len) |
| 97 | { |
| 98 | struct fscrypt_ctx *ctx; |
| 99 | struct page *ciphertext_page = NULL; |
| 100 | struct bio *bio; |
| 101 | int ret, err = 0; |
| 102 | |
| 103 | BUG_ON(inode->i_sb->s_blocksize != PAGE_SIZE); |
| 104 | |
Eric Biggers | 0db29a1 | 2019-03-18 10:23:33 -0700 | [diff] [blame^] | 105 | ctx = fscrypt_get_ctx(GFP_NOFS); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 106 | if (IS_ERR(ctx)) |
| 107 | return PTR_ERR(ctx); |
| 108 | |
| 109 | ciphertext_page = fscrypt_alloc_bounce_page(ctx, GFP_NOWAIT); |
| 110 | if (IS_ERR(ciphertext_page)) { |
| 111 | err = PTR_ERR(ciphertext_page); |
| 112 | goto errout; |
| 113 | } |
| 114 | |
| 115 | while (len--) { |
| 116 | err = fscrypt_do_page_crypto(inode, FS_ENCRYPT, lblk, |
| 117 | ZERO_PAGE(0), ciphertext_page, |
| 118 | PAGE_SIZE, 0, GFP_NOFS); |
| 119 | if (err) |
| 120 | goto errout; |
| 121 | |
| 122 | bio = bio_alloc(GFP_NOWAIT, 1); |
| 123 | if (!bio) { |
| 124 | err = -ENOMEM; |
| 125 | goto errout; |
| 126 | } |
| 127 | bio->bi_bdev = inode->i_sb->s_bdev; |
| 128 | bio->bi_iter.bi_sector = |
| 129 | pblk << (inode->i_sb->s_blocksize_bits - 9); |
| 130 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
| 131 | ret = bio_add_page(bio, ciphertext_page, |
| 132 | inode->i_sb->s_blocksize, 0); |
| 133 | if (ret != inode->i_sb->s_blocksize) { |
| 134 | /* should never happen! */ |
| 135 | WARN_ON(1); |
| 136 | bio_put(bio); |
| 137 | err = -EIO; |
| 138 | goto errout; |
| 139 | } |
| 140 | err = submit_bio_wait(bio); |
| 141 | if (err == 0 && bio->bi_error) |
| 142 | err = -EIO; |
| 143 | bio_put(bio); |
| 144 | if (err) |
| 145 | goto errout; |
| 146 | lblk++; |
| 147 | pblk++; |
| 148 | } |
| 149 | err = 0; |
| 150 | errout: |
| 151 | fscrypt_release_ctx(ctx); |
| 152 | return err; |
| 153 | } |
| 154 | EXPORT_SYMBOL(fscrypt_zeroout_range); |