OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
* [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build
@ 2026-11-25  6:03 Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 01/18] ms/objtool/klp: Fix detection of corrupt static branch/call entries Eva Kurchatova
                   ` (18 more replies)
  0 siblings, 19 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

Patchset for task https://virtuozzo.atlassian.net/browse/VSTOR-128510

klp-build in 26.x is the RHEL 10.2 import. Mainline has fixed many bugs
in it since, and the objtool in it fails 31 of 48 mainline tests.

This series takes only the mainline fixes for bugs that a vz10 build
(x86_64, gcc) can hit and that make klp-build produce a livepatch which
builds but is broken, or refuse a common patch:

- pointers to const data compared against a private copy (13)
- klp relocations to exports of another module put in a section the
  livepatch loader never applies, e.g. jump into garbage (14-16)
- static symbols bound to an export of the same name (17)
- strings and data cloned at the wrong offset (3, 4)
- module static keys not rejected at build time (1, 6)
- WARN_ONCE() flags correlated between unrelated call sites (5)
- functions using pr_debug() in modules can't be patched (2); objtool
  crashes on empty sections (8) and corrupts a relocation (10)

Patches 7, 9, 11 and 12 are mainline prerequisites of 13. All picks
match mainline line for line.

Since patch 13, a named const object is correlated, and the livepatch
references the original, so a patch which changes the object would
silently miss the change. Mainline rejects such a patch since its
correlation rewrite, 6016dd33a10a, which is too large to take here.

Patch 14 is original, and makes klp diff fail on changed .rodata object.

Changes in v2:
- Patch 14: Fail klp diff when a correlated .rodata object changed.
- Cover letter: Reflect the .rodata object handling change.
- Reviewed-by: Sashiko on patches 1-12.

Link to v1: http://lore.virtuozzo.com/kernel/20261124050655.1853940-1-eva.kurchatova@virtuozzo.com/T/#t

Eva Kurchatova (1):
  objtool/klp: Reject changed .rodata objects

Joe Lawrence (3):
  ms/objtool/klp: fix data alignment in __clone_symbol()
  ms/objtool/klp: Fix create_fake_symbols() skipping entsize-based
    sections
  ms/objtool/klp: Normalize Module.symvers paths to module names

Josh Poimboeuf (14):
  ms/objtool/klp: Fix detection of corrupt static branch/call entries
  ms/objtool/klp: Disable unsupported pr_debug() usage
  ms/objtool: Fix data alignment in elf_add_data()
  ms/objtool/klp: Fix .data..once static local non-correlation
  ms/objtool/klp: Fix handling of zero-length .altinstr_replacement
    sections
  ms/objtool/klp: Fix cloning of zero-length section symbols
  ms/objtool/klp: Fix relocation conversion failures for R_X86_64_NONE
  ms/objtool/klp: Fix reloc corruption in convert_reloc_sym_to_secsym()
  ms/objtool/klp: Simplify reloc symbol conversion
  ms/objtool: Move mark_rodata() to elf.c
  ms/objtool/klp: Fix pointer comparisons for rodata objects
  ms/objtool/klp: Fix module name normalization for paths with dots
  ms/objtool/klp: Fix cross-module klp relocation section naming
  ms/objtool/klp: Don't match local symbols against exports

 tools/objtool/check.c               |  11 +-
 tools/objtool/elf.c                 |  29 ++-
 tools/objtool/include/objtool/elf.h |   6 +
 tools/objtool/include/objtool/klp.h |  10 +-
 tools/objtool/klp-diff.c            | 324 +++++++++++++++++++++-------
 tools/objtool/klp-post-link.c       |  53 +++--
 6 files changed, 321 insertions(+), 112 deletions(-)

-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 01/18] ms/objtool/klp: Fix detection of corrupt static branch/call entries
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 02/18] ms/objtool/klp: Disable unsupported pr_debug() usage Eva Kurchatova
                   ` (17 subsequent siblings)
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Josh Poimboeuf <jpoimboe@kernel.org>

Patching a function which references a static key living in a kernel
module is unsupported due to ordering issues inherent to late module
patching:

  1) Load a livepatch module which has a __jump_table entry which needs
     a klp reloc to reference static key K which lives in module M.

  2) The __jump_table klp reloc does *not* get resolved because module M
     is not yet loaded.

  3) jump_label_add_module() corrupts memory (or causes a panic) when
     dereferencing the uninitialized pointer to key K.

validate_special_section_klp_reloc() intends to prevent that from ever
happening by catching it at build time.  However, it incorrectly assumes
the special section entry's reloc symbol references have already been
converted from section symbols to object symbols, causing the validation
to miss corruption in extracted static branch/call table entries.

Make sure the references have been properly converted before doing the
validation.

Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Reported-by: Song Liu <song@kernel.org>
Reviewed-and-tested-by: Song Liu <song@kernel.org>
Link: https://patch.msgid.link/124ad747b751df0df1725eff89de8332e3fb26d6.1770759954.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit f9fb44b0ecefc1f218db56661ed66d4e8d67317d)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 tools/objtool/klp-diff.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 4d1f9e9977eb..2db34b86e655 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1335,6 +1335,9 @@ static int validate_special_section_klp_reloc(struct elfs *e, struct symbol *sym
 		const char *sym_modname;
 		struct export *export;
 
+		if (convert_reloc_sym(e->patched, reloc))
+			continue;
+
 		/* Static branch/call keys are always STT_OBJECT */
 		if (reloc->sym->type != STT_OBJECT) {
 
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 02/18] ms/objtool/klp: Disable unsupported pr_debug() usage
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 01/18] ms/objtool/klp: Fix detection of corrupt static branch/call entries Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 03/18] ms/objtool: Fix data alignment in elf_add_data() Eva Kurchatova
                   ` (16 subsequent siblings)
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Josh Poimboeuf <jpoimboe@kernel.org>

Instead of erroring out on unsupported pr_debug() (e.g., when patching a
module), issue a warning and make it inert, similar to how unsupported
tracepoints are currently handled.

Reviewed-and-tested-by: Song Liu <song@kernel.org>
Link: https://patch.msgid.link/3a7db3a5b7d4abf9b2534803a74e2e7231322738.1770759954.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit e476bb277cf91b7ac3ea803ec78a4f0791bddec3)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 tools/objtool/klp-diff.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 2db34b86e655..13319ee9d56e 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1305,18 +1305,18 @@ static bool should_keep_special_sym(struct elf *elf, struct symbol *sym)
  * be applied after static branch/call init, resulting in code corruption.
  *
  * Validate a special section entry to avoid that.  Note that an inert
- * tracepoint is harmless enough, in that case just skip the entry and print a
- * warning.  Otherwise, return an error.
+ * tracepoint or pr_debug() is harmless enough, in that case just skip the
+ * entry and print a warning.  Otherwise, return an error.
  *
- * This is only a temporary limitation which will be fixed when livepatch adds
- * support for submodules: fully self-contained modules which are embedded in
- * the top-level livepatch module's data and which can be loaded on demand when
- * their corresponding to-be-patched module gets loaded.  Then klp relocs can
- * be retired.
+ * TODO: This is only a temporary limitation which will be fixed when livepatch
+ * adds support for submodules: fully self-contained modules which are embedded
+ * in the top-level livepatch module's data and which can be loaded on demand
+ * when their corresponding to-be-patched module gets loaded.  Then klp relocs
+ * can be retired.
  *
  * Return:
  *   -1: error: validation failed
- *    1: warning: tracepoint skipped
+ *    1: warning: disabled tracepoint or pr_debug()
  *    0: success
  */
 static int validate_special_section_klp_reloc(struct elfs *e, struct symbol *sym)
@@ -1374,6 +1374,13 @@ static int validate_special_section_klp_reloc(struct elfs *e, struct symbol *sym
 				continue;
 			}
 
+			if (strstr(reloc->sym->name, "__UNIQUE_ID_ddebug_")) {
+				WARN("%s: disabling unsupported pr_debug()",
+				     code_sym->name);
+				ret = 1;
+				continue;
+			}
+
 			ERROR("%s+0x%lx: unsupported static branch key %s.  Use static_key_enabled() instead",
 			      code_sym->name, code_offset, reloc->sym->name);
 			return -1;
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 03/18] ms/objtool: Fix data alignment in elf_add_data()
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 01/18] ms/objtool/klp: Fix detection of corrupt static branch/call entries Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 02/18] ms/objtool/klp: Disable unsupported pr_debug() usage Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 04/18] ms/objtool/klp: fix data alignment in __clone_symbol() Eva Kurchatova
                   ` (15 subsequent siblings)
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Josh Poimboeuf <jpoimboe@kernel.org>

