objtool/klp: Fix sympos for global/local symbol collisions
Subsystems (2)
cover objtool/klp: Fix sympos for global/local symbol collisions
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
Signature · 1 line
2.56.1.703.gf848ed807c-goog
PATCH 1/2 objtool/klp: Disambiguate global symbols colliding with local symbols
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.
--- 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(-)
tools/objtool/klp-diff.c +11 -0
@@ -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)
tools/objtool/klp-symid.c +1 -1
@@ -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))
tools/objtool/klp-sympos.c +6 -3
@@ -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)
Signature · 1 line
2.56.1.703.gf848ed807c-goog
[email protected]
AI reviewRe: [PATCH 1/2] objtool/klp: Disambiguate global symbols colliding with local symbols
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Medium] objtool/klp: inflated module sympos…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Medium] objtool/klp: inflated module sympos in klp_find_sympos() [1] --
commit 26d2979f735e7ba2e445358f238d7b4e6e36d856
Author: Wilson Felipe Pereira <[email protected]>
objtool/klp: Disambiguate global symbols colliding with local symbols
This commit teaches objtool's livepatch symbol handling to emit .klp.symid entries and compute symbol positions for defined non-local symbols when duplicate names exist in the object.
Quoted 23 lines
tools/objtool/klp-sympos.c +3
-3@@ -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
tools/objtool/klp-sympos.c unsigned long klp_find_sympos(struct elf *elf, struct symbol *sym) · line 408
Quoted 6 lines
tools/objtool/klp-sympos.c +3
-0@@ -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 ;
[Severity: Medium]
This problem wasn't introduced by this patch, but should this filter also skip is_init_sym(s) [1]?
The module loader excludes .init symbols when building core_kallsyms, which module_kallsyms_on_each_symbol() walks at runtime. If a module has an .init symbol with the same name as sym, counting it here inflates sympos so the livepatch either fails to resolve the symbol at load time or binds to the wrong occurrence.
[1] https://sashiko.dev/#/bug/linux-f8712b9c-c02a-4a37-83a3-92848caee298
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Josh Poimboeuf
maintainerRe: [PATCH 1/2] objtool/klp: Disambiguate global symbols colliding with local symbols
In reply to Wilson Felipe PereiraQuoted 43 lines · 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, andclone_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_lookupinkernel/bpf/local_storage.cvskernel/bpf/bpf_cgrp_storage.c). At runtime, bothkallsyms_on_each_match_symbol()andmodule_kallsyms_on_each_symbol()count local and global symbols together, so emitting sympos 0 for the global symbol fails module load inklp_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 viaklp_find_sympos()inclone_reloc_klp()when the exported target is defined in the patched object.Signed-off-by Wilson Felipe Pereira <[email protected]>--- 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(-)tools/objtool/klp-diff.c +5
-0@@ -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;
tools/objtool/klp-diff.c static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc, · line 1423
Quoted 10 lines
tools/objtool/klp-diff.c +6
-0sympos =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)
This could be simplified a bit?
if (export) {
sym_modname = export->mod;
sym_orig_name = export->sym;
} else {
sym_modname = find_modname(e);
if (!sym_modname)
return -1;
sym_orig_name = patched_sym->twin->name;
}
sympos = 0;
if (patched_sym->twin) {
sympos = klp_find_sympos(e->orig, patched_sym->twin);
if (sympos == ULONG_MAX)
return -1;
}Signature · 1 line
Josh
PATCH 2/2 objtool/klp: Add test for global/local symbol name collisions
In reply to Wilson Felipe PereiraAdd 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.
--- .../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
tools/objtool/tests/generic/fixtures/sympos_vmlinux.c +6 -0
@@ -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) {
tools/objtool/tests/generic/test-sympos-global-dup.sh new file +99 -0
99 lines not on this page · raw message
Signature · 1 line
2.56.1.703.gf848ed807c-goog