Merge tag 'docs-5.7-2' of git://git.lwn.net/linux
[linux-2.6-microblaze.git] / fs / verity / measure.c
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);