Any data added to a section needs to be aligned in accordance with the
section's sh_addralign value.  Particularly strings added to a .str1.8
section.  Otherwise you may get some funky strings.

Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Link: https://patch.msgid.link/d962fc0ca24fa0825cca8dad71932dccdd9312a9.1772681234.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 356e4b2f5b80f757965f3f4d0219c81fca91b6f2)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 tools/objtool/elf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index bc1ca8e439c9..da20cec49374 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -1351,7 +1351,7 @@ void *elf_add_data(struct elf *elf, struct section *sec, const void *data, size_
 		memcpy(sec->data->d_buf, data, size);
 
 	sec->data->d_size = size;
-	sec->data->d_align = 1;
+	sec->data->d_align = sec->sh.sh_addralign;
 
 	offset = ALIGN_UP(sec->sh.sh_size, sec->sh.sh_addralign);
 	sec->sh.sh_size = offset + size;
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 04/18] ms/objtool/klp: fix data alignment in __clone_symbol()
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (2 preceding siblings ...)
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 03/18] ms/objtool: Fix data alignment in elf_add_data() Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 05/18] ms/objtool/klp: Fix .data..once static local non-correlation Eva Kurchatova
                   ` (14 subsequent siblings)
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Joe Lawrence <joe.lawrence@redhat.com>

Commit 356e4b2f5b80 ("objtool: Fix data alignment in elf_add_data()")
corrected the alignment of data within a section (honoring the section's
sh_addralign).  Apply the same alignment when klp-diff mode clones a
symbol, adjusting the new symbol's offset for the output section's
sh_addralign.

Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
Link: https://patch.msgid.link/20260310203751.1479229-2-joe.lawrence@redhat.com
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 2f2600decb3004938762a3f2d0eba3ea9e01045b)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 tools/objtool/klp-diff.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 13319ee9d56e..e0c64690c1e0 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -14,6 +14,7 @@
 #include <objtool/util.h>
 #include <arch/special.h>
 
+#include <linux/align.h>
 #include <linux/objtool_types.h>
 #include <linux/livepatch_external.h>
 #include <linux/stringify.h>
@@ -531,7 +532,7 @@ static struct symbol *__clone_symbol(struct elf *elf, struct symbol *patched_sym
 		}
 
 		if (!is_sec_sym(patched_sym))
-			offset = sec_size(out_sec);
+			offset = ALIGN(sec_size(out_sec), out_sec->sh.sh_addralign);
 
 		if (patched_sym->len || is_sec_sym(patched_sym)) {
 			void *data = NULL;
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 05/18] ms/objtool/klp: Fix .data..once static local non-correlation
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (3 preceding siblings ...)
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 04/18] ms/objtool/klp: fix data alignment in __clone_symbol() Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 06/18] ms/objtool/klp: Fix create_fake_symbols() skipping entsize-based sections Eva Kurchatova
                   ` (13 subsequent siblings)
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Josh Poimboeuf <jpoimboe@kernel.org>

While there was once a section named .data.once, it has since been
renamed to .data..once with commit dbefa1f31a91 ("Rename .data.once to
.data..once to fix resetting WARN*_ONCE").  Fix it.

Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Acked-by: Song Liu <song@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit ff529864e738f447ff4c019956319930fc274a23)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 tools/objtool/klp-diff.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index e0c64690c1e0..0f9ce6541e93 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -256,7 +256,8 @@ static bool is_uncorrelated_static_local(struct symbol *sym)
 	if (!is_object_sym(sym) || !is_local_sym(sym))
 		return false;
 
-	if (!strcmp(sym->sec->name, ".data.once"))
+	/* WARN_ONCE, etc */
+	if (!strcmp(sym->sec->name, ".data..once"))
 		return true;
 
 	for (int i = 0; i < ARRAY_SIZE(vars); i++) {
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 06/18] ms/objtool/klp: Fix create_fake_symbols() skipping entsize-based sections
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (4 preceding siblings ...)
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 05/18] ms/objtool/klp: Fix .data..once static local non-correlation Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 07/18] ms/objtool/klp: Fix handling of zero-length .altinstr_replacement sections Eva Kurchatova
                   ` (12 subsequent siblings)
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Joe Lawrence <joe.lawrence@redhat.com>

create_fake_symbols() has two phases: creating symbols from
ANNOTATE_DATA_SPECIAL entries, and a fallback that uses sh_entsize for
special sections like .static_call_sites.

When .discard.annotate_data is absent, the function returns early,
skipping the entsize fallback and silently allowing unsupported
module-local static call keys through.

Fix it by jumping to the entsize phase instead of returning early.

Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Assisted-by: Claude:claude-4-opus
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
Acked-by: Song Liu <song@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 3de711fba73ad93b8b3fbe09cf681cefed5d573d)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 tools/objtool/klp-diff.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 0f9ce6541e93..9dd2224f45aa 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1219,7 +1219,7 @@ static int create_fake_symbols(struct elf *elf)
 
 	sec = find_section_by_name(elf, ".discard.annotate_data");
 	if (!sec || !sec->rsec)
-		return 0;
+		goto entsize;
 
 	for_each_reloc(sec->rsec, reloc) {
 		unsigned long offset, size;
@@ -1251,7 +1251,7 @@ static int create_fake_symbols(struct elf *elf)
 	/*
 	 * 2) Make symbols for sh_entsize, and simple arrays of pointers:
 	 */
-
+entsize:
 	for_each_sec(elf, sec) {
 		unsigned int entry_size;
 		unsigned long offset;
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 07/18] ms/objtool/klp: Fix handling of zero-length .altinstr_replacement sections
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (5 preceding siblings ...)
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 06/18] ms/objtool/klp: Fix create_fake_symbols() skipping entsize-based sections Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 08/18] ms/objtool/klp: Fix cloning of zero-length section symbols Eva Kurchatova
                   ` (11 subsequent siblings)
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Josh Poimboeuf <jpoimboe@kernel.org>

