From: Yonghong Song <yonghong.song@linux.dev>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Eduard Zingerman <eddyz87@gmail.com>,
kernel-team@fb.com
Subject: [PATCH bpf-next 0/3] libbpf: Check enum value relocations against the local enumerator type
Date: Thu, 26 Nov 2026 09:56:02 -0800 [thread overview]
Message-ID: <20261126175602.1882988-1-yonghong.song@linux.dev> (raw)
Clang now accepts the preserve_access_index attribute on an enum, see
llvm commit c080a0b9b673. Each reference to an enumerator of such an
enum gets a BPF_CORE_ENUMVAL_VALUE relocation, and the compiler converts
the relocated value to the type of the enumerator, e.g., uses only its
low 32 bits for an int. libbpf patches the 64-bit target value without
checking that it fits in that type, so a target value out of its range
becomes a different value: 0x100000000 becomes 0 for an int enumerator,
and 0x80000000 becomes negative. The program loads and uses it without
any error.
Patch 1 poisons such a relocation, as is done for a missing enumerator,
so the program is rejected only if the instruction is reachable. The
type of the local enumerator follows from the local enum, as in C: int
if all values of the enum fit in int, which includes the 1-byte packed
and mode(byte) enums in the kernel, and otherwise the type of the enum.
The check also applies to bpf_core_enum_value(), since libbpf cannot
tell the two forms apart.
Patch 2 tests relocations that still work, where the target enum differs
from the local one in size or value range. Patch 3 tests target values
out of the range of int, unsigned int and long enumerators, which make
the load fail.
Yonghong Song (3):
libbpf: Poison an enum value relocation out of the local enumerator
range
selftests/bpf: Test enum value relocations within the local enumerator
range
selftests/bpf: Test enum value relocations out of the local enumerator
range
tools/lib/bpf/bpf_core_read.h | 5 +
tools/lib/bpf/relo_core.c | 88 +++++++++++-
.../selftests/bpf/prog_tests/core_reloc.c | 45 +++++++
.../btf__core_reloc_enum64val___err_sign.c | 3 +
.../btf__core_reloc_enumval___byte_unpacked.c | 3 +
.../progs/btf__core_reloc_enumval___enum64.c | 3 +
.../progs/btf__core_reloc_enumval___err_int.c | 3 +
.../btf__core_reloc_enumval___err_too_big.c | 3 +
.../btf__core_reloc_enumval___err_too_small.c | 3 +
.../btf__core_reloc_enumval___err_uint.c | 3 +
.../btf__core_reloc_enumval___negative.c | 3 +
.../selftests/bpf/progs/core_reloc_types.h | 126 +++++++++++++++++-
.../bpf/progs/test_core_reloc_enumval.c | 14 ++
13 files changed, 294 insertions(+), 8 deletions(-)
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___err_sign.c
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___byte_unpacked.c
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___enum64.c
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_int.c
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_too_big.c
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_too_small.c
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_uint.c
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___negative.c
--
2.53.1-Meta
next reply other threads:[~2026-11-26 17:56 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 17:56 Yonghong Song [this message]
2026-11-26 17:56 ` [PATCH bpf-next 1/3] libbpf: Poison an enum value relocation out of the local enumerator range Yonghong Song
2026-11-26 18:36 ` bot+bpf-ci
2026-11-26 17:56 ` [PATCH bpf-next 2/3] selftests/bpf: Test enum value relocations within " Yonghong Song
2026-11-26 18:00 ` sashiko-bot
2026-11-26 17:56 ` [PATCH bpf-next 3/3] selftests/bpf: Test enum value relocations out of " Yonghong Song
2026-11-26 18:02 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261126175602.1882988-1-yonghong.song@linux.dev \
--to=yonghong.song@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kernel-team@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox