1 /*
2 * Copyright (c) 2020 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <os/refcnt.h>
30
31 #include <kern/ipc_kobject.h>
32 #include <kern/ipc_tt.h>
33 #include <kern/task_ident.h>
34
35 #include <mach/mach_types.h>
36 #include <mach/task.h>
37 #include <mach/notify.h>
38 #include <mach/kern_return.h>
39
40 #include <security/mac_mach_internal.h>
41 #include <kern/task_ident.h>
42 #include <corpses/task_corpse.h>
43
44 struct proc_ident {
45 uint64_t p_uniqueid;
46 pid_t p_pid;
47 int p_idversion;
48 };
49
50 extern void* proc_find_ident(struct proc_ident const *i);
51 extern int proc_rele(void* p);
52 extern task_t proc_task(void* p);
53 extern struct proc_ident proc_ident(void* p);
54 extern kern_return_t task_conversion_eval(task_t caller, task_t victim);
55
56 /* Exported to kexts */
57 extern typeof(task_id_token_port_name_to_task) task_id_token_port_name_to_task_external;
58
59 struct task_id_token {
60 struct proc_ident ident;
61 ipc_port_t port;
62 uint64_t task_uniqueid; /* for corpse task */
63 os_refcnt_t tidt_refs;
64 };
65
66 static ZONE_DECLARE(task_id_token_zone, "task_id_token",
67 sizeof(struct task_id_token), ZC_ZFREE_CLEARMEM);
68
69 void task_id_token_set_port(task_id_token_t token, ipc_port_t port);
70
71 static void
tidt_reference(task_id_token_t token)72 tidt_reference(task_id_token_t token)
73 {
74 if (token == TASK_ID_TOKEN_NULL) {
75 return;
76 }
77 os_ref_retain(&token->tidt_refs);
78 }
79
80 static void
tidt_release(task_id_token_t token)81 tidt_release(task_id_token_t token)
82 {
83 ipc_port_t port;
84
85 if (token == TASK_ID_TOKEN_NULL) {
86 return;
87 }
88
89 if (os_ref_release(&token->tidt_refs) > 0) {
90 return;
91 }
92
93 /* last ref */
94 port = token->port;
95
96 #if CONFIG_PROC_RESOURCE_LIMITS
97 /*
98 * Ports of type IKOT_TASK_FATAL use task_ident objects to avoid holding a task reference
99 * and are created to send resource limit notifications
100 */
101 int kotype = ip_kotype(port);
102 if (kotype == IKOT_TASK_ID_TOKEN || kotype == IKOT_TASK_FATAL) {
103 ipc_kobject_dealloc_port(port, 0, kotype);
104 } else {
105 panic("%s: unexpected kotype of port %p: got %d",
106 __func__, port, kotype);
107 }
108 #else /* CONFIG_PROC_RESOURCE_LIMITS */
109 ipc_kobject_dealloc_port(port, 0, IKOT_TASK_ID_TOKEN);
110 #endif /* CONFIG_PROC_RESOURCE_LIMITS */
111
112 zfree(task_id_token_zone, token);
113 }
114
115 void
task_id_token_release(task_id_token_t token)116 task_id_token_release(task_id_token_t token)
117 {
118 tidt_release(token);
119 }
120
121 static void
task_id_token_no_senders(ipc_port_t port,__unused mach_port_mscount_t mscount)122 task_id_token_no_senders(ipc_port_t port, __unused mach_port_mscount_t mscount)
123 {
124 task_id_token_t token;
125
126 token = ipc_kobject_get_stable(port, IKOT_TASK_ID_TOKEN);
127 assert(token != NULL);
128 assert(port->ip_srights == 0);
129
130 tidt_release(token); /* consumes ref given by notification */
131 }
132
133 IPC_KOBJECT_DEFINE(IKOT_TASK_ID_TOKEN,
134 .iko_op_stable = true,
135 .iko_op_no_senders = task_id_token_no_senders);
136
137 kern_return_t
task_create_identity_token(task_t task,task_id_token_t * tokenp)138 task_create_identity_token(
139 task_t task,
140 task_id_token_t *tokenp)
141 {
142 task_id_token_t token;
143
144 if (task == TASK_NULL || task == kernel_task) {
145 return KERN_INVALID_ARGUMENT;
146 }
147
148 token = zalloc_flags(task_id_token_zone, Z_ZERO | Z_WAITOK | Z_NOFAIL);
149
150 task_lock(task);
151
152 if (task->bsd_info) {
153 token->ident = proc_ident(task->bsd_info);
154 } else if (is_corpsetask(task)) {
155 token->task_uniqueid = task->task_uniqueid;
156 } else {
157 task_unlock(task);
158 zfree(task_id_token_zone, token);
159 return KERN_INVALID_ARGUMENT;
160 }
161
162 token->port = IP_NULL;
163 /* this reference will be donated to no-senders notification */
164 os_ref_init_count(&token->tidt_refs, NULL, 1);
165
166 task_unlock(task);
167
168 *tokenp = token;
169
170 return KERN_SUCCESS;
171 }
172
173 /* Produces (corpse) task reference, does not consume token reference */
174 kern_return_t
task_identity_token_get_task_grp(task_id_token_t token,task_t * taskp,task_grp_t grp)175 task_identity_token_get_task_grp(
176 task_id_token_t token,
177 task_t *taskp,
178 task_grp_t grp)
179 {
180 kern_return_t kr;
181 task_t task;
182
183 if (token == TASK_ID_TOKEN_NULL) {
184 return KERN_INVALID_ARGUMENT;
185 }
186
187 if (token->task_uniqueid) {
188 kr = find_corpse_task_by_uniqueid_grp(token->task_uniqueid, &task, grp); /* produces ref */
189 if (kr) {
190 return KERN_NOT_FOUND;
191 }
192 assert(is_corpsetask(task));
193 } else {
194 void* p = proc_find_ident(&token->ident);
195 if (p == NULL) {
196 return KERN_NOT_FOUND;
197 }
198 task = proc_task(p);
199 task_reference_grp(task, grp); /* produces ref */
200 proc_rele(p);
201 }
202
203 *taskp = task;
204
205 return KERN_SUCCESS;
206 }
207
208 /* Produces task port send right, does not consume token reference */
209 kern_return_t
task_identity_token_get_task_port(task_id_token_t token,task_flavor_t flavor,mach_port_t * portp)210 task_identity_token_get_task_port(
211 task_id_token_t token,
212 task_flavor_t flavor,
213 mach_port_t *portp)
214 {
215 task_t task;
216 kern_return_t kr;
217
218 if (token == TASK_ID_TOKEN_NULL) {
219 return KERN_INVALID_ARGUMENT;
220 }
221
222 if (flavor > TASK_FLAVOR_MAX) {
223 return KERN_INVALID_ARGUMENT;
224 }
225
226 if (token->task_uniqueid) {
227 /*
228 * For corpses, the control port reference would hold the corpse,
229 * only allow conversion to control port for now.
230 */
231 if (flavor != TASK_FLAVOR_CONTROL) {
232 return KERN_INVALID_ARGUMENT;
233 }
234 }
235
236 if ((kr = task_identity_token_get_task_grp(token, &task, TASK_GRP_KERNEL)) != KERN_SUCCESS) {
237 return kr;
238 }
239
240 assert(task != TASK_NULL);
241 assert(token != TASK_ID_TOKEN_NULL);
242
243 /* holding a ref on (corpse) task */
244
245 if (flavor == TASK_FLAVOR_CONTROL && task == current_task()) {
246 *portp = convert_task_to_port_pinned(task); /* consumes task ref */
247 return KERN_SUCCESS;
248 }
249 if (flavor <= TASK_FLAVOR_INSPECT && task_conversion_eval(current_task(), task)) {
250 task_deallocate(task);
251 return KERN_INVALID_ARGUMENT;
252 }
253
254 #if CONFIG_MACF
255
256 if (task != current_task()) {
257 if (mac_task_check_task_id_token_get_task(task, flavor)) {
258 task_deallocate(task);
259 return KERN_DENIED;
260 }
261 }
262 #endif
263
264 *portp = convert_task_to_port_with_flavor(task, flavor, TASK_GRP_KERNEL);
265 /* task ref consumed */
266
267 return KERN_SUCCESS;
268 }
269
270 /* Produces task reference */
271 static kern_return_t
task_id_token_port_name_to_task_grp(mach_port_name_t name,task_t * task,task_grp_t grp)272 task_id_token_port_name_to_task_grp(
273 mach_port_name_t name,
274 task_t *task,
275 task_grp_t grp)
276 {
277 kern_return_t kr;
278 task_id_token_t token;
279
280 token = port_name_to_task_id_token(name); /* produces ref */
281 kr = task_identity_token_get_task_grp(token, task, grp);
282
283 tidt_release(token); /* consumes ref */
284
285 return kr;
286 }
287 /* Used by kexts only */
288 kern_return_t
task_id_token_port_name_to_task_external(mach_port_name_t name,task_t * task)289 task_id_token_port_name_to_task_external(
290 mach_port_name_t name,
291 task_t *task)
292 {
293 return task_id_token_port_name_to_task_grp(name, task, TASK_GRP_EXTERNAL);
294 }
295 /* Used by kernel proper */
296 kern_return_t
task_id_token_port_name_to_task(mach_port_name_t name,task_t * task)297 task_id_token_port_name_to_task(
298 mach_port_name_t name,
299 task_t *task)
300 {
301 return task_id_token_port_name_to_task_grp(name, task, TASK_GRP_KERNEL);
302 }
303
304 /* Produces token reference */
305 task_id_token_t
convert_port_to_task_id_token(ipc_port_t port)306 convert_port_to_task_id_token(
307 ipc_port_t port)
308 {
309 task_id_token_t token = TASK_ID_TOKEN_NULL;
310
311 if (IP_VALID(port)) {
312 token = ipc_kobject_get_stable(port, IKOT_TASK_ID_TOKEN);
313 if (token != TASK_ID_TOKEN_NULL) {
314 zone_require(task_id_token_zone, token);
315 tidt_reference(token);
316 }
317 }
318 return token;
319 }
320
321 /* Consumes token reference */
322 ipc_port_t
convert_task_id_token_to_port(task_id_token_t token)323 convert_task_id_token_to_port(
324 task_id_token_t token)
325 {
326 boolean_t kr;
327
328 if (token == TASK_ID_TOKEN_NULL) {
329 return IP_NULL;
330 }
331
332 zone_require(task_id_token_zone, token);
333
334 kr = ipc_kobject_make_send_lazy_alloc_port(&token->port,
335 (ipc_kobject_t) token, IKOT_TASK_ID_TOKEN,
336 IPC_KOBJECT_ALLOC_NONE, 0);
337 assert(kr == TRUE); /* no-senders notification is armed, consumes token ref */
338
339 return token->port;
340 }
341
342 #if CONFIG_PROC_RESOURCE_LIMITS
343
344 /* Should be used only by ports of type IKOT_TASK_FATAL at allocation time */
345 void
task_id_token_set_port(task_id_token_t token,ipc_port_t port)346 task_id_token_set_port(
347 task_id_token_t token,
348 ipc_port_t port)
349 {
350 assert(token && port && (ip_kotype(port) == IKOT_TASK_FATAL));
351 token->port = port;
352 }
353 #endif /* CONFIG_PROC_RESOURCE_LIMITS */
354