Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 2 | /* |
Eric Biggers | f262ca7 | 2021-09-09 12:07:37 -0700 | [diff] [blame] | 3 | * Utility functions for file contents encryption/decryption on |
| 4 | * block device-based filesystems. |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 2015, Google, Inc. |
| 7 | * Copyright (C) 2015, Motorola Mobility |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/pagemap.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/bio.h> |
| 13 | #include <linux/namei.h> |
| 14 | #include "fscrypt_private.h" |
| 15 | |
Eric Biggers | f262ca7 | 2021-09-09 12:07:37 -0700 | [diff] [blame] | 16 | /** |
| 17 | * fscrypt_decrypt_bio() - decrypt the contents of a bio |
| 18 | * @bio: the bio to decrypt |
| 19 | * |
| 20 | * Decrypt the contents of a "read" bio following successful completion of the |
| 21 | * underlying disk read. The bio must be reading a whole number of blocks of an |
| 22 | * encrypted file directly into the page cache. If the bio is reading the |
| 23 | * ciphertext into bounce pages instead of the page cache (for example, because |
| 24 | * the file is also compressed, so decompression is required after decryption), |
| 25 | * then this function isn't applicable. This function may sleep, so it must be |
| 26 | * called from a workqueue rather than from the bio's bi_end_io callback. |
| 27 | * |
| 28 | * This function sets PG_error on any pages that contain any blocks that failed |
| 29 | * to be decrypted. The filesystem must not mark such pages uptodate. |
| 30 | */ |
Eric Biggers | 1565bda | 2019-10-09 16:34:17 -0700 | [diff] [blame] | 31 | void fscrypt_decrypt_bio(struct bio *bio) |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 32 | { |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 33 | struct bio_vec *bv; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 34 | struct bvec_iter_all iter_all; |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 35 | |
Christoph Hellwig | 2b070cf | 2019-04-25 09:03:00 +0200 | [diff] [blame] | 36 | bio_for_each_segment_all(bv, bio, iter_all) { |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 37 | struct page *page = bv->bv_page; |
Eric Biggers | ffceeef | 2019-05-20 09:29:48 -0700 | [diff] [blame] | 38 | int ret = fscrypt_decrypt_pagecache_blocks(page, bv->bv_len, |
| 39 | bv->bv_offset); |
Eric Biggers | ff5d3a9 | 2019-03-15 14:16:32 -0700 | [diff] [blame] | 40 | if (ret) |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 41 | SetPageError(page); |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 42 | } |
Eric Biggers | 0cb8dae | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 43 | } |
Eric Biggers | 0cb8dae | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 44 | EXPORT_SYMBOL(fscrypt_decrypt_bio); |
| 45 | |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 46 | static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode, |
| 47 | pgoff_t lblk, sector_t pblk, |
| 48 | unsigned int len) |
| 49 | { |
| 50 | const unsigned int blockbits = inode->i_blkbits; |
| 51 | const unsigned int blocks_per_page = 1 << (PAGE_SHIFT - blockbits); |
| 52 | struct bio *bio; |
| 53 | int ret, err = 0; |
| 54 | int num_pages = 0; |
| 55 | |
| 56 | /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */ |
Christoph Hellwig | a8affc0 | 2021-03-11 12:01:37 +0100 | [diff] [blame] | 57 | bio = bio_alloc(GFP_NOFS, BIO_MAX_VECS); |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 58 | |
| 59 | while (len) { |
| 60 | unsigned int blocks_this_page = min(len, blocks_per_page); |
| 61 | unsigned int bytes_this_page = blocks_this_page << blockbits; |
| 62 | |
| 63 | if (num_pages == 0) { |
| 64 | fscrypt_set_bio_crypt_ctx(bio, inode, lblk, GFP_NOFS); |
| 65 | bio_set_dev(bio, inode->i_sb->s_bdev); |
| 66 | bio->bi_iter.bi_sector = |
| 67 | pblk << (blockbits - SECTOR_SHIFT); |
| 68 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
| 69 | } |
| 70 | ret = bio_add_page(bio, ZERO_PAGE(0), bytes_this_page, 0); |
| 71 | if (WARN_ON(ret != bytes_this_page)) { |
| 72 | err = -EIO; |
| 73 | goto out; |
| 74 | } |
| 75 | num_pages++; |
| 76 | len -= blocks_this_page; |
| 77 | lblk += blocks_this_page; |
| 78 | pblk += blocks_this_page; |
Christoph Hellwig | a8affc0 | 2021-03-11 12:01:37 +0100 | [diff] [blame] | 79 | if (num_pages == BIO_MAX_VECS || !len || |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 80 | !fscrypt_mergeable_bio(bio, inode, lblk)) { |
| 81 | err = submit_bio_wait(bio); |
| 82 | if (err) |
| 83 | goto out; |
| 84 | bio_reset(bio); |
| 85 | num_pages = 0; |
| 86 | } |
| 87 | } |
| 88 | out: |
| 89 | bio_put(bio); |
| 90 | return err; |
| 91 | } |
| 92 | |
Eric Biggers | 796f12d7 | 2019-12-26 10:08:13 -0600 | [diff] [blame] | 93 | /** |
| 94 | * fscrypt_zeroout_range() - zero out a range of blocks in an encrypted file |
| 95 | * @inode: the file's inode |
| 96 | * @lblk: the first file logical block to zero out |
| 97 | * @pblk: the first filesystem physical block to zero out |
| 98 | * @len: number of blocks to zero out |
| 99 | * |
| 100 | * Zero out filesystem blocks in an encrypted regular file on-disk, i.e. write |
| 101 | * ciphertext blocks which decrypt to the all-zeroes block. The blocks must be |
| 102 | * both logically and physically contiguous. It's also assumed that the |
| 103 | * filesystem only uses a single block device, ->s_bdev. |
| 104 | * |
| 105 | * Note that since each block uses a different IV, this involves writing a |
| 106 | * different ciphertext to each block; we can't simply reuse the same one. |
| 107 | * |
| 108 | * Return: 0 on success; -errno on failure. |
| 109 | */ |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 110 | int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, |
Eric Biggers | 796f12d7 | 2019-12-26 10:08:13 -0600 | [diff] [blame] | 111 | sector_t pblk, unsigned int len) |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 112 | { |
Eric Biggers | 930d453 | 2019-05-20 09:29:45 -0700 | [diff] [blame] | 113 | const unsigned int blockbits = inode->i_blkbits; |
| 114 | const unsigned int blocksize = 1 << blockbits; |
Eric Biggers | 796f12d7 | 2019-12-26 10:08:13 -0600 | [diff] [blame] | 115 | const unsigned int blocks_per_page_bits = PAGE_SHIFT - blockbits; |
| 116 | const unsigned int blocks_per_page = 1 << blocks_per_page_bits; |
| 117 | struct page *pages[16]; /* write up to 16 pages at a time */ |
| 118 | unsigned int nr_pages; |
| 119 | unsigned int i; |
| 120 | unsigned int offset; |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 121 | struct bio *bio; |
Eric Biggers | 796f12d7 | 2019-12-26 10:08:13 -0600 | [diff] [blame] | 122 | int ret, err; |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 123 | |
Eric Biggers | 796f12d7 | 2019-12-26 10:08:13 -0600 | [diff] [blame] | 124 | if (len == 0) |
| 125 | return 0; |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 126 | |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 127 | if (fscrypt_inode_uses_inline_crypto(inode)) |
| 128 | return fscrypt_zeroout_range_inline_crypt(inode, lblk, pblk, |
| 129 | len); |
| 130 | |
Christoph Hellwig | a8affc0 | 2021-03-11 12:01:37 +0100 | [diff] [blame] | 131 | BUILD_BUG_ON(ARRAY_SIZE(pages) > BIO_MAX_VECS); |
Eric Biggers | 796f12d7 | 2019-12-26 10:08:13 -0600 | [diff] [blame] | 132 | nr_pages = min_t(unsigned int, ARRAY_SIZE(pages), |
| 133 | (len + blocks_per_page - 1) >> blocks_per_page_bits); |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 134 | |
Eric Biggers | 796f12d7 | 2019-12-26 10:08:13 -0600 | [diff] [blame] | 135 | /* |
| 136 | * We need at least one page for ciphertext. Allocate the first one |
| 137 | * from a mempool, with __GFP_DIRECT_RECLAIM set so that it can't fail. |
| 138 | * |
| 139 | * Any additional page allocations are allowed to fail, as they only |
| 140 | * help performance, and waiting on the mempool for them could deadlock. |
| 141 | */ |
| 142 | for (i = 0; i < nr_pages; i++) { |
| 143 | pages[i] = fscrypt_alloc_bounce_page(i == 0 ? GFP_NOFS : |
| 144 | GFP_NOWAIT | __GFP_NOWARN); |
| 145 | if (!pages[i]) |
| 146 | break; |
| 147 | } |
| 148 | nr_pages = i; |
| 149 | if (WARN_ON(nr_pages <= 0)) |
| 150 | return -EINVAL; |
| 151 | |
| 152 | /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */ |
| 153 | bio = bio_alloc(GFP_NOFS, nr_pages); |
| 154 | |
| 155 | do { |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 156 | bio_set_dev(bio, inode->i_sb->s_bdev); |
Eric Biggers | 930d453 | 2019-05-20 09:29:45 -0700 | [diff] [blame] | 157 | bio->bi_iter.bi_sector = pblk << (blockbits - 9); |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 158 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
Eric Biggers | 796f12d7 | 2019-12-26 10:08:13 -0600 | [diff] [blame] | 159 | |
| 160 | i = 0; |
| 161 | offset = 0; |
| 162 | do { |
| 163 | err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk, |
| 164 | ZERO_PAGE(0), pages[i], |
| 165 | blocksize, offset, GFP_NOFS); |
| 166 | if (err) |
| 167 | goto out; |
| 168 | lblk++; |
| 169 | pblk++; |
| 170 | len--; |
| 171 | offset += blocksize; |
| 172 | if (offset == PAGE_SIZE || len == 0) { |
| 173 | ret = bio_add_page(bio, pages[i++], offset, 0); |
| 174 | if (WARN_ON(ret != offset)) { |
| 175 | err = -EIO; |
| 176 | goto out; |
| 177 | } |
| 178 | offset = 0; |
| 179 | } |
| 180 | } while (i != nr_pages && len != 0); |
| 181 | |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 182 | err = submit_bio_wait(bio); |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 183 | if (err) |
Eric Biggers | 796f12d7 | 2019-12-26 10:08:13 -0600 | [diff] [blame] | 184 | goto out; |
| 185 | bio_reset(bio); |
| 186 | } while (len != 0); |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 187 | err = 0; |
Eric Biggers | 796f12d7 | 2019-12-26 10:08:13 -0600 | [diff] [blame] | 188 | out: |
| 189 | bio_put(bio); |
| 190 | for (i = 0; i < nr_pages; i++) |
| 191 | fscrypt_free_bounce_page(pages[i]); |
Richard Weinberger | 58ae746 | 2016-12-19 12:25:32 +0100 | [diff] [blame] | 192 | return err; |
| 193 | } |
| 194 | EXPORT_SYMBOL(fscrypt_zeroout_range); |