Skip to content

Commit 6f64e47

Browse files
sinkapAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Update verification logic for LSM programs
The current logic checks if the name of the BTF type passed in attach_btf_id starts with "bpf_lsm_", this is not sufficient as it also allows attachment to non-LSM hooks like the very function that performs this check, i.e. bpf_lsm_verify_prog. In order to ensure that this verification logic allows attachment to only LSM hooks, the LSM_HOOK definitions in lsm_hook_defs.h are used to generate a BTF_ID set. Upon verification, the attach_btf_id of the program being attached is checked for presence in this set. Fixes: 9e4e01d ("bpf: lsm: Implement attach, detach and execution") Signed-off-by: KP Singh <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent d3bec01 commit 6f64e47

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

kernel/bpf/bpf_lsm.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/bpf_verifier.h>
1414
#include <net/bpf_sk_storage.h>
1515
#include <linux/bpf_local_storage.h>
16+
#include <linux/btf_ids.h>
1617

1718
/* For every LSM hook that allows attachment of BPF programs, declare a nop
1819
* function where a BPF program can be attached.
@@ -26,7 +27,11 @@ noinline RET bpf_lsm_##NAME(__VA_ARGS__) \
2627
#include <linux/lsm_hook_defs.h>
2728
#undef LSM_HOOK
2829

29-
#define BPF_LSM_SYM_PREFX "bpf_lsm_"
30+
#define LSM_HOOK(RET, DEFAULT, NAME, ...) BTF_ID(func, bpf_lsm_##NAME)
31+
BTF_SET_START(bpf_lsm_hooks)
32+
#include <linux/lsm_hook_defs.h>
33+
#undef LSM_HOOK
34+
BTF_SET_END(bpf_lsm_hooks)
3035

3136
int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
3237
const struct bpf_prog *prog)
@@ -37,8 +42,7 @@ int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
3742
return -EINVAL;
3843
}
3944

40-
if (strncmp(BPF_LSM_SYM_PREFX, prog->aux->attach_func_name,
41-
sizeof(BPF_LSM_SYM_PREFX) - 1)) {
45+
if (!btf_id_set_contains(&bpf_lsm_hooks, prog->aux->attach_btf_id)) {
4246
bpf_log(vlog, "attach_btf_id %u points to wrong type name %s\n",
4347
prog->aux->attach_btf_id, prog->aux->attach_func_name);
4448
return -EINVAL;

0 commit comments

Comments
 (0)