Skip to contentSkip to conversation

perf symbols: ignore livepatch symbols

PATCH Superseded Joe Lawrence 3 participants 3 messages Jun 24 – Jun 25

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Joe Lawrence

author maintainer
#0 lore raw

PATCH 1/1 perf symbols: ignore livepatch symbols

Livepatch modules contain special symbols (prefixed by ".klp.sym.") that act as relocation placeholders. Once resolved, they point to the same addresses as the original kernel symbols they reference. [1]

These special symbols confuse the 'vmlinux symtab matches kallsyms' perf test as kallsyms may report multiple symbols sharing a single kernel address. For example:

kallsyms (without livepatch)
----------------------------
ffffffff81a41110 T __pfx_arch_release_task_struct

Quoted 1 line

ffffffff81a41120 T arch_release_task_struct

ffffffff81a41140 T __pfx_exit_thread
ffffffff81a41150 T exit_thread

kallsyms (with livepatch loaded)
---------------------------------
ffffffff81a41110 T __pfx_arch_release_task_struct

Quoted 1 line

ffffffff81a41120 T arch_release_task_struct

ffffffff81a41140 T __pfx_exit_thread
ffffffff81a41150 T exit_thread

Quoted 1 line

ffffffff81a41120 w .klp.sym.vmlinux.arch_release_task_struct,0 [kpatch_5_14_0_570_94_1_1_3]

When perf loads kallsyms, both symbols are inserted into the symbol table at the same address, corrupting symbol end-address calculations and causing test failures.

Filter out symbols prefixed with ".klp.sym." when loading kallsyms, as they alias existing kernel symbols.

---
 tools/perf/util/symbol.c | 4 ++++
 1 file changed, 4 insertions(+)

From Ben's tests:

Without patch:
./tools/perf/perf test 1
1: vmlinux symtab matches kallsyms : FAILED!

With patch:
./tools/perf/perf test 1
1: vmlinux symtab matches kallsyms : Ok

tools/perf/util/symbol.c +4 -0
index fcaeeddbbb6b..eeaed3f5cf50 100644
@@ -774,6 +774,10 @@ static int map__process_kallsym_symbol(void *arg, const char *name,
 	if (name[0] == '$')
 		return 0;
 
+	/* Ignore livepatch symbols */
+	if (strstarts(name, ".klp.sym."))
+		return 0;
+
 	/*
 	 * module symbols are not sorted so we add all
 	 * symbols, setting length to 0, and rely on
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Out-of-bounds write of a null terminator in kallsyms__parse() stack array.
- [High] Livepatch symbols (.klp.sym.*) corrupt module boundaries in kcore_copy, leading to silently omitted module memory and bogus mappings. --

commit 2ebd0b4f62f44ca82022b5aeb4aaa75ac016e938
Author: Joe Lawrence <[email protected]>

perf symbols: ignore livepatch symbols

Livepatch modules contain special symbols (prefixed by ".klp.sym.") that act as relocation placeholders. Once resolved, they point to the same addresses as the original kernel symbols they reference. These special symbols confuse the 'vmlinux symtab matches kallsyms' perf test as kallsyms may report multiple symbols sharing a single kernel address.

tools/perf/util/symbol.c static int map__process_kallsym_symbol(void *arg, const char *name, · line 780

Quoted 12 lines
tools/perf/util/symbol.c +4 -0
index fcaeeddbbb6b9..eeaed3f5cf50d 100644
@@ -774,6 +774,10 @@ static int map__process_kallsym_symbol(void *arg, const char *name,
 	if (name[0] == '$')
 		return 0;
 
+	/* Ignore livepatch symbols */
+	if (strstarts(name, ".klp.sym."))
+		return 0;
+

[Severity: High]
This isn't a bug introduced by this patch, but does kcore_copy__process_kallsyms() need a similar filter for livepatch symbols?

Livepatch symbols contain a module name bracket (e.g., "[kpatch...]") but their address points to the core kernel space. In symbol-elf.c, this appears to trick the module boundary tracking:

tools/perf/util/symbol-elf.c:kcore_copy__process_kallsyms() {
    ...
	if (strchr(name, '[')) {
		if (!kci->first_module_symbol || start < kci->first_module_symbol)
			kci->first_module_symbol = start;
		if (start > kci->last_module_symbol)
			kci->last_module_symbol = start;
		return 0;
	}
    ...
}

Because the livepatch symbol address is in the core kernel range, does this incorrectly lower kci->first_module_symbol to the core kernel address space instead of the module vmalloc space, omitting module memory and creating a bogus mapping?

[Severity: Critical]
This is a pre-existing issue, but while reviewing the kallsyms parsing I noticed a potential out-of-bounds write when reading symbol names:

tools/lib/symbol/kallsyms.c:kallsyms__parse() {
    ...
		char symbol_name[KSYM_NAME_LEN + 1];

		[ ... ]

		for (i = 0; i < sizeof(symbol_name); i++) {
			ch = io__get_char(&io);
			if (ch < 0 || ch == '\n')
				break;
			symbol_name[i]  = ch;
		}
		symbol_name[i]  = '\0';
    ...
}

If a symbol name in kallsyms is longer than KSYM_NAME_LEN characters and doesn't have a newline or EOF, the loop increments i up to sizeof(symbol_name) (which is KSYM_NAME_LEN + 1). When the loop terminates without breaking, it writes '\0' at symbol_name[sizeof(symbol_name)], which is one byte past the end of the array.

Can this corrupt the stack if an untrusted user provides a crafted --kallsyms file to perf?

Quoted 3 lines
tools/perf/util/symbol.c +0 -0
 	/*
 	 * module symbols are not sorted so we add all
 	 * symbols, setting length to 0, and rely on
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Joe Lawrence
Quoted 52 lines · Joe Lawrence

Livepatch modules contain special symbols (prefixed by ".klp.sym.") that act as relocation placeholders. Once resolved, they point to the same addresses as the original kernel symbols they reference. [1]

These special symbols confuse the 'vmlinux symtab matches kallsyms' perf test as kallsyms may report multiple symbols sharing a single kernel address. For example:

kallsyms (without livepatch)
----------------------------
ffffffff81a41110 T __pfx_arch_release_task_struct

Quoted 1 line

ffffffff81a41120 T arch_release_task_struct

ffffffff81a41140 T __pfx_exit_thread
ffffffff81a41150 T exit_thread

kallsyms (with livepatch loaded)
---------------------------------
ffffffff81a41110 T __pfx_arch_release_task_struct

Quoted 1 line

ffffffff81a41120 T arch_release_task_struct

ffffffff81a41140 T __pfx_exit_thread
ffffffff81a41150 T exit_thread

Quoted 1 line

ffffffff81a41120 w .klp.sym.vmlinux.arch_release_task_struct,0 [kpatch_5_14_0_570_94_1_1_3]

When perf loads kallsyms, both symbols are inserted into the symbol table at the same address, corrupting symbol end-address calculations and causing test failures.

Filter out symbols prefixed with ".klp.sym." when loading kallsyms, as they alias existing kernel symbols.

---
 tools/perf/util/symbol.c | 4 ++++
 1 file changed, 4 insertions(+)
Quoted 1 line

From Ben's tests:

Without patch:
./tools/perf/perf test 1
1: vmlinux symtab matches kallsyms : FAILED!

With patch:
./tools/perf/perf test 1
1: vmlinux symtab matches kallsyms : Ok

tools/perf/util/symbol.c +0 -0
index fcaeeddbbb6b..eeaed3f5cf50 100644

tools/perf/util/symbol.c static int map__process_kallsym_symbol(void *arg, const char *name, · line 775

Quoted 2 lines
tools/perf/util/symbol.c +0 -0
@@ -774,6 +774,10 @@ static int map__process_kallsym_symbol(void *arg, const char *name,
 	if (name[0] == '$')
 		return 0;

It seems that the above checks has been moved into is_ignored_kernel_symbol(), in the current Linus' tree, see
https://lore.kernel.org/lkml/[email protected]/

tools/perf/util/symbol.c static int map__process_kallsym_symbol(void *arg, const char *name, · line 779

Quoted 3 lines
tools/perf/util/symbol.c +3 -0
+	/* Ignore livepatch symbols */
+	if (strstarts(name, ".klp.sym."))
+		return 0;

I guess that this check should be moved there as well.

Nit: It might be possible to include <linux/livepatch_external.h>
and use KLP_SYM_PREFIX instead of ".klp.sym."

tools/perf/util/symbol.c static int map__process_kallsym_symbol(void *arg, const char *name, · line 783

Quoted 4 lines
tools/perf/util/symbol.c +1 -0
+
 	/*
 	 * module symbols are not sorted so we add all
 	 * symbols, setting length to 0, and rely on

Best Regards,
Petr