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/20250722153434.20571-1-kafai.wan@linux.dev/

v2:
- change verifier log message (Alexei)
- add missing Suggested-by
  https://lore.kernel.org/bpf/20250714120408.1627128-1-mannkafai@gmail.com/

v1:
 https://lore.kernel.org/all/20250710162717.3808020-1-mannkafai@gmail.com/
---
====================

Link: https://patch.msgid.link/20250724151454.499040-1-kafai.wan@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
pull/1309/head
Alexei Starovoitov 2025-07-28 19:39:30 -07:00
commit cd7c97f458
5 changed files with 68 additions and 25 deletions

View File

@ -23981,11 +23981,14 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
return ret;
} else if (prog->type == BPF_PROG_TYPE_TRACING &&
btf_id_set_contains(&btf_id_deny, btf_id)) {
verbose(env, "Attaching tracing programs to function '%s' is rejected.\n",
tgt_info.tgt_name);
return -EINVAL;
} else if ((prog->expected_attach_type == BPF_TRACE_FEXIT ||
prog->expected_attach_type == BPF_MODIFY_RETURN) &&
btf_id_set_contains(&noreturn_deny, btf_id)) {
verbose(env, "Attaching fexit/fmod_ret to __noreturn functions is rejected.\n");
verbose(env, "Attaching fexit/fmod_ret to __noreturn function '%s' is rejected.\n",
tgt_info.tgt_name);
return -EINVAL;
}

View File

@ -1,9 +0,0 @@
// SPDX-License-Identifier: GPL-2.0
#include <test_progs.h>
#include "fexit_noreturns.skel.h"
void test_fexit_noreturns(void)
{
RUN_TESTS(fexit_noreturns);
}

View File

@ -28,10 +28,62 @@ out:
tracing_failure__destroy(skel);
}
static void test_tracing_fail_prog(const char *prog_name, const char *exp_msg)
{
struct tracing_failure *skel;
struct bpf_program *prog;
char log_buf[256];
int err;
skel = tracing_failure__open();
if (!ASSERT_OK_PTR(skel, "tracing_failure__open"))
return;
prog = bpf_object__find_program_by_name(skel->obj, prog_name);
if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
goto out;
bpf_program__set_autoload(prog, true);
bpf_program__set_log_buf(prog, log_buf, sizeof(log_buf));
err = tracing_failure__load(skel);
if (!ASSERT_ERR(err, "tracing_failure__load"))
goto out;
ASSERT_HAS_SUBSTR(log_buf, exp_msg, "log_buf");
out:
tracing_failure__destroy(skel);
}
static void test_tracing_deny(void)
{
int btf_id;
/* __rcu_read_lock depends on CONFIG_PREEMPT_RCU */
btf_id = libbpf_find_vmlinux_btf_id("__rcu_read_lock", BPF_TRACE_FENTRY);
if (btf_id <= 0) {
test__skip();
return;
}
test_tracing_fail_prog("tracing_deny",
"Attaching tracing programs to function '__rcu_read_lock' is rejected.");
}
static void test_fexit_noreturns(void)
{
test_tracing_fail_prog("fexit_noreturns",
"Attaching fexit/fmod_ret to __noreturn function 'do_exit' is rejected.");
}
void test_tracing_failure(void)
{
if (test__start_subtest("bpf_spin_lock"))
test_bpf_spin_lock(true);
if (test__start_subtest("bpf_spin_unlock"))
test_bpf_spin_lock(false);
if (test__start_subtest("tracing_deny"))
test_tracing_deny();
if (test__start_subtest("fexit_noreturns"))
test_fexit_noreturns();
}

View File

@ -1,15 +0,0 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "bpf_misc.h"
char _license[] SEC("license") = "GPL";
SEC("fexit/do_exit")
__failure __msg("Attaching fexit/fmod_ret to __noreturn functions is rejected.")
int BPF_PROG(noreturns)
{
return 0;
}

View File

@ -18,3 +18,15 @@ int BPF_PROG(test_spin_unlock, struct bpf_spin_lock *lock)
{
return 0;
}
SEC("?fentry/__rcu_read_lock")
int BPF_PROG(tracing_deny)
{
return 0;
}
SEC("?fexit/do_exit")
int BPF_PROG(fexit_noreturns)
{
return 0;
}