Message ID | 20190814113822.9505-2-christian.brauner@ubuntu.com |
---|---|
State | New |
Headers | show |
Series | waitid: process group enhancement | expand |
On 08/14, christian.brauner@ubuntu.com wrote: > > case P_PGID: > type = PIDTYPE_PGID; > - if (upid <= 0) > + if (upid < 0) > return -EINVAL; > + > + if (upid == 0) > + pid = get_pid(task_pgrp(current)); this needs rcu lock or tasklist_lock, this can race with another thread doing sys_setpgid/setsid (see change_pid(PIDTYPE_PGID)). Oleg.
On Wed, Aug 14, 2019 at 02:29:10PM +0200, Oleg Nesterov wrote: > On 08/14, christian.brauner@ubuntu.com wrote: > > > > case P_PGID: > > type = PIDTYPE_PGID; > > - if (upid <= 0) > > + if (upid < 0) > > return -EINVAL; > > + > > + if (upid == 0) > > + pid = get_pid(task_pgrp(current)); > > this needs rcu lock or tasklist_lock, this can race with another thread > doing sys_setpgid/setsid (see change_pid(PIDTYPE_PGID)). Oh, I naively assumed task_pgrp() would take an rcu lock... kernel/sys.c takes both, i.e. rcu_read_lock(); write_lock_irq(&tasklist_lock); though I think we should be fine with just rcu_read_lock(). setpgid() indicates that it wants to check real_parent and needs the write_lock_irq() because it might change behind its back which we don't care about since we're not changing the pgrp. Christian
On 08/14, Christian Brauner wrote: > > On Wed, Aug 14, 2019 at 02:29:10PM +0200, Oleg Nesterov wrote: > > On 08/14, christian.brauner@ubuntu.com wrote: > > > > > > case P_PGID: > > > type = PIDTYPE_PGID; > > > - if (upid <= 0) > > > + if (upid < 0) > > > return -EINVAL; > > > + > > > + if (upid == 0) > > > + pid = get_pid(task_pgrp(current)); > > > > this needs rcu lock or tasklist_lock, this can race with another thread > > doing sys_setpgid/setsid (see change_pid(PIDTYPE_PGID)). > > Oh, I naively assumed task_pgrp() would take an rcu lock... but it would not help ;) > though I think we should be fine with just rcu_read_lock(). Yes, Oleg.
On Wed, Aug 14, 2019 at 02:50:12PM +0200, Oleg Nesterov wrote: > On 08/14, Christian Brauner wrote: > > > > On Wed, Aug 14, 2019 at 02:29:10PM +0200, Oleg Nesterov wrote: > > > On 08/14, christian.brauner@ubuntu.com wrote: > > > > > > > > case P_PGID: > > > > type = PIDTYPE_PGID; > > > > - if (upid <= 0) > > > > + if (upid < 0) > > > > return -EINVAL; > > > > + > > > > + if (upid == 0) > > > > + pid = get_pid(task_pgrp(current)); > > > > > > this needs rcu lock or tasklist_lock, this can race with another thread > > > doing sys_setpgid/setsid (see change_pid(PIDTYPE_PGID)). > > > > Oh, I naively assumed task_pgrp() would take an rcu lock... > > but it would not help ;) Yeah, it doesn't do a get. :)
diff --git a/kernel/exit.c b/kernel/exit.c index 5b4a5dcce8f8..e70083b14f31 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -1576,19 +1576,23 @@ static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop, type = PIDTYPE_PID; if (upid <= 0) return -EINVAL; + + pid = find_get_pid(upid); break; case P_PGID: type = PIDTYPE_PGID; - if (upid <= 0) + if (upid < 0) return -EINVAL; + + if (upid == 0) + pid = get_pid(task_pgrp(current)); + else + pid = find_get_pid(upid); break; default: return -EINVAL; } - if (type < PIDTYPE_MAX) - pid = find_get_pid(upid); - wo.wo_type = type; wo.wo_pid = pid; wo.wo_flags = options;