This source file includes following definitions.
- build_merkle_tree_level
- build_merkle_tree
- enable_verity
- fsverity_ioctl_enable
1
2
3
4
5
6
7
8 #include "fsverity_private.h"
9
10 #include <crypto/hash.h>
11 #include <linux/mount.h>
12 #include <linux/pagemap.h>
13 #include <linux/sched/signal.h>
14 #include <linux/uaccess.h>
15
16 static int build_merkle_tree_level(struct inode *inode, unsigned int level,
17 u64 num_blocks_to_hash,
18 const struct merkle_tree_params *params,
19 u8 *pending_hashes,
20 struct ahash_request *req)
21 {
22 const struct fsverity_operations *vops = inode->i_sb->s_vop;
23 unsigned int pending_size = 0;
24 u64 dst_block_num;
25 u64 i;
26 int err;
27
28 if (WARN_ON(params->block_size != PAGE_SIZE))
29 return -EINVAL;
30
31 if (level < params->num_levels) {
32 dst_block_num = params->level_start[level];
33 } else {
34 if (WARN_ON(num_blocks_to_hash != 1))
35 return -EINVAL;
36 dst_block_num = 0;
37 }
38
39 for (i = 0; i < num_blocks_to_hash; i++) {
40 struct page *src_page;
41
42 if ((pgoff_t)i % 10000 == 0 || i + 1 == num_blocks_to_hash)
43 pr_debug("Hashing block %llu of %llu for level %u\n",
44 i + 1, num_blocks_to_hash, level);
45
46 if (level == 0) {
47
48 src_page = read_mapping_page(inode->i_mapping, i, NULL);
49 if (IS_ERR(src_page)) {
50 err = PTR_ERR(src_page);
51 fsverity_err(inode,
52 "Error %d reading data page %llu",
53 err, i);
54 return err;
55 }
56 } else {
57
58 src_page = vops->read_merkle_tree_page(inode,
59 params->level_start[level - 1] + i);
60 if (IS_ERR(src_page)) {
61 err = PTR_ERR(src_page);
62 fsverity_err(inode,
63 "Error %d reading Merkle tree page %llu",
64 err, params->level_start[level - 1] + i);
65 return err;
66 }
67 }
68
69 err = fsverity_hash_page(params, inode, req, src_page,
70 &pending_hashes[pending_size]);
71 put_page(src_page);
72 if (err)
73 return err;
74 pending_size += params->digest_size;
75
76 if (level == params->num_levels)
77 return 0;
78
79 if (pending_size + params->digest_size > params->block_size ||
80 i + 1 == num_blocks_to_hash) {
81
82 memset(&pending_hashes[pending_size], 0,
83 params->block_size - pending_size);
84 err = vops->write_merkle_tree_block(inode,
85 pending_hashes,
86 dst_block_num,
87 params->log_blocksize);
88 if (err) {
89 fsverity_err(inode,
90 "Error %d writing Merkle tree block %llu",
91 err, dst_block_num);
92 return err;
93 }
94 dst_block_num++;
95 pending_size = 0;
96 }
97
98 if (fatal_signal_pending(current))
99 return -EINTR;
100 cond_resched();
101 }
102 return 0;
103 }
104
105
106
107
108
109
110
111
112
113 static int build_merkle_tree(struct inode *inode,
114 const struct merkle_tree_params *params,
115 u8 *root_hash)
116 {
117 u8 *pending_hashes;
118 struct ahash_request *req;
119 u64 blocks;
120 unsigned int level;
121 int err = -ENOMEM;
122
123 if (inode->i_size == 0) {
124
125 memset(root_hash, 0, params->digest_size);
126 return 0;
127 }
128
129 pending_hashes = kmalloc(params->block_size, GFP_KERNEL);
130 req = ahash_request_alloc(params->hash_alg->tfm, GFP_KERNEL);
131 if (!pending_hashes || !req)
132 goto out;
133
134
135
136
137
138
139 blocks = (inode->i_size + params->block_size - 1) >>
140 params->log_blocksize;
141 for (level = 0; level <= params->num_levels; level++) {
142 err = build_merkle_tree_level(inode, level, blocks, params,
143 pending_hashes, req);
144 if (err)
145 goto out;
146 blocks = (blocks + params->hashes_per_block - 1) >>
147 params->log_arity;
148 }
149 memcpy(root_hash, pending_hashes, params->digest_size);
150 err = 0;
151 out:
152 kfree(pending_hashes);
153 ahash_request_free(req);
154 return err;
155 }
156
157 static int enable_verity(struct file *filp,
158 const struct fsverity_enable_arg *arg)
159 {
160 struct inode *inode = file_inode(filp);
161 const struct fsverity_operations *vops = inode->i_sb->s_vop;
162 struct merkle_tree_params params = { };
163 struct fsverity_descriptor *desc;
164 size_t desc_size = sizeof(*desc) + arg->sig_size;
165 struct fsverity_info *vi;
166 int err;
167
168
169 desc = kzalloc(desc_size, GFP_KERNEL);
170 if (!desc)
171 return -ENOMEM;
172 desc->version = 1;
173 desc->hash_algorithm = arg->hash_algorithm;
174 desc->log_blocksize = ilog2(arg->block_size);
175
176
177 if (arg->salt_size &&
178 copy_from_user(desc->salt,
179 (const u8 __user *)(uintptr_t)arg->salt_ptr,
180 arg->salt_size)) {
181 err = -EFAULT;
182 goto out;
183 }
184 desc->salt_size = arg->salt_size;
185
186
187 if (arg->sig_size &&
188 copy_from_user(desc->signature,
189 (const u8 __user *)(uintptr_t)arg->sig_ptr,
190 arg->sig_size)) {
191 err = -EFAULT;
192 goto out;
193 }
194 desc->sig_size = cpu_to_le32(arg->sig_size);
195
196 desc->data_size = cpu_to_le64(inode->i_size);
197
198
199 err = fsverity_init_merkle_tree_params(¶ms, inode,
200 arg->hash_algorithm,
201 desc->log_blocksize,
202 desc->salt, desc->salt_size);
203 if (err)
204 goto out;
205
206
207
208
209
210 inode_lock(inode);
211 if (IS_VERITY(inode))
212 err = -EEXIST;
213 else
214 err = vops->begin_enable_verity(filp);
215 inode_unlock(inode);
216 if (err)
217 goto out;
218
219
220
221
222
223
224
225
226
227
228 pr_debug("Building Merkle tree...\n");
229 BUILD_BUG_ON(sizeof(desc->root_hash) < FS_VERITY_MAX_DIGEST_SIZE);
230 err = build_merkle_tree(inode, ¶ms, desc->root_hash);
231 if (err) {
232 fsverity_err(inode, "Error %d building Merkle tree", err);
233 goto rollback;
234 }
235 pr_debug("Done building Merkle tree. Root hash is %s:%*phN\n",
236 params.hash_alg->name, params.digest_size, desc->root_hash);
237
238
239
240
241
242
243
244
245 vi = fsverity_create_info(inode, desc, desc_size);
246 if (IS_ERR(vi)) {
247 err = PTR_ERR(vi);
248 goto rollback;
249 }
250
251 if (arg->sig_size)
252 pr_debug("Storing a %u-byte PKCS#7 signature alongside the file\n",
253 arg->sig_size);
254
255
256
257
258
259 inode_lock(inode);
260 err = vops->end_enable_verity(filp, desc, desc_size, params.tree_size);
261 inode_unlock(inode);
262 if (err) {
263 fsverity_err(inode, "%ps() failed with err %d",
264 vops->end_enable_verity, err);
265 fsverity_free_info(vi);
266 } else if (WARN_ON(!IS_VERITY(inode))) {
267 err = -EINVAL;
268 fsverity_free_info(vi);
269 } else {
270
271
272
273
274
275
276
277 fsverity_set_info(inode, vi);
278 }
279 out:
280 kfree(params.hashstate);
281 kfree(desc);
282 return err;
283
284 rollback:
285 inode_lock(inode);
286 (void)vops->end_enable_verity(filp, NULL, 0, params.tree_size);
287 inode_unlock(inode);
288 goto out;
289 }
290
291
292
293
294
295
296
297
298
299 int fsverity_ioctl_enable(struct file *filp, const void __user *uarg)
300 {
301 struct inode *inode = file_inode(filp);
302 struct fsverity_enable_arg arg;
303 int err;
304
305 if (copy_from_user(&arg, uarg, sizeof(arg)))
306 return -EFAULT;
307
308 if (arg.version != 1)
309 return -EINVAL;
310
311 if (arg.__reserved1 ||
312 memchr_inv(arg.__reserved2, 0, sizeof(arg.__reserved2)))
313 return -EINVAL;
314
315 if (arg.block_size != PAGE_SIZE)
316 return -EINVAL;
317
318 if (arg.salt_size > FIELD_SIZEOF(struct fsverity_descriptor, salt))
319 return -EMSGSIZE;
320
321 if (arg.sig_size > FS_VERITY_MAX_SIGNATURE_SIZE)
322 return -EMSGSIZE;
323
324
325
326
327
328
329
330
331 err = inode_permission(inode, MAY_WRITE);
332 if (err)
333 return err;
334
335 if (IS_APPEND(inode))
336 return -EPERM;
337
338 if (S_ISDIR(inode->i_mode))
339 return -EISDIR;
340
341 if (!S_ISREG(inode->i_mode))
342 return -EINVAL;
343
344 err = mnt_want_write_file(filp);
345 if (err)
346 return err;
347
348 err = deny_write_access(filp);
349 if (err)
350 goto out_drop_write;
351
352 err = enable_verity(filp, &arg);
353 if (err)
354 goto out_allow_write_access;
355
356
357
358
359
360
361
362
363
364 filemap_write_and_wait(inode->i_mapping);
365 invalidate_inode_pages2(inode->i_mapping);
366
367
368
369
370
371 out_allow_write_access:
372 allow_write_access(filp);
373 out_drop_write:
374 mnt_drop_write_file(filp);
375 return err;
376 }
377 EXPORT_SYMBOL_GPL(fsverity_ioctl_enable);