Loading...
1// SPDX-License-Identifier: GPL-2.0
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 fscrypt_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#include <linux/pagemap.h>
24#include <linux/module.h>
25#include <linux/bio.h>
26#include <linux/namei.h>
27#include "fscrypt_private.h"
28
29static void __fscrypt_decrypt_bio(struct bio *bio, bool done)
30{
31 struct bio_vec *bv;
32 struct bvec_iter_all iter_all;
33
34 bio_for_each_segment_all(bv, bio, iter_all) {
35 struct page *page = bv->bv_page;
36 int ret = fscrypt_decrypt_pagecache_blocks(page, bv->bv_len,
37 bv->bv_offset);
38 if (ret)
39 SetPageError(page);
40 else if (done)
41 SetPageUptodate(page);
42 if (done)
43 unlock_page(page);
44 }
45}
46
47void fscrypt_decrypt_bio(struct bio *bio)
48{
49 __fscrypt_decrypt_bio(bio, false);
50}
51EXPORT_SYMBOL(fscrypt_decrypt_bio);
52
53static void completion_pages(struct work_struct *work)
54{
55 struct fscrypt_ctx *ctx = container_of(work, struct fscrypt_ctx, work);
56 struct bio *bio = ctx->bio;
57
58 __fscrypt_decrypt_bio(bio, true);
59 fscrypt_release_ctx(ctx);
60 bio_put(bio);
61}
62
63void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx, struct bio *bio)
64{
65 INIT_WORK(&ctx->work, completion_pages);
66 ctx->bio = bio;
67 fscrypt_enqueue_decrypt_work(&ctx->work);
68}
69EXPORT_SYMBOL(fscrypt_enqueue_decrypt_bio);
70
71int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
72 sector_t pblk, unsigned int len)
73{
74 const unsigned int blockbits = inode->i_blkbits;
75 const unsigned int blocksize = 1 << blockbits;
76 struct page *ciphertext_page;
77 struct bio *bio;
78 int ret, err = 0;
79
80 ciphertext_page = fscrypt_alloc_bounce_page(GFP_NOWAIT);
81 if (!ciphertext_page)
82 return -ENOMEM;
83
84 while (len--) {
85 err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk,
86 ZERO_PAGE(0), ciphertext_page,
87 blocksize, 0, GFP_NOFS);
88 if (err)
89 goto errout;
90
91 bio = bio_alloc(GFP_NOWAIT, 1);
92 if (!bio) {
93 err = -ENOMEM;
94 goto errout;
95 }
96 bio_set_dev(bio, inode->i_sb->s_bdev);
97 bio->bi_iter.bi_sector = pblk << (blockbits - 9);
98 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
99 ret = bio_add_page(bio, ciphertext_page, blocksize, 0);
100 if (WARN_ON(ret != blocksize)) {
101 /* should never happen! */
102 bio_put(bio);
103 err = -EIO;
104 goto errout;
105 }
106 err = submit_bio_wait(bio);
107 if (err == 0 && bio->bi_status)
108 err = -EIO;
109 bio_put(bio);
110 if (err)
111 goto errout;
112 lblk++;
113 pblk++;
114 }
115 err = 0;
116errout:
117 fscrypt_free_bounce_page(ciphertext_page);
118 return err;
119}
120EXPORT_SYMBOL(fscrypt_zeroout_range);
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Utility functions for file contents encryption/decryption on
4 * block device-based filesystems.
5 *
6 * Copyright (C) 2015, Google, Inc.
7 * Copyright (C) 2015, Motorola Mobility
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
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 * Return: %true on success; %false on failure. On failure, bio->bi_status is
29 * also set to an error status.
30 */
31bool fscrypt_decrypt_bio(struct bio *bio)
32{
33 struct bio_vec *bv;
34 struct bvec_iter_all iter_all;
35
36 bio_for_each_segment_all(bv, bio, iter_all) {
37 struct page *page = bv->bv_page;
38 int err = fscrypt_decrypt_pagecache_blocks(page, bv->bv_len,
39 bv->bv_offset);
40
41 if (err) {
42 bio->bi_status = errno_to_blk_status(err);
43 return false;
44 }
45 }
46 return true;
47}
48EXPORT_SYMBOL(fscrypt_decrypt_bio);
49
50static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode,
51 pgoff_t lblk, sector_t pblk,
52 unsigned int len)
53{
54 const unsigned int blockbits = inode->i_blkbits;
55 const unsigned int blocks_per_page = 1 << (PAGE_SHIFT - blockbits);
56 struct bio *bio;
57 int ret, err = 0;
58 int num_pages = 0;
59
60 /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
61 bio = bio_alloc(inode->i_sb->s_bdev, BIO_MAX_VECS, REQ_OP_WRITE,
62 GFP_NOFS);
63
64 while (len) {
65 unsigned int blocks_this_page = min(len, blocks_per_page);
66 unsigned int bytes_this_page = blocks_this_page << blockbits;
67
68 if (num_pages == 0) {
69 fscrypt_set_bio_crypt_ctx(bio, inode, lblk, GFP_NOFS);
70 bio->bi_iter.bi_sector =
71 pblk << (blockbits - SECTOR_SHIFT);
72 }
73 ret = bio_add_page(bio, ZERO_PAGE(0), bytes_this_page, 0);
74 if (WARN_ON(ret != bytes_this_page)) {
75 err = -EIO;
76 goto out;
77 }
78 num_pages++;
79 len -= blocks_this_page;
80 lblk += blocks_this_page;
81 pblk += blocks_this_page;
82 if (num_pages == BIO_MAX_VECS || !len ||
83 !fscrypt_mergeable_bio(bio, inode, lblk)) {
84 err = submit_bio_wait(bio);
85 if (err)
86 goto out;
87 bio_reset(bio, inode->i_sb->s_bdev, REQ_OP_WRITE);
88 num_pages = 0;
89 }
90 }
91out:
92 bio_put(bio);
93 return err;
94}
95
96/**
97 * fscrypt_zeroout_range() - zero out a range of blocks in an encrypted file
98 * @inode: the file's inode
99 * @lblk: the first file logical block to zero out
100 * @pblk: the first filesystem physical block to zero out
101 * @len: number of blocks to zero out
102 *
103 * Zero out filesystem blocks in an encrypted regular file on-disk, i.e. write
104 * ciphertext blocks which decrypt to the all-zeroes block. The blocks must be
105 * both logically and physically contiguous. It's also assumed that the
106 * filesystem only uses a single block device, ->s_bdev.
107 *
108 * Note that since each block uses a different IV, this involves writing a
109 * different ciphertext to each block; we can't simply reuse the same one.
110 *
111 * Return: 0 on success; -errno on failure.
112 */
113int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
114 sector_t pblk, unsigned int len)
115{
116 const unsigned int blockbits = inode->i_blkbits;
117 const unsigned int blocksize = 1 << blockbits;
118 const unsigned int blocks_per_page_bits = PAGE_SHIFT - blockbits;
119 const unsigned int blocks_per_page = 1 << blocks_per_page_bits;
120 struct page *pages[16]; /* write up to 16 pages at a time */
121 unsigned int nr_pages;
122 unsigned int i;
123 unsigned int offset;
124 struct bio *bio;
125 int ret, err;
126
127 if (len == 0)
128 return 0;
129
130 if (fscrypt_inode_uses_inline_crypto(inode))
131 return fscrypt_zeroout_range_inline_crypt(inode, lblk, pblk,
132 len);
133
134 BUILD_BUG_ON(ARRAY_SIZE(pages) > BIO_MAX_VECS);
135 nr_pages = min_t(unsigned int, ARRAY_SIZE(pages),
136 (len + blocks_per_page - 1) >> blocks_per_page_bits);
137
138 /*
139 * We need at least one page for ciphertext. Allocate the first one
140 * from a mempool, with __GFP_DIRECT_RECLAIM set so that it can't fail.
141 *
142 * Any additional page allocations are allowed to fail, as they only
143 * help performance, and waiting on the mempool for them could deadlock.
144 */
145 for (i = 0; i < nr_pages; i++) {
146 pages[i] = fscrypt_alloc_bounce_page(i == 0 ? GFP_NOFS :
147 GFP_NOWAIT | __GFP_NOWARN);
148 if (!pages[i])
149 break;
150 }
151 nr_pages = i;
152 if (WARN_ON(nr_pages <= 0))
153 return -EINVAL;
154
155 /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
156 bio = bio_alloc(inode->i_sb->s_bdev, nr_pages, REQ_OP_WRITE, GFP_NOFS);
157
158 do {
159 bio->bi_iter.bi_sector = pblk << (blockbits - 9);
160
161 i = 0;
162 offset = 0;
163 do {
164 err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk,
165 ZERO_PAGE(0), pages[i],
166 blocksize, offset, GFP_NOFS);
167 if (err)
168 goto out;
169 lblk++;
170 pblk++;
171 len--;
172 offset += blocksize;
173 if (offset == PAGE_SIZE || len == 0) {
174 ret = bio_add_page(bio, pages[i++], offset, 0);
175 if (WARN_ON(ret != offset)) {
176 err = -EIO;
177 goto out;
178 }
179 offset = 0;
180 }
181 } while (i != nr_pages && len != 0);
182
183 err = submit_bio_wait(bio);
184 if (err)
185 goto out;
186 bio_reset(bio, inode->i_sb->s_bdev, REQ_OP_WRITE);
187 } while (len != 0);
188 err = 0;
189out:
190 bio_put(bio);
191 for (i = 0; i < nr_pages; i++)
192 fscrypt_free_bounce_page(pages[i]);
193 return err;
194}
195EXPORT_SYMBOL(fscrypt_zeroout_range);