[v2] tracing: fix memory leak in tracing_read_pipe

Message ID 1667819090-4643-1-git-send-email-wangyufen@huawei.com
State New
Headers
Series [v2] tracing: fix memory leak in tracing_read_pipe |

Commit Message

wangyufen Nov. 7, 2022, 11:04 a.m. UTC
  kmemleak reports this issue:

unreferenced object 0xffff888105a18900 (size 128):
  comm "test_progs", pid 18933, jiffies 4336275356 (age 22801.766s)
  hex dump (first 32 bytes):
    25 73 00 90 81 88 ff ff 26 05 00 00 42 01 58 04  %s......&...B.X.
    03 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00  ................
  backtrace:
    [<00000000560143a1>] __kmalloc_node_track_caller+0x4a/0x140
    [<000000006af00822>] krealloc+0x8d/0xf0
    [<00000000c309be6a>] trace_iter_expand_format+0x99/0x150
    [<000000005a53bdb6>] trace_check_vprintf+0x1e0/0x11d0
    [<0000000065629d9d>] trace_event_printf+0xb6/0xf0
    [<000000009a690dc7>] trace_raw_output_bpf_trace_printk+0x89/0xc0
    [<00000000d22db172>] print_trace_line+0x73c/0x1480
    [<00000000cdba76ba>] tracing_read_pipe+0x45c/0x9f0
    [<0000000015b58459>] vfs_read+0x17b/0x7c0
    [<000000004aeee8ed>] ksys_read+0xed/0x1c0
    [<0000000063d3d898>] do_syscall_64+0x3b/0x90
    [<00000000a06dda7f>] entry_SYSCALL_64_after_hwframe+0x63/0xcd

iter->fmt alloced in
  tracing_read_pipe() -> .. ->trace_iter_expand_format(), but not
freed, to fix, add free in tracing_release_pipe()

Signed-off-by: Wang Yufen <wangyufen@huawei.com>
---
v1 -> v2: del if (iter->fmt), free iter->fmt directly
 kernel/trace/trace.c | 1 +
 1 file changed, 1 insertion(+)
  

Comments

