Skip to contentSkip to conversation

bpf: Show precise rejected function when attaching to __noreturn and deny list functions

PATCH In review KaFai Wan 2 participants 6 messages Jul 14, 2025 – Jul 17, 2025

Subsystems (4)
lore
Share & tools
lore

KaFai Wan

author
#0 lore raw

v2 cover 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.

changes:
v2:
- change verifier log message (Alexei)
- add missing Suggested-by

v1:
https://lore.kernel.org/all/[email protected]/

---
KaFai Wan (3):
bpf: Show precise rejected function when attaching fexit/fmod_ret to
__noreturn functions
bpf: Add log for attaching tracing programs to functions in deny list selftests/bpf: Add selftest for attaching tracing programs to
functions in deny list

 kernel/bpf/verifier.c                             |  5 ++++-
 .../selftests/bpf/prog_tests/tracing_deny.c       | 11 +++++++++++
 .../testing/selftests/bpf/progs/fexit_noreturns.c |  2 +-
 tools/testing/selftests/bpf/progs/tracing_deny.c  | 15 +++++++++++++++
 4 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tracing_deny.c
 create mode 100644 tools/testing/selftests/bpf/progs/tracing_deny.c
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/3 bpf: Show precise rejected function when attaching fexit/fmod_ret to __noreturn functions

With this change, we know the precise rejected function name when attaching fexit/fmod_ret to __noreturn functions from log.

$ ./fexit
libbpf: prog 'fexit': BPF program load failed: -EINVAL
libbpf: prog 'fexit': -- BEGIN PROG LOAD LOG --
Attaching fexit/fmod_ret to __noreturn function 'do_exit' is rejected.
---
 kernel/bpf/verifier.c                               | 3 ++-
 tools/testing/selftests/bpf/progs/fexit_noreturns.c | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +2 -1
index e2fcea860755..00d287814f12 100644
@@ -23946,7 +23946,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
 	} 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;
 	}
 
tools/testing/selftests/bpf/progs/fexit_noreturns.c +1 -1
index 54654539f550..b1c33d958ae2 100644
@@ -8,7 +8,7 @@
 char _license[] SEC("license") = "GPL";
 
 SEC("fexit/do_exit")
-__failure __msg("Attaching fexit/fmod_ret to __noreturn functions is rejected.")
+__failure __msg("Attaching fexit/fmod_ret to __noreturn function 'do_exit' is rejected.")
 int BPF_PROG(noreturns)
 {
 	return 0;
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v2 2/3 bpf: Add log for attaching tracing programs to functions in deny list

In reply to KaFai Wan

Show the rejected function name when attaching tracing programs to functions in deny list.

With this change, we know why tracing programs can't attach to functions like migrate_disable() from log.

$ ./fentry
libbpf: prog 'migrate_disable': BPF program load failed: -EINVAL
libbpf: prog 'migrate_disable': -- BEGIN PROG LOAD LOG --
Attaching tracing programs to function 'migrate_disable' is rejected.
---
 kernel/bpf/verifier.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/bpf/verifier.c +2 -0
index 00d287814f12..c24c0d57e595 100644
@@ -23942,6 +23942,8 @@ 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) &&
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v2 3/3 selftests/bpf: Add selftest for attaching tracing programs to functions in deny list

In reply to KaFai Wan

The reuslt:

  $ tools/testing/selftests/bpf/test_progs --name=tracing_deny
  #467/1   tracing_deny/migrate_disable:OK
  #467     tracing_deny:OK
  Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
---
 .../selftests/bpf/prog_tests/tracing_deny.c       | 11 +++++++++++
 tools/testing/selftests/bpf/progs/tracing_deny.c  | 15 +++++++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tracing_deny.c
 create mode 100644 tools/testing/selftests/bpf/progs/tracing_deny.c
tools/testing/selftests/bpf/prog_tests/tracing_deny.c new file +11 -0
new file mode 100644
index 000000000000..460c59a9667f
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "tracing_deny.skel.h"
+
+void test_tracing_deny(void)
+{
+	/* migrate_disable depends on CONFIG_SMP */
+	if (libbpf_find_vmlinux_btf_id("migrate_disable", BPF_TRACE_FENTRY) > 0)
+		RUN_TESTS(tracing_deny);
+}
tools/testing/selftests/bpf/progs/tracing_deny.c new file +15 -0
new file mode 100644
index 000000000000..98ef834f0b6d
@@ -0,0 +1,15 @@
+// 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("fentry/migrate_disable")
+__failure __msg("Attaching tracing programs to function 'migrate_disable' is rejected.")
+int BPF_PROG(migrate_disable)
+{
+	return 0;
+}
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH bpf-next v2 3/3] selftests/bpf: Add selftest for attaching tracing programs to functions in deny list

