Skip to content

Commit b4cf490

Browse files
oleg-nesterovgregkh
authored andcommitted
pids: make task_tgid_nr_ns() safe
commit dd1c1f2f2028a7b851f701fc6a8ebe39dcb95e7c upstream. This was reported many times, and this was even mentioned in commit 52ee2df ("pids: refactor vnr/nr_ns helpers to make them safe") but somehow nobody bothered to fix the obvious problem: task_tgid_nr_ns() is not safe because task->group_leader points to nowhere after the exiting task passes exit_notify(), rcu_read_lock() can not help. We really need to change __unhash_process() to nullify group_leader, parent, and real_parent, but this needs some cleanups. Until then we can turn task_tgid_nr_ns() into another user of __task_pid_nr_ns() and fix the problem. Reported-by: Troy Kensinger <tkensinger@google.com> Signed-off-by: Oleg Nesterov <oleg@redhat.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 46d51a2 commit b4cf490

3 files changed

Lines changed: 34 additions & 31 deletions

File tree

include/linux/pid.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ enum pid_type
88
PIDTYPE_PID,
99
PIDTYPE_PGID,
1010
PIDTYPE_SID,
11-
PIDTYPE_MAX
11+
PIDTYPE_MAX,
12+
/* only valid to __task_pid_nr_ns() */
13+
__PIDTYPE_TGID
1214
};
1315

1416
/*

include/linux/sched.h

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,31 +1949,8 @@ static inline pid_t task_tgid_nr(struct task_struct *tsk)
19491949
return tsk->tgid;
19501950
}
19511951

1952-
pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
1953-
1954-
static inline pid_t task_tgid_vnr(struct task_struct *tsk)
1955-
{
1956-
return pid_vnr(task_tgid(tsk));
1957-
}
1958-
19591952

19601953
static inline int pid_alive(const struct task_struct *p);
1961-
static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
1962-
{
1963-
pid_t pid = 0;
1964-
1965-
rcu_read_lock();
1966-
if (pid_alive(tsk))
1967-
pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
1968-
rcu_read_unlock();
1969-
1970-
return pid;
1971-
}
1972-
1973-
static inline pid_t task_ppid_nr(const struct task_struct *tsk)
1974-
{
1975-
return task_ppid_nr_ns(tsk, &init_pid_ns);
1976-
}
19771954

19781955
static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk,
19791956
struct pid_namespace *ns)
@@ -1998,6 +1975,33 @@ static inline pid_t task_session_vnr(struct task_struct *tsk)
19981975
return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
19991976
}
20001977

1978+
static inline pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1979+
{
1980+
return __task_pid_nr_ns(tsk, __PIDTYPE_TGID, ns);
1981+
}
1982+
1983+
static inline pid_t task_tgid_vnr(struct task_struct *tsk)
1984+
{
1985+
return __task_pid_nr_ns(tsk, __PIDTYPE_TGID, NULL);
1986+
}
1987+
1988+
static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
1989+
{
1990+
pid_t pid = 0;
1991+
1992+
rcu_read_lock();
1993+
if (pid_alive(tsk))
1994+
pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
1995+
rcu_read_unlock();
1996+
1997+
return pid;
1998+
}
1999+
2000+
static inline pid_t task_ppid_nr(const struct task_struct *tsk)
2001+
{
2002+
return task_ppid_nr_ns(tsk, &init_pid_ns);
2003+
}
2004+
20012005
/* obsolete, do not use */
20022006
static inline pid_t task_pgrp_nr(struct task_struct *tsk)
20032007
{

kernel/pid.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,11 @@ pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
526526
if (!ns)
527527
ns = task_active_pid_ns(current);
528528
if (likely(pid_alive(task))) {
529-
if (type != PIDTYPE_PID)
529+
if (type != PIDTYPE_PID) {
530+
if (type == __PIDTYPE_TGID)
531+
type = PIDTYPE_PID;
530532
task = task->group_leader;
533+
}
531534
nr = pid_nr_ns(rcu_dereference(task->pids[type].pid), ns);
532535
}
533536
rcu_read_unlock();
@@ -536,12 +539,6 @@ pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
536539
}
537540
EXPORT_SYMBOL(__task_pid_nr_ns);
538541

539-
pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
540-
{
541-
return pid_nr_ns(task_tgid(tsk), ns);
542-
}
543-
EXPORT_SYMBOL(task_tgid_nr_ns);
544-
545542
struct pid_namespace *task_active_pid_ns(struct task_struct *tsk)
546543
{
547544
return ns_of_pid(task_pid(tsk));

0 commit comments

Comments
 (0)