Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Arnaldo Carvalho de Melo" <acme@kernel.org>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 11/11] perf tools: Use scnprintf() in build_id__snprintf() and hwmon read_events()
Date: Mon, 08 Jun 2026 01:54:33 +0000	[thread overview]
Message-ID: <20260608015434.37F7E1F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260608013057.1942953-12-acme@kernel.org>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [High] Missing size adjustment in read() and pread() size arguments leads to out-of-bounds null termination on stack buffers.
- [High] If the `read()` system call returns `<= 0`, uninitialized stack memory is evaluated and potentially duplicated, leading to an information leak or crash.
- [Medium] A flawed check `if (fd > 0)` fails to process or close file descriptor 0, causing a resource leak if openat returns 0.
--

commit 63dc76c4229ffb5cbb25da7f0bdc34b17ef66fae
Author: Arnaldo Carvalho de Melo <acme@redhat.com>

perf tools: Use scnprintf() in build_id__snprintf() and hwmon read_events()

build_id__snprintf() and hwmon_pmu__read_events() accumulate formatted
output via snprintf(), which returns the would-have-been-written count
on truncation. In hwmon_pmu__read_events(), len overshoots out_buf_len
and the next 'out_buf_len - len' underflows. Switch both to scnprintf()
which returns actual bytes written.

[ ... ]

> diff --git a/tools/perf/util/hwmon_pmu.c b/tools/perf/util/hwmon_pmu.c
> index fb3ffa8d32ad2..dbf6a71af47f9 100644
> --- a/tools/perf/util/hwmon_pmu.c
> +++ b/tools/perf/util/hwmon_pmu.c
> @@ -442,12 +442,12 @@ static size_t hwmon_pmu__describe_items(struct hwmon_pmu *hwm, char *out_buf, si
>  
>  				buf[read_len] = '\0';
                                ^^^^^^^^^^^^^^^^^^^^
[Severity: High]
This isn't a bug introduced by this patch, but doesn't this result in an
out-of-bounds write?

Since read() can return up to sizeof(buf), if the sysfs file contents
completely fill the buffer (i.e. read_len == sizeof(buf)), writing the null
terminator here will overwrite one byte past the end of the buf array on the
stack.

Could we pass sizeof(buf) - 1 to read() to avoid this?

This same pre-existing issue seems to be present in hwmon_pmu__read_events()
and evsel__hwmon_pmu_read() as well, where read() and pread() are called with
the full buffer size and similarly null-terminated.


[Severity: Medium]
Also a pre-existing issue in hwmon_pmu__describe_items() just above this hunk,
can this leak a file descriptor?

hwmon_pmu__describe_items() opens a sysfs file using openat(), and then checks:

    fd = openat(dir, buf, O_RDONLY);
    if (fd > 0) {

If the tool runs in an environment where standard input has been closed,
openat() can successfully return 0. The current logic treats 0 as an error,
skips file processing, and fails to call close(fd), causing a resource leak.

Should this check be if (fd >= 0) instead?


[Severity: High]
Another pre-existing issue in hwmon_pmu__read_events(), can this leak
uninitialized stack memory?

If read() returns 0 or less (for example, on an error or empty file), read_len
will be 0 or less. The code skips null-terminating the buffer and proceeds
to check:

    if (buf[0] == '\0') {
        ...
    value->label = strdup(buf);

Since char buf[128] is uninitialized, wouldn't evaluating buf[0] and passing
buf to strdup() result in reading and copying garbage stack memory until it
hits a random null byte?

>  				val = strtoll(buf, /*endptr=*/NULL, 10);
> -				len += snprintf(out_buf + len, out_buf_len - len, "%s%s%s=%g%s",
> -						len == 0 ? " " : ", ",
> -						hwmon_item_strs[bit],
> -						is_alarm ? "_alarm" : "",
> -						(double)val / 1000.0,
> -						hwmon_units[key.type]);
> +				len += scnprintf(out_buf + len, out_buf_len - len, "%s%s%s=%g%s",
> +						 len == 0 ? " " : ", ",
> +						 hwmon_item_strs[bit],
> +						 is_alarm ? "_alarm" : "",
> +						 (double)val / 1000.0,
> +						 hwmon_units[key.type]);
>  			}
>  			close(fd);
>  		}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260608013057.1942953-1-acme@kernel.org?part=11

  reply	other threads:[~2026-06-08  1:54 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-08  1:30 [PATCHES v2 00/11] perf tools: Assorted fixes Arnaldo Carvalho de Melo
2026-06-08  1:30 ` [PATCH 01/11] perf tools: Fix get_max_num() size_t underflow on empty sysfs file Arnaldo Carvalho de Melo
2026-06-08  1:45   ` sashiko-bot
2026-06-08  1:30 ` [PATCH 02/11] perf tools: Use scnprintf() in cpu_map__snprint() to prevent overflow Arnaldo Carvalho de Melo
2026-06-08  1:30 ` [PATCH 03/11] perf tools: Use perf_env__get_cpu_topology() in machine__resolve() Arnaldo Carvalho de Melo
2026-06-08  1:51   ` sashiko-bot
2026-06-08  1:30 ` [PATCH 04/11] perf mmap: Fix mbind() maxnode vs bitmap allocation mismatch in aio_bind Arnaldo Carvalho de Melo
2026-06-08  1:30 ` [PATCH 05/11] perf tools: NULL bitmap pointers after bitmap_free() Arnaldo Carvalho de Melo
2026-06-08  1:45   ` sashiko-bot
2026-06-08  1:30 ` [PATCH 06/11] perf sched: Bounds-check prio before test_bit() in timehist Arnaldo Carvalho de Melo
2026-06-08  1:51   ` sashiko-bot
2026-06-08  1:30 ` [PATCH 07/11] perf sched: Fix idle-hist callchain display using wrong rb_first variant Arnaldo Carvalho de Melo
2026-06-08  1:30 ` [PATCH 08/11] perf tools: Add O_CLOEXEC to open() calls in DSO and ELF code Arnaldo Carvalho de Melo
2026-06-08  1:44   ` sashiko-bot
2026-06-08  1:30 ` [PATCH 09/11] perf bpf: Use scnprintf() in snprintf_hex() and synthesize_bpf_prog_name() Arnaldo Carvalho de Melo
2026-06-08  1:30 ` [PATCH 10/11] perf hists: Fix snprintf() in hists__scnprintf_title() UID filter path Arnaldo Carvalho de Melo
2026-06-08  1:51   ` sashiko-bot
2026-06-08  1:30 ` [PATCH 11/11] perf tools: Use scnprintf() in build_id__snprintf() and hwmon read_events() Arnaldo Carvalho de Melo
2026-06-08  1:54   ` sashiko-bot [this message]
  -- strict thread matches above, loose matches on Subject: below --
2026-06-07 23:29 [PATCHES v1 00/11] perf tools: Assorted fixes Arnaldo Carvalho de Melo
2026-06-07 23:29 ` [PATCH 11/11] perf tools: Use scnprintf() in build_id__snprintf() and hwmon read_events() Arnaldo Carvalho de Melo
2026-06-07 23:49   ` sashiko-bot

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=20260608015434.37F7E1F00893@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=acme@kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox