linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] copy_process(): fix a memleak in copy_process()
@ 2022-10-17  6:34 Gaosheng Cui
  2022-10-17  7:18 ` Christian Brauner
  0 siblings, 1 reply; 3+ messages in thread
From: Gaosheng Cui @ 2022-10-17  6:34 UTC (permalink / raw)
  To: brauner, akpm, tglx, ebiederm, luto, bigeasy, Liam.Howlett,
	fenghua.yu, peterz, viro, jannh, cuigaosheng1
  Cc: linux-kernel

If CLONE_PIDFD is set in clone_flags, pidfile will hold the reference
count of pid by getpid(pid), In the error path bad_fork_put_pidfd, the
reference of pid needs to be released, otherwise there will be a
memleak issue, fix it.

unreferenced object 0xffff888164aed400 (size 224):
  comm "sh", pid 75274, jiffies 4295717290 (age 2955.536s)
  hex dump (first 32 bytes):
    01 00 00 00 00 00 00 00 00 00 00 00 ad 4e ad de  .............N..
    ff ff ff ff 00 00 00 00 ff ff ff ff ff ff ff ff  ................
  backtrace:
    [<00000000bcb9eebb>] kmem_cache_alloc+0x16a/0x7f0
    [<00000000340cf9ad>] alloc_pid+0xc5/0xce0
    [<000000002387362c>] copy_process+0x29ef/0x6c90
    [<00000000bf7d7efc>] kernel_clone+0xd9/0xc70
    [<0000000047b1a04f>] __do_sys_clone+0xe1/0x120
    [<0000000000f1aa25>] __x64_sys_clone+0xc3/0x150
    [<00000000250a19f1>] do_syscall_64+0x5c/0x90
    [<000000007e0ac417>] entry_SYSCALL_64_after_hwframe+0x63/0xcd

Fixes: 6fd2fe494b17 ("copy_process(): don't use ksys_close() on cleanups")
Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
---
 kernel/fork.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/kernel/fork.c b/kernel/fork.c
index 08969f5aa38d..8706c06be8af 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -2499,6 +2499,7 @@ static __latent_entropy struct task_struct *copy_process(
 	cgroup_cancel_fork(p, args);
 bad_fork_put_pidfd:
 	if (clone_flags & CLONE_PIDFD) {
+		put_pid(pid);
 		fput(pidfile);
 		put_unused_fd(pidfd);
 	}
-- 
2.25.1


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

* Re: [PATCH] copy_process(): fix a memleak in copy_process()
  2022-10-17  6:34 [PATCH] copy_process(): fix a memleak in copy_process() Gaosheng Cui
@ 2022-10-17  7:18 ` Christian Brauner
  2022-10-18  2:26   ` cuigaosheng
  0 siblings, 1 reply; 3+ messages in thread
From: Christian Brauner @ 2022-10-17  7:18 UTC (permalink / raw)
  To: Gaosheng Cui
  Cc: akpm, tglx, ebiederm, luto, bigeasy, Liam.Howlett, fenghua.yu,
	peterz, viro, jannh, linux-kernel

On Mon, Oct 17, 2022 at 02:34:06PM +0800, Gaosheng Cui wrote:
> If CLONE_PIDFD is set in clone_flags, pidfile will hold the reference
> count of pid by getpid(pid), In the error path bad_fork_put_pidfd, the
> reference of pid needs to be released, otherwise there will be a
> memleak issue, fix it.
> 
> unreferenced object 0xffff888164aed400 (size 224):
>   comm "sh", pid 75274, jiffies 4295717290 (age 2955.536s)
>   hex dump (first 32 bytes):
>     01 00 00 00 00 00 00 00 00 00 00 00 ad 4e ad de  .............N..
>     ff ff ff ff 00 00 00 00 ff ff ff ff ff ff ff ff  ................
>   backtrace:
>     [<00000000bcb9eebb>] kmem_cache_alloc+0x16a/0x7f0
>     [<00000000340cf9ad>] alloc_pid+0xc5/0xce0
>     [<000000002387362c>] copy_process+0x29ef/0x6c90
>     [<00000000bf7d7efc>] kernel_clone+0xd9/0xc70
>     [<0000000047b1a04f>] __do_sys_clone+0xe1/0x120
>     [<0000000000f1aa25>] __x64_sys_clone+0xc3/0x150
>     [<00000000250a19f1>] do_syscall_64+0x5c/0x90
>     [<000000007e0ac417>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
> 
> Fixes: 6fd2fe494b17 ("copy_process(): don't use ksys_close() on cleanups")
> Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
> ---
>  kernel/fork.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/kernel/fork.c b/kernel/fork.c
> index 08969f5aa38d..8706c06be8af 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -2499,6 +2499,7 @@ static __latent_entropy struct task_struct *copy_process(
>  	cgroup_cancel_fork(p, args);
>  bad_fork_put_pidfd:
>  	if (clone_flags & CLONE_PIDFD) {
> +		put_pid(pid);

This will be released during __fput() when ->release() ==
pidfd_release() is called so calling put_pid() here will result in UAF
later on. What syzbot instance does this report come from?

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

* Re: [PATCH] copy_process(): fix a memleak in copy_process()
  2022-10-17  7:18 ` Christian Brauner