When a section is empty (e.g. only zero-length alternative
replacements), there are no symbols to convert a section symbol
reference to.  Skip the reloc instead of erroring out.

Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Acked-by: Song Liu <song@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit c4c02d4450b5b7e2fbde578252f71a5697180112)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 tools/objtool/klp-diff.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 9dd2224f45aa..5e39839759bb 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -864,6 +864,13 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
 	/* No dedicated section; find the symbol manually */
 	sym = find_symbol_containing(sec, arch_adjusted_addend(reloc));
 	if (!sym) {
+		/*
+		 * This is presumably an .altinstr_replacement section which is
+		 * empty due to it only having zero-length replacement(s).
+		 */
+		if (!sec_size(sec))
+			return 1;
+
 		/*
 		 * This can happen for special section references to weak code
 		 * whose symbol has been stripped by the linker.
@@ -1124,6 +1131,7 @@ static int clone_sym_relocs(struct elfs *e, struct symbol *patched_sym)
 
 	for_each_reloc(patched_rsec, patched_reloc) {
 		unsigned long offset;
+		int ret;
 
 		if (reloc_offset(patched_reloc) < start ||
 		    reloc_offset(patched_reloc) >= end)
@@ -1137,12 +1145,15 @@ static int clone_sym_relocs(struct elfs *e, struct symbol *patched_sym)
 		    !strcmp(patched_reloc->sym->sec->name, ".altinstr_aux"))
 			continue;
 
-		if (convert_reloc_sym(e->patched, patched_reloc)) {
+		ret = convert_reloc_sym(e->patched, patched_reloc);
+		if (ret < 0) {
 			ERROR_FUNC(patched_rsec->base, reloc_offset(patched_reloc),
 				   "failed to convert reloc sym '%s' to its proper format",
 				   patched_reloc->sym->name);
 			return -1;
 		}
+		if (ret > 0)
+			continue;
 
 		offset = out_sym->offset + (reloc_offset(patched_reloc) - patched_sym->offset);
 
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 08/18] ms/objtool/klp: Fix cloning of zero-length section symbols
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (6 preceding siblings ...)
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 07/18] ms/objtool/klp: Fix handling of zero-length .altinstr_replacement sections Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 09/18] ms/objtool/klp: Fix relocation conversion failures for R_X86_64_NONE Eva Kurchatova
                   ` (10 subsequent siblings)
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Josh Poimboeuf <jpoimboe@kernel.org>

Fix NULL dereference when cloning a symbol from an empty section.
sec->data is only populated for sections with non-zero size.

Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Acked-by: Song Liu <song@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 98377f3ba7c02d6eb34e203c9f9823bc62b0d231)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 tools/objtool/klp-diff.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 5e39839759bb..817db8540c8b 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -540,7 +540,7 @@ static struct symbol *__clone_symbol(struct elf *elf, struct symbol *patched_sym
 			size_t size;
 
 			/* bss doesn't have data */
-			if (patched_sym->sec->data->d_buf)
+			if (patched_sym->sec->data && patched_sym->sec->data->d_buf)
 				data = patched_sym->sec->data->d_buf + patched_sym->offset;
 
 			if (is_sec_sym(patched_sym))
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 09/18] ms/objtool/klp: Fix relocation conversion failures for R_X86_64_NONE
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (7 preceding siblings ...)
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 08/18] ms/objtool/klp: Fix cloning of zero-length section symbols Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 10/18] ms/objtool/klp: Fix reloc corruption in convert_reloc_sym_to_secsym() Eva Kurchatova
                   ` (9 subsequent siblings)
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Josh Poimboeuf <jpoimboe@kernel.org>

Objtool has some hacks which NOP out certain calls/jumps and replace
their relocations with R_X86_64_NONE.  The klp-diff relocation
extraction code will error out when trying to copy these relocations due
to their negative addend, which would only makes sense for a PC-relative
branch instruction.  Just ignore them.

Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Acked-by: Song Liu <song@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 3787e82a4e3a0a04aeb5543580ee90bed3a36e55)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 tools/objtool/klp-diff.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 817db8540c8b..941be5829ce7 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -891,6 +891,9 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
  */
 static int convert_reloc_sym(struct elf *elf, struct reloc *reloc)
 {
+	if (reloc_type(reloc) == R_NONE)
+		return 1;
+
 	if (is_reloc_allowed(reloc))
 		return 0;
 
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 10/18] ms/objtool/klp: Fix reloc corruption in convert_reloc_sym_to_secsym()
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (8 preceding siblings ...)
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 09/18] ms/objtool/klp: Fix relocation conversion failures for R_X86_64_NONE Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 11/18] ms/objtool/klp: Simplify reloc symbol conversion Eva Kurchatova
                   ` (8 subsequent siblings)
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Josh Poimboeuf <jpoimboe@kernel.org>

Use the section symbol's index instead of the old symbol's index when
updating the ELF relocation entry in convert_reloc_sym_to_secsym().

Found by Sashiko review.

Acked-by: Song Liu <song@kernel.org>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 5f49ec82b9f65507c262a6e3f1b53663f79eedce)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 tools/objtool/klp-diff.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 941be5829ce7..fa7a5c857b73 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -846,7 +846,7 @@ static int convert_reloc_sym_to_secsym(struct elf *elf, struct reloc *reloc)
 		return -1;
 
 	reloc->sym = sec->sym;
-	set_reloc_sym(elf, reloc, sym->idx);
+	set_reloc_sym(elf, reloc, sec->sym->idx);
 	set_reloc_addend(elf, reloc, sym->offset + reloc_addend(reloc));
 	return 0;
 }
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 11/18] ms/objtool/klp: Simplify reloc symbol conversion
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (9 preceding siblings ...)
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 10/18] ms/objtool/klp: Fix reloc corruption in convert_reloc_sym_to_secsym() Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 12/18] ms/objtool: Move mark_rodata() to elf.c Eva Kurchatova
                   ` (7 subsequent siblings)
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Josh Poimboeuf <jpoimboe@kernel.org>

Inline section_reference_needed() and is_reloc_allowed() into
convert_reloc_sym() and remove the redundant is_reloc_allowed() check in
clone_reloc().

Move the is_sec_sym() checks into the convert callees so they become
no-ops when the reloc is already in the right format.  This allows
convert_reloc_sym() to unconditionally dispatch to the right converter
based on section type.

Acked-by: Song Liu <song@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 8fdc3585b3b09f2c55d8573edc6a1dbfeb728f03)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 tools/objtool/klp-diff.c | 76 +++++++++++++++-------------------------
 1 file changed, 28 insertions(+), 48 deletions(-)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index fa7a5c857b73..55208c6edfc6 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -696,39 +696,6 @@ static int clone_included_functions(struct elfs *e)
 	return 0;
 }
 
-/*
- * Determine whether a relocation should reference the section rather than the
- * underlying symbol.
- */
-static bool section_reference_needed(struct section *sec)
-{
-	/*
-	 * String symbols are zero-length and uncorrelated.  It's easier to
-	 * deal with them as section symbols.
-	 */
-	if (is_string_sec(sec))
-		return true;
-
-	/*
-	 * .rodata has mostly anonymous data so there's no way to determine the
-	 * length of a needed reference.  just copy the whole section if needed.
-	 */
-	if (strstarts(sec->name, ".rodata"))
-		return true;
-
-	/* UBSAN anonymous data */
-	if (strstarts(sec->name, ".data..Lubsan") ||	/* GCC */
-	    strstarts(sec->name, ".data..L__unnamed_"))	/* Clang */
-		return true;
-
-	return false;
-}
-
-static bool is_reloc_allowed(struct reloc *reloc)
-{
-	return section_reference_needed(reloc->sym->sec) == is_sec_sym(reloc->sym);
-}
-
 static struct export *find_export(struct symbol *sym)
 {
 	struct export *export;
@@ -837,11 +804,15 @@ static bool klp_reloc_needed(struct reloc *patched_reloc)
 	return true;
 }
 
+/* Return -1 error, 0 success, 1 skip */
 static int convert_reloc_sym_to_secsym(struct elf *elf, struct reloc *reloc)
 {
 	struct symbol *sym = reloc->sym;
 	struct section *sec = sym->sec;
 
+	if (is_sec_sym(sym))
+		return 0;
+
 	if (!sec->sym && !elf_create_section_symbol(elf, sec))
 		return -1;
 
@@ -851,11 +822,15 @@ static int convert_reloc_sym_to_secsym(struct elf *elf, struct reloc *reloc)
 	return 0;
 }
 
+/* Return -1 error, 0 success, 1 skip */
 static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
 {
 	struct symbol *sym = reloc->sym;
 	struct section *sec = sym->sec;
 
+	if (!is_sec_sym(sym))
+		return 0;
+
 	/* If the symbol has a dedicated section, it's easy to find */
 	sym = find_symbol_by_offset(sec, 0);
 	if (sym && sym->len == sec_size(sec))
@@ -885,22 +860,34 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
 	return 0;
 }
 
+/*
+ * Sections with anonymous or uncorrelated data (strings, UBSAN data)
+ * need section symbol references.
+ */
+static bool is_uncorrelated_section(struct section *sec)
+{
+	return is_string_sec(sec) ||
+	       strstarts(sec->name, ".rodata") ||
+	       strstarts(sec->name, ".data..Lubsan") ||		/* GCC */
+	       strstarts(sec->name, ".data..L__unnamed_");	/* Clang */
+}
+
 /*
  * Convert a relocation symbol reference to the needed format: either a section
- * symbol or the underlying symbol itself.
+ * symbol or the underlying symbol itself.  Return -1 error, 0 success, 1 skip.
  */
 static int convert_reloc_sym(struct elf *elf, struct reloc *reloc)
 {
+	struct section *sec = reloc->sym->sec;
+
 	if (reloc_type(reloc) == R_NONE)
 		return 1;
 
-	if (is_reloc_allowed(reloc))
-		return 0;
-
-	if (section_reference_needed(reloc->sym->sec))
+	if (is_uncorrelated_section(sec))
 		return convert_reloc_sym_to_secsym(elf, reloc);
-	else
-		return convert_reloc_secsym_to_sym(elf, reloc);
+
+	/* Everything else: references should use named symbols. */
+	return convert_reloc_secsym_to_sym(elf, reloc);
 }
 
 /*
@@ -1045,13 +1032,6 @@ static int clone_reloc(struct elfs *e, struct reloc *patched_reloc,
 	struct symbol *out_sym;
 	bool klp;
 
-	if (!is_reloc_allowed(patched_reloc)) {
-		ERROR_FUNC(patched_reloc->sec->base, reloc_offset(patched_reloc),
-			   "missing symbol for reference to %s+%ld",
-			   patched_sym->name, addend);
-		return -1;
-	}
-
 	klp = klp_reloc_needed(patched_reloc);
 
 	dbg_clone_reloc(sec, offset, patched_sym, addend, export, klp);
@@ -1081,7 +1061,7 @@ static int clone_reloc(struct elfs *e, struct reloc *patched_reloc,
 
 	/*
 	 * For strings, all references use section symbols, thanks to
-	 * section_reference_needed().  clone_symbol() has cloned an empty
+	 * convert_reloc_sym().  clone_symbol() has cloned an empty
 	 * version of the string section.  Now copy the string itself.
 	 */
 	if (is_string_sec(patched_sym->sec)) {
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 12/18] ms/objtool: Move mark_rodata() to elf.c
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (10 preceding siblings ...)
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 11/18] ms/objtool/klp: Simplify reloc symbol conversion Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 13/18] ms/objtool/klp: Fix pointer comparisons for rodata objects Eva Kurchatova
                   ` (6 subsequent siblings)
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Josh Poimboeuf <jpoimboe@kernel.org>

