Skip to content

Commit

Permalink
selftests/bpf: add destructive kfunc test
Browse files Browse the repository at this point in the history
Add a test checking that programs calling destructive kfuncs can only do
so if they have CAP_SYS_BOOT capabilities.

Signed-off-by: Artem Savkov <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Alexei Starovoitov <[email protected]>
  • Loading branch information
sm00th authored and Alexei Starovoitov committed Aug 10, 2022
1 parent 1337905 commit e338945
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions net/bpf/test_run.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,10 @@ noinline void bpf_kfunc_call_test_ref(struct prog_test_ref_kfunc *p)
{
}

noinline void bpf_kfunc_call_test_destructive(void)
{
}

__diag_pop();

ALLOW_ERROR_INJECTION(bpf_modify_return_test, ERRNO);
Expand All @@ -719,6 +723,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_pass1)
BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail1)
BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail2)
BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_TRUSTED_ARGS)
BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE)
BTF_SET8_END(test_sk_check_kfunc_ids)

static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size,
Expand Down
36 changes: 36 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/kfunc_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "kfunc_call_test.lskel.h"
#include "kfunc_call_test_subprog.skel.h"
#include "kfunc_call_test_subprog.lskel.h"
#include "kfunc_call_destructive.skel.h"

#include "cap_helpers.h"

static void test_main(void)
{
Expand Down Expand Up @@ -86,6 +89,36 @@ static void test_subprog_lskel(void)
kfunc_call_test_subprog_lskel__destroy(skel);
}

static int test_destructive_open_and_load(void)
{
struct kfunc_call_destructive *skel;
int err;

skel = kfunc_call_destructive__open();
if (!ASSERT_OK_PTR(skel, "prog_open"))
return -1;

err = kfunc_call_destructive__load(skel);

kfunc_call_destructive__destroy(skel);

return err;
}

static void test_destructive(void)
{
__u64 save_caps = 0;

ASSERT_OK(test_destructive_open_and_load(), "succesful_load");

if (!ASSERT_OK(cap_disable_effective(1ULL << CAP_SYS_BOOT, &save_caps), "drop_caps"))
return;

ASSERT_EQ(test_destructive_open_and_load(), -13, "no_caps_failure");

cap_enable_effective(save_caps, NULL);
}

void test_kfunc_call(void)
{
if (test__start_subtest("main"))
Expand All @@ -96,4 +129,7 @@ void test_kfunc_call(void)

if (test__start_subtest("subprog_lskel"))
test_subprog_lskel();

if (test__start_subtest("destructive"))
test_destructive();
}
14 changes: 14 additions & 0 deletions tools/testing/selftests/bpf/progs/kfunc_call_destructive.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: GPL-2.0
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>

extern void bpf_kfunc_call_test_destructive(void) __ksym;

SEC("tc")
int kfunc_destructive_test(void)
{
bpf_kfunc_call_test_destructive();
return 0;
}

char _license[] SEC("license") = "GPL";

0 comments on commit e338945

Please sign in to comment.