Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/power x86_energy_perf_policy: Fix potential NULL pointer dereference

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>

authored by

Malaya Kumar Rout and committed by
Len Brown
51860d63 7446bd61

+6 -1
+6 -1
tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
··· 520 520 521 521 void print_version(void) 522 522 { 523 - printf("x86_energy_perf_policy 2025.9.19 Len Brown <lenb@kernel.org>\n"); 523 + printf("x86_energy_perf_policy 2025.11.22 Len Brown <lenb@kernel.org>\n"); 524 524 } 525 525 526 526 void cmdline(int argc, char **argv) ··· 662 662 } 663 663 664 664 flags = strstr(buffer, "flags"); 665 + if (!flags) { 666 + fclose(cpuinfo); 667 + free(buffer); 668 + err(1, "Failed to find 'flags' in /proc/cpuinfo"); 669 + } 665 670 rewind(cpuinfo); 666 671 fseek(cpuinfo, flags - buffer, SEEK_SET); 667 672 if (!fgets(buffer, 4096, cpuinfo)) {