2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
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,
61 * Copyright (c) 2005 SPARTA, Inc.
66 * File: kern/ipc_kobject.c
70 * Functions for letting a port represent a kernel object.
73 #include <mach_debug.h>
74 #include <mach_ipc_test.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>
82 #include <mach/vm_param.h>
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>
101 #include <mach/vm32_map_server.h>
103 #include <mach/thread_act_server.h>
104 #include <mach/restartable_server.h>
106 #include <mach/exc_server.h>
107 #include <mach/mach_exc_server.h>
108 #include <mach/mach_eventlink_server.h>
110 #include <device/device_types.h>
111 #include <device/device_server.h>
113 #include <UserNotification/UNDReplyServer.h>
116 #include <mach/arcade_register_server.h>
120 #include <kern/audit_sessionport.h>
123 #if MACH_MACHINE_ROUTINES
124 #include <machine/machine_routines.h>
125 #endif /* MACH_MACHINE_ROUTINES */
127 #include <uk_xkern/xk_uproxy_server.h>
128 #endif /* XK_PROXY */
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>
139 #include <kern/arcade.h>
140 #endif /* CONFIG_ARCADE */
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>
151 #include <kern/hv_support.h>
154 #include <vm/vm_protos.h>
156 #include <security/mac_mach_internal.h>
158 extern char *proc_name_address(void *p
);
160 extern int proc_pid(struct proc
*p
);
163 * Routine: ipc_kobject_notify
165 * Deliver notifications to kobjects that care about them.
169 mach_msg_header_t
*request_header
,
170 mach_msg_header_t
*reply_header
);
174 mig_routine_t routine
;
178 mach_counter_t callcount
;
182 #define MAX_MIG_ENTRIES 1031
183 #define MIG_HASH(x) (x)
185 #define KOBJ_IDX_NOT_SET (-1)
188 #define max(a, b) (((a) > (b)) ? (a) : (b))
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 */
195 static ZONE_DECLARE(ipc_kobject_label_zone
, "ipc kobject labels",
196 sizeof(struct ipc_kobject_label
), ZC_NONE
);
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
,
214 (const struct mig_subsystem
*)&vm32_map_subsystem
,
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
,
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 */
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
,
234 (const struct mig_subsystem
*)&arcade_register_subsystem
,
236 (const struct mig_subsystem
*)&mach_eventlink_subsystem
,
242 unsigned int i
, n
= sizeof(mig_e
) / sizeof(const struct mig_subsystem
*);
244 mach_msg_id_t j
, pos
, nentry
, range
;
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!");
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");
263 if (howmany
== MAX_MIG_ENTRIES
) {
264 panic("the mig dispatch table is too small");
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
;
273 mig_buckets
[pos
].size
= mig_e
[i
]->maxsize
;
275 mig_buckets
[pos
].kobjidx
= KOBJ_IDX_NOT_SET
;
277 mig_table_max_displ
= max(howmany
, mig_table_max_displ
);
282 printf("mig_table_max_displ = %d mach_kobj_count = %d\n",
283 mig_table_max_displ
, mach_kobj_count
);
285 STARTUP(MACH_IPC
, STARTUP_RANK_FIRST
, mig_init
);
288 * Do a hash table lookup for given msgh_id. Return 0
292 find_mig_hash_entry(int msgh_id
)
294 unsigned int i
= (unsigned int)MIG_HASH(msgh_id
);
295 int max_iter
= mig_table_max_displ
;
299 ptr
= &mig_buckets
[i
++ % MAX_MIG_ENTRIES
];
300 } while (msgh_id
!= ptr
->num
&& ptr
->num
&& --max_iter
);
302 if (!ptr
->routine
|| msgh_id
!= ptr
->num
) {
303 ptr
= (mig_hash_t
*)0;
314 * Routine: ipc_kobject_set_kobjidx
316 * Set the index for the kobject filter
317 * mask for a given message ID.
320 ipc_kobject_set_kobjidx(
324 mig_hash_t
*ptr
= find_mig_hash_entry(msgh_id
);
326 if (ptr
== (mig_hash_t
*)0) {
327 return KERN_INVALID_ARGUMENT
;
330 assert(index
< mach_kobj_count
);
331 ptr
->kobjidx
= index
;
337 * Routine: ipc_kobject_server
339 * Handle a message sent to the kernel.
340 * Generates a reply message.
341 * Version for Untyped IPC.
349 mach_msg_option_t __unused option
)
351 mach_msg_size_t reply_size
;
354 ipc_port_t replyp
= IPC_PORT_NULL
;
355 mach_msg_max_trailer_t
*trailer
;
357 task_t task
= TASK_NULL
;
359 boolean_t exec_token_changed
= FALSE
;
360 int request_msgh_id
= request
->ikm_header
->msgh_id
;
365 port
= request
->ikm_header
->msgh_remote_port
;
366 if (IP_VALID(port
)) {
367 ikot
= ip_kotype(port
);
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
);
379 /* Find corresponding mig_hash entry, if any */
380 ptr
= find_mig_hash_entry(request_msgh_id
);
382 /* Get the reply_size. */
383 if (ptr
== (mig_hash_t
*)0) {
384 reply_size
= sizeof(mig_reply_error_t
);
386 reply_size
= ptr
->size
;
389 /* round up for trailer size */
390 reply_size
+= MAX_TRAILER_SIZE
;
391 reply
= ipc_kmsg_alloc(reply_size
);
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
);
401 * Initialize reply message.
404 #define InP ((mach_msg_header_t *) request->ikm_header)
405 #define OutP ((mig_reply_error_t *) reply->ikm_header)
408 * MIG should really assure no data leakage -
409 * but until it does, pessimistically zero the
410 * whole reply buffer.
412 bzero((void *)OutP
, reply_size
);
414 OutP
->NDR
= NDR_record
;
415 OutP
->Head
.msgh_size
= sizeof(mig_reply_error_t
);
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;
429 * Find the routine to call, and call it
430 * to perform the kernel function
432 ipc_kmsg_trace_send(request
, option
);
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.
439 if (ikot
== IKOT_TASK_CONTROL
) {
440 task
= convert_port_to_task_with_exec_token(port
, &exec_token
, TRUE
);
444 int idx
= ptr
->kobjidx
;
445 task_t curtask
= current_task();
446 uint8_t *filter_mask
= curtask
->mach_kobj_filter_mask
;
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
;
461 #endif /* CONFIG_MACF */
463 (*ptr
->routine
)(request
->ikm_header
, reply
->ikm_header
);
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
;
474 task_deallocate(task
);
477 kernel_task
->messages_received
++;
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
);
486 ((mig_reply_error_t
*) reply
->ikm_header
)->RetCode
489 kernel_task
->messages_received
++;
492 kernel_task
->messages_sent
++;
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
;
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.
511 * We set msgh_remote_port to IP_NULL so that the kmsg
512 * destroy routines don't try to destroy the port twice.
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
;
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
;
526 panic("ipc_kobject_server: strange destination rights");
530 * Destroy voucher. The kernel MIG servers never take ownership
531 * of vouchers sent in messages. Swallow any such rights here.
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
;
540 if ((kr
== KERN_SUCCESS
) || (kr
== MIG_NO_REPLY
)) {
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
548 ipc_kmsg_free(request
);
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.
555 request
->ikm_header
->msgh_local_port
= MACH_PORT_NULL
;
556 ipc_kmsg_destroy(request
);
559 if (kr
== MIG_NO_REPLY
) {
561 * The server function will send a reply message
562 * using the reply port right, which it has saved.
566 ipc_kmsg_free(reply
);
572 replyp
= reply
->ikm_header
->msgh_remote_port
;
575 if (!IP_VALID(replyp
)) {
577 * Can't queue the reply message if the destination
578 * (the reply port) isn't valid.
581 ipc_kmsg_destroy(reply
);
584 } else if (replyp
->ip_receiver
== ipc_space_kernel
) {
586 * Don't send replies to kobject kernel ports
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
);
596 /* Fail the MIG call if the task exec token changed during the call */
597 if (kr
== KERN_SUCCESS
&& exec_token_changed
) {
599 * Create a new reply msg with error and destroy the old reply msg.
601 ipc_kmsg_t new_reply
= ipc_kmsg_alloc(reply_size
);
603 if (new_reply
== IKM_NULL
) {
604 printf("ipc_kobject_server: dropping request\n");
605 ipc_kmsg_destroy(reply
);
609 * Initialize the new reply message.
612 #define OutP_new ((mig_reply_error_t *) new_reply->ikm_header)
613 #define OutP_old ((mig_reply_error_t *) reply->ikm_header)
615 bzero((void *)OutP_new
, reply_size
);
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
;
625 /* Set the error as KERN_INVALID_TASK */
626 OutP_new
->RetCode
= KERN_INVALID_TASK
;
633 * Destroy everything in reply except the reply port right,
634 * which is needed in the new reply message.
636 reply
->ikm_header
->msgh_remote_port
= MACH_PORT_NULL
;
637 ipc_kmsg_destroy(reply
);
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
;
654 * Routine: ipc_kobject_set
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.
660 * Nothing locked. The port must be active if setting
661 * a kobject linkage. Clearing a linkage is OK on an
667 ipc_kobject_t kobject
,
668 ipc_kobject_type_t type
)
671 ipc_kobject_set_atomically(port
, kobject
, type
);
676 ipc_kobject_set_atomically(
678 ipc_kobject_t kobject
,
679 ipc_kobject_type_t type
)
681 assert(type
== IKOT_NONE
|| ip_active(port
));
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
;
690 port
->ip_kobject
= kobject
;
692 if (type
!= IKOT_NONE
) {
693 /* Once set, this bit can never be unset */
694 port
->ip_object
.io_bits
|= IO_BITS_KOBJECT
;
699 * Routine: ipc_kobject_init_port
701 * Initialize a kobject port with the given types and options.
703 * This function never fails.
706 ipc_kobject_init_port(
708 ipc_kobject_t kobject
,
709 ipc_kobject_type_t type
,
710 ipc_kobject_alloc_options_t options
)
712 ipc_kobject_set_atomically(port
, kobject
, type
);
714 if (options
& IPC_KOBJECT_ALLOC_MAKE_SEND
) {
715 ipc_port_make_send_locked(port
);
717 if (options
& IPC_KOBJECT_ALLOC_NSREQUEST
) {
718 ipc_port_make_sonce_locked(port
);
719 port
->ip_nsrequest
= port
;
721 if (options
& IPC_KOBJECT_ALLOC_NO_GRANT
) {
722 port
->ip_no_grant
= 1;
724 if (options
& IPC_KOBJECT_ALLOC_IMMOVABLE_SEND
) {
725 port
->ip_immovable_send
= 1;
730 * Routine: ipc_kobject_alloc_port
732 * Allocate a kobject port in the kernel space of the specified type.
734 * This function never fails.
737 * No locks held (memory is allocated)
740 ipc_kobject_alloc_port(
741 ipc_kobject_t kobject
,
742 ipc_kobject_type_t type
,
743 ipc_kobject_alloc_options_t options
)
745 ipc_port_t port
= ipc_port_alloc_kernel();
747 if (port
== IP_NULL
) {
748 panic("ipc_kobject_alloc_port(): failed to allocate port");
751 ipc_kobject_init_port(port
, kobject
, type
, options
);
756 * Routine: ipc_kobject_alloc_labeled_port
758 * Allocate a kobject port and associated mandatory access label
759 * in the kernel space of the specified type.
761 * This function never fails.
764 * No locks held (memory is allocated)
768 ipc_kobject_alloc_labeled_port(
769 ipc_kobject_t kobject
,
770 ipc_kobject_type_t type
,
772 ipc_kobject_alloc_options_t options
)
775 ipc_kobject_label_t labelp
;
777 port
= ipc_port_alloc_kernel();
778 if (port
== IP_NULL
) {
779 panic("ipc_kobject_alloc_port(): failed to allocate port");
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");
786 labelp
->ikol_label
= label
;
787 port
->ip_kolabel
= labelp
;
788 port
->ip_object
.io_bits
|= IO_BITS_KOLABEL
;
790 ipc_kobject_init_port(port
, kobject
, type
, options
);
795 * Routine: ipc_kobject_make_send_lazy_alloc_port
797 * Make a send once for a kobject port.
799 * A location owning this port is passed in port_store.
800 * If no port exists, a port is made lazily.
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.
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.
811 * TRUE if a notification was armed
815 * Nothing is locked, memory can be allocated.
816 * The caller must be able to donate a kobject reference to the port.
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
)
826 ipc_port_t port
, previous
, __ptrauth_only port_addr
;
827 boolean_t rc
= FALSE
;
829 port
= os_atomic_load(port_store
, dependency
);
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
));
838 #endif // __has_feature(ptrauth_calls)
840 if (!IP_VALID(port
)) {
841 port
= ipc_kobject_alloc_port(kobject
, type
,
842 IPC_KOBJECT_ALLOC_MAKE_SEND
| IPC_KOBJECT_ALLOC_NSREQUEST
);
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
));
854 #endif // __has_feature(ptrauth_calls)
856 if (os_atomic_cmpxchgv(port_store
, IP_NULL
, port_addr
, &previous
, release
)) {
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;
867 ipc_port_dealloc_kernel(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
;
886 * Routine: ipc_kobject_make_send_lazy_alloc_labeled_port
888 * Make a send once for a kobject port.
890 * A location owning this port is passed in port_store.
891 * If no port exists, a port is made lazily.
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.
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.
902 * TRUE if a notification was armed
906 * Nothing is locked, memory can be allocated.
907 * The caller must be able to donate a kobject reference to the port.
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
,
916 ipc_port_t port
, previous
;
917 boolean_t rc
= FALSE
;
919 port
= os_atomic_load(port_store
, dependency
);
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
)) {
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;
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
);
941 assert(ip_is_kolabeled(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
;
959 * Routine: ipc_kobject_destroy
961 * Release any kernel object resources associated
962 * with the port, which is being destroyed.
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.
970 * Cleans up any kobject label that might be present.
972 * The port is not locked, but it is dead.
979 switch (ip_kotype(port
)) {
981 mk_timer_port_destroy(port
);
984 case IKOT_NAMED_ENTRY
:
985 mach_destroy_memory_entry(port
);
988 case IKOT_HOST_NOTIFY
:
989 host_notify_port_destroy(port
);
993 suid_cred_destroy(port
);
1000 if (ip_is_kolabeled(port
)) {
1001 ipc_kobject_label_t labelp
= port
->ip_kolabel
;
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
);
1012 * Routine: ipc_kobject_label_check
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.
1020 * Space is write locked and active.
1021 * Port is locked and active.
1024 ipc_kobject_label_check(
1027 __unused mach_msg_type_name_t msgt_name
)
1029 ipc_kobject_label_t labelp
;
1031 assert(is_active(space
));
1032 assert(ip_active(port
));
1034 /* Unlabled ports/kobjects are always allowed */
1035 if (!ip_is_kolabeled(port
)) {
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");
1044 labelp
= port
->ip_kolabel
;
1045 return (labelp
->ikol_label
& space
->is_label
) == labelp
->ikol_label
;
1050 mach_msg_header_t
*request_header
,
1051 mach_msg_header_t
*reply_header
)
1053 mach_msg_max_trailer_t
* trailer
;
1054 ipc_port_t port
= request_header
->msgh_remote_port
;
1056 ((mig_reply_error_t
*) reply_header
)->RetCode
= MIG_NO_REPLY
;
1058 trailer
= (mach_msg_max_trailer_t
*)
1059 ((vm_offset_t
)request_header
+ request_header
->msgh_size
);
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!
1066 if (0 != bcmp(&trailer
->msgh_audit
, &KERNEL_AUDIT_TOKEN
,
1067 sizeof(trailer
->msgh_audit
))) {
1070 if (0 != bcmp(&trailer
->msgh_sender
, &KERNEL_SECURITY_TOKEN
,
1071 sizeof(trailer
->msgh_sender
))) {
1075 switch (request_header
->msgh_id
) {
1076 case MACH_NOTIFY_NO_SENDERS
:
1077 switch (ip_kotype(port
)) {
1079 ipc_voucher_notify(request_header
);
1082 case IKOT_VOUCHER_ATTR_CONTROL
:
1083 ipc_voucher_attr_control_notify(request_header
);
1086 case IKOT_SEMAPHORE
:
1087 semaphore_notify(request_header
);
1090 case IKOT_EVENTLINK
:
1091 ipc_eventlink_notify(request_header
);
1094 case IKOT_TASK_CONTROL
:
1095 task_port_notify(request_header
);
1098 case IKOT_NAMED_ENTRY
:
1102 * Bring the sequence number and mscount in
1103 * line with ipc_port_destroy assertion.
1105 port
->ip_mscount
= 0;
1106 port
->ip_messages
.imq_seqno
= 0;
1107 ipc_port_destroy(port
); /* releases lock */
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
;
1120 case IKOT_AU_SESSIONPORT
:
1121 audit_session_nosenders(request_header
);
1125 fileport_notify(request_header
);
1128 case IKOT_WORK_INTERVAL
:
1129 work_interval_port_notify(request_header
);
1131 case IKOT_TASK_READ
:
1132 case IKOT_TASK_INSPECT
:
1133 task_port_with_flavor_notify(request_header
);
1135 case IKOT_THREAD_READ
:
1136 case IKOT_THREAD_INSPECT
:
1137 thread_port_with_flavor_notify(request_header
);
1139 case IKOT_SUID_CRED
:
1140 suid_cred_notify(request_header
);
1143 case IKOT_HYPERVISOR
:
1144 hv_port_notify(request_header
);
1151 case MACH_NOTIFY_PORT_DELETED
:
1152 case MACH_NOTIFY_PORT_DESTROYED
:
1153 case MACH_NOTIFY_SEND_ONCE
:
1154 case MACH_NOTIFY_DEAD_NAME
:
1160 switch (ip_kotype(port
)) {
1162 case IKOT_IOKIT_OBJECT
:
1163 case IKOT_IOKIT_CONNECT
:
1164 case IKOT_IOKIT_IDENT
:
1165 case IKOT_UEXT_OBJECT
:
1167 return iokit_notify(request_header
);
1170 case IKOT_TASK_RESUME
:
1172 return task_suspension_notify(request_header
);