Skip to content

Commit 246d921

Browse files
committed
fs-verity: define a function to return the integrity protected file digest
Define a function named fsverity_get_digest() to return the verity file digest and the associated hash algorithm (enum hash_algo). This assumes that before calling fsverity_get_digest() the file must have been opened, which is even true for the IMA measure/appraise on file open policy rule use case (func=FILE_CHECK). do_open() calls vfs_open() immediately prior to ima_file_check(). Acked-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
1 parent 09091c4 commit 246d921

4 files changed

Lines changed: 62 additions & 7 deletions

File tree

fs/verity/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
config FS_VERITY
44
bool "FS Verity (read-only file-based authenticity protection)"
55
select CRYPTO
6+
select CRYPTO_HASH_INFO
67
# SHA-256 is implied as it's intended to be the default hash algorithm.
78
# To avoid bloat, other wanted algorithms must be selected explicitly.
89
# Note that CRYPTO_SHA256 denotes the generic C implementation, but

fs/verity/fsverity_private.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#define pr_fmt(fmt) "fs-verity: " fmt
1616

17-
#include <crypto/sha2.h>
1817
#include <linux/fsverity.h>
1918
#include <linux/mempool.h>
2019

@@ -26,12 +25,6 @@ struct ahash_request;
2625
*/
2726
#define FS_VERITY_MAX_LEVELS 8
2827

29-
/*
30-
* Largest digest size among all hash algorithms supported by fs-verity.
31-
* Currently assumed to be <= size of fsverity_descriptor::root_hash.
32-
*/
33-
#define FS_VERITY_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
34-
3528
/* A hash algorithm supported by fs-verity */
3629
struct fsverity_hash_alg {
3730
struct crypto_ahash *tfm; /* hash tfm, allocated on demand */

fs/verity/measure.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,46 @@ int fsverity_ioctl_measure(struct file *filp, void __user *_uarg)
5757
return 0;
5858
}
5959
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+
* @digest: (out) pointer to the digest
65+
* @alg: (out) pointer to the hash algorithm enumeration
66+
*
67+
* Return the file hash algorithm and digest of an fsverity protected file.
68+
* Assumption: before calling fsverity_get_digest(), the file must have been
69+
* opened.
70+
*
71+
* Return: 0 on success, -errno on failure
72+
*/
73+
int fsverity_get_digest(struct inode *inode,
74+
u8 digest[FS_VERITY_MAX_DIGEST_SIZE],
75+
enum hash_algo *alg)
76+
{
77+
const struct fsverity_info *vi;
78+
const struct fsverity_hash_alg *hash_alg;
79+
int i;
80+
81+
vi = fsverity_get_info(inode);
82+
if (!vi)
83+
return -ENODATA; /* not a verity file */
84+
85+
hash_alg = vi->tree_params.hash_alg;
86+
memset(digest, 0, FS_VERITY_MAX_DIGEST_SIZE);
87+
88+
/* convert the verity hash algorithm name to a hash_algo_name enum */
89+
i = match_string(hash_algo_name, HASH_ALGO__LAST, hash_alg->name);
90+
if (i < 0)
91+
return -EINVAL;
92+
*alg = i;
93+
94+
if (WARN_ON_ONCE(hash_alg->digest_size != hash_digest_size[*alg]))
95+
return -EINVAL;
96+
memcpy(digest, vi->file_digest, hash_alg->digest_size);
97+
98+
pr_debug("file digest %s:%*phN\n", hash_algo_name[*alg],
99+
hash_digest_size[*alg], digest);
100+
101+
return 0;
102+
}

include/linux/fsverity.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@
1212
#define _LINUX_FSVERITY_H
1313

1414
#include <linux/fs.h>
15+
#include <crypto/hash_info.h>
16+
#include <crypto/sha2.h>
1517
#include <uapi/linux/fsverity.h>
1618

19+
/*
20+
* Largest digest size among all hash algorithms supported by fs-verity.
21+
* Currently assumed to be <= size of fsverity_descriptor::root_hash.
22+
*/
23+
#define FS_VERITY_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
24+
1725
/* Verity operations for filesystems */
1826
struct fsverity_operations {
1927

@@ -131,6 +139,9 @@ int fsverity_ioctl_enable(struct file *filp, const void __user *arg);
131139
/* measure.c */
132140

133141
int fsverity_ioctl_measure(struct file *filp, void __user *arg);
142+
int fsverity_get_digest(struct inode *inode,
143+
u8 digest[FS_VERITY_MAX_DIGEST_SIZE],
144+
enum hash_algo *alg);
134145

135146
/* open.c */
136147

@@ -170,6 +181,13 @@ static inline int fsverity_ioctl_measure(struct file *filp, void __user *arg)
170181
return -EOPNOTSUPP;
171182
}
172183

184+
static inline int fsverity_get_digest(struct inode *inode,
185+
u8 digest[FS_VERITY_MAX_DIGEST_SIZE],
186+
enum hash_algo *alg)
187+
{
188+
return -EOPNOTSUPP;
189+
}
190+
173191
/* open.c */
174192

175193
static inline int fsverity_file_open(struct inode *inode, struct file *filp)

0 commit comments

Comments
 (0)