mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] tools/power x86_energy_perf_policy: Fix potential NULL pointer dereference
@ 2025-11-22 15:16 Malaya Kumar Rout
  2025-11-25 17:23 ` Len Brown
  0 siblings, 1 reply; 3+ messages in thread
From: Malaya Kumar Rout @ 2025-11-22 15:16 UTC (permalink / raw)
  To: linux-kernel; +Cc: mrout, malayarout91, Len Brown, Kaushlendra Kumar

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>
---
 .../x86/x86_energy_perf_policy/x86_energy_perf_policy.c      | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c b/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
index 884a4c746f32..be2a9c285a85 100644
--- a/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
+++ b/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
@@ -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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] tools/power x86_energy_perf_policy: Fix potential NULL pointer dereference
  2025-11-22 15:16 [PATCH] tools/power x86_energy_perf_policy: Fix potential NULL pointer dereference Malaya Kumar Rout
@ 2025-11-25 17:23 ` Len Brown
  2025-11-26 15:34   ` Malaya Kumar Rout
  0 siblings, 1 reply; 3+ messages in thread
From: Len Brown @ 2025-11-25 17:23 UTC (permalink / raw)
  To: Malaya Kumar Rout
  Cc: linux-kernel, malayarout91, Len Brown, Kaushlendra Kumar

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

On Sat, Nov 22, 2025 at 10:17 AM Malaya Kumar Rout <mrout@redhat.com> wrote:
>
> 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>
> ---
>  .../x86/x86_energy_perf_policy/x86_energy_perf_policy.c      | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c b/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
> index 884a4c746f32..be2a9c285a85 100644
> --- a/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
> +++ b/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
> @@ -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
>
>


-- 
Len Brown, Intel Open Source Technology Center

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] tools/power x86_energy_perf_policy: Fix potential NULL pointer dereference
  2025-11-25 17:23 ` Len Brown
@ 2025-11-26 15:34   ` Malaya Kumar Rout
  0 siblings, 0 replies; 3+ messages in thread
From: Malaya Kumar Rout @ 2025-11-26 15:34 UTC (permalink / raw)
  To: Len Brown; +Cc: linux-kernel, malayarout91, Len Brown, Kaushlendra Kumar

On Tue, Nov 25, 2025 at 10:53 PM Len Brown <lenb@kernel.org> wrote:
>
> 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.
>
> On Sat, Nov 22, 2025 at 10:17 AM Malaya Kumar Rout <mrout@redhat.com> wrote:
> >
> > 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>
> > ---
> >  .../x86/x86_energy_perf_policy/x86_energy_perf_policy.c      | 5 +++++
> >  1 file changed, 5 insertions(+)
> >
> > diff --git a/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c b/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
> > index 884a4c746f32..be2a9c285a85 100644
> > --- a/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
> > +++ b/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
> > @@ -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
> >
> >
>
>
> --
> Len Brown, Intel Open Source Technology Center
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-11-26 15:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-22 15:16 [PATCH] tools/power x86_energy_perf_policy: Fix potential NULL pointer dereference Malaya Kumar Rout
2025-11-25 17:23 ` Len Brown
2025-11-26 15:34   ` 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

all inboxes | Powered by JetHome®