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