bpf: Fix error checks against bpf_get_btf_vmlinux().
PATCH 1/1 bpf: Fix error checks against bpf_get_btf_vmlinux().
From: Kui-Feng Lee <[email protected]>
Check whether the returned pointer is NULL. Previously, it was assumed that an error code would be returned if BTF is not available or fails to parse. However, it actually returns NULL if BTF is disabled.
In the function check_struct_ops_btf_id(), we have stopped using btf_vmlinux as a backup because attach_btf is never null when attach_btf_id is set. However, the function test_libbpf_probe_prog_types() in libbpf_probes.c does not set both attach_btf_obj_fd and attach_btf_id, resulting in attach_btf being null, and it expects ENOTSUPP as a result. So, if attach_btf_id is not set, it will return ENOTSUPP.
--- kernel/bpf/bpf_struct_ops.c | 2 ++ kernel/bpf/verifier.c | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-)
kernel/bpf/bpf_struct_ops.c +2 -0
@@ -669,6 +669,8 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) btf =bpf_get_btf_vmlinux ();if (IS_ERR (btf))return ERR_CAST (btf); +if (!btf) +return ERR_PTR (-ENOTSUPP ); } st_ops_desc =bpf_struct_ops_find_value (btf, attr->btf_vmlinux_value_type_id);
kernel/bpf/verifier.c +7 -1
@@ -20298,7 +20298,13 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)return -EINVAL ; } - btf = prog->aux->attach_btf ?:bpf_get_btf_vmlinux (); +if (!prog->aux->attach_btf_id) +return -ENOTSUPP ; + + btf = prog->aux->attach_btf; +if (!btf) +return -ENOTSUPP ; +if (btf_is_module (btf)) {/* Make sure st_ops is valid through the lifetime of env */ env->attach_btf_mod =btf_try_get_module (btf);
Signature · 1 line
2.34.1
Martin KaFai Lau
reviewerQuoted 13 lines · On 1/25/24 3:31 PM, [email protected] wrote:
From: Kui-Feng Lee <[email protected]>
Check whether the returned pointer is NULL. Previously, it was assumed that an error code would be returned if BTF is not available or fails to parse. However, it actually returns NULL if BTF is disabled.
In the function
check_struct_ops_btf_id(), we have stopped usingbtf_vmlinuxas a backup becauseattach_btfis never null whenattach_btf_idis set. However, the functiontest_libbpf_probe_prog_types()inlibbpf_probes.cdoes not set bothattach_btf_obj_fdandattach_btf_id, resulting inattach_btfbeing null, and it expects ENOTSUPP as a result. So, ifattach_btf_idis not set, it will return ENOTSUPP.
Quoted 2 lines
Reported-by [email protected]
There were two different syzbot report. Both should be tagged here as Reported-by.
Quoted 25 lines
Fixes fcc2c1fb0651 ("bpf: pass attached BTF to the bpf_struct_ops subsystem")Signed-off-by Kui-Feng Lee <[email protected]>--- kernel/bpf/bpf_struct_ops.c | 2 ++ kernel/bpf/verifier.c | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-)kernel/bpf/bpf_struct_ops.c +2
-0@@ -669,6 +669,8 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) btf =bpf_get_btf_vmlinux ();if (IS_ERR (btf))return ERR_CAST (btf); +if (!btf) +return ERR_PTR (-ENOTSUPP ); } st_ops_desc =bpf_struct_ops_find_value (btf, attr->btf_vmlinux_value_type_id);kernel/bpf/verifier.c +0
-0
kernel/bpf/verifier.c static int check_struct_ops_btf_id(struct bpf_verifier_env *env) · line 20305
Quoted 9 lines
kernel/bpf/verifier.c +5
-1@@ -20298,7 +20298,13 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)return -EINVAL ; } - btf = prog->aux->attach_btf ?:bpf_get_btf_vmlinux (); +if (!prog->aux->attach_btf_id) +return -ENOTSUPP ; + + btf = prog->aux->attach_btf; +if (!btf)
The commit message mentioned "attach_btf is never null when attach_btf_id is set". Then why this test is still needed when the above has just tested the attach_btf_id. attach_btf must be valid here as long as attach_btf_id is set. This should have been guaranteed by syscall.c, no?
Quoted 5 lines
kernel/bpf/verifier.c +2
-0+return -ENOTSUPP ; +if (btf_is_module (btf)) {/* Make sure st_ops is valid through the lifetime of env */ env->attach_btf_mod =btf_try_get_module (btf);
Quoted 14 lines · Martin KaFai Lau
Quoted 13 lines · On 1/25/24 3:31 PM, [email protected] wrote:
From: Kui-Feng Lee <[email protected]>
Check whether the returned pointer is NULL. Previously, it was assumed that an error code would be returned if BTF is not available or fails to parse. However, it actually returns NULL if BTF is disabled.
In the function
check_struct_ops_btf_id(), we have stopped usingbtf_vmlinuxas a backup becauseattach_btfis never null whenattach_btf_idis set. However, the functiontest_libbpf_probe_prog_types()inlibbpf_probes.cdoes not set bothattach_btf_obj_fdandattach_btf_id, resulting inattach_btfbeing null, and it expects ENOTSUPP as a result. So, ifattach_btf_idis not set, it will return ENOTSUPP.
Quoted 4 lines
Quoted 2 lines
Reported-by [email protected]There were two different syzbot report. Both should be tagged here as Reported-by.
Sure!
Quoted 29 lines
Quoted 29 lines
Fixes fcc2c1fb0651 ("bpf: pass attached BTF to the bpf_struct_ops subsystem")Signed-off-by Kui-Feng Lee <[email protected]>---
kernel/bpf/bpf_struct_ops.c| 2 ++
kernel/bpf/verifier.c | 8 +++++++-2 files changed, 9 insertions(+), 1 deletion(-)kernel/bpf/bpf_struct_ops.c +2
-0@@ -669,6 +669,8 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) btf = bpf_get_btf_vmlinux(); if (IS_ERR(btf)) return ERR_CAST(btf); + if (!btf) + return ERR_PTR(-ENOTSUPP); } st_ops_desc = bpf_struct_ops_find_value(btf, attr->btf_vmlinux_value_type_id);kernel/bpf/verifier.c +0
-0@@ -20298,7 +20298,13 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env) return -EINVAL; }
Quoted 8 lines
kernel/bpf/verifier.c static int check_struct_ops_btf_id(struct bpf_verifier_env *env) · line 20305
Quoted 6 lines
kernel/bpf/verifier.c +5
-1- btf = prog->aux->attach_btf ?: bpf_get_btf_vmlinux(); + if (!prog->aux->attach_btf_id) + return -ENOTSUPP; + + btf = prog->aux->attach_btf; + if (!btf)The commit message mentioned "
attach_btfis never null whenattach_btf_idis set". Then why this test is still needed when the above has just tested theattach_btf_id.attach_btfmust be valid here as long asattach_btf_idis set. This should have been guaranteed bysyscall.c, no?
Yes, you are right.
Quoted 7 lines
Quoted 6 lines
kernel/bpf/verifier.c +2
-0+ return -ENOTSUPP; + if (btf_is_module(btf)) { /* Make sure st_ops is valid through the lifetime of env */ env->attach_btf_mod = btf_try_get_module(btf);