Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH bpf v2 0/2] libbpf: Reject out-of-range linker relocation offsets
@ 2026-06-14  9:26 HyeongJun An
  2026-06-14  9:26 ` [PATCH bpf v2 1/2] " HyeongJun An
  2026-06-14  9:26 ` [PATCH bpf v2 2/2] selftests/bpf: Test linker rejects out-of-range relocation offset HyeongJun An
  0 siblings, 2 replies; 4+ messages in thread
From: HyeongJun An @ 2026-06-14  9:26 UTC (permalink / raw)
  To: Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann
  Cc: Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
	Song Liu, Yonghong Song, Jiri Olsa, Shuah Khan, bpf,
	linux-kselftest, linux-kernel, HyeongJun An

The libbpf static linker validates relocation type, symbol index and
instruction alignment in linker_sanity_check_elf_relos(), but does not
check that the relocation offset is inside the relocated section.  A
malformed BPF object processed by the static linker (e.g. via
"bpftool gen object") can therefore carry an out-of-range r_offset that
linker_append_elf_relos() then uses to index the section data, reading
and writing past the buffer.

The normal object-loading path already rejects such offsets (libbpf.c,
rel->r_offset >= scn_data->d_size); the static linker path is the
missing sibling.  Patch 1 adds the same bound.  Patch 2 adds a selftest
that builds a tiny object with an out-of-range relocation offset and
checks that the linker now rejects it, with a valid relocation as a
positive control.

Reproduced with ASAN: before patch 1 the out-of-range relocation is
accepted (and triggers a heap-buffer-overflow); after, it is rejected
with -EINVAL.

Changes in v2:
- selftest: set the generated ELF object's EI_DATA from the host byte
  order instead of hardcoding little-endian, so it works on big-endian
  hosts (e.g. s390x).
- selftest: add fallback definitions for EM_BPF and R_BPF_64_64 for
  older system headers.
Patch 1 (the fix) is unchanged.

HyeongJun An (2):
  libbpf: Reject out-of-range linker relocation offsets
  selftests/bpf: Test linker rejects out-of-range relocation offset

 tools/lib/bpf/linker.c                        |   6 +
 .../selftests/bpf/prog_tests/libbpf_linker.c  | 231 ++++++++++++++++++
 2 files changed, 237 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/libbpf_linker.c

-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-06-15  0:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-14  9:26 [PATCH bpf v2 0/2] libbpf: Reject out-of-range linker relocation offsets HyeongJun An
2026-06-14  9:26 ` [PATCH bpf v2 1/2] " HyeongJun An
2026-06-15  0:05   ` Alexei Starovoitov
2026-06-14  9:26 ` [PATCH bpf v2 2/2] selftests/bpf: Test linker rejects out-of-range relocation offset HyeongJun An

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox