measure.c 1.6 KB

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