All Virtuozzo development lists (kernel + QEMU)
 help / color / mirror / Atom feed
From: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
To: khorenko@virtuozzo.com, vladimir.riabchun@virtuozzo.com
Cc: devel@openvz.org
Subject: [Devel] [PATCH hci-8.2 v2 14/18] objtool/klp: Reject changed .rodata objects
Date: Wed, 25 Nov 2026 08:03:56 +0200	[thread overview]
Message-ID: <20261125061124.1995383-15-eva.kurchatova@virtuozzo.com> (raw)
In-Reply-To: <20261125061124.1995383-1-eva.kurchatova@virtuozzo.com>

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

  parent reply	other threads:[~2026-11-25  6:14 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Eva Kurchatova [this message]
2026-11-25 10:35   ` [Devel] [PATCH hci-8.2 v2 14/18] objtool/klp: Reject changed .rodata objects 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125061124.1995383-15-eva.kurchatova@virtuozzo.com \
    --to=eva.kurchatova@virtuozzo.com \
    --cc=devel@openvz.org \
    --cc=khorenko@virtuozzo.com \
    --cc=vladimir.riabchun@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.