]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ipc_kobject.c
xnu-7195.60.75.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_kobject.c
1 /*
2 * Copyright (c) 2000-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 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
60 * Version 2.0.
61 * Copyright (c) 2005 SPARTA, Inc.
62 */
63 /*
64 */
65 /*
66 * File: kern/ipc_kobject.c
67 * Author: Rich Draves
68 * Date: 1989
69 *
70 * Functions for letting a port represent a kernel object.
71 */
72
73 #include <mach_debug.h>
74 #include <mach_ipc_test.h>
75 #include <mach/mig.h>
76 #include <mach/port.h>
77 #include <mach/kern_return.h>
78 #include <mach/message.h>
79 #include <mach/mig_errors.h>
80 #include <mach/notify.h>
81 #include <mach/ndr.h>
82 #include <mach/vm_param.h>
83
84 #include <mach/mach_vm_server.h>
85 #include <mach/mach_port_server.h>
86 #include <mach/mach_host_server.h>
87 #include <mach/host_priv_server.h>
88 #include <mach/host_security_server.h>
89 #include <mach/clock_server.h>
90 #include <mach/clock_priv_server.h>
91 #include <mach/lock_set_server.h>
92 #include <mach/memory_entry_server.h>
93 #include <mach/memory_object_control_server.h>
94 #include <mach/memory_object_default_server.h>
95 #include <mach/processor_server.h>
96 #include <mach/processor_set_server.h>
97 #include <mach/task_server.h>
98 #include <mach/mach_voucher_server.h>
99 #include <mach/mach_voucher_attr_control_server.h>
100 #ifdef VM32_SUPPORT
101 #include <mach/vm32_map_server.h>
102 #endif
103 #include <mach/thread_act_server.h>
104 #include <mach/restartable_server.h>
105
106 #include <mach/exc_server.h>
107 #include <mach/mach_exc_server.h>
108 #include <mach/mach_eventlink_server.h>
109
110 #include <device/device_types.h>
111 #include <device/device_server.h>
112
113 #include <UserNotification/UNDReplyServer.h>
114
115 #if CONFIG_ARCADE
116 #include <mach/arcade_register_server.h>
117 #endif
118
119 #if CONFIG_AUDIT
120 #include <kern/audit_sessionport.h>
121 #endif
122
123 #if MACH_MACHINE_ROUTINES
124 #include <machine/machine_routines.h>
125 #endif /* MACH_MACHINE_ROUTINES */
126 #if XK_PROXY
127 #include <uk_xkern/xk_uproxy_server.h>
128 #endif /* XK_PROXY */
129
130 #include <kern/ipc_tt.h>
131 #include <kern/ipc_mig.h>
132 #include <kern/ipc_misc.h>
133 #include <kern/ipc_kobject.h>
134 #include <kern/host_notify.h>
135 #include <kern/mk_timer.h>
136 #include <kern/misc_protos.h>
137
138 #if CONFIG_ARCADE
139 #include <kern/arcade.h>
140 #endif /* CONFIG_ARCADE */
141
142 #include <ipc/ipc_kmsg.h>
143 #include <ipc/ipc_port.h>
144 #include <ipc/ipc_voucher.h>
145 #include <kern/sync_sema.h>
146 #include <kern/counters.h>
147 #include <kern/work_interval.h>
148 #include <kern/suid_cred.h>
149
150 #if HYPERVISOR
151 #include <kern/hv_support.h>
152 #endif
153
154 #include <vm/vm_protos.h>
155
156 #include <security/mac_mach_internal.h>
157
158 extern char *proc_name_address(void *p);
159 struct proc;
160 extern int proc_pid(struct proc *p);
161
162 /*
163 * Routine: ipc_kobject_notify
164 * Purpose:
165 * Deliver notifications to kobjects that care about them.
166 */
167 boolean_t
168 ipc_kobject_notify(
169 mach_msg_header_t *request_header,
170 mach_msg_header_t *reply_header);
171
172 typedef struct {
173 mach_msg_id_t num;
174 mig_routine_t routine;
175 int size;
176 int kobjidx;
177 #if MACH_COUNTERS
178 mach_counter_t callcount;
179 #endif
180 } mig_hash_t;
181
182 #define MAX_MIG_ENTRIES 1031
183 #define MIG_HASH(x) (x)
184
185 #define KOBJ_IDX_NOT_SET (-1)
186
187 #ifndef max
188 #define max(a, b) (((a) > (b)) ? (a) : (b))
189 #endif /* max */
190
191 static SECURITY_READ_ONLY_LATE(mig_hash_t) mig_buckets[MAX_MIG_ENTRIES];
192 static SECURITY_READ_ONLY_LATE(int) mig_table_max_displ;
193 SECURITY_READ_ONLY_LATE(int) mach_kobj_count; /* count of total number of kobjects */
194
195 static ZONE_DECLARE(ipc_kobject_label_zone, "ipc kobject labels",
196 sizeof(struct ipc_kobject_label), ZC_NONE);
197
198 __startup_data
199 static const struct mig_subsystem *mig_e[] = {
200 (const struct mig_subsystem *)&mach_vm_subsystem,
201 (const struct mig_subsystem *)&mach_port_subsystem,
202 (const struct mig_subsystem *)&mach_host_subsystem,
203 (const struct mig_subsystem *)&host_priv_subsystem,
204 (const struct mig_subsystem *)&host_security_subsystem,
205 (const struct mig_subsystem *)&clock_subsystem,
206 (const struct mig_subsystem *)&clock_priv_subsystem,
207 (const struct mig_subsystem *)&processor_subsystem,
208 (const struct mig_subsystem *)&processor_set_subsystem,
209 (const struct mig_subsystem *)&is_iokit_subsystem,
210 (const struct mig_subsystem *)&lock_set_subsystem,
211 (const struct mig_subsystem *)&task_subsystem,
212 (const struct mig_subsystem *)&thread_act_subsystem,
213 #ifdef VM32_SUPPORT
214 (const struct mig_subsystem *)&vm32_map_subsystem,
215 #endif
216 (const struct mig_subsystem *)&UNDReply_subsystem,
217 (const struct mig_subsystem *)&mach_voucher_subsystem,
218 (const struct mig_subsystem *)&mach_voucher_attr_control_subsystem,
219 (const struct mig_subsystem *)&memory_entry_subsystem,
220 (const struct mig_subsystem *)&task_restartable_subsystem,
221
222 #if XK_PROXY
223 (const struct mig_subsystem *)&do_uproxy_xk_uproxy_subsystem,
224 #endif /* XK_PROXY */
225 #if MACH_MACHINE_ROUTINES
226 (const struct mig_subsystem *)&MACHINE_SUBSYSTEM,
227 #endif /* MACH_MACHINE_ROUTINES */
228 #if MCMSG && iPSC860
229 (const struct mig_subsystem *)&mcmsg_info_subsystem,
230 #endif /* MCMSG && iPSC860 */
231 (const struct mig_subsystem *)&catch_exc_subsystem,
232 (const struct mig_subsystem *)&catch_mach_exc_subsystem,
233 #if CONFIG_ARCADE
234 (const struct mig_subsystem *)&arcade_register_subsystem,
235 #endif
236 (const struct mig_subsystem *)&mach_eventlink_subsystem,
237 };
238
239 static void
240 mig_init(void)
241 {
242 unsigned int i, n = sizeof(mig_e) / sizeof(const struct mig_subsystem *);
243 int howmany;
244 mach_msg_id_t j, pos, nentry, range;
245
246 for (i = 0; i < n; i++) {
247 range = mig_e[i]->end - mig_e[i]->start;
248 if (!mig_e[i]->start || range < 0) {
249 panic("the msgh_ids in mig_e[] aren't valid!");
250 }
251
252 for (j = 0; j < range; j++) {
253 if (mig_e[i]->routine[j].stub_routine) {
254 /* Only put real entries in the table */
255 nentry = j + mig_e[i]->start;
256 for (pos = MIG_HASH(nentry) % MAX_MIG_ENTRIES, howmany = 1;
257 mig_buckets[pos].num;
258 pos++, pos = pos % MAX_MIG_ENTRIES, howmany++) {
259 if (mig_buckets[pos].num == nentry) {
260 printf("message id = %d\n", nentry);
261 panic("multiple entries with the same msgh_id");
262 }
263 if (howmany == MAX_MIG_ENTRIES) {
264 panic("the mig dispatch table is too small");
265 }
266 }
267
268 mig_buckets[pos].num = nentry;
269 mig_buckets[pos].routine = mig_e[i]->routine[j].stub_routine;
270 if (mig_e[i]->routine[j].max_reply_msg) {
271 mig_buckets[pos].size = mig_e[i]->routine[j].max_reply_msg;
272 } else {
273 mig_buckets[pos].size = mig_e[i]->maxsize;
274 }
275 mig_buckets[pos].kobjidx = KOBJ_IDX_NOT_SET;
276
277 mig_table_max_displ = max(howmany, mig_table_max_displ);
278 mach_kobj_count++;
279 }
280 }
281 }
282 printf("mig_table_max_displ = %d mach_kobj_count = %d\n",
283 mig_table_max_displ, mach_kobj_count);
284 }
285 STARTUP(MACH_IPC, STARTUP_RANK_FIRST, mig_init);
286
287 /*
288 * Do a hash table lookup for given msgh_id. Return 0
289 * if not found.
290 */
291 static mig_hash_t *
292 find_mig_hash_entry(int msgh_id)
293 {
294 unsigned int i = (unsigned int)MIG_HASH(msgh_id);
295 int max_iter = mig_table_max_displ;
296 mig_hash_t *ptr;
297
298 do {
299 ptr = &mig_buckets[i++ % MAX_MIG_ENTRIES];
300 } while (msgh_id != ptr->num && ptr->num && --max_iter);
301
302 if (!ptr->routine || msgh_id != ptr->num) {
303 ptr = (mig_hash_t *)0;
304 } else {
305 #if MACH_COUNTERS
306 ptr->callcount++;
307 #endif
308 }
309
310 return ptr;
311 }
312
313 /*
314 * Routine: ipc_kobject_set_kobjidx
315 * Purpose:
316 * Set the index for the kobject filter
317 * mask for a given message ID.
318 */
319 kern_return_t
320 ipc_kobject_set_kobjidx(
321 int msgh_id,
322 int index)
323 {
324 mig_hash_t *ptr = find_mig_hash_entry(msgh_id);
325
326 if (ptr == (mig_hash_t *)0) {
327 return KERN_INVALID_ARGUMENT;
328 }
329
330 assert(index < mach_kobj_count);
331 ptr->kobjidx = index;
332
333 return KERN_SUCCESS;
334 }
335
336 /*
337 * Routine: ipc_kobject_server
338 * Purpose:
339 * Handle a message sent to the kernel.
340 * Generates a reply message.
341 * Version for Untyped IPC.
342 * Conditions:
343 * Nothing locked.
344 */
345
346 ipc_kmsg_t
347 ipc_kobject_server(
348 ipc_kmsg_t request,
349 mach_msg_option_t __unused option)
350 {
351 mach_msg_size_t reply_size;
352 ipc_kmsg_t reply;
353 kern_return_t kr;
354 ipc_port_t replyp = IPC_PORT_NULL;
355 mach_msg_max_trailer_t *trailer;
356 mig_hash_t *ptr;
357 task_t task = TASK_NULL;
358 uint32_t exec_token;
359 boolean_t exec_token_changed = FALSE;
360 int request_msgh_id = request->ikm_header->msgh_id;
361 natural_t ikot;
362 ipc_port_t port;
363
364 reply = NULL;
365 port = request->ikm_header->msgh_remote_port;
366 if (IP_VALID(port)) {
367 ikot = ip_kotype(port);
368 } else {
369 ikot = IKOT_UNKNOWN;
370 }
371 if (ikot == IKOT_UEXT_OBJECT) {
372 kr = uext_server(request, &reply);
373 if ((MIG_NO_REPLY == kr) || (KERN_SUCCESS == kr)) {
374 ipc_kmsg_trace_send(request, option);
375 goto msgdone;
376 }
377 }
378
379 /* Find corresponding mig_hash entry, if any */
380 ptr = find_mig_hash_entry(request_msgh_id);
381
382 /* Get the reply_size. */
383 if (ptr == (mig_hash_t *)0) {
384 reply_size = sizeof(mig_reply_error_t);
385 } else {
386 reply_size = ptr->size;
387 }
388
389 /* round up for trailer size */
390 reply_size += MAX_TRAILER_SIZE;
391 reply = ipc_kmsg_alloc(reply_size);
392
393 if (reply == IKM_NULL) {
394 printf("ipc_kobject_server: dropping request\n");
395 ipc_kmsg_trace_send(request, option);
396 ipc_kmsg_destroy(request);
397 return IKM_NULL;
398 }
399
400 /*
401 * Initialize reply message.
402 */
403 {
404 #define InP ((mach_msg_header_t *) request->ikm_header)
405 #define OutP ((mig_reply_error_t *) reply->ikm_header)
406
407 /*
408 * MIG should really assure no data leakage -
409 * but until it does, pessimistically zero the
410 * whole reply buffer.
411 */
412 bzero((void *)OutP, reply_size);
413
414 OutP->NDR = NDR_record;
415 OutP->Head.msgh_size = sizeof(mig_reply_error_t);
416
417 OutP->Head.msgh_bits =
418 MACH_MSGH_BITS_SET(MACH_MSGH_BITS_LOCAL(InP->msgh_bits), 0, 0, 0);
419 OutP->Head.msgh_remote_port = InP->msgh_local_port;
420 OutP->Head.msgh_local_port = MACH_PORT_NULL;
421 OutP->Head.msgh_voucher_port = MACH_PORT_NULL;
422 OutP->Head.msgh_id = InP->msgh_id + 100;
423
424 #undef InP
425 #undef OutP
426 }
427
428 /*
429 * Find the routine to call, and call it
430 * to perform the kernel function
431 */
432 ipc_kmsg_trace_send(request, option);
433 {
434 if (ptr) {
435 /*
436 * Check if the port is a task port, if its a task port then
437 * snapshot the task exec token before the mig routine call.
438 */
439 if (ikot == IKOT_TASK_CONTROL) {
440 task = convert_port_to_task_with_exec_token(port, &exec_token, TRUE);
441 }
442
443 #if CONFIG_MACF
444 int idx = ptr->kobjidx;
445 task_t curtask = current_task();
446 uint8_t *filter_mask = curtask->mach_kobj_filter_mask;
447
448 /* Check kobject mig filter mask, if exists. */
449 if (__improbable(filter_mask != NULL && idx != KOBJ_IDX_NOT_SET &&
450 !bitstr_test(filter_mask, idx))) {
451 /* Not in filter mask, evaluate policy. */
452 if (mac_task_kobj_msg_evaluate != NULL) {
453 kr = mac_task_kobj_msg_evaluate(get_bsdtask_info(curtask),
454 request_msgh_id, idx);
455 if (kr != KERN_SUCCESS) {
456 ((mig_reply_error_t *) reply->ikm_header)->RetCode = kr;
457 goto skip_kobjcall;
458 }
459 }
460 }
461 #endif /* CONFIG_MACF */
462
463 (*ptr->routine)(request->ikm_header, reply->ikm_header);
464
465 #if CONFIG_MACF
466 skip_kobjcall:
467 #endif
468
469 /* Check if the exec token changed during the mig routine */
470 if (task != TASK_NULL) {
471 if (exec_token != task->exec_token) {
472 exec_token_changed = TRUE;
473 }
474 task_deallocate(task);
475 }
476
477 kernel_task->messages_received++;
478 } else {
479 if (!ipc_kobject_notify(request->ikm_header, reply->ikm_header)) {
480 #if DEVELOPMENT || DEBUG
481 printf("ipc_kobject_server: bogus kernel message, id=%d\n",
482 request->ikm_header->msgh_id);
483 #endif /* DEVELOPMENT || DEBUG */
484 _MIG_MSGID_INVALID(request->ikm_header->msgh_id);
485
486 ((mig_reply_error_t *) reply->ikm_header)->RetCode
487 = MIG_BAD_ID;
488 } else {
489 kernel_task->messages_received++;
490 }
491 }
492 kernel_task->messages_sent++;
493 }
494
495 if (!(reply->ikm_header->msgh_bits & MACH_MSGH_BITS_COMPLEX) &&
496 ((mig_reply_error_t *) reply->ikm_header)->RetCode != KERN_SUCCESS) {
497 kr = ((mig_reply_error_t *) reply->ikm_header)->RetCode;
498 } else {
499 kr = KERN_SUCCESS;
500 }
501
502 msgdone:
503 /*
504 * Destroy destination. The following code differs from
505 * ipc_object_destroy in that we release the send-once
506 * right instead of generating a send-once notification
507 * (which would bring us here again, creating a loop).
508 * It also differs in that we only expect send or
509 * send-once rights, never receive rights.
510 *
511 * We set msgh_remote_port to IP_NULL so that the kmsg
512 * destroy routines don't try to destroy the port twice.
513 */
514 switch (MACH_MSGH_BITS_REMOTE(request->ikm_header->msgh_bits)) {
515 case MACH_MSG_TYPE_PORT_SEND:
516 ipc_port_release_send(request->ikm_header->msgh_remote_port);
517 request->ikm_header->msgh_remote_port = IP_NULL;
518 break;
519
520 case MACH_MSG_TYPE_PORT_SEND_ONCE:
521 ipc_port_release_sonce(request->ikm_header->msgh_remote_port);
522 request->ikm_header->msgh_remote_port = IP_NULL;
523 break;
524
525 default:
526 panic("ipc_kobject_server: strange destination rights");
527 }
528
529 /*
530 * Destroy voucher. The kernel MIG servers never take ownership
531 * of vouchers sent in messages. Swallow any such rights here.
532 */
533 if (IP_VALID(request->ikm_voucher)) {
534 assert(MACH_MSG_TYPE_PORT_SEND ==
535 MACH_MSGH_BITS_VOUCHER(request->ikm_header->msgh_bits));
536 ipc_port_release_send(request->ikm_voucher);
537 request->ikm_voucher = IP_NULL;
538 }
539
540 if ((kr == KERN_SUCCESS) || (kr == MIG_NO_REPLY)) {
541 /*
542 * The server function is responsible for the contents
543 * of the message. The reply port right is moved
544 * to the reply message, and we have deallocated
545 * the destination port right, so we just need
546 * to free the kmsg.
547 */
548 ipc_kmsg_free(request);
549 } else {
550 /*
551 * The message contents of the request are intact.
552 * Destroy everthing except the reply port right,
553 * which is needed in the reply message.
554 */
555 request->ikm_header->msgh_local_port = MACH_PORT_NULL;
556 ipc_kmsg_destroy(request);
557 }
558
559 if (kr == MIG_NO_REPLY) {
560 /*
561 * The server function will send a reply message
562 * using the reply port right, which it has saved.
563 */
564
565 if (reply) {
566 ipc_kmsg_free(reply);
567 }
568 return IKM_NULL;
569 }
570
571 if (reply) {
572 replyp = reply->ikm_header->msgh_remote_port;
573 }
574
575 if (!IP_VALID(replyp)) {
576 /*
577 * Can't queue the reply message if the destination
578 * (the reply port) isn't valid.
579 */
580
581 ipc_kmsg_destroy(reply);
582
583 return IKM_NULL;
584 } else if (replyp->ip_receiver == ipc_space_kernel) {
585 /*
586 * Don't send replies to kobject kernel ports
587 */
588 #if DEVELOPMENT || DEBUG
589 printf("%s: refusing to send reply to kobject %d port (id:%d)\n",
590 __func__, ip_kotype(replyp), request_msgh_id);
591 #endif /* DEVELOPMENT || DEBUG */
592 ipc_kmsg_destroy(reply);
593 return IKM_NULL;
594 }
595
596 /* Fail the MIG call if the task exec token changed during the call */
597 if (kr == KERN_SUCCESS && exec_token_changed) {
598 /*
599 * Create a new reply msg with error and destroy the old reply msg.
600 */
601 ipc_kmsg_t new_reply = ipc_kmsg_alloc(reply_size);
602
603 if (new_reply == IKM_NULL) {
604 printf("ipc_kobject_server: dropping request\n");
605 ipc_kmsg_destroy(reply);
606 return IKM_NULL;
607 }
608 /*
609 * Initialize the new reply message.
610 */
611 {
612 #define OutP_new ((mig_reply_error_t *) new_reply->ikm_header)
613 #define OutP_old ((mig_reply_error_t *) reply->ikm_header)
614
615 bzero((void *)OutP_new, reply_size);
616
617 OutP_new->NDR = OutP_old->NDR;
618 OutP_new->Head.msgh_size = sizeof(mig_reply_error_t);
619 OutP_new->Head.msgh_bits = OutP_old->Head.msgh_bits & ~MACH_MSGH_BITS_COMPLEX;
620 OutP_new->Head.msgh_remote_port = OutP_old->Head.msgh_remote_port;
621 OutP_new->Head.msgh_local_port = MACH_PORT_NULL;
622 OutP_new->Head.msgh_voucher_port = MACH_PORT_NULL;
623 OutP_new->Head.msgh_id = OutP_old->Head.msgh_id;
624
625 /* Set the error as KERN_INVALID_TASK */
626 OutP_new->RetCode = KERN_INVALID_TASK;
627
628 #undef OutP_new
629 #undef OutP_old
630 }
631
632 /*
633 * Destroy everything in reply except the reply port right,
634 * which is needed in the new reply message.
635 */
636 reply->ikm_header->msgh_remote_port = MACH_PORT_NULL;
637 ipc_kmsg_destroy(reply);
638
639 reply = new_reply;
640 }
641
642 trailer = (mach_msg_max_trailer_t *)
643 ((vm_offset_t)reply->ikm_header + (int)reply->ikm_header->msgh_size);
644 bzero(trailer, sizeof(*trailer));
645 trailer->msgh_sender = KERNEL_SECURITY_TOKEN;
646 trailer->msgh_audit = KERNEL_AUDIT_TOKEN;
647 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
648 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
649
650 return reply;
651 }
652
653 /*
654 * Routine: ipc_kobject_set
655 * Purpose:
656 * Make a port represent a kernel object of the given type.
657 * The caller is responsible for handling refs for the
658 * kernel object, if necessary.
659 * Conditions:
660 * Nothing locked. The port must be active if setting
661 * a kobject linkage. Clearing a linkage is OK on an
662 * inactive port.
663 */
664 void
665 ipc_kobject_set(
666 ipc_port_t port,
667 ipc_kobject_t kobject,
668 ipc_kobject_type_t type)
669 {
670 ip_lock(port);
671 ipc_kobject_set_atomically(port, kobject, type);
672 ip_unlock(port);
673 }
674
675 void
676 ipc_kobject_set_atomically(
677 ipc_port_t port,
678 ipc_kobject_t kobject,
679 ipc_kobject_type_t type)
680 {
681 assert(type == IKOT_NONE || ip_active(port));
682 #if MACH_ASSERT
683 port->ip_spares[2] = (port->ip_object.io_bits & IO_BITS_KOTYPE);
684 #endif /* MACH_ASSERT */
685 port->ip_object.io_bits = (port->ip_object.io_bits & ~IO_BITS_KOTYPE) | type;
686 if (ip_is_kolabeled(port)) {
687 ipc_kobject_label_t labelp = port->ip_kolabel;
688 labelp->ikol_kobject = kobject;
689 } else {
690 port->ip_kobject = kobject;
691 }
692 if (type != IKOT_NONE) {
693 /* Once set, this bit can never be unset */
694 port->ip_object.io_bits |= IO_BITS_KOBJECT;
695 }
696 }
697
698 /*
699 * Routine: ipc_kobject_init_port
700 * Purpose:
701 * Initialize a kobject port with the given types and options.
702 *
703 * This function never fails.
704 */
705 static inline void
706 ipc_kobject_init_port(
707 ipc_port_t port,
708 ipc_kobject_t kobject,
709 ipc_kobject_type_t type,
710 ipc_kobject_alloc_options_t options)
711 {
712 ipc_kobject_set_atomically(port, kobject, type);
713
714 if (options & IPC_KOBJECT_ALLOC_MAKE_SEND) {
715 ipc_port_make_send_locked(port);
716 }
717 if (options & IPC_KOBJECT_ALLOC_NSREQUEST) {
718 ipc_port_make_sonce_locked(port);
719 port->ip_nsrequest = port;
720 }
721 if (options & IPC_KOBJECT_ALLOC_NO_GRANT) {
722 port->ip_no_grant = 1;
723 }
724 if (options & IPC_KOBJECT_ALLOC_IMMOVABLE_SEND) {
725 port->ip_immovable_send = 1;
726 }
727 }
728
729 /*
730 * Routine: ipc_kobject_alloc_port
731 * Purpose:
732 * Allocate a kobject port in the kernel space of the specified type.
733 *
734 * This function never fails.
735 *
736 * Conditions:
737 * No locks held (memory is allocated)
738 */
739 ipc_port_t
740 ipc_kobject_alloc_port(
741 ipc_kobject_t kobject,
742 ipc_kobject_type_t type,
743 ipc_kobject_alloc_options_t options)
744 {
745 ipc_port_t port = ipc_port_alloc_kernel();
746
747 if (port == IP_NULL) {
748 panic("ipc_kobject_alloc_port(): failed to allocate port");
749 }
750
751 ipc_kobject_init_port(port, kobject, type, options);
752 return port;
753 }
754
755 /*
756 * Routine: ipc_kobject_alloc_labeled_port
757 * Purpose:
758 * Allocate a kobject port and associated mandatory access label
759 * in the kernel space of the specified type.
760 *
761 * This function never fails.
762 *
763 * Conditions:
764 * No locks held (memory is allocated)
765 */
766
767 ipc_port_t
768 ipc_kobject_alloc_labeled_port(
769 ipc_kobject_t kobject,
770 ipc_kobject_type_t type,
771 ipc_label_t label,
772 ipc_kobject_alloc_options_t options)
773 {
774 ipc_port_t port;
775 ipc_kobject_label_t labelp;
776
777 port = ipc_port_alloc_kernel();
778 if (port == IP_NULL) {
779 panic("ipc_kobject_alloc_port(): failed to allocate port");
780 }
781
782 labelp = (ipc_kobject_label_t)zalloc(ipc_kobject_label_zone);
783 if (labelp == NULL) {
784 panic("ipc_kobject_alloc_labeled_port(): failed to allocate label");
785 }
786 labelp->ikol_label = label;
787 port->ip_kolabel = labelp;
788 port->ip_object.io_bits |= IO_BITS_KOLABEL;
789
790 ipc_kobject_init_port(port, kobject, type, options);
791 return port;
792 }
793
794 /*
795 * Routine: ipc_kobject_make_send_lazy_alloc_port
796 * Purpose:
797 * Make a send once for a kobject port.
798 *
799 * A location owning this port is passed in port_store.
800 * If no port exists, a port is made lazily.
801 *
802 * A send right is made for the port, and if this is the first one
803 * (possibly not for the first time), then the no-more-senders
804 * notification is rearmed.
805 *
806 * When a notification is armed, the kobject must donate
807 * one of its references to the port. It is expected
808 * the no-more-senders notification will consume this reference.
809 *
810 * Returns:
811 * TRUE if a notification was armed
812 * FALSE else
813 *
814 * Conditions:
815 * Nothing is locked, memory can be allocated.
816 * The caller must be able to donate a kobject reference to the port.
817 */
818 boolean_t
819 ipc_kobject_make_send_lazy_alloc_port(
820 ipc_port_t *port_store,
821 ipc_kobject_t kobject,
822 ipc_kobject_type_t type,
823 boolean_t __ptrauth_only should_ptrauth,
824 uint64_t __ptrauth_only ptrauth_discriminator)
825 {
826 ipc_port_t port, previous, __ptrauth_only port_addr;
827 boolean_t rc = FALSE;
828
829 port = os_atomic_load(port_store, dependency);
830
831 #if __has_feature(ptrauth_calls)
832 /* If we're on a ptrauth system and this port is signed, authenticate and strip the pointer */
833 if (should_ptrauth && IP_VALID(port)) {
834 port = ptrauth_auth_data(port,
835 ptrauth_key_process_independent_data,
836 ptrauth_blend_discriminator(port_store, ptrauth_discriminator));
837 }
838 #endif // __has_feature(ptrauth_calls)
839
840 if (!IP_VALID(port)) {
841 port = ipc_kobject_alloc_port(kobject, type,
842 IPC_KOBJECT_ALLOC_MAKE_SEND | IPC_KOBJECT_ALLOC_NSREQUEST);
843
844 #if __has_feature(ptrauth_calls)
845 if (should_ptrauth) {
846 port_addr = ptrauth_sign_unauthenticated(port,
847 ptrauth_key_process_independent_data,
848 ptrauth_blend_discriminator(port_store, ptrauth_discriminator));
849 } else {
850 port_addr = port;
851 }
852 #else
853 port_addr = port;
854 #endif // __has_feature(ptrauth_calls)
855
856 if (os_atomic_cmpxchgv(port_store, IP_NULL, port_addr, &previous, release)) {
857 return TRUE;
858 }
859
860 // undo what ipc_kobject_alloc_port() did above
861 port->ip_nsrequest = IP_NULL;
862 port->ip_mscount = 0;
863 port->ip_sorights = 0;
864 port->ip_srights = 0;
865 ip_release(port);
866 ip_release(port);
867 ipc_port_dealloc_kernel(port);
868
869 port = previous;
870 }
871
872 ip_lock(port);
873 ipc_port_make_send_locked(port);
874 if (port->ip_srights == 1) {
875 ipc_port_make_sonce_locked(port);
876 assert(port->ip_nsrequest == IP_NULL);
877 port->ip_nsrequest = port;
878 rc = TRUE;
879 }
880 ip_unlock(port);
881
882 return rc;
883 }
884
885 /*
886 * Routine: ipc_kobject_make_send_lazy_alloc_labeled_port
887 * Purpose:
888 * Make a send once for a kobject port.
889 *
890 * A location owning this port is passed in port_store.
891 * If no port exists, a port is made lazily.
892 *
893 * A send right is made for the port, and if this is the first one
894 * (possibly not for the first time), then the no-more-senders
895 * notification is rearmed.
896 *
897 * When a notification is armed, the kobject must donate
898 * one of its references to the port. It is expected
899 * the no-more-senders notification will consume this reference.
900 *
901 * Returns:
902 * TRUE if a notification was armed
903 * FALSE else
904 *
905 * Conditions:
906 * Nothing is locked, memory can be allocated.
907 * The caller must be able to donate a kobject reference to the port.
908 */
909 boolean_t
910 ipc_kobject_make_send_lazy_alloc_labeled_port(
911 ipc_port_t *port_store,
912 ipc_kobject_t kobject,
913 ipc_kobject_type_t type,
914 ipc_label_t label)
915 {
916 ipc_port_t port, previous;
917 boolean_t rc = FALSE;
918
919 port = os_atomic_load(port_store, dependency);
920
921 if (!IP_VALID(port)) {
922 port = ipc_kobject_alloc_labeled_port(kobject, type, label,
923 IPC_KOBJECT_ALLOC_MAKE_SEND | IPC_KOBJECT_ALLOC_NSREQUEST);
924 if (os_atomic_cmpxchgv(port_store, IP_NULL, port, &previous, release)) {
925 return TRUE;
926 }
927
928 // undo what ipc_kobject_alloc_port() did above
929 port->ip_nsrequest = IP_NULL;
930 port->ip_mscount = 0;
931 port->ip_sorights = 0;
932 port->ip_srights = 0;
933 ip_release(port);
934 ip_release(port);
935 zfree(ipc_kobject_label_zone, port->ip_kolabel);
936 port->ip_object.io_bits &= ~IO_BITS_KOLABEL;
937 port->ip_kolabel = NULL;
938 ipc_port_dealloc_kernel(port);
939
940 port = previous;
941 assert(ip_is_kolabeled(port));
942 }
943
944 ip_lock(port);
945 ipc_port_make_send_locked(port);
946 if (port->ip_srights == 1) {
947 ipc_port_make_sonce_locked(port);
948 assert(port->ip_nsrequest == IP_NULL);
949 port->ip_nsrequest = port;
950 rc = TRUE;
951 }
952 ip_unlock(port);
953
954 return rc;
955 }
956
957
958 /*
959 * Routine: ipc_kobject_destroy
960 * Purpose:
961 * Release any kernel object resources associated
962 * with the port, which is being destroyed.
963 *
964 * This path to free object resources should only be
965 * needed when resources are associated with a user's port.
966 * In the normal case, when the kernel is the receiver,
967 * the code calling ipc_port_dealloc_kernel should clean
968 * up the object resources.
969 *
970 * Cleans up any kobject label that might be present.
971 * Conditions:
972 * The port is not locked, but it is dead.
973 */
974
975 void
976 ipc_kobject_destroy(
977 ipc_port_t port)
978 {
979 switch (ip_kotype(port)) {
980 case IKOT_TIMER:
981 mk_timer_port_destroy(port);
982 break;
983
984 case IKOT_NAMED_ENTRY:
985 mach_destroy_memory_entry(port);
986 break;
987
988 case IKOT_HOST_NOTIFY:
989 host_notify_port_destroy(port);
990 break;
991
992 case IKOT_SUID_CRED:
993 suid_cred_destroy(port);
994 break;
995
996 default:
997 break;
998 }
999
1000 if (ip_is_kolabeled(port)) {
1001 ipc_kobject_label_t labelp = port->ip_kolabel;
1002
1003 assert(labelp != NULL);
1004 assert(ip_is_kobject(port));
1005 port->ip_kolabel = NULL;
1006 port->ip_object.io_bits &= ~IO_BITS_KOLABEL;
1007 zfree(ipc_kobject_label_zone, labelp);
1008 }
1009 }
1010
1011 /*
1012 * Routine: ipc_kobject_label_check
1013 * Purpose:
1014 * Check to see if the space is allowed to possess a
1015 * right for the given port. In order to qualify, the
1016 * space label must contain all the privileges listed
1017 * in the port/kobject label.
1018 *
1019 * Conditions:
1020 * Space is write locked and active.
1021 * Port is locked and active.
1022 */
1023 boolean_t
1024 ipc_kobject_label_check(
1025 ipc_space_t space,
1026 ipc_port_t port,
1027 __unused mach_msg_type_name_t msgt_name)
1028 {
1029 ipc_kobject_label_t labelp;
1030
1031 assert(is_active(space));
1032 assert(ip_active(port));
1033
1034 /* Unlabled ports/kobjects are always allowed */
1035 if (!ip_is_kolabeled(port)) {
1036 return TRUE;
1037 }
1038
1039 /* Never OK to copyout the receive right for a labeled kobject */
1040 if (msgt_name == MACH_MSG_TYPE_PORT_RECEIVE) {
1041 panic("ipc_kobject_label_check: attempted receive right copyout for labeled kobject");
1042 }
1043
1044 labelp = port->ip_kolabel;
1045 return (labelp->ikol_label & space->is_label) == labelp->ikol_label;
1046 }
1047
1048 boolean_t
1049 ipc_kobject_notify(
1050 mach_msg_header_t *request_header,
1051 mach_msg_header_t *reply_header)
1052 {
1053 mach_msg_max_trailer_t * trailer;
1054 ipc_port_t port = request_header->msgh_remote_port;
1055
1056 ((mig_reply_error_t *) reply_header)->RetCode = MIG_NO_REPLY;
1057
1058 trailer = (mach_msg_max_trailer_t *)
1059 ((vm_offset_t)request_header + request_header->msgh_size);
1060
1061 /*
1062 * The kobject notification is privileged and can change the
1063 * refcount on kernel-internal objects - make sure
1064 * that the message wasn't faked!
1065 */
1066 if (0 != bcmp(&trailer->msgh_audit, &KERNEL_AUDIT_TOKEN,
1067 sizeof(trailer->msgh_audit))) {
1068 return FALSE;
1069 }
1070 if (0 != bcmp(&trailer->msgh_sender, &KERNEL_SECURITY_TOKEN,
1071 sizeof(trailer->msgh_sender))) {
1072 return FALSE;
1073 }
1074
1075 switch (request_header->msgh_id) {
1076 case MACH_NOTIFY_NO_SENDERS:
1077 switch (ip_kotype(port)) {
1078 case IKOT_VOUCHER:
1079 ipc_voucher_notify(request_header);
1080 return TRUE;
1081
1082 case IKOT_VOUCHER_ATTR_CONTROL:
1083 ipc_voucher_attr_control_notify(request_header);
1084 return TRUE;
1085
1086 case IKOT_SEMAPHORE:
1087 semaphore_notify(request_header);
1088 return TRUE;
1089
1090 case IKOT_EVENTLINK:
1091 ipc_eventlink_notify(request_header);
1092 return TRUE;
1093
1094 case IKOT_TASK_CONTROL:
1095 task_port_notify(request_header);
1096 return TRUE;
1097
1098 case IKOT_NAMED_ENTRY:
1099 ip_lock(port);
1100
1101 /*
1102 * Bring the sequence number and mscount in
1103 * line with ipc_port_destroy assertion.
1104 */
1105 port->ip_mscount = 0;
1106 port->ip_messages.imq_seqno = 0;
1107 ipc_port_destroy(port); /* releases lock */
1108 return TRUE;
1109
1110 case IKOT_UPL:
1111 upl_no_senders(
1112 request_header->msgh_remote_port,
1113 (mach_port_mscount_t)
1114 ((mach_no_senders_notification_t *)
1115 request_header)->not_count);
1116 reply_header->msgh_remote_port = MACH_PORT_NULL;
1117 return TRUE;
1118
1119 #if CONFIG_AUDIT
1120 case IKOT_AU_SESSIONPORT:
1121 audit_session_nosenders(request_header);
1122 return TRUE;
1123 #endif
1124 case IKOT_FILEPORT:
1125 fileport_notify(request_header);
1126 return TRUE;
1127
1128 case IKOT_WORK_INTERVAL:
1129 work_interval_port_notify(request_header);
1130 return TRUE;
1131 case IKOT_TASK_READ:
1132 case IKOT_TASK_INSPECT:
1133 task_port_with_flavor_notify(request_header);
1134 return TRUE;
1135 case IKOT_THREAD_READ:
1136 case IKOT_THREAD_INSPECT:
1137 thread_port_with_flavor_notify(request_header);
1138 return TRUE;
1139 case IKOT_SUID_CRED:
1140 suid_cred_notify(request_header);
1141 return TRUE;
1142 #if HYPERVISOR
1143 case IKOT_HYPERVISOR:
1144 hv_port_notify(request_header);
1145 return TRUE;
1146 #endif
1147 }
1148
1149 break;
1150
1151 case MACH_NOTIFY_PORT_DELETED:
1152 case MACH_NOTIFY_PORT_DESTROYED:
1153 case MACH_NOTIFY_SEND_ONCE:
1154 case MACH_NOTIFY_DEAD_NAME:
1155 break;
1156
1157 default:
1158 return FALSE;
1159 }
1160 switch (ip_kotype(port)) {
1161 #ifdef IOKIT
1162 case IKOT_IOKIT_OBJECT:
1163 case IKOT_IOKIT_CONNECT:
1164 case IKOT_IOKIT_IDENT:
1165 case IKOT_UEXT_OBJECT:
1166 {
1167 return iokit_notify(request_header);
1168 }
1169 #endif
1170 case IKOT_TASK_RESUME:
1171 {
1172 return task_suspension_notify(request_header);
1173 }
1174
1175 default:
1176 return FALSE;
1177 }
1178 }