objtool/klp: sympos/module/alternative/etc fixes
Subsystems (2)
Josh Poimboeuf
maintainerv3 cover objtool/klp: sympos/module/alternative/etc fixes
v3:
- remove "size = 0" and add comments to create_fake_symbols()
- add patches 8 & 9
v2: https://lore.kernel.org/[email protected]
- rebased on tip/master (first 6 patches were merged)
- dropped original patches 7-8 (will post followup)
- added .klp.symid fix
v1: https://lore.kernel.org/[email protected]
This consolidates fixes for the klp-build issues reported by Joe over the last several weeks, plus some more things I found while testing/reviewing.
Joe Lawrence (1):
objtool/klp: Allow new references to module exports
Josh Poimboeuf (8):
objtool/klp: Fix vmlinux .klp.symid link error for .no_trim_symbol
symbols
objtool/klp: Fix size of empty special section entries objtool/klp: Ignore replacement offset of empty x86 alternatives objtool/klp: Explicitly disallow patching or referencing init
code/data
objtool/klp: Fix cross-module klp relocation section naming objtool/klp: Don't match local symbols against exports objtool/klp: Fix relocations for EXPORT_SYMBOL_FOR_MODULES() symbols objtool/klp: Fix line numbers in Module.symvers parse errors
tools/objtool/arch/x86/special.c | 27 ++++++ tools/objtool/include/objtool/klp.h | 10 ++- tools/objtool/include/objtool/special.h | 7 ++ tools/objtool/klp-diff.c | 106 ++++++++++++++++++++---- tools/objtool/klp-post-link.c | 53 +++++++----- tools/objtool/klp-symid.c | 1 + tools/objtool/klp-sympos.c | 10 +++ 7 files changed, 175 insertions(+), 39 deletions(-)
Signature · 1 line
2.54.0
Josh Poimboeuf
maintainerPATCH v3 1/9 objtool/klp: Fix vmlinux .klp.symid link error for .no_trim_symbol symbols
Testing klp-build with arm64 produced the following linker error during the original kernel build:
__notrim.1' referenced in section .klp.symid' of vmlinux.o: defined in discarded section `.no_trim_symbol' of vmlinux.o
symbol_get() puts a static __notrim[] in .no_trim_symbol, which GCC names __notrim.1, __notrim.2, etc. Two or more built-in translation units calling symbol_get() thus produce duplicate names, resulting in corresponding .klp.symid references which trigger the above error.
Add .no_trim_symbol to the discarded section list so its symbols don't get symids.
Note this issue is not specific to arm64: it just needs two built-in symbol_get() callers. arm64 trips over it easily because it has KVM always compiled in vmlinux, whereas on x86 it's typically a module.
--- tools/objtool/klp-symid.c | 1 + 1 file changed, 1 insertion(+)
tools/objtool/klp-symid.c +1 -0
@@ -31,6 +31,7 @@static const char *const discarded_secs[] = {".discard" ,".modinfo" , +".no_trim_symbol" ,"__tracepoint_check" , };
Signature · 1 line
2.54.0
Josh Poimboeuf
maintainerPATCH v3 2/9 objtool/klp: Fix size of empty special section entries
In reply to Josh Poimboeufcreate_fake_symbols() sizes each ANNOTATE_DATA_SPECIAL entry from the offset of the next annotation, falling back to the end of the section for the last entry. But the last entry is detected by a zero size, which also happens for an *empty* entry: ALTERNATIVE(oldinstr, "", ft) still annotates its zero-length replacement, at the same offset as the next entry's annotation.
So every empty replacement gets a fake symbol spanning the entire rest of .altinstr_replacement. That's harmless today only because find_symbol_containing() picks the smaller of two overlapping symbols.
Track whether a next annotation was found rather than inferring it from the size. A zero-length fake symbol is fine: find_symbol_containing() skips those, so the properly sized symbol at the same offset still wins.
--- tools/objtool/klp-diff.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
tools/objtool/klp-diff.c +11 -2
@@ -1627,13 +1627,17 @@ static int create_fake_symbols(struct elf *elf)for_each_reloc (sec->rsec, reloc) {unsigned long offset, size;struct reloc *next_reloc; +bool last = true;if (annotype (elf, sec, reloc) !=ANNOTYPE_DATA_SPECIAL )continue ; offset =reloc_addend (reloc); - size =0 ; +/* +* Find the start of the next entry so the fake symbol size can +* be calculated. +*/ next_reloc = reloc;for_each_reloc_continue (sec->rsec, next_reloc) { if (annotype(elf, sec, next_reloc) != ANNOTYPE_DATA_SPECIAL ||
@@ -1641,10 +1645,15 @@ static int create_fake_symbols(struct elf *elf)continue ; size =reloc_addend (next_reloc) - offset; + last = false;break ; } -if (!size) +/* +* If no next entry found, this is the last entry, so its size +* is from the current offset to the end of the section. +*/ +if (last) size =sec_size (reloc->sym->sec) - offset;if (create_fake_symbol (elf, reloc->sym->sec, offset, size))
Signature · 1 line
2.54.0
Josh Poimboeuf
maintainerPATCH v3 3/9 objtool/klp: Ignore replacement offset of empty x86 alternatives
In reply to Josh PoimboeufAn x86 alternative with an empty replacement, e.g. the second entry of
ALTERNATIVE_2("orig", "repl", ft1, "", ft2)
has a replacementlen of zero. Its replacement offset still gets a relocation, but the label it points at is the end of the previous replacement, which is also the beginning of the *next* alternative's replacement. The value is meaningless; get_alt_entry() already ignores it for that reason.
klp diff doesn't ignore it. When such an alternative belongs to a changed function, cloning its relocations drags in the unrelated neighboring replacement, along with everything that replacement references. On an x86 clang/lto build an empty alternative in meminfo_proc_show() pulled in the replacement of an alternative in proc_kcore_init(), silently emitting a klp relocation against init text which has long since been freed by the time the patch is applied.
Add arch_alt_ignore_new_reloc() and skip such relocations when cloning. This has to be arch specific: on arm64 a zero-length replacement instead identifies an alternative callback, whose replacement offset points at the callback function and must be preserved.
--- tools/objtool/arch/x86/special.c | 27 +++++++++++++++++++++++++ tools/objtool/include/objtool/special.h | 7 +++++++ tools/objtool/klp-diff.c | 6 +++++- 3 files changed, 39 insertions(+), 1 deletion(-)
tools/objtool/arch/x86/special.c +27 -0
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0-or-later #include <string.h> +#include <arch/special.h> #include <objtool/special.h> #include <objtool/builtin.h> #include <objtool/warn.h>
@@ -9,6 +10,32 @@/* cpu feature name array generated from cpufeatures.h */ #include "cpu-feature-names.c" +/* +* An alternative with an empty replacement, e.g. the second entry of +* +* ALTERNATIVE_2("orig", "repl", ft1, "", ft2) +* +* still gets a relocation for its replacement offset. But the label it points +* at is the end of the previous entry's replacement, which is also the +* beginning of the *next* entry's replacement. The value is meaningless: it's +* only ever used with a length of zero. +*/ +bool arch_alt_ignore_new_reloc (struct section *sec,unsigned long offset) +{ +unsigned long entry_off; + +if (strcmp (sec->name,".altinstructions" )) +return false; + + entry_off = offset - (offset %ALT_ENTRY_SIZE ); + +if (offset - entry_off !=ALT_NEW_OFFSET ) +return false; + +return !*(unsigned char *)(sec->data->d_buf + entry_off + +ALT_NEW_LEN_OFFSET ); +} +void arch_handle_alternative (struct special_alt *alt) {static struct special_alt *group, *prev;
tools/objtool/include/objtool/special.h +7 -0
@@ -32,6 +32,13 @@ int special_get_alts(struct elf *elf, struct list_head *alts);void arch_handle_alternative (struct special_alt *alt); +/* +* Should the reloc at @offset -- the "new" (replacement) field of a special +* section group entry -- be ignored? The meaning of a zero-length replacement +* is arch specific, so the arch decides. +*/ +bool arch_alt_ignore_new_reloc (struct section *sec,unsigned long offset); +bool arch_support_alt_relocation (struct special_alt *special_alt,struct instruction *insn,struct reloc *reloc);
tools/objtool/klp-diff.c +5 -1
@@ -12,7 +12,7 @@#include <objtool/arch.h> #include <objtool/klp.h> #include <objtool/util.h> -#include <arch/special.h> +#include <objtool/special.h> #include <linux/align.h> #include <linux/objtool_types.h>
@@ -1537,6 +1537,10 @@ static int clone_sym_relocs(struct elfs *e, struct symbol *patched_sym) !strcmp(patched_reloc->sym->sec->name, ".altinstr_aux")) continue; +if (arch_alt_ignore_new_reloc (patched_sym->sec, +reloc_offset (patched_reloc))) +continue ; + ret =convert_reloc_sym (e->patched, patched_reloc);if (ret <0 ) { ERROR_FUNC(patched_rsec->base, reloc_offset(patched_reloc),
Signature · 1 line
2.54.0
Josh Poimboeuf
maintainerPATCH v3 4/9 objtool/klp: Explicitly disallow patching or referencing init code/data
In reply to Josh PoimboeufExplicitly disallow the patching and referencing of init code/data. Otherwise it could potentially introduce some odd edge cases depending on whether the target object's init section has been freed yet (note that the init code still exists in the target module when doing late module patching).
Such edge cases include sympos calculation and the patching and/or referencing of non-existent (init-freed) code/data. Not to mention the inherent differences in behavior that occur when the init code is only patched *some* of the time depending on module loading order or kernel config.
--- tools/objtool/klp-sympos.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
tools/objtool/klp-sympos.c +10 -0
@@ -367,6 +367,11 @@ static unsigned long find_vmlinux_sympos(struct symbol *sym)return sympos; } +static bool is_init_sym (struct symbol *sym) +{ +return strstarts (sym->sec->name,".init" ); +} +/* * "sympos" is used by livepatch to disambiguate duplicate symbol names. */
@@ -376,6 +381,11 @@ unsigned long klp_find_sympos(struct elf *elf, struct symbol *sym)bool has_dup = false;struct symbol *s; +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 ;
Signature · 1 line
2.54.0
Josh Poimboeuf
maintainerPATCH v3 5/9 objtool/klp: Fix cross-module klp relocation section naming
In reply to Josh PoimboeufA klp relocation section is .klp.rela.<objname>.<secname>, where objname is the object being patched.
klp-build wrongly derives objname from where the referenced symbol lives, not where it's referenced. For a cross-module reference like patched can_isotp code calling can.ko's can_rx_unregister(), that gives .klp.rela.can..text rather than .klp.rela.can_isotp..text. Unless the patch happens to patch can.ko as well, the relocation never gets applied and the call goes off into the weeds.
Name the intermediate section __klp_relocs.<objname> so post-link can read the patched object's name from there.
--- tools/objtool/include/objtool/klp.h | 10 ++++-- tools/objtool/klp-diff.c | 17 +++++++-- tools/objtool/klp-post-link.c | 53 +++++++++++++++++------------ 3 files changed, 52 insertions(+), 28 deletions(-)
tools/objtool/include/objtool/klp.h +7 -3
@@ -14,11 +14,15 @@#define KLP_FUNCS_SEC ".init.klp_funcs"/* -* __klp_relocs is an intermediate section which are created by klp diff and -* converted into KLP symbols/relas by "objtool klp post-link". This is needed -* to work around the linker, which doesn't preserve SHN_LIVEPATCH or +* __klp_relocs.<objname> are intermediate sections which are created by klp +* diff and converted into KLP symbols/relas by "objtool klp post-link". This +* is needed to work around the linker, which doesn't preserve SHN_LIVEPATCH or * SHF_RELA_LIVEPATCH, nor does it support having two RELA sections for a * single PROGBITS section. +* +* "objname" is the name of the object being patched ("vmlinux" or a module +* name). post-link uses it to name the resulting +* .klp.rela.objname.section_name sections. */ #define KLP_RELOCS_SEC "__klp_relocs"#define KLP_STRINGS_SEC ".rodata.klp.str1.1"
tools/objtool/klp-diff.c +14 -3
@@ -1380,8 +1380,8 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc, }/* -* Create the __klp_relocs entry. This will be converted to an actual -* KLP rela by "objtool klp post-link". +* Create the __klp_relocs.<objname> entry. This will be converted to +* an actual KLP rela by "objtool klp post-link". * * This intermediate step is necessary to prevent corruption by the * linker, which doesn't know how to properly handle two rela sections
@@ -1389,7 +1389,18 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,*/ if (!klp_relocs) { - klp_relocs =elf_create_section (e->out,KLP_RELOCS_SEC ,0 , +const char *objname =find_modname (e); +char sec_name[SEC_NAME_LEN ]; + +if (!objname) +return -1 ; + +/* section format: __klp_relocs.objname */ +if (snprintf_check (sec_name,SEC_NAME_LEN , +KLP_RELOCS_SEC ".%s" , objname)) +return -1 ; + + klp_relocs =elf_create_section (e->out, sec_name,0 ,0 ,SHT_PROGBITS ,8 ,SHF_ALLOC );if (!klp_relocs)return -1 ;
tools/objtool/klp-post-link.c +31 -22
98 lines not on this page · raw message
Signature · 1 line
2.54.0
Josh Poimboeuf
maintainerPATCH v3 6/9 objtool/klp: Don't match local symbols against exports
In reply to Josh PoimboeufWhile cloning a reloc, klp diff calls find_export() to determine whether the referenced symbol is exported. That decides whether the reference needs a klp reloc, which object the klp symbol belongs to, and whether the symbol's data needs to be copied into the patch module.
But find_export() matches purely on symbol name, so a static function or variable which happens to share its name with an export is mistaken for a reference to that export:
- klp_reloc_needed() creates a klp reloc pointing at the exporting module's symbol rather than the local one. For a vmlinux export it skips the klp reloc altogether, leaving a normal reloc which the module loader resolves to the vmlinux symbol.
- clone_reloc() treats the symbol as external and clones it without its data, leaving a dangling reference.
- validate_special_section_klp_reloc() attributes a static branch or call key to the wrong module, and for a vmlinux export skips the unsupported-key check entirely.
Exports are always global, so ignore local symbols in find_export().
--- tools/objtool/klp-diff.c | 3 +++ 1 file changed, 3 insertions(+)
tools/objtool/klp-diff.c +3 -0
@@ -1101,6 +1101,9 @@ static struct export *find_export(struct symbol *sym) {struct export *export; +if (is_local_sym (sym)) +return NULL ; +hash_for_each_possible (exports, export, hash,str_hash (sym->name)) {if (!strcmp (export->sym, sym->name))return export;
Signature · 1 line
2.54.0
Josh Poimboeuf
maintainerPATCH v3 7/9 objtool/klp: Allow new references to module exports
In reply to Josh PoimboeufFrom: Joe Lawrence <[email protected]>
klp_reloc_needed() returns true for module exports to support late-module patching. However, clone_reloc_klp() unconditionally rejects symbols without a twin (i.e., new references added by the patch), even when the symbol is a known export from Module.symvers.
Relax the check: allow new references to exported symbols by only erroring on !twin when there is no export. The export metadata from Module.symvers provides sufficient context to emit the klp-relocation without a twin.
For a module export that isn't sufficient on its own though, as the resulting klp relocation will only be resolved at patch-enable time if the exporting module is loaded.
If the original (unpatched) module already depends on the exporting module, the dependency is safe: the module loader ensures the dependency is satisfied before the patched module can be loaded, so the klp relocation target will exist.
However, if the patch introduces a reference to a module that the original doesn't depend on, there is no such guarantee. The exporting module could be absent or could be unloaded at any time, leading to a relocation failure or use-after-free.
So also add a build-time check: when a new symbol reference (no twin) targets a module export, verify that the original module already has at least one UNDEF symbol resolving to that same exporting module. If not, error out with a diagnostic message.
--- tools/objtool/klp-diff.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-)
tools/objtool/klp-diff.c +33 -2
@@ -1294,6 +1294,28 @@ static int convert_reloc_sym(struct elf *elf, struct reloc *reloc)return convert_reloc_secsym_to_sym (elf, reloc); } +/* +* Check if the original module already has a dependency on dep_mod, i.e. it +* already references at least one export from that module. +*/ +static bool has_module_dep (struct elfs *e,const char *dep_mod) +{ +struct symbol *sym; + +for_each_sym (e->orig, sym) { +struct export *exp; + +if (!is_undef_sym (sym) ||is_weak_sym (sym)) +continue ; + + exp =find_export (sym); +if (exp && !strcmp (exp->mod, dep_mod)) +return true; + } + +return false; +} +/* * Convert a regular relocation to a klp relocation (sort of). */
@@ -1313,8 +1335,17 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,unsigned long sympos;if (!patched_sym->twin) { -ERROR ("unexpected klp reloc for new symbol %s" , patched_sym->name); -return -1 ; +if (!export) { +ERROR ("unexpected klp reloc for new symbol %s" , patched_sym->name); +return -1 ; + } + +if (strcmp (export->mod,"vmlinux" ) && + !has_module_dep (e, export->mod)) { +ERROR ("%s: new reference to %s (exported by %s) would create an undeclared module dependency" , + patched_sym->name, export->sym, export->mod); +return -1 ; + } }/*
Signature · 1 line
2.54.0
Josh Poimboeuf
maintainerPATCH v3 8/9 objtool/klp: Fix relocations for EXPORT_SYMBOL_FOR_MODULES() symbols
In reply to Josh PoimboeufEXPORT_SYMBOL_FOR_MODULES() puts a symbol in a "module:<names>" namespace, which the module loader grants access to by matching the importing module's name against that list.
klp_reloc_needed() only creates a klp reloc for module-owned exports; a vmlinux export gets a normal reloc. For a vmlinux symbol exported with EXPORT_SYMBOL_FOR_MODULES(), using a normal reloc results in a modpost failure in klp-build:
ERROR: modpost: module livepatch-foo uses symbol mpol_shared_policy_lookup from namespace module:kvm, but does not import it.
And the modpost error is correct: even with that error removed, the patch module would fail to load:
livepatch_foo: module uses symbol (mpol_shared_policy_lookup) from namespace module:kvm, but does not import it.
livepatch_foo: Unknown symbol mpol_shared_policy_lookup (err -22)
Treat it like an unexported symbol by using a klp reloc.
Note this only affects "module:" namespaces. Ordinary namespaced exports continue to work with normal relocs thanks to copy_import_ns(), which propagates the patched object's import_ns tags to the patch module.
--- tools/objtool/klp-diff.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-)
tools/objtool/klp-diff.c +23 -5
@@ -30,7 +30,9 @@ struct elfs {struct export {struct hlist_node hash; -char *mod, *sym; +char *mod; +char *sym; +bool mod_ns; };bool debug, debug_correlate, debug_clone;
@@ -135,7 +137,7 @@ static int read_exports(void) }while (fgets (line,1024 , file)) { -char *sym, *mod, *type; +char *sym, *mod, *type, *namespace;struct export *export; sym =strchr (line,'\t' );
@@ -162,6 +164,14 @@ static int read_exports(void) *type++ ='\0' ; + namespace =strchr (type,'\t' ); +if (!namespace) { +ERROR ("malformed Module.symvers (namespace) at line %d" , line_num); +return -1 ; + } + + *namespace++ ='\0' ; +if (*sym =='\0' || *mod =='\0' ) {ERROR ("malformed Module.symvers at line %d" , line_num);return -1 ;
@@ -188,6 +198,9 @@ static int read_exports(void)return -1 ; } +/* EXPORT_SYMBOL_FOR_MODULES() */ + export->mod_ns =strstarts (namespace,"module:" ); +hash_add (exports, &export->hash,str_hash (sym)); }
@@ -1174,11 +1187,16 @@ static bool klp_reloc_needed(struct reloc *patched_reloc)* clusterfunk that is late module patching, the patch module is * allowed to be loaded before any modules it depends on. * -* If exported by vmlinux, a normal reloc will do. +* If exported by vmlinux to all modules, a normal reloc will do. */ export =find_export (patched_sym); -if (export) -return strcmp (export->mod,"vmlinux" ); +if (export) { +if (strcmp (export->mod,"vmlinux" )) +return true; + +/* EXPORT_SYMBOL_FOR_MODULES() gets a klp reloc */ +return export->mod_ns; + }if (!patched_sym->twin) {/*
Signature · 1 line
2.54.0
Josh Poimboeuf
maintainerPATCH v3 9/9 objtool/klp: Fix line numbers in Module.symvers parse errors
In reply to Josh Poimboeufread_exports() reports the offending line number when it fails to parse Module.symvers. The counter is initialized to 1 but never incremented, so every error blames line 1 regardless of where the bad line is.
--- tools/objtool/klp-diff.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/objtool/klp-diff.c +3 -1
@@ -118,7 +118,7 @@ static int read_exports(void) {const char *symvers ="Module.symvers" ;char line[1024 ], *path =NULL ; -unsigned int line_num =1 ; +unsigned int line_num =0 ;FILE *file; file =fopen (symvers,"r" );
@@ -140,6 +140,8 @@ static int read_exports(void)char *sym, *mod, *type, *namespace;struct export *export; + line_num++; + sym =strchr (line,'\t' );if (!sym) {ERROR ("malformed Module.symvers (sym) at line %d" , line_num);
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v3 6/9] objtool/klp: Don't match local symbols against exports
In reply to Josh PoimboeufThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Unexported global symbols that share a name with an export are incorrectly matched against the global exports list, causing misrouted livepatch relocations. --
--- Patch [6]: [PATCH v3 6/9] objtool/klp: Don't match local symbols against exports --- commit 91941e947fdde72be13c7760463244a153a175ce
Author: Josh Poimboeuf <[email protected]>
objtool/klp: Don't match local symbols against exports
This commit updates find_export() in klp-diff.c to ignore local symbols. This addresses an issue where static functions or variables that share a name with an export are incorrectly treated as export references during livepatch generation.
tools/objtool/klp-diff.c static struct export *find_export(struct symbol *sym) · line 1109
Quoted 13 lines
tools/objtool/klp-diff.c +3
-0@@ -1101,6 +1101,9 @@ static struct export *find_export(struct symbol *sym) {struct export *export; +if (is_local_sym (sym)) +return NULL ; +hash_for_each_possible (exports, export, hash,str_hash (sym->name)) {if (!strcmp (export->sym, sym->name))return export;
[Severity: High]
This is a pre-existing issue, but does this still leave unexported global symbols vulnerable to incorrect matching?
If a module contains an unexported global symbol that shares its name with an unrelated exported symbol, it will bypass the is_local_sym() check.
When the module is livepatched and klp-diff processes a relocation to this internal symbol, find_export() will perform a string match and return the unrelated export. Would this cause a KLP relocation to incorrectly point to the external module's export instead of the internal symbol?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Josh Poimboeuf
maintainerRe: [PATCH v3 6/9] objtool/klp: Don't match local symbols against exports
Quoted 35 lines · On Fri, Aug 07, 2026 at 09:46:11PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Unexported global symbols that share a name with an export are incorrectly matched against the global exports list, causing misrouted livepatch relocations. ----- Patch [6]: [PATCH v3 6/9] objtool/klp: Don't match local symbols against exports --- commit
91941e947fdde72be13c7760463244a153a175ce
Author: Josh Poimboeuf <[email protected]>objtool/klp: Don't match local symbols against exports
This commit updates
find_export()inklp-diff.cto ignore local symbols. This addresses an issue where static functions or variables that share a name with an export are incorrectly treated as export references during livepatch generation.tools/objtool/klp-diff.c static struct export *find_export(struct symbol *sym) · line 1109
Quoted 13 lines
tools/objtool/klp-diff.c +3
-0@@ -1101,6 +1101,9 @@ static struct export *find_export(struct symbol *sym) {struct export *export; +if (is_local_sym (sym)) +return NULL ; +hash_for_each_possible (exports, export, hash,str_hash (sym->name)) {if (!strcmp (export->sym, sym->name))return export;[Severity: High]
This is a pre-existing issue, but does this still leave unexported global symbols vulnerable to incorrect matching?
Quoted 7 lines
If a module contains an unexported global symbol that shares its name with an unrelated exported symbol, it will bypass the
is_local_sym()check.When the module is livepatched and klp-diff processes a relocation to this internal symbol,
find_export()will perform a string match and return the unrelated export. Would this cause a KLP relocation to incorrectly point to the external module's export instead of the internal symbol?
I'm not convinced this is a realistic scenario (module having a global symbol with the same name as an exported symbol), but as mentioned it's a preexisting issue that can be fixed in a followup.
Signature · 1 line
Josh
Quoted 30 lines · Josh Poimboeuf
v3:
- remove "size = 0" and add comments tocreate_fake_symbols()
- add patches 8 & 9v2: https://lore.kernel.org/[email protected]
- rebased on tip/master (first 6 patches were merged)
- dropped original patches 7-8 (will post followup)
- added .klp.symid fixv1: https://lore.kernel.org/[email protected]
This consolidates fixes for the klp-build issues reported by Joe over the last several weeks, plus some more things I found while testing/reviewing.
Joe Lawrence (1):
objtool/klp: Allow new references to module exportsJosh Poimboeuf (8):
objtool/klp: Fix vmlinux .klp.symid link error for .no_trim_symbol
symbols
objtool/klp: Fix size of empty special section entries objtool/klp: Ignore replacement offset of empty x86 alternatives objtool/klp: Explicitly disallow patching or referencing init
code/data
objtool/klp: Fix cross-module klp relocation section naming objtool/klp: Don't match local symbols against exports objtool/klp: Fix relocations forEXPORT_SYMBOL_FOR_MODULES()symbols objtool/klp: Fix line numbers in Module.symvers parse errors
Quoted 9 lines
tools/objtool/arch/x86/special.c | 27 ++++++ tools/objtool/include/objtool/klp.h | 10 ++- tools/objtool/include/objtool/special.h | 7 ++ tools/objtool/klp-diff.c | 106 ++++++++++++++++++++---- tools/objtool/klp-post-link.c | 53 +++++++----- tools/objtool/klp-symid.c | 1 + tools/objtool/klp-sympos.c | 10 +++ 7 files changed, 175 insertions(+), 39 deletions(-)
For the series:
Previous test cases from the earlier parent patchsets and reports run positive now.
Thanks,
Signature · 1 line
Joe
Re: [PATCH v3 4/9] objtool/klp: Explicitly disallow patching or referencing init code/data
In reply to Josh PoimboeufQuoted 7 lines · Josh Poimboeuf
Explicitly disallow the patching and referencing of init code/data. Otherwise it could potentially introduce some odd edge cases depending on whether the target object's init section has been freed yet (note that the init code still exists in the target module when doing late module patching).
Quoted 7 lines
Such edge cases include sympos calculation and the patching and/or referencing of non-existent (init-freed) code/data. Not to mention the inherent differences in behavior that occur when the init code is only patched *some* of the time depending on module loading order or kernel config.
Signed-off-by Josh Poimboeuf <[email protected]>
Re: [PATCH v3 8/9] objtool/klp: Fix relocations for EXPORT_SYMBOL_FOR_MODULES() symbols
In reply to Josh PoimboeufQuoted 25 lines · Josh Poimboeuf
EXPORT_SYMBOL_FOR_MODULES()puts a symbol in a "module:<names>" namespace, which the module loader grants access to by matching the importing module's name against that list.
klp_reloc_needed()only creates a klp reloc for module-owned exports; a vmlinux export gets a normal reloc. For a vmlinux symbol exported withEXPORT_SYMBOL_FOR_MODULES(), using a normal reloc results in a modpost failure in klp-build:ERROR: modpost: module livepatch-foo uses symbol
mpol_shared_policy_lookupfrom namespace module:kvm, but does not import it.And the modpost error is correct: even with that error removed, the patch module would fail to load:
livepatch_foo: module uses symbol (mpol_shared_policy_lookup) from namespace module:kvm, but does not import it.
livepatch_foo: Unknown symbolmpol_shared_policy_lookup(err -22)Treat it like an unexported symbol by using a klp reloc.
Note this only affects "module:" namespaces. Ordinary namespaced exports continue to work with normal relocs thanks to
copy_import_ns(), which propagates the patched object'simport_nstags to the patch module.
Quoted 4 lines
Fixes dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")Reported-by Joe Lawrence <[email protected]>Signed-off-by Josh Poimboeuf <[email protected]>
Re: [PATCH v3 9/9] objtool/klp: Fix line numbers in Module.symvers parse errors
In reply to Josh PoimboeufQuoted 6 lines · Josh Poimboeuf
read_exports()reports the offending line number when it fails to parse Module.symvers. The counter is initialized to 1 but never incremented, so every error blames line 1 regardless of where the bad line is.Signed-off-by Josh Poimboeuf <[email protected]>
[tip: objtool/core] objtool/klp: Fix line numbers in Module.symvers parse errors
In reply to Josh PoimboeufThe following commit has been merged into the objtool/core branch of tip:
The following commit has been merged into the objtool/core branch of tip:
Commit-ID: 51c1de13486315ad46a74b40ec17124916a08277 Gitweb: https://git.kernel.org/tip/51c1de13486315ad46a74b40ec17124916a08277 Author: Josh Poimboeuf <[email protected]> AuthorDate: Fri, 07 Aug 2026 14:37:54 -07:00 Committer: Josh Poimboeuf <[email protected]> CommitterDate: Tue, 11 Aug 2026 16:10:58 -07:00
objtool/klp: Fix line numbers in Module.symvers parse errors
read_exports() reports the offending line number when it fails to parse Module.symvers. The counter is initialized to 1 but never incremented, so every error blames line 1 regardless of where the bad line is.
--- tools/objtool/klp-diff.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/objtool/klp-diff.c +3 -1
@@ -118,7 +118,7 @@ static int read_exports(void) {const char *symvers ="Module.symvers" ;char line[1024 ], *path =NULL ; -unsigned int line_num =1 ; +unsigned int line_num =0 ;FILE *file; file =fopen (symvers,"r" );
@@ -140,6 +140,8 @@ static int read_exports(void)char *sym, *mod, *type, *namespace;struct export *export; + line_num++; + sym =strchr (line,'\t' );if (!sym) {ERROR ("malformed Module.symvers (sym) at line %d" , line_num);
[tip: objtool/core] objtool/klp: Fix relocations for EXPORT_SYMBOL_FOR_MODULES() symbols
In reply to Josh PoimboeufThe following commit has been merged into the objtool/core branch of tip:
The following commit has been merged into the objtool/core branch of tip:
Commit-ID: 4cd3cfb8b54feca89a682720434092a87bfa4967 Gitweb: https://git.kernel.org/tip/4cd3cfb8b54feca89a682720434092a87bfa4967 Author: Josh Poimboeuf <[email protected]> AuthorDate: Fri, 07 Aug 2026 14:37:53 -07:00 Committer: Josh Poimboeuf <[email protected]> CommitterDate: Tue, 11 Aug 2026 16:10:51 -07:00
objtool/klp: Fix relocations for EXPORT_SYMBOL_FOR_MODULES() symbols
EXPORT_SYMBOL_FOR_MODULES() puts a symbol in a "module:<names>" namespace, which the module loader grants access to by matching the importing module's name against that list.
klp_reloc_needed() only creates a klp reloc for module-owned exports; a vmlinux export gets a normal reloc. For a vmlinux symbol exported with EXPORT_SYMBOL_FOR_MODULES(), using a normal reloc results in a modpost failure in klp-build:
ERROR: modpost: module livepatch-foo uses symbol mpol_shared_policy_lookup from namespace module:kvm, but does not import it.
And the modpost error is correct: even with that error removed, the patch module would fail to load:
livepatch_foo: module uses symbol (mpol_shared_policy_lookup) from namespace module:kvm, but does not import it.
livepatch_foo: Unknown symbol mpol_shared_policy_lookup (err -22)
Treat it like an unexported symbol by using a klp reloc.
Note this only affects "module:" namespaces. Ordinary namespaced exports continue to work with normal relocs thanks to copy_import_ns(), which propagates the patched object's import_ns tags to the patch module.
--- tools/objtool/klp-diff.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-)
tools/objtool/klp-diff.c +23 -5
@@ -30,7 +30,9 @@ struct elfs {struct export {struct hlist_node hash; -char *mod, *sym; +char *mod; +char *sym; +bool mod_ns; };bool debug, debug_correlate, debug_clone;
@@ -135,7 +137,7 @@ static int read_exports(void) }while (fgets (line,1024 , file)) { -char *sym, *mod, *type; +char *sym, *mod, *type, *namespace;struct export *export; sym =strchr (line,'\t' );
@@ -162,6 +164,14 @@ static int read_exports(void) *type++ ='\0' ; + namespace =strchr (type,'\t' ); +if (!namespace) { +ERROR ("malformed Module.symvers (namespace) at line %d" , line_num); +return -1 ; + } + + *namespace++ ='\0' ; +if (*sym =='\0' || *mod =='\0' ) {ERROR ("malformed Module.symvers at line %d" , line_num);return -1 ;
@@ -188,6 +198,9 @@ static int read_exports(void)return -1 ; } +/* EXPORT_SYMBOL_FOR_MODULES() */ + export->mod_ns =strstarts (namespace,"module:" ); +hash_add (exports, &export->hash,str_hash (sym)); }
@@ -1175,11 +1188,16 @@ static bool klp_reloc_needed(struct reloc *patched_reloc)* clusterfunk that is late module patching, the patch module is * allowed to be loaded before any modules it depends on. * -* If exported by vmlinux, a normal reloc will do. +* If exported by vmlinux to all modules, a normal reloc will do. */ export =find_export (patched_sym); -if (export) -return strcmp (export->mod,"vmlinux" ); +if (export) { +if (strcmp (export->mod,"vmlinux" )) +return true; + +/* EXPORT_SYMBOL_FOR_MODULES() gets a klp reloc */ +return export->mod_ns; + }if (!patched_sym->twin) {/*
[tip: objtool/core] objtool/klp: Allow new references to module exports
In reply to Josh PoimboeufThe following commit has been merged into the objtool/core branch of tip:
The following commit has been merged into the objtool/core branch of tip:
Commit-ID: 72d76d0c18ebd40f6a32df875ff40528564849c1 Gitweb: https://git.kernel.org/tip/72d76d0c18ebd40f6a32df875ff40528564849c1 Author: Joe Lawrence <[email protected]> AuthorDate: Fri, 07 Aug 2026 14:37:52 -07:00 Committer: Josh Poimboeuf <[email protected]> CommitterDate: Tue, 11 Aug 2026 16:10:51 -07:00
objtool/klp: Allow new references to module exports
klp_reloc_needed() returns true for module exports to support late-module patching. However, clone_reloc_klp() unconditionally rejects symbols without a twin (i.e., new references added by the patch), even when the symbol is a known export from Module.symvers.
Relax the check: allow new references to exported symbols by only erroring on !twin when there is no export. The export metadata from Module.symvers provides sufficient context to emit the klp-relocation without a twin.
For a module export that isn't sufficient on its own though, as the resulting klp relocation will only be resolved at patch-enable time if the exporting module is loaded.
If the original (unpatched) module already depends on the exporting module, the dependency is safe: the module loader ensures the dependency is satisfied before the patched module can be loaded, so the klp relocation target will exist.
However, if the patch introduces a reference to a module that the original doesn't depend on, there is no such guarantee. The exporting module could be absent or could be unloaded at any time, leading to a relocation failure or use-after-free.
So also add a build-time check: when a new symbol reference (no twin) targets a module export, verify that the original module already has at least one UNDEF symbol resolving to that same exporting module. If not, error out with a diagnostic message.
--- tools/objtool/klp-diff.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-)
tools/objtool/klp-diff.c +33 -2
@@ -1296,6 +1296,28 @@ static int convert_reloc_sym(struct elf *elf, struct reloc *reloc) }/* +* Check if the original module already has a dependency on dep_mod, i.e. it +* already references at least one export from that module. +*/ +static bool has_module_dep (struct elfs *e,const char *dep_mod) +{ +struct symbol *sym; + +for_each_sym (e->orig, sym) { +struct export *exp; + +if (!is_undef_sym (sym) ||is_weak_sym (sym)) +continue ; + + exp =find_export (sym); +if (exp && !strcmp (exp->mod, dep_mod)) +return true; + } + +return false; +} + +/* * Convert a regular relocation to a klp relocation (sort of). */ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,
@@ -1314,8 +1336,17 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,unsigned long sympos;if (!patched_sym->twin) { -ERROR ("unexpected klp reloc for new symbol %s" , patched_sym->name); -return -1 ; +if (!export) { +ERROR ("unexpected klp reloc for new symbol %s" , patched_sym->name); +return -1 ; + } + +if (strcmp (export->mod,"vmlinux" ) && + !has_module_dep (e, export->mod)) { +ERROR ("%s: new reference to %s (exported by %s) would create an undeclared module dependency" , + patched_sym->name, export->sym, export->mod); +return -1 ; + } }/*
[tip: objtool/core] objtool/klp: Don't match local symbols against exports
In reply to Josh PoimboeufThe following commit has been merged into the objtool/core branch of tip:
The following commit has been merged into the objtool/core branch of tip:
Commit-ID: 86a697572c6271083694025eefa8de518eeef623 Gitweb: https://git.kernel.org/tip/86a697572c6271083694025eefa8de518eeef623 Author: Josh Poimboeuf <[email protected]> AuthorDate: Fri, 07 Aug 2026 14:37:51 -07:00 Committer: Josh Poimboeuf <[email protected]> CommitterDate: Tue, 11 Aug 2026 16:10:51 -07:00
objtool/klp: Don't match local symbols against exports
While cloning a reloc, klp diff calls find_export() to determine whether the referenced symbol is exported. That decides whether the reference needs a klp reloc, which object the klp symbol belongs to, and whether the symbol's data needs to be copied into the patch module.
But find_export() matches purely on symbol name, so a static function or variable which happens to share its name with an export is mistaken for a reference to that export:
- klp_reloc_needed() creates a klp reloc pointing at the exporting module's symbol rather than the local one. For a vmlinux export it skips the klp reloc altogether, leaving a normal reloc which the module loader resolves to the vmlinux symbol.
- clone_reloc() treats the symbol as external and clones it without its data, leaving a dangling reference.
- validate_special_section_klp_reloc() attributes a static branch or call key to the wrong module, and for a vmlinux export skips the unsupported-key check entirely.
Exports are always global, so ignore local symbols in find_export().
--- tools/objtool/klp-diff.c | 3 +++ 1 file changed, 3 insertions(+)
tools/objtool/klp-diff.c +3 -0
@@ -1102,6 +1102,9 @@ static struct export *find_export(struct symbol *sym) {struct export *export; +if (is_local_sym (sym)) +return NULL ; +hash_for_each_possible (exports, export, hash,str_hash (sym->name)) {if (!strcmp (export->sym, sym->name))return export;
[tip: objtool/core] objtool/klp: Explicitly disallow patching or referencing init code/data
In reply to Josh PoimboeufThe following commit has been merged into the objtool/core branch of tip:
The following commit has been merged into the objtool/core branch of tip:
Commit-ID: 69f361b8a7a2f65c1bb236ea0899cdaad7267653 Gitweb: https://git.kernel.org/tip/69f361b8a7a2f65c1bb236ea0899cdaad7267653 Author: Josh Poimboeuf <[email protected]> AuthorDate: Fri, 07 Aug 2026 14:37:49 -07:00 Committer: Josh Poimboeuf <[email protected]> CommitterDate: Tue, 11 Aug 2026 16:10:38 -07:00
objtool/klp: Explicitly disallow patching or referencing init code/data
Explicitly disallow the patching and referencing of init code/data. Otherwise it could potentially introduce some odd edge cases depending on whether the target object's init section has been freed yet (note that the init code still exists in the target module when doing late module patching).
Such edge cases include sympos calculation and the patching and/or referencing of non-existent (init-freed) code/data. Not to mention the inherent differences in behavior that occur when the init code is only patched *some* of the time depending on module loading order or kernel config.
--- tools/objtool/klp-sympos.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
tools/objtool/klp-sympos.c +10 -0
@@ -367,6 +367,11 @@ static unsigned long find_vmlinux_sympos(struct symbol *sym)return sympos; } +static bool is_init_sym (struct symbol *sym) +{ +return strstarts (sym->sec->name,".init" ); +} +/* * "sympos" is used by livepatch to disambiguate duplicate symbol names. */
@@ -376,6 +381,11 @@ unsigned long klp_find_sympos(struct elf *elf, struct symbol *sym)bool has_dup = false;struct symbol *s; +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 ;
[tip: objtool/core] objtool/klp: Fix cross-module klp relocation section naming
In reply to Josh PoimboeufThe following commit has been merged into the objtool/core branch of tip:
The following commit has been merged into the objtool/core branch of tip:
Commit-ID: 07f14d6af9d7791fdc2b44a3dd0693e42c71d2b8 Gitweb: https://git.kernel.org/tip/07f14d6af9d7791fdc2b44a3dd0693e42c71d2b8 Author: Josh Poimboeuf <[email protected]> AuthorDate: Fri, 07 Aug 2026 14:37:50 -07:00 Committer: Josh Poimboeuf <[email protected]> CommitterDate: Tue, 11 Aug 2026 16:10:51 -07:00
objtool/klp: Fix cross-module klp relocation section naming
A klp relocation section is .klp.rela.<objname>.<secname>, where objname is the object being patched.
klp-build wrongly derives objname from where the referenced symbol lives, not where it's referenced. For a cross-module reference like patched can_isotp code calling can.ko's can_rx_unregister(), that gives .klp.rela.can..text rather than .klp.rela.can_isotp..text. Unless the patch happens to patch can.ko as well, the relocation never gets applied and the call goes off into the weeds.
Name the intermediate section __klp_relocs.<objname> so post-link can read the patched object's name from there.
--- tools/objtool/include/objtool/klp.h | 10 +++-- tools/objtool/klp-diff.c | 17 +++++++-- tools/objtool/klp-post-link.c | 53 ++++++++++++++++------------ 3 files changed, 52 insertions(+), 28 deletions(-)
tools/objtool/include/objtool/klp.h +7 -3
@@ -14,11 +14,15 @@#define KLP_FUNCS_SEC ".init.klp_funcs"/* -* __klp_relocs is an intermediate section which are created by klp diff and -* converted into KLP symbols/relas by "objtool klp post-link". This is needed -* to work around the linker, which doesn't preserve SHN_LIVEPATCH or +* __klp_relocs.<objname> are intermediate sections which are created by klp +* diff and converted into KLP symbols/relas by "objtool klp post-link". This +* is needed to work around the linker, which doesn't preserve SHN_LIVEPATCH or * SHF_RELA_LIVEPATCH, nor does it support having two RELA sections for a * single PROGBITS section. +* +* "objname" is the name of the object being patched ("vmlinux" or a module +* name). post-link uses it to name the resulting +* .klp.rela.objname.section_name sections. */ #define KLP_RELOCS_SEC "__klp_relocs"#define KLP_STRINGS_SEC ".rodata.klp.str1.1"
tools/objtool/klp-diff.c +14 -3
@@ -1381,8 +1381,8 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc, }/* -* Create the __klp_relocs entry. This will be converted to an actual -* KLP rela by "objtool klp post-link". +* Create the __klp_relocs.<objname> entry. This will be converted to +* an actual KLP rela by "objtool klp post-link". * * This intermediate step is necessary to prevent corruption by the * linker, which doesn't know how to properly handle two rela sections
@@ -1390,7 +1390,18 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,*/ if (!klp_relocs) { - klp_relocs =elf_create_section (e->out,KLP_RELOCS_SEC ,0 , +const char *objname =find_modname (e); +char sec_name[SEC_NAME_LEN ]; + +if (!objname) +return -1 ; + +/* section format: __klp_relocs.objname */ +if (snprintf_check (sec_name,SEC_NAME_LEN , +KLP_RELOCS_SEC ".%s" , objname)) +return -1 ; + + klp_relocs =elf_create_section (e->out, sec_name,0 ,0 ,SHT_PROGBITS ,8 ,SHF_ALLOC );if (!klp_relocs)return -1 ;
tools/objtool/klp-post-link.c +31 -22
98 lines not on this page · raw message
[tip: objtool/core] objtool/klp: Ignore replacement offset of empty x86 alternatives
In reply to Josh PoimboeufThe following commit has been merged into the objtool/core branch of tip:
The following commit has been merged into the objtool/core branch of tip:
Commit-ID: 636f230ce21e2730072c58280f1fa0bed4a00eed Gitweb: https://git.kernel.org/tip/636f230ce21e2730072c58280f1fa0bed4a00eed Author: Josh Poimboeuf <[email protected]> AuthorDate: Fri, 07 Aug 2026 14:37:48 -07:00 Committer: Josh Poimboeuf <[email protected]> CommitterDate: Tue, 11 Aug 2026 15:04:57 -07:00
objtool/klp: Ignore replacement offset of empty x86 alternatives
An x86 alternative with an empty replacement, e.g. the second entry of
ALTERNATIVE_2("orig", "repl", ft1, "", ft2)
has a replacementlen of zero. Its replacement offset still gets a relocation, but the label it points at is the end of the previous replacement, which is also the beginning of the *next* alternative's replacement. The value is meaningless; get_alt_entry() already ignores it for that reason.
klp diff doesn't ignore it. When such an alternative belongs to a changed function, cloning its relocations drags in the unrelated neighboring replacement, along with everything that replacement references. On an x86 clang/lto build an empty alternative in meminfo_proc_show() pulled in the replacement of an alternative in proc_kcore_init(), silently emitting a klp relocation against init text which has long since been freed by the time the patch is applied.
Add arch_alt_ignore_new_reloc() and skip such relocations when cloning. This has to be arch specific: on arm64 a zero-length replacement instead identifies an alternative callback, whose replacement offset points at the callback function and must be preserved.
--- tools/objtool/arch/x86/special.c | 27 ++++++++++++++++++++++++- tools/objtool/include/objtool/special.h | 7 ++++++- tools/objtool/klp-diff.c | 6 ++++- 3 files changed, 39 insertions(+), 1 deletion(-)
tools/objtool/arch/x86/special.c +27 -0
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0-or-later #include <string.h> +#include <arch/special.h> #include <objtool/special.h> #include <objtool/builtin.h> #include <objtool/warn.h>
@@ -9,6 +10,32 @@/* cpu feature name array generated from cpufeatures.h */ #include "cpu-feature-names.c" +/* +* An alternative with an empty replacement, e.g. the second entry of +* +* ALTERNATIVE_2("orig", "repl", ft1, "", ft2) +* +* still gets a relocation for its replacement offset. But the label it points +* at is the end of the previous entry's replacement, which is also the +* beginning of the *next* entry's replacement. The value is meaningless: it's +* only ever used with a length of zero. +*/ +bool arch_alt_ignore_new_reloc (struct section *sec,unsigned long offset) +{ +unsigned long entry_off; + +if (strcmp (sec->name,".altinstructions" )) +return false; + + entry_off = offset - (offset %ALT_ENTRY_SIZE ); + +if (offset - entry_off !=ALT_NEW_OFFSET ) +return false; + +return !*(unsigned char *)(sec->data->d_buf + entry_off + +ALT_NEW_LEN_OFFSET ); +} +void arch_handle_alternative (struct special_alt *alt) {static struct special_alt *group, *prev;
tools/objtool/include/objtool/special.h +7 -0
@@ -32,6 +32,13 @@ int special_get_alts(struct elf *elf, struct list_head *alts);void arch_handle_alternative (struct special_alt *alt); +/* +* Should the reloc at @offset -- the "new" (replacement) field of a special +* section group entry -- be ignored? The meaning of a zero-length replacement +* is arch specific, so the arch decides. +*/ +bool arch_alt_ignore_new_reloc (struct section *sec,unsigned long offset); +bool arch_support_alt_relocation (struct special_alt *special_alt,struct instruction *insn,struct reloc *reloc);
tools/objtool/klp-diff.c +5 -1
@@ -12,7 +12,7 @@#include <objtool/arch.h> #include <objtool/klp.h> #include <objtool/util.h> -#include <arch/special.h> +#include <objtool/special.h> #include <linux/align.h> #include <linux/objtool_types.h>
@@ -1538,6 +1538,10 @@ static int clone_sym_relocs(struct elfs *e, struct symbol *patched_sym) !strcmp(patched_reloc->sym->sec->name, ".altinstr_aux")) continue; +if (arch_alt_ignore_new_reloc (patched_sym->sec, +reloc_offset (patched_reloc))) +continue ; + ret =convert_reloc_sym (e->patched, patched_reloc);if (ret <0 ) { ERROR_FUNC(patched_rsec->base, reloc_offset(patched_reloc),
[tip: objtool/core] objtool/klp: Fix size of empty special section entries
In reply to Josh PoimboeufThe following commit has been merged into the objtool/core branch of tip:
The following commit has been merged into the objtool/core branch of tip:
Commit-ID: 1ea786097cd79522b76cbd41beeb8f84ef3a76f4 Gitweb: https://git.kernel.org/tip/1ea786097cd79522b76cbd41beeb8f84ef3a76f4 Author: Josh Poimboeuf <[email protected]> AuthorDate: Fri, 07 Aug 2026 14:37:47 -07:00 Committer: Josh Poimboeuf <[email protected]> CommitterDate: Tue, 11 Aug 2026 15:04:57 -07:00
objtool/klp: Fix size of empty special section entries
create_fake_symbols() sizes each ANNOTATE_DATA_SPECIAL entry from the offset of the next annotation, falling back to the end of the section for the last entry. But the last entry is detected by a zero size, which also happens for an *empty* entry: ALTERNATIVE(oldinstr, "", ft) still annotates its zero-length replacement, at the same offset as the next entry's annotation.
So every empty replacement gets a fake symbol spanning the entire rest of .altinstr_replacement. That's harmless today only because find_symbol_containing() picks the smaller of two overlapping symbols.
Track whether a next annotation was found rather than inferring it from the size. A zero-length fake symbol is fine: find_symbol_containing() skips those, so the properly sized symbol at the same offset still wins.
--- tools/objtool/klp-diff.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
tools/objtool/klp-diff.c +11 -2
@@ -1628,13 +1628,17 @@ static int create_fake_symbols(struct elf *elf)for_each_reloc (sec->rsec, reloc) {unsigned long offset, size;struct reloc *next_reloc; +bool last = true;if (annotype (elf, sec, reloc) !=ANNOTYPE_DATA_SPECIAL )continue ; offset =reloc_addend (reloc); - size =0 ; +/* +* Find the start of the next entry so the fake symbol size can +* be calculated. +*/ next_reloc = reloc;for_each_reloc_continue (sec->rsec, next_reloc) { if (annotype(elf, sec, next_reloc) != ANNOTYPE_DATA_SPECIAL ||
@@ -1642,10 +1646,15 @@ static int create_fake_symbols(struct elf *elf)continue ; size =reloc_addend (next_reloc) - offset; + last = false;break ; } -if (!size) +/* +* If no next entry found, this is the last entry, so its size +* is from the current offset to the end of the section. +*/ +if (last) size =sec_size (reloc->sym->sec) - offset;if (create_fake_symbol (elf, reloc->sym->sec, offset, size))
[tip: objtool/core] objtool/klp: Fix vmlinux .klp.symid link error for .no_trim_symbol symbols
In reply to Josh PoimboeufThe following commit has been merged into the objtool/core branch of tip:
The following commit has been merged into the objtool/core branch of tip:
Commit-ID: 3a83d9cd3d0e66c0ef99ce84f972ed26f50c990f Gitweb: https://git.kernel.org/tip/3a83d9cd3d0e66c0ef99ce84f972ed26f50c990f Author: Josh Poimboeuf <[email protected]> AuthorDate: Fri, 07 Aug 2026 14:37:46 -07:00 Committer: Josh Poimboeuf <[email protected]> CommitterDate: Tue, 11 Aug 2026 15:04:57 -07:00
objtool/klp: Fix vmlinux .klp.symid link error for .no_trim_symbol symbols
Testing klp-build with arm64 produced the following linker error during the original kernel build:
__notrim.1' referenced in section .klp.symid' of vmlinux.o: defined in discarded section `.no_trim_symbol' of vmlinux.o
symbol_get() puts a static __notrim[] in .no_trim_symbol, which GCC names __notrim.1, __notrim.2, etc. Two or more built-in translation units calling symbol_get() thus produce duplicate names, resulting in corresponding .klp.symid references which trigger the above error.
Add .no_trim_symbol to the discarded section list so its symbols don't get symids.
Note this issue is not specific to arm64: it just needs two built-in symbol_get() callers. arm64 trips over it easily because it has KVM always compiled in vmlinux, whereas on x86 it's typically a module.
--- tools/objtool/klp-symid.c | 1 + 1 file changed, 1 insertion(+)
tools/objtool/klp-symid.c +1 -0
@@ -31,6 +31,7 @@static const char *const discarded_secs[] = {".discard" ,".modinfo" , +".no_trim_symbol" ,"__tracepoint_check" , };
Re: [PATCH v3 8/9] objtool/klp: Fix relocations for EXPORT_SYMBOL_FOR_MODULES() symbols
In reply to Josh PoimboeufHi Josh,
Quoted 103 lines · Josh Poimboeuf
EXPORT_SYMBOL_FOR_MODULES()puts a symbol in a "module:<names>" namespace, which the module loader grants access to by matching the importing module's name against that list.
klp_reloc_needed()only creates a klp reloc for module-owned exports; a vmlinux export gets a normal reloc. For a vmlinux symbol exported withEXPORT_SYMBOL_FOR_MODULES(), using a normal reloc results in a modpost failure in klp-build:ERROR: modpost: module livepatch-foo uses symbol
mpol_shared_policy_lookupfrom namespace module:kvm, but does not import it.And the modpost error is correct: even with that error removed, the patch module would fail to load:
livepatch_foo: module uses symbol (mpol_shared_policy_lookup) from namespace module:kvm, but does not import it.
livepatch_foo: Unknown symbolmpol_shared_policy_lookup(err -22)Treat it like an unexported symbol by using a klp reloc.
Note this only affects "module:" namespaces. Ordinary namespaced exports continue to work with normal relocs thanks to
copy_import_ns(), which propagates the patched object'simport_nstags to the patch module.Fixes dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")Reported-by Joe Lawrence <[email protected]>Signed-off-by Josh Poimboeuf <[email protected]>--- tools/objtool/klp-diff.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-)tools/objtool/klp-diff.c +23
-6@@ -30,7 +30,9 @@ struct elfs {struct export {struct hlist_node hash; -char *mod, *sym; +char *mod; +char *sym; +bool mod_ns; };bool debug, debug_correlate, debug_clone;@@ -135,7 +137,7 @@ static int read_exports(void) }while (fgets (line,1024 , file)) { -char *sym, *mod, *type; +char *sym, *mod, *type, *namespace;struct export *export; sym =strchr (line,'\t' );@@ -162,6 +164,14 @@ static int read_exports(void) *type++ ='\0' ; + namespace =strchr (type,'\t' ); +if (!namespace) { +ERROR ("malformed Module.symvers (namespace) at line %d" , line_num); +return -1 ; + } + + *namespace++ ='\0' ; +if (*sym =='\0' || *mod =='\0' ) {ERROR ("malformed Module.symvers at line %d" , line_num);return -1 ;@@ -188,6 +198,9 @@ static int read_exports(void)return -1 ; } +/* EXPORT_SYMBOL_FOR_MODULES() */ + export->mod_ns =strstarts (namespace,"module:" ); +hash_add (exports, &export->hash,str_hash (sym)); }@@ -1174,11 +1187,16 @@ static bool klp_reloc_needed(struct reloc *patched_reloc)* clusterfunk that is late module patching, the patch module is * allowed to be loaded before any modules it depends on. * -* If exported by vmlinux, a normal reloc will do. +* If exported by vmlinux to all modules, a normal reloc will do. */ export =find_export (patched_sym); -if (export) -return strcmp (export->mod,"vmlinux" ); +if (export) { +if (strcmp (export->mod,"vmlinux" )) +return true; + +/* EXPORT_SYMBOL_FOR_MODULES() gets a klp reloc */ +return export->mod_ns; + }if (!patched_sym->twin) {/* --
Quoted 3 lines
2.54.0
Following up on the other thread [1], I noticed that when a patch is touching a module function with a reference to one of these module-exported symbols, the patch/module is rejected because KLP relocs referencing vmlinux symbols are not allowed from module-specific livepatch relocation sections. I was able to reproduce this with a simple module/livepatch combo that depends on one of these symbols [2] (see samples/livepatch/testmod.c and test.patch):
root@debian-vm:~$ insmod livepatch-test.ko root@debian-vm:~$ insmod testmod.ko insmod: ERROR: could not insert module testmod.ko: Invalid parameters
With dmesg:
[ 655.596876] livepatch_test: loading out-of-tree module taints kernel. [ 655.600961] livepatch_test: tainting kernel with TAINT_LIVEPATCH [ 655.605436] livepatch: enabling patch 'livepatch_test' [ 655.609119] livepatch: 'livepatch_test': starting patching transition [ 656.653454] livepatch: 'livepatch_test': patching complete [ 738.777872] livepatch: invalid access to vmlinux symbol 'get_task_policy' from module-specific livepatch relocation section [ 738.784899] livepatch: failed to initialize patch 'livepatch_test' for module 'testmod' (-22) [ 738.790371] livepatch: patch 'livepatch_test' failed for module
'testmod', refusing to load module 'testmod'
Do you recommend a strategy for working around this, or is this something that would have to be fixed in the kernel?
Refs:
[1]: https://lore.kernel.org/all/CADBMgpyY6R_YmEqhZSw=88-Bfkv9=s-N-x1siE1uPAgH_fTURw@mail.gmail.com/
[2]: https://github.com/dylanbhatch/linux/tree/mod-ns-lp
Thanks,
Dylan
Josh Poimboeuf
maintainerRe: [PATCH v3 8/9] objtool/klp: Fix relocations for EXPORT_SYMBOL_FOR_MODULES() symbols
Quoted 23 lines · Dylan Hatch
Following up on the other thread [1], I noticed that when a patch is touching a module function with a reference to one of these module-exported symbols, the patch/module is rejected because KLP relocs referencing vmlinux symbols are not allowed from module-specific livepatch relocation sections. I was able to reproduce this with a simple module/livepatch combo that depends on one of these symbols [2] (see
samples/livepatch/testmod.cand test.patch):root@debian-vm:~$ insmod livepatch-test.ko root@debian-vm:~$ insmod testmod.ko insmod: ERROR: could not insert module testmod.ko: Invalid parametersWith dmesg:
[ 655.596876] livepatch_test: loading out-of-tree module taints kernel. [ 655.600961] livepatch_test: tainting kernel with TAINT_LIVEPATCH [ 655.605436] livepatch: enabling patch 'livepatch_test' [ 655.609119] livepatch: 'livepatch_test': starting patching transition [ 656.653454] livepatch: 'livepatch_test': patching complete [ 738.777872] livepatch: invalid access to vmlinux symbol 'get_task_policy' from module-specific livepatch relocation section [ 738.784899] livepatch: failed to initialize patch 'livepatch_test' for module 'testmod' (-22) [ 738.790371] livepatch: patch 'livepatch_test' failed for module
Quoted 4 lines
'testmod', refusing to load module 'testmod'
Do you recommend a strategy for working around this, or is this something that would have to be fixed in the kernel?
Ah, this is another tooling issue, let me work up a patch.
Signature · 1 line
Josh
Josh Poimboeuf
maintainerRe: [PATCH v3 8/9] objtool/klp: Fix relocations for EXPORT_SYMBOL_FOR_MODULES() symbols
Quoted 26 lines · Josh Poimboeuf
Quoted 25 lines · Dylan Hatch
Following up on the other thread [1], I noticed that when a patch is touching a module function with a reference to one of these module-exported symbols, the patch/module is rejected because KLP relocs referencing vmlinux symbols are not allowed from module-specific livepatch relocation sections. I was able to reproduce this with a simple module/livepatch combo that depends on one of these symbols [2] (see
samples/livepatch/testmod.cand test.patch):root@debian-vm:~$ insmod livepatch-test.ko root@debian-vm:~$ insmod testmod.ko insmod: ERROR: could not insert module testmod.ko: Invalid parametersWith dmesg:
[ 655.596876] livepatch_test: loading out-of-tree module taints kernel. [ 655.600961] livepatch_test: tainting kernel with TAINT_LIVEPATCH [ 655.605436] livepatch: enabling patch 'livepatch_test' [ 655.609119] livepatch: 'livepatch_test': starting patching transition [ 656.653454] livepatch: 'livepatch_test': patching complete [ 738.777872] livepatch: invalid access to vmlinux symbol 'get_task_policy' from module-specific livepatch relocation section [ 738.784899] livepatch: failed to initialize patch 'livepatch_test' for module 'testmod' (-22) [ 738.790371] livepatch: patch 'livepatch_test' failed for module'testmod', refusing to load module 'testmod'
Quoted 4 lines
Quoted 2 lines
Do you recommend a strategy for working around this, or is this something that would have to be fixed in the kernel?
Ah, this is another tooling issue, let me work up a patch.
Here is an untested diff, I'll post a proper patch once I get a chance to test it.
tools/objtool/include/objtool/klp.h +3 -2
@@ -20,8 +20,9 @@* SHF_RELA_LIVEPATCH, nor does it support having two RELA sections for a * single PROGBITS section. * -* "objname" is the name of the object being patched ("vmlinux" or a module -* name). post-link uses it to name the resulting +* "objname" is the object whose loading gates the relocation: "vmlinux" for +* references to vmlinux symbols, otherwise the name of the module being +* patched. post-link uses it to name the resulting * .klp.rela.objname.section_name sections. */ #define KLP_RELOCS_SEC "__klp_relocs"
tools/objtool/klp-diff.c +22 -13
@@ -1344,13 +1344,14 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc, struct section *sec, unsigned long offset, struct export *export) { + const char *sym_modname, *sym_orig_name, *sec_objname;struct symbol *patched_sym = patched_reloc->sym;s64 addend =reloc_addend (patched_reloc); -const char *sym_modname, *sym_orig_name; -static struct section *klp_relocs;char tombstone_name[SYM_NAME_LEN ];struct symbol *sym, *klp_sym;unsigned long klp_reloc_off; +struct section *klp_relocs; +char sec_name[SEC_NAME_LEN ];char sym_name[SYM_NAME_LEN ];struct klp_reloc klp_reloc;unsigned long sympos;
@@ -1441,20 +1442,28 @@ static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc,* This intermediate step is necessary to prevent corruption by the * linker, which doesn't know how to properly handle two rela sections * applying to the same base section. +* +* The objname decides when the reloc gets applied. A reference to a +* vmlinux symbol goes in the vmlinux section so it gets applied when +* the patch module loads. Everything else goes in the patched +* object's section, applied when the patched module is loaded. */ +if (!strcmp (sym_modname,"vmlinux" )) { + sec_objname ="vmlinux" ; + }else { + sec_objname =find_modname (e); +if (!sec_objname) +return -1 ; + } + +/* section format: __klp_relocs.objname */ +if (snprintf_check (sec_name,SEC_NAME_LEN , +KLP_RELOCS_SEC ".%s" , sec_objname)) +return -1 ; + + klp_relocs =find_section_by_name (e->out, sec_name);if (!klp_relocs) { -const char *objname =find_modname (e); -char sec_name[SEC_NAME_LEN ]; - -if (!objname) -return -1 ; - -/* section format: __klp_relocs.objname */ -if (snprintf_check (sec_name,SEC_NAME_LEN , -KLP_RELOCS_SEC ".%s" , objname)) -return -1 ; - klp_relocs =elf_create_section (e->out, sec_name,0 ,0 ,SHT_PROGBITS ,8 ,SHF_ALLOC );if (!klp_relocs)