Move the sec->rodata marking from check.c to elf.c so it's set during
ELF reading rather than during the check pipeline.  This makes the
rodata flag available to all objtool users, including klp-diff which
reads ELF files directly without running check().

Add an is_rodata_sec() helper to elf.h for consistency with
is_text_sec() and is_string_sec().

Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Song Liu <song@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 3e01ab44af207a2d8c197653e7278083b039a274)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
---
 tools/objtool/check.c               | 11 +++--------
 tools/objtool/elf.c                 | 13 +++++++++++++
 tools/objtool/include/objtool/elf.h |  5 +++++
 3 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index dc24ceed3c7d..f9539dae294f 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2524,7 +2524,6 @@ static int classify_symbols(struct objtool_file *file)
 static void mark_rodata(struct objtool_file *file)
 {
 	struct section *sec;
-	bool found = false;
 
 	/*
 	 * Search for the following rodata sections, each of which can
@@ -2537,15 +2536,11 @@ static void mark_rodata(struct objtool_file *file)
 	 * .rodata.str1.* sections are ignored; they don't contain jump tables.
 	 */
 	for_each_sec(file->elf, sec) {
-		if ((!strncmp(sec->name, ".rodata", 7) &&
-		     !strstr(sec->name, ".str1.")) ||
-		    !strncmp(sec->name, ".data.rel.ro", 12)) {
-			sec->rodata = true;
-			found = true;
+		if (is_rodata_sec(sec)) {
+			file->rodata = true;
+			return;
 		}
 	}
-
-	file->rodata = found;
 }
 
 static void mark_holes(struct objtool_file *file)
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index da20cec49374..a1cbaba7e1c1 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -1096,6 +1096,17 @@ static int read_relocs(struct elf *elf)
 	return 0;
 }
 
+static void mark_rodata(struct elf *elf)
+{
+	struct section *sec;
+
+	for_each_sec(elf, sec) {
+		if ((strstarts(sec->name, ".rodata") && !strstr(sec->name, ".str1.")) ||
+		    strstarts(sec->name, ".data.rel.ro"))
+			sec->rodata = true;
+	}
+}
+
 struct elf *elf_open_read(const char *name, int flags)
 {
 	struct elf *elf;
@@ -1146,6 +1157,8 @@ struct elf *elf_open_read(const char *name, int flags)
 	if (read_sections(elf))
 		goto err;
 
+	mark_rodata(elf);
+
 	if (read_symbols(elf))
 		goto err;
 
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index a2b3fb97aed9..fd2afbbee903 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -292,6 +292,11 @@ static inline bool is_text_sec(struct section *sec)
 	return sec->sh.sh_flags & SHF_EXECINSTR;
 }
 
+static inline bool is_rodata_sec(struct section *sec)
+{
+	return sec->rodata;
+}
+
 static inline bool sec_changed(struct section *sec)
 {
 	return sec->_changed;
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 13/18] ms/objtool/klp: Fix pointer comparisons for rodata objects
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (11 preceding siblings ...)
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 12/18] ms/objtool: Move mark_rodata() to elf.c Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25 10:35   ` Konstantin Khorenko
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 14/18] objtool/klp: Reject changed .rodata objects Eva Kurchatova
                   ` (5 subsequent siblings)
  18 siblings, 1 reply; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Josh Poimboeuf <jpoimboe@kernel.org>

klp-diff treats all rodata as uncorrelated, so any reference to it uses
a duplicated copy rather than using a KLP reloc.

For the contents of the data itself, a duplicated copy is fine.
However, pointer comparisons (e.g., f->f_op == &foo_ops) are broken.

Fix it by correlating non-anonymous rodata objects.

Also, use a new find_symbol_containing_inclusive() helper for matching
the end of a symbol so bounds calculations don't get broken, for the
case where an array or other symbol's ending address is used as part of
a bounds calculation.

While these are really two distinct changes, they need to be done in the
same patch so as to avoid introducing bisection regressions.

Acked-by: Song Liu <song@kernel.org>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit d5b0f025281f287759820f57b7ba1bac7827b338)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 tools/objtool/elf.c                 | 14 ++++++++++++++
 tools/objtool/include/objtool/elf.h |  1 +
 tools/objtool/klp-diff.c            | 15 +++++++++++++--
 3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index a1cbaba7e1c1..9f1ebb643686 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -223,6 +223,20 @@ struct symbol *find_symbol_containing(const struct section *sec, unsigned long o
 	return sym;
 }
 
+/*
+ * Also match the symbol end address which can be used for a bounds comparison.
+ */
+struct symbol *find_symbol_containing_inclusive(const struct section *sec,
+						unsigned long offset)
+{
+	struct symbol *sym = find_symbol_containing(sec, offset);
+
+	if (!sym && offset)
+		sym = find_symbol_containing(sec, offset - 1);
+
+	return sym;
+}
+
 /*
  * Returns size of hole starting at @offset.
  */
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index fd2afbbee903..bc1f604fc67a 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -186,6 +186,7 @@ struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name);
 struct symbol *find_global_symbol_by_name(const struct elf *elf, const char *name);
 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset);
+struct symbol *find_symbol_containing_inclusive(const struct section *sec, unsigned long offset);
 int find_symbol_hole_containing(const struct section *sec, unsigned long offset);
 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset);
 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 55208c6edfc6..2f1cedf9632b 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -352,6 +352,7 @@ static bool dont_correlate(struct symbol *sym)
 	       is_uncorrelated_static_local(sym) ||
 	       is_clang_tmp_label(sym) ||
 	       is_string_sec(sym->sec) ||
+	       (is_rodata_sec(sym->sec) && !is_object_sym(sym)) ||
 	       is_special_section(sym->sec) ||
 	       is_special_section_aux(sym->sec) ||
 	       strstarts(sym->name, "__initcall__");
@@ -837,7 +838,7 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
 		goto found_sym;
 
 	/* No dedicated section; find the symbol manually */
