1 From dd1c1f2f2028a7b851f701fc6a8ebe39dcb95e7c Mon Sep 17 00:00:00 2001
2 From: Oleg Nesterov <oleg@redhat.com>
3 Date: Mon, 21 Aug 2017 17:35:02 +0200
4 Subject: pids: make task_tgid_nr_ns() safe
6 From: Oleg Nesterov <oleg@redhat.com>
8 commit dd1c1f2f2028a7b851f701fc6a8ebe39dcb95e7c upstream.
10 This was reported many times, and this was even mentioned in commit
11 52ee2dfdd4f5 ("pids: refactor vnr/nr_ns helpers to make them safe") but
12 somehow nobody bothered to fix the obvious problem: task_tgid_nr_ns() is
13 not safe because task->group_leader points to nowhere after the exiting
14 task passes exit_notify(), rcu_read_lock() can not help.
16 We really need to change __unhash_process() to nullify group_leader,
17 parent, and real_parent, but this needs some cleanups. Until then we
18 can turn task_tgid_nr_ns() into another user of __task_pid_nr_ns() and
21 Reported-by: Troy Kensinger <tkensinger@google.com>
22 Signed-off-by: Oleg Nesterov <oleg@redhat.com>
23 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
24 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
26 include/linux/pid.h | 4 +++-
27 include/linux/sched.h | 50 +++++++++++++++++++++++++++-----------------------
28 kernel/pid.c | 11 ++++-------
29 3 files changed, 34 insertions(+), 31 deletions(-)
31 --- a/include/linux/pid.h
32 +++ b/include/linux/pid.h
33 @@ -8,7 +8,9 @@ enum pid_type
39 + /* only valid to __task_pid_nr_ns() */
44 --- a/include/linux/sched.h
45 +++ b/include/linux/sched.h
46 @@ -2132,31 +2132,8 @@ static inline pid_t task_tgid_nr(struct
50 -pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
52 -static inline pid_t task_tgid_vnr(struct task_struct *tsk)
54 - return pid_vnr(task_tgid(tsk));
58 static inline int pid_alive(const struct task_struct *p);
59 -static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
65 - pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
71 -static inline pid_t task_ppid_nr(const struct task_struct *tsk)
73 - return task_ppid_nr_ns(tsk, &init_pid_ns);
76 static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk,
77 struct pid_namespace *ns)
78 @@ -2181,6 +2158,33 @@ static inline pid_t task_session_vnr(str
79 return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
82 +static inline pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
84 + return __task_pid_nr_ns(tsk, __PIDTYPE_TGID, ns);
87 +static inline pid_t task_tgid_vnr(struct task_struct *tsk)
89 + return __task_pid_nr_ns(tsk, __PIDTYPE_TGID, NULL);
92 +static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
98 + pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
104 +static inline pid_t task_ppid_nr(const struct task_struct *tsk)
106 + return task_ppid_nr_ns(tsk, &init_pid_ns);
109 /* obsolete, do not use */
110 static inline pid_t task_pgrp_nr(struct task_struct *tsk)
114 @@ -526,8 +526,11 @@ pid_t __task_pid_nr_ns(struct task_struc
116 ns = task_active_pid_ns(current);
117 if (likely(pid_alive(task))) {
118 - if (type != PIDTYPE_PID)
119 + if (type != PIDTYPE_PID) {
120 + if (type == __PIDTYPE_TGID)
121 + type = PIDTYPE_PID;
122 task = task->group_leader;
124 nr = pid_nr_ns(rcu_dereference(task->pids[type].pid), ns);
127 @@ -536,12 +539,6 @@ pid_t __task_pid_nr_ns(struct task_struc
129 EXPORT_SYMBOL(__task_pid_nr_ns);
131 -pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
133 - return pid_nr_ns(task_tgid(tsk), ns);
135 -EXPORT_SYMBOL(task_tgid_nr_ns);
137 struct pid_namespace *task_active_pid_ns(struct task_struct *tsk)
139 return ns_of_pid(task_pid(tsk));