Debian Codegraph @

runtime/linux/namespaces.h

raw
1 /* -*- linux-c -*-
2 * Namespace Functions
3 * Copyright (C) 2015 Red Hat Inc.
4 *
5 * This file is part of systemtap, and is free software. You can
6 * redistribute it and/or modify it under the terms of the GNU General
7 * Public License (GPL); either version 2, or (at your option) any
8 * later version.
9 */
10
11 #ifndef _LINUX_NAMESPACES_H_
12 #define _LINUX_NAMESPACES_H_
13
14 #if defined(CONFIG_PID_NS)
15 #include <linux/pid_namespace.h>
16 #endif
17
18 #if defined(CONFIG_USER_NS)
19 #include <linux/user_namespace.h>
20 #endif
21
22 typedef enum {
23 PID,
24 TID,
25 PGRP,
26 SID
27 } PIDINFOTYPE;
28
29 typedef enum {
30 UID,
31 EUID,
32 GID,
33 EGID
34 } USERINFOTYPE;
35
36
37 // The get_task_from_pid() function assumes the rcu_read_lock is
38 // held. The returned task_struct, if not NULL, has its reference
39 // count increased. Callers must call put_task_struct() when finished
40 // to decrease the reference count.
41 //
42 // Also note that the returned task_struct pointer, if not NULL,
43 // should be a valid task_struct pointer that doesn't need to be
44 // dereferenced before using.
45 static struct task_struct *get_task_from_pid(int target_pid)
46 {
47 struct task_struct *target_ns_task = NULL;
48 #if defined(CONFIG_PID_NS) || defined(CONFIG_USER_NS)
49 struct pid *target_ns_pid;
50
51 // can't use find_get_pid() since it ends up looking for the
52 // STP_TARGET_NS_PID in the current process' ns, when the PID is
53 // what's seen in the init ns (I think)
54 target_ns_pid = find_pid_ns(target_pid, &init_pid_ns);
55
56 if (!target_ns_pid)
57 return NULL;
58
59 // use pid_task instead of since we want to handle our own locking
60 target_ns_task = pid_task(target_ns_pid, PIDTYPE_PID);
61 if (!target_ns_task)
62 return NULL;
63
64 get_task_struct(target_ns_task);
65 #endif
66 return target_ns_task;
67 }
68
69
70 // The get_pid_namespace() function assumes the rcu_read_lock is
71 // held. The returned pid_namespace, if not NULL, has its reference
72 // count increased. Callers must call put_pid_ns() when finished to
73 // decrease the reference count.
74 static struct pid_namespace *get_pid_namespace(int target_ns)
75 {
76 #if defined(CONFIG_PID_NS)
77 struct task_struct *target_ns_task;
78 struct pid_namespace *target_pid_ns;
79
80 target_ns_task = get_task_from_pid(target_ns);
81 if (!target_ns_task)
82 return NULL;
83
84 target_pid_ns = task_active_pid_ns(target_ns_task);
85 if (target_pid_ns)
86 get_pid_ns(target_pid_ns);
87
88 // there is no put_pid_task(), so do the next best thing
89 put_task_struct(target_ns_task);
90 return target_pid_ns;
91
92 #else
93 return NULL;
94 #endif
95 }
96
97
98 // The _stp_task_struct_valid() function ensures that 't' is a valid
99 // task by looking for it on the kernel's task list. Returns 1 if the
100 // task struct pointer was found, 0 if not found.
101 static int _stp_task_struct_valid(struct task_struct *t)
102 {
103 struct task_struct *grp, *tsk;
104 int rc = 0;
105
106 rcu_read_lock();
107 for_each_process_thread(grp, tsk) {
108 if (tsk == t) {
109 rc = 1;
110 goto do_each_thread_out;
111 }
112 }
113 do_each_thread_out:
114 rcu_read_unlock();
115 return rc;
116 }
117
118
119 static int from_target_pid_ns(struct task_struct *ts, PIDINFOTYPE type)
120 {
121 #if defined(CONFIG_PID_NS) && LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)
122 struct pid_namespace *target_pid_ns;
123 int ret = -1;
124
125 // At this point, the caller above us has determined that the memory
126 // at 'ts' is valid to read. However, we *really* need to know not
127 // only if this memory is valid to read, but is it a real task
128 // struct pointer. Why? For example, the task_tgid_nr_ns() function
129 // calls task_tgid() on 'ts', then passes that result to
130 // pid_nr_ns(). task_tgid() reads
131 // ts->group_leader->pids[N]. pid_nr_ns() reads pid->numbers[N].
132 //
133 // We could try dereferencing all those items (and hope that the
134 // kernel implementation of those functions doesn't change), but at
135 // this point we're getting bogged down in the internals of
136 // task_tgid(), task_tgid_nr_ns() and pid_nr_ns().
137 //
138 // So, instead let's try making sure that 'ts' is a valid task by
139 // looking for it on the task list. We assume that if 'ts' is
140 // actually on the task list, we can call task_*_nr_ns() on it
141 // without accessing invalid memory and causing a kernel crash.
142 rcu_read_lock();
143 if (! _stp_task_struct_valid(ts)) {
144 rcu_read_unlock();
145 return -1;
146 }
147
148 target_pid_ns = get_pid_namespace(_stp_namespaces_pid);
149 if (!target_pid_ns) {
150 rcu_read_unlock();
151 return -1;
152 }
153
154 switch (type) {
155 case PID:
156 ret = task_tgid_nr_ns(ts, target_pid_ns);
157 break;
158 case TID:
159 ret = task_pid_nr_ns(ts, target_pid_ns);
160 break;
161 case PGRP:
162 ret = task_pgrp_nr_ns(ts, target_pid_ns);
163 break;
164 case SID:
165 ret = task_session_nr_ns(ts, target_pid_ns);
166 break;
167 }
168
169 // done with the pid namespace and the read lock
170 put_pid_ns(target_pid_ns);
171 rcu_read_unlock();
172 return ret;
173 #else
174 return -1;
175 #endif
176 }
177
178
179 // The get_user_namespace() function assumes the rcu_read_lock is
180 // held. The returned user_namespace, if not NULL, has its reference
181 // count increased by get_user_ns(). Callers must call put_user_ns()
182 // when finished with the returned user_namespace to decrease the
183 // reference count.
184 static struct user_namespace *get_user_namespace(int target_ns)
185 {
186 #if defined(CONFIG_USER_NS)
187 struct task_struct *target_ns_task;
188 struct user_namespace *target_user_ns;
189
190 target_ns_task = get_task_from_pid(target_ns);
191 if (!target_ns_task)
192 return NULL;
193
194 /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39) */
195 target_user_ns = task_cred_xxx(target_ns_task, user_ns);
196
197 if (target_user_ns)
198 get_user_ns(target_user_ns);
199 put_task_struct(target_ns_task);
200 return target_user_ns;
201 #endif /* defined(CONFIG_USER_NS) */
202 return NULL;
203 }
204
205 static int from_target_user_ns(struct task_struct *ts, USERINFOTYPE type)
206 {
207 #if defined(CONFIG_USER_NS)
208 struct user_namespace *target_user_ns;
209 int ret = -1;
210
211 rcu_read_lock();
212 target_user_ns = get_user_namespace(_stp_namespaces_pid);
213 if (!target_user_ns) {
214 rcu_read_unlock();
215 return -1;
216 }
217
218 switch (type) {
219 case UID:
220 ret = from_kuid_munged(target_user_ns, task_uid(ts));
221 break;
222 case EUID:
223 ret = from_kuid_munged(target_user_ns, task_euid(ts));
224 break;
225 case GID:
226 /* If task_gid() isn't defined, make our own. */
227 #if !defined(task_gid) && defined(task_cred_xxx)
228 #define task_gid(task) (task_cred_xxx((task), gid))
229 #endif
230 ret = from_kgid_munged(target_user_ns, task_gid(ts));
231 break;
232 case EGID:
233 /* If task_egid() isn't defined, make our own. */
234 #if !defined(task_egid) && defined(task_cred_xxx)
235 #define task_egid(task) (task_cred_xxx((task), egid))
236 #endif
237 ret = from_kgid_munged(target_user_ns, task_egid(ts));
238 break;
239 }
240
241 // done with the user namespace and the read lock
242 put_user_ns(target_user_ns);
243 rcu_read_unlock();
244 return ret;
245 #else
246 return -1;
247 #endif
248 }
249
250 #endif /* _LINUX_NAMESPACES_H_ */
251