bpf: enforce returning 0 for fentry/fexit programs
Subsystems (4)
cover bpf: enforce returning 0 for fentry/fexit programs
Currently, tracing fentry/fexit program return value can be anything, and these return values are actually ignored by trampoline codes. Let us force return value to be 0 to avoid confusion and allow possible future extension. Patch #1 is the kernel change and Patch #2 fixed the selftest.
Yonghong Song (2):
bpf: enforce returning 0 for fentry/fexit progs selftests/bpf: enforce returning 0 for fentry/fexit programs
kernel/bpf/verifier.c | 7 +++++++ tools/testing/selftests/bpf/progs/test_overhead.c | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-)
Signature · 1 line
2.24.1
PATCH 1/2 bpf: enforce returning 0 for fentry/fexit progs
Currently, tracing/fentry and tracing/fexit prog return values are not enforced. In trampoline codes, the fentry/fexit prog return values are ignored. Let us enforce it to be 0 to avoid confusion and allows potential future extension.
--- kernel/bpf/verifier.c | 7 +++++++ 1 file changed, 7 insertions(+)
kernel/bpf/verifier.c +7 -0
@@ -7059,6 +7059,13 @@ static int check_return_code(struct bpf_verifier_env *env)return 0 ; range =tnum_const (0 );break ; +case BPF_PROG_TYPE_TRACING : +if (env->prog->expected_attach_type ==BPF_TRACE_FENTRY || + env->prog->expected_attach_type ==BPF_TRACE_FEXIT ) { + range =tnum_const (0 ); +break ; + } +return 0 ;default :return 0 ; }
Signature · 1 line
2.24.1
Re: [PATCH bpf 1/2] bpf: enforce returning 0 for fentry/fexit progs
Quoted 18 lines · Yonghong Song
Currently, tracing/fentry and tracing/fexit prog return values are not enforced. In trampoline codes, the fentry/fexit prog return values are ignored. Let us enforce it to be 0 to avoid confusion and allows potential future extension.
Fixes fec56f5890d9 ("bpf: Introduce BPF trampoline")Signed-off-by Yonghong Song <[email protected]>--- kernel/bpf/verifier.c | 7 +++++++ 1 file changed, 7 insertions(+)kernel/bpf/verifier.c +0
-0
kernel/bpf/verifier.c static int check_return_code(struct bpf_verifier_env *env) · line 7068
Quoted 10 lines
kernel/bpf/verifier.c +7
-0@@ -7059,6 +7059,13 @@ static int check_return_code(struct bpf_verifier_env *env)return 0 ; range =tnum_const (0 );break ; +case BPF_PROG_TYPE_TRACING : +if (env->prog->expected_attach_type ==BPF_TRACE_FENTRY || + env->prog->expected_attach_type ==BPF_TRACE_FEXIT ) { + range =tnum_const (0 ); +break ; + } +return 0 ;
I find such if conditions without explicitly handling "else" case very error-prone and easy to miss when adding new functionality. Having an explicit switch with all known cases handled and default failing seems best. WDYT?
E.g., in this case
case BPF_PROG_TYPE_TRACING:
switch (env->prog->expected_attach_type) {
case BPF_TRACE_FENTRY:
case BPF_TRACE_FEXIT:
range = tnum_const(0);
break;
case BPF_MODIFY_RETURN:
break;
default:
return -ENOTSUPP;
}This way if someone adds new tracing sub-type, they will need to explicitly decide what to do with exit codes.
Quoted 6 lines
kernel/bpf/verifier.c +0
-1default :return 0 ; } --2.24.1
Re: [PATCH bpf 1/2] bpf: enforce returning 0 for fentry/fexit progs
Quoted 27 lines · Andrii Nakryiko
Quoted 26 lines · Yonghong Song
Currently, tracing/fentry and tracing/fexit prog return values are not enforced. In trampoline codes, the fentry/fexit prog return values are ignored. Let us enforce it to be 0 to avoid confusion and allows potential future extension.
Fixes fec56f5890d9 ("bpf: Introduce BPF trampoline")Signed-off-by Yonghong Song <[email protected]>--- kernel/bpf/verifier.c | 7 +++++++ 1 file changed, 7 insertions(+)kernel/bpf/verifier.c +5
-0@@ -7059,6 +7059,13 @@ static int check_return_code(struct bpf_verifier_env *env)return 0 ; range =tnum_const (0 );break ; +case BPF_PROG_TYPE_TRACING : +if (env->prog->expected_attach_type ==BPF_TRACE_FENTRY || + env->prog->expected_attach_type ==BPF_TRACE_FEXIT ) { + range =tnum_const (0 ); +break ;
Quoted 8 lines
kernel/bpf/verifier.c static int check_return_code(struct bpf_verifier_env *env) · line 7068
Quoted 2 lines
kernel/bpf/verifier.c +2
-0+ } +return 0 ;I find such if conditions without explicitly handling "else" case very error-prone and easy to miss when adding new functionality. Having an explicit switch with all known cases handled and default failing seems best. WDYT?
Make sense. Will send v2.
Quoted 24 lines
E.g., in this case
case
BPF_PROG_TYPE_TRACING:switch (env->prog->expected_attach_type) { case BPF_TRACE_FENTRY: case BPF_TRACE_FEXIT: range = tnum_const(0); break; case BPF_MODIFY_RETURN: break; default: return -ENOTSUPP; }This way if someone adds new tracing sub-type, they will need to explicitly decide what to do with exit codes.
Quoted 6 lines
kernel/bpf/verifier.c +0
-1default :return 0 ; } --2.24.1
PATCH 2/2 selftests/bpf: enforce returning 0 for fentry/fexit programs
In reply to Yonghong SongThere are a few fentry/fexit programs returning non-0. The tests with these programs will break with the previous patch which enfoced return-0 rules. Fix them properly.
--- tools/testing/selftests/bpf/progs/test_overhead.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/test_overhead.c +2 -2
@@ -30,13 +30,13 @@ int prog3(struct bpf_raw_tracepoint_args *ctx) SEC("fentry/__set_task_comm")int BPF_PROG (prog4 ,struct task_struct *tsk,const char *buf,bool exec) { -return !tsk; +return 0 ; } SEC("fexit/__set_task_comm")int BPF_PROG (prog5 ,struct task_struct *tsk,const char *buf,bool exec) { -return !tsk; +return 0 ; } char _license[] SEC("license") = "GPL";
Signature · 1 line
2.24.1