[Devel] [PATCH hci-8.2 v2 15/28] objtool/klp: reject changed .rodata objects
Eva Kurchatova
eva.kurchatova at virtuozzo.com
Thu Nov 26 08:07:14 MSK 2026
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 at 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
More information about the Devel
mailing list