xref: /openbmc/linux/fs/verity/measure.c (revision 69f03be1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Ioctl to get a verity file's digest
4  *
5  * Copyright 2019 Google LLC
6  */
7 
8 #include "fsverity_private.h"
9 
10 #include <linux/uaccess.h>
11 
12 /**
13  * fsverity_ioctl_measure() - get a verity file's digest
14  * @filp: file to get digest of
15  * @_uarg: user pointer to fsverity_digest
16  *
17  * Retrieve the file digest that the kernel is enforcing for reads from a verity
18  * file.  See the "FS_IOC_MEASURE_VERITY" section of
19  * Documentation/filesystems/fsverity.rst for the documentation.
20  *
21  * Return: 0 on success, -errno on failure
22  */
23 int fsverity_ioctl_measure(struct file *filp, void __user *_uarg)
24 {
25 	const struct inode *inode = file_inode(filp);
26 	struct fsverity_digest __user *uarg = _uarg;
27 	const struct fsverity_info *vi;
28 	const struct fsverity_hash_alg *hash_alg;
29 	struct fsverity_digest arg;
30 
31 	vi = fsverity_get_info(inode);
32 	if (!vi)
33 		return -ENODATA; /* not a verity file */
34 	hash_alg = vi->tree_params.hash_alg;
35 
36 	/*
37 	 * The user specifies the digest_size their buffer has space for; we can
38 	 * return the digest if it fits in the available space.  We write back
39 	 * the actual size, which may be shorter than the user-specified size.
40 	 */
41 
42 	if (get_user(arg.digest_size, &uarg->digest_size))
43 		return -EFAULT;
44 	if (arg.digest_size < hash_alg->digest_size)
45 		return -EOVERFLOW;
46 
47 	memset(&arg, 0, sizeof(arg));
48 	arg.digest_algorithm = hash_alg - fsverity_hash_algs;
49 	arg.digest_size = hash_alg->digest_size;
50 
51 	if (copy_to_user(uarg, &arg, sizeof(arg)))
52 		return -EFAULT;
53 
54 	if (copy_to_user(uarg->digest, vi->file_digest, hash_alg->digest_size))
55 		return -EFAULT;
56 
57 	return 0;
58 }
59 EXPORT_SYMBOL_GPL(fsverity_ioctl_measure);
60 
61 /**
62  * fsverity_get_digest() - get a verity file's digest
63  * @inode: inode to get digest of
64  * @raw_digest: (out) the raw file digest
65  * @alg: (out) the digest's algorithm, as a FS_VERITY_HASH_ALG_* value
66  * @halg: (out) the digest's algorithm, as a HASH_ALGO_* value
67  *
68  * Retrieves the fsverity digest of the given file.  The file must have been
69  * opened at least once since the inode was last loaded into the inode cache;
70  * otherwise this function will not recognize when fsverity is enabled.
71  *
72  * The file's fsverity digest consists of @raw_digest in combination with either
73  * @alg or @halg.  (The caller can choose which one of @alg or @halg to use.)
74  *
75  * IMPORTANT: Callers *must* make use of one of the two algorithm IDs, since
76  * @raw_digest is meaningless without knowing which algorithm it uses!  fsverity
77  * provides no security guarantee for users who ignore the algorithm ID, even if
78  * they use the digest size (since algorithms can share the same digest size).
79  *
80  * Return: The size of the raw digest in bytes, or 0 if the file doesn't have
81  *	   fsverity enabled.
82  */
83 int fsverity_get_digest(struct inode *inode,
84 			u8 raw_digest[FS_VERITY_MAX_DIGEST_SIZE],
85 			u8 *alg, enum hash_algo *halg)
86 {
87 	const struct fsverity_info *vi;
88 	const struct fsverity_hash_alg *hash_alg;
89 
90 	vi = fsverity_get_info(inode);
91 	if (!vi)
92 		return 0; /* not a verity file */
93 
94 	hash_alg = vi->tree_params.hash_alg;
95 	memcpy(raw_digest, vi->file_digest, hash_alg->digest_size);
96 	if (alg)
97 		*alg = hash_alg - fsverity_hash_algs;
98 	if (halg)
99 		*halg = hash_alg->algo_id;
100 	return hash_alg->digest_size;
101 }
102 EXPORT_SYMBOL_GPL(fsverity_get_digest);
103