xref: /openbmc/linux/fs/verity/open.c (revision b868a02e)
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/slab.h>
11 
12 static struct kmem_cache *fsverity_info_cachep;
13 
14 /**
15  * fsverity_init_merkle_tree_params() - initialize Merkle tree parameters
16  * @params: the parameters struct to initialize
17  * @inode: the inode for which the Merkle tree is being built
18  * @hash_algorithm: number of hash algorithm to use
19  * @log_blocksize: log base 2 of block size to use
20  * @salt: pointer to salt (optional)
21  * @salt_size: size of salt, possibly 0
22  *
23  * Validate the hash algorithm and block size, then compute the tree topology
24  * (num levels, num blocks in each level, etc.) and initialize @params.
25  *
26  * Return: 0 on success, -errno on failure
27  */
28 int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
29 				     const struct inode *inode,
30 				     unsigned int hash_algorithm,
31 				     unsigned int log_blocksize,
32 				     const u8 *salt, size_t salt_size)
33 {
34 	struct fsverity_hash_alg *hash_alg;
35 	int err;
36 	u64 blocks;
37 	u64 offset;
38 	int level;
39 
40 	memset(params, 0, sizeof(*params));
41 
42 	hash_alg = fsverity_get_hash_alg(inode, hash_algorithm);
43 	if (IS_ERR(hash_alg))
44 		return PTR_ERR(hash_alg);
45 	params->hash_alg = hash_alg;
46 	params->digest_size = hash_alg->digest_size;
47 
48 	params->hashstate = fsverity_prepare_hash_state(hash_alg, salt,
49 							salt_size);
50 	if (IS_ERR(params->hashstate)) {
51 		err = PTR_ERR(params->hashstate);
52 		params->hashstate = NULL;
53 		fsverity_err(inode, "Error %d preparing hash state", err);
54 		goto out_err;
55 	}
56 
57 	if (log_blocksize != PAGE_SHIFT) {
58 		fsverity_warn(inode, "Unsupported log_blocksize: %u",
59 			      log_blocksize);
60 		err = -EINVAL;
61 		goto out_err;
62 	}
63 	params->log_blocksize = log_blocksize;
64 	params->block_size = 1 << log_blocksize;
65 
66 	if (WARN_ON(!is_power_of_2(params->digest_size))) {
67 		err = -EINVAL;
68 		goto out_err;
69 	}
70 	if (params->block_size < 2 * params->digest_size) {
71 		fsverity_warn(inode,
72 			      "Merkle tree block size (%u) too small for hash algorithm \"%s\"",
73 			      params->block_size, hash_alg->name);
74 		err = -EINVAL;
75 		goto out_err;
76 	}
77 	params->log_arity = params->log_blocksize - ilog2(params->digest_size);
78 	params->hashes_per_block = 1 << params->log_arity;
79 
80 	pr_debug("Merkle tree uses %s with %u-byte blocks (%u hashes/block), salt=%*phN\n",
81 		 hash_alg->name, params->block_size, params->hashes_per_block,
82 		 (int)salt_size, salt);
83 
84 	/*
85 	 * Compute the number of levels in the Merkle tree and create a map from
86 	 * level to the starting block of that level.  Level 'num_levels - 1' is
87 	 * the root and is stored first.  Level 0 is the level directly "above"
88 	 * the data blocks and is stored last.
89 	 */
90 
91 	/* Compute number of levels and the number of blocks in each level */
92 	blocks = ((u64)inode->i_size + params->block_size - 1) >> log_blocksize;
93 	pr_debug("Data is %lld bytes (%llu blocks)\n", inode->i_size, blocks);
94 	while (blocks > 1) {
95 		if (params->num_levels >= FS_VERITY_MAX_LEVELS) {
96 			fsverity_err(inode, "Too many levels in Merkle tree");
97 			err = -EINVAL;
98 			goto out_err;
99 		}
100 		blocks = (blocks + params->hashes_per_block - 1) >>
101 			 params->log_arity;
102 		/* temporarily using level_start[] to store blocks in level */
103 		params->level_start[params->num_levels++] = blocks;
104 	}
105 	params->level0_blocks = params->level_start[0];
106 
107 	/* Compute the starting block of each level */
108 	offset = 0;
109 	for (level = (int)params->num_levels - 1; level >= 0; level--) {
110 		blocks = params->level_start[level];
111 		params->level_start[level] = offset;
112 		pr_debug("Level %d is %llu blocks starting at index %llu\n",
113 			 level, blocks, offset);
114 		offset += blocks;
115 	}
116 
117 	params->tree_size = offset << log_blocksize;
118 	return 0;
119 
120 out_err:
121 	kfree(params->hashstate);
122 	memset(params, 0, sizeof(*params));
123 	return err;
124 }
125 
126 /*
127  * Compute the file digest by hashing the fsverity_descriptor excluding the
128  * signature and with the sig_size field set to 0.
129  */
130 static int compute_file_digest(struct fsverity_hash_alg *hash_alg,
131 			       struct fsverity_descriptor *desc,
132 			       u8 *file_digest)
133 {
134 	__le32 sig_size = desc->sig_size;
135 	int err;
136 
137 	desc->sig_size = 0;
138 	err = fsverity_hash_buffer(hash_alg, desc, sizeof(*desc), file_digest);
139 	desc->sig_size = sig_size;
140 
141 	return err;
142 }
143 
144 /*
145  * Create a new fsverity_info from the given fsverity_descriptor (with optional
146  * appended signature), and check the signature if present.  The
147  * fsverity_descriptor must have already undergone basic validation.
148  */
149 struct fsverity_info *fsverity_create_info(const struct inode *inode,
150 					   struct fsverity_descriptor *desc)
151 {
152 	struct fsverity_info *vi;
153 	int err;
154 
155 	vi = kmem_cache_zalloc(fsverity_info_cachep, GFP_KERNEL);
156 	if (!vi)
157 		return ERR_PTR(-ENOMEM);
158 	vi->inode = inode;
159 
160 	err = fsverity_init_merkle_tree_params(&vi->tree_params, inode,
161 					       desc->hash_algorithm,
162 					       desc->log_blocksize,
163 					       desc->salt, desc->salt_size);
164 	if (err) {
165 		fsverity_err(inode,
166 			     "Error %d initializing Merkle tree parameters",
167 			     err);
168 		goto out;
169 	}
170 
171 	memcpy(vi->root_hash, desc->root_hash, vi->tree_params.digest_size);
172 
173 	err = compute_file_digest(vi->tree_params.hash_alg, desc,
174 				  vi->file_digest);
175 	if (err) {
176 		fsverity_err(inode, "Error %d computing file digest", err);
177 		goto out;
178 	}
179 	pr_debug("Computed file digest: %s:%*phN\n",
180 		 vi->tree_params.hash_alg->name,
181 		 vi->tree_params.digest_size, vi->file_digest);
182 
183 	err = fsverity_verify_signature(vi, desc->signature,
184 					le32_to_cpu(desc->sig_size));
185 out:
186 	if (err) {
187 		fsverity_free_info(vi);
188 		vi = ERR_PTR(err);
189 	}
190 	return vi;
191 }
192 
193 void fsverity_set_info(struct inode *inode, struct fsverity_info *vi)
194 {
195 	/*
196 	 * Multiple tasks may race to set ->i_verity_info, so use
197 	 * cmpxchg_release().  This pairs with the smp_load_acquire() in
198 	 * fsverity_get_info().  I.e., here we publish ->i_verity_info with a
199 	 * RELEASE barrier so that other tasks can ACQUIRE it.
200 	 */
201 	if (cmpxchg_release(&inode->i_verity_info, NULL, vi) != NULL) {
202 		/* Lost the race, so free the fsverity_info we allocated. */
203 		fsverity_free_info(vi);
204 		/*
205 		 * Afterwards, the caller may access ->i_verity_info directly,
206 		 * so make sure to ACQUIRE the winning fsverity_info.
207 		 */
208 		(void)fsverity_get_info(inode);
209 	}
210 }
211 
212 void fsverity_free_info(struct fsverity_info *vi)
213 {
214 	if (!vi)
215 		return;
216 	kfree(vi->tree_params.hashstate);
217 	kmem_cache_free(fsverity_info_cachep, vi);
218 }
219 
220 static bool validate_fsverity_descriptor(struct inode *inode,
221 					 const struct fsverity_descriptor *desc,
222 					 size_t desc_size)
223 {
224 	if (desc_size < sizeof(*desc)) {
225 		fsverity_err(inode, "Unrecognized descriptor size: %zu bytes",
226 			     desc_size);
227 		return false;
228 	}
229 
230 	if (desc->version != 1) {
231 		fsverity_err(inode, "Unrecognized descriptor version: %u",
232 			     desc->version);
233 		return false;
234 	}
235 
236 	if (memchr_inv(desc->__reserved, 0, sizeof(desc->__reserved))) {
237 		fsverity_err(inode, "Reserved bits set in descriptor");
238 		return false;
239 	}
240 
241 	if (desc->salt_size > sizeof(desc->salt)) {
242 		fsverity_err(inode, "Invalid salt_size: %u", desc->salt_size);
243 		return false;
244 	}
245 
246 	if (le64_to_cpu(desc->data_size) != inode->i_size) {
247 		fsverity_err(inode,
248 			     "Wrong data_size: %llu (desc) != %lld (inode)",
249 			     le64_to_cpu(desc->data_size), inode->i_size);
250 		return false;
251 	}
252 
253 	if (le32_to_cpu(desc->sig_size) > desc_size - sizeof(*desc)) {
254 		fsverity_err(inode, "Signature overflows verity descriptor");
255 		return false;
256 	}
257 
258 	return true;
259 }
260 
261 /*
262  * Read the inode's fsverity_descriptor (with optional appended signature) from
263  * the filesystem, and do basic validation of it.
264  */
265 int fsverity_get_descriptor(struct inode *inode,
266 			    struct fsverity_descriptor **desc_ret)
267 {
268 	int res;
269 	struct fsverity_descriptor *desc;
270 
271 	res = inode->i_sb->s_vop->get_verity_descriptor(inode, NULL, 0);
272 	if (res < 0) {
273 		fsverity_err(inode,
274 			     "Error %d getting verity descriptor size", res);
275 		return res;
276 	}
277 	if (res > FS_VERITY_MAX_DESCRIPTOR_SIZE) {
278 		fsverity_err(inode, "Verity descriptor is too large (%d bytes)",
279 			     res);
280 		return -EMSGSIZE;
281 	}
282 	desc = kmalloc(res, GFP_KERNEL);
283 	if (!desc)
284 		return -ENOMEM;
285 	res = inode->i_sb->s_vop->get_verity_descriptor(inode, desc, res);
286 	if (res < 0) {
287 		fsverity_err(inode, "Error %d reading verity descriptor", res);
288 		kfree(desc);
289 		return res;
290 	}
291 
292 	if (!validate_fsverity_descriptor(inode, desc, res)) {
293 		kfree(desc);
294 		return -EINVAL;
295 	}
296 
297 	*desc_ret = desc;
298 	return 0;
299 }
300 
301 /* Ensure the inode has an ->i_verity_info */
302 static int ensure_verity_info(struct inode *inode)
303 {
304 	struct fsverity_info *vi = fsverity_get_info(inode);
305 	struct fsverity_descriptor *desc;
306 	int err;
307 
308 	if (vi)
309 		return 0;
310 
311 	err = fsverity_get_descriptor(inode, &desc);
312 	if (err)
313 		return err;
314 
315 	vi = fsverity_create_info(inode, desc);
316 	if (IS_ERR(vi)) {
317 		err = PTR_ERR(vi);
318 		goto out_free_desc;
319 	}
320 
321 	fsverity_set_info(inode, vi);
322 	err = 0;
323 out_free_desc:
324 	kfree(desc);
325 	return err;
326 }
327 
328 /**
329  * fsverity_file_open() - prepare to open a verity file
330  * @inode: the inode being opened
331  * @filp: the struct file being set up
332  *
333  * When opening a verity file, deny the open if it is for writing.  Otherwise,
334  * set up the inode's ->i_verity_info if not already done.
335  *
336  * When combined with fscrypt, this must be called after fscrypt_file_open().
337  * Otherwise, we won't have the key set up to decrypt the verity metadata.
338  *
339  * Return: 0 on success, -errno on failure
340  */
341 int fsverity_file_open(struct inode *inode, struct file *filp)
342 {
343 	if (!IS_VERITY(inode))
344 		return 0;
345 
346 	if (filp->f_mode & FMODE_WRITE) {
347 		pr_debug("Denying opening verity file (ino %lu) for write\n",
348 			 inode->i_ino);
349 		return -EPERM;
350 	}
351 
352 	return ensure_verity_info(inode);
353 }
354 EXPORT_SYMBOL_GPL(fsverity_file_open);
355 
356 /**
357  * fsverity_prepare_setattr() - prepare to change a verity inode's attributes
358  * @dentry: dentry through which the inode is being changed
359  * @attr: attributes to change
360  *
361  * Verity files are immutable, so deny truncates.  This isn't covered by the
362  * open-time check because sys_truncate() takes a path, not a file descriptor.
363  *
364  * Return: 0 on success, -errno on failure
365  */
366 int fsverity_prepare_setattr(struct dentry *dentry, struct iattr *attr)
367 {
368 	if (IS_VERITY(d_inode(dentry)) && (attr->ia_valid & ATTR_SIZE)) {
369 		pr_debug("Denying truncate of verity file (ino %lu)\n",
370 			 d_inode(dentry)->i_ino);
371 		return -EPERM;
372 	}
373 	return 0;
374 }
375 EXPORT_SYMBOL_GPL(fsverity_prepare_setattr);
376 
377 /**
378  * fsverity_cleanup_inode() - free the inode's verity info, if present
379  * @inode: an inode being evicted
380  *
381  * Filesystems must call this on inode eviction to free ->i_verity_info.
382  */
383 void fsverity_cleanup_inode(struct inode *inode)
384 {
385 	fsverity_free_info(inode->i_verity_info);
386 	inode->i_verity_info = NULL;
387 }
388 EXPORT_SYMBOL_GPL(fsverity_cleanup_inode);
389 
390 int __init fsverity_init_info_cache(void)
391 {
392 	fsverity_info_cachep = KMEM_CACHE_USERCOPY(fsverity_info,
393 						   SLAB_RECLAIM_ACCOUNT,
394 						   file_digest);
395 	if (!fsverity_info_cachep)
396 		return -ENOMEM;
397 	return 0;
398 }
399 
400 void __init fsverity_exit_info_cache(void)
401 {
402 	kmem_cache_destroy(fsverity_info_cachep);
403 	fsverity_info_cachep = NULL;
404 }
405