[PATCH] sys_getppid oopses on debug kernel
sys_getppid() optimization can access a freed memory. On kernels with DEBUG_SLAB turned ON, this results in Oops. As Dave Hansen noted, this optimization is also unsafe for memory hotplug. So this patch always takes the lock to be safe. [oleg@tv-sign.ru: simplifications] Signed-off-by: Kirill Korotaev <dev@openvz.org> Cc: <stable@kernel.org> Cc: Dave Hansen <haveblue@us.ibm.com> Signed-off-by: Oleg Nesterov <oleg@tv-sign.ru> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
This commit is contained in:
Родитель
012c437d03
Коммит
6997a6faaa
|
@ -1324,46 +1324,19 @@ asmlinkage long sys_getpid(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Accessing ->group_leader->real_parent is not SMP-safe, it could
|
* Accessing ->real_parent is not SMP-safe, it could
|
||||||
* change from under us. However, rather than getting any lock
|
* change from under us. However, we can use a stale
|
||||||
* we can use an optimistic algorithm: get the parent
|
* value of ->real_parent under rcu_read_lock(), see
|
||||||
* pid, and go back and check that the parent is still
|
* release_task()->call_rcu(delayed_put_task_struct).
|
||||||
* the same. If it has changed (which is extremely unlikely
|
|
||||||
* indeed), we just try again..
|
|
||||||
*
|
|
||||||
* NOTE! This depends on the fact that even if we _do_
|
|
||||||
* get an old value of "parent", we can happily dereference
|
|
||||||
* the pointer (it was and remains a dereferencable kernel pointer
|
|
||||||
* no matter what): we just can't necessarily trust the result
|
|
||||||
* until we know that the parent pointer is valid.
|
|
||||||
*
|
|
||||||
* NOTE2: ->group_leader never changes from under us.
|
|
||||||
*/
|
*/
|
||||||
asmlinkage long sys_getppid(void)
|
asmlinkage long sys_getppid(void)
|
||||||
{
|
{
|
||||||
int pid;
|
int pid;
|
||||||
struct task_struct *me = current;
|
|
||||||
struct task_struct *parent;
|
|
||||||
|
|
||||||
parent = me->group_leader->real_parent;
|
rcu_read_lock();
|
||||||
for (;;) {
|
pid = rcu_dereference(current->real_parent)->tgid;
|
||||||
pid = parent->tgid;
|
rcu_read_unlock();
|
||||||
#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
|
|
||||||
{
|
|
||||||
struct task_struct *old = parent;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Make sure we read the pid before re-reading the
|
|
||||||
* parent pointer:
|
|
||||||
*/
|
|
||||||
smp_rmb();
|
|
||||||
parent = me->group_leader->real_parent;
|
|
||||||
if (old != parent)
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return pid;
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче