VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700   1) // SPDX-License-Identifier: GPL-2.0
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700   2) /*
7bf765dd8442d (Eric Biggers      2020-11-13 13:19:15 -0800   3)  * Ioctl to enable verity on a file
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700   4)  *
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700   5)  * Copyright 2019 Google LLC
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700   6)  */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700   7) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700   8) #include "fsverity_private.h"
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700   9) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  10) #include <crypto/hash.h>
fd39073dba863 (Eric Biggers      2020-01-06 12:55:33 -0800  11) #include <linux/backing-dev.h>
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  12) #include <linux/mount.h>
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  13) #include <linux/pagemap.h>
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  14) #include <linux/sched/signal.h>
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  15) #include <linux/uaccess.h>
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  16) 
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  17) /*
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  18)  * Read a file data page for Merkle tree construction.  Do aggressive readahead,
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  19)  * since we're sequentially reading the entire file.
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  20)  */
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  21) static struct page *read_file_data_page(struct file *filp, pgoff_t index,
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  22) 					struct file_ra_state *ra,
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  23) 					unsigned long remaining_pages)
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  24) {
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  25) 	struct page *page;
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  26) 
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  27) 	page = find_get_page_flags(filp->f_mapping, index, FGP_ACCESSED);
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  28) 	if (!page || !PageUptodate(page)) {
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  29) 		if (page)
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  30) 			put_page(page);
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  31) 		else
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  32) 			page_cache_sync_readahead(filp->f_mapping, ra, filp,
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  33) 						  index, remaining_pages);
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  34) 		page = read_mapping_page(filp->f_mapping, index, NULL);
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  35) 		if (IS_ERR(page))
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  36) 			return page;
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  37) 	}
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  38) 	if (PageReadahead(page))
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  39) 		page_cache_async_readahead(filp->f_mapping, ra, filp, page,
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  40) 					   index, remaining_pages);
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  41) 	return page;
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  42) }
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  43) 
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  44) static int build_merkle_tree_level(struct file *filp, unsigned int level,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  45) 				   u64 num_blocks_to_hash,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  46) 				   const struct merkle_tree_params *params,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  47) 				   u8 *pending_hashes,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  48) 				   struct ahash_request *req)
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  49) {
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  50) 	struct inode *inode = file_inode(filp);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  51) 	const struct fsverity_operations *vops = inode->i_sb->s_vop;
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  52) 	struct file_ra_state ra = { 0 };
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  53) 	unsigned int pending_size = 0;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  54) 	u64 dst_block_num;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  55) 	u64 i;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  56) 	int err;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  57) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  58) 	if (WARN_ON(params->block_size != PAGE_SIZE)) /* checked earlier too */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  59) 		return -EINVAL;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  60) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  61) 	if (level < params->num_levels) {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  62) 		dst_block_num = params->level_start[level];
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  63) 	} else {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  64) 		if (WARN_ON(num_blocks_to_hash != 1))
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  65) 			return -EINVAL;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  66) 		dst_block_num = 0; /* unused */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  67) 	}
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  68) 
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  69) 	file_ra_state_init(&ra, filp->f_mapping);
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  70) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  71) 	for (i = 0; i < num_blocks_to_hash; i++) {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  72) 		struct page *src_page;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  73) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  74) 		if ((pgoff_t)i % 10000 == 0 || i + 1 == num_blocks_to_hash)
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  75) 			pr_debug("Hashing block %llu of %llu for level %u\n",
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  76) 				 i + 1, num_blocks_to_hash, level);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  77) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  78) 		if (level == 0) {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  79) 			/* Leaf: hashing a data block */
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  80) 			src_page = read_file_data_page(filp, i, &ra,
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800  81) 						       num_blocks_to_hash - i);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  82) 			if (IS_ERR(src_page)) {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  83) 				err = PTR_ERR(src_page);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  84) 				fsverity_err(inode,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  85) 					     "Error %d reading data page %llu",
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  86) 					     err, i);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  87) 				return err;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  88) 			}
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  89) 		} else {
fd39073dba863 (Eric Biggers      2020-01-06 12:55:33 -0800  90) 			unsigned long num_ra_pages =
fd39073dba863 (Eric Biggers      2020-01-06 12:55:33 -0800  91) 				min_t(unsigned long, num_blocks_to_hash - i,
fd39073dba863 (Eric Biggers      2020-01-06 12:55:33 -0800  92) 				      inode->i_sb->s_bdi->io_pages);
fd39073dba863 (Eric Biggers      2020-01-06 12:55:33 -0800  93) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  94) 			/* Non-leaf: hashing hash block from level below */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  95) 			src_page = vops->read_merkle_tree_page(inode,
fd39073dba863 (Eric Biggers      2020-01-06 12:55:33 -0800  96) 					params->level_start[level - 1] + i,
fd39073dba863 (Eric Biggers      2020-01-06 12:55:33 -0800  97) 					num_ra_pages);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  98) 			if (IS_ERR(src_page)) {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700  99) 				err = PTR_ERR(src_page);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 100) 				fsverity_err(inode,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 101) 					     "Error %d reading Merkle tree page %llu",
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 102) 					     err, params->level_start[level - 1] + i);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 103) 				return err;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 104) 			}
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 105) 		}
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 106) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 107) 		err = fsverity_hash_page(params, inode, req, src_page,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 108) 					 &pending_hashes[pending_size]);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 109) 		put_page(src_page);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 110) 		if (err)
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 111) 			return err;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 112) 		pending_size += params->digest_size;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 113) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 114) 		if (level == params->num_levels) /* Root hash? */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 115) 			return 0;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 116) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 117) 		if (pending_size + params->digest_size > params->block_size ||
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 118) 		    i + 1 == num_blocks_to_hash) {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 119) 			/* Flush the pending hash block */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 120) 			memset(&pending_hashes[pending_size], 0,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 121) 			       params->block_size - pending_size);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 122) 			err = vops->write_merkle_tree_block(inode,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 123) 					pending_hashes,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 124) 					dst_block_num,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 125) 					params->log_blocksize);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 126) 			if (err) {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 127) 				fsverity_err(inode,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 128) 					     "Error %d writing Merkle tree block %llu",
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 129) 					     err, dst_block_num);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 130) 				return err;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 131) 			}
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 132) 			dst_block_num++;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 133) 			pending_size = 0;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 134) 		}
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 135) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 136) 		if (fatal_signal_pending(current))
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 137) 			return -EINTR;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 138) 		cond_resched();
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 139) 	}
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 140) 	return 0;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 141) }
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 142) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 143) /*
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800 144)  * Build the Merkle tree for the given file using the given parameters, and
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 145)  * return the root hash in @root_hash.
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 146)  *
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 147)  * The tree is written to a filesystem-specific location as determined by the
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 148)  * ->write_merkle_tree_block() method.  However, the blocks that comprise the
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 149)  * tree are the same for all filesystems.
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 150)  */
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800 151) static int build_merkle_tree(struct file *filp,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 152) 			     const struct merkle_tree_params *params,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 153) 			     u8 *root_hash)
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 154) {
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800 155) 	struct inode *inode = file_inode(filp);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 156) 	u8 *pending_hashes;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 157) 	struct ahash_request *req;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 158) 	u64 blocks;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 159) 	unsigned int level;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 160) 	int err = -ENOMEM;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 161) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 162) 	if (inode->i_size == 0) {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 163) 		/* Empty file is a special case; root hash is all 0's */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 164) 		memset(root_hash, 0, params->digest_size);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 165) 		return 0;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 166) 	}
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 167) 
439bea104c3d2 (Eric Biggers      2019-12-31 11:55:45 -0600 168) 	/* This allocation never fails, since it's mempool-backed. */
439bea104c3d2 (Eric Biggers      2019-12-31 11:55:45 -0600 169) 	req = fsverity_alloc_hash_request(params->hash_alg, GFP_KERNEL);
439bea104c3d2 (Eric Biggers      2019-12-31 11:55:45 -0600 170) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 171) 	pending_hashes = kmalloc(params->block_size, GFP_KERNEL);
439bea104c3d2 (Eric Biggers      2019-12-31 11:55:45 -0600 172) 	if (!pending_hashes)
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 173) 		goto out;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 174) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 175) 	/*
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 176) 	 * Build each level of the Merkle tree, starting at the leaf level
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 177) 	 * (level 0) and ascending to the root node (level 'num_levels - 1').
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 178) 	 * Then at the end (level 'num_levels'), calculate the root hash.
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 179) 	 */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 180) 	blocks = (inode->i_size + params->block_size - 1) >>
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 181) 		 params->log_blocksize;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 182) 	for (level = 0; level <= params->num_levels; level++) {
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800 183) 		err = build_merkle_tree_level(filp, level, blocks, params,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 184) 					      pending_hashes, req);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 185) 		if (err)
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 186) 			goto out;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 187) 		blocks = (blocks + params->hashes_per_block - 1) >>
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 188) 			 params->log_arity;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 189) 	}
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 190) 	memcpy(root_hash, pending_hashes, params->digest_size);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 191) 	err = 0;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 192) out:
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 193) 	kfree(pending_hashes);
439bea104c3d2 (Eric Biggers      2019-12-31 11:55:45 -0600 194) 	fsverity_free_hash_request(params->hash_alg, req);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 195) 	return err;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 196) }
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 197) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 198) static int enable_verity(struct file *filp,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 199) 			 const struct fsverity_enable_arg *arg)
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 200) {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 201) 	struct inode *inode = file_inode(filp);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 202) 	const struct fsverity_operations *vops = inode->i_sb->s_vop;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 203) 	struct merkle_tree_params params = { };
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 204) 	struct fsverity_descriptor *desc;
432434c9f8e18 (Eric Biggers      2019-07-22 09:26:23 -0700 205) 	size_t desc_size = sizeof(*desc) + arg->sig_size;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 206) 	struct fsverity_info *vi;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 207) 	int err;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 208) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 209) 	/* Start initializing the fsverity_descriptor */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 210) 	desc = kzalloc(desc_size, GFP_KERNEL);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 211) 	if (!desc)
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 212) 		return -ENOMEM;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 213) 	desc->version = 1;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 214) 	desc->hash_algorithm = arg->hash_algorithm;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 215) 	desc->log_blocksize = ilog2(arg->block_size);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 216) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 217) 	/* Get the salt if the user provided one */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 218) 	if (arg->salt_size &&
da3a3da4e6c68 (Eric Biggers      2019-12-31 11:54:08 -0600 219) 	    copy_from_user(desc->salt, u64_to_user_ptr(arg->salt_ptr),
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 220) 			   arg->salt_size)) {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 221) 		err = -EFAULT;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 222) 		goto out;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 223) 	}
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 224) 	desc->salt_size = arg->salt_size;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 225) 
432434c9f8e18 (Eric Biggers      2019-07-22 09:26:23 -0700 226) 	/* Get the signature if the user provided one */
432434c9f8e18 (Eric Biggers      2019-07-22 09:26:23 -0700 227) 	if (arg->sig_size &&
da3a3da4e6c68 (Eric Biggers      2019-12-31 11:54:08 -0600 228) 	    copy_from_user(desc->signature, u64_to_user_ptr(arg->sig_ptr),
432434c9f8e18 (Eric Biggers      2019-07-22 09:26:23 -0700 229) 			   arg->sig_size)) {
432434c9f8e18 (Eric Biggers      2019-07-22 09:26:23 -0700 230) 		err = -EFAULT;
432434c9f8e18 (Eric Biggers      2019-07-22 09:26:23 -0700 231) 		goto out;
432434c9f8e18 (Eric Biggers      2019-07-22 09:26:23 -0700 232) 	}
432434c9f8e18 (Eric Biggers      2019-07-22 09:26:23 -0700 233) 	desc->sig_size = cpu_to_le32(arg->sig_size);
432434c9f8e18 (Eric Biggers      2019-07-22 09:26:23 -0700 234) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 235) 	desc->data_size = cpu_to_le64(inode->i_size);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 236) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 237) 	/* Prepare the Merkle tree parameters */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 238) 	err = fsverity_init_merkle_tree_params(&params, inode,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 239) 					       arg->hash_algorithm,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 240) 					       desc->log_blocksize,
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 241) 					       desc->salt, desc->salt_size);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 242) 	if (err)
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 243) 		goto out;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 244) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 245) 	/*
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 246) 	 * Start enabling verity on this file, serialized by the inode lock.
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 247) 	 * Fail if verity is already enabled or is already being enabled.
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 248) 	 */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 249) 	inode_lock(inode);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 250) 	if (IS_VERITY(inode))
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 251) 		err = -EEXIST;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 252) 	else
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 253) 		err = vops->begin_enable_verity(filp);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 254) 	inode_unlock(inode);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 255) 	if (err)
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 256) 		goto out;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 257) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 258) 	/*
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 259) 	 * Build the Merkle tree.  Don't hold the inode lock during this, since
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 260) 	 * on huge files this may take a very long time and we don't want to
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 261) 	 * force unrelated syscalls like chown() to block forever.  We don't
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 262) 	 * need the inode lock here because deny_write_access() already prevents
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 263) 	 * the file from being written to or truncated, and we still serialize
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 264) 	 * ->begin_enable_verity() and ->end_enable_verity() using the inode
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 265) 	 * lock and only allow one process to be here at a time on a given file.
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 266) 	 */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 267) 	pr_debug("Building Merkle tree...\n");
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 268) 	BUILD_BUG_ON(sizeof(desc->root_hash) < FS_VERITY_MAX_DIGEST_SIZE);
c22415d333fba (Eric Biggers      2020-01-06 12:54:10 -0800 269) 	err = build_merkle_tree(filp, &params, desc->root_hash);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 270) 	if (err) {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 271) 		fsverity_err(inode, "Error %d building Merkle tree", err);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 272) 		goto rollback;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 273) 	}
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 274) 	pr_debug("Done building Merkle tree.  Root hash is %s:%*phN\n",
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 275) 		 params.hash_alg->name, params.digest_size, desc->root_hash);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 276) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 277) 	/*
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 278) 	 * Create the fsverity_info.  Don't bother trying to save work by
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 279) 	 * reusing the merkle_tree_params from above.  Instead, just create the
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 280) 	 * fsverity_info from the fsverity_descriptor as if it were just loaded
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 281) 	 * from disk.  This is simpler, and it serves as an extra check that the
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 282) 	 * metadata we're writing is valid before actually enabling verity.
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 283) 	 */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 284) 	vi = fsverity_create_info(inode, desc, desc_size);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 285) 	if (IS_ERR(vi)) {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 286) 		err = PTR_ERR(vi);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 287) 		goto rollback;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 288) 	}
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 289) 
432434c9f8e18 (Eric Biggers      2019-07-22 09:26:23 -0700 290) 	if (arg->sig_size)
432434c9f8e18 (Eric Biggers      2019-07-22 09:26:23 -0700 291) 		pr_debug("Storing a %u-byte PKCS#7 signature alongside the file\n",
432434c9f8e18 (Eric Biggers      2019-07-22 09:26:23 -0700 292) 			 arg->sig_size);
432434c9f8e18 (Eric Biggers      2019-07-22 09:26:23 -0700 293) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 294) 	/*
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 295) 	 * Tell the filesystem to finish enabling verity on the file.
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 296) 	 * Serialized with ->begin_enable_verity() by the inode lock.
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 297) 	 */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 298) 	inode_lock(inode);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 299) 	err = vops->end_enable_verity(filp, desc, desc_size, params.tree_size);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 300) 	inode_unlock(inode);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 301) 	if (err) {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 302) 		fsverity_err(inode, "%ps() failed with err %d",
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 303) 			     vops->end_enable_verity, err);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 304) 		fsverity_free_info(vi);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 305) 	} else if (WARN_ON(!IS_VERITY(inode))) {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 306) 		err = -EINVAL;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 307) 		fsverity_free_info(vi);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 308) 	} else {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 309) 		/* Successfully enabled verity */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 310) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 311) 		/*
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 312) 		 * Readers can start using ->i_verity_info immediately, so it
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 313) 		 * can't be rolled back once set.  So don't set it until just
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 314) 		 * after the filesystem has successfully enabled verity.
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 315) 		 */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 316) 		fsverity_set_info(inode, vi);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 317) 	}
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 318) out:
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 319) 	kfree(params.hashstate);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 320) 	kfree(desc);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 321) 	return err;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 322) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 323) rollback:
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 324) 	inode_lock(inode);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 325) 	(void)vops->end_enable_verity(filp, NULL, 0, params.tree_size);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 326) 	inode_unlock(inode);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 327) 	goto out;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 328) }
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 329) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 330) /**
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 331)  * fsverity_ioctl_enable() - enable verity on a file
6377a38bd345b (Eric Biggers      2020-05-11 12:21:17 -0700 332)  * @filp: file to enable verity on
6377a38bd345b (Eric Biggers      2020-05-11 12:21:17 -0700 333)  * @uarg: user pointer to fsverity_enable_arg
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 334)  *
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 335)  * Enable fs-verity on a file.  See the "FS_IOC_ENABLE_VERITY" section of
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 336)  * Documentation/filesystems/fsverity.rst for the documentation.
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 337)  *
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 338)  * Return: 0 on success, -errno on failure
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 339)  */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 340) int fsverity_ioctl_enable(struct file *filp, const void __user *uarg)
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 341) {
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 342) 	struct inode *inode = file_inode(filp);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 343) 	struct fsverity_enable_arg arg;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 344) 	int err;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 345) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 346) 	if (copy_from_user(&arg, uarg, sizeof(arg)))
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 347) 		return -EFAULT;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 348) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 349) 	if (arg.version != 1)
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 350) 		return -EINVAL;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 351) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 352) 	if (arg.__reserved1 ||
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 353) 	    memchr_inv(arg.__reserved2, 0, sizeof(arg.__reserved2)))
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 354) 		return -EINVAL;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 355) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 356) 	if (arg.block_size != PAGE_SIZE)
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 357) 		return -EINVAL;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 358) 
c593642c8be04 (Pankaj Bharadiya  2019-12-09 10:31:43 -0800 359) 	if (arg.salt_size > sizeof_field(struct fsverity_descriptor, salt))
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 360) 		return -EMSGSIZE;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 361) 
432434c9f8e18 (Eric Biggers      2019-07-22 09:26:23 -0700 362) 	if (arg.sig_size > FS_VERITY_MAX_SIGNATURE_SIZE)
432434c9f8e18 (Eric Biggers      2019-07-22 09:26:23 -0700 363) 		return -EMSGSIZE;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 364) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 365) 	/*
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 366) 	 * Require a regular file with write access.  But the actual fd must
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 367) 	 * still be readonly so that we can lock out all writers.  This is
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 368) 	 * needed to guarantee that no writable fds exist to the file once it
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 369) 	 * has verity enabled, and to stabilize the data being hashed.
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 370) 	 */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 371) 
02f92b3868a1b (Christian Brauner 2021-01-21 14:19:22 +0100 372) 	err = file_permission(filp, MAY_WRITE);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 373) 	if (err)
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 374) 		return err;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 375) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 376) 	if (IS_APPEND(inode))
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 377) 		return -EPERM;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 378) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 379) 	if (S_ISDIR(inode->i_mode))
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 380) 		return -EISDIR;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 381) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 382) 	if (!S_ISREG(inode->i_mode))
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 383) 		return -EINVAL;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 384) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 385) 	err = mnt_want_write_file(filp);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 386) 	if (err) /* -EROFS */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 387) 		return err;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 388) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 389) 	err = deny_write_access(filp);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 390) 	if (err) /* -ETXTBSY */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 391) 		goto out_drop_write;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 392) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 393) 	err = enable_verity(filp, &arg);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 394) 	if (err)
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 395) 		goto out_allow_write_access;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 396) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 397) 	/*
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 398) 	 * Some pages of the file may have been evicted from pagecache after
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 399) 	 * being used in the Merkle tree construction, then read into pagecache
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 400) 	 * again by another process reading from the file concurrently.  Since
ed45e20164934 (Eric Biggers      2020-11-13 13:19:17 -0800 401) 	 * these pages didn't undergo verification against the file digest which
ed45e20164934 (Eric Biggers      2020-11-13 13:19:17 -0800 402) 	 * fs-verity now claims to be enforcing, we have to wipe the pagecache
ed45e20164934 (Eric Biggers      2020-11-13 13:19:17 -0800 403) 	 * to ensure that all future reads are verified.
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 404) 	 */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 405) 	filemap_write_and_wait(inode->i_mapping);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 406) 	invalidate_inode_pages2(inode->i_mapping);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 407) 
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 408) 	/*
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 409) 	 * allow_write_access() is needed to pair with deny_write_access().
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 410) 	 * Regardless, the filesystem won't allow writing to verity files.
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 411) 	 */
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 412) out_allow_write_access:
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 413) 	allow_write_access(filp);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 414) out_drop_write:
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 415) 	mnt_drop_write_file(filp);
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 416) 	return err;
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 417) }
3fda4c617e84c (Eric Biggers      2019-07-22 09:26:22 -0700 418) EXPORT_SYMBOL_GPL(fsverity_ioctl_enable);