]> git.ipfire.org Git - thirdparty/linux.git/commit
libbpf: fix LDX/STX/ST CO-RE relocation size adjustment logic
authorAndrii Nakryiko <andrii@kernel.org>
Fri, 7 Feb 2025 01:48:08 +0000 (17:48 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Sat, 15 Feb 2025 03:58:05 +0000 (19:58 -0800)
commit06096d19ee3897a7e70922580159607fe315da7a
treed248c2cb4adcafa7231a56ad8868d68590755e15
parent772b9b11e6e05842e6e7b734471775dea9af6acd
libbpf: fix LDX/STX/ST CO-RE relocation size adjustment logic

Libbpf has a somewhat obscure feature of automatically adjusting the
"size" of LDX/STX/ST instruction (memory store and load instructions),
based on originally recorded access size (u8, u16, u32, or u64) and the
actual size of the field on target kernel. This is meant to facilitate
using BPF CO-RE on 32-bit architectures (pointers are always 64-bit in
BPF, but host kernel's BTF will have it as 32-bit type), as well as
generally supporting safe type changes (unsigned integer type changes
can be transparently "relocated").

One issue that surfaced only now, 5 years after this logic was
implemented, is how this all works when dealing with fields that are
arrays. This isn't all that easy and straightforward to hit (see
selftests that reproduce this condition), but one of sched_ext BPF
programs did hit it with innocent looking loop.

Long story short, libbpf used to calculate entire array size, instead of
making sure to only calculate array's element size. But it's the element
that is loaded by LDX/STX/ST instructions (1, 2, 4, or 8 bytes), so
that's what libbpf should check. This patch adjusts the logic for
arrays and fixed the issue.

Reported-by: Emil Tsalapatis <emil@etsalapatis.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20250207014809.1573841-1-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/lib/bpf/relo_core.c