]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
perf hwmon: Use scnprintf() in hwmon_pmu__for_each_event()
authorArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 8 Jun 2026 01:38:48 +0000 (22:38 -0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 10 Jun 2026 21:56:01 +0000 (18:56 -0300)
hwmon_pmu__for_each_event() formats description strings via:

    len = snprintf(desc_buf, sizeof(desc_buf), "%s in unit %s named %s.", ...);
    len += hwmon_pmu__describe_items(hwm, desc_buf + len, sizeof(desc_buf) - len, ...);

If value->label is long enough to cause snprintf() to truncate, it
returns the would-have-been-written count, making len exceed
sizeof(desc_buf).  The subsequent sizeof(desc_buf) - len underflows
to a huge size_t value, disabling bounds checking in
hwmon_pmu__describe_items().

The alias_buf snprintf has the same issue.  Switch both to scnprintf()
which returns actual bytes written.

Fixes: 53cc0b351ec99278 ("perf hwmon_pmu: Add a tool PMU exposing events from hwmon in sysfs")
Reported-by: sashiko-bot <sashiko-bot@kernel.org>
Cc: Ian Rogers <irogers@google.com>
Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/hwmon_pmu.c

index d895cd74f2a05f9dfefd5e8f93471fc69dab7a1a..fbfb872ceb1826d8e3873910bb970706b848ceb7 100644 (file)
@@ -514,14 +514,14 @@ int hwmon_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_callb
                int ret;
                size_t len;
 
-               len = snprintf(alias_buf, sizeof(alias_buf), "%s%d",
-                              hwmon_type_strs[key.type], key.num);
+               scnprintf(alias_buf, sizeof(alias_buf), "%s%d",
+                         hwmon_type_strs[key.type], key.num);
                if (!info.name) {
                        info.name = info.alias;
                        info.alias = NULL;
                }
 
-               len = snprintf(desc_buf, sizeof(desc_buf), "%s in unit %s named %s.",
+               len = scnprintf(desc_buf, sizeof(desc_buf), "%s in unit %s named %s.",
                        hwmon_desc[key.type],
                        pmu->name + 6,
                        value->label ?: info.name);