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, int flavor);
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_DEFINE_TYPE(task_id_token_zone, "task_id_token",
67 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 (IP_VALID(port)) {
97 #if CONFIG_PROC_RESOURCE_LIMITS
98 /*
99 * Ports of type IKOT_TASK_FATAL use task_ident objects to avoid holding a task reference
100 * and are created to send resource limit notifications
101 */
102 int kotype = ip_kotype(port);
103 if (kotype == IKOT_TASK_ID_TOKEN || kotype == IKOT_TASK_FATAL) {
104 ipc_kobject_dealloc_port(port, 0, kotype);
105 } else {
106 panic("%s: unexpected kotype of port %p: got %d",
107 __func__, port, kotype);
108 }
109 #else /* CONFIG_PROC_RESOURCE_LIMITS */
110 ipc_kobject_dealloc_port(port, 0, IKOT_TASK_ID_TOKEN);
111 #endif /* CONFIG_PROC_RESOURCE_LIMITS */
112 }
113
114 zfree(task_id_token_zone, token);
115 }
116
117 void
task_id_token_release(task_id_token_t token)118 task_id_token_release(task_id_token_t token)
119 {
120 tidt_release(token);
121 }
122
123 static void
task_id_token_no_senders(ipc_port_t port,__unused mach_port_mscount_t mscount)124 task_id_token_no_senders(ipc_port_t port, __unused mach_port_mscount_t mscount)
125 {
126 task_id_token_t token;
127
128 token = ipc_kobject_get_stable(port, IKOT_TASK_ID_TOKEN);
129 assert(token != NULL);
130 assert(port->ip_srights == 0);
131
132 tidt_release(token); /* consumes ref given by notification */
133 }
134
135 IPC_KOBJECT_DEFINE(IKOT_TASK_ID_TOKEN,
136 .iko_op_stable = true,
137 .iko_op_no_senders = task_id_token_no_senders);
138
139 kern_return_t
task_create_identity_token(task_t task,task_id_token_t * tokenp)140 task_create_identity_token(
141 task_t task,
142 task_id_token_t *tokenp)
143 {
144 task_id_token_t token;
145 void *bsd_info = NULL;
146
147 if (task == TASK_NULL || task == kernel_task) {
148 return KERN_INVALID_ARGUMENT;
149 }
150
151 token = zalloc_flags(task_id_token_zone, Z_ZERO | Z_WAITOK | Z_NOFAIL);
152
153 task_lock(task);
154
155 bsd_info = get_bsdtask_info(task);
156 if (is_corpsetask(task)) {
157 token->task_uniqueid = task->task_uniqueid;
158 } else if (task->active && bsd_info != NULL) {
159 token->ident = proc_ident(bsd_info);
160 } else {
161 task_unlock(task);
162 zfree(task_id_token_zone, token);
163 return KERN_INVALID_ARGUMENT;
164 }
165
166 task_unlock(task);
167
168 token->port = IP_NULL;
169 /* this reference will be donated to no-senders notification */
170 os_ref_init_count(&token->tidt_refs, NULL, 1);
171
172 *tokenp = token;
173
174 return KERN_SUCCESS;
175 }
176
177 /* Produces (corpse) task reference, does not consume token reference */
178 kern_return_t
task_identity_token_get_task_grp(task_id_token_t token,task_t * taskp,task_grp_t grp)179 task_identity_token_get_task_grp(
180 task_id_token_t token,
181 task_t *taskp,
182 task_grp_t grp)
183 {
184 kern_return_t kr;
185 task_t task;
186
187 if (token == TASK_ID_TOKEN_NULL) {
188 return KERN_INVALID_ARGUMENT;
189 }
190
191 if (token->task_uniqueid) {
192 kr = find_corpse_task_by_uniqueid_grp(token->task_uniqueid, &task, grp); /* produces ref */
193 if (kr) {
194 return KERN_NOT_FOUND;
195 }
196 assert(is_corpsetask(task));
197 } else {
198 void* p = proc_find_ident(&token->ident);
199 if (p == NULL) {
200 return KERN_NOT_FOUND;
201 }
202 task = proc_task(p);
203 task_reference_grp(task, grp); /* produces ref */
204 proc_rele(p);
205 }
206
207 *taskp = task;
208
209 return KERN_SUCCESS;
210 }
211
212 /* Produces task port send right, does not consume token reference */
213 kern_return_t
task_identity_token_get_task_port(task_id_token_t token,task_flavor_t flavor,mach_port_t * portp)214 task_identity_token_get_task_port(
215 task_id_token_t token,
216 task_flavor_t flavor,
217 mach_port_t *portp)
218 {
219 task_t task;
220 kern_return_t kr;
221
222 if (token == TASK_ID_TOKEN_NULL) {
223 return KERN_INVALID_ARGUMENT;
224 }
225
226 if (flavor > TASK_FLAVOR_MAX) {
227 return KERN_INVALID_ARGUMENT;
228 }
229
230 if (token->task_uniqueid) {
231 /*
232 * For corpses, the control port reference would hold the corpse,
233 * only allow conversion to control port for now.
234 */
235 if (flavor != TASK_FLAVOR_CONTROL) {
236 return KERN_INVALID_ARGUMENT;
237 }
238 }
239
240 if ((kr = task_identity_token_get_task_grp(token, &task, TASK_GRP_KERNEL)) != KERN_SUCCESS) {
241 return kr;
242 }
243
244 assert(task != TASK_NULL);
245 assert(token != TASK_ID_TOKEN_NULL);
246
247 /* holding a ref on (corpse) task */
248
249 if (flavor == TASK_FLAVOR_CONTROL && task == current_task()) {
250 *portp = convert_task_to_port_pinned(task); /* consumes task ref */
251 return KERN_SUCCESS;
252 }
253
254 if (flavor <= TASK_FLAVOR_READ &&
255 task_conversion_eval(current_task(), task, flavor)) {
256 task_deallocate(task);
257 return KERN_INVALID_ARGUMENT;
258 }
259
260 #if CONFIG_MACF
261
262 if (task != current_task()) {
263 if (mac_task_check_task_id_token_get_task(task, flavor)) {
264 task_deallocate(task);
265 return KERN_DENIED;
266 }
267 }
268 #endif
269
270 *portp = convert_task_to_port_with_flavor(task, flavor, TASK_GRP_KERNEL);
271 /* task ref consumed */
272
273 return KERN_SUCCESS;
274 }
275
276 /* Produces task reference */
277 static kern_return_t
task_id_token_port_name_to_task_grp(mach_port_name_t name,task_t * task,task_grp_t grp)278 task_id_token_port_name_to_task_grp(
279 mach_port_name_t name,
280 task_t *task,
281 task_grp_t grp)
282 {
283 kern_return_t kr;
284 task_id_token_t token;
285
286 token = port_name_to_task_id_token(name); /* produces ref */
287 kr = task_identity_token_get_task_grp(token, task, grp);
288
289 tidt_release(token); /* consumes ref */
290
291 return kr;
292 }
293 /* Used by kexts only */
294 kern_return_t
task_id_token_port_name_to_task_external(mach_port_name_t name,task_t * task)295 task_id_token_port_name_to_task_external(
296 mach_port_name_t name,
297 task_t *task)
298 {
299 return task_id_token_port_name_to_task_grp(name, task, TASK_GRP_EXTERNAL);
300 }
301 /* Used by kernel proper */
302 kern_return_t
task_id_token_port_name_to_task(mach_port_name_t name,task_t * task)303 task_id_token_port_name_to_task(
304 mach_port_name_t name,
305 task_t *task)
306 {
307 return task_id_token_port_name_to_task_grp(name, task, TASK_GRP_KERNEL);
308 }
309
310 /* Produces token reference */
311 task_id_token_t
convert_port_to_task_id_token(ipc_port_t port)312 convert_port_to_task_id_token(
313 ipc_port_t port)
314 {
315 task_id_token_t token = TASK_ID_TOKEN_NULL;
316
317 if (IP_VALID(port)) {
318 token = ipc_kobject_get_stable(port, IKOT_TASK_ID_TOKEN);
319 if (token != TASK_ID_TOKEN_NULL) {
320 zone_require(task_id_token_zone, token);
321 tidt_reference(token);
322 }
323 }
324 return token;
325 }
326
327 /* Consumes token reference */
328 ipc_port_t
convert_task_id_token_to_port(task_id_token_t token)329 convert_task_id_token_to_port(
330 task_id_token_t token)
331 {
332 __assert_only bool kr;
333
334 if (token == TASK_ID_TOKEN_NULL) {
335 return IP_NULL;
336 }
337
338 zone_require(task_id_token_zone, token);
339
340 kr = ipc_kobject_make_send_lazy_alloc_port(&token->port,
341 token, IKOT_TASK_ID_TOKEN, IPC_KOBJECT_ALLOC_NONE);
342 assert(kr == TRUE); /* no-senders notification is armed, consumes token ref */
343
344 return token->port;
345 }
346
347 #if CONFIG_PROC_RESOURCE_LIMITS
348
349 /* Should be used only by ports of type IKOT_TASK_FATAL at allocation time */
350 void
task_id_token_set_port(task_id_token_t token,ipc_port_t port)351 task_id_token_set_port(
352 task_id_token_t token,
353 ipc_port_t port)
354 {
355 assert(token && port && (ip_kotype(port) == IKOT_TASK_FATAL));
356 token->port = port;
357 }
358 #endif /* CONFIG_PROC_RESOURCE_LIMITS */
359