All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Athira Rajeev" <atrajeev@linux.ibm.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 1/3] perf build-id: add shared is_valid_elf() helper
Date: Mon, 07 Sep 2026 03:53:35 +0000	[thread overview]
Message-ID: <20260907035336.33C5F1F00A3D@smtp.kernel.org> (raw)
In-Reply-To: <20260907034554.714-1-atrajeev@linux.ibm.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The helper `is_valid_elf` is vulnerable to a Time-of-Check to Time-of-Use (TOCTOU) race condition because it verifies the file by name and immediately closes it.
- [Low] The patch uses POSIX libc functions and ELF constants without explicitly including their required standard headers, violating subsystem musl compatibility guidelines.
--

commit 61718d5b137531f7814b491a94911627f3ad9015
Author: Athira Rajeev <atrajeev@linux.ibm.com>

perf build-id: add shared is_valid_elf() helper

This patch adds an is_valid_elf() helper to build-id.c and build-id.h
to allow multiple perf subsystems to verify elf magic without duplicating
the check. The function opens the file, checks the SELFMAG bytes, and
closes the file descriptor.

> diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
> index eb95ab90f9741..28b6b3f8d5d35 100644
> --- a/tools/perf/util/build-id.c
> +++ b/tools/perf/util/build-id.c
> @@ -43,6 +43,20 @@
>  
>  static bool no_buildid_cache;
>  
> +bool is_valid_elf(const char *filename)

[Severity: Medium]
Is this helper vulnerable to a time-of-check to time-of-use race condition?
By verifying the file by name and immediately closing the file descriptor,
a local user could replace the target file between this check and when the
caller reopens or links it. This might allow invalid files to bypass the
validation and pollute the build-id cache.

Would it be safer to change the function signature to operate on a stable,
already-opened file descriptor instead of passing a filename?

> +{
> +	unsigned char magic[SELFMAG];
> +	int fd = open(filename, O_RDONLY);
> +	bool valid = false;

[Severity: Low]
Does this code introduce missing explicit header inclusions for musl
compatibility? The subsystem guidelines require that files using libc
functions or constants directly include the standard headers where those
symbols are declared.

While the commit message notes that elf.h and fcntl.h are already included
in build-id.c, this function introduces uses of open, O_RDONLY, and
SELFMAG, without guaranteeing that fcntl.h and elf.h are explicitly and
directly included at the top of the file.

> +
> +	if (fd < 0)
> +		return false;
> +	if (read(fd, magic, sizeof(magic)) == (ssize_t)sizeof(magic))
> +		valid = (memcmp(magic, ELFMAG, SELFMAG) == 0);
> +	close(fd);

[Severity: Low]
Similarly, read, close, and memcmp are used here without explicitly
including unistd.h and string.h. Can we add direct includes for these
to ensure strict header separation?

> +	return valid;
> +}
> +
>  static int mark_dso_hit_callback(struct callchain_cursor_node *node, void *data __maybe_unused)

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260907034554.714-1-atrajeev@linux.ibm.com?part=1

      parent reply	other threads:[~2026-09-07  3:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-07  3:45 [PATCH 1/3] perf build-id: add shared is_valid_elf() helper Athira Rajeev
2026-09-07  3:45 ` [PATCH 2/3] perf annotate: fall back to original binary if build-id cache entry is not a valid ELF Athira Rajeev
2026-09-07  3:59   ` sashiko-bot
2026-09-07  3:45 ` [PATCH 3/3] perf buildid-cache: skip caching non-ELF files and treat unreadable build-id as mismatch Athira Rajeev
2026-09-07  4:01   ` sashiko-bot
2026-09-07  3:53 ` sashiko-bot [this message]

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=20260907035336.33C5F1F00A3D@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=atrajeev@linux.ibm.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.