Skip to contentSkip to conversation

libbpf: Fix CO-RE relocations of unsigned 32-bit enum values

PATCH Applied Yonghong Song 2 participants 4 messages Nov 24

Subsystems (4)
lore
Share & tools
lore

Yonghong Song

author reviewer
#0 lore raw

v6 cover libbpf: Fix CO-RE relocations of unsigned 32-bit enum values

For a BPF_CORE_ENUMVAL_VALUE relocation, libbpf sign-extends the value of a 32-bit enumerator even if the enum is unsigned. A value in [0x80000000, 0xffffffff], e.g., DCACHE_PRIVATE of enum dentry_flags, then doesn't match the zero-extended value clang emits, and the whole object is refused. And a target value in that range is patched sign-extended.

Patch 1 extends a 32-bit enum value according to the kflag of its enum, i.e., sign-extends it if the enum is signed (kflag 1), and zero-extends it otherwise (kflag 0), for both the local and the target value. The kernel builds the same relo_core.c, so it gets the fix too.

GCC emits every 32-bit enum value sign-extended, since its __builtin_preserve_enum_value() returns int, so a GCC object with an unsigned enum value in [0x80000000, 0xffffffff] is now refused. That needs to be fixed in GCC.

Patch 2 tests the unsigned enum values. With GCC, the test is skipped if the target BTF has the unsigned enum.

Changelog:
v5 -> v6:
- v5: https://lore.kernel.org/bpf/[email protected]/
- Extend a 32-bit enum value according to the kflag of its own enum in bpf_core_calc_enumval_relo(), instead of zero-extending the values in bpf_core_calc_relo() (patch 1).
- Don't take a target value with the kflag 0 as signed if the local enum is signed, and drop its test (patch 3).
- Don't accept the sign-extended form of an unsigned enum value emitted by GCC (patch 1), and skip the test with GCC instead
(patch 2).
v4 -> v5:
- v4: https://lore.kernel.org/bpf/[email protected]/
- Keep the sign extension in bpf_core_calc_enumval_relo(), and zero-extend the 32-bit values of unsigned enums once in bpf_core_calc_relo(). This drops btf_enum_value() from include/linux/btf.h and the three helpers in relo_core.c. No
change in behavior (patch 1).
v3 -> v4:
- v3: https://lore.kernel.org/bpf/[email protected]/
- Take a 32-bit target enum value as signed if the target enum has the kflag 0 but the local enum is signed, for the BTF of kernels without enum signedness (patch 1).
- Test it with a target BTF whose enums have the kflag cleared
(patch 3).
v2 -> v3:
- v2: https://lore.kernel.org/bpf/[email protected]/
- Fold patch 3 into patch 1 and patch 4 into patch 2 to avoid test failures when bisecting.
v1 -> v2:
- v1: https://lore.kernel.org/bpf/[email protected]/
- Accept the sign-extended enum values emitted by GCC (patch 3), and make the new test work with GCC (patch 4).

Yonghong Song (2):
libbpf: Fix CO-RE relocations of unsigned 32-bit enum values selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values

 tools/lib/bpf/relo_core.c                     |  9 +++++---
 .../selftests/bpf/prog_tests/core_reloc.c     |  2 ++
 .../selftests/bpf/progs/core_reloc_types.h    |  8 +++++++
 .../bpf/progs/test_core_reloc_enumval.c       | 23 +++++++++++++++++++
 4 files changed, 39 insertions(+), 3 deletions(-)
Signature · 1 line
2.53.1-Meta
author reviewer
#1 lore raw

PATCH v6 1/2 libbpf: Fix CO-RE relocations of unsigned 32-bit enum values

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") fixed this for btf_dump and map fields, but not for CO-RE relocations, whose relo_core.c is also built into the kernel. Extend a 32-bit enum value according to the kflag of its enum in bpf_core_calc_enumval_relo(), i.e., sign-extend it if the kflag is 1, and zero-extend it otherwise, for both the local and the target value.

The kflag 0 is taken as unsigned. pahole records the signedness since v1.24. The BTF of a kernel before v6.0 has the kflag 0 on every enum, though, so a negative target value there is now patched zero-extended.

GCC sign-extends every 32-bit enum value in the ld_imm64, since its __builtin_preserve_enum_value() returns int, so a GCC object with an unsigned enum value in [0x80000000, 0xffffffff] is now refused. That needs to be fixed in GCC, whose builtin should return long, as clang's does.

Fixes 776281652ddc ("libbpf: Permit 64bit relocation value")
---
 tools/lib/bpf/relo_core.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
tools/lib/bpf/relo_core.c +6 -3
index 2672623a4198..58c782cc60e8 100644
@@ -866,6 +866,7 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
 				      __u64 *val)
 {
 	const struct btf_type *t;
+	const struct btf_enum *e;
 
 	switch (relo->kind) {
 	case BPF_CORE_ENUMVAL_EXISTS:
@@ -875,10 +876,12 @@ 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
+		if (btf_is_enum(t)) {
+			e = btf_enum(t) + spec->spec[0].idx;
+			*val = BTF_INFO_KFLAG(t->info) ? (__s64)e->val : (__u32)e->val;
+		} else {
 			*val = btf_enum64_value(btf_enum64(t) + spec->spec[0].idx);
+		}
 		break;
 	default:
 		return -EOPNOTSUPP;
Signature · 1 line
2.53.1-Meta
author reviewer
#2 lore raw

PATCH v6 2/2 selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values

In reply to Yonghong Song

Test the 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.

GCC's __builtin_preserve_enum_value() returns int, so GCC emits these values sign-extended, and libbpf refuses the object. With GCC, don't read the values of enum named_uint_enum. Only test whether the enum exists in the target BTF, which libbpf patches without checking the value, and skip the test if it does.

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

---
 .../selftests/bpf/prog_tests/core_reloc.c     |  2 ++
 .../selftests/bpf/progs/core_reloc_types.h    |  8 +++++++
 .../bpf/progs/test_core_reloc_enumval.c       | 23 +++++++++++++++++++
 3 files changed, 33 insertions(+)
tools/testing/selftests/bpf/prog_tests/core_reloc.c +2 -0
index 80432c4656b5..98b3c5fb1028 100644
@@ -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
index 5760ae015e09..6d758e74bd58 100644
@@ -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 +23 -0
index e7ef3dada2bf..9933eadc2993 100644
@@ -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,21 @@ 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 */
+#ifdef __clang__
+	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
+	/*
+	 * GCC's __builtin_preserve_enum_value() returns int, so GCC emits the
+	 * values sign-extended, and libbpf refuses them.
+	 */
+	if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL1))
+		data.skip = true;
+#endif
 #else
 	data.skip = true;
 #endif
Signature · 1 line
2.53.1-Meta
#3 lore raw
In reply to Yonghong Song
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 8 lines · On Mon, 23 Nov 2026 16:05:14 -0800 you wrote:

For a BPF_CORE_ENUMVAL_VALUE relocation, libbpf sign-extends the value of a 32-bit enumerator even if the enum is unsigned. A value in [0x80000000, 0xffffffff], e.g., DCACHE_PRIVATE of enum dentry_flags, then doesn't match the zero-extended value clang emits, and the whole object is refused. And a target value in that range is patched sign-extended.

[...]

Here is the summary with links:
- [bpf-next,v6,1/2] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values

    https://git.kernel.org/bpf/bpf-next/c/4fad237494f1
  - [bpf-next,v6,2/2] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values
    https://git.kernel.org/bpf/bpf-next/c/885fd5836d3d

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html