blob: 9f69f19952b3ff02a004e7b2cc4a555db92765c4 [file] [log] [blame]
Hyojun Kim63da4202017-10-06 17:10:08 -07001/*
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 Biggers823fd562018-04-18 11:09:47 -070028static void __fscrypt_decrypt_bio(struct bio *bio, bool done)
Hyojun Kim63da4202017-10-06 17:10:08 -070029{
Hyojun Kim63da4202017-10-06 17:10:08 -070030 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 Kim63da4202017-10-06 17:10:08 -070035
Neeraj Soni36c65122018-04-18 21:04:46 +053036 if (fscrypt_using_hardware_encryption(page->mapping->host)) {
Hyojun Kim63da4202017-10-06 17:10:08 -070037 SetPageUptodate(page);
Neeraj Sonic692cb92018-04-18 17:20:22 +053038 } else {
39 int ret = fscrypt_decrypt_page(page->mapping->host,
40 page, PAGE_SIZE, 0, page->index);
41 if (ret) {
Neeraj Sonic692cb92018-04-18 17:20:22 +053042 SetPageError(page);
43 } else if (done) {
44 SetPageUptodate(page);
45 }
Hyojun Kim63da4202017-10-06 17:10:08 -070046 }
Eric Biggers823fd562018-04-18 11:09:47 -070047 if (done)
48 unlock_page(page);
Hyojun Kim63da4202017-10-06 17:10:08 -070049 }
Eric Biggers823fd562018-04-18 11:09:47 -070050}
51
52void fscrypt_decrypt_bio(struct bio *bio)
53{
54 __fscrypt_decrypt_bio(bio, false);
55}
56EXPORT_SYMBOL(fscrypt_decrypt_bio);
57
58static void completion_pages(struct work_struct *work)
59{
60 struct fscrypt_ctx *ctx =
61 container_of(work, struct fscrypt_ctx, r.work);
62 struct bio *bio = ctx->r.bio;
63
64 __fscrypt_decrypt_bio(bio, true);
Hyojun Kim63da4202017-10-06 17:10:08 -070065 fscrypt_release_ctx(ctx);
66 bio_put(bio);
67}
68
Eric Biggers823fd562018-04-18 11:09:47 -070069void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx, struct bio *bio)
Hyojun Kim63da4202017-10-06 17:10:08 -070070{
71 INIT_WORK(&ctx->r.work, completion_pages);
72 ctx->r.bio = bio;
Eric Biggers823fd562018-04-18 11:09:47 -070073 fscrypt_enqueue_decrypt_work(&ctx->r.work);
Hyojun Kim63da4202017-10-06 17:10:08 -070074}
Eric Biggers823fd562018-04-18 11:09:47 -070075EXPORT_SYMBOL(fscrypt_enqueue_decrypt_bio);
Hyojun Kim63da4202017-10-06 17:10:08 -070076
77void fscrypt_pullback_bio_page(struct page **page, bool restore)
78{
79 struct fscrypt_ctx *ctx;
80 struct page *bounce_page;
81
82 /* The bounce data pages are unmapped. */
83 if ((*page)->mapping)
84 return;
85
86 /* The bounce data page is unmapped. */
87 bounce_page = *page;
88 ctx = (struct fscrypt_ctx *)page_private(bounce_page);
89
90 /* restore control page */
91 *page = ctx->w.control_page;
92
93 if (restore)
94 fscrypt_restore_control_page(bounce_page);
95}
96EXPORT_SYMBOL(fscrypt_pullback_bio_page);
97
98int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
99 sector_t pblk, unsigned int len)
100{
101 struct fscrypt_ctx *ctx;
102 struct page *ciphertext_page = NULL;
103 struct bio *bio;
104 int ret, err = 0;
105
106 BUG_ON(inode->i_sb->s_blocksize != PAGE_SIZE);
107
Eric Biggers0db29a12019-03-18 10:23:33 -0700108 ctx = fscrypt_get_ctx(GFP_NOFS);
Hyojun Kim63da4202017-10-06 17:10:08 -0700109 if (IS_ERR(ctx))
110 return PTR_ERR(ctx);
111
112 ciphertext_page = fscrypt_alloc_bounce_page(ctx, GFP_NOWAIT);
113 if (IS_ERR(ciphertext_page)) {
114 err = PTR_ERR(ciphertext_page);
115 goto errout;
116 }
117
118 while (len--) {
119 err = fscrypt_do_page_crypto(inode, FS_ENCRYPT, lblk,
120 ZERO_PAGE(0), ciphertext_page,
121 PAGE_SIZE, 0, GFP_NOFS);
122 if (err)
123 goto errout;
124
125 bio = bio_alloc(GFP_NOWAIT, 1);
126 if (!bio) {
127 err = -ENOMEM;
128 goto errout;
129 }
130 bio->bi_bdev = inode->i_sb->s_bdev;
131 bio->bi_iter.bi_sector =
132 pblk << (inode->i_sb->s_blocksize_bits - 9);
Michael Halcrowe93c1c12017-10-11 16:36:05 -0700133 bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_NOENCRYPT);
Hyojun Kim63da4202017-10-06 17:10:08 -0700134 ret = bio_add_page(bio, ciphertext_page,
135 inode->i_sb->s_blocksize, 0);
136 if (ret != inode->i_sb->s_blocksize) {
137 /* should never happen! */
138 WARN_ON(1);
139 bio_put(bio);
140 err = -EIO;
141 goto errout;
142 }
143 err = submit_bio_wait(bio);
144 if (err == 0 && bio->bi_error)
145 err = -EIO;
146 bio_put(bio);
147 if (err)
148 goto errout;
149 lblk++;
150 pblk++;
151 }
152 err = 0;
153errout:
154 fscrypt_release_ctx(ctx);
155 return err;
156}
157EXPORT_SYMBOL(fscrypt_zeroout_range);