bpf: btf: fix truncated last_member_type_id in btf_struct_resolve
PATCH 1/1 bpf: btf: fix truncated last_member_type_id in btf_struct_resolve
When trying to finish resolving a struct member, btf_struct_resolve saves the member type id in a u16 temporary variable. This truncates the 32 bit type id value if it exceeds UINT16_MAX.
As a result, structs that have members with type ids > UINT16_MAX and which need resolution will fail with a message like this:
[67414] STRUCT ff_device size=120 vlen=12
effect_owners type_id=67434 bits_offset=960 Member exceeds struct_size
Fix this by changing the type of last_member_type_id to u32.
--- kernel/bpf/btf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/btf.c +1 -1
@@ -3128,7 +3128,7 @@ static int btf_struct_resolve(struct btf_verifier_env *env,if (v->next_member) {const struct btf_type *last_member_type;const struct btf_member *last_member; -u16 last_member_type_id; +u32 last_member_type_id; last_member =btf_type_member (v->t) + v->next_member -1 ; last_member_type_id = last_member->type;
Signature · 1 line
2.34.1
Quoted 4 lines · Lorenz Bauer
When trying to finish resolving a struct member,
btf_struct_resolvesaves the member type id in a u16 temporary variable. This truncates the 32 bit type id value if it exceedsUINT16_MAX.
Quoted 8 lines
As a result, structs that have members with type ids >
UINT16_MAXand which need resolution will fail with a message like this:[67414] STRUCT
ff_devicesize=120 vlen=12
effect_ownerstype_id=67434 bits_offset=960 Member exceedsstruct_sizeFix this by changing the type of
last_member_type_idto u32.
Makes sense to me; I'm surprised the compiler isn't raising a warning on line:last_member_type_id = last_member->type;
Quoted 22 lines
Fixes eb3f595dab40 ("bpf: btf: Validate type reference")Signed-off-by Lorenz Bauer <[email protected]>--- kernel/bpf/btf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/btf.c +1
-1@@ -3128,7 +3128,7 @@ static int btf_struct_resolve(struct btf_verifier_env *env, if (v->next_member) {const struct btf_type *last_member_type;const struct btf_member *last_member; -u16 last_member_type_id; +u32 last_member_type_id; last_member =btf_type_member (v->t) + v->next_member -1 ;
last_member_type_id=last_member->type;
--
2.34.1
Martin KaFai Lau
reviewerQuoted 7 lines · Lorenz Bauer
When trying to finish resolving a struct member,
btf_struct_resolvesaves the member type id in a u16 temporary variable. This truncates the 32 bit type id value if it exceedsUINT16_MAX.As a result, structs that have members with type ids >
UINT16_MAXand which need resolution will fail with a message like this:
Quoted 6 lines
[67414] STRUCT
ff_devicesize=120 vlen=12
effect_ownerstype_id=67434 bits_offset=960 Member exceedsstruct_sizeFix this by changing the type of
last_member_type_idto u32.Fixes eb3f595dab40 ("bpf: btf: Validate type reference")
The fix tag should be
Quoted 10 lines
Signed-off-by Lorenz Bauer <[email protected]>--- kernel/bpf/btf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/btf.c +0
-0
kernel/bpf/btf.c static int btf_struct_resolve(struct btf_verifier_env *env, · line 3131
Quoted 5 lines
kernel/bpf/btf.c +1
-1@@ -3128,7 +3128,7 @@ static int btf_struct_resolve(struct btf_verifier_env *env,if (v->next_member) {const struct btf_type *last_member_type;const struct btf_member *last_member; -u16 last_member_type_id; +u32 last_member_type_id;
The change makes sense.
The kernel's vmlinux and module btf parsing doesn't go through this resolve check though. Are you trying to __sys_bpf(BPF_BTF_LOAD) the btf from the vmlinux file into the kernel ?
Quoted 4 lines · Martin KaFai Lau
The fix tag should be
Fixes a0791f0df7d2 ("bpf: fix BTF limits")
Ah, thanks very much! Sent a v2.
Quoted 5 lines
The change makes sense.
The kernel's vmlinux and module btf parsing doesn't go through this resolve check though. Are you trying to
__sys_bpf(BPF_BTF_LOAD)the btf from the vmlinux file into the kernel ?
Yes, with the twist that the BTF was serialised by github.com/cilium/ebpf instead of libbpf.