All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC v1] pidfd: fix a race in setting exit_state for pidfd polling
@ 2019-07-17 17:21 Joel Fernandes (Google)
  2019-07-17 17:55 ` Christian Brauner
                   ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: Joel Fernandes (Google) @ 2019-07-17 17:21 UTC (permalink / raw)
  To: linux-kernel
  Cc: Suren Baghdasaryan, kernel-team, Joel Fernandes, Andrea Arcangeli,
	Andrew Morton, Christian Brauner, Eric W. Biederman,
	Oleg Nesterov, Tejun Heo

From: Suren Baghdasaryan <surenb@google.com>

There is a race between reading task->exit_state in pidfd_poll and writing
it after do_notify_parent calls do_notify_pidfd. Expected sequence of
events is:

CPU 0                            CPU 1
------------------------------------------------
exit_notify
  do_notify_parent
    do_notify_pidfd
  tsk->exit_state = EXIT_DEAD
                                  pidfd_poll
                                     if (tsk->exit_state)

However nothing prevents the following sequence:

CPU 0                            CPU 1
------------------------------------------------
exit_notify
  do_notify_parent
    do_notify_pidfd
                                   pidfd_poll
                                      if (tsk->exit_state)
  tsk->exit_state = EXIT_DEAD

This causes a polling task to wait forever, since poll blocks because
exit_state is 0 and the waiting task is not notified again. A stress
test continuously doing pidfd poll and process exits uncovered this bug,
and the below patch fixes it.

To fix this, we set tsk->exit_state before calling do_notify_pidfd.

Cc: kernel-team@android.com
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>

---
 kernel/exit.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/kernel/exit.c b/kernel/exit.c
index a75b6a7f458a..740ceacb4b76 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -720,6 +720,7 @@ static void exit_notify(struct task_struct *tsk, int group_dead)
 	if (group_dead)
 		kill_orphaned_pgrp(tsk->group_leader, NULL);
 
+	tsk->exit_state = EXIT_ZOMBIE;
 	if (unlikely(tsk->ptrace)) {
 		int sig = thread_group_leader(tsk) &&
 				thread_group_empty(tsk) &&
@@ -1156,10 +1157,11 @@ static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
 		ptrace_unlink(p);
 
 		/* If parent wants a zombie, don't release it now */
-		state = EXIT_ZOMBIE;
+		p->exit_state = EXIT_ZOMBIE;
 		if (do_notify_parent(p, p->exit_signal))
-			state = EXIT_DEAD;
-		p->exit_state = state;
+			p->exit_state = EXIT_DEAD;
+
+		state = p->exit_state;
 		write_unlock_irq(&tasklist_lock);
 	}
 	if (state == EXIT_DEAD)
-- 
2.22.0.657.g960e92d24f-goog


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

end of thread, other threads:[~2019-07-19 16:53 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-07-17 17:21 [PATCH RFC v1] pidfd: fix a race in setting exit_state for pidfd polling Joel Fernandes (Google)
2019-07-17 17:55 ` Christian Brauner
2019-07-17 18:09   ` Suren Baghdasaryan
2019-07-17 20:47     ` Joel Fernandes
2019-07-18 10:09       ` Christian Brauner
2019-07-17 20:51   ` Joel Fernandes
2019-07-18 10:17 ` Christian Brauner
2019-07-18 16:05   ` Suren Baghdasaryan
2019-07-18 15:00 ` Oleg Nesterov
2019-07-19 16:14 ` Oleg Nesterov
2019-07-19 16:27   ` Christian Brauner
2019-07-19 16:33     ` Suren Baghdasaryan
2019-07-19 16:51     ` Joel Fernandes
2019-07-19 16:53       ` Christian Brauner

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.