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