Skip to content

Commit cd7c97f

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-show-precise-rejected-function-when-attaching-to-__noreturn-and-deny-list-functions'
KaFai Wan says: ==================== bpf: Show precise rejected function when attaching to __noreturn and deny list functions Show precise rejected function when attaching fexit/fmod_ret to __noreturn functions. Add log for attaching tracing programs to functions in deny list. Add selftest for attaching tracing programs to functions in deny list. Migrate fexit_noreturns case into tracing_failure test suite. changes: v4: - change tracing_deny case attaching function (Yonghong Song) - add Acked-by: Yafang Shao and Yonghong Song v3: - add tracing_deny case into existing files (Alexei) - migrate fexit_noreturns into tracing_failure - change SOB https://lore.kernel.org/bpf/[email protected]/ v2: - change verifier log message (Alexei) - add missing Suggested-by https://lore.kernel.org/bpf/[email protected]/ v1: https://lore.kernel.org/all/[email protected]/ --- ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 5b4c54a + 51d3750 commit cd7c97f

File tree

5 files changed

+68
-25
lines changed

5 files changed

+68
-25
lines changed

kernel/bpf/verifier.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23981,11 +23981,14 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
2398123981
return ret;
2398223982
} else if (prog->type == BPF_PROG_TYPE_TRACING &&
2398323983
btf_id_set_contains(&btf_id_deny, btf_id)) {
23984+
verbose(env, "Attaching tracing programs to function '%s' is rejected.\n",
23985+
tgt_info.tgt_name);
2398423986
return -EINVAL;
2398523987
} else if ((prog->expected_attach_type == BPF_TRACE_FEXIT ||
2398623988
prog->expected_attach_type == BPF_MODIFY_RETURN) &&
2398723989
btf_id_set_contains(&noreturn_deny, btf_id)) {
23988-
verbose(env, "Attaching fexit/fmod_ret to __noreturn functions is rejected.\n");
23990+
verbose(env, "Attaching fexit/fmod_ret to __noreturn function '%s' is rejected.\n",
23991+
tgt_info.tgt_name);
2398923992
return -EINVAL;
2399023993
}
2399123994

tools/testing/selftests/bpf/prog_tests/fexit_noreturns.c

Lines changed: 0 additions & 9 deletions
This file was deleted.

tools/testing/selftests/bpf/prog_tests/tracing_failure.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,62 @@ static void test_bpf_spin_lock(bool is_spin_lock)
2828
tracing_failure__destroy(skel);
2929
}
3030

31+
static void test_tracing_fail_prog(const char *prog_name, const char *exp_msg)
32+
{
33+
struct tracing_failure *skel;
34+
struct bpf_program *prog;
35+
char log_buf[256];
36+
int err;
37+
38+
skel = tracing_failure__open();
39+
if (!ASSERT_OK_PTR(skel, "tracing_failure__open"))
40+
return;
41+
42+
prog = bpf_object__find_program_by_name(skel->obj, prog_name);
43+
if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
44+
goto out;
45+
46+
bpf_program__set_autoload(prog, true);
47+
bpf_program__set_log_buf(prog, log_buf, sizeof(log_buf));
48+
49+
err = tracing_failure__load(skel);
50+
if (!ASSERT_ERR(err, "tracing_failure__load"))
51+
goto out;
52+
53+
ASSERT_HAS_SUBSTR(log_buf, exp_msg, "log_buf");
54+
out:
55+
tracing_failure__destroy(skel);
56+
}
57+
58+
static void test_tracing_deny(void)
59+
{
60+
int btf_id;
61+
62+
/* __rcu_read_lock depends on CONFIG_PREEMPT_RCU */
63+
btf_id = libbpf_find_vmlinux_btf_id("__rcu_read_lock", BPF_TRACE_FENTRY);
64+
if (btf_id <= 0) {
65+
test__skip();
66+
return;
67+
}
68+
69+
test_tracing_fail_prog("tracing_deny",
70+
"Attaching tracing programs to function '__rcu_read_lock' is rejected.");
71+
}
72+
73+
static void test_fexit_noreturns(void)
74+
{
75+
test_tracing_fail_prog("fexit_noreturns",
76+
"Attaching fexit/fmod_ret to __noreturn function 'do_exit' is rejected.");
77+
}
78+
3179
void test_tracing_failure(void)
3280
{
3381
if (test__start_subtest("bpf_spin_lock"))
3482
test_bpf_spin_lock(true);
3583
if (test__start_subtest("bpf_spin_unlock"))
3684
test_bpf_spin_lock(false);
85+
if (test__start_subtest("tracing_deny"))
86+
test_tracing_deny();
87+
if (test__start_subtest("fexit_noreturns"))
88+
test_fexit_noreturns();
3789
}

tools/testing/selftests/bpf/progs/fexit_noreturns.c

Lines changed: 0 additions & 15 deletions
This file was deleted.

tools/testing/selftests/bpf/progs/tracing_failure.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,15 @@ int BPF_PROG(test_spin_unlock, struct bpf_spin_lock *lock)
1818
{
1919
return 0;
2020
}
21+
22+
SEC("?fentry/__rcu_read_lock")
23+
int BPF_PROG(tracing_deny)
24+
{
25+
return 0;
26+
}
27+
28+
SEC("?fexit/do_exit")
29+
int BPF_PROG(fexit_noreturns)
30+
{
31+
return 0;
32+
}

0 commit comments

Comments
 (0)