[PATCH] Make setsid() more robust
authorEric W. Biederman <ebiederm@xmission.com>
Fri, 31 Mar 2006 10:31:33 +0000 (02:31 -0800)
committerLinus Torvalds <torvalds@g5.osdl.org>
Fri, 31 Mar 2006 20:18:59 +0000 (12:18 -0800)
The core problem: setsid fails if it is called by init.  The effect in 2.6.16
and the earlier kernels that have this problem is that if you do a "ps -j 1 or
ps -ej 1" you will see that init and several of it's children have process
group and session == 0.  Instead of process group == session == 1.  Despite
init calling setsid.

The reason it fails is that daemonize calls set_special_pids(1,1) on kernel
threads that are launched before /sbin/init is called.

The only remaining effect in that current->signal->leader == 0 for init
instead of 1.  And the setsid call fails.  No one has noticed because
/sbin/init does not check the return value of setsid.

In 2.4 where we don't have the pidhash table, and daemonize doesn't exist
setsid actually works for init.

I care a lot about pid == 1 not being a special case that we leave broken,
because of the container/jail work that I am doing.

- Carefully allow init (pid == 1) to call setsid despite the kernel using
  its session.

- Use find_task_by_pid instead of find_pid because find_pid taking a
  pidtype is going away.

Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
kernel/sys.c

index 7ef7f60..0b6ec0e 100644 (file)
@@ -1372,18 +1372,29 @@ asmlinkage long sys_getsid(pid_t pid)
 asmlinkage long sys_setsid(void)
 {
        struct task_struct *group_leader = current->group_leader;
-       struct pid *pid;
+       pid_t session;
        int err = -EPERM;
 
        mutex_lock(&tty_mutex);
        write_lock_irq(&tasklist_lock);
 
-       pid = find_pid(PIDTYPE_PGID, group_leader->pid);
-       if (pid)
+       /* Fail if I am already a session leader */
+       if (group_leader->signal->leader)
+               goto out;
+
+       session = group_leader->pid;
+       /* Fail if a process group id already exists that equals the
+        * proposed session id.
+        *
+        * Don't check if session id == 1 because kernel threads use this
+        * session id and so the check will always fail and make it so
+        * init cannot successfully call setsid.
+        */
+       if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
                goto out;
 
        group_leader->signal->leader = 1;
-       __set_special_pids(group_leader->pid, group_leader->pid);
+       __set_special_pids(session, session);
        group_leader->signal->tty = NULL;
        group_leader->signal->tty_old_pgrp = 0;
        err = process_group(group_leader);