public inbox for linux-perf-users@vger.kernel.org
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ian Rogers <irogers@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>,
	Ingo Molnar <mingo@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	James Clark <james.clark@linaro.org>,
	Jiri Olsa <jolsa@kernel.org>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Kan Liang <kan.liang@linux.intel.com>,
	Clark Williams <williams@redhat.com>,
	linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
	Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: Re: [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype
Date: Tue, 7 Apr 2026 09:08:44 -0300	[thread overview]
Message-ID: <adTzzKCWES52egGl@x1> (raw)
In-Reply-To: <CAP-5=fVv=5H-6DOaAr4BSDE=pKrxWDa9oduYbVTozYc-ZeeuRg@mail.gmail.com>

On Thu, Apr 02, 2026 at 08:19:24AM -0700, Ian Rogers wrote:
> On Wed, Apr 1, 2026 at 5:20 PM Arnaldo Carvalho de Melo <acme@kernel.org> wrote:
> >
> > From: Arnaldo Carvalho de Melo <acme@redhat.com>
> >
> > This works in some places because there are tricks to prefer a GNU
> > version instead of the XPG version, but started breaking in some musl
> > libc systems due to some unrelated change that made libgen.h stop being
> > included by chance.
> >
> > Since symbol.h is using basename(), make it include libgen.h.
> 
> Huh, this is being flagged by Sashiko:
> """
> Could this include cause segmentation faults by exposing the POSIX version
> of basename() to the rest of the tools?
> 
> According to the POSIX specification, if the input is an empty string
> or slashes, basename() can return a pointer to a statically allocated,
> read-only string literal (such as "." or "/").
> 
> In tools/perf/util/probe-event.c:convert_exec_to_group(), the code takes
> the return value of basename() and modifies it in-place:
>     ptr = basename(exec);
>     ...
>     for (ptr2 = ptr; *ptr2 != '\0'; ptr2++) {
>             if (!isalnum(*ptr2) && *ptr2 != '_')
>                     *ptr2 = '_';
>     }
> If a user provides an empty target or "/", the POSIX basename() would
> return a read-only string. When the loop attempts to write a null or
> underscore into it, wouldn't it trigger an immediate segmentation fault?
> 
> Is there a way we can safely duplicate the returned string before modifying it,
> or ensure the inputs never resolve to these static strings?
> """
> 
> The story of the two basename versions and the issues appears in the
> man pages. The whole thing makes me think we should avoid basename :-)

What sashiko pointed out is a problem in some other part of perf that is
unrelated to this patch and requires a separate fix, no?

I.e. tools/perf/util/probe-event.c already includes libgen.h since
581037151910126a ("perf probe: Add missing libgen.h header needed for
using basename()") from March, 2024.

In the scope of this patch the potential problem isn't possible as
__symbol__join_symfs() isn't changing the return of basename(), right?

⬢ [acme@toolbx perf-tools-next]$ git grep '\<basename(' tools/perf
tools/perf/builtin-daemon.c:    base = basename(basen);
tools/perf/scripts/python/exported-sql-viewer.py:               if short_name[0:7] == "[kernel" and os.path.basename(long_name) == "kcore":
tools/perf/util/annotate.c:             d_filename = basename(filename);
tools/perf/util/dsos.c:        * basename() may modify path buffer, so we must pass
tools/perf/util/dsos.c:          * basename() may return a pointer to internal
tools/perf/util/dsos.c:         base = strdup(basename(lname));
tools/perf/util/probe-event.c:  ptr1 = basename(exec_copy);
tools/perf/util/symbol.h:               base = basename(path_copy);
⬢ [acme@toolbx perf-tools-next]$

It seems just the probe-event.c case needs fixing.

And we also have this in tools/perf/util/srcline.c:

/* basename version that takes a const input string */
static const char *gnu_basename(const char *path)
{
        const char *base = strrchr(path, '/');

        return base ? base + 1 : path;
}

That probably could be reused to avoid these basename() oddities?

- Arnaldo

  parent reply	other threads:[~2026-04-07 12:08 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-02  0:17 [PATCHES v3 perf-tools-next 0/4] Cleanups and a fix Arnaldo Carvalho de Melo
2026-04-02  0:17 ` [PATCH 1/4] perf tools: Make more global variables static Arnaldo Carvalho de Melo
2026-04-07 12:50   ` Arnaldo Carvalho de Melo
2026-04-02  0:17 ` [PATCH 2/4] perf bench: Constify tables Arnaldo Carvalho de Melo
2026-04-02  0:17 ` [PATCH 3/4] perf tools: Use calloc() were applicable Arnaldo Carvalho de Melo
2026-04-02  0:17 ` [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype Arnaldo Carvalho de Melo
2026-04-02 15:19   ` Ian Rogers
2026-04-02 15:48     ` Arnaldo Melo
2026-04-07 12:08     ` Arnaldo Carvalho de Melo [this message]
  -- strict thread matches above, loose matches on Subject: below --
2026-04-01 21:53 [PATCHES v2 perf-tools-next 0/4] Cleanups and a fix Arnaldo Carvalho de Melo
2026-04-01 21:53 ` [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype Arnaldo Carvalho de Melo
2026-04-01 21:01 [PATCHES perf-tools-next 0/4] Cleanups and a fix Arnaldo Carvalho de Melo
2026-04-01 21:02 ` [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype Arnaldo Carvalho de Melo
2026-04-01 21:08   ` Ian Rogers
2026-04-01 21:41     ` Arnaldo Melo
2026-04-01 23:10       ` Namhyung Kim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=adTzzKCWES52egGl@x1 \
    --to=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=adrian.hunter@intel.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=williams@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox