[Devel] [PATCH hci-8.2 v2 14/18] objtool/klp: Reject changed .rodata objects
Eva Kurchatova
eva.kurchatova at virtuozzo.com
Wed Nov 25 09:03:56 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.
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 at 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
More information about the Devel
mailing list