BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/3] libbpf: Check enum value relocations against the local enumerator type
@ 2026-11-26 17:56 Yonghong Song
  2026-11-26 17:56 ` [PATCH bpf-next 1/3] libbpf: Poison an enum value relocation out of the local enumerator range Yonghong Song
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Yonghong Song @ 2026-11-26 17:56 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Eduard Zingerman, kernel-team

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


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

end of thread, other threads:[~2026-11-26 20:20 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 17:56 [PATCH bpf-next 0/3] libbpf: Check enum value relocations against the local enumerator type Yonghong Song
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 19:35     ` Yonghong Song
2026-11-26 20:20   ` Alexei Starovoitov
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

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