]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tools/power x86_energy_perf_policy: Fix potential NULL pointer dereference
authorMalaya Kumar Rout <mrout@redhat.com>
Sat, 22 Nov 2025 15:16:52 +0000 (20:46 +0530)
committerLen Brown <len.brown@intel.com>
Tue, 2 Dec 2025 21:11:09 +0000 (16:11 -0500)
In err_on_hypervisor(), strstr() is called to search for "flags" in the
buffer, but the return value is not checked before being used in pointer
arithmetic (flags - buffer). If strstr() returns NULL because "flags" is
not found in /proc/cpuinfo, this will cause undefined behavior and likely
a crash.

Add a NULL check after the strstr() call and handle the error appropriately
by cleaning up resources and reporting a meaningful error message.

Signed-off-by: Malaya Kumar Rout <mrout@redhat.com>
Signed-off-by: Len Brown <len.brown@intel.com>
tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c

index b2125275c69e0dd496fe4e1d48870b9561c8a264..ac37132207a47c90d88b7952bd7583f2ea5cd1a6 100644 (file)
@@ -520,7 +520,7 @@ void for_packages(unsigned long long pkg_set, int (func)(int))
 
 void print_version(void)
 {
-       printf("x86_energy_perf_policy 2025.9.19 Len Brown <lenb@kernel.org>\n");
+       printf("x86_energy_perf_policy 2025.11.22 Len Brown <lenb@kernel.org>\n");
 }
 
 void cmdline(int argc, char **argv)
@@ -662,6 +662,11 @@ void err_on_hypervisor(void)
        }
 
        flags = strstr(buffer, "flags");
+       if (!flags) {
+               fclose(cpuinfo);
+               free(buffer);
+               err(1, "Failed to find 'flags' in /proc/cpuinfo");
+       }
        rewind(cpuinfo);
        fseek(cpuinfo, flags - buffer, SEEK_SET);
        if (!fgets(buffer, 4096, cpuinfo)) {