xref: /linux/fs/verity/open.c (revision 4b65b859f55b036649a4525f09fa7c5bbbab384e) !
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Opening fs-verity files
4  *
5  * Copyright 2019 Google LLC
6  */
7 
8 #include "fsverity_private.h"
9 
10 #include <linux/export.h>
11 #include <linux/mm.h>
12 #include <linux/slab.h>
13 
14 static struct kmem_cache *fsverity_info_cachep;
15 
16 /**
17  * fsverity_init_merkle_tree_params() - initialize Merkle tree parameters
18  * @params: the parameters struct to initialize
19  * @inode: the inode for which the Merkle tree is being built
20  * @hash_algorithm: number of hash algorithm to use
21  * @log_blocksize: log base 2 of block size to use
22  * @salt: pointer to salt (optional)
23  * @salt_size: size of salt, possibly 0
24  *
25  * Validate the hash algorithm and block size, then compute the tree topology
26  * (num levels, num blocks in each level, etc.) and initialize @params.
27  *
28  * Return: 0 on success, -errno on failure
29  */
fsverity_init_merkle_tree_params(struct merkle_tree_params * params,const struct inode * inode,unsigned int hash_algorithm,unsigned int log_blocksize,const u8 * salt,size_t salt_size)30 int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
31 				     const struct inode *inode,
32 				     unsigned int hash_algorithm,
33 				     unsigned int log_blocksize,
34 				     const u8 *salt, size_t salt_size)
35 {
36 	const struct fsverity_hash_alg *hash_alg;
37 	int err;
38 	u64 blocks;
39 	u64 blocks_in_level[FS_VERITY_MAX_LEVELS];
40 	u64 offset;
41 	int level;
42 
43 	memset(params, 0, sizeof(*params));
44 
45 	hash_alg = fsverity_get_hash_alg(inode, hash_algorithm);
46 	if (!hash_alg)
47 		return -EINVAL;
48 	params->hash_alg = hash_alg;
49 	params->digest_size = hash_alg->digest_size;
50 
51 	if (salt_size) {
52 		params->hashstate =
53 			fsverity_prepare_hash_state(hash_alg, salt, salt_size);
54 		if (!params->hashstate) {
55 			err = -ENOMEM;
56 			goto out_err;
57 		}
58 	}
59 
60 	/*
61 	 * fs/verity/ directly assumes that the Merkle tree block size is a
62 	 * power of 2 less than or equal to PAGE_SIZE.  Another restriction
63 	 * arises from the interaction between fs/verity/ and the filesystems
64 	 * themselves: filesystems expect to be able to verify a single
65 	 * filesystem block of data at a time.  Therefore, the Merkle tree block
66 	 * size must also be less than or equal to the filesystem block size.
67 	 *
68 	 * The above are the only hard limitations, so in theory the Merkle tree
69 	 * block size could be as small as twice the digest size.  However,
70 	 * that's not useful, and it would result in some unusually deep and
71 	 * large Merkle trees.  So we currently require that the Merkle tree
72 	 * block size be at least 1024 bytes.  That's small enough to test the
73 	 * sub-page block case on systems with 4K pages, but not too small.
74 	 */
75 	if (log_blocksize < 10 || log_blocksize > PAGE_SHIFT ||
76 	    log_blocksize > inode->i_blkbits) {
77 		fsverity_warn(inode, "Unsupported log_blocksize: %u",
78 			      log_blocksize);
79 		err = -EINVAL;
80 		goto out_err;
81 	}
82 	params->log_blocksize = log_blocksize;
83 	params->block_size = 1 << log_blocksize;
84 	params->log_blocks_per_page = PAGE_SHIFT - log_blocksize;
85 	params->blocks_per_page = 1 << params->log_blocks_per_page;
86 
87 	if (WARN_ON_ONCE(!is_power_of_2(params->digest_size))) {
88 		err = -EINVAL;
89 		goto out_err;
90 	}
91 	if (params->block_size < 2 * params->digest_size) {
92 		fsverity_warn(inode,
93 			      "Merkle tree block size (%u) too small for hash algorithm \"%s\"",
94 			      params->block_size, hash_alg->name);
95 		err = -EINVAL;
96 		goto out_err;
97 	}
98 	params->log_digestsize = ilog2(params->digest_size);
99 	params->log_arity = log_blocksize - params->log_digestsize;
100 	params->hashes_per_block = 1 << params->log_arity;
101 
102 	/*
103 	 * Compute the number of levels in the Merkle tree and create a map from
104 	 * level to the starting block of that level.  Level 'num_levels - 1' is
105 	 * the root and is stored first.  Level 0 is the level directly "above"
106 	 * the data blocks and is stored last.
107 	 */
108 
109 	/* Compute number of levels and the number of blocks in each level */
110 	blocks = ((u64)inode->i_size + params->block_size - 1) >> log_blocksize;
111 	while (blocks > 1) {
112 		if (params->num_levels >= FS_VERITY_MAX_LEVELS) {
113 			fsverity_err(inode, "Too many levels in Merkle tree");
114 			err = -EFBIG;
115 			goto out_err;
116 		}
117 		blocks = (blocks + params->hashes_per_block - 1) >>
118 			 params->log_arity;
119 		blocks_in_level[params->num_levels++] = blocks;
120 	}
121 
122 	/* Compute the starting block of each level */
123 	offset = 0;
124 	for (level = (int)params->num_levels - 1; level >= 0; level--) {
125 		params->level_start[level] = offset;
126 		offset += blocks_in_level[level];
127 	}
128 
129 	/*
130 	 * With block_size != PAGE_SIZE, an in-memory bitmap will need to be
131 	 * allocated to track the "verified" status of hash blocks.  Don't allow
132 	 * this bitmap to get too large.  For now, limit it to 1 MiB, which
133 	 * limits the file size to about 4.4 TB with SHA-256 and 4K blocks.
134 	 *
135 	 * Together with the fact that the data, and thus also the Merkle tree,
136 	 * cannot have more than ULONG_MAX pages, this implies that hash block
137 	 * indices can always fit in an 'unsigned long'.  But to be safe, we
138 	 * explicitly check for that too.  Note, this is only for hash block
139 	 * indices; data block indices might not fit in an 'unsigned long'.
140 	 */
141 	if ((params->block_size != PAGE_SIZE && offset > 1 << 23) ||
142 	    offset > ULONG_MAX) {
143 		fsverity_err(inode, "Too many blocks in Merkle tree");
144 		err = -EFBIG;
145 		goto out_err;
146 	}
147 
148 	params->tree_size = offset << log_blocksize;
149 	params->tree_pages = PAGE_ALIGN(params->tree_size) >> PAGE_SHIFT;
150 	return 0;
151 
152 out_err:
153 	kfree(params->hashstate);
154 	memset(params, 0, sizeof(*params));
155 	return err;
156 }
157 
158 /*
159  * Compute the file digest by hashing the fsverity_descriptor excluding the
160  * builtin signature and with the sig_size field set to 0.
161  */
compute_file_digest(const struct fsverity_hash_alg * hash_alg,struct fsverity_descriptor * desc,u8 * file_digest)162 static void compute_file_digest(const struct fsverity_hash_alg *hash_alg,
163 				struct fsverity_descriptor *desc,
164 				u8 *file_digest)
165 {
166 	__le32 sig_size = desc->sig_size;
167 
168 	desc->sig_size = 0;
169 	fsverity_hash_buffer(hash_alg, desc, sizeof(*desc), file_digest);
170 	desc->sig_size = sig_size;
171 }
172 
173 /*
174  * Create a new fsverity_info from the given fsverity_descriptor (with optional
175  * appended builtin signature), and check the signature if present.  The
176  * fsverity_descriptor must have already undergone basic validation.
177  */
fsverity_create_info(const struct inode * inode,struct fsverity_descriptor * desc)178 struct fsverity_info *fsverity_create_info(const struct inode *inode,
179 					   struct fsverity_descriptor *desc)
180 {
181 	struct fsverity_info *vi;
182 	int err;
183 
184 	vi = kmem_cache_zalloc(fsverity_info_cachep, GFP_KERNEL);
185 	if (!vi)
186 		return ERR_PTR(-ENOMEM);
187 	vi->inode = inode;
188 
189 	err = fsverity_init_merkle_tree_params(&vi->tree_params, inode,
190 					       desc->hash_algorithm,
191 					       desc->log_blocksize,
192 					       desc->salt, desc->salt_size);
193 	if (err) {
194 		fsverity_err(inode,
195 			     "Error %d initializing Merkle tree parameters",
196 			     err);
197 		goto fail;
198 	}
199 
200 	memcpy(vi->root_hash, desc->root_hash, vi->tree_params.digest_size);
201 
202 	compute_file_digest(vi->tree_params.hash_alg, desc, vi->file_digest);
203 
204 	err = fsverity_verify_signature(vi, desc->signature,
205 					le32_to_cpu(desc->sig_size));
206 	if (err)
207 		goto fail;
208 
209 	if (vi->tree_params.block_size != PAGE_SIZE) {
210 		/*
211 		 * When the Merkle tree block size and page size differ, we use
212 		 * a bitmap to keep track of which hash blocks have been
213 		 * verified.  This bitmap must contain one bit per hash block,
214 		 * including alignment to a page boundary at the end.
215 		 *
216 		 * Eventually, to support extremely large files in an efficient
217 		 * way, it might be necessary to make pages of this bitmap
218 		 * reclaimable.  But for now, simply allocating the whole bitmap
219 		 * is a simple solution that works well on the files on which
220 		 * fsverity is realistically used.  E.g., with SHA-256 and 4K
221 		 * blocks, a 100MB file only needs a 24-byte bitmap, and the
222 		 * bitmap for any file under 17GB fits in a 4K page.
223 		 */
224 		unsigned long num_bits =
225 			vi->tree_params.tree_pages <<
226 			vi->tree_params.log_blocks_per_page;
227 
228 		vi->hash_block_verified = kvcalloc(BITS_TO_LONGS(num_bits),
229 						   sizeof(unsigned long),
230 						   GFP_KERNEL);
231 		if (!vi->hash_block_verified) {
232 			err = -ENOMEM;
233 			goto fail;
234 		}
235 	}
236 
237 	return vi;
238 
239 fail:
240 	fsverity_free_info(vi);
241 	return ERR_PTR(err);
242 }
243 
fsverity_set_info(struct inode * inode,struct fsverity_info * vi)244 void fsverity_set_info(struct inode *inode, struct fsverity_info *vi)
245 {
246 	/*
247 	 * Multiple tasks may race to set ->i_verity_info, so use
248 	 * cmpxchg_release().  This pairs with the smp_load_acquire() in
249 	 * fsverity_get_info().  I.e., here we publish ->i_verity_info with a
250 	 * RELEASE barrier so that other tasks can ACQUIRE it.
251 	 */
252 	if (cmpxchg_release(&inode->i_verity_info, NULL, vi) != NULL) {
253 		/* Lost the race, so free the fsverity_info we allocated. */
254 		fsverity_free_info(vi);
255 		/*
256 		 * Afterwards, the caller may access ->i_verity_info directly,
257 		 * so make sure to ACQUIRE the winning fsverity_info.
258 		 */
259 		(void)fsverity_get_info(inode);
260 	}
261 }
262 
fsverity_free_info(struct fsverity_info * vi)263 void fsverity_free_info(struct fsverity_info *vi)
264 {
265 	if (!vi)
266 		return;
267 	kfree(vi->tree_params.hashstate);
268 	kvfree(vi->hash_block_verified);
269 	kmem_cache_free(fsverity_info_cachep, vi);
270 }
271 
validate_fsverity_descriptor(struct inode * inode,const struct fsverity_descriptor * desc,size_t desc_size)272 static bool validate_fsverity_descriptor(struct inode *inode,
273 					 const struct fsverity_descriptor *desc,
274 					 size_t desc_size)
275 {
276 	if (desc_size < sizeof(*desc)) {
277 		fsverity_err(inode, "Unrecognized descriptor size: %zu bytes",
278 			     desc_size);
279 		return false;
280 	}
281 
282 	if (desc->version != 1) {
283 		fsverity_err(inode, "Unrecognized descriptor version: %u",
284 			     desc->version);
285 		return false;
286 	}
287 
288 	if (memchr_inv(desc->__reserved, 0, sizeof(desc->__reserved))) {
289 		fsverity_err(inode, "Reserved bits set in descriptor");
290 		return false;
291 	}
292 
293 	if (desc->salt_size > sizeof(desc->salt)) {
294 		fsverity_err(inode, "Invalid salt_size: %u", desc->salt_size);
295 		return false;
296 	}
297 
298 	if (le64_to_cpu(desc->data_size) != inode->i_size) {
299 		fsverity_err(inode,
300 			     "Wrong data_size: %llu (desc) != %lld (inode)",
301 			     le64_to_cpu(desc->data_size), inode->i_size);
302 		return false;
303 	}
304 
305 	if (le32_to_cpu(desc->sig_size) > desc_size - sizeof(*desc)) {
306 		fsverity_err(inode, "Signature overflows verity descriptor");
307 		return false;
308 	}
309 
310 	return true;
311 }
312 
313 /*
314  * Read the inode's fsverity_descriptor (with optional appended builtin
315  * signature) from the filesystem, and do basic validation of it.
316  */
fsverity_get_descriptor(struct inode * inode,struct fsverity_descriptor ** desc_ret)317 int fsverity_get_descriptor(struct inode *inode,
318 			    struct fsverity_descriptor **desc_ret)
319 {
320 	int res;
321 	struct fsverity_descriptor *desc;
322 
323 	res = inode->i_sb->s_vop->get_verity_descriptor(inode, NULL, 0);
324 	if (res < 0) {
325 		fsverity_err(inode,
326 			     "Error %d getting verity descriptor size", res);
327 		return res;
328 	}
329 	if (res > FS_VERITY_MAX_DESCRIPTOR_SIZE) {
330 		fsverity_err(inode, "Verity descriptor is too large (%d bytes)",
331 			     res);
332 		return -EMSGSIZE;
333 	}
334 	desc = kmalloc(res, GFP_KERNEL);
335 	if (!desc)
336 		return -ENOMEM;
337 	res = inode->i_sb->s_vop->get_verity_descriptor(inode, desc, res);
338 	if (res < 0) {
339 		fsverity_err(inode, "Error %d reading verity descriptor", res);
340 		kfree(desc);
341 		return res;
342 	}
343 
344 	if (!validate_fsverity_descriptor(inode, desc, res)) {
345 		kfree(desc);
346 		return -EINVAL;
347 	}
348 
349 	*desc_ret = desc;
350 	return 0;
351 }
352 
353 /* Ensure the inode has an ->i_verity_info */
ensure_verity_info(struct inode * inode)354 static int ensure_verity_info(struct inode *inode)
355 {
356 	struct fsverity_info *vi = fsverity_get_info(inode);
357 	struct fsverity_descriptor *desc;
358 	int err;
359 
360 	if (vi)
361 		return 0;
362 
363 	err = fsverity_get_descriptor(inode, &desc);
364 	if (err)
365 		return err;
366 
367 	vi = fsverity_create_info(inode, desc);
368 	if (IS_ERR(vi)) {
369 		err = PTR_ERR(vi);
370 		goto out_free_desc;
371 	}
372 
373 	fsverity_set_info(inode, vi);
374 	err = 0;
375 out_free_desc:
376 	kfree(desc);
377 	return err;
378 }
379 
__fsverity_file_open(struct inode * inode,struct file * filp)380 int __fsverity_file_open(struct inode *inode, struct file *filp)
381 {
382 	if (filp->f_mode & FMODE_WRITE)
383 		return -EPERM;
384 	return ensure_verity_info(inode);
385 }
386 EXPORT_SYMBOL_GPL(__fsverity_file_open);
387 
__fsverity_prepare_setattr(struct dentry * dentry,struct iattr * attr)388 int __fsverity_prepare_setattr(struct dentry *dentry, struct iattr *attr)
389 {
390 	if (attr->ia_valid & ATTR_SIZE)
391 		return -EPERM;
392 	return 0;
393 }
394 EXPORT_SYMBOL_GPL(__fsverity_prepare_setattr);
395 
__fsverity_cleanup_inode(struct inode * inode)396 void __fsverity_cleanup_inode(struct inode *inode)
397 {
398 	fsverity_free_info(inode->i_verity_info);
399 	inode->i_verity_info = NULL;
400 }
401 EXPORT_SYMBOL_GPL(__fsverity_cleanup_inode);
402 
fsverity_init_info_cache(void)403 void __init fsverity_init_info_cache(void)
404 {
405 	fsverity_info_cachep = KMEM_CACHE_USERCOPY(
406 					fsverity_info,
407 					SLAB_RECLAIM_ACCOUNT | SLAB_PANIC,
408 					file_digest);
409 }
410