-	sym = find_symbol_containing(sec, arch_adjusted_addend(reloc));
+	sym = find_symbol_containing_inclusive(sec, arch_adjusted_addend(reloc));
 	if (!sym) {
 		/*
 		 * This is presumably an .altinstr_replacement section which is
@@ -846,6 +847,17 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
 		if (!sec_size(sec))
 			return 1;
 
+		/*
+		 * .rodata is a mixed bag of named objects and anonymous data.
+		 *
+		 * Convert section symbol references to named object symbols
+		 * when possible, to preserve pointer identity for const
+		 * structs like file_operations.  Otherwise a section symbol is
+		 * fine.
+		 */
+		if (is_rodata_sec(sec))
+			return 0;
+
 		/*
 		 * This can happen for special section references to weak code
 		 * whose symbol has been stripped by the linker.
@@ -867,7 +879,6 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
 static bool is_uncorrelated_section(struct section *sec)
 {
 	return is_string_sec(sec) ||
-	       strstarts(sec->name, ".rodata") ||
 	       strstarts(sec->name, ".data..Lubsan") ||		/* GCC */
 	       strstarts(sec->name, ".data..L__unnamed_");	/* Clang */
 }
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 14/18] objtool/klp: Reject changed .rodata objects
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (12 preceding siblings ...)
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 13/18] ms/objtool/klp: Fix pointer comparisons for rodata objects Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25 10:35   ` Konstantin Khorenko
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 15/18] ms/objtool/klp: Fix module name normalization for paths with dots Eva Kurchatova
                   ` (4 subsequent siblings)
  18 siblings, 1 reply; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

Since "objtool/klp: Fix pointer comparisons for rodata objects", a named
.rodata object is correlated, and the patched code references the
original through a klp reloc. A change to the object, such as a new row
of a static const table, is then lost without notice; so is a reference
to another function's same-named local table, which correlation by name
in order binds when the patch adds one earlier in the file.

Fail klp diff when a correlated .rodata object differs in length, bytes
or relocations, as mainline does since 6016dd33a10a ("objtool/klp:
Rewrite symbol correlation algorithm"), which is too large to take here.
Relocations are compared by target: strings by contents, correlated
symbols by twin and offset, uncorrelated ones such as __func__ by name
and bytes, anything else by section and offset.

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 tools/objtool/klp-diff.c | 116 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 116 insertions(+)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 2f1cedf9632b..177ec61d7d19 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -443,6 +443,119 @@ static int correlate_symbols(struct elfs *e)
 	return 0;
 }
 
+/* The symbol a reloc points into and the offset in it, NULL if anonymous */
+static struct symbol *reloc_target(struct reloc *reloc, s64 *offset)
+{
+	struct symbol *sym = reloc->sym;
+
+	*offset = arch_adjusted_addend(reloc);
+	if (!is_sec_sym(sym))
+		return sym;
+
+	sym = find_symbol_containing_inclusive(sym->sec, *offset);
+	if (sym)
+		*offset -= sym->offset;
+	return sym;
+}
+
+static const void *sym_data(struct symbol *sym)
+{
+	if (!sym->sec->data || !sym->sec->data->d_buf)
+		return NULL;
+
+	return sym->sec->data->d_buf + sym->offset;
+}
+
+static const char *reloc_string(struct reloc *reloc)
+{
+	struct symbol *sym = reloc->sym;
+
+	return sym->sec->data->d_buf + sym->offset + arch_adjusted_addend(reloc);
+}
+
+static bool reloc_changed(struct reloc *orig, struct reloc *patched)
+{
+	struct symbol *orig_sym, *patched_sym;
+	s64 orig_off, patched_off;
+
+	if (reloc_type(orig) != reloc_type(patched))
+		return true;
+
+	/* Strings move inside their merged section: compare the contents */
+	if (is_string_sec(orig->sym->sec) || is_string_sec(patched->sym->sec))
+		return !is_string_sec(orig->sym->sec) ||
+		       !is_string_sec(patched->sym->sec) ||
+		       strcmp(reloc_string(orig), reloc_string(patched));
+
+	orig_sym = reloc_target(orig, &orig_off);
+	patched_sym = reloc_target(patched, &patched_off);
+
+	if (!orig_sym || !patched_sym)
+		return orig_sym != patched_sym ||
+		       strcmp(orig->sym->sec->name, patched->sym->sec->name) ||
+		       arch_adjusted_addend(orig) != arch_adjusted_addend(patched);
+
+	if (orig_off != patched_off)
+		return true;
+
+	if (orig_sym->twin || patched_sym->twin)
+		return orig_sym->twin != patched_sym;
+
+	/* Uncorrelated, e.g. __func__: the original stays referenced too */
+	return strcmp(orig_sym->demangled_name, patched_sym->demangled_name) ||
+	       orig_sym->len != patched_sym->len ||
+	       (sym_data(orig_sym) && sym_data(patched_sym) &&
+		memcmp(sym_data(orig_sym), sym_data(patched_sym), orig_sym->len));
+}
+
+static bool object_changed(struct elfs *e, struct symbol *orig, struct symbol *patched)
+{
+	struct reloc *orig_reloc, *patched_reloc;
+
+	/* A twin outside .rodata (no longer const) loses the change too */
+	if (!is_rodata_sec(patched->sec) || orig->len != patched->len ||
+	    (orig->len && memcmp(sym_data(orig), sym_data(patched), orig->len)))
+		return true;
+
+	patched_reloc = find_reloc_by_dest_range(e->patched, patched->sec,
+						 patched->offset, patched->len);
+	sym_for_each_reloc(e->orig, orig, orig_reloc) {
+		if (!patched_reloc ||
+		    reloc_offset(patched_reloc) >= patched->offset + patched->len ||
+		    reloc_offset(orig_reloc) - orig->offset !=
+		    reloc_offset(patched_reloc) - patched->offset ||
+		    reloc_changed(orig_reloc, patched_reloc))
+			return true;
+
+		patched_reloc = rsec_next_reloc(patched->sec->rsec, patched_reloc);
+	}
+
+	return patched_reloc &&
+	       reloc_offset(patched_reloc) < patched->offset + patched->len;
+}
+
+/*
+ * The patched code references a correlated .rodata object through a klp
+ * reloc to the original, so a change to the object would be lost.
+ */
+static int check_changed_rodata(struct elfs *e)
+{
+	struct symbol *orig;
+	int ret = 0;
+
+	for_each_sym(e->orig, orig) {
+		if (!is_object_sym(orig) || !is_rodata_sec(orig->sec) || !orig->twin)
+			continue;
+
+		if (object_changed(e, orig, orig->twin)) {
+			ERROR("changed data: %s", orig->name);
+			ret = -1;
+		}
+	}
+
+	return ret;
+}
+
 /* "sympos" is used by livepatch to disambiguate duplicate symbol names */
 static unsigned long find_sympos(struct elf *elf, struct symbol *sym)
 {
@@ -1714,6 +1827,9 @@ int cmd_klp_diff(int argc, const char **argv)
 	if (correlate_symbols(&e))
 		return -1;
 
+	if (check_changed_rodata(&e))
+		return -1;
+
 	if (mark_changed_functions(&e))
 		return 0;
 
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 15/18] ms/objtool/klp: Fix module name normalization for paths with dots
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (13 preceding siblings ...)
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 14/18] objtool/klp: Reject changed .rodata objects Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 16/18] ms/objtool/klp: Normalize Module.symvers paths to module names Eva Kurchatova
                   ` (3 subsequent siblings)
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Josh Poimboeuf <jpoimboe@kernel.org>

When .modinfo has no "name=" tag, __find_modname() falls back to
converting the object's build-tree path to a runtime module name by
stripping directory components, converting '-' to '_' and truncating the
file extension.

It does all that in a single pass over the entire path, so the first dot
anywhere in the path ends the name.  For an object built in a directory
whose name contains a dot, e.g. "drivers/foo-1.0/bar.o", the result is a
bogus module name.

Strip the directory components up front so only the basename is scanned
for the extension separator.

Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: live-patching@vger.kernel.org
Link: https://patch.msgid.link/9017b4609553bed16674e8f924d34691cbc2b2c1.1785727106.git.jpoimboe@kernel.org
(cherry picked from commit 165affd6f095323d953b0ed5823ec4d0db3b7d0d)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 tools/objtool/klp-diff.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 177ec61d7d19..dae9b5249ed2 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -825,7 +825,7 @@ static struct export *find_export(struct symbol *sym)
 static const char *__find_modname(struct elfs *e)
 {
 	struct section *sec;
-	char *name;
+	char *name, *slash;
 
 	sec = find_section_by_name(e->orig, ".modinfo");
 	if (!sec) {
@@ -843,10 +843,12 @@ static const char *__find_modname(struct elfs *e)
 		return NULL;
 	}
 
+	slash = strrchr(name, '/');
+	if (slash)
+		name = slash + 1;
+
 	for (char *c = name; *c; c++) {
-		if (*c == '/')
-			name = c + 1;
-		else if (*c == '-')
+		if (*c == '-')
 			*c = '_';
 		else if (*c == '.') {
 			*c = '\0';
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 16/18] ms/objtool/klp: Normalize Module.symvers paths to module names
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (14 preceding siblings ...)
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 15/18] ms/objtool/klp: Fix module name normalization for paths with dots Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 17/18] ms/objtool/klp: Fix cross-module klp relocation section naming Eva Kurchatova
                   ` (2 subsequent siblings)
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Joe Lawrence <joe.lawrence@redhat.com>

