^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Ioctl to get a verity file's digest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2019 Google LLC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "fsverity_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * fsverity_ioctl_measure() - get a verity file's digest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * @filp: file to get digest of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * @_uarg: user pointer to fsverity_digest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Retrieve the file digest that the kernel is enforcing for reads from a verity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * file. See the "FS_IOC_MEASURE_VERITY" section of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Documentation/filesystems/fsverity.rst for the documentation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Return: 0 on success, -errno on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int fsverity_ioctl_measure(struct file *filp, void __user *_uarg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) const struct inode *inode = file_inode(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct fsverity_digest __user *uarg = _uarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) const struct fsverity_info *vi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) const struct fsverity_hash_alg *hash_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct fsverity_digest arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) vi = fsverity_get_info(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (!vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return -ENODATA; /* not a verity file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) hash_alg = vi->tree_params.hash_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * The user specifies the digest_size their buffer has space for; we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * return the digest if it fits in the available space. We write back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * the actual size, which may be shorter than the user-specified size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (get_user(arg.digest_size, &uarg->digest_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (arg.digest_size < hash_alg->digest_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) memset(&arg, 0, sizeof(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) arg.digest_algorithm = hash_alg - fsverity_hash_algs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) arg.digest_size = hash_alg->digest_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (copy_to_user(uarg, &arg, sizeof(arg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (copy_to_user(uarg->digest, vi->file_digest, hash_alg->digest_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) EXPORT_SYMBOL_GPL(fsverity_ioctl_measure);