Skip to content

libbpf: Check enum value relocations against the local enumerator type

Yonghong Song bpf 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest3 patches 3 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3libbpf: Poison an enum value relocation out of the local enumerator rangetools/lib/bpf/bpf_core_read.h tools/lib/bpf/relo_core.c+86 -73 replies
2/3selftests/bpf: Test enum value relocations within the local enumerator rangetools/testing/selftests/bpf/prog_tests/core_reloc.c tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___byte_unpacked.c tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___enum64.c +3 more+118 -10 replies
3/3selftests/bpf: Test enum value relocations out of the local enumerator rangetools/testing/selftests/bpf/prog_tests/core_reloc.c tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___err_sign.c tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_int.c +4 more+90 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH bpf-next 0/3] libbpf: Check enum value relocations against the local enumerator type

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
Signature · 1 line
2.53.1-Meta