lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <YsLbggVXONPJcZsn@zx2c4.com>
Date:   Mon, 4 Jul 2022 14:22:26 +0200
From:   "Jason A. Donenfeld" <Jason@...c4.com>
To:     "Eric W. Biederman" <ebiederm@...ssion.com>,
        linux-kernel@...r.kernel.org
Cc:     Ingo Molnar <mingo@...hat.com>,
        Peter Zijlstra <peterz@...radead.org>,
        Toke Høiland-Jørgensen <toke@...hat.com>,
        Kalle Valo <kvalo@...nel.org>,
        Johannes Berg <johannes@...solutions.net>
Subject: Re: [PATCH v3] signal: break out of wait loops on kthread_stop()

Hey Eric,

On Tue, Jun 28, 2022 at 06:14:41PM +0200, Jason A. Donenfeld wrote:
> I was recently surprised to learn that msleep_interruptible(),
> wait_for_completion_interruptible_timeout(), and related functions
> simply hung when I called kthread_stop() on kthreads using them. The
> solution to fixing the case with msleep_interruptible() was more simply
> to move to schedule_timeout_interruptible(). Why?
> 
> The reason is that msleep_interruptible(), and many functions just like
> it, has a loop like this:
> 
>         while (timeout && !signal_pending(current))
>                 timeout = schedule_timeout_interruptible(timeout);
> 
> The call to kthread_stop() woke up the thread, so schedule_timeout_
> interruptible() returned early, but because signal_pending() returned
> true, it went back into another timeout, which was never woken up.
> 
> This wait loop pattern is common to various pieces of code, and I
> suspect that the subtle misuse in a kthread that caused a deadlock in
> the code I looked at last week is also found elsewhere.
> 
> So this commit causes signal_pending() to return true when
> kthread_stop() is called, by setting TIF_NOTIFY_SIGNAL.
> 
> The same also applies to the similar kthread_park() functionality.
> 
> Cc: Ingo Molnar <mingo@...hat.com>
> Cc: Peter Zijlstra <peterz@...radead.org>
> Cc: Eric W. Biederman <ebiederm@...ssion.com>
> Cc: Toke Høiland-Jørgensen <toke@...hat.com>
> Cc: Kalle Valo <kvalo@...nel.org>
> Cc: Johannes Berg <johannes@...solutions.net>
> Signed-off-by: Jason A. Donenfeld <Jason@...c4.com>
> ---
>  kernel/kthread.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/kernel/kthread.c b/kernel/kthread.c
> index 3c677918d8f2..63d5a1f4cb93 100644
> --- a/kernel/kthread.c
> +++ b/kernel/kthread.c
> @@ -661,12 +661,14 @@ int kthread_park(struct task_struct *k)
>  
>  	set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
>  	if (k != current) {
> +		test_and_set_tsk_thread_flag(k, TIF_NOTIFY_SIGNAL);
>  		wake_up_process(k);
>  		/*
>  		 * Wait for __kthread_parkme() to complete(), this means we
>  		 * _will_ have TASK_PARKED and are about to call schedule().
>  		 */
>  		wait_for_completion(&kthread->parked);
> +		clear_tsk_thread_flag(k, TIF_NOTIFY_SIGNAL);
>  		/*
>  		 * Now wait for that schedule() to complete and the task to
>  		 * get scheduled out.
> @@ -704,8 +706,10 @@ int kthread_stop(struct task_struct *k)
>  	kthread = to_kthread(k);
>  	set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
>  	kthread_unpark(k);
> +	test_and_set_tsk_thread_flag(k, TIF_NOTIFY_SIGNAL);
>  	wake_up_process(k);
>  	wait_for_completion(&kthread->exited);
> +	clear_tsk_thread_flag(k, TIF_NOTIFY_SIGNAL);
>  	ret = kthread->result;
>  	put_task_struct(k);
>  
> -- 
> 2.35.1
> 

Is this more to the tune of what you had in mind in your message [1]?

Jason

[1] https://lore.kernel.org/lkml/877d51udc7.fsf@email.froward.int.ebiederm.org/

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