1 /*
2 * Copyright (c) 2015 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 <mach/exception_types.h>
30 #include <mach/mach_types.h>
31 #include <osfmk/kern/exception.h>
32 #include <osfmk/kern/task.h>
33 #include <sys/codesign.h>
34 #include <sys/param.h>
35 #include <sys/user.h>
36 #include <sys/proc.h>
37 #include <sys/proc_internal.h>
38 #include <sys/kauth.h>
39
40 #include <kern/task.h>
41 #include <kern/telemetry.h>
42
43 #include <security/mac_framework.h>
44 #include <security/mac_internal.h>
45 #include <security/mac_mach_internal.h>
46
47 #if CONFIG_CSR
48 #include <sys/csr.h>
49 // Panic on internal builds, just log otherwise.
50 #define MAC_MACH_UNEXPECTED(fmt...) \
51 if (csr_check(CSR_ALLOW_APPLE_INTERNAL) == 0) { panic(fmt); } else { printf(fmt); }
52 #else
53 #define MAC_MACH_UNEXPECTED(fmt...) printf(fmt)
54 #endif
55
56 static struct proc *
mac_task_get_proc(struct task * task)57 mac_task_get_proc(struct task *task)
58 {
59 if (task == current_task()) {
60 return proc_self();
61 }
62
63 /*
64 * Tasks don't really hold a reference on a proc unless the
65 * calling thread belongs to the task in question.
66 */
67 int pid = task_pid(task);
68 struct proc *p = proc_find(pid);
69
70 if (p != NULL) {
71 if (proc_task(p) == task) {
72 return p;
73 }
74 proc_rele(p);
75 }
76 return NULL;
77 }
78
79 int
mac_task_check_expose_task(struct task * task,mach_task_flavor_t flavor)80 mac_task_check_expose_task(struct task *task, mach_task_flavor_t flavor)
81 {
82 int error;
83
84 assert(flavor <= TASK_FLAVOR_NAME);
85
86 struct proc *p = mac_task_get_proc(task);
87 if (p == NULL) {
88 return ESRCH;
89 }
90 struct proc_ident pident = proc_ident(p);
91
92 struct ucred *cred = kauth_cred_get();
93 proc_rele(p);
94
95 MAC_CHECK(proc_check_expose_task_with_flavor, cred, &pident, flavor);
96
97 return error;
98 }
99
100 int
mac_task_check_task_id_token_get_task(struct task * task,mach_task_flavor_t flavor)101 mac_task_check_task_id_token_get_task(struct task *task, mach_task_flavor_t flavor)
102 {
103 int error;
104 struct proc *target_proc = NULL;
105 struct proc_ident *pidentp = NULL;
106 struct proc_ident pident;
107
108 assert(flavor <= TASK_FLAVOR_NAME);
109
110 if (!is_corpsetask(task)) {
111 /* only live task has proc */
112 target_proc = mac_task_get_proc(task);
113 if (target_proc == NULL) {
114 return ESRCH;
115 }
116 pident = proc_ident(target_proc);
117 pidentp = &pident;
118 proc_rele(target_proc);
119 }
120
121 kauth_cred_t cred = kauth_cred_proc_ref(current_proc());
122
123 /* pidentp is NULL for corpse task */
124 MAC_CHECK(proc_check_task_id_token_get_task, cred, pidentp, flavor);
125 kauth_cred_unref(&cred);
126 return error;
127 }
128
129 int
mac_task_check_get_movable_control_port(void)130 mac_task_check_get_movable_control_port(void)
131 {
132 int error;
133 struct proc *p = current_proc();
134
135 kauth_cred_t cred = kauth_cred_proc_ref(p);
136 MAC_CHECK(proc_check_get_movable_control_port, cred);
137 kauth_cred_unref(&cred);
138 return error;
139 }
140
141 int
mac_task_check_set_host_special_port(struct task * task,int id,struct ipc_port * port)142 mac_task_check_set_host_special_port(struct task *task, int id, struct ipc_port *port)
143 {
144 int error;
145
146 struct proc *p = mac_task_get_proc(task);
147 if (p == NULL) {
148 return ESRCH;
149 }
150
151 kauth_cred_t cred = kauth_cred_proc_ref(p);
152 MAC_CHECK(proc_check_set_host_special_port, cred, id, port);
153 kauth_cred_unref(&cred);
154 proc_rele(p);
155 return error;
156 }
157
158 int
mac_task_check_set_host_exception_port(struct task * task,unsigned int exception)159 mac_task_check_set_host_exception_port(struct task *task, unsigned int exception)
160 {
161 int error;
162
163 struct proc *p = mac_task_get_proc(task);
164 if (p == NULL) {
165 return ESRCH;
166 }
167
168 kauth_cred_t cred = kauth_cred_proc_ref(p);
169 MAC_CHECK(proc_check_set_host_exception_port, cred, exception);
170 kauth_cred_unref(&cred);
171 proc_rele(p);
172 return error;
173 }
174
175 int
mac_task_check_get_task_special_port(struct task * task,struct task * target,int which)176 mac_task_check_get_task_special_port(struct task *task, struct task *target, int which)
177 {
178 int error;
179 struct proc *target_proc = NULL;
180 struct proc_ident *pidentp = NULL;
181 struct proc_ident pident;
182
183 struct proc *p = mac_task_get_proc(task);
184 if (p == NULL) {
185 return ESRCH;
186 }
187
188 if (!is_corpsetask(target)) {
189 /* only live task has proc */
190 target_proc = mac_task_get_proc(target);
191 if (target_proc == NULL) {
192 proc_rele(p);
193 return ESRCH;
194 }
195 pident = proc_ident(target_proc);
196 pidentp = &pident;
197 proc_rele(target_proc);
198 }
199
200 kauth_cred_t cred = kauth_cred_proc_ref(p);
201 proc_rele(p);
202
203 /* pidentp is NULL for corpse task */
204 MAC_CHECK(proc_check_get_task_special_port, cred, pidentp, which);
205 kauth_cred_unref(&cred);
206 return error;
207 }
208
209 int
mac_task_check_set_task_special_port(struct task * task,struct task * target,int which,struct ipc_port * port)210 mac_task_check_set_task_special_port(struct task *task, struct task *target, int which, struct ipc_port *port)
211 {
212 int error;
213
214 struct proc *p = mac_task_get_proc(task);
215 if (p == NULL) {
216 return ESRCH;
217 }
218
219 /*
220 * task_set_special_port() is a CONTROL level interface, so we are guaranteed
221 * by MIG intrans that target is not a corpse.
222 */
223 assert(!is_corpsetask(target));
224
225 struct proc *targetp = mac_task_get_proc(target);
226 if (targetp == NULL) {
227 proc_rele(p);
228 return ESRCH;
229 }
230
231 struct proc_ident pident = proc_ident(targetp);
232 kauth_cred_t cred = kauth_cred_proc_ref(p);
233 proc_rele(targetp);
234 proc_rele(p);
235
236 MAC_CHECK(proc_check_set_task_special_port, cred, &pident, which, port);
237 kauth_cred_unref(&cred);
238 return error;
239 }
240
241 int
mac_task_check_dyld_process_info_notify_register(void)242 mac_task_check_dyld_process_info_notify_register(void)
243 {
244 int error;
245 struct proc *p = current_proc();
246
247 kauth_cred_t cred = kauth_cred_proc_ref(p);
248 MAC_CHECK(proc_check_dyld_process_info_notify_register, cred);
249 kauth_cred_unref(&cred);
250 return error;
251 }
252
253 int
mac_task_check_set_host_exception_ports(struct task * task,unsigned int exception_mask)254 mac_task_check_set_host_exception_ports(struct task *task, unsigned int exception_mask)
255 {
256 int error = 0;
257 int exception;
258
259 struct proc *p = mac_task_get_proc(task);
260 if (p == NULL) {
261 return ESRCH;
262 }
263
264 kauth_cred_t cred = kauth_cred_proc_ref(p);
265 for (exception = FIRST_EXCEPTION; exception < EXC_TYPES_COUNT; exception++) {
266 if (exception_mask & (1 << exception)) {
267 MAC_CHECK(proc_check_set_host_exception_port, cred, exception);
268 if (error) {
269 break;
270 }
271 }
272 }
273 kauth_cred_unref(&cred);
274 proc_rele(p);
275 return error;
276 }
277
278 void
mac_thread_userret(struct thread * td)279 mac_thread_userret(struct thread *td)
280 {
281 MAC_PERFORM(thread_userret, td);
282 }
283
284 void
mac_thread_telemetry(struct thread * t,int err,void * data,size_t length)285 mac_thread_telemetry(struct thread *t, int err, void *data, size_t length)
286 {
287 MAC_PERFORM(thread_telemetry, t, err, data, length);
288 }
289
290 void
mac_proc_notify_exec_complete(struct proc * proc)291 mac_proc_notify_exec_complete(struct proc *proc)
292 {
293 thread_t thread = current_thread();
294
295 /*
296 * Since this MAC hook was designed to support upcalls, make sure the hook
297 * is called with kernel importance propagation enabled so any daemons
298 * can get any appropriate importance donations.
299 */
300 thread_enable_send_importance(thread, TRUE);
301 MAC_PERFORM(proc_notify_exec_complete, proc);
302 thread_enable_send_importance(thread, FALSE);
303 }
304
305 /**** Exception Policy
306 *
307 * Note that the functions below do not fully follow the usual convention for mac policy functions
308 * in the kernel. Besides avoiding confusion in how the mac function names are mixed with the actual
309 * policy function names, we diverge because the exception policy is somewhat special:
310 * It is used in places where allocation and association must be separate, and its labels do not
311 * only belong to one type of object as usual, but to two (on exception actions and on tasks as
312 * crash labels).
313 */
314
315 struct label *
mac_exc_label(struct exception_action * action)316 mac_exc_label(struct exception_action *action)
317 {
318 return mac_label_verify(&action->label);
319 }
320
321 void
mac_exc_set_label(struct exception_action * action,struct label * label)322 mac_exc_set_label(struct exception_action *action, struct label *label)
323 {
324 action->label = label;
325 }
326
327 // Label allocation and deallocation, may sleep.
328
329 struct label *
mac_exc_create_label(struct exception_action * action)330 mac_exc_create_label(struct exception_action *action)
331 {
332 return mac_labelzone_alloc_for_owner(action ? &action->label : NULL, MAC_WAITOK, ^(struct label *label) {
333 // Policy initialization of the label, typically performs allocations as well.
334 // (Unless the policy's full data really fits into a pointer size.)
335 MAC_PERFORM(exc_action_label_init, label);
336 });
337 }
338
339 void
mac_exc_free_label(struct label * label)340 mac_exc_free_label(struct label *label)
341 {
342 MAC_PERFORM(exc_action_label_destroy, label);
343 mac_labelzone_free(label);
344 }
345
346 // Action label initialization and teardown, may sleep.
347
348 void
mac_exc_associate_action_label(struct exception_action * action,struct label * label)349 mac_exc_associate_action_label(struct exception_action *action, struct label *label)
350 {
351 mac_exc_set_label(action, label);
352 MAC_PERFORM(exc_action_label_associate, action, mac_exc_label(action));
353 }
354
355 void
mac_exc_free_action_label(struct exception_action * action)356 mac_exc_free_action_label(struct exception_action *action)
357 {
358 mac_exc_free_label(mac_exc_label(action));
359 mac_exc_set_label(action, NULL);
360 }
361
362 // Action label update and inheritance, may NOT sleep and must be quick.
363
364 int
mac_exc_update_action_label(struct exception_action * action,struct label * newlabel)365 mac_exc_update_action_label(struct exception_action *action,
366 struct label *newlabel)
367 {
368 int error;
369
370 MAC_CHECK(exc_action_label_update, action, mac_exc_label(action), newlabel);
371
372 return error;
373 }
374
375 int
mac_exc_inherit_action_label(struct exception_action * parent,struct exception_action * child)376 mac_exc_inherit_action_label(struct exception_action *parent,
377 struct exception_action *child)
378 {
379 return mac_exc_update_action_label(child, mac_exc_label(parent));
380 }
381
382 int
mac_exc_update_task_crash_label(struct task * task,struct label * label)383 mac_exc_update_task_crash_label(struct task *task, struct label *label)
384 {
385 int error;
386
387 assert(task != kernel_task);
388
389 struct label *crash_label = get_task_crash_label(task);
390
391 MAC_CHECK(exc_action_label_update, NULL, crash_label, label);
392
393 return error;
394 }
395
396 // Process label creation, may sleep.
397
398 struct label *
mac_exc_create_label_for_proc(struct proc * proc)399 mac_exc_create_label_for_proc(struct proc *proc)
400 {
401 struct label *label = mac_exc_create_label(NULL);
402 MAC_PERFORM(exc_action_label_populate, label, proc);
403 return label;
404 }
405
406 struct label *
mac_exc_create_label_for_current_proc(void)407 mac_exc_create_label_for_current_proc(void)
408 {
409 return mac_exc_create_label_for_proc(current_proc());
410 }
411
412 // Exception handler policy checking, may sleep.
413
414 int
mac_exc_action_check_exception_send(struct task * victim_task,struct exception_action * action)415 mac_exc_action_check_exception_send(struct task *victim_task, struct exception_action *action)
416 {
417 int error = 0;
418
419 struct proc *p = get_bsdtask_info(victim_task);
420 struct label *bsd_label = NULL;
421 struct label *label = NULL;
422
423 if (p != NULL) {
424 // Create a label from the still existing bsd process...
425 label = bsd_label = mac_exc_create_label_for_proc(p);
426 } else {
427 // ... otherwise use the crash label on the task.
428 label = get_task_crash_label(victim_task);
429 }
430
431 if (label == NULL) {
432 MAC_MACH_UNEXPECTED("mac_exc_action_check_exception_send: no exc_action label for process");
433 return EPERM;
434 }
435
436 MAC_CHECK(exc_action_check_exception_send, label, action, mac_exc_label(action));
437
438 if (bsd_label != NULL) {
439 mac_exc_free_label(bsd_label);
440 }
441
442 return error;
443 }
444
445 int
mac_schedule_telemetry(void)446 mac_schedule_telemetry(void)
447 {
448 return telemetry_macf_mark_curthread();
449 }
450