[PATCH v2 1/2] objtool/klp: Copy constant pool entries for local labels

From: Binoy Ravindran via B4 Relay

Date: Mon Nov 30 2026 - 18:51:08 EST


From: Binoy Ravindran <binoy@xxxxxx>

The compiler refers to a constant in a .rodata.cst* pool through a local
label such as .LC32. The label has no size. Since commit 51e1dfce24c8
("objtool/klp: Don't correlate .rodata.cst* constant pool objects"),
klp-diff clones such a label together with its data. But
__clone_symbol() copies data only for a symbol with a nonzero length or
for a section symbol. For a .LC label it copies nothing and places the
label at the current end of the output section.

The patched function then reads whatever ends up at that place when the
module is linked. Building livepatches with klp-build for recent fixes
on v7.4-rc2 (x86-64, GCC 12), three of 54 modules read the wrong 2-byte
constant:

fib_table_insert() .LC32 expected 0x0000, reads 0x6e00
ieee80211_rx_mgmt_beacon() .LC68 expected 0x0000, reads 0x6e00
cfg80211_inform_bss_data() .LC9 expected 0x1000, reads 0x0000

In each case the bytes come from the end of an unrelated string in the
module's .rodata. The modules load and patch without any warning. We
confirmed this in a running kernel: with the first module loaded, the
patched fib_table_insert() loads 0x6e00.

A label in a SHF_MERGE constant pool names one entry of sh_entsize
bytes. Copy that entry when the label has no size. With this change, all
three modules hold the compiler's constants.

Fixes: 51e1dfce24c8 ("objtool/klp: Don't correlate .rodata.cst* constant pool objects")
Assisted-by: Claude:claude-opus-5-5
Acked-by: Joe Lawrence <joe.lawrence@xxxxxxxxxx>
Signed-off-by: Binoy Ravindran <binoy@xxxxxx>
---
tools/objtool/klp-diff.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 95d0d86666d7..f8a638b29a26 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -923,6 +923,7 @@ static struct symbol *__clone_symbol(struct elf *elf, struct symbol *patched_sym

if (data_too && !is_undef_sym(patched_sym)) {
struct section *patched_sec = patched_sym->sec;
+ size_t len = patched_sym->len;

out_sec = find_section_by_name(elf, patched_sec->name);
if (!out_sec) {
@@ -946,7 +947,17 @@ static struct symbol *__clone_symbol(struct elf *elf, struct symbol *patched_sym
if (!is_sec_sym(patched_sym))
offset = ALIGN(sec_size(out_sec), out_sec->sh.sh_addralign);

- if (patched_sym->len || is_sec_sym(patched_sym)) {
+ /*
+ * A local label in a constant pool (.rodata.cst*) has no size,
+ * but it names one entry of sh_entsize bytes. Copy that entry,
+ * or the label ends up pointing at whatever data follows it.
+ */
+ if (!len && !is_sec_sym(patched_sym) &&
+ (patched_sec->sh.sh_flags & SHF_MERGE) &&
+ !is_string_sec(patched_sec) && patched_sec->sh.sh_entsize)
+ len = patched_sec->sh.sh_entsize;
+
+ if (len || is_sec_sym(patched_sym)) {
void *data = NULL;
size_t size;

@@ -957,7 +968,7 @@ static struct symbol *__clone_symbol(struct elf *elf, struct symbol *patched_sym
if (is_sec_sym(patched_sym))
size = sec_size(patched_sym->sec);
else
- size = patched_sym->len;
+ size = len;

if (!elf_add_data(elf, out_sec, data, size))
return NULL;

--
2.39.5