Quoted 44 lines · KaFai Wan

The reuslt:

  $ tools/testing/selftests/bpf/test_progs --name=tracing_deny
  #467/1   tracing_deny/migrate_disable:OK
  #467     tracing_deny:OK
  Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
---
 .../selftests/bpf/prog_tests/tracing_deny.c       | 11 +++++++++++
 tools/testing/selftests/bpf/progs/tracing_deny.c  | 15 +++++++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tracing_deny.c
 create mode 100644 tools/testing/selftests/bpf/progs/tracing_deny.c
tools/testing/selftests/bpf/prog_tests/tracing_deny.c new file +11 -0
new file mode 100644
index 000000000000..460c59a9667f
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "tracing_deny.skel.h"
+
+void test_tracing_deny(void)
+{
+       /* migrate_disable depends on CONFIG_SMP */
+       if (libbpf_find_vmlinux_btf_id("migrate_disable", BPF_TRACE_FENTRY) > 0)
+               RUN_TESTS(tracing_deny);
+}
tools/testing/selftests/bpf/progs/tracing_deny.c new file +5 -0
new file mode 100644
index 000000000000..98ef834f0b6d
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>

tools/testing/selftests/bpf/progs/tracing_deny.c line 15

Quoted 10 lines
tools/testing/selftests/bpf/progs/tracing_deny.c new file +10 -0
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("fentry/migrate_disable")
+__failure __msg("Attaching tracing programs to function 'migrate_disable' is rejected.")
+int BPF_PROG(migrate_disable)
+{
+       return 0;
+}

Please roll these two tiny files into existing files in progs/ and prog_tests/ directories.
Every file takes time to compile 4 times, so let's avoid unnecessary overhead.

--
pw-bot: cr

author
#5 lore raw

Re: [PATCH bpf-next v2 3/3] selftests/bpf: Add selftest for attaching tracing programs to functions in deny list

Quoted 61 lines · Alexei Starovoitov
Quoted 59 lines · KaFai Wan

The reuslt:

  $ tools/testing/selftests/bpf/test_progs --name=tracing_deny
  #467/1   tracing_deny/migrate_disable:OK
  #467     tracing_deny:OK
  Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED

---
 .../selftests/bpf/prog_tests/tracing_deny.c       | 11 +++++++++++ tools/testing/selftests/bpf/progs/tracing_deny.c  | 15
+++++++++++++++

 2 files changed, 26 insertions(+)

 create mode 100644 tools/testing/selftests/bpf/prog_tests/tracing_deny.c create mode 100644 tools/testing/selftests/bpf/progs/tracing_deny.c

tools/testing/selftests/bpf/prog_tests/tracing_deny.c new file +9 -0
new file mode 100644
index 000000000000..460c59a9667f
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "tracing_deny.skel.h"
+
+void test_tracing_deny(void)
+{
+       /* migrate_disable depends on CONFIG_SMP */
+       if (libbpf_find_vmlinux_btf_id("migrate_disable",

BPF_TRACE_FENTRY) > 0)
+               RUN_TESTS(tracing_deny);

tools/testing/selftests/bpf/prog_tests/tracing_deny.c new file +1 -0
+}
tools/testing/selftests/bpf/progs/tracing_deny.c new file +11 -0
new file mode 100644
index 000000000000..98ef834f0b6d
@@ -0,0 +1,15 @@
+// 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("fentry/migrate_disable")
+__failure __msg("Attaching tracing programs to function

'migrate_disable' is rejected.")

tools/testing/selftests/bpf/progs/tracing_deny.c new file +2 -0
+int BPF_PROG(migrate_disable)
+{
Quoted 9 lines
Quoted 2 lines

+       return 0;
+}

Please roll these two tiny files into existing files in progs/ and prog_tests/
directories.
Every file takes time to compile 4 times, so let's avoid unnecessary overhead.

Okay, will update it in v3.

Quoted 2 lines

--
pw-bot: cr

Signature · 2 lines
Thanks,
KaFai