bpf: Show precise rejected function when attaching to __noreturn and deny list functions
Subsystems (4)
v3 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. Migrate fexit_noreturns case into tracing_failure test suite.
changes:
v3:
- add tracing_deny case into existing files (Alexei)
- migrate fexit_noreturns into tracing_failure
- change SOB
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]/
---
KaFai Wan (4):
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
selftests/bpf: Migrate fexit_noreturns case into tracing_failure test
suite
kernel/bpf/verifier.c | 5 +- .../bpf/prog_tests/fexit_noreturns.c | 9 ---- .../bpf/prog_tests/tracing_failure.c | 52 +++++++++++++++++++ .../selftests/bpf/progs/fexit_noreturns.c | 15 ------ .../selftests/bpf/progs/tracing_failure.c | 12 +++++ 5 files changed, 68 insertions(+), 25 deletions(-) delete mode 100644 tools/testing/selftests/bpf/prog_tests/fexit_noreturns.c delete mode 100644 tools/testing/selftests/bpf/progs/fexit_noreturns.c
Signature · 1 line
2.43.0
PATCH v3 1/4 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
@@ -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
@@ -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
Re: [PATCH bpf-next v3 1/4] bpf: Show precise rejected function when attaching fexit/fmod_ret to __noreturn functions
Quoted 11 lines · KaFai Wan
With this change, we know the precise rejected function name when attaching fexit/fmod_ret to
__noreturnfunctions 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.Suggested-by Leon Hwang <[email protected]>Signed-off-by KaFai Wan <[email protected]>
Quoted 35 lines
--- 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@@ -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
-2@@ -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 ; --2.43.0
Signature · 2 lines
Regards Yafang
Re: [PATCH bpf-next v3 1/4] bpf: Show precise rejected function when attaching fexit/fmod_ret to __noreturn functions
In reply to KaFai WanQuoted 10 lines · KaFai Wan
With this change, we know the precise rejected function name when attaching fexit/fmod_ret to
__noreturnfunctions 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.Suggested-by Leon Hwang <[email protected]>Signed-off-by KaFai Wan <[email protected]>
PATCH v3 2/4 bpf: Add log for attaching tracing programs to functions in deny list
In reply to KaFai WanShow 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
@@ -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
Re: [PATCH bpf-next v3 2/4] bpf: Add log for attaching tracing programs to functions in deny list
Quoted 7 lines · 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.
Quoted 7 lines
$ ./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.Suggested-by Leon Hwang <[email protected]>Signed-off-by KaFai Wan <[email protected]>
Quoted 20 lines
--- kernel/bpf/verifier.c | 2 ++ 1 file changed, 2 insertions(+)kernel/bpf/verifier.c +2
-1@@ -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) && --2.43.0
Signature · 2 lines
Regards Yafang
Re: [PATCH bpf-next v3 2/4] bpf: Add log for attaching tracing programs to functions in deny list
In reply to KaFai WanQuoted 6 lines · 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.
Quoted 7 lines
$ ./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.Suggested-by Leon Hwang <[email protected]>Signed-off-by KaFai Wan <[email protected]>
PATCH v3 3/4 selftests/bpf: Add selftest for attaching tracing programs to functions in deny list
In reply to KaFai WanThe result:
$ tools/testing/selftests/bpf/test_progs -t tracing_failure/tracing_deny #468/3 tracing_failure/tracing_deny:OK #468 tracing_failure:OK Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
--- .../bpf/prog_tests/tracing_failure.c | 33 +++++++++++++++++++ .../selftests/bpf/progs/tracing_failure.c | 6 ++++ 2 files changed, 39 insertions(+)
tools/testing/selftests/bpf/prog_tests/tracing_failure.c +33 -0
@@ -28,10 +28,43 @@ static void test_bpf_spin_lock(bool is_spin_lock)tracing_failure__destroy (skel); } +static void test_tracing_deny (void ) +{ +struct tracing_failure *skel; +char log_buf[256 ]; +int btf_id, err; + +/* migrate_disable depends on CONFIG_SMP */ + btf_id =libbpf_find_vmlinux_btf_id ("migrate_disable" ,BPF_TRACE_FENTRY ); +if (btf_id <=0 ) { +test__skip (); +return ; + } + + skel =tracing_failure__open (); +if (!ASSERT_OK_PTR (skel,"tracing_failure__open" )) +return ; + +bpf_program__set_autoload (skel->progs.tracing_deny, true); +bpf_program__set_log_buf (skel->progs.tracing_deny, log_buf,sizeof (log_buf)); + + err =tracing_failure__load (skel); +if (!ASSERT_ERR (err,"tracing_failure__load" )) + goto out; + +ASSERT_HAS_SUBSTR (log_buf, +"Attaching tracing programs to function 'migrate_disable' is rejected." , +"log_buf" ); +out: +tracing_failure__destroy (skel); +} +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 (); }
tools/testing/selftests/bpf/progs/tracing_failure.c +6 -0
@@ -18,3 +18,9 @@ int BPF_PROG(test_spin_unlock, struct bpf_spin_lock *lock) {return 0 ; } + +SEC("?fentry/migrate_disable") +int BPF_PROG (tracing_deny ) +{ +return 0 ; +}
Signature · 1 line
2.43.0
Re: [PATCH bpf-next v3 3/4] selftests/bpf: Add selftest for attaching tracing programs to functions in deny list
Quoted 8 lines · KaFai Wan
The result:
$ tools/testing/selftests/bpf/test_progs -t tracing_failure/tracing_deny #468/3 tracing_failure/tracing_deny:OK #468 tracing_failure:OK Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILEDSigned-off-by KaFai Wan <[email protected]>
LGTM but see a nit below.
Quoted 15 lines
--- .../bpf/prog_tests/tracing_failure.c | 33 +++++++++++++++++++ .../selftests/bpf/progs/tracing_failure.c | 6 ++++ 2 files changed, 39 insertions(+)tools/testing/selftests/bpf/prog_tests/tracing_failure.c +2
-0@@ -28,10 +28,43 @@ static void test_bpf_spin_lock(bool is_spin_lock)tracing_failure__destroy (skel); } +static void test_tracing_deny (void ) +{
tools/testing/selftests/bpf/prog_tests/tracing_failure.c static void test_bpf_spin_lock(bool is_spin_lock) · line 42
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tracing_failure.c +10
-0+struct tracing_failure *skel; +char log_buf[256 ]; +int btf_id, err; + +/* migrate_disable depends on CONFIG_SMP */ + btf_id =libbpf_find_vmlinux_btf_id ("migrate_disable" ,BPF_TRACE_FENTRY ); +if (btf_id <=0 ) { +test__skip (); +return ; + }
There is a discussion about inlining migrate_disable(). See
https://lore.kernel.org/bpf/CAADnVQ+Afov4E=9t=3M=zZmO9z4ZqT6imWD5xijDHshTf3J=RA@mail.gmail.com/
Maybe trying to find a different function? Otherwise, if migrate_disable is inlined and this test will become useless.
Quoted 42 lines
tools/testing/selftests/bpf/prog_tests/tracing_failure.c +21
-0+ + skel =tracing_failure__open (); +if (!ASSERT_OK_PTR (skel,"tracing_failure__open" )) +return ; + +bpf_program__set_autoload (skel->progs.tracing_deny, true); +bpf_program__set_log_buf (skel->progs.tracing_deny, log_buf,sizeof (log_buf)); + + err =tracing_failure__load (skel); +if (!ASSERT_ERR (err,"tracing_failure__load" )) + goto out; + +ASSERT_HAS_SUBSTR (log_buf, +"Attaching tracing programs to function 'migrate_disable' is rejected." , +"log_buf" ); +out: +tracing_failure__destroy (skel); +} +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 (); }tools/testing/selftests/bpf/progs/tracing_failure.c +6
-0@@ -18,3 +18,9 @@ int BPF_PROG(test_spin_unlock, struct bpf_spin_lock *lock) {return 0 ; } + +SEC("?fentry/migrate_disable") +int BPF_PROG (tracing_deny ) +{ +return 0 ; +}
Re: [PATCH bpf-next v3 3/4] selftests/bpf: Add selftest for attaching tracing programs to functions in deny list
Quoted 52 lines · Yonghong Song
Quoted 9 lines · KaFai Wan
The result:
$ tools/testing/selftests/bpf/test_progs -t tracing_failure/tracing_deny #468/3 tracing_failure/tracing_deny:OK #468 tracing_failure:OK Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILEDSigned-off-by KaFai Wan <[email protected]>LGTM but see a nit below.
Acked-by Yonghong Song <[email protected]>Quoted 26 lines
---
.../bpf/prog_tests/tracing_failure.c | 33
+++++++++++++++++++
.../selftests/bpf/progs/tracing_failure.c | 6 ++++2 files changed, 39 insertions(+)diff --git
a/tools/testing/selftests/bpf/prog_tests/tracing_failure.cb/tools/testing/selftests/bpf/prog_tests/tracing_failure.cindexa222df765bc3..140fb0d175cf100644tools/testing/selftests/bpf/prog_tests/tracing_failure.c +0
-0@@ -28,10 +28,43 @@ static void test_bpf_spin_lock(bool is_spin_lock) tracing_failure__destroy(skel); }tools/testing/selftests/bpf/prog_tests/tracing_failure.c +8
-0+static void test_tracing_deny (void ) +{ +struct tracing_failure *skel; +char log_buf[256 ]; +int btf_id, err; + +/* migrate_disable depends on CONFIG_SMP */ + btf_id = libbpf_find_vmlinux_btf_id("migrate_disable",
BPF_TRACE_FENTRY);tools/testing/selftests/bpf/prog_tests/tracing_failure.c static void test_bpf_spin_lock(bool is_spin_lock) · line 42
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/tracing_failure.c +4
-0+if (btf_id <=0 ) { +test__skip (); +return ; + }There is a discussion about inlining
migrate_disable(). See
https://lore.kernel.org/bpf/CAADnVQ+Afov4E=9t=3M=zZmO9z4ZqT6imWD5xijDHshTf3J=RA@mail.gmail.com/
Quoted 4 lines
Maybe trying to find a different function? Otherwise, if
migrate_disable
is inlined and this test will become useless.
Okey, I will use __rcu_read_lock() instead.
Quoted 47 lines
Quoted 46 lines
tools/testing/selftests/bpf/prog_tests/tracing_failure.c +7
-0+ + skel =tracing_failure__open (); +if (!ASSERT_OK_PTR (skel,"tracing_failure__open" )) +return ; + +bpf_program__set_autoload (skel->progs.tracing_deny, true); + bpf_program__set_log_buf(skel->progs.tracing_deny,
log_buf,sizeof(log_buf));tools/testing/selftests/bpf/prog_tests/tracing_failure.c +7
-0+ + err =tracing_failure__load (skel); +if (!ASSERT_ERR (err,"tracing_failure__load" )) + goto out; + + ASSERT_HAS_SUBSTR(log_buf, + "Attaching tracing programs to function'
migrate_disable' is rejected.",tools/testing/selftests/bpf/prog_tests/tracing_failure.c +5
-0+ "log_buf"); +out: +tracing_failure__destroy (skel); +} +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);tools/testing/selftests/bpf/prog_tests/tracing_failure.c +2
-0+if (test__start_subtest ("tracing_deny" )) +test_tracing_deny ();}
tools/testing/selftests/bpf/progs/tracing_failure.c +0
-0@@ -18,3 +18,9 @@ int BPF_PROG(test_spin_unlock, struct bpf_spin_lock *lock) { return 0;}
tools/testing/selftests/bpf/progs/tracing_failure.c +6
-0+ +SEC("?fentry/migrate_disable") +int BPF_PROG (tracing_deny ) +{ +return 0 ; +}
Signature · 2 lines
Thanks, KaFai
PATCH v3 4/4 selftests/bpf: Migrate fexit_noreturns case into tracing_failure test suite
In reply to KaFai WanDelete fexit_noreturns.c files and migrate the cases into tracing_failure.c files.
The result:
$ tools/testing/selftests/bpf/test_progs -t tracing_failure/fexit_noreturns #467/4 tracing_failure/fexit_noreturns:OK #467 tracing_failure:OK Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
--- .../bpf/prog_tests/fexit_noreturns.c | 9 ---- .../bpf/prog_tests/tracing_failure.c | 47 +++++++++++++------ .../selftests/bpf/progs/fexit_noreturns.c | 15 ------ .../selftests/bpf/progs/tracing_failure.c | 6 +++ 4 files changed, 39 insertions(+), 38 deletions(-) delete mode 100644 tools/testing/selftests/bpf/prog_tests/fexit_noreturns.c delete mode 100644 tools/testing/selftests/bpf/progs/fexit_noreturns.c
tools/testing/selftests/bpf/prog_tests/fexit_noreturns.c deleted +0 -9
@@ -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); -}
tools/testing/selftests/bpf/prog_tests/tracing_failure.c +33 -14
74 lines not on this page · raw message
tools/testing/selftests/bpf/progs/fexit_noreturns.c deleted +0 -15
@@ -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 function 'do_exit' is rejected.") -int BPF_PROG(noreturns) -{ -return 0 ; -}
tools/testing/selftests/bpf/progs/tracing_failure.c +6 -0
@@ -24,3 +24,9 @@ int BPF_PROG(tracing_deny) {return 0 ; } + +SEC("?fexit/do_exit") +int BPF_PROG (fexit_noreturns ) +{ +return 0 ; +}
Signature · 1 line
2.43.0
Re: [PATCH bpf-next v3 4/4] selftests/bpf: Migrate fexit_noreturns case into tracing_failure test suite
Quoted 11 lines · KaFai Wan
Delete
fexit_noreturns.cfiles and migrate the cases intotracing_failure.cfiles.The result:
$ tools/testing/selftests/bpf/test_progs -t tracing_failure/fexit_noreturns #467/4 tracing_failure/fexit_noreturns:OK #467 tracing_failure:OK Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILEDSigned-off-by KaFai Wan <[email protected]>