Skip to content

Commit 4dd893d

Browse files
committed
fs-verity: implement FS_IOC_MEASURE_VERITY ioctl
Add a function for filesystems to call to implement the FS_IOC_MEASURE_VERITY ioctl. This ioctl retrieves the file measurement that fs-verity calculated for the given file and is enforcing for reads; i.e., reads that don't match this hash will fail. This ioctl can be used for authentication or logging of file measurements in userspace. See the "FS_IOC_MEASURE_VERITY" section of Documentation/filesystems/fsverity.rst for the documentation. Reviewed-by: Theodore Ts'o <tytso@mit.edu> Reviewed-by: Jaegeuk Kim <jaegeuk@kernel.org> Signed-off-by: Eric Biggers <ebiggers@google.com>
1 parent 3fda4c6 commit 4dd893d

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

‎fs/verity/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
obj-$(CONFIG_FS_VERITY) += enable.o \
44
hash_algs.o \
55
init.o \
6+
measure.o \
67
open.o \
78
verify.o

‎fs/verity/measure.c‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* fs/verity/measure.c: ioctl to get a verity file's measurement
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 measurement
14+
*
15+
* Retrieve the file measurement that the kernel is enforcing for reads from a
16+
* verity file. See the "FS_IOC_MEASURE_VERITY" section of
17+
* Documentation/filesystems/fsverity.rst for the documentation.
18+
*
19+
* Return: 0 on success, -errno on failure
20+
*/
21+
int fsverity_ioctl_measure(struct file *filp, void __user *_uarg)
22+
{
23+
const struct inode *inode = file_inode(filp);
24+
struct fsverity_digest __user *uarg = _uarg;
25+
const struct fsverity_info *vi;
26+
const struct fsverity_hash_alg *hash_alg;
27+
struct fsverity_digest arg;
28+
29+
vi = fsverity_get_info(inode);
30+
if (!vi)
31+
return -ENODATA; /* not a verity file */
32+
hash_alg = vi->tree_params.hash_alg;
33+
34+
/*
35+
* The user specifies the digest_size their buffer has space for; we can
36+
* return the digest if it fits in the available space. We write back
37+
* the actual size, which may be shorter than the user-specified size.
38+
*/
39+
40+
if (get_user(arg.digest_size, &uarg->digest_size))
41+
return -EFAULT;
42+
if (arg.digest_size < hash_alg->digest_size)
43+
return -EOVERFLOW;
44+
45+
memset(&arg, 0, sizeof(arg));
46+
arg.digest_algorithm = hash_alg - fsverity_hash_algs;
47+
arg.digest_size = hash_alg->digest_size;
48+
49+
if (copy_to_user(uarg, &arg, sizeof(arg)))
50+
return -EFAULT;
51+
52+
if (copy_to_user(uarg->digest, vi->measurement, hash_alg->digest_size))
53+
return -EFAULT;
54+
55+
return 0;
56+
}
57+
EXPORT_SYMBOL_GPL(fsverity_ioctl_measure);

‎include/linux/fsverity.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ static inline struct fsverity_info *fsverity_get_info(const struct inode *inode)
118118

119119
extern int fsverity_ioctl_enable(struct file *filp, const void __user *arg);
120120

121+
/* measure.c */
122+
123+
extern int fsverity_ioctl_measure(struct file *filp, void __user *arg);
124+
121125
/* open.c */
122126

123127
extern int fsverity_file_open(struct inode *inode, struct file *filp);
@@ -145,6 +149,13 @@ static inline int fsverity_ioctl_enable(struct file *filp,
145149
return -EOPNOTSUPP;
146150
}
147151

152+
/* measure.c */
153+
154+
static inline int fsverity_ioctl_measure(struct file *filp, void __user *arg)
155+
{
156+
return -EOPNOTSUPP;
157+
}
158+
148159
/* open.c */
149160

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

0 commit comments

Comments
 (0)