wangyufen Nov. 7, 2022, 11:45 a.m. UTC | #1
在 2022/11/7 19:04, Wang Yufen 写道:
> kmemleak reports this issue:
>
> unreferenced object 0xffff888105a18900 (size 128):
>    comm "test_progs", pid 18933, jiffies 4336275356 (age 22801.766s)
>    hex dump (first 32 bytes):
>      25 73 00 90 81 88 ff ff 26 05 00 00 42 01 58 04  %s......&...B.X.
>      03 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00  ................
>    backtrace:
>      [<00000000560143a1>] __kmalloc_node_track_caller+0x4a/0x140
>      [<000000006af00822>] krealloc+0x8d/0xf0
>      [<00000000c309be6a>] trace_iter_expand_format+0x99/0x150
>      [<000000005a53bdb6>] trace_check_vprintf+0x1e0/0x11d0
>      [<0000000065629d9d>] trace_event_printf+0xb6/0xf0
>      [<000000009a690dc7>] trace_raw_output_bpf_trace_printk+0x89/0xc0
>      [<00000000d22db172>] print_trace_line+0x73c/0x1480
>      [<00000000cdba76ba>] tracing_read_pipe+0x45c/0x9f0
>      [<0000000015b58459>] vfs_read+0x17b/0x7c0
>      [<000000004aeee8ed>] ksys_read+0xed/0x1c0
>      [<0000000063d3d898>] do_syscall_64+0x3b/0x90
>      [<00000000a06dda7f>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
>
> iter->fmt alloced in
>    tracing_read_pipe() -> .. ->trace_iter_expand_format(), but not
> freed, to fix, add free in tracing_release_pipe()
Fixes: efbbdaa22bb7 ("tracing: Show real address for trace event arguments")
> Signed-off-by: Wang Yufen <wangyufen@huawei.com>
> ---
> v1 -> v2: del if (iter->fmt), free iter->fmt directly
>   kernel/trace/trace.c | 1 +
>   1 file changed, 1 insertion(+)
>
> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> index 47a44b0..f9d98e7 100644
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
> @@ -6657,6 +6657,7 @@ static int tracing_release_pipe(struct inode *inode, struct file *file)
>   	mutex_unlock(&trace_types_lock);
>   
>   	free_cpumask_var(iter->started);
> +	kfree(iter->fmt);
>   	mutex_destroy(&iter->mutex);
>   	kfree(iter);
>
  
Masami Hiramatsu (Google) Nov. 7, 2022, 1:17 p.m. UTC | #2
On Mon, 7 Nov 2022 19:45:04 +0800
wangyufen <wangyufen@huawei.com> wrote:

> 
> 在 2022/11/7 19:04, Wang Yufen 写道:
> > kmemleak reports this issue:
> >
> > unreferenced object 0xffff888105a18900 (size 128):
> >    comm "test_progs", pid 18933, jiffies 4336275356 (age 22801.766s)
> >    hex dump (first 32 bytes):
> >      25 73 00 90 81 88 ff ff 26 05 00 00 42 01 58 04  %s......&...B.X.
> >      03 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00  ................
> >    backtrace:
> >      [<00000000560143a1>] __kmalloc_node_track_caller+0x4a/0x140
> >      [<000000006af00822>] krealloc+0x8d/0xf0
> >      [<00000000c309be6a>] trace_iter_expand_format+0x99/0x150
> >      [<000000005a53bdb6>] trace_check_vprintf+0x1e0/0x11d0
> >      [<0000000065629d9d>] trace_event_printf+0xb6/0xf0
> >      [<000000009a690dc7>] trace_raw_output_bpf_trace_printk+0x89/0xc0
> >      [<00000000d22db172>] print_trace_line+0x73c/0x1480
> >      [<00000000cdba76ba>] tracing_read_pipe+0x45c/0x9f0
> >      [<0000000015b58459>] vfs_read+0x17b/0x7c0
> >      [<000000004aeee8ed>] ksys_read+0xed/0x1c0
> >      [<0000000063d3d898>] do_syscall_64+0x3b/0x90
> >      [<00000000a06dda7f>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
> >
> > iter->fmt alloced in
> >    tracing_read_pipe() -> .. ->trace_iter_expand_format(), but not
> > freed, to fix, add free in tracing_release_pipe()
> Fixes: efbbdaa22bb7 ("tracing: Show real address for trace event arguments")

Oops, good catch!

Cc: stable@vger.kernel.org

Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Thank you,

> > Signed-off-by: Wang Yufen <wangyufen@huawei.com>
> > ---
> > v1 -> v2: del if (iter->fmt), free iter->fmt directly
> >   kernel/trace/trace.c | 1 +
> >   1 file changed, 1 insertion(+)
> >
> > diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> > index 47a44b0..f9d98e7 100644
> > --- a/kernel/trace/trace.c
> > +++ b/kernel/trace/trace.c
> > @@ -6657,6 +6657,7 @@ static int tracing_release_pipe(struct inode *inode, struct file *file)
> >   	mutex_unlock(&trace_types_lock);
> >   
> >   	free_cpumask_var(iter->started);
> > +	kfree(iter->fmt);
> >   	mutex_destroy(&iter->mutex);
> >   	kfree(iter);
> >
  
Steven Rostedt Nov. 17, 2022, 2:36 a.m. UTC | #3
On Mon, 7 Nov 2022 22:17:10 +0900
Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote:

> Oops, good catch!
> 
> Cc: stable@vger.kernel.org
> 
> Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Queued, thanks Wang and Masami!

-- Steve
  

Patch

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 47a44b0..f9d98e7 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -6657,6 +6657,7 @@  static int tracing_release_pipe(struct inode *inode, struct file *file)
 	mutex_unlock(&trace_types_lock);
 
 	free_cpumask_var(iter->started);
+	kfree(iter->fmt);
 	mutex_destroy(&iter->mutex);
 	kfree(iter);