tools/power x86_energy_perf_policy: Fix potential NULL pointer dereference
PATCH 1/1 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.
--- .../x86/x86_energy_perf_policy/x86_energy_perf_policy.c | 5 +++++ 1 file changed, 5 insertions(+)
tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c +5 -0
@@ -660,6 +660,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)) {
Signature · 1 line
2.51.0
Len Brown
AppliedI've applied these two patches, thanks.
BTW. we generally use the linux-pm list for patches to these utilities rather than lkml -- as we manage the patches there via patchwork.
-Len
Quoted 35 lines · Malaya Kumar Rout
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). Ifstrstr()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 <[email protected]>--- .../x86/x86_energy_perf_policy/x86_energy_perf_policy.c | 5 +++++ 1 file changed, 5 insertions(+)tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c +5
-1@@ -660,6 +660,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)) { --2.51.0
Signature · 1 line
Len Brown, Intel Open Source Technology Center
Quoted 7 lines · Len Brown
I've applied these two patches, thanks.
BTW. we generally use the linux-pm list for patches to these utilities rather than lkml -- as we manage the patches there via patchwork.
-Len
Thanks for the clarification. Sorry for posting it to LKML.
I will send future patches for these utilities to linux-pm.
Quoted 42 lines · Malaya Kumar Rout
Quoted 32 lines
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). Ifstrstr()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 <[email protected]>--- .../x86/x86_energy_perf_policy/x86_energy_perf_policy.c | 5 +++++ 1 file changed, 5 insertions(+)tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c +5
-1@@ -660,6 +660,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)) { --Quoted 3 lines
2.51.0
--
Len Brown, Intel Open Source Technology Center