* [PATCH v2 0/2] objtool/klp: Copy constant pool entries for local labels
@ 2026-11-30 23:50 Binoy Ravindran via B4 Relay
2026-11-30 23:50 ` [PATCH v2 1/2] " Binoy Ravindran via B4 Relay
2026-11-30 23:50 ` [PATCH v2 2/2] objtool/klp: Add test for constant pool labels Binoy Ravindran via B4 Relay
0 siblings, 2 replies; 3+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-11-30 23:50 UTC (permalink / raw)
To: Josh Poimboeuf, Peter Zijlstra, Song Liu, Nathan Chancellor,
Nick Desaulniers, Bill Wendling, Justin Stitt
Cc: Joe Lawrence, linux-kernel, llvm, Binoy Ravindran
A small problem in __clone_symbol(): a .LC label in a .rodata.cst*
pool has no size, so its constant is never copied into the livepatch
module, and the patched function reads whatever bytes follow. Patch 1
fixes it. Details and the three affected modules are in that patch.
Patch 2 adds a test to tools/objtool/tests, as Joe suggested. With
patch 1's change to klp-diff.c reverted it fails, and with the change
in place it passes, under gcc 12.2.0 and clang 14.0.6. The whole suite
passes under both compilers.
One thing I noticed while running the suite: with a gcc built with
--enable-default-pie, as Debian's is, -mnop-mcount is rejected,
preflight falls back to objtool's mcount, and every gcc test that
needs an ftrace call site fails with "no ftrace call site, can't
patch". CC="gcc -fno-PIE -fcf-protection=none" avoids it. The kernel
always builds with -fno-PIE, so FIXTURE_CFLAGS might want it as well.
I can send a patch for that if it is useful.
---
Changes in v2:
- Add a test for the fix (Joe).
- Add Joe's Acked-by to patch 1.
- Link to v1: https://patch.msgid.link/20261127-objtool-klp-cst-v1-1-dc19b08c8676@vt.edu
---
Binoy Ravindran (2):
objtool/klp: Copy constant pool entries for local labels
objtool/klp: Add test for constant pool labels
tools/objtool/klp-diff.c | 15 +++-
.../objtool/tests/generic/fixtures/constant_pool.c | 31 ++++++++
tools/objtool/tests/generic/test-constant-pool.sh | 91 ++++++++++++++++++++++
3 files changed, 135 insertions(+), 2 deletions(-)
---
base-commit: b99a5337da3c847db216476d645b69acd714c3ba
change-id: 20261127-objtool-klp-cst-53a24047f0e9
Best regards,
--
Binoy Ravindran <binoy@vt.edu>
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2 1/2] objtool/klp: Copy constant pool entries for local labels
2026-11-30 23:50 [PATCH v2 0/2] objtool/klp: Copy constant pool entries for local labels Binoy Ravindran via B4 Relay
@ 2026-11-30 23:50 ` Binoy Ravindran via B4 Relay
2026-11-30 23:50 ` [PATCH v2 2/2] objtool/klp: Add test for constant pool labels Binoy Ravindran via B4 Relay
1 sibling, 0 replies; 3+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-11-30 23:50 UTC (permalink / raw)
To: Josh Poimboeuf, Peter Zijlstra, Song Liu, Nathan Chancellor,
Nick Desaulniers, Bill Wendling, Justin Stitt
Cc: Joe Lawrence, linux-kernel, llvm, Binoy Ravindran
From: Binoy Ravindran <binoy@vt.edu>
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@redhat.com>
Signed-off-by: Binoy Ravindran <binoy@vt.edu>
---
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2 2/2] objtool/klp: Add test for constant pool labels
2026-11-30 23:50 [PATCH v2 0/2] objtool/klp: Copy constant pool entries for local labels Binoy Ravindran via B4 Relay
2026-11-30 23:50 ` [PATCH v2 1/2] " Binoy Ravindran via B4 Relay
@ 2026-11-30 23:50 ` Binoy Ravindran via B4 Relay
1 sibling, 0 replies; 3+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-11-30 23:50 UTC (permalink / raw)
To: Josh Poimboeuf, Peter Zijlstra, Song Liu, Nathan Chancellor,
Nick Desaulniers, Bill Wendling, Justin Stitt
Cc: Joe Lawrence, linux-kernel, llvm, Binoy Ravindran
From: Binoy Ravindran <binoy@vt.edu>
A function that loads a constant from a .rodata.cst* pool reaches it
through a local label with no size: .LC0 with gcc, .LCPI0_0 with clang.
klp diff clones that label with its data. Before the previous patch,
__clone_symbol() copied no data for such a label and placed it at the
end of the output section, so the patched function loaded whatever was
linked there. Nothing fails at build or load time.
The fixture loads a double, which neither x86 nor arm64 can encode as an
immediate, so both compilers put it in .rodata.cst8. The test finds the
label by section, type and size rather than by name. It checks that the
output pool holds the eight bytes that the label names in the patched
object, and that every label in the output points at them.
Verified on x86-64 with gcc 12.2.0 and clang 14.0.6. With the previous
patch's change to klp-diff.c reverted, the test fails because the
constant is not copied. With it restored, the test passes. The whole
suite passes under both compilers. gcc was run as
"gcc -fno-PIE -fcf-protection=none": the distribution's gcc builds PIE
by default and then rejects -mnop-mcount, which makes every gcc test
that needs an ftrace call site fail.
Suggested-by: Joe Lawrence <joe.lawrence@redhat.com>
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Binoy Ravindran <binoy@vt.edu>
---
.../objtool/tests/generic/fixtures/constant_pool.c | 31 ++++++++
tools/objtool/tests/generic/test-constant-pool.sh | 91 ++++++++++++++++++++++
2 files changed, 122 insertions(+)
diff --git a/tools/objtool/tests/generic/fixtures/constant_pool.c b/tools/objtool/tests/generic/fixtures/constant_pool.c
new file mode 100644
index 000000000000..73ff434a2420
--- /dev/null
+++ b/tools/objtool/tests/generic/fixtures/constant_pool.c
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A changed function that loads a constant from a .rodata.cst* pool.
+ *
+ * The compiler reaches a pool entry through a local label with no size: .LC0
+ * for gcc, .LCPI0_0 for clang. The assembler keeps that label in the symbol
+ * table, rather than relocating against the section, because the section is
+ * SHF_MERGE and the linker may move its entries.
+ *
+ * A floating-point constant is used because it is the one way freestanding C
+ * gets a constant pool from both compilers on both architectures: neither x86
+ * nor arm64 can encode an arbitrary double as an immediate. 1234.5678 is
+ * chosen so that no other bytes in the object look like it.
+ */
+
+static const char __modinfo[]
+ __attribute__((section(".modinfo"), used, aligned(1))) = "\0name=vmlinux";
+
+int untouched(int x)
+{
+ return x * 3;
+}
+
+double changed(double x)
+{
+#ifdef PATCHED
+ return x * 1234.5678;
+#else
+ return x * 2.5;
+#endif
+}
diff --git a/tools/objtool/tests/generic/test-constant-pool.sh b/tools/objtool/tests/generic/test-constant-pool.sh
new file mode 100755
index 000000000000..19e9468e0c08
--- /dev/null
+++ b/tools/objtool/tests/generic/test-constant-pool.sh
@@ -0,0 +1,91 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# A constant in a .rodata.cst* pool travels with the function that loads it.
+#
+# The compiler reaches the constant through a local label that has no size.
+# klp diff clones that label with its data, but __clone_symbol() used to copy
+# data only for a symbol with a size, so for such a label it copied nothing
+# and placed the label at the current end of the output section. The patched
+# function then loads whatever lands there when the module is linked: the
+# module builds, loads and patches without a warning, and computes with the
+# wrong constant.
+#
+# The label's name depends on the compiler (.LC0 for gcc, .LCPI0_0 for
+# clang), so the test finds it by section, type and size, and checks the bytes
+# rather than the name. The section is .rodata.cst8 under both compilers.
+
+. "$(dirname "$0")/../lib.sh"
+
+POOL=.rodata.cst8
+
+# pool_labels <object>
+#
+# The values of the sizeless, untyped symbols defined in $POOL: the labels the
+# compiler uses for pool entries. Section symbols are type SECTION, so they
+# are not included.
+pool_labels()
+{
+ local idx
+
+ idx="$($READELF -S -W "$1" 2>/dev/null |
+ sed -n "s/^ *\[ *\([0-9]*\)\] $(re_quote "$POOL") .*/\1/p" | head -1)"
+ [ -n "$idx" ] || return 0
+
+ $READELF -s -W "$1" 2>/dev/null |
+ awk -v i="$idx" '$4 == "NOTYPE" && $3 == "0" && $7 == i { print $2 }'
+}
+
+# pool_bytes <object>
+#
+# The contents of $POOL as one hex string, two digits per byte.
+pool_bytes()
+{
+ $OBJCOPY -O binary --only-section="$POOL" "$1" "$workdir/pool.bin" 2>/dev/null &&
+ od -An -v -tx1 "$workdir/pool.bin" | tr -d ' \n'
+}
+
+setup
+build_pair constant_pool.c
+
+# The premise: the patched function loads a constant from the pool through a
+# sizeless label. Without it the rest passes whether or not the bug is there.
+assert_input_section "$POOL"
+labels="$(pool_labels "$workdir/$patched_obj")"
+[ -n "$labels" ] || fail "no sizeless label in $POOL of $patched_obj"
+
+# The eight bytes the patched function expects: the entry at its label.
+label="$(echo "$labels" | head -1)"
+in_pool="$(pool_bytes "$workdir/$patched_obj")" || fail "cannot read $POOL of $patched_obj"
+off=$((16#$label))
+want="${in_pool:$((off * 2)):16}"
+[ "${#want}" = 16 ] || fail "label at $label lies outside $POOL of $patched_obj"
+
+run_diff
+
+assert_patched changed
+assert_not_patched untouched
+
+# The result: the output pool holds that entry, and every label in it points
+# at an entry inside the section rather than at its end.
+assert_section "$POOL"
+out_pool="$(pool_bytes "$workdir/out.o")" || fail "cannot read $POOL of out.o"
+
+case "$out_pool" in
+*"$want"*) ;;
+*) fail "the constant ($want) was not copied into $POOL of out.o" \
+ "(contents: ${out_pool:-empty})" ;;
+esac
+
+out_labels="$(pool_labels "$workdir/out.o")"
+for l in $out_labels; do
+ o=$((16#$l))
+ got="${out_pool:$((o * 2)):16}"
+ [ "${#got}" = 16 ] ||
+ fail "label at $l points past the end of $POOL in out.o" \
+ "($(( ${#out_pool} / 2 )) bytes)"
+ [ "$got" = "$want" ] ||
+ fail "label at $l in out.o names $got, expected $want"
+done
+
+pass "constant pool entry copied with its sizeless label"
--
2.39.5
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-30 23:51 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 23:50 [PATCH v2 0/2] objtool/klp: Copy constant pool entries for local labels Binoy Ravindran via B4 Relay
2026-11-30 23:50 ` [PATCH v2 1/2] " Binoy Ravindran via B4 Relay
2026-11-30 23:50 ` [PATCH v2 2/2] objtool/klp: Add test for constant pool labels Binoy Ravindran via B4 Relay
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®