libbpf: Check enum value relocations against the local enumerator type
Subsystems (4)
Yonghong Song
reviewercover 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
Yonghong Song
reviewerPATCH 1/3 libbpf: Poison an enum value relocation out of the local enumerator range
For a BPF_CORE_ENUMVAL_VALUE relocation, libbpf patches the 64-bit value of the target enumerator into the ld_imm64 without checking whether it fits in the type of the local enumerator.
This has been fine because such a relocation could only come from __builtin_preserve_enum_value(), e.g., through bpf_core_enum_value(). The builtin returns a long, whatever the type of the local enumerator, so the program uses all 64 bits of the patched value:
enum E { N = 10 };
long g(void) { return bpf_core_enum_value(enum E, N); }is compiled to:
g:
r0 = 0xa ll /* CO-RE <enumval_value> enum E::N */
exitIf N is 0x100000000 in the kernel, g() returns 0x100000000, although the local N has type int. This is why commit 776281652ddc ("libbpf:
Permit 64bit relocation value") allowed any 64-bit value, so that a local enum can match a kernel enum64.
Clang now accepts the preserve_access_index attribute on an enum, see llvm commit c080a0b9b673 ("[BPF] Support preserve_access_index attribute for enum"). Each reference to an enumerator of such an enum is relocated, and the relocated value is converted to the type of the enumerator. For example:
enum E { N = 10 } __attribute__((preserve_access_index));
enum U { M = 0x80000000 } __attribute__((preserve_access_index)); unsigned f1(unsigned hash) { return hash % N; }
long f2(void) { return N; }
unsigned long f3(void) { return M; }
is compiled to:
f1:
w0 = w1
r1 = 0xa ll /* CO-RE <enumval_value> enum E::N */
w0 %= w1
exit
f2:
r0 = 0xa ll /* CO-RE <enumval_value> enum E::N */
r0 <<= 0x20
r0 s>>= 0x20
exit
f3:
r0 = 0x80000000 ll /* CO-RE <enumval_value> enum U::M */
r0 <<= 0x20
r0 >>= 0x20
exitN has type int and M has type unsigned int, so only the low 32 bits of the patched value are used, even where the result is 64-bit:
- If N is 0x100000000 in the kernel, f1() computes hash % 0 and f2() returns 0.
- If N is 0x80000000 in the kernel, f2() returns 0xffffffff80000000, a negative value.
- If M is 0x100000000 in the kernel, f3() returns 0.
In each case the program loads and silently uses a wrong value.
The type of the local enumerator follows from the local enum, as in C:
int if all values of the enum fit in int, and otherwise the type of the enum, i.e., unsigned int for a 4-byte enum, and long or unsigned long for an 8-byte one. A 1-byte enum made with the packed or mode(byte) attribute, as in the kernel, still has int enumerators.
If the target value is out of the range of that type, poison the instruction, as is done when the enumerator does not exist in the target, and warn:
libbpf: prog 'test_core_enumval': relo #7: enumerator
'NAMED_ENUM_VAL2___err_int' value 0x80000000 does not fit in int type of local enumerator
So a program is only rejected if the poisoned instruction is reachable.
This also applies to bpf_core_enum_value(), since libbpf cannot tell the two forms apart. A program like g() above, which could use a target value out of the range of the local enumerator type before, is now rejected if the instruction is reachable. Document this in bpf_core_read.h.
The target value is taken as signed or unsigned according to the kflag of the target enum, which pahole sets for a signed enum since v1.24, the minimum version now required for kernel BTF.
--- tools/lib/bpf/bpf_core_read.h | 5 ++ tools/lib/bpf/relo_core.c | 88 ++++++++++++++++++++++++++++++++--- 2 files changed, 86 insertions(+), 7 deletions(-)
tools/lib/bpf/bpf_core_read.h +5 -0
@@ -283,6 +283,11 @@ enum bpf_enum_value_kind {* 64-bit value, if specified enum type and its enumerator value are * present in target kernel's BTF; * 0, if no matching enum and/or enum value within that enum is found. +* If the target value does not fit in the type of the local enumerator, the +* instruction is poisoned, so the program is rejected if it is reachable. +* The local enumerator has type int if all values of the local enum fit in +* int. Otherwise, it has type unsigned int for a 4-byte enum, and long or +* unsigned long for an 8-byte enum, depending on whether the enum is signed. */ #ifdef __clang__#define bpf_core_enum_value (enum_type, enum_value) \
tools/lib/bpf/relo_core.c +81 -7
117 lines not on this page · raw message
Signature · 1 line
2.53.1-Meta
[email protected]
AI reviewRe: [PATCH bpf-next 1/3] libbpf: Poison an enum value relocation out of the local enumerator range
TL;DR: Possible wrong behaviour in relo_core.c: enum value relocations may be wrongly poisoned when target BTF lacks the kflag (pre-6.0 kernels) and negative enumerators, so valid programs could…
Quoted 1 line
libbpf: Poison an enum value relocation out of the local enumerator range
TL;DR: Possible wrong behaviour in relo_core.c: enum value relocations may be wrongly poisoned when target BTF lacks the kflag (pre-6.0 kernels) and negative enumerators, so valid programs could fail to load.
Quoted 3 lines
tools/lib/bpf/relo_core.c +0
-0
[ ... ]
Quoted 4 lines
tools/lib/bpf/relo_core.c +4
-0+static bool bpf_core_enum_val_fits_int (__u64 val,bool val_signed) +{ +return val_signed ? (__s64 )val == (__s32 )val : val <=INT_MAX ; +}
[ ... ]
tools/lib/bpf/relo_core.c static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo, · line 948
Quoted 12 lines
tools/lib/bpf/relo_core.c +12
-0+ local_t =btf_type_by_id (local_spec->btf, local_spec->spec[0 ].type_id); + targ_t =btf_type_by_id (targ_spec->btf, targ_spec->spec[0 ].type_id); + local_signed =BTF_INFO_KFLAG (local_t->info); + targ_signed =BTF_INFO_KFLAG (targ_t->info); + +if (bpf_core_enum_fits_int (local_t)) { + type ="int" ; + fits =bpf_core_enum_val_fits_int (targ_val, targ_signed); + }else if (local_t->size ==4 ) { + type ="unsigned int" ; +/* a negative value is sign-extended, so it is also too big */ + fits = targ_val <=UINT_MAX ;
Can this reject valid programs when the target BTF has no kflag on a signed enum?
Before commit 6089fb325cf7 ("bpf: Add btf enum64 support"), btf_enum_check_meta() rejected any BTF_KIND_ENUM with kind_flag set, so vmlinux and module BTF from pre-6.0 kernels (and from kernels built with pahole older than 1.24) have kflag == 0 and store negative enumerators as their u32 bit pattern.
For kflag == 0, bpf_core_enum_val() returns (__u32)e->val, so a target enumerator of -1 becomes targ_val = 0xffffffff with targ_signed = false.
Consider a local enum such as enum perf_event_state from a modern vmlinux.h, where PERF_EVENT_STATE_OFF = -1, running on a 5.x kernel:
bpf_core_enum_fits_int(local_t) -> true
bpf_core_enum_val_fits_int(0xffffffff, false) -> 0xffffffff <= INT_MAX
-> falseThe relocation is poisoned and the program fails to load if the instruction is reachable. Before this patch the ld_imm64 got 0xffffffff, which truncates to the correct -1 in an int context such as:
int x = bpf_core_enum_value(enum perf_event_state,
PERF_EVENT_STATE_OFF);
The mirror case also looks affected. If the local enum comes from a vmlinux.h generated on an old kernel, btf_dump prints the kflag == 0 values with %u (e.g. PERF_EVENT_STATE_OFF = 4294967295), so the local enum is a 4-byte unsigned enum. On a 6.0+ kernel with kflag set, targ_val is 0xffffffffffffffff, which fails "targ_val <= UINT_MAX" and is poisoned, where bpf_core_enum_value() previously returned the kernel's -1.
The commit message says pahole sets the kflag for a signed enum since v1.24, "the minimum version now required for kernel BTF". Doesn't that only cover kernels built from this tree, rather than the older running kernels that CO-RE targets?
The selftests added later in the series appear to use only target BTF compiled by a modern clang that sets kflag, so they would not exercise this.
Would it make sense to judge a 4-byte BTF_KIND_ENUM target without kflag by its low 32 bits, accepting the value if it fits as either int or unsigned int against a 32-bit local enumerator?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45873143557
Yonghong Song
reviewerRe: [PATCH bpf-next 1/3] libbpf: Poison an enum value relocation out of the local enumerator range
Quoted 4 lines · On 11/26/26 10:36 AM, [email protected] wrote:
Quoted 1 line
libbpf: Poison an enum value relocation out of the local enumerator range
TL;DR: Possible wrong behaviour in
relo_core.c: enum value relocations may be wrongly poisoned when target BTF lacks the kflag (pre-6.0 kernels) and negative enumerators, so valid programs could fail to load.
Please take a look at commit 6036922777d3 or
https://lore.kernel.org/all/[email protected]/
We do assume that kflag 0 is taken as unsigned.
Quoted 76 lines
Quoted 4 lines
tools/lib/bpf/relo_core.c +0
-0[ ... ]
Quoted 4 lines
tools/lib/bpf/relo_core.c +4
-0+static bool bpf_core_enum_val_fits_int (__u64 val,bool val_signed) +{ +return val_signed ? (__s64 )val == (__s32 )val : val <=INT_MAX ; +}[ ... ]
tools/lib/bpf/relo_core.c static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo, · line 948
Quoted 12 lines
tools/lib/bpf/relo_core.c +12
-0+ local_t =btf_type_by_id (local_spec->btf, local_spec->spec[0 ].type_id); + targ_t =btf_type_by_id (targ_spec->btf, targ_spec->spec[0 ].type_id); + local_signed =BTF_INFO_KFLAG (local_t->info); + targ_signed =BTF_INFO_KFLAG (targ_t->info); + +if (bpf_core_enum_fits_int (local_t)) { + type ="int" ; + fits =bpf_core_enum_val_fits_int (targ_val, targ_signed); + }else if (local_t->size ==4 ) { + type ="unsigned int" ; +/* a negative value is sign-extended, so it is also too big */ + fits = targ_val <=UINT_MAX ;Can this reject valid programs when the target BTF has no kflag on a signed enum?
Before commit
6089fb325cf7("bpf: Add btf enum64 support"),btf_enum_check_meta()rejected anyBTF_KIND_ENUMwithkind_flagset, so vmlinux and module BTF from pre-6.0 kernels (and from kernels built with pahole older than 1.24) have kflag == 0 and store negative enumerators as their u32 bit pattern.For kflag == 0,
bpf_core_enum_val()returns (__u32)e->val, so a target enumerator of -1 becomestarg_val=0xffffffffwithtarg_signed= false.Consider a local enum such as enum
perf_event_statefrom a modernvmlinux.h, wherePERF_EVENT_STATE_OFF= -1, running on a 5.x kernel:bpf_core_enum_fits_int(local_t) -> true bpf_core_enum_val_fits_int(0xffffffff, false) -> 0xffffffff <= INT_MAX -> falseThe relocation is poisoned and the program fails to load if the instruction is reachable. Before this patch the
ld_imm64got0xffffffff, which truncates to the correct -1 in an int context such as:int x = bpf_core_enum_value(enum
perf_event_state,
PERF_EVENT_STATE_OFF);The mirror case also looks affected. If the local enum comes from a
vmlinux.hgenerated on an old kernel,btf_dumpprints the kflag == 0 values with %u (e.g.PERF_EVENT_STATE_OFF= 4294967295), so the local enum is a 4-byte unsigned enum. On a 6.0+ kernel with kflag set,targ_valis0xffffffffffffffff, which fails "targ_val<=UINT_MAX" and is poisoned, wherebpf_core_enum_value()previously returned the kernel's -1.The commit message says pahole sets the kflag for a signed enum since v1.24, "the minimum version now required for kernel BTF". Doesn't that only cover kernels built from this tree, rather than the older running kernels that CO-RE targets?
The selftests added later in the series appear to use only target BTF compiled by a modern clang that sets kflag, so they would not exercise this.
Would it make sense to judge a 4-byte
BTF_KIND_ENUMtarget without kflag by its low 32 bits, accepting the value if it fits as either int or unsigned int against a 32-bit local enumerator?---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/45873143557
Re: [PATCH bpf-next 1/3] libbpf: Poison an enum value relocation out of the local enumerator range
In reply to Yonghong SongQuoted 7 lines · Yonghong Song
So a program is only rejected if the poisoned instruction is reachable.
This also applies to
bpf_core_enum_value(), since libbpf cannot tell the two forms apart. A program likeg()above, which could use a target value out of the range of the local enumerator type before, is now rejected if the instruction is reachable. Document this inbpf_core_read.h.
The usual guard doesn't make it unreachable. bpf_core_enum_value_exists() returns 1 here.
I think this takes back what your commit 776281652ddc allowed. The local value is often a placeholder, like in profiler.inc.h:
pids_cgrp_id___local = 123, /* value doesn't matter */
What bpf_core_enum_value() gets today and with the patch:
local enumerator target today patch --------------------------------- ----------- ---------- -------- SCX_ENQ_PREEMPT___local = 0 1ULL << 32 1ULL << 32 poisoned PERF_EVENT_STATE_OFF = -1 -1, kflag 0 0xffffffff poisoned PERF_EVENT_STATE_OFF = 4294967295 -1, kflag 1 -1 poisoned
Row 2 runs on v5.15. Before commit 4fad237494f1 it got -1. Row 3 has vmlinux.h of v5.15 and runs on bpf-next.
The kernel builds relo_core.c too.
A light skeleton with row 1 stops loading after a kernel upgrade.
Quoted 3 lines
The target value is taken as signed or unsigned according to the kflag of the target enum, which pahole sets for a signed enum since v1.24, the minimum version now required for kernel BTF.
That covers new kernels only.
Before v6.0 btf_enum_check_meta() refuses an enum with kflag. Row 2 ends up in:
val <= INT_MAX val = 0xffffffff, val_signed = 0
Code like f2() gets -1 there today, since s>>= restores the sign. So the attribute form fails to load too, and its value is correct.
Review
Quoted 6 lines
(quoted diff) +6
-0+/* + * As in C, the enumerators of an enum have type int if all values of the enum + * fit in int, even for a 1-byte enum made with the packed or mode(byte) + * attribute, and otherwise the type of the enum, which is then 4 or 8 bytes. + */ +static bool bpf_core_enum_fits_int(const struct btf_type *t)
Not with a fixed underlying type. sizeof(N) is 8 with clang 21:
enum E : unsigned long { N = 10 };This function says int, and 0x100000000 gets poisoned. BTF doesn't have the type of the enumerator. The compiler has it.
can clang emit mov for 32-bit enumerators, like for field relos ?
enumerator insn ------------ --------------- int r0 = 0xa unsigned int w0 = 0x80000000 long r0 = 0xa ll
bpf_core_enum_value() keeps ld_imm64 and works as before. bpf_core_patch_insn() patches ALU imm already, without a range check. With mov the diff poisons all three examples from the log. Rows 2 and 3 load in both forms.
The diff skips the sign of a long and fixed types of 1 and 2 bytes. No need for bpf_core_check_enumval_range(), bpf_core_enum_fits_int(), and for the shifts in f2() and f3().
relo_core.c in place of patch 1, compiles for libbpf and kernel, not run:
tools/lib/bpf/relo_core.c +20 -1
@@ -1031,6 +1031,20 @@ static int insn_bytes_to_bpf_size(__u32 sz) } } +/* +* ALU zero-extends imm, ALU64 sign-extends it. BTF before v6.0 has no sign of +* an enum, so accept both forms when bit 31 of the local value is set. +*/ +static bool insn_imm_fits (const struct bpf_insn *insn,__u64 val) +{ +bool zext = val == (__u32 )val; +bool sext = (__s64 )val == (__s32 )val; + +if (insn->imm <0 ) +return zext || sext; +return BPF_CLASS (insn->code) ==BPF_ALU ? zext : sext; +} +/* * Patch relocatable BPF instruction. *
@@ -1067,13 +1081,18 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn, goto bad_insn;if (res->poison)return bpf_core_poison_insn (prog_name, relo_idx, insn, insn_idx); -if (res->validate && insn->imm != orig_val) { +if (res->validate && insn->imm != (__s32 )orig_val) {pr_warn ("prog '%s': relo #%d: unexpected insn #%d (ALU/ALU64) value: got %d, exp %llu -> %llu\n" , prog_name, relo_idx, insn_idx, insn->imm, (unsigned long long )orig_val, (unsigned long long )new_val);return -EINVAL ; } +if (!insn_imm_fits (insn, new_val)) { +pr_warn ("prog '%s': relo #%d: insn #%d (ALU/ALU64) value out of range: 0x%llx\n" , + prog_name, relo_idx, insn_idx, (unsigned long long )new_val); +return bpf_core_poison_insn (prog_name, relo_idx, insn, insn_idx); + } orig_val = insn->imm; insn->imm = new_val; pr_debug("prog '%s': relo #%d: patched insn #%d (ALU/ALU64) imm %llu -> %llu\n",
pw-bot: cr
Yonghong Song
reviewerRe: [PATCH bpf-next 1/3] libbpf: Poison an enum value relocation out of the local enumerator range
Quoted 23 lines · Alexei Starovoitov
Quoted 7 lines · Yonghong Song
So a program is only rejected if the poisoned instruction is reachable.
This also applies to
bpf_core_enum_value(), since libbpf cannot tell the two forms apart. A program likeg()above, which could use a target value out of the range of the local enumerator type before, is now rejected if the instruction is reachable. Document this inbpf_core_read.h.The usual guard doesn't make it unreachable.
bpf_core_enum_value_exists()returns 1 here.
I think this takes back what your commit776281652ddcallowed. The local value is often a placeholder, like inprofiler.inc.h:pids_cgrp_id___local = 123, /* value doesn't matter */What
bpf_core_enum_value()gets today and with the patch:local enumerator target today patch --------------------------------- ----------- ---------- -------- SCX_ENQ_PREEMPT___local = 0 1ULL << 32 1ULL << 32 poisoned PERF_EVENT_STATE_OFF = -1 -1, kflag 0 0xffffffff poisoned PERF_EVENT_STATE_OFF = 4294967295 -1, kflag 1 -1 poisoned
Quoted 4 lines
Row 2 runs on v5.15. Before commit
4fad237494f1it got -1. Row 3 hasvmlinux.hof v5.15 and runs on bpf-next.
The kernel buildsrelo_core.ctoo.
A light skeleton with row 1 stops loading after a kernel upgrade.
Indeed, looks like based on local enumerator to imply the expected value range is incorrect.
Quoted 4 lines
Quoted 4 lines
The target value is taken as signed or unsigned according to the kflag of the target enum, which pahole sets for a signed enum since v1.24, the minimum version now required for kernel BTF.
Quoted 8 lines
That covers new kernels only.
Before v6.0btf_enum_check_meta()refuses an enum with kflag. Row 2 ends up in:val <= INT_MAX val = 0xffffffff, val_signed = 0Code like
f2()gets -1 there today, since s>>= restores the sign. So the attribute form fails to load too, and its value is correct.
My current implementation assumes >= v6.0 kernel and it may indeed reject some cases for < v6.0 kernel.
Quoted 14 lines
Review
Quoted 7 lines
(quoted diff) +6
-0+/* + * As in C, the enumerators of an enum have type int if all values of the enum + * fit in int, even for a 1-byte enum made with the packed or mode(byte) + * attribute, and otherwise the type of the enum, which is then 4 or 8 bytes. + */ +static bool bpf_core_enum_fits_int(const struct btf_type *t)Not with a fixed underlying type. sizeof(N) is 8 with clang 21:
enum E : unsigned long { N = 10 };This function says int, and
0x100000000gets poisoned. BTF doesn't have the type of the enumerator. The compiler has it.
Quoted 7 lines
can clang emit mov for 32-bit enumerators, like for field relos ?
enumerator insn ------------ --------------- int r0 = 0xa unsigned int w0 = 0x80000000 long r0 = 0xa ll
I remembered that we discussed this before during implementing
llvm commit c080a0b9b673e ([BPF] Support preserve_access_index attribute for enum (#232498)). We briefly discussed whether we should do 'w0 = ...' or 'r0 = ...'
although eventually we used 'r0 = 0xa ll' style.
Looks like we do need to use 'r0/w0 = ...' style too. This will gate the value in tight range to simplify libbpf.
Quoted 46 lines
bpf_core_enum_value()keepsld_imm64and works as before.bpf_core_patch_insn()patches ALU imm already, without a range check. With mov the diff poisons all three examples from the log. Rows 2 and 3 load in both forms.
The diff skips the sign of a long and fixed types of 1 and 2 bytes. No need forbpf_core_check_enumval_range(),bpf_core_enum_fits_int(), and for the shifts inf2()andf3().
relo_core.cin place of patch 1, compiles for libbpf and kernel, not run:tools/lib/bpf/relo_core.c +15
-1@@ -1031,6 +1031,20 @@ static int insn_bytes_to_bpf_size(__u32 sz) } } +/* +* ALU zero-extends imm, ALU64 sign-extends it. BTF before v6.0 has no sign of +* an enum, so accept both forms when bit 31 of the local value is set. +*/ +static bool insn_imm_fits (const struct bpf_insn *insn,__u64 val) +{ +bool zext = val == (__u32 )val; +bool sext = (__s64 )val == (__s32 )val; + +if (insn->imm <0 ) +return zext || sext; +return BPF_CLASS (insn->code) ==BPF_ALU ? zext : sext; +} +/* * Patch relocatable BPF instruction. * @@ -1067,13 +1081,18 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn, goto bad_insn;if (res->poison)return bpf_core_poison_insn (prog_name, relo_idx, insn, insn_idx); -if (res->validate && insn->imm != orig_val) { +if (res->validate && insn->imm != (__s32 )orig_val) {pr_warn ("prog '%s': relo #%d: unexpected insn #%d (ALU/ALU64) value: got %d, exp %llu -> %llu\n" , prog_name, relo_idx, insn_idx, insn->imm, (unsigned long long )orig_val, (unsigned long long )new_val);
tools/lib/bpf/relo_core.c int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn, · line 1098
Quoted 10 lines
tools/lib/bpf/relo_core.c +5
-0return -EINVAL ; } +if (!insn_imm_fits (insn, new_val)) { +pr_warn ("prog '%s': relo #%d: insn #%d (ALU/ALU64) value out of range: 0x%llx\n" , + prog_name, relo_idx, insn_idx, (unsigned long long )new_val); +return bpf_core_poison_insn (prog_name, relo_idx, insn, insn_idx); + } orig_val = insn->imm; insn->imm = new_val; pr_debug("prog '%s': relo #%d: patched insn #%d (ALU/ALU64) imm %llu -> %llu\n",
I will work on llvm as soon as possible. The above diff looks correct if llvm side with 'w0/r0 = ...' for relocation.
Quoted 2 lines
pw-bot: cr
Yonghong Song
reviewerPATCH 2/3 selftests/bpf: Test enum value relocations within the local enumerator range
In reply to Yonghong SongTest the previous patch with enum value relocations whose target value is within the range of the type of the local enumerator, so they are still relocated, even where the target enum differs from the local one in size or value range.
test_core_reloc_enumval gets a local 1-byte enum with the packed attribute. Its values are read only if the target has the enum, guarded by bpf_core_enum_value_exists(), so targets without the enum are not affected. The local values fit in int, so the enumerators are int, and a target value out of a byte is relocated. The enumval target gets the same enum, also packed.
New core_reloc cases, with the target enums in the shapes the kernel has:
- enumval___enum64: the target enums are 8 bytes, but the relocated values fit in int, so they work as before.
- enumval___negative: a negative target value for an int enumerator is relocated.
- enumval___byte_unpacked: 0x100 for an enumerator of the local 1-byte enum, from a 4-byte target enum, is relocated.
--- .../selftests/bpf/prog_tests/core_reloc.c | 40 +++++++++++++ .../btf__core_reloc_enumval___byte_unpacked.c | 3 + .../progs/btf__core_reloc_enumval___enum64.c | 3 + .../btf__core_reloc_enumval___negative.c | 3 + .../selftests/bpf/progs/core_reloc_types.h | 56 ++++++++++++++++++- .../bpf/progs/test_core_reloc_enumval.c | 14 +++++ 6 files changed, 118 insertions(+), 1 deletion(-) 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___negative.c
tools/testing/selftests/bpf/prog_tests/core_reloc.c +40 -0
@@ -904,6 +904,8 @@ static const struct core_reloc_test_case test_cases[] = { .named_val2 = 2, .anon_val1 = 0x10, .anon_val2 = 0x20, + .byte_val1 = 0x11, + .byte_val2 = 0xff, .uint_val1 = 0x80000001, .uint_val2 = 0xffffffff, }),
@@ -932,6 +934,44 @@ static const struct core_reloc_test_case test_cases[] = { .anon_val2 = 0x222, }), ENUMVAL_ERR_CASE(enumval___err_missing), + ENUMVAL_CASE(enumval___enum64, { + .named_val1_exists = true, + .named_val2_exists = true, + .named_val3_exists = true, + .anon_val1_exists = true, + .anon_val2_exists = true, + .anon_val3_exists = true, + .named_val1 = 1001, + .named_val2 = 2002, + .anon_val1 = 0x11, + .anon_val2 = 0x22, + }), + ENUMVAL_CASE(enumval___negative, { + .named_val1_exists = true, + .named_val2_exists = true, + .named_val3_exists = true, + .anon_val1_exists = true, + .anon_val2_exists = true, + .anon_val3_exists = true, + .named_val1 = 1, + .named_val2 = 2, + .anon_val1 = -0x10, + .anon_val2 = 0x20, + }), + ENUMVAL_CASE(enumval___byte_unpacked, { + .named_val1_exists = true, + .named_val2_exists = true, + .named_val3_exists = true, + .anon_val1_exists = true, + .anon_val2_exists = true, + .anon_val3_exists = true, + .named_val1 = 1, + .named_val2 = 2, + .anon_val1 = 0x10, + .anon_val2 = 0x20, + .byte_val1 = 0x11, + .byte_val2 = 0x100, + }), /* 64bit enumerator value existence and value relocations */ ENUM64VAL_CASE(enum64val, {
tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___byte_unpacked.c new file +3 -0
@@ -0,0 +1,3 @@ +#include "core_reloc_types.h" + +void f (struct core_reloc_enumval___byte_unpacked x) {}
tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___enum64.c new file +3 -0
@@ -0,0 +1,3 @@ +#include "core_reloc_types.h" + +void f (struct core_reloc_enumval___enum64 x) {}
tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___negative.c new file +3 -0
@@ -0,0 +1,3 @@ +#include "core_reloc_types.h" + +void f (struct core_reloc_enumval___negative x) {}
tools/testing/selftests/bpf/progs/core_reloc_types.h +55 -1
80 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +14 -0
@@ -27,6 +27,12 @@ typedef enum {ANON_ENUM_VAL3 =0x30 , } anon_enum; +/* 1-byte enum, whose values fit in int, so its enumerators are int */ +enum named_byte_enum { +BYTE_ENUM_VAL1 =1 , +BYTE_ENUM_VAL2 =2 , +} __attribute__((packed)); +/* 4-byte enum with values out of the range of int, so it is unsigned */ enum named_uint_enum { UINT_ENUM_VAL1 = 0x80000000,
@@ -45,6 +51,8 @@ struct core_reloc_enumval_output {int named_val2;int anon_val1;int anon_val2; +int byte_val1; +int byte_val2;long uint_val1;long uint_val2; };
@@ -73,6 +81,12 @@ int test_core_enumval(void *ctx) out->anon_val2 =bpf_core_enum_value (anon,ANON_ENUM_VAL2 );/* ANON_ENUM_VAL3 value is optional */ +/* enum named_byte_enum is optional */ + if (bpf_core_enum_value_exists(enum named_byte_enum, BYTE_ENUM_VAL1)) + out->byte_val1 = bpf_core_enum_value(enum named_byte_enum, BYTE_ENUM_VAL1); + if (bpf_core_enum_value_exists(enum named_byte_enum, BYTE_ENUM_VAL2)) + out->byte_val2 = bpf_core_enum_value(enum named_byte_enum, BYTE_ENUM_VAL2); +/* enum named_uint_enum is optional */ #ifdef __clang__ if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL1))
Signature · 1 line
2.53.1-Meta
[email protected]
AI reviewRe: [PATCH bpf-next 2/3] selftests/bpf: Test enum value relocations within the local enumerator range
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] selftests/bpf: non-standard multi-line comment style in core_reloc_types.h --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests/bpf: non-standard multi-line comment style in core_reloc_types.h
--
commit 7b2de00cc3d3bd98446d7a82129006ec470a720b
Author: Yonghong Song <[email protected]>
selftests/bpf: Test enum value relocations within the local enumerator range
This commit adds CO-RE enum value relocation tests for target enum values that fit within the range of the local enumerator type across 8-byte, negative, and unpacked 1-byte target enums.
Quoted 4 lines
tools/testing/selftests/bpf/progs/core_reloc_types.h +0
-0
[ ... ]
Quoted 29 lines
tools/testing/selftests/bpf/progs/core_reloc_types.h +25
-0@@ -1355,6 +1363,52 @@ struct core_reloc_enumval___err_missing {anon_enum___err_missing f2; }; +/* 8-byte target enums, whose relocated values fit in the int enumerators of +* the local enums +*/ +enum named_enum___enum64 { + NAMED_ENUM_VAL1___enum64 =1001 , + NAMED_ENUM_VAL2___enum64 =2002 , + NAMED_ENUM_VAL3___enum64 =0x300000000ULL , +}; + +typedef enum { + ANON_ENUM_VAL1___enum64 =0x11 , + ANON_ENUM_VAL2___enum64 =0x22 , + ANON_ENUM_VAL3___enum64 =0x300000000ULL , +}anon_enum___enum64 ; + +struct core_reloc_enumval___enum64 { +enum named_enum___enum64 f1; +anon_enum___enum64 f2; +}; + +/* negative value for an int enumerator */ +typedef enum { + ANON_ENUM_VAL1___negative = -0x10, + ANON_ENUM_VAL2___negative = 0x20, + ANON_ENUM_VAL3___negative = 0x30,
tools/testing/selftests/bpf/progs/core_reloc_types.h struct core_reloc_enumval___err_missing { · line 1400
Quoted 10 lines
tools/testing/selftests/bpf/progs/core_reloc_types.h +10
-0+} anon_enum___negative; + +struct core_reloc_enumval___negative { +enum named_enum f1; +anon_enum___negative f2; +}; + +/* 4-byte target enum with a value out of a byte, for the int enumerators of +* the local 1-byte enum +*/
[Severity: Low]
This isn't a bug, but BPF coding style places the opening /* of multi-line comments on its own line in these two comments.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Yonghong Song
reviewerPATCH 3/3 selftests/bpf: Test enum value relocations out of the local enumerator range
In reply to Yonghong SongTest the libbpf patch, which poisons an enum value relocation whose target value is out of the range of the type of the local enumerator. In each new core_reloc case, the poisoned instruction is reachable, so the load fails:
- enumval___err_too_big: 0x200000000 for an int enumerator, from an 8-byte target enum.
- enumval___err_too_small: -0x100000000 for an int enumerator, from a signed 8-byte target enum.
- enumval___err_int: 0x80000000 for an int enumerator, from an unsigned 4-byte target enum.
- enumval___err_uint: 0x100000000 for an unsigned int enumerator, from an 8-byte target enum.
- enum64val___err_sign: 0x8000000000000000 for a long enumerator, from an unsigned 8-byte target enum.
With GCC, the values of enum named_uint_enum are not read, and the enumval___err_uint case is skipped, as for the enumval case, and so is enum64val___err_sign, as for all enum64val cases.
--- .../selftests/bpf/prog_tests/core_reloc.c | 5 ++ .../btf__core_reloc_enum64val___err_sign.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 + .../selftests/bpf/progs/core_reloc_types.h | 70 +++++++++++++++++++ 7 files changed, 90 insertions(+) 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___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
tools/testing/selftests/bpf/prog_tests/core_reloc.c +5 -0
@@ -972,6 +972,10 @@ static const struct core_reloc_test_case test_cases[] = { .byte_val1 = 0x11, .byte_val2 = 0x100, }), + ENUMVAL_ERR_CASE(enumval___err_too_big), + ENUMVAL_ERR_CASE(enumval___err_too_small), + ENUMVAL_ERR_CASE(enumval___err_int), + ENUMVAL_ERR_CASE(enumval___err_uint), /* 64bit enumerator value existence and value relocations */ ENUM64VAL_CASE(enum64val, {
@@ -1011,6 +1015,7 @@ static const struct core_reloc_test_case test_cases[] = { .signed_val2 = -222, }),ENUM64VAL_ERR_CASE (enum64val___err_missing), + ENUM64VAL_ERR_CASE(enum64val___err_sign), };struct data {
tools/testing/selftests/bpf/progs/btf__core_reloc_enum64val___err_sign.c new file +3 -0
@@ -0,0 +1,3 @@ +#include "core_reloc_types.h" + +void f (struct core_reloc_enum64val___err_sign x) {}
tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_int.c new file +3 -0
@@ -0,0 +1,3 @@ +#include "core_reloc_types.h" + +void f (struct core_reloc_enumval___err_int x) {}
tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_too_big.c new file +3 -0
@@ -0,0 +1,3 @@ +#include "core_reloc_types.h" + +void f (struct core_reloc_enumval___err_too_big x) {}
tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_too_small.c new file +3 -0
@@ -0,0 +1,3 @@ +#include "core_reloc_types.h" + +void f (struct core_reloc_enumval___err_too_small x) {}
tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___err_uint.c new file +3 -0
@@ -0,0 +1,3 @@ +#include "core_reloc_types.h" + +void f (struct core_reloc_enumval___err_uint x) {}
tools/testing/selftests/bpf/progs/core_reloc_types.h +70 -0
79 lines not on this page · raw message
Signature · 1 line
2.53.1-Meta
[email protected]
AI reviewRe: [PATCH bpf-next 3/3] selftests/bpf: Test enum value relocations out of the local enumerator range
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] selftests/bpf: non-standard multi-line comments in core_reloc_types.h --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests/bpf: non-standard multi-line comments in core_reloc_types.h
--
commit b84e0514078e764d621bc1f043d99af2bc50dbfb
Author: Yonghong Song <[email protected]>
selftests/bpf: Test enum value relocations out of the local enumerator range
This commit adds CO-RE relocation selftests verifying that libbpf poisons enum and enum64 value relocations whose target values fall outside the range of the local enumerator type.
Quoted 68 lines
tools/testing/selftests/bpf/progs/core_reloc_types.h +56
-0@@ -1409,6 +1409,62 @@ struct core_reloc_enumval___byte_unpacked {enum named_byte_enum___byte_unpacked f3; }; +/* second enum value out of the range of the local int enumerator, as the +* target enum is 8 bytes, should fail +*/ +enum named_enum___err_too_big { + NAMED_ENUM_VAL1___err_too_big =1 , + NAMED_ENUM_VAL2___err_too_big =0x200000000ULL , + NAMED_ENUM_VAL3___err_too_big =3 , +}; + +struct core_reloc_enumval___err_too_big { +enum named_enum___err_too_big f1; +anon_enum f2; +}; + +/* second enum value out of the range of the local int enumerator, as the +* target enum is signed and 8 bytes, should fail +*/ +enum named_enum___err_too_small { + NAMED_ENUM_VAL1___err_too_small =1 , + NAMED_ENUM_VAL2___err_too_small =-0x100000000LL , + NAMED_ENUM_VAL3___err_too_small =3 , +}; + +struct core_reloc_enumval___err_too_small { +enum named_enum___err_too_small f1; +anon_enum f2; +}; + +/* second enum value out of the range of the local int enumerator, as the +* target enum is unsigned, should fail +*/ +enum named_enum___err_int { + NAMED_ENUM_VAL1___err_int =1 , + NAMED_ENUM_VAL2___err_int =0x80000000 , + NAMED_ENUM_VAL3___err_int =3 , +}; + +struct core_reloc_enumval___err_int { +enum named_enum___err_int f1; +anon_enum f2; +}; + +/* first unsigned enum value out of the range of the local unsigned int +* enumerator, as the target enum is 8 bytes, should fail +*/ +enum named_uint_enum___err_uint { + UINT_ENUM_VAL1___err_uint =0x100000000ULL , + UINT_ENUM_VAL2___err_uint =0xffffffff , +}; + +struct core_reloc_enumval___err_uint { +enum named_enum f1; +anon_enum f2; +enum named_uint_enum___err_uint f3; +}; +enum named_unsigned_enum64___err_missing { UNSIGNED_ENUM64_VAL1___err_missing =0x1ffffffffULL , UNSIGNED_ENUM64_VAL3___err_missing =0x3ffffffffULL ,
tools/testing/selftests/bpf/progs/core_reloc_types.h struct core_reloc_enum64val___err_missing { · line 1485
Quoted 7 lines
tools/testing/selftests/bpf/progs/core_reloc_types.h +4
-0@@ -1423,3 +1479,17 @@ struct core_reloc_enum64val___err_missing {enum named_unsigned_enum64___err_missing f1;enum named_signed_enum64___err_missing f2; }; + +/* first signed enum value out of the range of the local long enumerator, as +* the target enum is unsigned, should fail +*/
[Severity: Low]
This isn't a bug, but the five new multi-line comments in core_reloc_types.h place text on the same line as the opening /*. Per BPF coding style, the opening /* of a multi-line comment should be on its own line with the comment text starting on the next line.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3