Skip to content
This repository was archived by the owner on Nov 18, 2021. It is now read-only.

Commit 55289ec

Browse files
oleg-nesterovalthafvly
authored andcommitted
BACKPORT: FROMLIST: pids: make task_tgid_nr_ns() safe
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 <[email protected]> Signed-off-by: Oleg Nesterov <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> (url: https://patchwork.kernel.org/patch/9913055/) Bug: 31495866 Change-Id: I5e67b02a77e805f71fa3a787249f13c1310f02e2
1 parent c4982b0 commit 55289ec

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

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: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,13 +1647,6 @@ static inline pid_t task_tgid_nr(struct task_struct *tsk)
16471647
return tsk->tgid;
16481648
}
16491649

1650-
pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
1651-
1652-
static inline pid_t task_tgid_vnr(struct task_struct *tsk)
1653-
{
1654-
return pid_vnr(task_tgid(tsk));
1655-
}
1656-
16571650

16581651
static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk,
16591652
struct pid_namespace *ns)
@@ -1678,6 +1671,16 @@ static inline pid_t task_session_vnr(struct task_struct *tsk)
16781671
return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
16791672
}
16801673

1674+
static inline pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1675+
{
1676+
return __task_pid_nr_ns(tsk, __PIDTYPE_TGID, ns);
1677+
}
1678+
1679+
static inline pid_t task_tgid_vnr(struct task_struct *tsk)
1680+
{
1681+
return __task_pid_nr_ns(tsk, __PIDTYPE_TGID, NULL);
1682+
}
1683+
16811684
/* obsolete, do not use */
16821685
static inline pid_t task_pgrp_nr(struct task_struct *tsk)
16831686
{

kernel/pid.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,11 @@ pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
520520
if (!ns)
521521
ns = task_active_pid_ns(current);
522522
if (likely(pid_alive(task))) {
523-
if (type != PIDTYPE_PID)
523+
if (type != PIDTYPE_PID) {
524+
if (type == __PIDTYPE_TGID)
525+
type = PIDTYPE_PID;
524526
task = task->group_leader;
527+
}
525528
nr = pid_nr_ns(task->pids[type].pid, ns);
526529
}
527530
rcu_read_unlock();
@@ -530,12 +533,6 @@ pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
530533
}
531534
EXPORT_SYMBOL(__task_pid_nr_ns);
532535

533-
pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
534-
{
535-
return pid_nr_ns(task_tgid(tsk), ns);
536-
}
537-
EXPORT_SYMBOL(task_tgid_nr_ns);
538-
539536
struct pid_namespace *task_active_pid_ns(struct task_struct *tsk)
540537
{
541538
return ns_of_pid(task_pid(tsk));

0 commit comments

Comments
 (0)