perf DSO hardening series
Arnaldo Carvalho de Melo
maintainerv5 cover perf DSO hardening series
Hi,
Please consider merging,
Five hardening fixes for the perf DSO data handling code path, all found by the sashiko-bot AI reviewer:
- __open_dso() can return a file descriptor of 0 (stdin) when dso__get_filename() fails without setting errno, so fd = -errno = 0 looked like a successfully opened DSO;
- dso__decompress_kmodule_path() calls close(fd) with fd = -1 when decompression fails or the DSO is not compressed, clobbering errno with EBADF on the error path;
- file_read() and file_size() read the stale global errno after try_to_open_dso() fails, when by then errno has been through mutex_lock(), nsinfo__mountns_enter() and several open() attempts;
- dso_cache__memcpy() underflows when cache->size is smaller than the
RB tree offset (short pread), computing a huge memcpy length;
- dso__read_symbol() asserts on an input-dependent condition instead of doing a runtime check, keeping the BPF metadata safe from crafted input.
Best regards,
- Arnaldo
What changed from v4 (6b06155e7794e6af):
PATCH 1/5:
- Kept errno = ENOENT for the callers that check it after a negative fd, but dso__get_filename()'s chroot fallback now re-stats() and only takes the chroot path when stat() actually failed with ENOENT:
a successful stat() on a non-regular file (e.g. a directory) leaves a stale errno, which the try_to_open_dso() fallback loop could inherit from the forced ENOENT and wrongly send down the chroot path [sashiko-bot review of PATCH 1/5].
PATCH 3/5:
- Dropped the assert(ret < 0) calls and the "fd is always negative" comments added in v3: the enclosing if (dso__data(dso)->fd < 0) already guarantees ret < 0, so they — and the <assert.h> include the asserts required — are gone [Namhyung Kim review].
- All 5 patches: moved Reviewed-by: Ian Rogers above the Assisted-by/Signed-off-by trailers with no blank line, fixing the trailer layout issue seen on the last submission.
What changed from v3 (41ed39bf2dac1d80):
PATCH 3/5:
- Added assert(ret < 0) after ret = dso__data(dso)->fd in both file_read() and file_size() to verify the comment that fd is always negative, never 0 [Ian Rogers review nit].
- All 5 patches carry Reviewed-by: Ian Rogers.
What changed from v2 (2f944ea022b6429f):
PATCH 3/5:
- file_size() now also uses the stored fd error instead of the stale global errno on open failure; the commit was retitled to cover both file_read() and file_size() [sashiko-bot review of PATCH 3/5].
- Comment corrected: on failure dso__data(dso)->fd is always negative — -errno from __open_dso() or -1 from do_open() — never 0.
PATCH 4/5:
- Clarified that returning 0 for an offset past a short-read chunk is EOF semantics — for a regular file a short pread only happens at end-of-file, so 0 is what a direct pread() at that offset would return — not a cache-miss that triggers a re-read; comment and commit message updated [sashiko-bot review of PATCH 4/5].
- The lockless dso cache RB tree lookup vs. concurrent insert question raised in the same review is pre-existing; it is recorded in tools/perf/TODO.hardening (item 175) for follow-up work, with no change to this series.
What changed from v1 ([email protected]):
PATCH 1/5:
- __open_dso() now sets errno = ENOENT directly when dso__get_filename() fails with errno == 0, instead of only computing fd = -ENOENT. Callers that check errno after a negative fd (e.g. file_read() returning -errno) no longer get 0/EOF for an open failure [sashiko-bot review of PATCH 1/5].
- Rebased onto the current perf-tools-next head (bf10e6ee2ac3034c).
This series was developed with assistance from Claude (claude-opus-4.6) and Opencode (deepseek-v4-flash-free) for code analysis, patch generation, and commit message composition. All changes were validated by the maintainer.
Arnaldo Carvalho de Melo (5):
perf dso: Guard against errno==0 when dso__get_filename() returns NULL perf dso: Guard close() against invalid fd in
dso__decompress_kmodule_path()
perf dso: Use stored fd error instead of stale errno in file_read()
and file_size()
perf dso: Guard against cache underflow on short reads in
dso_cache__memcpy()
perf dso: Replace assert with runtime check in dso__read_symbol()
tools/perf/util/dso.c | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-)
base-commit: bf10e6ee2ac3034c9068e03eed418fd16961984e
v4-head: 6b06155e7794e6af7d2f0901d75a8d8a8607f030
v3-head: 41ed39bf2dac1d80
v2-head: 2f944ea022b6429f
v1-head: f6cb9e46c7b8949b
Arnaldo Carvalho de Melo
maintainerPATCH v5 1/5 perf dso: Guard against errno==0 when dso__get_filename() returns NULL
From: Arnaldo Carvalho de Melo <[email protected]>
__open_dso() computes fd = -errno when dso__get_filename() returns NULL. Some failure paths in dso__get_filename() (e.g. binary type mismatch) return NULL without making a syscall, leaving errno at 0 from a prior successful call. fd = -0 = 0, which is stdin — subsequent code treats it as a valid file descriptor.
Fall back to ENOENT when errno is 0, ensuring fd is always negative on failure.
The forced ENOENT stays in errno for the callers that check it after a negative fd. It must not misdirect the try_to_open_dso() fallback loop, though: dso__get_filename()'s chroot fallback used to accept a stale ENOENT even when stat() succeeded on a non-regular file (e.g. a directory). Re-stat() there and only take the chroot path when stat() actually failed with ENOENT [sashiko-bot review of PATCH 1/5].
--- tools/perf/util/dso.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
tools/perf/util/dso.c +15 -3
@@ -582,9 +582,18 @@ static char *dso__get_filename(struct dso *dso, const char *root_dir, goto out;if (!is_regular_file (name)) { +struct stat st;char *new_name; -if (errno !=ENOENT ||dso__nsinfo (dso) ==NULL ) +/* +* errno only reflects the failure reason when stat() itself +* failed: a successful stat() on a non-regular file (e.g. a +* directory) leaves a stale errno, which a previous failed +* iteration of the try_to_open_dso() fallback loop may have +* set to ENOENT. +*/ +if (stat (name, &st) ==0 || errno !=ENOENT || +dso__nsinfo (dso) ==NULL ) goto out; new_name =dso__filename_with_chroot (dso, name);
@@ -640,10 +649,13 @@ static int __open_dso(struct dso *dso, struct machine *machine)mutex_lock (dso__lock (dso)); name =dso__get_filename (dso, machine ? machine->root_dir :"" , &decomp); -if (name) +if (name) { fd =do_open (name); -else + }else { +if (errno ==0 ) + errno =ENOENT ; fd = -errno; + }if (decomp)unlink (name);
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 1/5] perf dso: Guard against errno==0 when dso__get_filename() returns NULL
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 18 lines
From: Arnaldo Carvalho de Melo <[email protected]>
__open_dso()computes fd = -errno whendso__get_filename()returns NULL. Some failure paths indso__get_filename()(e.g. binary type mismatch) return NULL without making a syscall, leaving errno at 0 from a prior successful call. fd = -0 = 0, which is stdin — subsequent code treats it as a valid file descriptor.Fall back to ENOENT when errno is 0, ensuring fd is always negative on failure.
The forced ENOENT stays in errno for the callers that check it after a negative fd. It must not misdirect the
try_to_open_dso()fallback loop, though: dso__get_filename()'s chroot fallback used to accept a stale ENOENT even whenstat()succeeded on a non-regular file (e.g. a directory). Re-stat() there and only take the chroot path whenstat()actually failed with ENOENT [sashiko-bot review of PATCH 1/5].
Quoted 6 lines
Fixes eba5102d2f0b ("perf tools: Add global list of opened dso objects")Reported-by sashiko-bot <[email protected]>Cc Jiri Olsa <[email protected]>Reviewed-by Ian Rogers <[email protected]>Assisted-by Claude:claude-opus-4.6Signed-off-by Arnaldo Carvalho de Melo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Arnaldo Carvalho de Melo
maintainerPATCH v5 2/5 perf dso: Guard close() against invalid fd in dso__decompress_kmodule_path()
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
dso__decompress_kmodule_path() unconditionally calls close(fd) on the return value of decompress_kmodule(). When decompression fails or the DSO is not compressed, decompress_kmodule() returns -1. close(-1) fails with EBADF and clobbers errno, which callers up the chain (dso__get_filename → __open_dso) depend on for error propagation.
Guard the close() call with fd >= 0 so only valid file descriptors are closed.
--- tools/perf/util/dso.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/util/dso.c +3 -1
@@ -395,7 +395,9 @@ int dso__decompress_kmodule_path(struct dso *dso, const char *name, {int fd =decompress_kmodule (dso, name, pathname, len); -close (fd); +/* decompress_kmodule() returns -1 on failure, don't close(-1) */ +if (fd >=0 ) +close (fd);return fd >=0 ?0 :-1 ; }
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 2/5] perf dso: Guard close() against invalid fd in dso__decompress_kmodule_path()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
From: Arnaldo Carvalho de Melo <[email protected]>
dso__decompress_kmodule_path()unconditionally calls close(fd) on the return value ofdecompress_kmodule(). When decompression fails or the DSO is not compressed,decompress_kmodule()returns -1. close(-1) fails with EBADF and clobbers errno, which callers up the chain (dso__get_filename→__open_dso) depend on for error propagation.Guard the
close()call with fd >= 0 so only valid file descriptors are closed.
Quoted 6 lines
Fixes 42b3fa670825 ("perf tools: Introduce dso__decompress_kmodule_{fd,path}")Reported-by sashiko-bot <[email protected]>Reviewed-by Ian Rogers <[email protected]>Cc Namhyung Kim <[email protected]>Assisted-by Claude:claude-opus-4.6Signed-off-by Arnaldo Carvalho de Melo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Arnaldo Carvalho de Melo
maintainerPATCH v5 3/5 perf dso: Use stored fd error instead of stale errno in file_read() and file_size()
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
file_read() and file_size() use ret = -errno when dso__data(dso)->fd is negative after try_to_open_dso() fails. By this point errno has been through mutex_lock(), nsinfo__mountns_enter(), and multiple open() attempts inside try_to_open_dso() — it no longer reflects the actual open failure. If errno happens to be 0, ret = 0 looks like EOF rather than an error, and file_size() callers like dso__data_size() would then report a zero-sized file instead of failing.
dso__data(dso)->fd is always negative on failure — -errno from __open_dso() when no filename could be built (e.g. -EINVAL, -ENOENT), or -1 when do_open() itself failed — and never 0, so use it directly instead of reading the stale global errno.
No assert() or comment is needed after the assignment: the enclosing if (dso__data(dso)->fd < 0) already guarantees ret < 0 [Namhyung Kim review].
--- tools/perf/util/dso.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/dso.c +2 -2
@@ -1038,7 +1038,7 @@ static ssize_t file_read(struct dso *dso, struct machine *machine,if (dso__data (dso)->fd <0 ) {dso__data (dso)->status =DSO_DATA_STATUS_ERROR ; - ret = -errno; + ret =dso__data (dso)->fd; goto out; }
@@ -1160,8 +1160,8 @@ static int file_size(struct dso *dso, struct machine *machine)try_to_open_dso (dso, machine);if (dso__data (dso)->fd <0 ) { - ret = -errno;dso__data (dso)->status =DSO_DATA_STATUS_ERROR ; + ret =dso__data (dso)->fd; goto out; }
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 3/5] perf dso: Use stored fd error instead of stale errno in file_read() and file_size()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 20 lines
From: Arnaldo Carvalho de Melo <[email protected]>
file_read()andfile_size()use ret = -errno when dso__data(dso)->fd is negative aftertry_to_open_dso()fails. By this point errno has been throughmutex_lock(),nsinfo__mountns_enter(), and multipleopen()attempts insidetry_to_open_dso()— it no longer reflects the actual open failure. If errno happens to be 0, ret = 0 looks like EOF rather than an error, andfile_size()callers likedso__data_size()would then report a zero-sized file instead of failing.dso__data(dso)->fd is always negative on failure — -errno from
__open_dso()when no filename could be built (e.g.-EINVAL,-ENOENT), or -1 whendo_open()itself failed — and never 0, so use it directly instead of reading the stale global errno.No
assert()or comment is needed after the assignment: the enclosing if (dso__data(dso)->fd < 0) already guarantees ret < 0 [Namhyung Kim review].
Quoted 6 lines
Fixes 33bdedcea2d7 ("perf tools: Protect dso cache fd with a mutex")Reported-by sashiko-bot <[email protected]>Reviewed-by Ian Rogers <[email protected]>Cc Namhyung Kim <[email protected]>Assisted-by Claude:claude-opus-4.6Signed-off-by Arnaldo Carvalho de Melo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Arnaldo Carvalho de Melo
maintainerPATCH v5 4/5 perf dso: Guard against cache underflow on short reads in dso_cache__memcpy()
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
dso_cache__memcpy() computes cache_offset = offset - cache->offset, then cache_size = min(cache->size - cache_offset, size). The RB tree lookup in __dso_cache__find() matches using the full DSO__DATA_CACHE_SIZE window, but cache->size reflects the actual pread return value from dso_cache__populate().
A short pread (e.g. near end-of-file) makes cache->size smaller than DSO__DATA_CACHE_SIZE. If a subsequent access targets an offset past cache->offset + cache->size but within the DSO__DATA_CACHE_SIZE window, the cache entry is found but cache_offset exceeds cache->size. Since both are u64, the subtraction cache->size - cache_offset wraps to a large value, min() selects the caller's size, and memcpy reads out of bounds.
Return 0 for an offset past the valid cached data. For a regular file a short pread only happens at end-of-file, so 0 is what a direct pread() at that offset would return: cached_io() stops its read loop as on EOF. Re-reading from the backing file would not help — a second pread at the same offset returns the same short count.
--- tools/perf/util/dso.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
tools/perf/util/dso.c +14 -1
@@ -1014,7 +1014,20 @@ static ssize_t dso_cache__memcpy(struct dso_cache *cache, u64 offset, u8 *data, u64 size, bool out) {u64 cache_offset = offset - cache->offset; -u64 cache_size =min (cache->size - cache_offset, size); +u64 cache_size; + +/* +* The RB tree matches using DSO__DATA_CACHE_SIZE, but a short +* pread may leave cache->size smaller. For a regular file a +* short pread only happens at end-of-file, so an offset past +* the valid data is EOF: return 0, matching what a direct +* pread() at that offset would return, and cached_io() then +* stops its read loop. +*/ +if (cache_offset >= cache->size) +return 0 ; + + cache_size =min (cache->size - cache_offset, size);if (out)memcpy (data, cache->data + cache_offset, cache_size);
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 4/5] perf dso: Guard against cache underflow on short reads in dso_cache__memcpy()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 22 lines
From: Arnaldo Carvalho de Melo <[email protected]>
dso_cache__memcpy()computescache_offset= offset -cache->offset, thencache_size=min(cache->size - cache_offset, size). The RB tree lookup in__dso_cache__find()matches using the fullDSO__DATA_CACHE_SIZEwindow, butcache->sizereflects the actual pread return value fromdso_cache__populate().A short pread (e.g. near end-of-file) makes
cache->sizesmaller thanDSO__DATA_CACHE_SIZE. If a subsequent access targets an offset pastcache->offset+cache->sizebut within theDSO__DATA_CACHE_SIZEwindow, the cache entry is found butcache_offsetexceedscache->size. Since both are u64, the subtractioncache->size-cache_offsetwraps to a large value,min()selects the caller's size, and memcpy reads out of bounds.Return 0 for an offset past the valid cached data. For a regular file a short pread only happens at end-of-file, so 0 is what a direct
pread()at that offset would return:cached_io()stops its read loop as on EOF. Re-reading from the backing file would not help — a second pread at the same offset returns the same short count.
Quoted 6 lines
Fixes 366df72657e0 ("perf dso: Refactor dso_cache__read()")Reported-by sashiko-bot <[email protected]>Reviewed-by Ian Rogers <[email protected]>Cc Adrian Hunter <[email protected]>Assisted-by Claude:claude-opus-4.6Signed-off-by Arnaldo Carvalho de Melo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Arnaldo Carvalho de Melo
maintainerPATCH v5 5/5 perf dso: Replace assert with runtime check in dso__read_symbol()
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
dso__read_symbol() asserts that len <= jited_prog_len, where len comes from sym->end - sym->start (parsed from PERF_RECORD_KSYMBOL in perf.data). Both values originate from untrusted file input.
With NDEBUG (production builds), the assert is compiled out, allowing an out-of-bounds heap read when the BPF program buffer is accessed. Without NDEBUG, a crafted perf.data crashes perf with an assertion failure.
Replace the assert with a runtime bounds check that returns NULL with an appropriate error code, matching the existing error handling pattern in this function.
--- tools/perf/util/dso.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
tools/perf/util/dso.c +6 -1
@@ -2038,7 +2038,12 @@ const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename, errno =SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF ; return NULL; } -assert (len <= info_linear->info.jited_prog_len); +if (len > info_linear->info.jited_prog_len) { +pr_debug ("BPF symbol length %zu exceeds jited_prog_len %u\n" , + len, info_linear->info.jited_prog_len); + errno =SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF ; +return NULL ; + } *out_buf_len = len;return (const u8 *)(uintptr_t )(info_linear->info.jited_prog_insns);#else
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 5/5] perf dso: Replace assert with runtime check in dso__read_symbol()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Misleading error code used for bounds check failure. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Misleading error code used for bounds check failure.
--
--- Patch [5]: [PATCH 5/5] perf dso: Replace assert with runtime check in dso__read_symbol() --- commit 99fef42823fa80b278999b8901d74d88e812bd2a
Author: Arnaldo Carvalho de Melo <[email protected]>
perf dso: Replace assert with runtime check in dso__read_symbol()
This commit replaces an assert in dso__read_symbol() with a runtime bounds check to prevent out-of-bounds heap reads or assertion failures when processing untrusted perf.data files.
tools/perf/util/dso.c const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename, · line 2044
Quoted 13 lines
tools/perf/util/dso.c +4
-1@@ -2038,7 +2038,12 @@ const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename, errno =SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF ;return NULL ; } -assert (len <= info_linear->info.jited_prog_len); +if (len > info_linear->info.jited_prog_len) { +pr_debug ("BPF symbol length %zu exceeds jited_prog_len %u\n" , + len, info_linear->info.jited_prog_len); + errno =SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF ;
[Severity: Medium]
Since this is a bounds check failure in dso__read_symbol() triggered by a corrupted perf.data file, reusing SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF might cause tools to incorrectly report that the BPF file is missing its BTF section instead of indicating a length mismatch.
Could this regression result in a confusing message for the user?
Quoted 5 lines
tools/perf/util/dso.c +2
-0+ return NULL; + } *out_buf_len = len;return (const u8 *)(uintptr_t )(info_linear->info.jited_prog_insns);#else
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 5/5] perf dso: Replace assert with runtime check in dso__read_symbol()
Quoted 17 lines · On Thu, Aug 13, 2026 at 03:26:29PM +0000, [email protected] wrote:
tools/perf/util/dso.c const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename, · line 2044
Quoted 10 lines
+++
b/tools/perf/util/dso.ctools/perf/util/dso.c +4
-1@@ -2038,7 +2038,12 @@ const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename, errno =SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF ;return NULL ; } -assert (len <= info_linear->info.jited_prog_len); +if (len > info_linear->info.jited_prog_len) { +pr_debug ("BPF symbol length %zu exceeds jited_prog_len %u\n" , + len, info_linear->info.jited_prog_len); + errno =SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF ;[Severity: Medium]
Since this is a bounds check failure indso__read_symbol()triggered by a corrupted perf.data file, reusingSYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTFmight cause tools to incorrectly report that the BPF file is missing its BTF section instead of indicating a length mismatch.
Quoted 1 line
Could this regression result in a confusing message for the user?
Some other cases near here are returning this error as well, we need to add more errors and update the associated symbol__strerror_disassemble to print the more precise error messages, probably SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_BTF.
I will add this to a TODO file for a new patch series improving this situation.
- Arnaldo
Quoted 10 lines · Arnaldo Carvalho de Melo
Please consider merging,
Five hardening fixes for the perf DSO data handling code path, all found by the sashiko-bot AI reviewer:
-
__open_dso()can return a file descriptor of 0 (stdin) whendso__get_filename()fails without setting errno, so fd = -errno = 0 looked like a successfully opened DSO;[...]
Applied to perf-tools-next, thanks!
Best regards,
Namhyung