selftests/bpf: Add test for lsm tail call
Add test for lsm tail call to ensure tail call can only be used between bpf lsm progs attached to the same hook. Signed-off-by: Xu Kuohai <xukuohai@huawei.com> Link: https://lore.kernel.org/r/20240719110059.797546-9-xukuohai@huaweicloud.com Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
This commit is contained in:
committed by
Andrii Nakryiko
parent
2b23b6c0f0
commit
d463dd9c9a
@@ -12,6 +12,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lsm.skel.h"
|
||||
#include "lsm_tailcall.skel.h"
|
||||
|
||||
char *CMD_ARGS[] = {"true", NULL};
|
||||
|
||||
@@ -95,7 +96,7 @@ static int test_lsm(struct lsm *skel)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void test_test_lsm(void)
|
||||
static void test_lsm_basic(void)
|
||||
{
|
||||
struct lsm *skel = NULL;
|
||||
int err;
|
||||
@@ -114,3 +115,46 @@ void test_test_lsm(void)
|
||||
close_prog:
|
||||
lsm__destroy(skel);
|
||||
}
|
||||
|
||||
static void test_lsm_tailcall(void)
|
||||
{
|
||||
struct lsm_tailcall *skel = NULL;
|
||||
int map_fd, prog_fd;
|
||||
int err, key;
|
||||
|
||||
skel = lsm_tailcall__open_and_load();
|
||||
if (!ASSERT_OK_PTR(skel, "lsm_tailcall__skel_load"))
|
||||
goto close_prog;
|
||||
|
||||
map_fd = bpf_map__fd(skel->maps.jmp_table);
|
||||
if (CHECK_FAIL(map_fd < 0))
|
||||
goto close_prog;
|
||||
|
||||
prog_fd = bpf_program__fd(skel->progs.lsm_file_permission_prog);
|
||||
if (CHECK_FAIL(prog_fd < 0))
|
||||
goto close_prog;
|
||||
|
||||
key = 0;
|
||||
err = bpf_map_update_elem(map_fd, &key, &prog_fd, BPF_ANY);
|
||||
if (CHECK_FAIL(!err))
|
||||
goto close_prog;
|
||||
|
||||
prog_fd = bpf_program__fd(skel->progs.lsm_file_alloc_security_prog);
|
||||
if (CHECK_FAIL(prog_fd < 0))
|
||||
goto close_prog;
|
||||
|
||||
err = bpf_map_update_elem(map_fd, &key, &prog_fd, BPF_ANY);
|
||||
if (CHECK_FAIL(err))
|
||||
goto close_prog;
|
||||
|
||||
close_prog:
|
||||
lsm_tailcall__destroy(skel);
|
||||
}
|
||||
|
||||
void test_test_lsm(void)
|
||||
{
|
||||
if (test__start_subtest("lsm_basic"))
|
||||
test_lsm_basic();
|
||||
if (test__start_subtest("lsm_tailcall"))
|
||||
test_lsm_tailcall();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
/* Copyright (c) 2024 Huawei Technologies Co., Ltd */
|
||||
|
||||
#include "vmlinux.h"
|
||||
#include <errno.h>
|
||||
#include <bpf/bpf_helpers.h>
|
||||
|
||||
char _license[] SEC("license") = "GPL";
|
||||
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
|
||||
__uint(max_entries, 1);
|
||||
__uint(key_size, sizeof(__u32));
|
||||
__uint(value_size, sizeof(__u32));
|
||||
} jmp_table SEC(".maps");
|
||||
|
||||
SEC("lsm/file_permission")
|
||||
int lsm_file_permission_prog(void *ctx)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("lsm/file_alloc_security")
|
||||
int lsm_file_alloc_security_prog(void *ctx)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("lsm/file_alloc_security")
|
||||
int lsm_file_alloc_security_entry(void *ctx)
|
||||
{
|
||||
bpf_tail_call_static(ctx, &jmp_table, 0);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user