Skip to contentSkip to conversation

tracing: Fix possible crash in ftrace_free_ftrace_ops()

PATCH In review Jeff Xie 3 participants 4 messages May 8, 2022 – May 9, 2022 Tracing

lore
Share & tools
lore

Jeff Xie

author
#0 lore raw

PATCH 1/1 tracing: Fix possible crash in ftrace_free_ftrace_ops()

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;
}
---
 kernel/trace/trace_functions.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/trace/trace_functions.c +3 -0
index 9f1bfbe105e8..d186d6101695 100644
@@ -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;
 }
Signature · 1 line
2.25.1
author
#1 lore raw

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

Quoted 50 lines · 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;
}
---
 kernel/trace/trace_functions.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/trace/trace_functions.c +3 -1
index 9f1bfbe105e8..d186d6101695 100644
@@ -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;
 }
--
Quoted 2 lines

2.25.1

Thanks,
JeffXie

#2 lore raw
Quoted 2 lines · Jeff Xie

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.

Signature · 1 line
Ammar Faizi
maintainer
#3 lore raw
In reply to Jeff Xie
Quoted 2 lines · On Mon, 9 May 2022 09:33:44 +0800 Jeff Xie <[email protected]> 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