2022-11-08 08:54:38

by Shang XiaoJing

[permalink] [raw]
Subject: [PATCH 0/2] tracing: Fix some bug about synth

Some bugs are found when insert synth_event_gen_test, and fixed by this
patch set.

Shang XiaoJing (2):
tracing: Fix memory leak in test_gen_synth_cmd() and
test_empty_synth_event()
tracing: Fix wild-memory-access in register_synth_event()

kernel/trace/synth_event_gen_test.c | 2 ++
kernel/trace/trace_events_synth.c | 2 ++
2 files changed, 4 insertions(+)

--
2.17.1



2022-11-08 09:09:44

by Shang XiaoJing

[permalink] [raw]
Subject: [PATCH 1/2] tracing: Fix memory leak in test_gen_synth_cmd() and test_empty_synth_event()

test_gen_synth_cmd() only free buf in fail path, hence buf will leak
when there is no failure. Add kfree(buf) to prevent the memleak. The
same reason and solution in test_empty_synth_event().

unreferenced object 0xffff8881127de000 (size 2048):
comm "modprobe", pid 247, jiffies 4294972316 (age 78.756s)
hex dump (first 32 bytes):
20 67 65 6e 5f 73 79 6e 74 68 5f 74 65 73 74 20 gen_synth_test
20 70 69 64 5f 74 20 6e 65 78 74 5f 70 69 64 5f pid_t next_pid_
backtrace:
[<000000004254801a>] kmalloc_trace+0x26/0x100
[<0000000039eb1cf5>] 0xffffffffa00083cd
[<000000000e8c3bc8>] 0xffffffffa00086ba
[<00000000c293d1ea>] do_one_initcall+0xdb/0x480
[<00000000aa189e6d>] do_init_module+0x1cf/0x680
[<00000000d513222b>] load_module+0x6a50/0x70a0
[<000000001fd4d529>] __do_sys_finit_module+0x12f/0x1c0
[<00000000b36c4c0f>] do_syscall_64+0x3f/0x90
[<00000000bbf20cf3>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
unreferenced object 0xffff8881127df000 (size 2048):
comm "modprobe", pid 247, jiffies 4294972324 (age 78.728s)
hex dump (first 32 bytes):
20 65 6d 70 74 79 5f 73 79 6e 74 68 5f 74 65 73 empty_synth_tes
74 20 20 70 69 64 5f 74 20 6e 65 78 74 5f 70 69 t pid_t next_pi
backtrace:
[<000000004254801a>] kmalloc_trace+0x26/0x100
[<00000000d4db9a3d>] 0xffffffffa0008071
[<00000000c31354a5>] 0xffffffffa00086ce
[<00000000c293d1ea>] do_one_initcall+0xdb/0x480
[<00000000aa189e6d>] do_init_module+0x1cf/0x680
[<00000000d513222b>] load_module+0x6a50/0x70a0
[<000000001fd4d529>] __do_sys_finit_module+0x12f/0x1c0
[<00000000b36c4c0f>] do_syscall_64+0x3f/0x90
[<00000000bbf20cf3>] entry_SYSCALL_64_after_hwframe+0x63/0xcd

Fixes: 9fe41efaca08 ("tracing: Add synth event generation test module")
Signed-off-by: Shang XiaoJing <[email protected]>
---
kernel/trace/synth_event_gen_test.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/kernel/trace/synth_event_gen_test.c b/kernel/trace/synth_event_gen_test.c
index 0b15e975d2c2..db1ec4809ad1 100644
--- a/kernel/trace/synth_event_gen_test.c
+++ b/kernel/trace/synth_event_gen_test.c
@@ -120,6 +120,7 @@ static int __init test_gen_synth_cmd(void)

/* Now generate a gen_synth_test event */
ret = synth_event_trace_array(gen_synth_test, vals, ARRAY_SIZE(vals));
+ kfree(buf);
out:
return ret;
delete:
@@ -227,6 +228,7 @@ static int __init test_empty_synth_event(void)

/* Now trace an empty_synth_test event */
ret = synth_event_trace_array(empty_synth_test, vals, ARRAY_SIZE(vals));
+ kfree(buf);
out:
return ret;
delete:
--
2.17.1


2022-11-12 21:23:21

by Tom Zanussi

[permalink] [raw]
Subject: Re: [PATCH 1/2] tracing: Fix memory leak in test_gen_synth_cmd() and test_empty_synth_event()

Hi Shang,

Thanks for finding this bug, comment below...

On Tue, 2022-11-08 at 16:31 +0800, Shang XiaoJing wrote:
> test_gen_synth_cmd() only free buf in fail path, hence buf will leak
> when there is no failure. Add kfree(buf) to prevent the memleak. The
> same reason and solution in test_empty_synth_event().
>
> unreferenced object 0xffff8881127de000 (size 2048):
>   comm "modprobe", pid 247, jiffies 4294972316 (age 78.756s)
>   hex dump (first 32 bytes):
>     20 67 65 6e 5f 73 79 6e 74 68 5f 74 65 73 74 20   gen_synth_test
>     20 70 69 64 5f 74 20 6e 65 78 74 5f 70 69 64 5f   pid_t next_pid_
>   backtrace:
>     [<000000004254801a>] kmalloc_trace+0x26/0x100
>     [<0000000039eb1cf5>] 0xffffffffa00083cd
>     [<000000000e8c3bc8>] 0xffffffffa00086ba
>     [<00000000c293d1ea>] do_one_initcall+0xdb/0x480
>     [<00000000aa189e6d>] do_init_module+0x1cf/0x680
>     [<00000000d513222b>] load_module+0x6a50/0x70a0
>     [<000000001fd4d529>] __do_sys_finit_module+0x12f/0x1c0
>     [<00000000b36c4c0f>] do_syscall_64+0x3f/0x90
>     [<00000000bbf20cf3>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
> unreferenced object 0xffff8881127df000 (size 2048):
>   comm "modprobe", pid 247, jiffies 4294972324 (age 78.728s)
>   hex dump (first 32 bytes):
>     20 65 6d 70 74 79 5f 73 79 6e 74 68 5f 74 65 73   empty_synth_tes
>     74 20 20 70 69 64 5f 74 20 6e 65 78 74 5f 70 69  t  pid_t next_pi
>   backtrace:
>     [<000000004254801a>] kmalloc_trace+0x26/0x100
>     [<00000000d4db9a3d>] 0xffffffffa0008071
>     [<00000000c31354a5>] 0xffffffffa00086ce
>     [<00000000c293d1ea>] do_one_initcall+0xdb/0x480
>     [<00000000aa189e6d>] do_init_module+0x1cf/0x680
>     [<00000000d513222b>] load_module+0x6a50/0x70a0
>     [<000000001fd4d529>] __do_sys_finit_module+0x12f/0x1c0
>     [<00000000b36c4c0f>] do_syscall_64+0x3f/0x90
>     [<00000000bbf20cf3>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
>
> Fixes: 9fe41efaca08 ("tracing: Add synth event generation test
> module")
> Signed-off-by: Shang XiaoJing <[email protected]>
> ---
>  kernel/trace/synth_event_gen_test.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/kernel/trace/synth_event_gen_test.c
> b/kernel/trace/synth_event_gen_test.c
> index 0b15e975d2c2..db1ec4809ad1 100644
> --- a/kernel/trace/synth_event_gen_test.c
> +++ b/kernel/trace/synth_event_gen_test.c
> @@ -120,6 +120,7 @@ static int __init test_gen_synth_cmd(void)
>  
>         /* Now generate a gen_synth_test event */
>         ret = synth_event_trace_array(gen_synth_test, vals,
> ARRAY_SIZE(vals));
> +       kfree(buf);
>   out:
>         return ret;
>   delete:
> @@ -227,6 +228,7 @@ static int __init test_empty_synth_event(void)
>  
>         /* Now trace an empty_synth_test event */
>         ret = synth_event_trace_array(empty_synth_test, vals,
> ARRAY_SIZE(vals));
> +       kfree(buf);
>   out:
>         return ret;
>   delete:

Makes sense, and if you do this then you could probably remove the
other kfree() at the bottom and clean the code up a bit, like change
this:

/* Now generate a gen_synth_test event */
ret = synth_event_trace_array(gen_synth_test, vals, ARRAY_SIZE(vals));
kfree(buf);
out:
return ret;
delete:
/* We got an error after creating the event, delete it */
synth_event_delete("gen_synth_test");
free:
kfree(buf);

goto out;
}

to this:

/* Now generate a gen_synth_test event */
ret = synth_event_trace_array(gen_synth_test, vals, ARRAY_SIZE(vals));
free:
kfree(buf);
return ret;
delete:
/* We got an error after creating the event, delete it */
synth_event_delete("gen_synth_test");
goto free;
}

What do you think?

Tom

2022-11-13 07:11:36

by Shang XiaoJing

[permalink] [raw]
Subject: Re: [PATCH 1/2] tracing: Fix memory leak in test_gen_synth_cmd() and test_empty_synth_event()



On 2022/11/13 5:12, Tom Zanussi wrote:
> Hi Shang,
>
> Thanks for finding this bug, comment below...
>
> On Tue, 2022-11-08 at 16:31 +0800, Shang XiaoJing wrote:
>> test_gen_synth_cmd() only free buf in fail path, hence buf will leak
>> when there is no failure. Add kfree(buf) to prevent the memleak. The
>> same reason and solution in test_empty_synth_event().
>>
>> unreferenced object 0xffff8881127de000 (size 2048):
>>   comm "modprobe", pid 247, jiffies 4294972316 (age 78.756s)
>>   hex dump (first 32 bytes):
>>     20 67 65 6e 5f 73 79 6e 74 68 5f 74 65 73 74 20   gen_synth_test
>>     20 70 69 64 5f 74 20 6e 65 78 74 5f 70 69 64 5f   pid_t next_pid_
>>   backtrace:
>>     [<000000004254801a>] kmalloc_trace+0x26/0x100
>>     [<0000000039eb1cf5>] 0xffffffffa00083cd
>>     [<000000000e8c3bc8>] 0xffffffffa00086ba
>>     [<00000000c293d1ea>] do_one_initcall+0xdb/0x480
>>     [<00000000aa189e6d>] do_init_module+0x1cf/0x680
>>     [<00000000d513222b>] load_module+0x6a50/0x70a0
>>     [<000000001fd4d529>] __do_sys_finit_module+0x12f/0x1c0
>>     [<00000000b36c4c0f>] do_syscall_64+0x3f/0x90
>>     [<00000000bbf20cf3>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
>> unreferenced object 0xffff8881127df000 (size 2048):
>>   comm "modprobe", pid 247, jiffies 4294972324 (age 78.728s)
>>   hex dump (first 32 bytes):
>>     20 65 6d 70 74 79 5f 73 79 6e 74 68 5f 74 65 73   empty_synth_tes
>>     74 20 20 70 69 64 5f 74 20 6e 65 78 74 5f 70 69  t  pid_t next_pi
>>   backtrace:
>>     [<000000004254801a>] kmalloc_trace+0x26/0x100
>>     [<00000000d4db9a3d>] 0xffffffffa0008071
>>     [<00000000c31354a5>] 0xffffffffa00086ce
>>     [<00000000c293d1ea>] do_one_initcall+0xdb/0x480
>>     [<00000000aa189e6d>] do_init_module+0x1cf/0x680
>>     [<00000000d513222b>] load_module+0x6a50/0x70a0
>>     [<000000001fd4d529>] __do_sys_finit_module+0x12f/0x1c0
>>     [<00000000b36c4c0f>] do_syscall_64+0x3f/0x90
>>     [<00000000bbf20cf3>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
>>
>> Fixes: 9fe41efaca08 ("tracing: Add synth event generation test
>> module")
>> Signed-off-by: Shang XiaoJing <[email protected]>
>> ---
>>  kernel/trace/synth_event_gen_test.c | 2 ++
>>  1 file changed, 2 insertions(+)
>>
>> diff --git a/kernel/trace/synth_event_gen_test.c
>> b/kernel/trace/synth_event_gen_test.c
>> index 0b15e975d2c2..db1ec4809ad1 100644
>> --- a/kernel/trace/synth_event_gen_test.c
>> +++ b/kernel/trace/synth_event_gen_test.c
>> @@ -120,6 +120,7 @@ static int __init test_gen_synth_cmd(void)
>>
>>         /* Now generate a gen_synth_test event */
>>         ret = synth_event_trace_array(gen_synth_test, vals,
>> ARRAY_SIZE(vals));
>> +       kfree(buf);
>>   out:
>>         return ret;
>>   delete:
>> @@ -227,6 +228,7 @@ static int __init test_empty_synth_event(void)
>>
>>         /* Now trace an empty_synth_test event */
>>         ret = synth_event_trace_array(empty_synth_test, vals,
>> ARRAY_SIZE(vals));
>> +       kfree(buf);
>>   out:
>>         return ret;
>>   delete:
>
> Makes sense, and if you do this then you could probably remove the
> other kfree() at the bottom and clean the code up a bit, like change
> this:
>
> /* Now generate a gen_synth_test event */
> ret = synth_event_trace_array(gen_synth_test, vals, ARRAY_SIZE(vals));
> kfree(buf);
> out:
> return ret;
> delete:
> /* We got an error after creating the event, delete it */
> synth_event_delete("gen_synth_test");
> free:
> kfree(buf);
>
> goto out;
> }
>
> to this:
>
> /* Now generate a gen_synth_test event */
> ret = synth_event_trace_array(gen_synth_test, vals, ARRAY_SIZE(vals));
> free:
> kfree(buf);
> return ret;
> delete:
> /* We got an error after creating the event, delete it */
> synth_event_delete("gen_synth_test");
> goto free;
> }
>
> What do you think?
>

Right, I thought maybe less change is better for the bugfix. It looks
better to carry some clean up, thanks for the review, will fix in v2.

Thanks,
--
Shang XiaoJing