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 8 #include "fsverity_private.h" 9 10 #include <linux/bpf.h> 11 #include <linux/btf.h> 12 #include <linux/export.h> 13 #include <linux/uaccess.h> 14 15 /** 16 * fsverity_ioctl_measure() - get a verity file's digest 17 * @filp: file to get digest of 18 * @_uarg: user pointer to fsverity_digest 19 * 20 * Retrieve the file digest that the kernel is enforcing for reads from a verity 21 * file. See the "FS_IOC_MEASURE_VERITY" section of 22 * Documentation/filesystems/fsverity.rst for the documentation. 23 * 24 * Return: 0 on success, -errno on failure 25 */ 26 int fsverity_ioctl_measure(struct file *filp, void __user *_uarg) 27 { 28 const struct inode *inode = file_inode(filp); 29 struct fsverity_digest __user *uarg = _uarg; 30 const struct fsverity_info *vi; 31 const struct fsverity_hash_alg *hash_alg; 32 struct fsverity_digest arg; 33 34 vi = fsverity_get_info(inode); 35 if (!vi) 36 return -ENODATA; /* not a verity file */ 37 hash_alg = vi->tree_params.hash_alg; 38 39 /* 40 * The user specifies the digest_size their buffer has space for; we can 41 * return the digest if it fits in the available space. We write back 42 * the actual size, which may be shorter than the user-specified size. 43 */ 44 45 if (get_user(arg.digest_size, &uarg->digest_size)) 46 return -EFAULT; 47 if (arg.digest_size < hash_alg->digest_size) 48 return -EOVERFLOW; 49 50 memset(&arg, 0, sizeof(arg)); 51 arg.digest_algorithm = hash_alg - fsverity_hash_algs; 52 arg.digest_size = hash_alg->digest_size; 53 54 if (copy_to_user(uarg, &arg, sizeof(arg))) 55 return -EFAULT; 56 57 if (copy_to_user(uarg->digest, vi->file_digest, hash_alg->digest_size)) 58 return -EFAULT; 59 60 return 0; 61 } 62 EXPORT_SYMBOL_GPL(fsverity_ioctl_measure); 63 64 /** 65 * fsverity_get_digest() - get a verity file's digest 66 * @inode: inode to get digest of 67 * @raw_digest: (out) the raw file digest 68 * @alg: (out) the digest's algorithm, as a FS_VERITY_HASH_ALG_* value 69 * @halg: (out) the digest's algorithm, as a HASH_ALGO_* value 70 * 71 * Retrieves the fsverity digest of the given file. The file must have been 72 * opened at least once since the inode was last loaded into the inode cache; 73 * otherwise this function will not recognize when fsverity is enabled. 74 * 75 * The file's fsverity digest consists of @raw_digest in combination with either 76 * @alg or @halg. (The caller can choose which one of @alg or @halg to use.) 77 * 78 * IMPORTANT: Callers *must* make use of one of the two algorithm IDs, since 79 * @raw_digest is meaningless without knowing which algorithm it uses! fsverity 80 * provides no security guarantee for users who ignore the algorithm ID, even if 81 * they use the digest size (since algorithms can share the same digest size). 82 * 83 * Return: The size of the raw digest in bytes, or 0 if the file doesn't have 84 * fsverity enabled. 85 */ 86 int fsverity_get_digest(struct inode *inode, 87 u8 raw_digest[FS_VERITY_MAX_DIGEST_SIZE], 88 u8 *alg, enum hash_algo *halg) 89 { 90 const struct fsverity_info *vi; 91 const struct fsverity_hash_alg *hash_alg; 92 93 vi = fsverity_get_info(inode); 94 if (!vi) 95 return 0; /* not a verity file */ 96 97 hash_alg = vi->tree_params.hash_alg; 98 memcpy(raw_digest, vi->file_digest, hash_alg->digest_size); 99 if (alg) 100 *alg = hash_alg - fsverity_hash_algs; 101 if (halg) 102 *halg = hash_alg->algo_id; 103 return hash_alg->digest_size; 104 } 105 EXPORT_SYMBOL_GPL(fsverity_get_digest); 106 107 #ifdef CONFIG_BPF_SYSCALL 108 109 /* bpf kfuncs */ 110 __bpf_kfunc_start_defs(); 111 112 /** 113 * bpf_get_fsverity_digest: read fsverity digest of file 114 * @file: file to get digest from 115 * @digest_p: (out) dynptr for struct fsverity_digest 116 * 117 * Read fsverity_digest of *file* into *digest_ptr*. 118 * 119 * Return: 0 on success, a negative value on error. 120 */ 121 __bpf_kfunc int bpf_get_fsverity_digest(struct file *file, struct bpf_dynptr *digest_p) 122 { 123 struct bpf_dynptr_kern *digest_ptr = (struct bpf_dynptr_kern *)digest_p; 124 const struct inode *inode = file_inode(file); 125 u32 dynptr_sz = __bpf_dynptr_size(digest_ptr); 126 struct fsverity_digest *arg; 127 const struct fsverity_info *vi; 128 const struct fsverity_hash_alg *hash_alg; 129 int out_digest_sz; 130 131 if (dynptr_sz < sizeof(struct fsverity_digest)) 132 return -EINVAL; 133 134 arg = __bpf_dynptr_data_rw(digest_ptr, dynptr_sz); 135 if (!arg) 136 return -EINVAL; 137 138 if (!IS_ALIGNED((uintptr_t)arg, __alignof__(*arg))) 139 return -EINVAL; 140 141 vi = fsverity_get_info(inode); 142 if (!vi) 143 return -ENODATA; /* not a verity file */ 144 145 hash_alg = vi->tree_params.hash_alg; 146 147 arg->digest_algorithm = hash_alg - fsverity_hash_algs; 148 arg->digest_size = hash_alg->digest_size; 149 150 out_digest_sz = dynptr_sz - sizeof(struct fsverity_digest); 151 152 /* copy digest */ 153 memcpy(arg->digest, vi->file_digest, min_t(int, hash_alg->digest_size, out_digest_sz)); 154 155 /* fill the extra buffer with zeros */ 156 if (out_digest_sz > hash_alg->digest_size) 157 memset(arg->digest + arg->digest_size, 0, out_digest_sz - hash_alg->digest_size); 158 159 return 0; 160 } 161 162 __bpf_kfunc_end_defs(); 163 164 BTF_KFUNCS_START(fsverity_set_ids) 165 BTF_ID_FLAGS(func, bpf_get_fsverity_digest, KF_TRUSTED_ARGS) 166 BTF_KFUNCS_END(fsverity_set_ids) 167 168 static int bpf_get_fsverity_digest_filter(const struct bpf_prog *prog, u32 kfunc_id) 169 { 170 if (!btf_id_set8_contains(&fsverity_set_ids, kfunc_id)) 171 return 0; 172 173 /* Only allow to attach from LSM hooks, to avoid recursion */ 174 return prog->type != BPF_PROG_TYPE_LSM ? -EACCES : 0; 175 } 176 177 static const struct btf_kfunc_id_set bpf_fsverity_set = { 178 .owner = THIS_MODULE, 179 .set = &fsverity_set_ids, 180 .filter = bpf_get_fsverity_digest_filter, 181 }; 182 183 void __init fsverity_init_bpf(void) 184 { 185 register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, &bpf_fsverity_set); 186 } 187 188 #endif /* CONFIG_BPF_SYSCALL */ 189