OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 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 15/28] objtool/klp: reject changed .rodata objects
Date: Thu, 26 Nov 2026 07:07:14 +0200	[thread overview]
Message-ID: <20261126060620.2126341-17-eva.kurchatova@virtuozzo.com> (raw)
In-Reply-To: <20261126060620.2126341-2-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. The same
goes for a writable object which the patch makes const and changes.

Fail klp diff when a correlated object in .rodata on either side differs
in length, bytes or relocations, or stops being const, 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. Targets no symbol
contains are compared by section only, as their offsets move with any
constant added before them, so a change to such data is not caught.

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 | 134 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 134 insertions(+)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 5122a3b51196..fa9089d6c3c1 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -443,6 +443,137 @@ 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;
+}
+
+/* A symbol without data is in .bss, which reads as zeroes */
+static bool data_changed(struct symbol *orig, struct symbol *patched)
+{
+	const unsigned char *orig_data = sym_data(orig);
+	const unsigned char *patched_data = sym_data(patched);
+	unsigned long i;
+
+	if (orig->len != patched->len)
+		return true;
+
+	if (orig_data && patched_data)
+		return memcmp(orig_data, patched_data, orig->len);
+
+	for (i = 0; i < orig->len; i++)
+		if ((orig_data && orig_data[i]) || (patched_data && patched_data[i]))
+			return true;
+
+	return false;
+}
+
+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);
+
+	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) ||
+	       data_changed(orig_sym, patched_sym);
+}
+
+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) || data_changed(orig, patched))
+		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 object through a klp reloc to
+ * the original, so a change to an object that is or becomes const 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) || !orig->twin ||
+		    (!is_rodata_sec(orig->sec) && !is_rodata_sec(orig->twin->sec)))
+			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)
 {
@@ -1723,6 +1854,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-26  6:13 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  5:06 [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 01/28] ms/objtool/klp: Fix detection of corrupt static branch/call entries Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 02/28] ms/objtool/klp: Disable unsupported pr_debug() usage Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 03/28] ms/objtool: Fix data alignment in elf_add_data() Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 04/28] ms/objtool/klp: fix data alignment in __clone_symbol() Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 05/28] ms/objtool/klp: Fix .data..once static local non-correlation Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 06/28] ms/objtool/klp: Fix create_fake_symbols() skipping entsize-based sections Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 07/28] ms/objtool/klp: Fix handling of zero-length .altinstr_replacement sections Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 08/28] ms/objtool/klp: Fix cloning of zero-length section symbols Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 09/28] ms/objtool/klp: Fix relocation conversion failures for R_X86_64_NONE Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 10/28] ms/objtool/klp: Fix reloc corruption in convert_reloc_sym_to_secsym() Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 11/28] ms/objtool/klp: Simplify reloc symbol conversion Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 12/28] ms/objtool: Move mark_rodata() to elf.c Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 13/28] ms/objtool/klp: Fix pointer comparisons for rodata objects Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 14/28] objtool/klp: use the section symbol for .rodata labels Eva Kurchatova
2026-11-26  5:07 ` Eva Kurchatova [this message]
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 16/28] ms/objtool/klp: Fix module name normalization for paths with dots Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 17/28] ms/objtool/klp: Normalize Module.symvers paths to module names Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 18/28] ms/objtool/klp: Fix cross-module klp relocation section naming Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 19/28] ms/objtool/klp: Don't match local symbols against exports Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 20/28] ms/objtool: Fix .cold function detection for duplicate symbols Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 21/28] ms/objtool/klp: Avoid NULL pointer dereference when printing code symbol name Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 22/28] ms/objtool/klp: Add .klp.symid for sympos disambiguation Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 23/28] ms/objtool/klp: Fix symbol resolution for duplicate data symbols Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 24/28] ms/objtool/klp: Fix size of empty special section entries Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 25/28] ms/objtool/klp: Fix vmlinux .klp.symid link error for .no_trim_symbol symbols Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 26/28] ms/objtool/klp: Ignore replacement offset of empty x86 alternatives Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 27/28] ms/objtool/klp: Allow new references to module exports Eva Kurchatova
2026-11-26  5:07 ` [Devel] [PATCH hci-8.2 v2 28/28] ms/objtool/klp: Fix vmlinux .klp.symid link error for .exitcall.exit symbols Eva Kurchatova
2026-11-26  6:15 ` [Devel] [PATCH hci-8.2 v2 00/28] ms/objtool: fix silently wrong livepatches by klp-build Eva Kurchatova (Virtuozzo)
2026-11-26 16:34 ` [Devel] [PATCH hci-8.2 v3 " 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=20261126060620.2126341-17-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox