perf dso: Fix kallsyms DSO detection with fallback logic
PATCH v3 1/1 perf dso: Fix kallsyms DSO detection with fallback logic
The current kallsyms detection in dso__is_kallsyms() uses the dso_binary_type enum which fixes the issue of kallsyms being cached in the build-id cache for out-of-tree modules.
However, during build-id injection in perf record/inject, dso_binary_type has not been explicitly set yet,so dso__binary_type() returns DSO_BINARY_TYPE__NOT_FOUND instead of DSO_BINARY_TYPE__KALLSYMS for the kernel DSO. The current check then fails to identify it as kallsyms, causing build-id symlinks to not be created in ~/.debug/.build-id/ and perf archive to fail with "Cannot stat" errors.
Steps to reproduce the issue:
1. rm -rf ~/.debug/.build-id
2. perf record sleep 1
3. perf archive
Fix by falling back to matching long_name against the known kallsyms strings explicitly when binary_type is not yet set
(== DSO_BINARY_TYPE__NOT_FOUND). Use strcmp() for exact matching of fixed names and strict validation for guest kallsyms with embedded PID to prevent path traversal attacks.
---
v2 -> v3: Replace strncmp() prefix matching with strcmp() for fixed
kallsyms names and add is_guest_kallsyms_pid_name() to strictly validate guest kallsyms with PID format, preventing path traversal attacks.
v1 -> v2: Rename DSO__NAME_GUEST_KALLSYMS to DSO__PREFIX_GUEST_KALLSYMS
to reflect that it is a prefix, not a full name.
v1: https://lore.kernel.org/all/[email protected]/
tools/perf/util/dso.h | 57 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-)
tools/perf/util/dso.h +56 -1
@@ -9,6 +9,7 @@#include <stdbool.h> #include <stdio.h> #include <linux/bitops.h> +#include <string.h> #include "build-id.h" #include "debuginfo.h" #include "mutex.h"
@@ -20,6 +21,40 @@ struct perf_env;#define DSO__NAME_KALLSYMS "[kernel.kallsyms]"#define DSO__NAME_KCORE "[kernel.kcore]" +#define DSO__NAME_GUEST_KALLSYMS "[guest.kernel.kallsyms]" +#define DSO__NAME_GUEST_KALLSYMS_PID_PREFIX "[guest.kernel.kallsyms." + +/* +* Validate names of the form "[guest.kernel.kallsyms.<pid>]", where +* <pid> is the PID of the guest VM and varies per guest, so it +* cannot be matched with strcmp() against a fixed string. +* +* Every character after the fixed prefix must be a decimal digit, +* with ']' immediately terminating the digit run and nothing +* following it. This rules out '/', "..", or any other character +* being smuggled into the name. +*/ +static inline bool is_guest_kallsyms_pid_name (const char *name) +{ +const size_t prefix_len =sizeof (DSO__NAME_GUEST_KALLSYMS_PID_PREFIX ) -1 ; +size_t digits; + +if (strncmp (name,DSO__NAME_GUEST_KALLSYMS_PID_PREFIX , prefix_len) !=0 ) +return false; + + digits =strspn (name + prefix_len,"0123456789" ); +if (digits ==0 ) +return false; + +/* ']' must terminate the digit run, with nothing trailing it */ +if (name[prefix_len + digits] !=']' ) +return false; + +if (name[prefix_len + digits +1 ] !='\0' ) +return false; + +return true; +}/** * enum dso_binary_type - The kind of DSO generally associated with a memory
@@ -914,8 +949,28 @@ static inline bool dso__is_kcore(const struct dso *dso)static inline bool dso__is_kallsyms (const struct dso *dso) {enum dso_binary_type bt =dso__binary_type (dso); +const char *name; + +if (bt ==DSO_BINARY_TYPE__KALLSYMS || bt ==DSO_BINARY_TYPE__GUEST_KALLSYMS ) +return true; + +if (bt !=DSO_BINARY_TYPE__NOT_FOUND ) +return false; + +if (!RC_CHK_ACCESS (dso)->kernel) +return false; + + name =RC_CHK_ACCESS (dso)->long_name; +if (!name) +return false; + +if (!strcmp (name,DSO__NAME_KALLSYMS )) +return true; + +if (!strcmp (name,DSO__NAME_GUEST_KALLSYMS )) +return true; -return bt ==DSO_BINARY_TYPE__KALLSYMS || bt ==DSO_BINARY_TYPE__GUEST_KALLSYMS ; +return is_guest_kallsyms_pid_name (name); }bool dso__is_object_file (const struct dso *dso);
Signature · 1 line
2.47.3
[email protected]
AI reviewThank 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] Use-After-Free vulnerability due to lockless access of dso->long_name in dso__is_kallsyms. --
commit 9c2571a16a38ac5caaaeb737772bc4cd229c357c
Author: Tanushree Shah <[email protected]>
perf dso: Fix kallsyms DSO detection with fallback logic
This commit fixes an issue where kallsyms build-id symlinks fail to be created during perf record/inject because the dso_binary_type is not yet explicitly set. It falls back to matching the long_name against known kallsyms strings when the binary type is not found. A strict validation function is also added for guest kallsyms to prevent path traversal attacks.
Quoted 4 lines
tools/perf/util/dso.h +0
-0
[ ... ]
Quoted 11 lines
tools/perf/util/dso.h +7
-0@@ -924,8 +959,28 @@ static inline bool dso__is_kcore(const struct dso *dso)static inline bool dso__is_kallsyms (const struct dso *dso) {enum dso_binary_type bt =dso__binary_type (dso); +const char *name; + +if (bt ==DSO_BINARY_TYPE__KALLSYMS || bt ==DSO_BINARY_TYPE__GUEST_KALLSYMS ) +return true; + +if (bt !=DSO_BINARY_TYPE__NOT_FOUND ) +return false;
tools/perf/util/dso.h static inline bool dso__is_kcore(const struct dso *dso) · line 978
Quoted 10 lines
tools/perf/util/dso.h +10
-0+ +if (!RC_CHK_ACCESS (dso)->kernel) +return false; + + name =RC_CHK_ACCESS (dso)->long_name; +if (!name) +return false; + +if (!strcmp (name,DSO__NAME_KALLSYMS )) +return true;
[Severity: High]
This isn't a bug introduced by this patch, but lockless access to dso->long_name is a systemic flaw that could lead to a use-after-free here.
If a concurrent thread modifies the DSO (e.g., the event processing thread in perf top handling a ksymbol register event calls machine__process_ksymbol_register() -> dso__set_long_name()), the old long_name can be freed while the UI thread is executing dso__is_kallsyms() and performing this strcmp().
Is it safe to expand the surface area of this pre-existing lockless string dereference into a function that was previously safe?
Quoted 9 lines
tools/perf/util/dso.h +4
-1+ +if (!strcmp (name,DSO__NAME_GUEST_KALLSYMS )) +return true; -return bt ==DSO_BINARY_TYPE__KALLSYMS || bt ==DSO_BINARY_TYPE__GUEST_KALLSYMS ; +return is_guest_kallsyms_pid_name (name); }bool dso__is_object_file (const struct dso *dso);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Thanks for the review.
Quoted 62 lines · On 26/06/26 22:01, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Use-After-Free vulnerability due to lockless access ofdso->long_nameindso__is_kallsyms. --commit
9c2571a16a38ac5caaaeb737772bc4cd229c357c
Author: Tanushree Shah <[email protected]>perf dso: Fix kallsyms DSO detection with fallback logic
This commit fixes an issue where kallsyms build-id symlinks fail to be created during perf record/inject because the
dso_binary_typeis not yet explicitly set. It falls back to matching thelong_nameagainst known kallsyms strings when the binary type is not found. A strict validation function is also added for guest kallsyms to prevent path traversal attacks.Quoted 4 lines
tools/perf/util/dso.h +0
-0[ ... ]
Quoted 11 lines
tools/perf/util/dso.h +7
-0@@ -924,8 +959,28 @@ static inline bool dso__is_kcore(const struct dso *dso)static inline bool dso__is_kallsyms (const struct dso *dso) {enum dso_binary_type bt =dso__binary_type (dso); +const char *name; + +if (bt ==DSO_BINARY_TYPE__KALLSYMS || bt ==DSO_BINARY_TYPE__GUEST_KALLSYMS ) +return true; + +if (bt !=DSO_BINARY_TYPE__NOT_FOUND ) +return false;tools/perf/util/dso.h static inline bool dso__is_kcore(const struct dso *dso) · line 978
Quoted 10 lines
tools/perf/util/dso.h +10
-0+ +if (!RC_CHK_ACCESS (dso)->kernel) +return false; + + name =RC_CHK_ACCESS (dso)->long_name; +if (!name) +return false; + +if (!strcmp (name,DSO__NAME_KALLSYMS )) +return true;[Severity: High]
This isn't a bug introduced by this patch, but lockless access todso->long_nameis a systemic flaw that could lead to a use-after-free here.If a concurrent thread modifies the DSO (e.g., the event processing thread in perf top handling a ksymbol register event calls
machine__process_ksymbol_register()->dso__set_long_name()), the oldlong_namecan be freed while the UI thread is executingdso__is_kallsyms()and performing thisstrcmp().Is it safe to expand the surface area of this pre-existing lockless string dereference into a function that was previously safe?
Quoted 4 lines
tools/perf/util/dso.h +3
-0+ +if (!strcmp (name,DSO__NAME_GUEST_KALLSYMS )) +return true;
Quoted 6 lines
tools/perf/util/dso.h static inline bool dso__is_kcore(const struct dso *dso) · line 976
Quoted 5 lines
tools/perf/util/dso.h +1
-1-return bt ==DSO_BINARY_TYPE__KALLSYMS || bt ==DSO_BINARY_TYPE__GUEST_KALLSYMS ; +return is_guest_kallsyms_pid_name (name); }bool dso__is_object_file (const struct dso *dso);
Since this is a pre-existing issue and outside the scope of this change, I will take this up separately.
Thanks
Tanushree Shah
Namhyung Kim
maintainerQuoted 92 lines · Tanushree Shah
The current kallsyms detection in
dso__is_kallsyms()uses thedso_binary_typeenum which fixes the issue of kallsyms being cached in the build-id cache for out-of-tree modules.However, during build-id injection in perf record/inject,
dso_binary_typehas not been explicitly set yet,sodso__binary_type()returnsDSO_BINARY_TYPE__NOT_FOUNDinstead ofDSO_BINARY_TYPE__KALLSYMSfor the kernel DSO. The current check then fails to identify it as kallsyms, causing build-id symlinks to not be created in ~/.debug/.build-id/ and perf archive to fail with "Cannot stat" errors.Steps to reproduce the issue:
1. rm -rf ~/.debug/.build-id
2. perf record sleep 1
3. perf archiveFix by falling back to matching
long_nameagainst the known kallsyms strings explicitly whenbinary_typeis not yet set
(==DSO_BINARY_TYPE__NOT_FOUND). Usestrcmp()for exact matching of fixed names and strict validation for guest kallsyms with embedded PID to prevent path traversal attacks.Fixes ebf0b332732d ("perf dso: fix dso__is_kallsyms() check")Signed-off-by Tanushree Shah <[email protected]>---
v2 -> v3: Replacestrncmp()prefix matching withstrcmp()for fixed
kallsyms names and addis_guest_kallsyms_pid_name()to strictly validate guest kallsyms with PID format, preventing path traversal attacks.v1 -> v2: Rename
DSO__NAME_GUEST_KALLSYMStoDSO__PREFIX_GUEST_KALLSYMS
to reflect that it is a prefix, not a full name.v1: https://lore.kernel.org/all/[email protected]/
tools/perf/util/dso.h | 57 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-)tools/perf/util/dso.h +35
-0@@ -9,6 +9,7 @@#include <stdbool.h> #include <stdio.h> #include <linux/bitops.h> +#include <string.h> #include "build-id.h" #include "debuginfo.h" #include "mutex.h" @@ -20,6 +21,40 @@ struct perf_env;#define DSO__NAME_KALLSYMS "[kernel.kallsyms]"#define DSO__NAME_KCORE "[kernel.kcore]" +#define DSO__NAME_GUEST_KALLSYMS "[guest.kernel.kallsyms]" +#define DSO__NAME_GUEST_KALLSYMS_PID_PREFIX "[guest.kernel.kallsyms." + +/* +* Validate names of the form "[guest.kernel.kallsyms.<pid>]", where +* <pid> is the PID of the guest VM and varies per guest, so it +* cannot be matched with strcmp() against a fixed string. +* +* Every character after the fixed prefix must be a decimal digit, +* with ']' immediately terminating the digit run and nothing +* following it. This rules out '/', "..", or any other character +* being smuggled into the name. +*/ +static inline bool is_guest_kallsyms_pid_name (const char *name) +{ +const size_t prefix_len =sizeof (DSO__NAME_GUEST_KALLSYMS_PID_PREFIX ) -1 ; +size_t digits; + +if (strncmp (name,DSO__NAME_GUEST_KALLSYMS_PID_PREFIX , prefix_len) !=0 ) +return false; + + digits =strspn (name + prefix_len,"0123456789" ); +if (digits ==0 ) +return false; + +/* ']' must terminate the digit run, with nothing trailing it */ +if (name[prefix_len + digits] !=']' ) +return false; + +if (name[prefix_len + digits +1 ] !='\0' ) +return false; + +return true; +}/** * enum dso_binary_type - The kind of DSO generally associated with a memory
tools/perf/util/dso.h static inline bool dso__is_kcore(const struct dso *dso) · line 951
Quoted 3 lines
tools/perf/util/dso.h +0
-0@@ -914,8 +949,28 @@ static inline bool dso__is_kcore(const struct dso *dso)static inline bool dso__is_kallsyms (const struct dso *dso) {enum dso_binary_type bt =dso__binary_type (dso);
I have to check its usage carefully but any chance dso__symtab_type(dso) instead produces better results?
tools/perf/util/dso.h static inline bool dso__is_kcore(const struct dso *dso) · line 960
Quoted 9 lines
tools/perf/util/dso.h +9
-0+const char *name; + +if (bt ==DSO_BINARY_TYPE__KALLSYMS || bt ==DSO_BINARY_TYPE__GUEST_KALLSYMS ) +return true; + +if (bt !=DSO_BINARY_TYPE__NOT_FOUND ) +return false; + +if (!RC_CHK_ACCESS (dso)->kernel)
I think the proper wrapper is dso__kernel().
tools/perf/util/dso.h static inline bool dso__is_kcore(const struct dso *dso) · line 963
Quoted 3 lines
tools/perf/util/dso.h +3
-0+return false; + + name =RC_CHK_ACCESS (dso)->long_name;
And dso__long_name().
Thanks,
Namhyung
Quoted 17 lines
tools/perf/util/dso.h +9
-1+if (!name) +return false; + +if (!strcmp (name,DSO__NAME_KALLSYMS )) +return true; + +if (!strcmp (name,DSO__NAME_GUEST_KALLSYMS )) +return true; -return bt ==DSO_BINARY_TYPE__KALLSYMS || bt ==DSO_BINARY_TYPE__GUEST_KALLSYMS ; +return is_guest_kallsyms_pid_name (name); }bool dso__is_object_file (const struct dso *dso);Signature · 1 line
2.47.3
Hello, Thanks for the review.
Quoted 90 lines · Namhyung Kim
Quoted 89 lines · Tanushree Shah
The current kallsyms detection in
dso__is_kallsyms()uses thedso_binary_typeenum which fixes the issue of kallsyms being cached in the build-id cache for out-of-tree modules.However, during build-id injection in perf record/inject,
dso_binary_typehas not been explicitly set yet,sodso__binary_type()returnsDSO_BINARY_TYPE__NOT_FOUNDinstead ofDSO_BINARY_TYPE__KALLSYMSfor the kernel DSO. The current check then fails to identify it as kallsyms, causing build-id symlinks to not be created in ~/.debug/.build-id/ and perf archive to fail with "Cannot stat" errors.Steps to reproduce the issue:
1. rm -rf ~/.debug/.build-id
2. perf record sleep 1
3. perf archiveFix by falling back to matching
long_nameagainst the known kallsyms strings explicitly whenbinary_typeis not yet set
(==DSO_BINARY_TYPE__NOT_FOUND). Usestrcmp()for exact matching of fixed names and strict validation for guest kallsyms with embedded PID to prevent path traversal attacks.Fixes ebf0b332732d ("perf dso: fix dso__is_kallsyms() check")Signed-off-by Tanushree Shah <[email protected]>---
v2 -> v3: Replacestrncmp()prefix matching withstrcmp()for fixed
kallsyms names and addis_guest_kallsyms_pid_name()to strictly validate guest kallsyms with PID format, preventing path traversal attacks.v1 -> v2: Rename
DSO__NAME_GUEST_KALLSYMStoDSO__PREFIX_GUEST_KALLSYMS
to reflect that it is a prefix, not a full name.v1: https://lore.kernel.org/all/[email protected]/
tools/perf/util/dso.h | 57 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-)tools/perf/util/dso.h +35
-0@@ -9,6 +9,7 @@#include <stdbool.h> #include <stdio.h> #include <linux/bitops.h> +#include <string.h> #include "build-id.h" #include "debuginfo.h" #include "mutex.h" @@ -20,6 +21,40 @@ struct perf_env;#define DSO__NAME_KALLSYMS "[kernel.kallsyms]"#define DSO__NAME_KCORE "[kernel.kcore]" +#define DSO__NAME_GUEST_KALLSYMS "[guest.kernel.kallsyms]" +#define DSO__NAME_GUEST_KALLSYMS_PID_PREFIX "[guest.kernel.kallsyms." + +/* +* Validate names of the form "[guest.kernel.kallsyms.<pid>]", where +* <pid> is the PID of the guest VM and varies per guest, so it +* cannot be matched with strcmp() against a fixed string. +* +* Every character after the fixed prefix must be a decimal digit, +* with ']' immediately terminating the digit run and nothing +* following it. This rules out '/', "..", or any other character +* being smuggled into the name. +*/ +static inline bool is_guest_kallsyms_pid_name (const char *name) +{ +const size_t prefix_len =sizeof (DSO__NAME_GUEST_KALLSYMS_PID_PREFIX ) -1 ; +size_t digits; + +if (strncmp (name,DSO__NAME_GUEST_KALLSYMS_PID_PREFIX , prefix_len) !=0 ) +return false; + + digits =strspn (name + prefix_len,"0123456789" ); +if (digits ==0 ) +return false; + +/* ']' must terminate the digit run, with nothing trailing it */ +if (name[prefix_len + digits] !=']' ) +return false; + +if (name[prefix_len + digits +1 ] !='\0' ) +return false; + +return true; +}
Quoted 10 lines
tools/perf/util/dso.h static inline bool dso__is_kcore(const struct dso *dso) · line 951
Quoted 6 lines
tools/perf/util/dso.h +0
-0/** * enum dso_binary_type - The kind of DSO generally associated with a memory @@ -914,8 +949,28 @@ static inline bool dso__is_kcore(const struct dso *dso)static inline bool dso__is_kallsyms (const struct dso *dso) {enum dso_binary_type bt =dso__binary_type (dso);I have to check its usage carefully but any chance
dso__symtab_type(dso)instead produces better results?
I did some additional logging and found that symtab_type is not getting set while running perf record, so it seems dso__symtab_type(dso) would likely have the same issue here.
Quoted 13 lines
tools/perf/util/dso.h static inline bool dso__is_kcore(const struct dso *dso) · line 960
Quoted 10 lines
tools/perf/util/dso.h +9
-0+const char *name; + +if (bt ==DSO_BINARY_TYPE__KALLSYMS || bt ==DSO_BINARY_TYPE__GUEST_KALLSYMS ) +return true; + +if (bt !=DSO_BINARY_TYPE__NOT_FOUND ) +return false; + +if (!RC_CHK_ACCESS (dso)->kernel)I think the proper wrapper is
dso__kernel().
Thanks for the pointer. Will use this and send v4.
Quoted 6 lines
tools/perf/util/dso.h static inline bool dso__is_kcore(const struct dso *dso) · line 963
Quoted 4 lines
tools/perf/util/dso.h +3
-0+return false; + + name =RC_CHK_ACCESS (dso)->long_name;And
dso__long_name().
Sure, will use this and send v4.
Quoted 14 lines
Thanks,
NamhyungQuoted 9 lines
tools/perf/util/dso.h +8
-0+if (!name) +return false; + +if (!strcmp (name,DSO__NAME_KALLSYMS )) +return true; + +if (!strcmp (name,DSO__NAME_GUEST_KALLSYMS )) +return true;
Quoted 9 lines
Quoted 8 lines
tools/perf/util/dso.h +1
-1-return bt ==DSO_BINARY_TYPE__KALLSYMS || bt ==DSO_BINARY_TYPE__GUEST_KALLSYMS ; +return is_guest_kallsyms_pid_name (name); }bool dso__is_object_file (const struct dso *dso);Signature · 1 line
2.47.3
Thanks
Tanushree Shah