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