Module.symvers contains build-tree object paths as module identifiers
(e.g., "arch/x86/kvm/kvm") rather than runtime module names ("kvm").
Objtool's clone_reloc_klp() uses this field directly for exported
symbols, while unexported symbols correctly go through __find_modname().

This means that exported symbol relocations may land in a .klp.rela
section named with the build path rather than the module name.  That is
a crash waiting to happen: the kernel's livepatch loader silently skips
this relocation because it doesn't match the expected klp_object name.
The unresolved relocation sits in the newly activated code, crashing
when executed.

Normalize export->mod at Module.symvers read time using the same logic
as __find_modname() (refactored into a shared normalize_modname()
helper).

Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Reported-by: Ben Procknow <bprockno@redhat.com>
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Cc: live-patching@vger.kernel.org
Link: https://patch.msgid.link/dbe1b72931bd3c31b751fd0729613d9f2226fff6.1785727106.git.jpoimboe@kernel.org
(cherry picked from commit 8668bf91e0508abeb8e98b4edd89032be02228a1)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 tools/objtool/klp-diff.c | 49 ++++++++++++++++++++++++++++------------
 1 file changed, 34 insertions(+), 15 deletions(-)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index dae9b5249ed2..bc33683e2bd5 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -83,6 +83,35 @@ static char *escape_str(const char *orig)
 	return new;
 }
 
+/*
+ * Convert a build-tree object path to a runtime module name: strip
+ * directory components, replace '-' with '_', and remove file
+ * extensions.  Examples:
+ *
+ *   "arch/x86/kvm/kvm" -> "kvm"
+ *   "arch/x86/kvm/kvm-intel" -> "kvm_intel".
+ *
+ * Used by read_exports() to normalize Module.symvers entries and by
+ * __find_modname() as a fallback when .modinfo lacks a "name=" tag.
+ */
+static char *normalize_modname(char *name)
+{
+	char *slash = strrchr(name, '/');
+
+	if (slash)
+		name = slash + 1;
+
+	for (char *c = name; *c; c++) {
+		if (*c == '-')
+			*c = '_';
+		else if (*c == '.') {
+			*c = '\0';
+			break;
+		}
+	}
+	return name;
+}
+
 static int read_exports(void)
 {
 	const char *symvers = "Module.symvers";
@@ -150,6 +179,9 @@ static int read_exports(void)
 			return -1;
 		}
 
