|
7 | 7 |
|
8 | 8 | #include "fsverity_private.h"
|
9 | 9 |
|
| 10 | +#include <linux/bpf.h> |
| 11 | +#include <linux/btf.h> |
10 | 12 | #include <linux/uaccess.h>
|
11 | 13 |
|
12 | 14 | /**
|
@@ -100,3 +102,85 @@ int fsverity_get_digest(struct inode *inode,
|
100 | 102 | return hash_alg->digest_size;
|
101 | 103 | }
|
102 | 104 | EXPORT_SYMBOL_GPL(fsverity_get_digest);
|
| 105 | + |
| 106 | +#ifdef CONFIG_BPF_SYSCALL |
| 107 | + |
| 108 | +/* bpf kfuncs */ |
| 109 | +__bpf_kfunc_start_defs(); |
| 110 | + |
| 111 | +/** |
| 112 | + * bpf_get_fsverity_digest: read fsverity digest of file |
| 113 | + * @file: file to get digest from |
| 114 | + * @digest_ptr: (out) dynptr for struct fsverity_digest |
| 115 | + * |
| 116 | + * Read fsverity_digest of *file* into *digest_ptr*. |
| 117 | + * |
| 118 | + * Return: 0 on success, a negative value on error. |
| 119 | + */ |
| 120 | +__bpf_kfunc int bpf_get_fsverity_digest(struct file *file, struct bpf_dynptr_kern *digest_ptr) |
| 121 | +{ |
| 122 | + const struct inode *inode = file_inode(file); |
| 123 | + u32 dynptr_sz = __bpf_dynptr_size(digest_ptr); |
| 124 | + struct fsverity_digest *arg; |
| 125 | + const struct fsverity_info *vi; |
| 126 | + const struct fsverity_hash_alg *hash_alg; |
| 127 | + int out_digest_sz; |
| 128 | + |
| 129 | + if (dynptr_sz < sizeof(struct fsverity_digest)) |
| 130 | + return -EINVAL; |
| 131 | + |
| 132 | + arg = __bpf_dynptr_data_rw(digest_ptr, dynptr_sz); |
| 133 | + if (!arg) |
| 134 | + return -EINVAL; |
| 135 | + |
| 136 | + if (!IS_ALIGNED((uintptr_t)arg, __alignof__(*arg))) |
| 137 | + return -EINVAL; |
| 138 | + |
| 139 | + vi = fsverity_get_info(inode); |
| 140 | + if (!vi) |
| 141 | + return -ENODATA; /* not a verity file */ |
| 142 | + |
| 143 | + hash_alg = vi->tree_params.hash_alg; |
| 144 | + |
| 145 | + arg->digest_algorithm = hash_alg - fsverity_hash_algs; |
| 146 | + arg->digest_size = hash_alg->digest_size; |
| 147 | + |
| 148 | + out_digest_sz = dynptr_sz - sizeof(struct fsverity_digest); |
| 149 | + |
| 150 | + /* copy digest */ |
| 151 | + memcpy(arg->digest, vi->file_digest, min_t(int, hash_alg->digest_size, out_digest_sz)); |
| 152 | + |
| 153 | + /* fill the extra buffer with zeros */ |
| 154 | + if (out_digest_sz > hash_alg->digest_size) |
| 155 | + memset(arg->digest + arg->digest_size, 0, out_digest_sz - hash_alg->digest_size); |
| 156 | + |
| 157 | + return 0; |
| 158 | +} |
| 159 | + |
| 160 | +__bpf_kfunc_end_defs(); |
| 161 | + |
| 162 | +BTF_SET8_START(fsverity_set_ids) |
| 163 | +BTF_ID_FLAGS(func, bpf_get_fsverity_digest, KF_TRUSTED_ARGS) |
| 164 | +BTF_SET8_END(fsverity_set_ids) |
| 165 | + |
| 166 | +static int bpf_get_fsverity_digest_filter(const struct bpf_prog *prog, u32 kfunc_id) |
| 167 | +{ |
| 168 | + if (!btf_id_set8_contains(&fsverity_set_ids, kfunc_id)) |
| 169 | + return 0; |
| 170 | + |
| 171 | + /* Only allow to attach from LSM hooks, to avoid recursion */ |
| 172 | + return prog->type != BPF_PROG_TYPE_LSM ? -EACCES : 0; |
| 173 | +} |
| 174 | + |
| 175 | +static const struct btf_kfunc_id_set bpf_fsverity_set = { |
| 176 | + .owner = THIS_MODULE, |
| 177 | + .set = &fsverity_set_ids, |
| 178 | + .filter = bpf_get_fsverity_digest_filter, |
| 179 | +}; |
| 180 | + |
| 181 | +void __init fsverity_init_bpf(void) |
| 182 | +{ |
| 183 | + register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, &bpf_fsverity_set); |
| 184 | +} |
| 185 | + |
| 186 | +#endif /* CONFIG_BPF_SYSCALL */ |
0 commit comments