xref: /linux/fs/verity/open.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
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 	params->tree_size = offset << log_blocksize;
157 	params->tree_pages = PAGE_ALIGN(params->tree_size) >> PAGE_SHIFT;
158 	return 0;
159 
160 out_err:
161 	kfree(params->hashstate);
162 	memset(params, 0, sizeof(*params));
163 	return err;
164 }
165 
166 /*
167  * Compute the file digest by hashing the fsverity_descriptor excluding the
168  * builtin signature and with the sig_size field set to 0.
169  */
170 static void compute_file_digest(const struct fsverity_hash_alg *hash_alg,
171 				struct fsverity_descriptor *desc,
172 				u8 *file_digest)
173 {
174 	__le32 sig_size = desc->sig_size;
175 
176 	desc->sig_size = 0;
177 	fsverity_hash_buffer(hash_alg, desc, sizeof(*desc), file_digest);
178 	desc->sig_size = sig_size;
179 }
180 
181 /*
182  * Create a new fsverity_info from the given fsverity_descriptor (with optional
183  * appended builtin signature), and check the signature if present.  The
184  * fsverity_descriptor must have already undergone basic validation.
185  */
186 struct fsverity_info *fsverity_create_info(struct inode *inode,
187 					   struct fsverity_descriptor *desc)
188 {
189 	struct fsverity_info *vi;
190 	int err;
191 
192 	vi = kmem_cache_zalloc(fsverity_info_cachep, GFP_KERNEL);
193 	if (!vi)
194 		return ERR_PTR(-ENOMEM);
195 	vi->inode = inode;
196 
197 	err = fsverity_init_merkle_tree_params(&vi->tree_params, inode,
198 					       desc->hash_algorithm,
199 					       desc->log_blocksize,
200 					       desc->salt, desc->salt_size);
201 	if (err) {
202 		fsverity_err(inode,
203 			     "Error %d initializing Merkle tree parameters",
204 			     err);
205 		goto fail;
206 	}
207 
208 	memcpy(vi->root_hash, desc->root_hash, vi->tree_params.digest_size);
209 
210 	compute_file_digest(vi->tree_params.hash_alg, desc, vi->file_digest);
211 
212 	err = fsverity_verify_signature(vi, desc->signature,
213 					le32_to_cpu(desc->sig_size));
214 	if (err)
215 		goto fail;
216 
217 	if (vi->tree_params.block_size != PAGE_SIZE) {
218 		/*
219 		 * When the Merkle tree block size and page size differ, we use
220 		 * a bitmap to keep track of which hash blocks have been
221 		 * verified.  This bitmap must contain one bit per hash block,
222 		 * including alignment to a page boundary at the end.
223 		 *
224 		 * Eventually, to support extremely large files in an efficient
225 		 * way, it might be necessary to make pages of this bitmap
226 		 * reclaimable.  But for now, simply allocating the whole bitmap
227 		 * is a simple solution that works well on the files on which
228 		 * fsverity is realistically used.  E.g., with SHA-256 and 4K
229 		 * blocks, a 100MB file only needs a 24-byte bitmap, and the
230 		 * bitmap for any file under 17GB fits in a 4K page.
231 		 */
232 		unsigned long num_bits =
233 			vi->tree_params.tree_pages <<
234 			vi->tree_params.log_blocks_per_page;
235 
236 		vi->hash_block_verified = kvcalloc(BITS_TO_LONGS(num_bits),
237 						   sizeof(unsigned long),
238 						   GFP_KERNEL);
239 		if (!vi->hash_block_verified) {
240 			err = -ENOMEM;
241 			goto fail;
242 		}
243 	}
244 
245 	return vi;
246 
247 fail:
248 	fsverity_free_info(vi);
249 	return ERR_PTR(err);
250 }
251 
252 int fsverity_set_info(struct fsverity_info *vi)
253 {
254 	return rhashtable_lookup_insert_fast(&fsverity_info_hash,
255 					     &vi->rhash_head,
256 					     fsverity_info_hash_params);
257 }
258 
259 struct fsverity_info *__fsverity_get_info(const struct inode *inode)
260 {
261 	return rhashtable_lookup_fast(&fsverity_info_hash, &inode,
262 				      fsverity_info_hash_params);
263 }
264 EXPORT_SYMBOL_GPL(__fsverity_get_info);
265 
266 static bool validate_fsverity_descriptor(struct inode *inode,
267 					 const struct fsverity_descriptor *desc,
268 					 size_t desc_size)
269 {
270 	if (desc_size < sizeof(*desc)) {
271 		fsverity_err(inode, "Unrecognized descriptor size: %zu bytes",
272 			     desc_size);
273 		return false;
274 	}
275 
276 	if (desc->version != 1) {
277 		fsverity_err(inode, "Unrecognized descriptor version: %u",
278 			     desc->version);
279 		return false;
280 	}
281 
282 	if (memchr_inv(desc->__reserved, 0, sizeof(desc->__reserved))) {
283 		fsverity_err(inode, "Reserved bits set in descriptor");
284 		return false;
285 	}
286 
287 	if (desc->salt_size > sizeof(desc->salt)) {
288 		fsverity_err(inode, "Invalid salt_size: %u", desc->salt_size);
289 		return false;
290 	}
291 
292 	if (le64_to_cpu(desc->data_size) != inode->i_size) {
293 		fsverity_err(inode,
294 			     "Wrong data_size: %llu (desc) != %lld (inode)",
295 			     le64_to_cpu(desc->data_size), inode->i_size);
296 		return false;
297 	}
298 
299 	if (le32_to_cpu(desc->sig_size) > desc_size - sizeof(*desc)) {
300 		fsverity_err(inode, "Signature overflows verity descriptor");
301 		return false;
302 	}
303 
304 	return true;
305 }
306 
307 /*
308  * Read the inode's fsverity_descriptor (with optional appended builtin
309  * signature) from the filesystem, and do basic validation of it.
310  */
311 int fsverity_get_descriptor(struct inode *inode,
312 			    struct fsverity_descriptor **desc_ret)
313 {
314 	int res;
315 	struct fsverity_descriptor *desc;
316 
317 	res = inode->i_sb->s_vop->get_verity_descriptor(inode, NULL, 0);
318 	if (res < 0) {
319 		fsverity_err(inode,
320 			     "Error %d getting verity descriptor size", res);
321 		return res;
322 	}
323 	if (res > FS_VERITY_MAX_DESCRIPTOR_SIZE) {
324 		fsverity_err(inode, "Verity descriptor is too large (%d bytes)",
325 			     res);
326 		return -EMSGSIZE;
327 	}
328 	desc = kmalloc(res, GFP_KERNEL);
329 	if (!desc)
330 		return -ENOMEM;
331 	res = inode->i_sb->s_vop->get_verity_descriptor(inode, desc, res);
332 	if (res < 0) {
333 		fsverity_err(inode, "Error %d reading verity descriptor", res);
334 		kfree(desc);
335 		return res;
336 	}
337 
338 	if (!validate_fsverity_descriptor(inode, desc, res)) {
339 		kfree(desc);
340 		return -EINVAL;
341 	}
342 
343 	*desc_ret = desc;
344 	return 0;
345 }
346 
347 static int ensure_verity_info(struct inode *inode)
348 {
349 	struct fsverity_info *vi = fsverity_get_info(inode), *found;
350 	struct fsverity_descriptor *desc;
351 	int err;
352 
353 	if (vi)
354 		return 0;
355 
356 	err = fsverity_get_descriptor(inode, &desc);
357 	if (err)
358 		return err;
359 
360 	vi = fsverity_create_info(inode, desc);
361 	if (IS_ERR(vi)) {
362 		err = PTR_ERR(vi);
363 		goto out_free_desc;
364 	}
365 
366 	/*
367 	 * Multiple tasks may race to set the inode's verity info, in which case
368 	 * we might find an existing fsverity_info in the hash table.
369 	 */
370 	found = rhashtable_lookup_get_insert_fast(&fsverity_info_hash,
371 						  &vi->rhash_head,
372 						  fsverity_info_hash_params);
373 	if (found) {
374 		fsverity_free_info(vi);
375 		if (IS_ERR(found))
376 			err = PTR_ERR(found);
377 	}
378 
379 out_free_desc:
380 	kfree(desc);
381 	return err;
382 }
383 
384 int __fsverity_file_open(struct inode *inode, struct file *filp)
385 {
386 	if (filp->f_mode & FMODE_WRITE)
387 		return -EPERM;
388 	return ensure_verity_info(inode);
389 }
390 EXPORT_SYMBOL_GPL(__fsverity_file_open);
391 
392 void fsverity_free_info(struct fsverity_info *vi)
393 {
394 	kfree(vi->tree_params.hashstate);
395 	kvfree(vi->hash_block_verified);
396 	kmem_cache_free(fsverity_info_cachep, vi);
397 }
398 
399 void fsverity_remove_info(struct fsverity_info *vi)
400 {
401 	rhashtable_remove_fast(&fsverity_info_hash, &vi->rhash_head,
402 			       fsverity_info_hash_params);
403 	fsverity_free_info(vi);
404 }
405 
406 void fsverity_cleanup_inode(struct inode *inode)
407 {
408 	struct fsverity_info *vi = fsverity_get_info(inode);
409 
410 	if (vi)
411 		fsverity_remove_info(vi);
412 }
413 
414 void __init fsverity_init_info_cache(void)
415 {
416 	if (rhashtable_init(&fsverity_info_hash, &fsverity_info_hash_params))
417 		panic("failed to initialize fsverity hash\n");
418 	fsverity_info_cachep = KMEM_CACHE_USERCOPY(
419 					fsverity_info,
420 					SLAB_RECLAIM_ACCOUNT | SLAB_PANIC,
421 					file_digest);
422 }
423