1 // SPDX-License-Identifier: GPL-2.0
3 * Ioctl to enable verity on a file
5 * Copyright 2019 Google LLC
8 #include "fsverity_private.h"
10 #include <crypto/hash.h>
11 #include <linux/backing-dev.h>
12 #include <linux/mount.h>
13 #include <linux/pagemap.h>
14 #include <linux/sched/signal.h>
15 #include <linux/uaccess.h>
18 * Read a file data page for Merkle tree construction. Do aggressive readahead,
19 * since we're sequentially reading the entire file.
21 static struct page *read_file_data_page(struct file *file, pgoff_t index,
22 struct file_ra_state *ra,
23 unsigned long remaining_pages)
25 DEFINE_READAHEAD(ractl, file, ra, file->f_mapping, index);
28 folio = __filemap_get_folio(ractl.mapping, index, FGP_ACCESSED, 0);
29 if (!folio || !folio_test_uptodate(folio)) {
33 page_cache_sync_ra(&ractl, remaining_pages);
34 folio = read_cache_folio(ractl.mapping, index, NULL, file);
38 if (folio_test_readahead(folio))
39 page_cache_async_ra(&ractl, folio, remaining_pages);
40 return folio_file_page(folio, index);
43 static int build_merkle_tree_level(struct file *filp, unsigned int level,
44 u64 num_blocks_to_hash,
45 const struct merkle_tree_params *params,
47 struct ahash_request *req)
49 struct inode *inode = file_inode(filp);
50 const struct fsverity_operations *vops = inode->i_sb->s_vop;
51 struct file_ra_state ra = { 0 };
52 unsigned int pending_size = 0;
57 if (WARN_ON(params->block_size != PAGE_SIZE)) /* checked earlier too */
60 if (level < params->num_levels) {
61 dst_block_num = params->level_start[level];
63 if (WARN_ON(num_blocks_to_hash != 1))
65 dst_block_num = 0; /* unused */
68 file_ra_state_init(&ra, filp->f_mapping);
70 for (i = 0; i < num_blocks_to_hash; i++) {
71 struct page *src_page;
73 if ((pgoff_t)i % 10000 == 0 || i + 1 == num_blocks_to_hash)
74 pr_debug("Hashing block %llu of %llu for level %u\n",
75 i + 1, num_blocks_to_hash, level);
78 /* Leaf: hashing a data block */
79 src_page = read_file_data_page(filp, i, &ra,
80 num_blocks_to_hash - i);
81 if (IS_ERR(src_page)) {
82 err = PTR_ERR(src_page);
84 "Error %d reading data page %llu",
89 unsigned long num_ra_pages =
90 min_t(unsigned long, num_blocks_to_hash - i,
91 inode->i_sb->s_bdi->io_pages);
93 /* Non-leaf: hashing hash block from level below */
94 src_page = vops->read_merkle_tree_page(inode,
95 params->level_start[level - 1] + i,
97 if (IS_ERR(src_page)) {
98 err = PTR_ERR(src_page);
100 "Error %d reading Merkle tree page %llu",
101 err, params->level_start[level - 1] + i);
106 err = fsverity_hash_page(params, inode, req, src_page,
107 &pending_hashes[pending_size]);
111 pending_size += params->digest_size;
113 if (level == params->num_levels) /* Root hash? */
116 if (pending_size + params->digest_size > params->block_size ||
117 i + 1 == num_blocks_to_hash) {
118 /* Flush the pending hash block */
119 memset(&pending_hashes[pending_size], 0,
120 params->block_size - pending_size);
121 err = vops->write_merkle_tree_block(inode,
124 params->log_blocksize);
127 "Error %d writing Merkle tree block %llu",
135 if (fatal_signal_pending(current))
143 * Build the Merkle tree for the given file using the given parameters, and
144 * return the root hash in @root_hash.
146 * The tree is written to a filesystem-specific location as determined by the
147 * ->write_merkle_tree_block() method. However, the blocks that comprise the
148 * tree are the same for all filesystems.
150 static int build_merkle_tree(struct file *filp,
151 const struct merkle_tree_params *params,
154 struct inode *inode = file_inode(filp);
156 struct ahash_request *req;
161 if (inode->i_size == 0) {
162 /* Empty file is a special case; root hash is all 0's */
163 memset(root_hash, 0, params->digest_size);
167 /* This allocation never fails, since it's mempool-backed. */
168 req = fsverity_alloc_hash_request(params->hash_alg, GFP_KERNEL);
170 pending_hashes = kmalloc(params->block_size, GFP_KERNEL);
175 * Build each level of the Merkle tree, starting at the leaf level
176 * (level 0) and ascending to the root node (level 'num_levels - 1').
177 * Then at the end (level 'num_levels'), calculate the root hash.
179 blocks = ((u64)inode->i_size + params->block_size - 1) >>
180 params->log_blocksize;
181 for (level = 0; level <= params->num_levels; level++) {
182 err = build_merkle_tree_level(filp, level, blocks, params,
183 pending_hashes, req);
186 blocks = (blocks + params->hashes_per_block - 1) >>
189 memcpy(root_hash, pending_hashes, params->digest_size);
192 kfree(pending_hashes);
193 fsverity_free_hash_request(params->hash_alg, req);
197 static int enable_verity(struct file *filp,
198 const struct fsverity_enable_arg *arg)
200 struct inode *inode = file_inode(filp);
201 const struct fsverity_operations *vops = inode->i_sb->s_vop;
202 struct merkle_tree_params params = { };
203 struct fsverity_descriptor *desc;
204 size_t desc_size = struct_size(desc, signature, arg->sig_size);
205 struct fsverity_info *vi;
208 /* Start initializing the fsverity_descriptor */
209 desc = kzalloc(desc_size, GFP_KERNEL);
213 desc->hash_algorithm = arg->hash_algorithm;
214 desc->log_blocksize = ilog2(arg->block_size);
216 /* Get the salt if the user provided one */
217 if (arg->salt_size &&
218 copy_from_user(desc->salt, u64_to_user_ptr(arg->salt_ptr),
223 desc->salt_size = arg->salt_size;
225 /* Get the signature if the user provided one */
227 copy_from_user(desc->signature, u64_to_user_ptr(arg->sig_ptr),
232 desc->sig_size = cpu_to_le32(arg->sig_size);
234 desc->data_size = cpu_to_le64(inode->i_size);
236 /* Prepare the Merkle tree parameters */
237 err = fsverity_init_merkle_tree_params(¶ms, inode,
240 desc->salt, desc->salt_size);
245 * Start enabling verity on this file, serialized by the inode lock.
246 * Fail if verity is already enabled or is already being enabled.
249 if (IS_VERITY(inode))
252 err = vops->begin_enable_verity(filp);
258 * Build the Merkle tree. Don't hold the inode lock during this, since
259 * on huge files this may take a very long time and we don't want to
260 * force unrelated syscalls like chown() to block forever. We don't
261 * need the inode lock here because deny_write_access() already prevents
262 * the file from being written to or truncated, and we still serialize
263 * ->begin_enable_verity() and ->end_enable_verity() using the inode
264 * lock and only allow one process to be here at a time on a given file.
266 pr_debug("Building Merkle tree...\n");
267 BUILD_BUG_ON(sizeof(desc->root_hash) < FS_VERITY_MAX_DIGEST_SIZE);
268 err = build_merkle_tree(filp, ¶ms, desc->root_hash);
270 fsverity_err(inode, "Error %d building Merkle tree", err);
273 pr_debug("Done building Merkle tree. Root hash is %s:%*phN\n",
274 params.hash_alg->name, params.digest_size, desc->root_hash);
277 * Create the fsverity_info. Don't bother trying to save work by
278 * reusing the merkle_tree_params from above. Instead, just create the
279 * fsverity_info from the fsverity_descriptor as if it were just loaded
280 * from disk. This is simpler, and it serves as an extra check that the
281 * metadata we're writing is valid before actually enabling verity.
283 vi = fsverity_create_info(inode, desc);
290 pr_debug("Storing a %u-byte PKCS#7 signature alongside the file\n",
294 * Tell the filesystem to finish enabling verity on the file.
295 * Serialized with ->begin_enable_verity() by the inode lock.
298 err = vops->end_enable_verity(filp, desc, desc_size, params.tree_size);
301 fsverity_err(inode, "%ps() failed with err %d",
302 vops->end_enable_verity, err);
303 fsverity_free_info(vi);
304 } else if (WARN_ON(!IS_VERITY(inode))) {
306 fsverity_free_info(vi);
308 /* Successfully enabled verity */
311 * Readers can start using ->i_verity_info immediately, so it
312 * can't be rolled back once set. So don't set it until just
313 * after the filesystem has successfully enabled verity.
315 fsverity_set_info(inode, vi);
318 kfree(params.hashstate);
324 (void)vops->end_enable_verity(filp, NULL, 0, params.tree_size);
330 * fsverity_ioctl_enable() - enable verity on a file
331 * @filp: file to enable verity on
332 * @uarg: user pointer to fsverity_enable_arg
334 * Enable fs-verity on a file. See the "FS_IOC_ENABLE_VERITY" section of
335 * Documentation/filesystems/fsverity.rst for the documentation.
337 * Return: 0 on success, -errno on failure
339 int fsverity_ioctl_enable(struct file *filp, const void __user *uarg)
341 struct inode *inode = file_inode(filp);
342 struct fsverity_enable_arg arg;
345 if (copy_from_user(&arg, uarg, sizeof(arg)))
348 if (arg.version != 1)
351 if (arg.__reserved1 ||
352 memchr_inv(arg.__reserved2, 0, sizeof(arg.__reserved2)))
355 if (arg.block_size != PAGE_SIZE)
358 if (arg.salt_size > sizeof_field(struct fsverity_descriptor, salt))
361 if (arg.sig_size > FS_VERITY_MAX_SIGNATURE_SIZE)
365 * Require a regular file with write access. But the actual fd must
366 * still be readonly so that we can lock out all writers. This is
367 * needed to guarantee that no writable fds exist to the file once it
368 * has verity enabled, and to stabilize the data being hashed.
371 err = file_permission(filp, MAY_WRITE);
375 if (IS_APPEND(inode))
378 if (S_ISDIR(inode->i_mode))
381 if (!S_ISREG(inode->i_mode))
384 err = mnt_want_write_file(filp);
385 if (err) /* -EROFS */
388 err = deny_write_access(filp);
389 if (err) /* -ETXTBSY */
392 err = enable_verity(filp, &arg);
395 * We no longer drop the inode's pagecache after enabling verity. This
396 * used to be done to try to avoid a race condition where pages could be
397 * evicted after being used in the Merkle tree construction, then
398 * re-instantiated by a concurrent read. Such pages are unverified, and
399 * the backing storage could have filled them with different content, so
400 * they shouldn't be used to fulfill reads once verity is enabled.
402 * But, dropping the pagecache has a big performance impact, and it
403 * doesn't fully solve the race condition anyway. So for those reasons,
404 * and also because this race condition isn't very important relatively
405 * speaking (especially for small-ish files, where the chance of a page
406 * being used, evicted, *and* re-instantiated all while enabling verity
407 * is quite small), we no longer drop the inode's pagecache.
411 * allow_write_access() is needed to pair with deny_write_access().
412 * Regardless, the filesystem won't allow writing to verity files.
414 allow_write_access(filp);
416 mnt_drop_write_file(filp);
419 EXPORT_SYMBOL_GPL(fsverity_ioctl_enable);