Linux Power Management development
 help / color / mirror / Atom feed
* [PATCH] cpupower: Fix NULL pointer dereference on allocation failure
@ 2026-07-04  8:43 Malaya Kumar Rout
  0 siblings, 0 replies; only message in thread
From: Malaya Kumar Rout @ 2026-07-04  8:43 UTC (permalink / raw)
  To: linux-kernel, linux-pm
  Cc: mrout, skhan, me, Malaya Kumar Rout, Thomas Renninger, Shuah Khan,
	John B. Wyatt IV, John Kacur, Brahadambal Srinivasan

The bitmask_alloc() function can return NULL if memory allocation fails,
but the code in main() does not check the return values before using the
allocated bitmasks. This can lead to a NULL pointer dereference when
bitmask_setall() or bitmask_parselist() is called with NULL pointers.

Add proper NULL checks after bitmask allocations and exit gracefully with
an error message if allocation fails.

Fixes: 748f0d70087c ("cpupower: Provide online and offline CPU information")
Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
---
 tools/power/cpupower/utils/cpupower.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/tools/power/cpupower/utils/cpupower.c b/tools/power/cpupower/utils/cpupower.c
index 9ec973165af1..8f7b18b8bb43 100644
--- a/tools/power/cpupower/utils/cpupower.c
+++ b/tools/power/cpupower/utils/cpupower.c
@@ -184,6 +184,17 @@ int main(int argc, const char *argv[])
 	online_cpus = bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF));
 	offline_cpus = bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF));
 
+	if (!cpus_chosen || !online_cpus || !offline_cpus) {
+		fprintf(stderr, _("Failed to allocate bitmasks\n"));
+		if (cpus_chosen)
+			bitmask_free(cpus_chosen);
+		if (online_cpus)
+			bitmask_free(online_cpus);
+		if (offline_cpus)
+			bitmask_free(offline_cpus);
+		return EXIT_FAILURE;
+	}
+
 	argc--;
 	argv += 1;
 
-- 
2.54.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-07-04  8:43 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-04  8:43 [PATCH] cpupower: Fix NULL pointer dereference on allocation failure Malaya Kumar Rout

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox