Skip to content

Commit 24336ea

Browse files
Oleg Nesterovtorvalds
authored andcommitted
pids: introduce change_pid() helper
Based on Eric W. Biederman's idea. Without tasklist_lock held task_session()/task_pgrp() can return NULL if the caller races with setprgp()/setsid() which does detach_pid() + attach_pid(). This can happen even if task == current. Intoduce the new helper, change_pid(), which should be used instead. This way the caller always sees the special pid != NULL, either old or new. Also change the prototype of attach_pid(), it always returns 0 and nobody check the returned value. Signed-off-by: Oleg Nesterov <oleg@tv-sign.ru> Cc: "Eric W. Biederman" <ebiederm@xmission.com> 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 65450ce commit 24336ea

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

‎include/linux/pid.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ extern struct pid *get_task_pid(struct task_struct *task, enum pid_type type);
8989
* attach_pid() and detach_pid() must be called with the tasklist_lock
9090
* write-held.
9191
*/
92-
extern int attach_pid(struct task_struct *task, enum pid_type type,
93-
struct pid *pid);
92+
extern void attach_pid(struct task_struct *task, enum pid_type type,
93+
struct pid *pid);
9494
extern void detach_pid(struct task_struct *task, enum pid_type);
95+
extern void change_pid(struct task_struct *task, enum pid_type,
96+
struct pid *pid);
9597
extern void transfer_pid(struct task_struct *old, struct task_struct *new,
9698
enum pid_type);
9799

‎kernel/pid.c‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,19 +317,18 @@ EXPORT_SYMBOL_GPL(find_pid);
317317
/*
318318
* attach_pid() must be called with the tasklist_lock write-held.
319319
*/
320-
int attach_pid(struct task_struct *task, enum pid_type type,
320+
void attach_pid(struct task_struct *task, enum pid_type type,
321321
struct pid *pid)
322322
{
323323
struct pid_link *link;
324324

325325
link = &task->pids[type];
326326
link->pid = pid;
327327
hlist_add_head_rcu(&link->node, &pid->tasks[type]);
328-
329-
return 0;
330328
}
331329

332-
void detach_pid(struct task_struct *task, enum pid_type type)
330+
static void __change_pid(struct task_struct *task, enum pid_type type,
331+
struct pid *new)
333332
{
334333
struct pid_link *link;
335334
struct pid *pid;
@@ -339,7 +338,7 @@ void detach_pid(struct task_struct *task, enum pid_type type)
339338
pid = link->pid;
340339

341340
hlist_del_rcu(&link->node);
342-
link->pid = NULL;
341+
link->pid = new;
343342

344343
for (tmp = PIDTYPE_MAX; --tmp >= 0; )
345344
if (!hlist_empty(&pid->tasks[tmp]))
@@ -348,6 +347,18 @@ void detach_pid(struct task_struct *task, enum pid_type type)
348347
free_pid(pid);
349348
}
350349

350+
void detach_pid(struct task_struct *task, enum pid_type type)
351+
{
352+
__change_pid(task, type, NULL);
353+
}
354+
355+
void change_pid(struct task_struct *task, enum pid_type type,
356+
struct pid *pid)
357+
{
358+
__change_pid(task, type, pid);
359+
attach_pid(task, type, pid);
360+
}
361+
351362
/* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */
352363
void transfer_pid(struct task_struct *old, struct task_struct *new,
353364
enum pid_type type)

0 commit comments

Comments
 (0)