@ 2022-10-18  2:26   ` cuigaosheng
  0 siblings, 0 replies; 3+ messages in thread
From: cuigaosheng @ 2022-10-18  2:26 UTC (permalink / raw)
  To: Christian Brauner
  Cc: akpm, tglx, ebiederm, luto, bigeasy, Liam.Howlett, fenghua.yu,
	peterz, viro, jannh, linux-kernel

> This will be released during __fput() when ->release() ==
> pidfd_release() is called so calling put_pid() here will result in UAF
> later on. What syzbot instance does this report come from?
> .

Thanks for taking time to review the patch, kmemleak report this when
I'm doing some kernel tests, unfortunately, I didn't realize that
pidfd_release would release pid. Perhaps there is another reason for this
issue, but I'm not sure now, I will try to prove it.

By the way, do you have any ideas about it? such as "->release() == NULL"?

Thanks very much!

On 2022/10/17 15:18, Christian Brauner wrote:
> On Mon, Oct 17, 2022 at 02:34:06PM +0800, Gaosheng Cui wrote:
>> If CLONE_PIDFD is set in clone_flags, pidfile will hold the reference
>> count of pid by getpid(pid), In the error path bad_fork_put_pidfd, the
>> reference of pid needs to be released, otherwise there will be a
>> memleak issue, fix it.
>>
>> unreferenced object 0xffff888164aed400 (size 224):
>>    comm "sh", pid 75274, jiffies 4295717290 (age 2955.536s)
>>    hex dump (first 32 bytes):
>>      01 00 00 00 00 00 00 00 00 00 00 00 ad 4e ad de  .............N..
>>      ff ff ff ff 00 00 00 00 ff ff ff ff ff ff ff ff  ................
>>    backtrace:
>>      [<00000000bcb9eebb>] kmem_cache_alloc+0x16a/0x7f0
>>      [<00000000340cf9ad>] alloc_pid+0xc5/0xce0
>>      [<000000002387362c>] copy_process+0x29ef/0x6c90
>>      [<00000000bf7d7efc>] kernel_clone+0xd9/0xc70
>>      [<0000000047b1a04f>] __do_sys_clone+0xe1/0x120
>>      [<0000000000f1aa25>] __x64_sys_clone+0xc3/0x150
>>      [<00000000250a19f1>] do_syscall_64+0x5c/0x90
>>      [<000000007e0ac417>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
>>
>> Fixes: 6fd2fe494b17 ("copy_process(): don't use ksys_close() on cleanups")
>> Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
>> ---
>>   kernel/fork.c | 1 +
>>   1 file changed, 1 insertion(+)
>>
>> diff --git a/kernel/fork.c b/kernel/fork.c
>> index 08969f5aa38d..8706c06be8af 100644
>> --- a/kernel/fork.c
>> +++ b/kernel/fork.c
>> @@ -2499,6 +2499,7 @@ static __latent_entropy struct task_struct *copy_process(
>>   	cgroup_cancel_fork(p, args);
>>   bad_fork_put_pidfd:
>>   	if (clone_flags & CLONE_PIDFD) {
>> +		put_pid(pid);
> This will be released during __fput() when ->release() ==
> pidfd_release() is called so calling put_pid() here will result in UAF
> later on. What syzbot instance does this report come from?
> .

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

end of thread, other threads:[~2022-10-18  2:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-17  6:34 [PATCH] copy_process(): fix a memleak in copy_process() Gaosheng Cui
2022-10-17  7:18 ` Christian Brauner
2022-10-18  2:26   ` cuigaosheng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).