[PATCH] tools: perf: session: avoid segfault on implicit stdin

Stephen Brennan posted 1 patch 1 month, 1 week ago
tools/perf/util/session.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
[PATCH] tools: perf: session: avoid segfault on implicit stdin
Posted by Stephen Brennan 1 month, 1 week ago
A user reported that running a command like:

    perf script flamegraph -ag -F99 -- sleep 10

Resulted in a segmentation fault. The reason is twofold. First, the
"flamegraph-report" script has a shebang line which ends with "--". This
disables option parsing, so that the exec'd "perf script report" command
does not see the option "-i -" which is appended to its command line.
Second, despite the unprocessed "-i -" option, the default behavior of
perf is to use stdin if it is a pipe -- and in this case, it is. Thus, the
report continues running, but segfaults on accessing the filename.

The fix for the second issue is a simple NULL check. Implement this
here.

Fixes: 4553c431e7dd2 ("perf report: Display pregress bar on redirected pipe data")

Signed-off-by: Stephen Brennan <stephen.s.brennan@oracle.com>
---
 tools/perf/util/session.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 26ae078278cd6..a9624505c0ca3 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -1854,9 +1854,11 @@ static int __perf_session__process_pipe_events(struct perf_session *session)
 	/*
 	 * If it's from a file saving pipe data (by redirection), it would have
 	 * a file name other than "-".  Then we can get the total size and show
-	 * the progress.
+	 * the progress. However, be careful because path may be NULL if input
+	 * is coming from stdin.
 	 */
-	if (strcmp(session->data->path, "-") && session->data->file.size) {
+	if (session->data->path && strcmp(session->data->path, "-")
+	    && session->data->file.size) {
 		ui_progress__init_size(&prog, session->data->file.size,
 				       "Processing events...");
 		update_prog = true;
-- 
2.47.3
Re: [PATCH] tools: perf: session: avoid segfault on implicit stdin
Posted by Arnaldo Carvalho de Melo 2 weeks, 6 days ago
On Mon, Aug 25, 2025 at 05:16:48PM -0700, Stephen Brennan wrote:
> A user reported that running a command like:
> 
>     perf script flamegraph -ag -F99 -- sleep 10
> 
> Resulted in a segmentation fault. The reason is twofold. First, the
> "flamegraph-report" script has a shebang line which ends with "--". This
> disables option parsing, so that the exec'd "perf script report" command
> does not see the option "-i -" which is appended to its command line.
> Second, despite the unprocessed "-i -" option, the default behavior of
> perf is to use stdin if it is a pipe -- and in this case, it is. Thus, the
> report continues running, but segfaults on accessing the filename.
> 
> The fix for the second issue is a simple NULL check. Implement this
> here.

I did a test before and after and the problem is indeed present:

root@number:~# perf script flamegraph -ag -F99 -- sleep 1
/home/acme/libexec/perf-core/scripts/python/bin/flamegraph-report: line 3: 362975 Segmentation fault      (core dumped) perf script -s "$PERF_EXEC_PATH"/scripts/python/flamegraph.py "$@"
root@number:~# 

But even after your patch, it still segfaults.

I'll try to look at this after processing lower hanging patches.

Thanks for working on this!

- Arnaldo
 
> Fixes: 4553c431e7dd2 ("perf report: Display pregress bar on redirected pipe data")
> 
> Signed-off-by: Stephen Brennan <stephen.s.brennan@oracle.com>
> ---
>  tools/perf/util/session.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
> index 26ae078278cd6..a9624505c0ca3 100644
> --- a/tools/perf/util/session.c
> +++ b/tools/perf/util/session.c
> @@ -1854,9 +1854,11 @@ static int __perf_session__process_pipe_events(struct perf_session *session)
>  	/*
>  	 * If it's from a file saving pipe data (by redirection), it would have
>  	 * a file name other than "-".  Then we can get the total size and show
> -	 * the progress.
> +	 * the progress. However, be careful because path may be NULL if input
> +	 * is coming from stdin.
>  	 */
> -	if (strcmp(session->data->path, "-") && session->data->file.size) {
> +	if (session->data->path && strcmp(session->data->path, "-")
> +	    && session->data->file.size) {
>  		ui_progress__init_size(&prog, session->data->file.size,
>  				       "Processing events...");
>  		update_prog = true;
> -- 
> 2.47.3