+		if (strcmp(export->mod, "vmlinux"))
+			export->mod = normalize_modname(export->mod);
+
 		export->sym = strdup(sym);
 		if (!export->sym) {
 			ERROR_GLIBC("strdup");
@@ -825,7 +857,7 @@ static struct export *find_export(struct symbol *sym)
 static const char *__find_modname(struct elfs *e)
 {
 	struct section *sec;
-	char *name, *slash;
+	char *name;
 
 	sec = find_section_by_name(e->orig, ".modinfo");
 	if (!sec) {
@@ -843,20 +875,7 @@ static const char *__find_modname(struct elfs *e)
 		return NULL;
 	}
 
-	slash = strrchr(name, '/');
-	if (slash)
-		name = slash + 1;
-
-	for (char *c = name; *c; c++) {
-		if (*c == '-')
-			*c = '_';
-		else if (*c == '.') {
-			*c = '\0';
-			break;
-		}
-	}
-
-	return name;
+	return normalize_modname(name);
 }
 
 /* Get the object's module name as defined by the kernel (and klp_object) */
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 17/18] ms/objtool/klp: Fix cross-module klp relocation section naming
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (15 preceding siblings ...)
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 16/18] ms/objtool/klp: Normalize Module.symvers paths to module names Eva Kurchatova
@ 2026-11-25  6:03 ` Eva Kurchatova
  2026-11-25  6:04 ` [Devel] [PATCH hci-8.2 v2 18/18] ms/objtool/klp: Don't match local symbols against exports Eva Kurchatova
  2026-11-25 10:35 ` [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Konstantin Khorenko
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:03 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Josh Poimboeuf <jpoimboe@kernel.org>

A klp relocation section is .klp.rela.<objname>.<secname>, where objname
is the object being patched.

klp-build wrongly derives objname from where the referenced symbol
lives, not where it's referenced.  For a cross-module reference like
patched can_isotp code calling can.ko's can_rx_unregister(), that gives
.klp.rela.can..text rather than .klp.rela.can_isotp..text.  Unless the
patch happens to patch can.ko as well, the relocation never gets applied
and the call goes off into the weeds.

Name the intermediate section __klp_relocs.<objname> so post-link can
read the patched object's name from there.

Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Reported-by: Joe Lawrence <joe.lawrence@redhat.com>
Link: https://lore.kernel.org/20260720145658.1103243-2-joe.lawrence@redhat.com
Acked-by: Song Liu <song@kernel.org>
Acked-by: Joe Lawrence <joe.lawrence@redhat.com>
Link: https://patch.msgid.link/ee93a08f3e55e76ffa67d04e283917ddaa893f09.1786138493.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 07f14d6af9d7791fdc2b44a3dd0693e42c71d2b8)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 tools/objtool/include/objtool/klp.h | 10 ++++--
 tools/objtool/klp-diff.c            | 17 +++++++--
 tools/objtool/klp-post-link.c       | 53 +++++++++++++++++------------
 3 files changed, 52 insertions(+), 28 deletions(-)

diff --git a/tools/objtool/include/objtool/klp.h b/tools/objtool/include/objtool/klp.h
index ad830a7ce55b..d9cb594f4924 100644
--- a/tools/objtool/include/objtool/klp.h
+++ b/tools/objtool/include/objtool/klp.h
@@ -14,11 +14,15 @@
 #define KLP_FUNCS_SEC	"__klp_funcs"
 
 /*
- * __klp_relocs is an intermediate section which are created by klp diff and
- * converted into KLP symbols/relas by "objtool klp post-link".  This is needed
- * to work around the linker, which doesn't preserve SHN_LIVEPATCH or
+ * __klp_relocs.<objname> are intermediate sections which are created by klp
+ * diff and converted into KLP symbols/relas by "objtool klp post-link".  This
+ * is needed to work around the linker, which doesn't preserve SHN_LIVEPATCH or
  * SHF_RELA_LIVEPATCH, nor does it support having two RELA sections for a
  * single PROGBITS section.
+ *
+ * "objname" is the name of the object being patched ("vmlinux" or a module
+ * name).  post-link uses it to name the resulting
+ * .klp.rela.objname.section_name sections.
  */
 #define KLP_RELOCS_SEC	"__klp_relocs"
 #define KLP_STRINGS_SEC	".rodata.klp.str1.1"
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index bc33683e2bd5..4712a616dc39 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1116,8 +1116,8 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,
 	}
 
 	/*
-	 * Create the __klp_relocs entry.  This will be converted to an actual
-	 * KLP rela by "objtool klp post-link".
+	 * Create the __klp_relocs.<objname> entry.  This will be converted to
+	 * an actual KLP rela by "objtool klp post-link".
 	 *
 	 * This intermediate step is necessary to prevent corruption by the
 	 * linker, which doesn't know how to properly handle two rela sections
@@ -1125,7 +1125,18 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,
 	 */
 
 	if (!klp_relocs) {
-		klp_relocs = elf_create_section(e->out, KLP_RELOCS_SEC, 0,
+		const char *objname = find_modname(e);
+		char sec_name[SEC_NAME_LEN];
+
+		if (!objname)
+			return -1;
+
+		/* section format: __klp_relocs.objname */
+		if (snprintf_check(sec_name, SEC_NAME_LEN,
+				   KLP_RELOCS_SEC ".%s", objname))
+			return -1;
+
+		klp_relocs = elf_create_section(e->out, sec_name, 0,
 						0, SHT_PROGBITS, 8, SHF_ALLOC);
 		if (!klp_relocs)
 			return -1;
diff --git a/tools/objtool/klp-post-link.c b/tools/objtool/klp-post-link.c
index c013e39957b1..350d20495897 100644
--- a/tools/objtool/klp-post-link.c
+++ b/tools/objtool/klp-post-link.c
@@ -19,19 +19,11 @@
 #include <objtool/util.h>
 #include <linux/livepatch_external.h>
 
-static int fix_klp_relocs(struct elf *elf)
+static int fix_klp_reloc_sec(struct elf *elf, struct section *symtab,
+			     struct section *klp_relocs)
 {
-	struct section *symtab, *klp_relocs;
-
-	klp_relocs = find_section_by_name(elf, KLP_RELOCS_SEC);
-	if (!klp_relocs)
-		return 0;
-
-	symtab = find_section_by_name(elf, ".symtab");
-	if (!symtab) {
-		ERROR("missing .symtab");
-		return -1;
-	}
+	/* section format: __klp_relocs.sec_objname */
+	const char *sec_objname = klp_relocs->name + strlen(KLP_RELOCS_SEC ".");
 
 	for (int i = 0; i < sec_size(klp_relocs) / sizeof(struct klp_reloc); i++) {
 		struct klp_reloc *klp_reloc;
@@ -39,7 +31,6 @@ static int fix_klp_relocs(struct elf *elf)
 		struct section *sec, *tmp, *klp_rsec;
 		unsigned long offset;
 		struct reloc *reloc;
-		char sym_modname[64];
 		char rsec_name[SEC_NAME_LEN];
 		u64 addend;
 		struct symbol *sym, *klp_sym;
@@ -55,7 +46,7 @@ static int fix_klp_relocs(struct elf *elf)
 		reloc = find_reloc_by_dest(elf, klp_relocs,
 					   klp_reloc_off + offsetof(struct klp_reloc, offset));
 		if (!reloc) {
-			ERROR("malformed " KLP_RELOCS_SEC " section");
+			ERROR("malformed %s section", klp_relocs->name);
 			return -1;
 		}
 
@@ -66,17 +57,13 @@ static int fix_klp_relocs(struct elf *elf)
 		reloc = find_reloc_by_dest(elf, klp_relocs,
 					   klp_reloc_off + offsetof(struct klp_reloc, sym));
 		if (!reloc) {
-			ERROR("malformed " KLP_RELOCS_SEC " section");
+			ERROR("malformed %s section", klp_relocs->name);
 			return -1;
 		}
 
 		klp_sym = reloc->sym;
 		addend = reloc_addend(reloc);
 
-		/* symbol format: .klp.sym.modname.sym_name,sympos */
-		if (sscanf(klp_sym->name + strlen(KLP_SYM_PREFIX), "%55[^.]", sym_modname) != 1)
-			ERROR("can't find modname in klp symbol '%s'", klp_sym->name);
-
 		/*
 		 * Create the KLP rela:
 		 */
@@ -84,7 +71,7 @@ static int fix_klp_relocs(struct elf *elf)
 		/* section format: .klp.rela.sec_objname.section_name */
 		if (snprintf_check(rsec_name, SEC_NAME_LEN,
 				   KLP_RELOC_SEC_PREFIX "%s.%s",
-				   sym_modname, sec->name))
+				   sec_objname, sec->name))
 			return -1;
 
 		klp_rsec = find_section_by_name(elf, rsec_name);
@@ -134,10 +121,32 @@ static int fix_klp_relocs(struct elf *elf)
 	return 0;
 }
 
+static int fix_klp_relocs(struct elf *elf)
+{
+	struct section *symtab, *sec;
+
+	symtab = find_section_by_name(elf, ".symtab");
+	if (!symtab) {
+		ERROR("missing .symtab");
+		return -1;
+	}
+
+	for_each_sec(elf, sec) {
+		if (strncmp(sec->name, KLP_RELOCS_SEC ".",
+			    strlen(KLP_RELOCS_SEC ".")))
+			continue;
+
+		if (fix_klp_reloc_sec(elf, symtab, sec))
+			return -1;
+	}
+
+	return 0;
+}
+
 /*
  * This runs on the livepatch module after all other linking has been done.  It
- * converts the intermediate __klp_relocs section into proper KLP relocs to be
- * processed by livepatch.  This needs to run last to avoid linker wreckage.
+ * converts the intermediate __klp_relocs.* sections into proper KLP relocs to
+ * be processed by livepatch.  This needs to run last to avoid linker wreckage.
  * Linkers don't tend to handle the "two rela sections for a single base
  * section" case very well, nor do they appreciate SHN_LIVEPATCH.
  */
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* [Devel] [PATCH hci-8.2 v2 18/18] ms/objtool/klp: Don't match local symbols against exports
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (16 preceding siblings ...)
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 17/18] ms/objtool/klp: Fix cross-module klp relocation section naming Eva Kurchatova
@ 2026-11-25  6:04 ` Eva Kurchatova
  2026-11-25 10:35 ` [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Konstantin Khorenko
  18 siblings, 0 replies; 22+ messages in thread
From: Eva Kurchatova @ 2026-11-25  6:04 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Josh Poimboeuf <jpoimboe@kernel.org>

While cloning a reloc, klp diff calls find_export() to determine whether
the referenced symbol is exported.  That decides whether the reference
needs a klp reloc, which object the klp symbol belongs to, and whether
the symbol's data needs to be copied into the patch module.

But find_export() matches purely on symbol name, so a static function or
variable which happens to share its name with an export is mistaken for
a reference to that export:

  - klp_reloc_needed() creates a klp reloc pointing at the exporting
    module's symbol rather than the local one.  For a vmlinux export it
    skips the klp reloc altogether, leaving a normal reloc which the
    module loader resolves to the vmlinux symbol.

  - clone_reloc() treats the symbol as external and clones it without
    its data, leaving a dangling reference.

  - validate_special_section_klp_reloc() attributes a static branch or
    call key to the wrong module, and for a vmlinux export skips the
    unsupported-key check entirely.

Exports are always global, so ignore local symbols in find_export().

Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Acked-by: Song Liu <song@kernel.org>
Acked-by: Joe Lawrence <joe.lawrence@redhat.com>
Link: https://patch.msgid.link/dafc7c017d1f62e2190a20dc84a52fbfc4c70210.1786138493.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
(cherry picked from commit 86a697572c6271083694025eefa8de518eeef623)

https://virtuozzo.atlassian.net/browse/VSTOR-128510
Feature: klp-build support

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 tools/objtool/klp-diff.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 4712a616dc39..3e5fc7f73a92 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -846,6 +846,9 @@ static struct export *find_export(struct symbol *sym)
 {
 	struct export *export;
 
+	if (is_local_sym(sym))
+		return NULL;
+
 	hash_for_each_possible(exports, export, hash, str_hash(sym->name)) {
 		if (!strcmp(export->sym, sym->name))
 			return export;
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* Re: [Devel] [PATCH hci-8.2 v2 13/18] ms/objtool/klp: Fix pointer comparisons for rodata objects
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 13/18] ms/objtool/klp: Fix pointer comparisons for rodata objects Eva Kurchatova
@ 2026-11-25 10:35   ` Konstantin Khorenko
  0 siblings, 0 replies; 22+ messages in thread
From: Konstantin Khorenko @ 2026-11-25 10:35 UTC (permalink / raw)
  To: Eva Kurchatova; +Cc: devel

