All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tracing: Fix possible crash in ftrace_free_ftrace_ops()
@ 2022-05-08 16:18 Jeff Xie
  2022-05-09  1:33 ` Jeff Xie
  0 siblings, 1 reply; 4+ messages in thread
From: Jeff Xie @ 2022-05-08 16:18 UTC (permalink / raw)
  To: rostedt; +Cc: mingo, mhiramat, zanussi, linux-kernel, Jeff Xie

Currently if the ftrace_allocate_ftrace_ops() return -ENOMEM,
the ftrace_free_ftrace_ops() will kfree(NULL).
 
trace_array_create()
{
	...
	if (ftrace_allocate_ftrace_ops(tr) < 0)
		goto out_free_tr;
	...
out_free_tr:
        ftrace_free_ftrace_ops(tr);
	...
}

ftrace_allocate_ftrace_ops()
{
	...
	ops = kzalloc(sizeof(*ops), GFP_KERNEL);
        if (!ops)
                return -ENOMEM;
	...
}

ftrace_free_ftrace_ops()
{
        kfree(tr->ops);
        tr->ops = NULL;
}

Signed-off-by: Jeff Xie <xiehuan09@gmail.com>
---
 kernel/trace/trace_functions.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/kernel/trace/trace_functions.c b/kernel/trace/trace_functions.c
index 9f1bfbe105e8..d186d6101695 100644
--- a/kernel/trace/trace_functions.c
+++ b/kernel/trace/trace_functions.c
@@ -73,6 +73,9 @@ int ftrace_allocate_ftrace_ops(struct trace_array *tr)
 
 void ftrace_free_ftrace_ops(struct trace_array *tr)
 {
+	if (!tr->ops)
+		return;
+
 	kfree(tr->ops);
 	tr->ops = NULL;
 }
-- 
2.25.1


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

* Re: [PATCH] tracing: Fix possible crash in ftrace_free_ftrace_ops()
  2022-05-08 16:18 [PATCH] tracing: Fix possible crash in ftrace_free_ftrace_ops() Jeff Xie
@ 2022-05-09  1:33 ` Jeff Xie
  2022-05-09  7:34   ` Ammar Faizi
  2022-05-09 22:54   ` Steven Rostedt
  0 siblings, 2 replies; 4+ messages in thread
From: Jeff Xie @ 2022-05-09  1:33 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: mingo, Masami Hiramatsu, Tom Zanussi, linux-kernel

I am so sorry to bother you, this may be an invalid patch, kfree can
return directly from null. ;-)

On Mon, May 9, 2022 at 12:18 AM Jeff Xie <xiehuan09@gmail.com> wrote:
>
> Currently if the ftrace_allocate_ftrace_ops() return -ENOMEM,
> the ftrace_free_ftrace_ops() will kfree(NULL).
>
> trace_array_create()
> {
>         ...
>         if (ftrace_allocate_ftrace_ops(tr) < 0)
>                 goto out_free_tr;
>         ...
> out_free_tr:
>         ftrace_free_ftrace_ops(tr);
>         ...
> }
>
> ftrace_allocate_ftrace_ops()
> {
>         ...
>         ops = kzalloc(sizeof(*ops), GFP_KERNEL);
>         if (!ops)
>                 return -ENOMEM;
>         ...
> }
>
> ftrace_free_ftrace_ops()
> {
>         kfree(tr->ops);
>         tr->ops = NULL;
> }
>
> Signed-off-by: Jeff Xie <xiehuan09@gmail.com>
> ---
>  kernel/trace/trace_functions.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/kernel/trace/trace_functions.c b/kernel/trace/trace_functions.c
> index 9f1bfbe105e8..d186d6101695 100644
> --- a/kernel/trace/trace_functions.c
> +++ b/kernel/trace/trace_functions.c
> @@ -73,6 +73,9 @@ int ftrace_allocate_ftrace_ops(struct trace_array *tr)
>
>  void ftrace_free_ftrace_ops(struct trace_array *tr)
>  {
> +       if (!tr->ops)
> +               return;
> +
>         kfree(tr->ops);
>         tr->ops = NULL;
>  }
> --
> 2.25.1
>

Thanks,
JeffXie

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

* Re: [PATCH] tracing: Fix possible crash in ftrace_free_ftrace_ops()
  2022-05-09  1:33 ` Jeff Xie
@ 2022-05-09  7:34   ` Ammar Faizi
  2022-05-09 22:54   ` Steven Rostedt
  1 sibling, 0 replies; 4+ messages in thread
From: Ammar Faizi @ 2022-05-09  7:34 UTC (permalink / raw)
  To: Jeff Xie
  Cc: Ingo Molnar, Masami Hiramatsu, Steven Rostedt, Tom Zanussi,
	Linux Kernel Mailing List

On 5/9/22 8:33 AM, Jeff Xie wrote:
> I am so sorry to bother you, this may be an invalid patch, kfree can
> return directly from null. ;-)

Yeah, kfree(NULL) is a valid call. There's no potential crash here.

-- 
Ammar Faizi

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

* Re: [PATCH] tracing: Fix possible crash in ftrace_free_ftrace_ops()
  2022-05-09  1:33 ` Jeff Xie
  2022-05-09  7:34   ` Ammar Faizi
@ 2022-05-09 22:54   ` Steven Rostedt
  1 sibling, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2022-05-09 22:54 UTC (permalink / raw)
  To: Jeff Xie; +Cc: mingo, Masami Hiramatsu, Tom Zanussi, linux-kernel

On Mon, 9 May 2022 09:33:44 +0800
Jeff Xie <xiehuan09@gmail.com> wrote:

> I am so sorry to bother you, this may be an invalid patch, kfree can
> return directly from null. ;-)

Yes, and so does free() in user space glibc.

-- Steve

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

end of thread, other threads:[~2022-05-09 22:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-08 16:18 [PATCH] tracing: Fix possible crash in ftrace_free_ftrace_ops() Jeff Xie
2022-05-09  1:33 ` Jeff Xie
2022-05-09  7:34   ` Ammar Faizi
2022-05-09 22:54   ` Steven Rostedt

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.