Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -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 | 823fd56 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 28 | static void __fscrypt_decrypt_bio(struct bio *bio, bool done) |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 29 | { |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -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; |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 35 | |
Neeraj Soni | 36c6512 | 2018-04-18 21:04:46 +0530 | [diff] [blame^] | 36 | if (fscrypt_using_hardware_encryption(page->mapping->host)) { |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 37 | SetPageUptodate(page); |
Neeraj Soni | c692cb9 | 2018-04-18 17:20:22 +0530 | [diff] [blame] | 38 | } else { |
| 39 | int ret = fscrypt_decrypt_page(page->mapping->host, |
| 40 | page, PAGE_SIZE, 0, page->index); |
| 41 | if (ret) { |
| 42 | WARN_ON_ONCE(1); |
| 43 | SetPageError(page); |
| 44 | } else if (done) { |
| 45 | SetPageUptodate(page); |
| 46 | } |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 47 | } |
Eric Biggers | 823fd56 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 48 | if (done) |
| 49 | unlock_page(page); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 50 | } |
Eric Biggers | 823fd56 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void fscrypt_decrypt_bio(struct bio *bio) |
| 54 | { |
| 55 | __fscrypt_decrypt_bio(bio, false); |
| 56 | } |
| 57 | EXPORT_SYMBOL(fscrypt_decrypt_bio); |
| 58 | |
| 59 | static void completion_pages(struct work_struct *work) |
| 60 | { |
| 61 | struct fscrypt_ctx *ctx = |
| 62 | container_of(work, struct fscrypt_ctx, r.work); |
| 63 | struct bio *bio = ctx->r.bio; |
| 64 | |
| 65 | __fscrypt_decrypt_bio(bio, true); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 66 | fscrypt_release_ctx(ctx); |
| 67 | bio_put(bio); |
| 68 | } |
| 69 | |
Eric Biggers | 823fd56 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 70 | void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx, struct bio *bio) |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 71 | { |
| 72 | INIT_WORK(&ctx->r.work, completion_pages); |
| 73 | ctx->r.bio = bio; |
Eric Biggers | 823fd56 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 74 | fscrypt_enqueue_decrypt_work(&ctx->r.work); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 75 | } |
Eric Biggers | 823fd56 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 76 | EXPORT_SYMBOL(fscrypt_enqueue_decrypt_bio); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 77 | |
| 78 | void fscrypt_pullback_bio_page(struct page **page, bool restore) |
| 79 | { |
| 80 | struct fscrypt_ctx *ctx; |
| 81 | struct page *bounce_page; |
| 82 | |
| 83 | /* The bounce data pages are unmapped. */ |
| 84 | if ((*page)->mapping) |
| 85 | return; |
| 86 | |
| 87 | /* The bounce data page is unmapped. */ |
| 88 | bounce_page = *page; |
| 89 | ctx = (struct fscrypt_ctx *)page_private(bounce_page); |
| 90 | |
| 91 | /* restore control page */ |
| 92 | *page = ctx->w.control_page; |
| 93 | |
| 94 | if (restore) |
| 95 | fscrypt_restore_control_page(bounce_page); |
| 96 | } |
| 97 | EXPORT_SYMBOL(fscrypt_pullback_bio_page); |
| 98 | |
| 99 | int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, |
| 100 | sector_t pblk, unsigned int len) |
| 101 | { |
| 102 | struct fscrypt_ctx *ctx; |
| 103 | struct page *ciphertext_page = NULL; |
| 104 | struct bio *bio; |
| 105 | int ret, err = 0; |
| 106 | |
| 107 | BUG_ON(inode->i_sb->s_blocksize != PAGE_SIZE); |
| 108 | |
| 109 | ctx = fscrypt_get_ctx(inode, GFP_NOFS); |
| 110 | if (IS_ERR(ctx)) |
| 111 | return PTR_ERR(ctx); |
| 112 | |
| 113 | ciphertext_page = fscrypt_alloc_bounce_page(ctx, GFP_NOWAIT); |
| 114 | if (IS_ERR(ciphertext_page)) { |
| 115 | err = PTR_ERR(ciphertext_page); |
| 116 | goto errout; |
| 117 | } |
| 118 | |
| 119 | while (len--) { |
| 120 | err = fscrypt_do_page_crypto(inode, FS_ENCRYPT, lblk, |
| 121 | ZERO_PAGE(0), ciphertext_page, |
| 122 | PAGE_SIZE, 0, GFP_NOFS); |
| 123 | if (err) |
| 124 | goto errout; |
| 125 | |
| 126 | bio = bio_alloc(GFP_NOWAIT, 1); |
| 127 | if (!bio) { |
| 128 | err = -ENOMEM; |
| 129 | goto errout; |
| 130 | } |
| 131 | bio->bi_bdev = inode->i_sb->s_bdev; |
| 132 | bio->bi_iter.bi_sector = |
| 133 | pblk << (inode->i_sb->s_blocksize_bits - 9); |
| 134 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
| 135 | ret = bio_add_page(bio, ciphertext_page, |
| 136 | inode->i_sb->s_blocksize, 0); |
| 137 | if (ret != inode->i_sb->s_blocksize) { |
| 138 | /* should never happen! */ |
| 139 | WARN_ON(1); |
| 140 | bio_put(bio); |
| 141 | err = -EIO; |
| 142 | goto errout; |
| 143 | } |
| 144 | err = submit_bio_wait(bio); |
| 145 | if (err == 0 && bio->bi_error) |
| 146 | err = -EIO; |
| 147 | bio_put(bio); |
| 148 | if (err) |
| 149 | goto errout; |
| 150 | lblk++; |
| 151 | pblk++; |
| 152 | } |
| 153 | err = 0; |
| 154 | errout: |
| 155 | fscrypt_release_ctx(ctx); |
| 156 | return err; |
| 157 | } |
| 158 | EXPORT_SYMBOL(fscrypt_zeroout_range); |