pids: sys_getsid: fix unsafe *pid usage, fix possible 0 instead of -ESRCH

1. sys_getsid() needs rcu_read_lock() to derive the session _nr, even if
   the task is current, otherwise we can race with another thread which
   does sys_setsid().

2. The task can exit between find_task_by_vpid() and task_session_vnr(),
   in that unlikely case sys_getsid() returns 0 instead of -ESRCH.

Signed-off-by: Oleg Nesterov <oleg@tv-sign.ru>
Cc: Roland McGrath <roland@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Oleg Nesterov 2008-04-30 00:54:28 -07:00 committed by Linus Torvalds
parent 7d8da0962e
commit 1dd768c081

View File

@ -1022,23 +1022,30 @@ asmlinkage long sys_getpgrp(void)
asmlinkage long sys_getsid(pid_t pid) asmlinkage long sys_getsid(pid_t pid)
{ {
if (!pid) struct task_struct *p;
return task_session_vnr(current); struct pid *sid;
else { int retval;
int retval;
struct task_struct *p;
rcu_read_lock(); rcu_read_lock();
p = find_task_by_vpid(pid); if (!pid)
sid = task_session(current);
else {
retval = -ESRCH; retval = -ESRCH;
if (p) { p = find_task_by_vpid(pid);
retval = security_task_getsid(p); if (!p)
if (!retval) goto out;
retval = task_session_vnr(p); sid = task_session(p);
} if (!sid)
rcu_read_unlock(); goto out;
return retval;
retval = security_task_getsid(p);
if (retval)
goto out;
} }
retval = pid_vnr(sid);
out:
rcu_read_unlock();
return retval;
} }
asmlinkage long sys_setsid(void) asmlinkage long sys_setsid(void)