Skip to content

Commit 67814c0

Browse files
liu-song-6Alexei Starovoitov
authored andcommitted
bpf, fsverity: Add kfunc bpf_get_fsverity_digest
fsverity provides fast and reliable hash of files, namely fsverity_digest. The digest can be used by security solutions to verify file contents. Add new kfunc bpf_get_fsverity_digest() so that we can access fsverity from BPF LSM programs. This kfunc is added to fs/verity/measure.c because some data structure used in the function is private to fsverity (fs/verity/fsverity_private.h). To avoid recursion, bpf_get_fsverity_digest is only allowed in BPF LSM programs. Signed-off-by: Song Liu <[email protected]> Acked-by: Eric Biggers <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent ac9c05e commit 67814c0

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

fs/verity/fsverity_private.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ fsverity_msg(const struct inode *inode, const char *level,
100100
#define fsverity_err(inode, fmt, ...) \
101101
fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
102102

103+
/* measure.c */
104+
105+
#ifdef CONFIG_BPF_SYSCALL
106+
void __init fsverity_init_bpf(void);
107+
#else
108+
static inline void fsverity_init_bpf(void)
109+
{
110+
}
111+
#endif
112+
103113
/* open.c */
104114

105115
int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,

fs/verity/init.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ static int __init fsverity_init(void)
6969
fsverity_init_workqueue();
7070
fsverity_init_sysctl();
7171
fsverity_init_signature();
72+
fsverity_init_bpf();
7273
return 0;
7374
}
7475
late_initcall(fsverity_init)

fs/verity/measure.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "fsverity_private.h"
99

10+
#include <linux/bpf.h>
11+
#include <linux/btf.h>
1012
#include <linux/uaccess.h>
1113

1214
/**
@@ -100,3 +102,85 @@ int fsverity_get_digest(struct inode *inode,
100102
return hash_alg->digest_size;
101103
}
102104
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

Comments
 (0)