mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] objtool/klp: Fix sympos for global/local symbol collisions
@ 2026-11-17  0:08 Wilson Felipe Pereira
  2026-11-17  0:08 ` [PATCH 1/2] objtool/klp: Disambiguate global symbols colliding with local symbols Wilson Felipe Pereira
  2026-11-17  0:08 ` [PATCH 2/2] objtool/klp: Add test for global/local symbol name collisions Wilson Felipe Pereira
  0 siblings, 2 replies; 3+ messages in thread
From: Wilson Felipe Pereira @ 2026-11-17  0:08 UTC (permalink / raw)
  To: Josh Poimboeuf, Peter Zijlstra, Miroslav Benes, Petr Mladek,
	Joe Lawrence, Song Liu
  Cc: live-patching, linux-kernel, Dylan Hatch, Yi Zhu, Wilson Felipe Pereira

When a defined STB_GLOBAL symbol in one translation unit shares its name
with a static (STB_LOCAL) symbol in another translation unit (for
example, cgroup_storage_lookup in kernel/bpf/local_storage.c vs
kernel/bpf/bpf_cgrp_storage.c), runtime kallsyms lookup counts both
symbols together. Emitting sympos 0 for the global symbol causes module
load to fail in klp_find_object_symbol() with "unresolvable ambiguity
for symbol".

Patch 1 disambiguates defined non-local symbols across .klp.symid
generation, klp_find_sympos(), and clone_reloc_klp() (while preserving
sympos 0 when unique). Patch 2 adds tests covering both module (.symtab
order) and vmlinux (.klp.symid address order) paths, including
EXPORT_SYMBOL_FOR_MODULES() exports.

Wilson Felipe Pereira (2):
  objtool/klp: Disambiguate global symbols colliding with local symbols
  objtool/klp: Add test for global/local symbol name collisions

 tools/objtool/klp-diff.c                      | 11 +++
 tools/objtool/klp-symid.c                     |  2 +-
 tools/objtool/klp-sympos.c                    |  9 +-
 .../tests/generic/fixtures/sympos_vmlinux.c   |  6 ++
 .../tests/generic/test-sympos-global-dup.sh   | 99 +++++++++++++++++++
 5 files changed, 123 insertions(+), 4 deletions(-)
 create mode 100755 tools/objtool/tests/generic/test-sympos-global-dup.sh


base-commit: 141b164bbe186423e295b139e40e8e2d11cf1341
-- 
2.56.1.703.gf848ed807c-goog


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

* [PATCH 1/2] objtool/klp: Disambiguate global symbols colliding with local symbols
  2026-11-17  0:08 [PATCH 0/2] objtool/klp: Fix sympos for global/local symbol collisions Wilson Felipe Pereira
@ 2026-11-17  0:08 ` Wilson Felipe Pereira
  2026-11-17  0:08 ` [PATCH 2/2] objtool/klp: Add test for global/local symbol name collisions Wilson Felipe Pereira
  1 sibling, 0 replies; 3+ messages in thread
From: Wilson Felipe Pereira @ 2026-11-17  0:08 UTC (permalink / raw)
  To: Josh Poimboeuf, Peter Zijlstra, Miroslav Benes, Petr Mladek,
	Joe Lawrence, Song Liu
  Cc: live-patching, linux-kernel, Dylan Hatch, Yi Zhu, Wilson Felipe Pereira

klp_find_sympos() returns 0 for any non-STB_LOCAL symbol, symid_needed()
skips non-local symbols when emitting .klp.symid entries for vmlinux.o,
and clone_reloc_klp() hardcodes sympos = 0 when a relocation targets a
restricted or module export.

However, a global symbol in one translation unit can share its name with a
static (STB_LOCAL) symbol in another translation unit (for example,
cgroup_storage_lookup in kernel/bpf/local_storage.c vs
kernel/bpf/bpf_cgrp_storage.c). At runtime, both
kallsyms_on_each_match_symbol() and module_kallsyms_on_each_symbol() count
local and global symbols together, so emitting sympos 0 for the global
symbol fails module load in klp_find_object_symbol() with 'unresolvable
ambiguity for symbol'.

