# \[PATCH 1/6\] signal: Remove the bogus sigkill\_pending in ptrace\_stop

- Subsystems: THE REST
- Maintainers: Linus Torvalds
- Status: LANDED (7d613f9f72ec · 2021-10-06 16:26)
- Landed in mainline as 7d613f9f72ec on 2021-10-06.
- List: linux-api
- 2 messages, 2 authors
- Page: https://ratatoskr.run/linux-api/2021/09/1520119/t

## \[PATCH 1/6\] signal: Remove the bogus sigkill\_pending in ptrace\_stop

- From: Eric W. Biederman \<hidden\>
- Date: 2021-09-24 00:09:48 
- Message: https://ratatoskr.run/linux-api/2021/09/1520119.md

```text

The existence of sigkill_pending is a little silly as it is
functionally a duplicate of fatal_signal_pending that is used in
exactly one place.

Checking for pending fatal signals and returning early in ptrace_stop
is actively harmful.  It casues the ptrace_stop called by
ptrace_signal to return early before setting current->exit_code.
Later when ptrace_signal reads the signal number from
current->exit_code is undefined, making it unpredictable what will
happen.

Instead rely on the fact that schedule will not sleep if there is a
pending signal that can awaken a task.

Removing the explict sigkill_pending test fixes fixes ptrace_signal
when ptrace_stop does not stop because current->exit_code is always
set to to signr.

Cc: <redacted>
Fixes: 3d749b9e676b ("ptrace: simplify ptrace_stop()->sigkill_pending() path")
Fixes: 1a669c2f16d4 ("Add arch_ptrace_stop")
Signed-off-by: "Eric W. Biederman" <redacted>
---
 kernel/signal.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)

diff --git a/kernel/signal.c b/kernel/signal.c
index 952741f6d0f9..9f2dc9cf3208 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -2182,15 +2182,6 @@ static inline bool may_ptrace_stop(void)
 	return true;
 }
 
-/*
- * Return non-zero if there is a SIGKILL that should be waking us up.
- * Called with the siglock held.
- */
-static bool sigkill_pending(struct task_struct *tsk)
-{
-	return sigismember(&tsk->pending.signal, SIGKILL) ||
-	       sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
-}
 
 /*
  * This must be called with current->sighand->siglock held.
@@ -2217,17 +2208,16 @@ static void ptrace_stop(int exit_code, int why, int clear_code, kernel_siginfo_t
 		 * calling arch_ptrace_stop, so we must release it now.
 		 * To preserve proper semantics, we must do this before
 		 * any signal bookkeeping like checking group_stop_count.
-		 * Meanwhile, a SIGKILL could come in before we retake the
-		 * siglock.  That must prevent us from sleeping in TASK_TRACED.
-		 * So after regaining the lock, we must check for SIGKILL.
 		 */
 		spin_unlock_irq(&current->sighand->siglock);
 		arch_ptrace_stop(exit_code, info);
 		spin_lock_irq(&current->sighand->siglock);
-		if (sigkill_pending(current))
-			return;
 	}
 
+	/*
+	 * schedule() will not sleep if there is a pending signal that
+	 * can awaken the task.
+	 */
 	set_special_state(TASK_TRACED);
 
 	/*
-- 
2.20.1

```

## Re: \[PATCH 1/6\] signal: Remove the bogus sigkill\_pending in ptrace\_stop

- From: Kees Cook \<hidden\>
- Date: 2021-09-24 15:22:25 
- Message: https://ratatoskr.run/linux-api/2021/09/1521097.md

```text
On Thu, Sep 23, 2021 at 07:09:34PM -0500, Eric W. Biederman wrote:
> 
> The existence of sigkill_pending is a little silly as it is
> functionally a duplicate of fatal_signal_pending that is used in
> exactly one place.

sigkill_pending() checks for &tsk->signal->shared_pending.signal but
fatal_signal_pending() doesn't.

> Checking for pending fatal signals and returning early in ptrace_stop
> is actively harmful.  It casues the ptrace_stop called by
> ptrace_signal to return early before setting current->exit_code.
> Later when ptrace_signal reads the signal number from
> current->exit_code is undefined, making it unpredictable what will
> happen.
> 
> Instead rely on the fact that schedule will not sleep if there is a
> pending signal that can awaken a task.

This reasoning sound fine, but I can't see where it's happening.
It looks like recalc_sigpending() is supposed to happen at the start
of scheduling? I see it at the end of ptrace_stop(), though, so it looks
like it's reasonable to skip checking shared_pending.

(Does the scheduler deal with shared_pending directly?)

> Removing the explict sigkill_pending test fixes fixes ptrace_signal
> when ptrace_stop does not stop because current->exit_code is always
> set to to signr.
> 
> Cc: <redacted>
> Fixes: 3d749b9e676b ("ptrace: simplify ptrace_stop()->sigkill_pending() path")
> Fixes: 1a669c2f16d4 ("Add arch_ptrace_stop")
> Signed-off-by: "Eric W. Biederman" <redacted>
> ---
>  kernel/signal.c | 18 ++++--------------
>  1 file changed, 4 insertions(+), 14 deletions(-)
> 
> diff --git a/kernel/signal.c b/kernel/signal.c
> index 952741f6d0f9..9f2dc9cf3208 100644
> --- a/kernel/signal.c
> +++ b/kernel/signal.c
> @@ -2182,15 +2182,6 @@ static inline bool may_ptrace_stop(void)
>  	return true;
>  }
>  
> -/*
> - * Return non-zero if there is a SIGKILL that should be waking us up.
> - * Called with the siglock held.
> - */
> -static bool sigkill_pending(struct task_struct *tsk)
> -{
> -	return sigismember(&tsk->pending.signal, SIGKILL) ||
> -	       sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
> -}
>  
>  /*
>   * This must be called with current->sighand->siglock held.
> @@ -2217,17 +2208,16 @@ static void ptrace_stop(int exit_code, int why, int clear_code, kernel_siginfo_t
>  		 * calling arch_ptrace_stop, so we must release it now.
>  		 * To preserve proper semantics, we must do this before
>  		 * any signal bookkeeping like checking group_stop_count.
> -		 * Meanwhile, a SIGKILL could come in before we retake the
> -		 * siglock.  That must prevent us from sleeping in TASK_TRACED.
> -		 * So after regaining the lock, we must check for SIGKILL.

Where is the sleep this comment is talking about?

i.e. will recalc_sigpending() have been called before the above sleep
would happen? I assume it's after ptrace_stop() returns... But I want to
make sure the sleep isn't in ptrace_stop() itself somewhere I can't see.
I *do* see freezable_schedule() called, and that dumps us into
__schedule(), and I don't see a recalc before it checks
signal_pending_state().

Does a recalc need to happen in plce of the old sigkill_pending()
call?

-- 
Kees Cook
```
