Linux Power Management development
 help / color / mirror / Atom feed
From: Ali Ahmet Memis <ali@iusegentoo.com>
To: Shuah Khan <skhan@linuxfoundation.org>,
	Shuah Khan <shuah@kernel.org>, Thomas Renninger <trenn@suse.com>,
	"John B . Wyatt IV" <jwyatt@redhat.com>,
	John Kacur <jkacur@redhat.com>
Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: [PATCH v3 1/3] cpupower: zero the topology array to avoid uninitialized reads
Date: Thu,  6 Aug 2026 17:51:38 +0000	[thread overview]
Message-ID: <20260806175140.270935-2-ali@iusegentoo.com> (raw)
In-Reply-To: <20260803175215.117518-1-ali@iusegentoo.com>

get_cpu_topology() allocates core_info with malloc() and then fills it in
per CPU. Three paths leave core_cpu_list untouched: a failed
physical_package_id read, a failed core_id read, and a core_cpus_list read
that comes back empty, which only prints a warning.

The array is then sorted with __compare_core_cpu_list(), which passes
core_cpu_list to strcmp(). For the entries above that buffer still holds
whatever malloc() returned, so strcmp() reads uninitialized memory, and if
the buffer happens to contain no NUL byte it reads past the end of it.

Allocate with calloc() so an entry that is never filled in compares as an
empty string.

Fixes: f89cb9cba7a2 ("cpupower: Implement CPU physical core querying")
Cc: stable@vger.kernel.org
Signed-off-by: Ali Ahmet Memis <ali@iusegentoo.com>
---
 tools/power/cpupower/lib/cpupower.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/power/cpupower/lib/cpupower.c b/tools/power/cpupower/lib/cpupower.c
index d7f7ec6f151c..559b04f4387e 100644
--- a/tools/power/cpupower/lib/cpupower.c
+++ b/tools/power/cpupower/lib/cpupower.c
@@ -171,7 +171,7 @@ int get_cpu_topology(struct cpupower_topology *cpu_top)
 	char path[SYSFS_PATH_MAX];
 	char *last_cpu_list;
 
-	cpu_top->core_info = malloc(sizeof(struct cpuid_core_info) * cpus);
+	cpu_top->core_info = calloc(cpus, sizeof(struct cpuid_core_info));
 	if (cpu_top->core_info == NULL)
 		return -ENOMEM;
 	cpu_top->pkgs = cpu_top->cores = 0;
-- 
2.55.0


  parent reply	other threads:[~2026-08-06 17:52 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-03 17:52 [PATCH v2 0/2] cpupower: fix topology array handling Ali Ahmet Memis
2026-08-03 17:52 ` [PATCH v2 1/2] cpupower: zero the topology array to avoid uninitialized reads Ali Ahmet Memis
2026-08-03 17:52 ` [PATCH v2 2/2] cpupower: do not count incomplete topology entries as physical cores Ali Ahmet Memis
2026-08-04 20:45 ` [PATCH v2 0/2] cpupower: fix topology array handling Shuah Khan
2026-08-05 11:43   ` Ali Ahmet Memis
2026-08-05 12:10     ` Ali Ahmet Memis
2026-08-06 15:50     ` Shuah Khan
2026-08-06 17:51 ` [PATCH v3 0/3] " Ali Ahmet Memis
2026-08-06 17:51 ` Ali Ahmet Memis [this message]
2026-08-06 17:51 ` [PATCH v3 2/3] cpupower: let the core == -1 check handle failed topology reads Ali Ahmet Memis
2026-08-06 17:51 ` [PATCH v3 3/3] cpupower: do not count incomplete topology entries as physical cores Ali Ahmet Memis

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=20260806175140.270935-2-ali@iusegentoo.com \
    --to=ali@iusegentoo.com \
    --cc=jkacur@redhat.com \
    --cc=jwyatt@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=stable@vger.kernel.org \
    --cc=trenn@suse.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