ftrace: Fix null pointer dereference in ftrace_add_mod()

Message ID 20221116015207.30858-1-xiujianfeng@huawei.com
State New
Headers
Series ftrace: Fix null pointer dereference in ftrace_add_mod() |

Commit Message

Xiu Jianfeng Nov. 16, 2022, 1:52 a.m. UTC
  The @ftrace_mod is allocated by kzalloc(), so both the members {prev,next}
of @ftrace_mode->list are NULL, it's not a valid state to call list_del().
If kstrdup() for @ftrace_mod->{func|module} fails, it goes to @out_free
tag and calls free_ftrace_mod() to destroy @ftrace_mod, then list_del()
will write prev->next and next->prev, where null pointer dereference
happens.

BUG: kernel NULL pointer dereference, address: 0000000000000008
Oops: 0002 [#1] PREEMPT SMP NOPTI
Call Trace:
 <TASK>
 ftrace_mod_callback+0x20d/0x220
 ? do_filp_open+0xd9/0x140
 ftrace_process_regex.isra.51+0xbf/0x130
 ftrace_regex_write.isra.52.part.53+0x6e/0x90
 vfs_write+0xee/0x3a0
 ? __audit_filter_op+0xb1/0x100
 ? auditd_test_task+0x38/0x50
 ksys_write+0xa5/0xe0
 do_syscall_64+0x3a/0x90
 entry_SYSCALL_64_after_hwframe+0x63/0xcd
Kernel panic - not syncing: Fatal exception

So call INIT_LIST_HEAD() to initialize the list member to fix this issue.

Fixes: 673feb9d76ab ("ftrace: Add :mod: caching infrastructure to trace_array")
Signed-off-by: Xiu Jianfeng <xiujianfeng@huawei.com>
---
 kernel/trace/ftrace.c | 1 +
 1 file changed, 1 insertion(+)
  

Comments

Steven Rostedt Nov. 17, 2022, 10:17 p.m. UTC | #1
On Wed, 16 Nov 2022 09:52:07 +0800
Xiu Jianfeng <xiujianfeng@huawei.com> wrote:

> The @ftrace_mod is allocated by kzalloc(), so both the members {prev,next}
> of @ftrace_mode->list are NULL, it's not a valid state to call list_del().
> If kstrdup() for @ftrace_mod->{func|module} fails, it goes to @out_free
> tag and calls free_ftrace_mod() to destroy @ftrace_mod, then list_del()
> will write prev->next and next->prev, where null pointer dereference
> happens.
> 
> BUG: kernel NULL pointer dereference, address: 0000000000000008
> Oops: 0002 [#1] PREEMPT SMP NOPTI
> Call Trace:
>  <TASK>
>  ftrace_mod_callback+0x20d/0x220
>  ? do_filp_open+0xd9/0x140
>  ftrace_process_regex.isra.51+0xbf/0x130
>  ftrace_regex_write.isra.52.part.53+0x6e/0x90
>  vfs_write+0xee/0x3a0
>  ? __audit_filter_op+0xb1/0x100
>  ? auditd_test_task+0x38/0x50
>  ksys_write+0xa5/0xe0
>  do_syscall_64+0x3a/0x90
>  entry_SYSCALL_64_after_hwframe+0x63/0xcd
> Kernel panic - not syncing: Fatal exception
> 
> So call INIT_LIST_HEAD() to initialize the list member to fix this issue.
> 
> Fixes: 673feb9d76ab ("ftrace: Add :mod: caching infrastructure to trace_array")
> Signed-off-by: Xiu Jianfeng <xiujianfeng@huawei.com>
> ---


Queued. Thanks Xiu!

-- Steve
  

Patch

diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index 705b990d264d..9af0c4065b4e 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -1289,6 +1289,7 @@  static int ftrace_add_mod(struct trace_array *tr,
 	if (!ftrace_mod)
 		return -ENOMEM;
 
+	INIT_LIST_HEAD(&ftrace_mod->list);
 	ftrace_mod->func = kstrdup(func, GFP_KERNEL);
 	ftrace_mod->module = kstrdup(module, GFP_KERNEL);
 	ftrace_mod->enable = enable;