linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] kernel/fork.c: annotate data races for copy_process
@ 2020-06-23  4:12 Weilong Chen
  2020-06-23 10:01 ` Christian Brauner
  0 siblings, 1 reply; 2+ messages in thread
From: Weilong Chen @ 2020-06-23  4:12 UTC (permalink / raw)
  To: christian.brauner, akpm, tglx, lizefan; +Cc: linux-kernel

KCSAN report there's a data race risk while using nr_threads.
But according to the comment above it:
'
        /*
         * If multiple threads are within copy_process(), then this check
         * triggers too late. This doesn't hurt, the check is only there
         * to stop root fork bombs.
         */
'
The concurrency problem is not care. And we needn't to use READ_ONCE/atomic/etc
to protect it. Meanwhile 'max_threads' is a sysctl variable which can
be modified concurrently while being read, we can use
'data_race(nr_threads >= max_threads)' to mark both of then.

BUG: KCSAN: data-race in copy_process / copy_process

write to 0xffffffff86205cf8 of 4 bytes by task 14779 on cpu 1:
  copy_process+0x2eba/0x3c40 kernel/fork.c:2273
  _do_fork+0xfe/0x7a0 kernel/fork.c:2421
  __do_sys_clone kernel/fork.c:2576 [inline]
  __se_sys_clone kernel/fork.c:2557 [inline]
  __x64_sys_clone+0x130/0x170 kernel/fork.c:2557
  do_syscall_64+0xcc/0x3a0 arch/x86/entry/common.c:294
  entry_SYSCALL_64_after_hwframe+0x44/0xa9

read to 0xffffffff86205cf8 of 4 bytes by task 6944 on cpu 0:
  copy_process+0x94d/0x3c40 kernel/fork.c:1954
  _do_fork+0xfe/0x7a0 kernel/fork.c:2421
  __do_sys_clone kernel/fork.c:2576 [inline]
  __se_sys_clone kernel/fork.c:2557 [inline]
  __x64_sys_clone+0x130/0x170 kernel/fork.c:2557
  do_syscall_64+0xcc/0x3a0 arch/x86/entry/common.c:294
  entry_SYSCALL_64_after_hwframe+0x44/0xa9
Link: https://groups.google.com/forum/#!msg/syzkaller-upstream-mo
deration/thvp7AHs5Ew/aPdYLXfYBQAJ

Reported-by: syzbot+52fced2d288f8ecd2b20@syzkaller.appspotmail.com
Cc: Qian Cai <cai@lca.pw>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Christian Brauner <christian.brauner@ubuntu.com>
Cc: Marco Elver <elver@google.com>
Signed-off-by: Zefan Li <lizefan@huawei.com>
Signed-off-by: Weilong Chen <chenweilong@huawei.com>
---
 kernel/fork.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/fork.c b/kernel/fork.c
index 63c8fb2f5ca7..caa9c1f27444 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1976,7 +1976,7 @@ static __latent_entropy struct task_struct *copy_process(
 	 * to stop root fork bombs.
 	 */
 	retval = -EAGAIN;
-	if (nr_threads >= max_threads)
+	if (data_race(nr_threads >= max_threads))
 		goto bad_fork_cleanup_count;
 
 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
-- 
2.17.1


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

* Re: [PATCH v2] kernel/fork.c: annotate data races for copy_process
  2020-06-23  4:12 [PATCH v2] kernel/fork.c: annotate data races for copy_process Weilong Chen
@ 2020-06-23 10:01 ` Christian Brauner
  0 siblings, 0 replies; 2+ messages in thread
From: Christian Brauner @ 2020-06-23 10:01 UTC (permalink / raw)
  To: Weilong Chen; +Cc: akpm, tglx, lizefan, linux-kernel

On Tue, Jun 23, 2020 at 12:12:40PM +0800, Weilong Chen wrote:
> KCSAN report there's a data race risk while using nr_threads.
> But according to the comment above it:
> '
>         /*
>          * If multiple threads are within copy_process(), then this check
>          * triggers too late. This doesn't hurt, the check is only there
>          * to stop root fork bombs.
>          */
> '
> The concurrency problem is not care. And we needn't to use READ_ONCE/atomic/etc
> to protect it. Meanwhile 'max_threads' is a sysctl variable which can
> be modified concurrently while being read, we can use
> 'data_race(nr_threads >= max_threads)' to mark both of then.
> 
> BUG: KCSAN: data-race in copy_process / copy_process
> 
> write to 0xffffffff86205cf8 of 4 bytes by task 14779 on cpu 1:
>   copy_process+0x2eba/0x3c40 kernel/fork.c:2273
>   _do_fork+0xfe/0x7a0 kernel/fork.c:2421
>   __do_sys_clone kernel/fork.c:2576 [inline]
>   __se_sys_clone kernel/fork.c:2557 [inline]
>   __x64_sys_clone+0x130/0x170 kernel/fork.c:2557
>   do_syscall_64+0xcc/0x3a0 arch/x86/entry/common.c:294
>   entry_SYSCALL_64_after_hwframe+0x44/0xa9
> 
> read to 0xffffffff86205cf8 of 4 bytes by task 6944 on cpu 0:
>   copy_process+0x94d/0x3c40 kernel/fork.c:1954
>   _do_fork+0xfe/0x7a0 kernel/fork.c:2421
>   __do_sys_clone kernel/fork.c:2576 [inline]
>   __se_sys_clone kernel/fork.c:2557 [inline]
>   __x64_sys_clone+0x130/0x170 kernel/fork.c:2557
>   do_syscall_64+0xcc/0x3a0 arch/x86/entry/common.c:294
>   entry_SYSCALL_64_after_hwframe+0x44/0xa9
> Link: https://groups.google.com/forum/#!msg/syzkaller-upstream-mo
> deration/thvp7AHs5Ew/aPdYLXfYBQAJ
> 
> Reported-by: syzbot+52fced2d288f8ecd2b20@syzkaller.appspotmail.com
> Cc: Qian Cai <cai@lca.pw>
> Cc: Oleg Nesterov <oleg@redhat.com>
> Cc: Christian Brauner <christian.brauner@ubuntu.com>
> Cc: Marco Elver <elver@google.com>
> Signed-off-by: Zefan Li <lizefan@huawei.com>
> Signed-off-by: Weilong Chen <chenweilong@huawei.com>
> ---

I'm going to fix up the commit message a little bit but otherwise I'm
going to take this unless I hear objections from someone.

Acked-by: Christian Brauner <christian.brauner@ubuntu.com>

Thanks!
Christian

>  kernel/fork.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/kernel/fork.c b/kernel/fork.c
> index 63c8fb2f5ca7..caa9c1f27444 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -1976,7 +1976,7 @@ static __latent_entropy struct task_struct *copy_process(
>  	 * to stop root fork bombs.
>  	 */
>  	retval = -EAGAIN;
> -	if (nr_threads >= max_threads)
> +	if (data_race(nr_threads >= max_threads))
>  		goto bad_fork_cleanup_count;
>  
>  	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
> -- 
> 2.17.1
> 

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

end of thread, other threads:[~2020-06-23 10:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-23  4:12 [PATCH v2] kernel/fork.c: annotate data races for copy_process Weilong Chen
2020-06-23 10:01 ` Christian Brauner

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).