Allow defined non-local symbols to receive .klp.symid entries when
duplicate names exist in vmlinux.o, compute sympos for defined non-local
symbols in klp_find_sympos() (returning 0 when unique and skipping
undefined and non-alloc symbols), and resolve sympos via klp_find_sympos()
in clone_reloc_klp() when the exported target is defined in the patched
object.

Signed-off-by: Wilson Felipe Pereira <wfelipe@google.com>
---
 tools/objtool/klp-diff.c   | 11 +++++++++++
 tools/objtool/klp-symid.c  |  2 +-
 tools/objtool/klp-sympos.c |  9 ++++++---
 3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 34da55e45609..37ad315d00af 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1404,9 +1404,20 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,
 	 */
 
 	if (export) {
+		const char *modname = find_modname(e);
+
+		if (!modname)
+			return -1;
+
 		sym_modname = export->mod;
 		sym_orig_name = export->sym;
 		sympos = 0;
+		if (!strcmp(export->mod, modname) &&
+		    patched_sym->twin && !is_undef_sym(patched_sym->twin)) {
+			sympos = klp_find_sympos(e->orig, patched_sym->twin);
+			if (sympos == ULONG_MAX)
+				return -1;
+		}
 	} else {
 		sym_modname = find_modname(e);
 		if (!sym_modname)
diff --git a/tools/objtool/klp-symid.c b/tools/objtool/klp-symid.c
index b19f76dff13e..d01c13b8ffa8 100644
--- a/tools/objtool/klp-symid.c
+++ b/tools/objtool/klp-symid.c
@@ -52,7 +52,7 @@ static bool symid_needed(struct elf *elf, struct symbol *sym)
 {
 	struct symbol *s;
 
-	if (!is_local_sym(sym) || is_undef_sym(sym))
+	if (is_undef_sym(sym))
 		return false;
 
 	if (!is_func_sym(sym) && !is_object_sym(sym))
diff --git a/tools/objtool/klp-sympos.c b/tools/objtool/klp-sympos.c
index dfca9dd74681..087892ef3593 100644
--- a/tools/objtool/klp-sympos.c
+++ b/tools/objtool/klp-sympos.c
@@ -381,14 +381,14 @@ unsigned long klp_find_sympos(struct elf *elf, struct symbol *sym)
 	bool has_dup = false;
 	struct symbol *s;
 
+	if (is_undef_sym(sym))
+		return 0;
+
 	if (is_init_sym(sym)) {
 		ERROR("%s: can't patch or reference init code/data", sym->name);
 		return ULONG_MAX;
 	}
 
-	if (sym->bind != STB_LOCAL)
-		return 0;
-
 	/*
 	 * vmlinux: the final link reorders symbols relative to vmlinux.o,
 	 * so the position needs to be derived from the linked orig vmlinux via
@@ -403,6 +403,9 @@ unsigned long klp_find_sympos(struct elf *elf, struct symbol *sym)
 	 * module_kallsyms_on_each_symbol().
 	 */
 	for_each_sym(elf, s) {
+		if (is_sec_sym(s) || is_file_sym(s) || is_undef_sym(s) ||
+		    !(s->sec->sh.sh_flags & SHF_ALLOC))
+			continue;
 		if (!strcmp(s->name, sym->name)) {
 			nr_matches++;
 			if (s == sym)
-- 
2.56.1.703.gf848ed807c-goog


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

* [PATCH 2/2] objtool/klp: Add test for global/local symbol name collisions
  2026-11-17  0:08 [PATCH 0/2] objtool/klp: Fix sympos for global/local symbol collisions Wilson Felipe Pereira
  2026-11-17  0:08 ` [PATCH 1/2] objtool/klp: Disambiguate global symbols colliding with local symbols Wilson Felipe Pereira
@ 2026-11-17  0:08 ` Wilson Felipe Pereira
  1 sibling, 0 replies; 3+ messages in thread
From: Wilson Felipe Pereira @ 2026-11-17  0:08 UTC (permalink / raw)
  To: Josh Poimboeuf, Peter Zijlstra, Miroslav Benes, Petr Mladek,
	Joe Lawrence, Song Liu
  Cc: live-patching, linux-kernel, Dylan Hatch, Yi Zhu, Wilson Felipe Pereira

Add a test covering sympos disambiguation when a defined STB_GLOBAL symbol
shares its name with a static STB_LOCAL symbol in another translation
unit:
- A unique non-exported global continues to emit sympos 0.
- On the module path, a static in the first TU and a non-exported global
  in the second TU emit sympos 1 and 2 in .symtab order.
- On the vmlinux (.klp.symid) path, placing the global at a lower address
  than the static emits sympos 1 in linked address order, both for an
  unexported global and for an EXPORT_SYMBOL_FOR_MODULES() export.

Verified by reverting each hunk in klp-sympos.c, klp-symid.c, and
klp-diff.c individually and confirming the corresponding subtest fails.

Signed-off-by: Wilson Felipe Pereira <wfelipe@google.com>
---
 .../tests/generic/fixtures/sympos_vmlinux.c   |  6 ++
 .../tests/generic/test-sympos-global-dup.sh   | 99 +++++++++++++++++++
 2 files changed, 105 insertions(+)
 create mode 100755 tools/objtool/tests/generic/test-sympos-global-dup.sh

diff --git a/tools/objtool/tests/generic/fixtures/sympos_vmlinux.c b/tools/objtool/tests/generic/fixtures/sympos_vmlinux.c
index d5e70994c582..68d6fca414e4 100644
--- a/tools/objtool/tests/generic/fixtures/sympos_vmlinux.c
+++ b/tools/objtool/tests/generic/fixtures/sympos_vmlinux.c
@@ -26,8 +26,14 @@ static const char __modinfo[]
 	__attribute__((section(".modinfo"), used, aligned(1))) = "\0name=vmlinux";
 #endif
 
+#ifdef GLOBAL_DUP
+int dup_counter = 2;
+#elif defined(EXTERN_DUP)
+extern int dup_counter;
+#else
 /* volatile so it survives as an STT_OBJECT rather than being folded away */
 static volatile int dup_counter __attribute__((section(VARSEC))) = 1;
+#endif
 
 int FUNC_NAME(int x)
 {
diff --git a/tools/objtool/tests/generic/test-sympos-global-dup.sh b/tools/objtool/tests/generic/test-sympos-global-dup.sh
new file mode 100755
index 000000000000..b309b1f3f7f8
--- /dev/null
+++ b/tools/objtool/tests/generic/test-sympos-global-dup.sh
@@ -0,0 +1,99 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# A defined STB_GLOBAL symbol in one translation unit can share its name with a
+# static (STB_LOCAL) symbol in another translation unit (for example,
+# cgroup_storage_lookup in kernel/bpf/local_storage.c vs
+# kernel/bpf/bpf_cgrp_storage.c).
+#
+# At runtime, kallsyms_on_each_match_symbol() and
+# module_kallsyms_on_each_symbol() count both local and global symbols of that
+# name, so emitting sympos 0 for the global reference fails module load with
+# "unresolvable ambiguity for symbol". Both the module path (symtab order) and
+# the vmlinux path (.klp.symid address order) must disambiguate the global
+# symbol with its 1-based position while still emitting 0 when the global name
+# is unique.
+
+. "$(dirname "$0")/../lib.sh"
+
+# 1. Unique non-exported global: sympos must remain 0.
+setup
+build_one sympos_vmlinux.c def_g.o     -DFUNC_NAME=def_g -DGLOBAL_DUP
+build_one sympos_vmlinux.c use_orig.o  -DFUNC_NAME=caller -DEXTERN_DUP -DNO_MODINFO
+build_one sympos_vmlinux.c use_patch.o -DFUNC_NAME=caller -DEXTERN_DUP -DNO_MODINFO \
+	-DPATCHED
+
+partial_link "$workdir/orig.o"    "$workdir/def_g.o" "$workdir/use_orig.o" ||
+	probe_skip "partial link unavailable"
+partial_link "$workdir/patched.o" "$workdir/def_g.o" "$workdir/use_patch.o" ||
+	probe_skip "partial link unavailable"
+
+run_diff
+assert_klp_sympos dup_counter 0
+cleanup
+
+# 2. Module path: STB_LOCAL in first TU + non-exported STB_GLOBAL in second TU.
+#    In ELF .symtab, STB_LOCAL symbols precede STB_GLOBAL symbols, so the local
+#    is position 1 and the global is position 2.
+setup
+for p in "" "-DPATCHED"; do
+	# shellcheck disable=SC2086
+	build_one sympos_vmlinux.c "loc$p.o" -DFUNC_NAME=use_local $p
+	# shellcheck disable=SC2086
+	build_one sympos_vmlinux.c "glob$p.o" -DFUNC_NAME=use_global \
+		-DGLOBAL_DUP -DNO_MODINFO $p
+done
+
+partial_link "$workdir/orig.o"    "$workdir/loc.o"         "$workdir/glob.o" ||
+	probe_skip "partial link unavailable"
+partial_link "$workdir/patched.o" "$workdir/loc-DPATCHED.o" "$workdir/glob-DPATCHED.o" ||
+	probe_skip "partial link unavailable"
+
+[ "$(count_input_symbols orig.o dup_counter)" = 2 ] ||
+	fail "fixture did not produce two dup_counter symbols"
+
+run_diff
+assert_klp_sympos dup_counter 1
+assert_klp_sympos dup_counter 2
+out_symbols | grep -qE '\.klp\.sym\.[^.]+\.dup_counter,0([[:space:]]|$)' &&
+	fail "sympos 0 emitted for global symbol colliding with local symbol"
+cleanup
+
+# 3. vmlinux path (.klp.symid): with -fdata-sections the STB_GLOBAL lands in
+#    .data.dup_counter (lower address) while the STB_LOCAL lands in .data.zzz
+#    (higher address), opposite to ELF .symtab order where locals come first.
+#    Patching only the global caller must emit sympos 1 (address order).
+setup
+build_one sympos_vmlinux.c loc.o -DFUNC_NAME=use_local -DVARSEC='".data.zzz"'
+build_one sympos_vmlinux.c glob_orig.o -DFUNC_NAME=use_global -DGLOBAL_DUP \
+	-DNO_MODINFO
+build_one sympos_vmlinux.c glob_patch.o -DFUNC_NAME=use_global -DGLOBAL_DUP \
+	-DNO_MODINFO -DPATCHED
+
+make_vmlinux_pair "$workdir/loc.o" "$workdir/glob_orig.o" \
+	       -- "$workdir/loc.o" "$workdir/glob_patch.o"
+
+[ "$(count_input_symbols vmlinux.o dup_counter)" = 2 ] ||
+	fail "vmlinux fixture did not produce two dup_counter symbols"
+
+# Premise: in ELF .symtab the local comes first (higher address .data.zzz),
+# while in address order the global comes first (.data.aaa).
+first_addr="$(in_symbols vmlinux | awk '$8 == "dup_counter" { print $2; exit }')"
+low_addr="$(in_symbols vmlinux | awk '$8 == "dup_counter" { print $2 }' | sort | head -1)"
+[ "$first_addr" != "$low_addr" ] ||
+	probe_skip "linker did not place global before local in address order"
+
+run_diff
+assert_klp_sympos dup_counter 1
+out_symbols | grep -qE '\.klp\.sym\.[^.]+\.dup_counter,(0|2)([[:space:]]|$)' &&
+	fail "wrong sympos emitted for vmlinux global colliding with higher-addr local"
+
+# 4. Same vmlinux collision when the global uses EXPORT_SYMBOL_FOR_MODULES(),
+#    which takes the export branch in clone_reloc_klp().
+add_exports_ns vmlinux "module:other" dup_counter
+run_diff
+assert_klp_sympos dup_counter 1
+out_symbols | grep -qE '\.klp\.sym\.[^.]+\.dup_counter,(0|2)([[:space:]]|$)' &&
+	fail "wrong sympos emitted for module-restricted export colliding with local"
+
+pass "global symbol colliding with static local gets disambiguated sympos"
-- 
2.56.1.703.gf848ed807c-goog


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

end of thread, other threads:[~2026-11-17  0:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17  0:08 [PATCH 0/2] objtool/klp: Fix sympos for global/local symbol collisions Wilson Felipe Pereira
2026-11-17  0:08 ` [PATCH 1/2] objtool/klp: Disambiguate global symbols colliding with local symbols Wilson Felipe Pereira
2026-11-17  0:08 ` [PATCH 2/2] objtool/klp: Add test for global/local symbol name collisions Wilson Felipe Pereira

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

all inboxes | Powered by JetHome®