xref: /openbmc/linux/fs/verity/enable.c (revision 672d6ef4)
13fda4c61SEric Biggers // SPDX-License-Identifier: GPL-2.0
23fda4c61SEric Biggers /*
37bf765ddSEric Biggers  * Ioctl to enable verity on a file
43fda4c61SEric Biggers  *
53fda4c61SEric Biggers  * Copyright 2019 Google LLC
63fda4c61SEric Biggers  */
73fda4c61SEric Biggers 
83fda4c61SEric Biggers #include "fsverity_private.h"
93fda4c61SEric Biggers 
108fcd94adSEric Biggers #include <crypto/hash.h>
113fda4c61SEric Biggers #include <linux/mount.h>
123fda4c61SEric Biggers #include <linux/sched/signal.h>
133fda4c61SEric Biggers #include <linux/uaccess.h>
143fda4c61SEric Biggers 
1556124d6cSEric Biggers struct block_buffer {
1656124d6cSEric Biggers 	u32 filled;
1739049b69SEric Biggers 	bool is_root_hash;
1856124d6cSEric Biggers 	u8 *data;
1956124d6cSEric Biggers };
20c22415d3SEric Biggers 
2156124d6cSEric Biggers /* Hash a block, writing the result to the next level's pending block buffer. */
hash_one_block(struct inode * inode,const struct merkle_tree_params * params,struct block_buffer * cur)2256124d6cSEric Biggers static int hash_one_block(struct inode *inode,
233fda4c61SEric Biggers 			  const struct merkle_tree_params *params,
248fcd94adSEric Biggers 			  struct block_buffer *cur)
253fda4c61SEric Biggers {
2656124d6cSEric Biggers 	struct block_buffer *next = cur + 1;
273fda4c61SEric Biggers 	int err;
283fda4c61SEric Biggers 
2939049b69SEric Biggers 	/*
3039049b69SEric Biggers 	 * Safety check to prevent a buffer overflow in case of a filesystem bug
3139049b69SEric Biggers 	 * that allows the file size to change despite deny_write_access(), or a
3239049b69SEric Biggers 	 * bug in the Merkle tree logic itself
3339049b69SEric Biggers 	 */
3439049b69SEric Biggers 	if (WARN_ON_ONCE(next->is_root_hash && next->filled != 0))
3539049b69SEric Biggers 		return -EINVAL;
3639049b69SEric Biggers 
3756124d6cSEric Biggers 	/* Zero-pad the block if it's shorter than the block size. */
3856124d6cSEric Biggers 	memset(&cur->data[cur->filled], 0, params->block_size - cur->filled);
393fda4c61SEric Biggers 
408fcd94adSEric Biggers 	err = fsverity_hash_block(params, inode, cur->data,
4156124d6cSEric Biggers 				  &next->data[next->filled]);
423fda4c61SEric Biggers 	if (err)
433fda4c61SEric Biggers 		return err;
4456124d6cSEric Biggers 	next->filled += params->digest_size;
4556124d6cSEric Biggers 	cur->filled = 0;
463fda4c61SEric Biggers 	return 0;
4756124d6cSEric Biggers }
483fda4c61SEric Biggers 
write_merkle_tree_block(struct inode * inode,const u8 * buf,unsigned long index,const struct merkle_tree_params * params)4956124d6cSEric Biggers static int write_merkle_tree_block(struct inode *inode, const u8 *buf,
5056124d6cSEric Biggers 				   unsigned long index,
5156124d6cSEric Biggers 				   const struct merkle_tree_params *params)
5256124d6cSEric Biggers {
5356124d6cSEric Biggers 	u64 pos = (u64)index << params->log_blocksize;
5456124d6cSEric Biggers 	int err;
5556124d6cSEric Biggers 
5656124d6cSEric Biggers 	err = inode->i_sb->s_vop->write_merkle_tree_block(inode, buf, pos,
5772ea15f0SEric Biggers 							  params->block_size);
5856124d6cSEric Biggers 	if (err)
5956124d6cSEric Biggers 		fsverity_err(inode, "Error %d writing Merkle tree block %lu",
6056124d6cSEric Biggers 			     err, index);
613fda4c61SEric Biggers 	return err;
623fda4c61SEric Biggers }
633fda4c61SEric Biggers 
643fda4c61SEric Biggers /*
65c22415d3SEric Biggers  * Build the Merkle tree for the given file using the given parameters, and
663fda4c61SEric Biggers  * return the root hash in @root_hash.
673fda4c61SEric Biggers  *
683fda4c61SEric Biggers  * The tree is written to a filesystem-specific location as determined by the
693fda4c61SEric Biggers  * ->write_merkle_tree_block() method.  However, the blocks that comprise the
703fda4c61SEric Biggers  * tree are the same for all filesystems.
713fda4c61SEric Biggers  */
build_merkle_tree(struct file * filp,const struct merkle_tree_params * params,u8 * root_hash)72c22415d3SEric Biggers static int build_merkle_tree(struct file *filp,
733fda4c61SEric Biggers 			     const struct merkle_tree_params *params,
743fda4c61SEric Biggers 			     u8 *root_hash)
753fda4c61SEric Biggers {
76c22415d3SEric Biggers 	struct inode *inode = file_inode(filp);
7756124d6cSEric Biggers 	const u64 data_size = inode->i_size;
7856124d6cSEric Biggers 	const int num_levels = params->num_levels;
7956124d6cSEric Biggers 	struct block_buffer _buffers[1 + FS_VERITY_MAX_LEVELS + 1] = {};
8056124d6cSEric Biggers 	struct block_buffer *buffers = &_buffers[1];
8156124d6cSEric Biggers 	unsigned long level_offset[FS_VERITY_MAX_LEVELS];
8256124d6cSEric Biggers 	int level;
8356124d6cSEric Biggers 	u64 offset;
8456124d6cSEric Biggers 	int err;
853fda4c61SEric Biggers 
8656124d6cSEric Biggers 	if (data_size == 0) {
873fda4c61SEric Biggers 		/* Empty file is a special case; root hash is all 0's */
883fda4c61SEric Biggers 		memset(root_hash, 0, params->digest_size);
893fda4c61SEric Biggers 		return 0;
903fda4c61SEric Biggers 	}
913fda4c61SEric Biggers 
923fda4c61SEric Biggers 	/*
9356124d6cSEric Biggers 	 * Allocate the block buffers.  Buffer "-1" is for data blocks.
9456124d6cSEric Biggers 	 * Buffers 0 <= level < num_levels are for the actual tree levels.
9556124d6cSEric Biggers 	 * Buffer 'num_levels' is for the root hash.
963fda4c61SEric Biggers 	 */
9756124d6cSEric Biggers 	for (level = -1; level < num_levels; level++) {
9856124d6cSEric Biggers 		buffers[level].data = kzalloc(params->block_size, GFP_KERNEL);
9956124d6cSEric Biggers 		if (!buffers[level].data) {
10056124d6cSEric Biggers 			err = -ENOMEM;
10156124d6cSEric Biggers 			goto out;
10256124d6cSEric Biggers 		}
10356124d6cSEric Biggers 	}
10456124d6cSEric Biggers 	buffers[num_levels].data = root_hash;
10539049b69SEric Biggers 	buffers[num_levels].is_root_hash = true;
10656124d6cSEric Biggers 
10756124d6cSEric Biggers 	BUILD_BUG_ON(sizeof(level_offset) != sizeof(params->level_start));
10856124d6cSEric Biggers 	memcpy(level_offset, params->level_start, sizeof(level_offset));
10956124d6cSEric Biggers 
11056124d6cSEric Biggers 	/* Hash each data block, also hashing the tree blocks as they fill up */
11156124d6cSEric Biggers 	for (offset = 0; offset < data_size; offset += params->block_size) {
11256124d6cSEric Biggers 		ssize_t bytes_read;
11356124d6cSEric Biggers 		loff_t pos = offset;
11456124d6cSEric Biggers 
11556124d6cSEric Biggers 		buffers[-1].filled = min_t(u64, params->block_size,
11656124d6cSEric Biggers 					   data_size - offset);
11756124d6cSEric Biggers 		bytes_read = __kernel_read(filp, buffers[-1].data,
11856124d6cSEric Biggers 					   buffers[-1].filled, &pos);
11956124d6cSEric Biggers 		if (bytes_read < 0) {
12056124d6cSEric Biggers 			err = bytes_read;
12156124d6cSEric Biggers 			fsverity_err(inode, "Error %d reading file data", err);
12256124d6cSEric Biggers 			goto out;
12356124d6cSEric Biggers 		}
12456124d6cSEric Biggers 		if (bytes_read != buffers[-1].filled) {
12556124d6cSEric Biggers 			err = -EINVAL;
12656124d6cSEric Biggers 			fsverity_err(inode, "Short read of file data");
12756124d6cSEric Biggers 			goto out;
12856124d6cSEric Biggers 		}
1298fcd94adSEric Biggers 		err = hash_one_block(inode, params, &buffers[-1]);
1303fda4c61SEric Biggers 		if (err)
1313fda4c61SEric Biggers 			goto out;
13256124d6cSEric Biggers 		for (level = 0; level < num_levels; level++) {
13356124d6cSEric Biggers 			if (buffers[level].filled + params->digest_size <=
13456124d6cSEric Biggers 			    params->block_size) {
13556124d6cSEric Biggers 				/* Next block at @level isn't full yet */
13656124d6cSEric Biggers 				break;
1373fda4c61SEric Biggers 			}
13856124d6cSEric Biggers 			/* Next block at @level is full */
13956124d6cSEric Biggers 
1408fcd94adSEric Biggers 			err = hash_one_block(inode, params, &buffers[level]);
14156124d6cSEric Biggers 			if (err)
14256124d6cSEric Biggers 				goto out;
14356124d6cSEric Biggers 			err = write_merkle_tree_block(inode,
14456124d6cSEric Biggers 						      buffers[level].data,
14556124d6cSEric Biggers 						      level_offset[level],
14656124d6cSEric Biggers 						      params);
14756124d6cSEric Biggers 			if (err)
14856124d6cSEric Biggers 				goto out;
14956124d6cSEric Biggers 			level_offset[level]++;
15056124d6cSEric Biggers 		}
15156124d6cSEric Biggers 		if (fatal_signal_pending(current)) {
15256124d6cSEric Biggers 			err = -EINTR;
15356124d6cSEric Biggers 			goto out;
15456124d6cSEric Biggers 		}
15556124d6cSEric Biggers 		cond_resched();
15656124d6cSEric Biggers 	}
15756124d6cSEric Biggers 	/* Finish all nonempty pending tree blocks. */
15856124d6cSEric Biggers 	for (level = 0; level < num_levels; level++) {
15956124d6cSEric Biggers 		if (buffers[level].filled != 0) {
1608fcd94adSEric Biggers 			err = hash_one_block(inode, params, &buffers[level]);
16156124d6cSEric Biggers 			if (err)
16256124d6cSEric Biggers 				goto out;
16356124d6cSEric Biggers 			err = write_merkle_tree_block(inode,
16456124d6cSEric Biggers 						      buffers[level].data,
16556124d6cSEric Biggers 						      level_offset[level],
16656124d6cSEric Biggers 						      params);
16756124d6cSEric Biggers 			if (err)
16856124d6cSEric Biggers 				goto out;
16956124d6cSEric Biggers 		}
17056124d6cSEric Biggers 	}
17156124d6cSEric Biggers 	/* The root hash was filled by the last call to hash_one_block(). */
1728eb8af4bSEric Biggers 	if (WARN_ON_ONCE(buffers[num_levels].filled != params->digest_size)) {
17356124d6cSEric Biggers 		err = -EINVAL;
17456124d6cSEric Biggers 		goto out;
17556124d6cSEric Biggers 	}
1763fda4c61SEric Biggers 	err = 0;
1773fda4c61SEric Biggers out:
17856124d6cSEric Biggers 	for (level = -1; level < num_levels; level++)
17956124d6cSEric Biggers 		kfree(buffers[level].data);
1803fda4c61SEric Biggers 	return err;
1813fda4c61SEric Biggers }
1823fda4c61SEric Biggers 
enable_verity(struct file * filp,const struct fsverity_enable_arg * arg)1833fda4c61SEric Biggers static int enable_verity(struct file *filp,
1843fda4c61SEric Biggers 			 const struct fsverity_enable_arg *arg)
1853fda4c61SEric Biggers {
1863fda4c61SEric Biggers 	struct inode *inode = file_inode(filp);
1873fda4c61SEric Biggers 	const struct fsverity_operations *vops = inode->i_sb->s_vop;
1883fda4c61SEric Biggers 	struct merkle_tree_params params = { };
1893fda4c61SEric Biggers 	struct fsverity_descriptor *desc;
190e6af1bb0SZhang Jianhua 	size_t desc_size = struct_size(desc, signature, arg->sig_size);
1913fda4c61SEric Biggers 	struct fsverity_info *vi;
1923fda4c61SEric Biggers 	int err;
1933fda4c61SEric Biggers 
1943fda4c61SEric Biggers 	/* Start initializing the fsverity_descriptor */
1953fda4c61SEric Biggers 	desc = kzalloc(desc_size, GFP_KERNEL);
1963fda4c61SEric Biggers 	if (!desc)
1973fda4c61SEric Biggers 		return -ENOMEM;
1983fda4c61SEric Biggers 	desc->version = 1;
1993fda4c61SEric Biggers 	desc->hash_algorithm = arg->hash_algorithm;
2003fda4c61SEric Biggers 	desc->log_blocksize = ilog2(arg->block_size);
2013fda4c61SEric Biggers 
2023fda4c61SEric Biggers 	/* Get the salt if the user provided one */
2033fda4c61SEric Biggers 	if (arg->salt_size &&
204da3a3da4SEric Biggers 	    copy_from_user(desc->salt, u64_to_user_ptr(arg->salt_ptr),
2053fda4c61SEric Biggers 			   arg->salt_size)) {
2063fda4c61SEric Biggers 		err = -EFAULT;
2073fda4c61SEric Biggers 		goto out;
2083fda4c61SEric Biggers 	}
2093fda4c61SEric Biggers 	desc->salt_size = arg->salt_size;
2103fda4c61SEric Biggers 
211*672d6ef4SEric Biggers 	/* Get the builtin signature if the user provided one */
212432434c9SEric Biggers 	if (arg->sig_size &&
213da3a3da4SEric Biggers 	    copy_from_user(desc->signature, u64_to_user_ptr(arg->sig_ptr),
214432434c9SEric Biggers 			   arg->sig_size)) {
215432434c9SEric Biggers 		err = -EFAULT;
216432434c9SEric Biggers 		goto out;
217432434c9SEric Biggers 	}
218432434c9SEric Biggers 	desc->sig_size = cpu_to_le32(arg->sig_size);
219432434c9SEric Biggers 
2203fda4c61SEric Biggers 	desc->data_size = cpu_to_le64(inode->i_size);
2213fda4c61SEric Biggers 
2223fda4c61SEric Biggers 	/* Prepare the Merkle tree parameters */
2233fda4c61SEric Biggers 	err = fsverity_init_merkle_tree_params(&params, inode,
2243fda4c61SEric Biggers 					       arg->hash_algorithm,
2253fda4c61SEric Biggers 					       desc->log_blocksize,
2263fda4c61SEric Biggers 					       desc->salt, desc->salt_size);
2273fda4c61SEric Biggers 	if (err)
2283fda4c61SEric Biggers 		goto out;
2293fda4c61SEric Biggers 
2303fda4c61SEric Biggers 	/*
2313fda4c61SEric Biggers 	 * Start enabling verity on this file, serialized by the inode lock.
2323fda4c61SEric Biggers 	 * Fail if verity is already enabled or is already being enabled.
2333fda4c61SEric Biggers 	 */
2343fda4c61SEric Biggers 	inode_lock(inode);
2353fda4c61SEric Biggers 	if (IS_VERITY(inode))
2363fda4c61SEric Biggers 		err = -EEXIST;
2373fda4c61SEric Biggers 	else
2383fda4c61SEric Biggers 		err = vops->begin_enable_verity(filp);
2393fda4c61SEric Biggers 	inode_unlock(inode);
2403fda4c61SEric Biggers 	if (err)
2413fda4c61SEric Biggers 		goto out;
2423fda4c61SEric Biggers 
2433fda4c61SEric Biggers 	/*
2443fda4c61SEric Biggers 	 * Build the Merkle tree.  Don't hold the inode lock during this, since
2453fda4c61SEric Biggers 	 * on huge files this may take a very long time and we don't want to
2463fda4c61SEric Biggers 	 * force unrelated syscalls like chown() to block forever.  We don't
2473fda4c61SEric Biggers 	 * need the inode lock here because deny_write_access() already prevents
2483fda4c61SEric Biggers 	 * the file from being written to or truncated, and we still serialize
2493fda4c61SEric Biggers 	 * ->begin_enable_verity() and ->end_enable_verity() using the inode
2503fda4c61SEric Biggers 	 * lock and only allow one process to be here at a time on a given file.
2513fda4c61SEric Biggers 	 */
2523fda4c61SEric Biggers 	BUILD_BUG_ON(sizeof(desc->root_hash) < FS_VERITY_MAX_DIGEST_SIZE);
253c22415d3SEric Biggers 	err = build_merkle_tree(filp, &params, desc->root_hash);
2543fda4c61SEric Biggers 	if (err) {
2553fda4c61SEric Biggers 		fsverity_err(inode, "Error %d building Merkle tree", err);
2563fda4c61SEric Biggers 		goto rollback;
2573fda4c61SEric Biggers 	}
2583fda4c61SEric Biggers 
2593fda4c61SEric Biggers 	/*
2603fda4c61SEric Biggers 	 * Create the fsverity_info.  Don't bother trying to save work by
2613fda4c61SEric Biggers 	 * reusing the merkle_tree_params from above.  Instead, just create the
2623fda4c61SEric Biggers 	 * fsverity_info from the fsverity_descriptor as if it were just loaded
2633fda4c61SEric Biggers 	 * from disk.  This is simpler, and it serves as an extra check that the
2643fda4c61SEric Biggers 	 * metadata we're writing is valid before actually enabling verity.
2653fda4c61SEric Biggers 	 */
266b0487edeSZhang Jianhua 	vi = fsverity_create_info(inode, desc);
2673fda4c61SEric Biggers 	if (IS_ERR(vi)) {
2683fda4c61SEric Biggers 		err = PTR_ERR(vi);
2693fda4c61SEric Biggers 		goto rollback;
2703fda4c61SEric Biggers 	}
2713fda4c61SEric Biggers 
2723fda4c61SEric Biggers 	/*
2733fda4c61SEric Biggers 	 * Tell the filesystem to finish enabling verity on the file.
2743fda4c61SEric Biggers 	 * Serialized with ->begin_enable_verity() by the inode lock.
2753fda4c61SEric Biggers 	 */
2763fda4c61SEric Biggers 	inode_lock(inode);
2773fda4c61SEric Biggers 	err = vops->end_enable_verity(filp, desc, desc_size, params.tree_size);
2783fda4c61SEric Biggers 	inode_unlock(inode);
2793fda4c61SEric Biggers 	if (err) {
2803fda4c61SEric Biggers 		fsverity_err(inode, "%ps() failed with err %d",
2813fda4c61SEric Biggers 			     vops->end_enable_verity, err);
2823fda4c61SEric Biggers 		fsverity_free_info(vi);
2838eb8af4bSEric Biggers 	} else if (WARN_ON_ONCE(!IS_VERITY(inode))) {
2843fda4c61SEric Biggers 		err = -EINVAL;
2853fda4c61SEric Biggers 		fsverity_free_info(vi);
2863fda4c61SEric Biggers 	} else {
2873fda4c61SEric Biggers 		/* Successfully enabled verity */
2883fda4c61SEric Biggers 
2893fda4c61SEric Biggers 		/*
2903fda4c61SEric Biggers 		 * Readers can start using ->i_verity_info immediately, so it
2913fda4c61SEric Biggers 		 * can't be rolled back once set.  So don't set it until just
2923fda4c61SEric Biggers 		 * after the filesystem has successfully enabled verity.
2933fda4c61SEric Biggers 		 */
2943fda4c61SEric Biggers 		fsverity_set_info(inode, vi);
2953fda4c61SEric Biggers 	}
2963fda4c61SEric Biggers out:
2973fda4c61SEric Biggers 	kfree(params.hashstate);
2983fda4c61SEric Biggers 	kfree(desc);
2993fda4c61SEric Biggers 	return err;
3003fda4c61SEric Biggers 
3013fda4c61SEric Biggers rollback:
3023fda4c61SEric Biggers 	inode_lock(inode);
3033fda4c61SEric Biggers 	(void)vops->end_enable_verity(filp, NULL, 0, params.tree_size);
3043fda4c61SEric Biggers 	inode_unlock(inode);
3053fda4c61SEric Biggers 	goto out;
3063fda4c61SEric Biggers }
3073fda4c61SEric Biggers 
3083fda4c61SEric Biggers /**
3093fda4c61SEric Biggers  * fsverity_ioctl_enable() - enable verity on a file
3106377a38bSEric Biggers  * @filp: file to enable verity on
3116377a38bSEric Biggers  * @uarg: user pointer to fsverity_enable_arg
3123fda4c61SEric Biggers  *
3133fda4c61SEric Biggers  * Enable fs-verity on a file.  See the "FS_IOC_ENABLE_VERITY" section of
3143fda4c61SEric Biggers  * Documentation/filesystems/fsverity.rst for the documentation.
3153fda4c61SEric Biggers  *
3163fda4c61SEric Biggers  * Return: 0 on success, -errno on failure
3173fda4c61SEric Biggers  */
fsverity_ioctl_enable(struct file * filp,const void __user * uarg)3183fda4c61SEric Biggers int fsverity_ioctl_enable(struct file *filp, const void __user *uarg)
3193fda4c61SEric Biggers {
3203fda4c61SEric Biggers 	struct inode *inode = file_inode(filp);
3213fda4c61SEric Biggers 	struct fsverity_enable_arg arg;
3223fda4c61SEric Biggers 	int err;
3233fda4c61SEric Biggers 
3243fda4c61SEric Biggers 	if (copy_from_user(&arg, uarg, sizeof(arg)))
3253fda4c61SEric Biggers 		return -EFAULT;
3263fda4c61SEric Biggers 
3273fda4c61SEric Biggers 	if (arg.version != 1)
3283fda4c61SEric Biggers 		return -EINVAL;
3293fda4c61SEric Biggers 
3303fda4c61SEric Biggers 	if (arg.__reserved1 ||
3313fda4c61SEric Biggers 	    memchr_inv(arg.__reserved2, 0, sizeof(arg.__reserved2)))
3323fda4c61SEric Biggers 		return -EINVAL;
3333fda4c61SEric Biggers 
33456124d6cSEric Biggers 	if (!is_power_of_2(arg.block_size))
3353fda4c61SEric Biggers 		return -EINVAL;
3363fda4c61SEric Biggers 
337c593642cSPankaj Bharadiya 	if (arg.salt_size > sizeof_field(struct fsverity_descriptor, salt))
3383fda4c61SEric Biggers 		return -EMSGSIZE;
3393fda4c61SEric Biggers 
340432434c9SEric Biggers 	if (arg.sig_size > FS_VERITY_MAX_SIGNATURE_SIZE)
341432434c9SEric Biggers 		return -EMSGSIZE;
3423fda4c61SEric Biggers 
3433fda4c61SEric Biggers 	/*
3443fda4c61SEric Biggers 	 * Require a regular file with write access.  But the actual fd must
3453fda4c61SEric Biggers 	 * still be readonly so that we can lock out all writers.  This is
3463fda4c61SEric Biggers 	 * needed to guarantee that no writable fds exist to the file once it
3473fda4c61SEric Biggers 	 * has verity enabled, and to stabilize the data being hashed.
3483fda4c61SEric Biggers 	 */
3493fda4c61SEric Biggers 
35002f92b38SChristian Brauner 	err = file_permission(filp, MAY_WRITE);
3513fda4c61SEric Biggers 	if (err)
3523fda4c61SEric Biggers 		return err;
35304839139SEric Biggers 	/*
35404839139SEric Biggers 	 * __kernel_read() is used while building the Merkle tree.  So, we can't
35504839139SEric Biggers 	 * allow file descriptors that were opened for ioctl access only, using
35604839139SEric Biggers 	 * the special nonstandard access mode 3.  O_RDONLY only, please!
35704839139SEric Biggers 	 */
35804839139SEric Biggers 	if (!(filp->f_mode & FMODE_READ))
35904839139SEric Biggers 		return -EBADF;
3603fda4c61SEric Biggers 
3613fda4c61SEric Biggers 	if (IS_APPEND(inode))
3623fda4c61SEric Biggers 		return -EPERM;
3633fda4c61SEric Biggers 
3643fda4c61SEric Biggers 	if (S_ISDIR(inode->i_mode))
3653fda4c61SEric Biggers 		return -EISDIR;
3663fda4c61SEric Biggers 
3673fda4c61SEric Biggers 	if (!S_ISREG(inode->i_mode))
3683fda4c61SEric Biggers 		return -EINVAL;
3693fda4c61SEric Biggers 
3703fda4c61SEric Biggers 	err = mnt_want_write_file(filp);
3713fda4c61SEric Biggers 	if (err) /* -EROFS */
3723fda4c61SEric Biggers 		return err;
3733fda4c61SEric Biggers 
3743fda4c61SEric Biggers 	err = deny_write_access(filp);
3753fda4c61SEric Biggers 	if (err) /* -ETXTBSY */
3763fda4c61SEric Biggers 		goto out_drop_write;
3773fda4c61SEric Biggers 
3783fda4c61SEric Biggers 	err = enable_verity(filp, &arg);
3793fda4c61SEric Biggers 
3803fda4c61SEric Biggers 	/*
381a075bacdSEric Biggers 	 * We no longer drop the inode's pagecache after enabling verity.  This
382a075bacdSEric Biggers 	 * used to be done to try to avoid a race condition where pages could be
383a075bacdSEric Biggers 	 * evicted after being used in the Merkle tree construction, then
384a075bacdSEric Biggers 	 * re-instantiated by a concurrent read.  Such pages are unverified, and
385a075bacdSEric Biggers 	 * the backing storage could have filled them with different content, so
386a075bacdSEric Biggers 	 * they shouldn't be used to fulfill reads once verity is enabled.
387a075bacdSEric Biggers 	 *
388a075bacdSEric Biggers 	 * But, dropping the pagecache has a big performance impact, and it
389a075bacdSEric Biggers 	 * doesn't fully solve the race condition anyway.  So for those reasons,
390a075bacdSEric Biggers 	 * and also because this race condition isn't very important relatively
391a075bacdSEric Biggers 	 * speaking (especially for small-ish files, where the chance of a page
392a075bacdSEric Biggers 	 * being used, evicted, *and* re-instantiated all while enabling verity
393a075bacdSEric Biggers 	 * is quite small), we no longer drop the inode's pagecache.
3943fda4c61SEric Biggers 	 */
3953fda4c61SEric Biggers 
3963fda4c61SEric Biggers 	/*
3973fda4c61SEric Biggers 	 * allow_write_access() is needed to pair with deny_write_access().
3983fda4c61SEric Biggers 	 * Regardless, the filesystem won't allow writing to verity files.
3993fda4c61SEric Biggers 	 */
4003fda4c61SEric Biggers 	allow_write_access(filp);
4013fda4c61SEric Biggers out_drop_write:
4023fda4c61SEric Biggers 	mnt_drop_write_file(filp);
4033fda4c61SEric Biggers 	return err;
4043fda4c61SEric Biggers }
4053fda4c61SEric Biggers EXPORT_SYMBOL_GPL(fsverity_ioctl_enable);
406