I won't apply this version, please send v3 with the changes below.

- A reference to a size-less global label in .rodata must keep using
  the section symbol, or fail klp diff: item 1 of the cover letter
  reply.

On Wed, 25 Nov 2026 08:03:55 +0200, Eva Kurchatova wrote:
> -	       strstarts(sec->name, ".rodata") ||
>  	       strstarts(sec->name, ".data..Lubsan") ||		/* GCC */

Some asm puts global labels without a size into .rodata:
uretprobe_trampoline_entry/_end, optprobe_template_*,
skl_call_thunk_template/_tail, kernel_config_data/_end. After this patch
a changed function's reference to such a label keeps the label symbol;
dont_correlate() is true for it, so there is no klp reloc, and the
label is cloned with data_too but length 0, so no bytes are copied. A
start/end pair then lands on one address at the end of the module's
.rodata, and a livepatch of arch_uprobe_trampoline() builds and runs on
garbage. Before this patch the whole section was cloned. Patch 14 does
not catch it: it checks STT_OBJECT only.

Converting such references to the section symbol, as before, would do:

	if (is_uncorrelated_section(sec) ||
	    (is_rodata_sec(sec) && !is_sec_sym(reloc->sym) &&
	     !is_object_sym(reloc->sym)))
		return convert_reloc_sym_to_secsym(elf, reloc);

-- 
Konstantin Khorenko,
Virtuozzo Linux Kernel Team

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* Re: [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build
  2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
                   ` (17 preceding siblings ...)
  2026-11-25  6:04 ` [Devel] [PATCH hci-8.2 v2 18/18] ms/objtool/klp: Don't match local symbols against exports Eva Kurchatova
@ 2026-11-25 10:35 ` Konstantin Khorenko
  18 siblings, 0 replies; 22+ messages in thread
From: Konstantin Khorenko @ 2026-11-25 10:35 UTC (permalink / raw)
  To: Eva Kurchatova; +Cc: devel

I won't apply this version, please send v3 with the changes below.

1. Patch 13: a reference to a size-less asm label in .rodata must keep
   the section symbol (or fail klp diff); a vz fix after 13 or folded
   into 14. Details in the reply to 13.
2. Patch 14: also check a pair whose patched side is in .rodata (an
   object made const and changed in the same patch).
3. Patch 14: do not compare the addend of a target that no symbol
   contains; it gives false "changed data" errors. Details in the reply
   to 14.
4. Patch 14: lowercase subject, "objtool/klp: reject changed .rodata
   objects".
5. Cover letter: renumber after the new patch 14.

Patch 14 does what the v1 review asked: a changed correlated const
object now fails klp diff. Patches 1-13 and 15-18 match mainline.

On Wed, 25 Nov 2026 08:03:42 +0200, Eva Kurchatova wrote:
> - klp relocations to exports of another module put in a section the
>   livepatch loader never applies, e.g. jump into garbage (14-16)
> - static symbols bound to an export of the same name (17)

Item 5: these are 15-17 and 18 now.

Separate patches, not needed for this series. I created a task for
it:

1. Mainline has the same bug as item 1 since its "objtool/klp: Fix
   pointer comparisons for rodata objects"; the fix should go there
   too.
   https://virtuozzo.atlassian.net/browse/VSTOR-157091

-- 
Konstantin Khorenko,
Virtuozzo Linux Kernel Team

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* Re: [Devel] [PATCH hci-8.2 v2 14/18] objtool/klp: Reject changed .rodata objects
  2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 14/18] objtool/klp: Reject changed .rodata objects Eva Kurchatova
@ 2026-11-25 10:35   ` Konstantin Khorenko
  0 siblings, 0 replies; 22+ messages in thread
From: Konstantin Khorenko @ 2026-11-25 10:35 UTC (permalink / raw)
  To: Eva Kurchatova; +Cc: devel

I won't apply this version, please send v3 with the changes below.

- Also check a pair whose patched side is in .rodata: item 2.
- Do not compare the addend of an anonymous target: item 3.
- Lowercase subject: item 4 of the cover letter reply.

On Wed, 25 Nov 2026 08:03:56 +0200, Eva Kurchatova wrote:
> Subject: [PATCH hci-8.2 v2 14/18] objtool/klp: Reject changed .rodata objects

Item 4: a vz commit, so "objtool/klp: reject changed .rodata objects".

> +	for_each_sym(e->orig, orig) {
> +		if (!is_object_sym(orig) || !is_rodata_sec(orig->sec) || !orig->twin)
> +			continue;

Item 2: a fix that turns a writable table into a const one with new
contents correlates the two by name; the patched code gets a klp reloc
to the old writable object, and this loop skips the pair because the
original is not in .rodata. Before patch 13 the new contents were
cloned. Check the pair when either side is in .rodata; for a .bss
original sym_data() is NULL, so the memcmp() in object_changed() needs
the same guard as in reloc_changed().

> +	if (!orig_sym || !patched_sym)
> +		return orig_sym != patched_sym ||
> +		       strcmp(orig->sym->sec->name, patched->sym->sec->name) ||
> +		       arch_adjusted_addend(orig) != arch_adjusted_addend(patched);

Item 3: klp diff runs on the whole vmlinux.o, an ld -r link in which
the plain .rodata of all files is one section, and anonymous constants
(strings with an embedded NUL, constant pool data) stay there even with
-fdata-sections. A patch that adds or resizes one such constant in
kernel/sys.c moves every later anonymous constant, and the crypto
testmgr vector tables, which point at hundreds of them, then fail with
"changed data" although they did not change. Mainline skips such
targets in its checksum; compare the section name only, and fix the
last sentence of the message to match.

-- 
Konstantin Khorenko,
Virtuozzo Linux Kernel Team

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

end of thread, other threads:[~2026-11-25 10:36 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25  6:03 [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 01/18] ms/objtool/klp: Fix detection of corrupt static branch/call entries Eva Kurchatova
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 02/18] ms/objtool/klp: Disable unsupported pr_debug() usage Eva Kurchatova
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 03/18] ms/objtool: Fix data alignment in elf_add_data() Eva Kurchatova
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 04/18] ms/objtool/klp: fix data alignment in __clone_symbol() Eva Kurchatova
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 05/18] ms/objtool/klp: Fix .data..once static local non-correlation Eva Kurchatova
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 06/18] ms/objtool/klp: Fix create_fake_symbols() skipping entsize-based sections Eva Kurchatova
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 07/18] ms/objtool/klp: Fix handling of zero-length .altinstr_replacement sections Eva Kurchatova
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 08/18] ms/objtool/klp: Fix cloning of zero-length section symbols Eva Kurchatova
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 09/18] ms/objtool/klp: Fix relocation conversion failures for R_X86_64_NONE Eva Kurchatova
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 10/18] ms/objtool/klp: Fix reloc corruption in convert_reloc_sym_to_secsym() Eva Kurchatova
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 11/18] ms/objtool/klp: Simplify reloc symbol conversion Eva Kurchatova
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 12/18] ms/objtool: Move mark_rodata() to elf.c Eva Kurchatova
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 13/18] ms/objtool/klp: Fix pointer comparisons for rodata objects Eva Kurchatova
2026-11-25 10:35   ` Konstantin Khorenko
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 14/18] objtool/klp: Reject changed .rodata objects Eva Kurchatova
2026-11-25 10:35   ` Konstantin Khorenko
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 15/18] ms/objtool/klp: Fix module name normalization for paths with dots Eva Kurchatova
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 16/18] ms/objtool/klp: Normalize Module.symvers paths to module names Eva Kurchatova
2026-11-25  6:03 ` [Devel] [PATCH hci-8.2 v2 17/18] ms/objtool/klp: Fix cross-module klp relocation section naming Eva Kurchatova
2026-11-25  6:04 ` [Devel] [PATCH hci-8.2 v2 18/18] ms/objtool/klp: Don't match local symbols against exports Eva Kurchatova
2026-11-25 10:35 ` [Devel] [PATCH hci-8.2 v2 00/18] ms/objtool: fix silently wrong livepatches by klp-build Konstantin Khorenko

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