Live Patching
 help / color / mirror / Atom feed
* [PATCH] objtool/klp: Copy constant pool entries for local labels
@ 2026-11-28  1:26 Binoy Ravindran via B4 Relay
  2026-11-30 20:18 ` Joe Lawrence
  0 siblings, 1 reply; 2+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-11-28  1:26 UTC (permalink / raw)
  To: Josh Poimboeuf, Peter Zijlstra, Song Liu
  Cc: linux-kernel, live-patching, 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
Signed-off-by: Binoy Ravindran <binoy@vt.edu>
---
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. Details
and the three affected modules are in the patch.

Tested on v7.4-rc2 (x86-64, GCC 12): with the fix, the three modules
hold the compiler's constants, and three unaffected modules are
unchanged. The patch applies to tip objtool/core and to mainline.
---
 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;

---
base-commit: b99a5337da3c847db216476d645b69acd714c3ba
change-id: 20261127-objtool-klp-cst-53a24047f0e9

Best regards,
--  
Binoy Ravindran <binoy@vt.edu>



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] objtool/klp: Copy constant pool entries for local labels
  2026-11-28  1:26 [PATCH] objtool/klp: Copy constant pool entries for local labels Binoy Ravindran via B4 Relay
@ 2026-11-30 20:18 ` Joe Lawrence
  0 siblings, 0 replies; 2+ messages in thread
From: Joe Lawrence @ 2026-11-30 20:18 UTC (permalink / raw)
  To: binoy; +Cc: Josh Poimboeuf, Peter Zijlstra, Song Liu, linux-kernel,
	live-patching

On Fri, Nov 27, 2026 at 08:26:09PM -0500, Binoy Ravindran via B4 Relay wrote:
> 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
> Signed-off-by: Binoy Ravindran <binoy@vt.edu>
> ---
> 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. Details
> and the three affected modules are in the patch.
> 
> Tested on v7.4-rc2 (x86-64, GCC 12): with the fix, the three modules
> hold the compiler's constants, and three unaffected modules are
> unchanged. The patch applies to tip objtool/core and to mainline.

Hi Binoy,

This fix looks reasonable to me:
Acked-by: Joe Lawrence <joe.lawrence@redhat.com>


Can you take a look at
tools/objtool/Documentation/{klp-write-tests.txt,klp-test-design.txt}
(or point your AI agent there) and incorporate a test case into the
objtool tests?  This patch looks like a good test candidate, and since
the the section is idential with the only difference between gcc and
clang being the zero-size .L* label name (.LC0 vs .LCPI0_0), a single
test in generic/ with that asserts on the .rodata.cst* section (rather
than the label name) should cover both compilers when CI runs the
matrix.

Thanks!

-- Joe


> ---
>  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;
> 
> ---
> base-commit: b99a5337da3c847db216476d645b69acd714c3ba
> change-id: 20261127-objtool-klp-cst-53a24047f0e9
> 
> Best regards,
> --  
> Binoy Ravindran <binoy@vt.edu>
> 
> 
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30 20:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28  1:26 [PATCH] objtool/klp: Copy constant pool entries for local labels Binoy Ravindran via B4 Relay
2026-11-30 20:18 ` Joe Lawrence

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox