Skip to contentSkip to conversation

fix SIGNAL_UNKILLABLE && SIGKILL interaction

PATCH Applied Oleg Nesterov 3 participants 7 messages Nov 3, 2017 – Nov 13, 2017 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CAP045Ap9Xv67GaeskDt_gAajp1Cni_S0Z0u_vsuw_ptRUuJD6Q@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

cover fix SIGNAL_UNKILLABLE && SIGKILL interaction

Quoted 3 lines · Oleg Nesterov

I need to write the changelog, and perhaps even split this small patch for better documentation.

OK, it is not clear if I answered Eric's concerns or not, let me send the patches for review anyway. I tried to document every change in signal.c.

Oleg.

 kernel/signal.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
#3 lore raw

PATCH 1/3 protect the traced SIGNAL_UNKILLABLE tasks from SIGKILL

The comment in sig_ignored() says "Tracers may want to know about even ignored signals" but SIGKILL can not be reported to debugger and it is just wrong to return 0 in this case: SIGKILL should only kill the SIGNAL_UNKILLABLE task if it comes from the parent ns.

Change sig_ignored() to ignore ->ptrace if sig == SIGKILL and rely on sig_task_ignored().

SISGTOP coming from within the namespace is not really right too but at least debugger can intercept it, and we can't drop it here because this will break "gdb -p 1": ptrace_attach() won't work. Perhaps we will add another ->ptrace check later, we will see.

---
 kernel/signal.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
kernel/signal.c +7 -5
index 800a18f..b9aebe1 100644
@@ -94,13 +94,15 @@ static int sig_ignored(struct task_struct *t, int sig, bool force)
 	if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
 		return 0;
 
-	if (!sig_task_ignored(t, sig, force))
-		return 0;
-
 	/*
-	 * Tracers may want to know about even ignored signals.
+	 * Tracers may want to know about even ignored signal unless it
+	 * is SIGKILL which can't be reported anyway but can be ignored
+	 * by SIGNAL_UNKILLABLE task.
 	 */
-	return !t->ptrace;
+	if (t->ptrace && sig != SIGKILL)
+		return 0;
+
+	return sig_task_ignored(t, sig, force);
 }
 
 /*
Signature · 1 line
2.5.0
#4 lore raw

PATCH 2/3 protect the SIGNAL_UNKILLABLE tasks from !sig_kernel_only() signals

In reply to Oleg Nesterov

Change sig_task_ignored() to drop the SIG_DFL && !sig_kernel_only() signals even if force == T. This simplifies the next change and this matches the same check in get_signal() which will drop these signals anyway.

---
 kernel/signal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/signal.c +1 -1
index b9aebe1..8fc0182 100644
@@ -78,7 +78,7 @@ static int sig_task_ignored(struct task_struct *t, int sig, bool force)
 	handler = sig_handler(t, sig);
 
 	if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
-			handler == SIG_DFL && !force)
+	    handler == SIG_DFL && !(force && sig_kernel_only(sig)))
 		return 1;
 
 	return sig_handler_ignored(handler, sig);
Signature · 1 line
2.5.0
#5 lore raw

PATCH 3/3 remove the no longer needed SIGNAL_UNKILLABLE check in complete_signal()

In reply to Oleg Nesterov

complete_signal() checks SIGNAL_UNKILLABLE before it starts to destroy the thread group, today this is wrong in many ways.

If nothing else, fatal_signal_pending() should always imply that the whole thread group (except ->group_exit_task if it is not NULL) is killed, this check breaks the rule.

After the previous changes we can rely on sig_task_ignored(); sig_fatal(sig) && SIGNAL_UNKILLABLE can only be true if we actually want to kill this task and sig == SIGKILL OR it is traced and debugger can intercept the signal.

This should hopefully fix the problem reported by Dmitry. This test-case

	static int init(void *arg)
	{
		for (;;)
			pause();
	}

	int main(void)
	{
		char stack[16 * 1024];

		for (;;) {
			int pid = clone(init, stack + sizeof(stack)/2,
					CLONE_NEWPID | SIGCHLD, NULL);
			assert(pid > 0);

			assert(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
			assert(waitpid(-1, NULL, WSTOPPED) == pid);

			assert(ptrace(PTRACE_DETACH, pid, 0, SIGSTOP) == 0);
			assert(syscall(__NR_tkill, pid, SIGKILL) == 0);
			assert(pid == wait(NULL));
		}
	}

triggers the WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING)) in task_participate_group_stop(). do_signal_stop()->signal_group_exit() checks SIGNAL_GROUP_EXIT and return false, but task_set_jobctl_pending() checks fatal_signal_pending() and does not set JOBCTL_STOP_PENDING.

And his should fix the minor security problem reported by Kyle, SECCOMP_RET_TRACE can miss fatal_signal_pending() the same way if the task is the root of a pid namespace.

---
 kernel/signal.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/signal.c +2 -2
index 8fc0182..7e15b56 100644
@@ -931,9 +931,9 @@ static void complete_signal(int sig, struct task_struct *p, int group)
 	 * then start taking the whole group down immediately.
 	 */
 	if (sig_fatal(p, sig) &&
-	    !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
+	    !(signal->flags & SIGNAL_GROUP_EXIT) &&
 	    !sigismember(&t->real_blocked, sig) &&
-	    (sig == SIGKILL || !t->ptrace)) {
+	    (sig == SIGKILL || !p->ptrace)) {
 		/*
 		 * This signal will be fatal to the whole group.
 		 */
Signature · 1 line
2.5.0
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] remove the no longer needed SIGNAL_UNKILLABLE check in complete_signal()

Quoted 42 lines · Oleg Nesterov

complete_signal() checks SIGNAL_UNKILLABLE before it starts to destroy the thread group, today this is wrong in many ways.

If nothing else, fatal_signal_pending() should always imply that the whole thread group (except ->group_exit_task if it is not NULL) is killed, this check breaks the rule.

After the previous changes we can rely on sig_task_ignored(); sig_fatal(sig) && SIGNAL_UNKILLABLE can only be true if we actually want to kill this task and sig == SIGKILL OR it is traced and debugger can intercept the signal.

This should hopefully fix the problem reported by Dmitry. This test-case

        static int init(void *arg)
        {
                for (;;)
                        pause();
        }

        int main(void)
        {
                char stack[16 * 1024];

                for (;;) {
                        int pid = clone(init, stack + sizeof(stack)/2,
                                        CLONE_NEWPID | SIGCHLD, NULL);
                        assert(pid > 0);

                        assert(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
                        assert(waitpid(-1, NULL, WSTOPPED) == pid);

                        assert(ptrace(PTRACE_DETACH, pid, 0, SIGSTOP) == 0);
                        assert(syscall(__NR_tkill, pid, SIGKILL) == 0);
                        assert(pid == wait(NULL));
                }
        }

triggers the WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING)) in task_participate_group_stop(). do_signal_stop()->signal_group_exit() checks SIGNAL_GROUP_EXIT and return false, but task_set_jobctl_pending() checks fatal_signal_pending() and does not set JOBCTL_STOP_PENDING.

Quoted 7 lines

And his should fix the minor security problem reported by Kyle, SECCOMP_RET_TRACE can miss fatal_signal_pending() the same way if the task is the root of a pid namespace.

Thanks for digging through this! Two birds, one stone, etc. :)

-Kees

Quoted 24 lines
---
 kernel/signal.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/signal.c +2 -3
index 8fc0182..7e15b56 100644
@@ -931,9 +931,9 @@ static void complete_signal(int sig, struct task_struct *p, int group)
         * then start taking the whole group down immediately.
         */
        if (sig_fatal(p, sig) &&
-           !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
+           !(signal->flags & SIGNAL_GROUP_EXIT) &&
            !sigismember(&t->real_blocked, sig) &&
-           (sig == SIGKILL || !t->ptrace)) {
+           (sig == SIGKILL || !p->ptrace)) {
                /*
                 * This signal will be fatal to the whole group.
                 */
--

2.5.0

Signature · 2 lines
Kees Cook
Pixel Security
#7 lore raw
In reply to Oleg Nesterov

ping...

Dmitry confirms this actually fixes the problem reported by syzkaller we discussed in another thread.

Quoted 13 lines · Oleg Nesterov
Quoted 3 lines · Oleg Nesterov

I need to write the changelog, and perhaps even split this small patch for better documentation.

OK, it is not clear if I answered Eric's concerns or not, let me send the patches for review anyway. I tried to document every change in signal.c.

Oleg.

 kernel/signal.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
#8 lore raw
Quoted 21 lines · Oleg Nesterov

ping...

Dmitry confirms this actually fixes the problem reported by syzkaller we discussed in another thread.

Quoted 13 lines · Oleg Nesterov
Quoted 3 lines · Oleg Nesterov

I need to write the changelog, and perhaps even split this small patch for better documentation.

OK, it is not clear if I answered Eric's concerns or not, let me send the patches for review anyway. I tried to document every change in signal.c.

Oleg.

 kernel/signal.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

I can confirm that your patches fix the issue I reported as well.

- Kyle