libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
Subsystems (4)
Yonghong Song
reviewerPATCH 1/2 libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
For a BPF_CORE_ENUMVAL_VALUE relocation, bpf_core_calc_enumval_relo() reads the value of a 32-bit enumerator from btf_enum->val, which is __s32, so the value is sign-extended to 64 bits even if the enum is unsigned. A value in [0x80000000, 0xffffffff] of an unsigned enum becomes 0xffffffff80000000 or above.
Clang emits such a value zero-extended in the ld_imm64 of the relocation, so the check of the original value fails, and the whole object is refused even if the target value is the same:
enum dentry_flags { DCACHE_PRIVATE = 0x80000000 };
out = bpf_core_enum_value(enum dentry_flags, DCACHE_PRIVATE);libbpf: prog 'p': relo #0: unexpected insn #0 (LDIMM64) value: got
2147483648, exp 18446744071562067968 -> 18446744071562067968
libbpf: prog 'p': relo #0: failed to patch insn #0: -EINVAL
And a target value in that range is patched sign-extended, which is wrong wherever the program uses it as a 64-bit value. The x86 vmlinux has 29 enums with such values, e.g., dentry_flags, init_ns_ino and e820_type.
Commit 89e7a42e3030 ("libbpf: Normalize enum values according to signedness") added btf_enum_value(), which extends the value as given by the kflag of the enum, for btf_dump and map fields, but not for CO-RE relocations, whose relo_core.c is also built into the kernel. Use it in bpf_core_calc_enumval_relo() as well, and add it to the kernel's include/linux/btf.h, so that the kernel gets the same fix.
--- include/linux/btf.h | 5 +++++ tools/lib/bpf/relo_core.c | 12 ++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-)
include/linux/btf.h +5 -0
@@ -525,6 +525,11 @@ static inline struct btf_enum *btf_enum(const struct btf_type *t)return (struct btf_enum *)(t +1 ); } +static inline u64 btf_enum_value (const struct btf_type *t,const struct btf_enum *e) +{ +return btf_type_kflag (t) ? (s64 )e->val : (u32 )e->val; +} +static inline struct btf_enum64 *btf_enum64 (const struct btf_type *t) {return (struct btf_enum64 *)(t +1 );
tools/lib/bpf/relo_core.c +8 -4
@@ -861,6 +861,13 @@ static int bpf_core_calc_type_relo(const struct bpf_core_relo *relo,return 0 ; } +static __u64 bpf_core_enumval (const struct btf_type *t,int idx) +{ +if (btf_is_enum (t)) +return btf_enum_value (t,btf_enum (t) + idx); +return btf_enum64_value (btf_enum64 (t) + idx); +} +static int bpf_core_calc_enumval_relo (const struct bpf_core_relo *relo,const struct bpf_core_spec *spec,__u64 *val)
@@ -875,10 +882,7 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,if (!spec)return -EUCLEAN ;/* request instruction poisoning */ t =btf_type_by_id (spec->btf, spec->spec[0 ].type_id); -if (btf_is_enum (t)) - *val =btf_enum (t)[spec->spec[0 ].idx].val; -else - *val =btf_enum64_value (btf_enum64 (t) + spec->spec[0 ].idx); + *val =bpf_core_enumval (t, spec->spec[0 ].idx);break ;default :return -EOPNOTSUPP ;
Signature · 1 line
2.53.1-Meta
Yonghong Song
reviewerPATCH 2/2 selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values
Test the zero extension of unsigned 32-bit enum values in CO-RE relocations from the previous patch.
test_core_reloc_enumval gets an optional local enum with values out of the range of int, i.e., an unsigned 4-byte enum, whose values clang emits zero-extended in the ld_imm64 of the relocations. The enumval target has the same enum with different values, 0x80000001 and 0xffffffff, which are expected zero-extended in the 64-bit outputs.
Without the fix, the check of the original value fails, and the whole object is refused:
libbpf: prog 'test_core_enumval': relo #11: unexpected insn #53 (LDIMM64)
value: got 2147483648, exp 18446744071562067968 -> 18446744071562067969
libbpf: prog 'test_core_enumval': relo #11: failed to patch insn #53: -EINVAL #122/71 core_reloc/enumval:FAIL
--- .../testing/selftests/bpf/prog_tests/core_reloc.c | 2 ++ .../testing/selftests/bpf/progs/core_reloc_types.h | 8 ++++++++ .../selftests/bpf/progs/test_core_reloc_enumval.c | 14 ++++++++++++++ 3 files changed, 24 insertions(+)
tools/testing/selftests/bpf/prog_tests/core_reloc.c +2 -0
@@ -904,6 +904,8 @@ static const struct core_reloc_test_case test_cases[] = { .named_val2 = 2, .anon_val1 = 0x10, .anon_val2 = 0x20, + .uint_val1 = 0x80000001, + .uint_val2 = 0xffffffff, }), ENUMVAL_CASE(enumval___diff, { .named_val1_exists = true,
tools/testing/selftests/bpf/progs/core_reloc_types.h +8 -0
@@ -1215,6 +1215,8 @@ struct core_reloc_enumval_output {int named_val2;int anon_val1;int anon_val2; +long uint_val1; +long uint_val2; };struct core_reloc_enum64val_output {
@@ -1243,9 +1245,15 @@ typedef enum {ANON_ENUM_VAL3 =0x30 , } anon_enum; +enum named_uint_enum { +UINT_ENUM_VAL1 =0x80000001 , +UINT_ENUM_VAL2 =0xffffffff , +}; +struct core_reloc_enumval {enum named_enum f1;anon_enum f2; +enum named_uint_enum f3; }; enum named_unsigned_enum64 {
tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c +14 -0
@@ -27,6 +27,12 @@ typedef enum {ANON_ENUM_VAL3 =0x30 , } anon_enum; +/* 4-byte enum with values out of the range of int, so it is unsigned */ +enum named_uint_enum { +UINT_ENUM_VAL1 =0x80000000 , +UINT_ENUM_VAL2 =0xfffffff0 , +}; +struct core_reloc_enumval_output {bool named_val1_exists;bool named_val2_exists;
@@ -39,6 +45,8 @@ struct core_reloc_enumval_output {int named_val2;int anon_val1;int anon_val2; +long uint_val1; +long uint_val2; };SEC ("raw_tracepoint/sys_enter" )
@@ -64,6 +72,12 @@ int test_core_enumval(void *ctx) out->anon_val1 =bpf_core_enum_value (anon,ANON_ENUM_VAL1 ); out->anon_val2 =bpf_core_enum_value (anon,ANON_ENUM_VAL2 );/* ANON_ENUM_VAL3 value is optional */ + +/* enum named_uint_enum is optional */ + if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL1)) + out->uint_val1 = bpf_core_enum_value(enum named_uint_enum, UINT_ENUM_VAL1); + if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL2)) + out->uint_val2 = bpf_core_enum_value(enum named_uint_enum, UINT_ENUM_VAL2);#else data.skip = true;#endif
Signature · 1 line
2.53.1-Meta
[email protected]
AI reviewTL;DR: Zero-extending unsigned 32-bit enum values in CO-RE relocations may make GCC-built objects, which appear to emit sign-extended values (unverified), fail to load with -EINVAL, in libbpf and…
Quoted 1 line
libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
TL;DR: Zero-extending unsigned 32-bit enum values in CO-RE relocations may make GCC-built objects, which appear to emit sign-extended values (unverified), fail to load with -EINVAL, in libbpf and kernel bpf_core_apply().
Quoted 3 lines
tools/lib/bpf/relo_core.c +0
-0
[ ... ]
tools/lib/bpf/relo_core.c static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo, · line 886
Quoted 10 lines
tools/lib/bpf/relo_core.c +1
-4@@ -875,10 +882,7 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,if (!spec)return -EUCLEAN ;/* request instruction poisoning */ t =btf_type_by_id (spec->btf, spec->spec[0 ].type_id); -if (btf_is_enum (t)) - *val =btf_enum (t)[spec->spec[0 ].idx].val; -else - *val =btf_enum64_value (btf_enum64 (t) + spec->spec[0 ].idx); + *val =bpf_core_enumval (t, spec->spec[0 ].idx);break ;
Can this break objects built by GCC?
bpf_core_patch_insn() compares the local (orig) value against the instruction immediate using a full 64-bit compare. In the ALU path that is insn->imm != orig_val, where the int imm is sign-extended, and in the LDIMM64 path it is:
(__u32)insn[0].imm | ((__u64)insn[1].imm << 32)
Clang emits unsigned enumerators zero-extended, which is the case this patch fixes. From reading the GCC source (not verified by compiling), GCC appears to emit every 32-bit enumerator sign-extended:
gcc/config/bpf/core-builtins.cc:pack_enum_value() {
ret.reloc_data.default_value = enum_value;
}bpf_expand_core_builtin() then does expand_normal(data->default_value) and wraps it in an UNSPEC. An SImode CONST_INT is stored sign-extended in RTL, so 0x80000000 would be printed as 0xffffffff80000000. GCC's btfout.cc also sets btf_kflag to BTF_KF_ENUM_UNSIGNED, which is 0, for unsigned enums.
For a GCC-built object calling bpf_core_enum_value() on an enumerator of 0x80000000 in an unsigned enum:
Before this patch, *val was the sign-extended 0xffffffff80000000, which matched the instruction.
After this patch, btf_enum_value() returns 0x80000000 for kflag == 0, so bpf_core_patch_insn() would report "unexpected insn #N (ALU/ALU64|LDIMM64)
value: got ..., exp 2147483648" and return -EINVAL. The same code is built into the kernel through kernel/bpf/relo_core.c, so bpf_core_apply() is affected as well.
Would objects built by Clang now load while GCC-built objects that loaded before are refused? The core_reloc/enumval tests are not in ci/vmtest/configs/DENYLIST.test_progs-bpf_gcc, and the test added by 0aff1079d5e1 ("selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values") uses UINT_ENUM_VAL1 = 0x80000000, so it may show this in the test_progs-bpf_gcc lane.
Could the validation for 32-bit enums compare only the low 32 bits and accept upper bits that are either zero or a sign extension? Alternatively, the GCC encoding could be determined before changing orig_val.
---
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/44347609693
Yonghong Song
reviewerQuoted 55 lines · On 11/18/26 8:59 AM, [email protected] wrote:
Quoted 1 line
libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
TL;DR: Zero-extending unsigned 32-bit enum values in CO-RE relocations may make GCC-built objects, which appear to emit sign-extended values (unverified), fail to load with
-EINVAL, in libbpf and kernelbpf_core_apply().Quoted 3 lines
tools/lib/bpf/relo_core.c +0
-0[ ... ]
tools/lib/bpf/relo_core.c static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo, · line 886
Quoted 10 lines
tools/lib/bpf/relo_core.c +1
-4@@ -875,10 +882,7 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,if (!spec)return -EUCLEAN ;/* request instruction poisoning */ t =btf_type_by_id (spec->btf, spec->spec[0 ].type_id); -if (btf_is_enum (t)) - *val =btf_enum (t)[spec->spec[0 ].idx].val; -else - *val =btf_enum64_value (btf_enum64 (t) + spec->spec[0 ].idx); + *val =bpf_core_enumval (t, spec->spec[0 ].idx);break ;Can this break objects built by GCC?
bpf_core_patch_insn()compares the local (orig) value against the instruction immediate using a full 64-bit compare. In the ALU path that isinsn->imm!=orig_val, where the int imm is sign-extended, and in the LDIMM64 path it is:(__u32)insn[0].imm | ((__u64)insn[1].imm << 32)Clang emits unsigned enumerators zero-extended, which is the case this patch fixes. From reading the GCC source (not verified by compiling), GCC appears to emit every 32-bit enumerator sign-extended:
gcc/config/bpf/core-builtins.cc:pack_enum_value() { ret.reloc_data.default_value = enum_value; }
bpf_expand_core_builtin()then doesexpand_normal(data->default_value)and wraps it in an UNSPEC. An SImodeCONST_INTis stored sign-extended in RTL, so0x80000000would be printed as0xffffffff80000000. GCC's btfout.cc also setsbtf_kflagtoBTF_KF_ENUM_UNSIGNED, which is 0, for unsigned enums.For a GCC-built object calling
bpf_core_enum_value()on an enumerator of0x80000000in an unsigned enum:Before this patch, *val was the sign-extended
0xffffffff80000000, which matched the instruction.After this patch,
btf_enum_value()returns0x80000000for kflag == 0, sobpf_core_patch_insn()would report "unexpected insn #N (ALU/ALU64|LDIMM64)
value: got ..., exp 2147483648" and return-EINVAL. The same code is built into the kernel throughkernel/bpf/relo_core.c, sobpf_core_apply()is affected as well.
Quoted 6 lines
Would objects built by Clang now load while GCC-built objects that loaded before are refused? The core_reloc/enumval tests are not in ci/vmtest/configs/DENYLIST.test_progs-bpf_gcc, and the test added by
0aff1079d5e1("selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values") usesUINT_ENUM_VAL1=0x80000000, so it may show this in the test_progs-bpf_gcc lane.
Yes, this is indeed an issue for GCC. Specifically because:
- BTF type is unsigned. This actually correct for the following:
enum dentry_flags { DCACHE_PRIVATE = 0x80000000 };- Without this patch, both clang and gcc will have enum val with sign extension. gcc enum return value has type int so naturally it will do sign extension, but clang return value is unsigned, but it use signed type, so this is wrong.
This patch fixed clang to use unsigned type based on BTF and this is correct.
For gcc, BTF type is unsigned (based on language), but the enum return value is signed. This caused a problem. gcc already generates code assuming signed type and we cannot change already generated code, so the only thing is to change gcc BTF type from 'unsigned' to 'signed'. This is doable though.
Quoted 4 lines
Could the validation for 32-bit enums compare only the low 32 bits and accept upper bits that are either zero or a sign extension? Alternatively, the GCC encoding could be determined before changing
orig_val.
So yes, we can change orig_val to be signed (instead of original unsigned type) to fix the issue. Then enum value will be different, for clang, the value is 32bit unsigned. For gcc, the value will be 32bit sign-extension.
The divergence between clang and gcc is in return type, clang (u64, unsigned) and gcc(int, signed). If gcc can have return value the same as clang, things can be simplified.
The following gcc diff might be useful for gcc folks:
gcc/config/bpf/bpf.cc +1 -1
@@ -1048,7 +1048,7 @@ bpf_init_builtins (void) integer_type_node, 0)); def_builtin ("__builtin_preserve_enum_value", BPF_BUILTIN_PRESERVE_ENUM_VALUE, - build_function_type_list (integer_type_node, ptr_type_node, + build_function_type_list (ullt, ptr_type_node, integer_type_node, integer_type_node, 0));
gcc/config/bpf/core-builtins.cc +5 -1
@@ -877,8 +877,12 @@ pack_enum_value_fail: else { ret.reloc_data.expr = tmp; + /* Extend the enumerator to 64 bits according to its own signedness, + as clang does, instead of expanding it as a sign-extended SImode + CONST_INT. */ if (kind == BPF_RELO_ENUMVAL_VALUE) - ret.reloc_data.default_value = enum_value; + ret.reloc_data.default_value + = fold_convert (long_long_unsigned_type_node, enum_value); else ret.reloc_data.default_value = integer_one_node; }
Quoted 7 lines
---
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/44347609693