Skip to content

Commit d36174b

Browse files
Oleg NesterovLinus Torvalds
authored andcommitted
uglify kill_pid_info() to fix kill() vs exec() race
kill_pid_info()->pid_task() could be the old leader of the execing process. In that case it is possible that the leader will be released before we take siglock. This means that kill_pid_info() (and thus sys_kill()) can return a false -ESRCH. Change the code to retry when lock_task_sighand() fails. The endless loop is not possible, __exit_signal() both clears ->sighand and does detach_pid(). Signed-off-by: Oleg Nesterov <oleg@tv-sign.ru> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: Davide Libenzi <davidel@xmailserver.org> Cc: Pavel Emelyanov <xemul@openvz.org> Cc: Roland McGrath <roland@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f374ada commit d36174b

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

‎kernel/signal.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,17 +1050,26 @@ int kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
10501050

10511051
int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
10521052
{
1053-
int error;
1053+
int error = -ESRCH;
10541054
struct task_struct *p;
10551055

10561056
rcu_read_lock();
10571057
if (unlikely(sig_needs_tasklist(sig)))
10581058
read_lock(&tasklist_lock);
10591059

1060+
retry:
10601061
p = pid_task(pid, PIDTYPE_PID);
1061-
error = -ESRCH;
1062-
if (p)
1062+
if (p) {
10631063
error = group_send_sig_info(sig, info, p);
1064+
if (unlikely(error == -ESRCH))
1065+
/*
1066+
* The task was unhashed in between, try again.
1067+
* If it is dead, pid_task() will return NULL,
1068+
* if we race with de_thread() it will find the
1069+
* new leader.
1070+
*/
1071+
goto retry;
1072+
}
10641073

10651074
if (unlikely(sig_needs_tasklist(sig)))
10661075
read_unlock(&tasklist_lock);

0 commit comments

Comments
 (0)