2 * Copyright (c) 2000-2010 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,1988,1987 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,
68 * Task and thread related IPC functions.
71 #include <mach/mach_types.h>
72 #include <mach/boolean.h>
73 #include <mach/kern_return.h>
74 #include <mach/mach_param.h>
75 #include <mach/task_special_ports.h>
76 #include <mach/thread_special_ports.h>
77 #include <mach/thread_status.h>
78 #include <mach/exception_types.h>
79 #include <mach/memory_object_types.h>
80 #include <mach/mach_traps.h>
81 #include <mach/task_server.h>
82 #include <mach/thread_act_server.h>
83 #include <mach/mach_host_server.h>
84 #include <mach/host_priv_server.h>
85 #include <mach/vm_map_server.h>
87 #include <kern/kern_types.h>
88 #include <kern/host.h>
89 #include <kern/ipc_kobject.h>
90 #include <kern/ipc_tt.h>
91 #include <kern/kalloc.h>
92 #include <kern/thread.h>
93 #include <kern/misc_protos.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_pageout.h>
97 #include <vm/vm_protos.h>
99 #include <security/mac_mach_internal.h>
101 #if CONFIG_EMBEDDED && !SECURE_KERNEL
102 extern int cs_relax_platform_task_ports
;
105 /* forward declarations */
106 task_t
convert_port_to_locked_task(ipc_port_t port
);
107 task_inspect_t
convert_port_to_locked_task_inspect(ipc_port_t port
);
108 static void ipc_port_bind_special_reply_port_locked(ipc_port_t port
);
109 static kern_return_t
ipc_port_unbind_special_reply_port(thread_t thread
, boolean_t unbind_active_port
);
110 kern_return_t
task_conversion_eval(task_t caller
, task_t victim
);
113 * Routine: ipc_task_init
115 * Initialize a task's IPC state.
117 * If non-null, some state will be inherited from the parent.
118 * The parent must be appropriately initialized.
135 kr
= ipc_space_create(&ipc_table_entries
[0], &space
);
136 if (kr
!= KERN_SUCCESS
)
137 panic("ipc_task_init");
139 space
->is_task
= task
;
141 kport
= ipc_port_alloc_kernel();
142 if (kport
== IP_NULL
)
143 panic("ipc_task_init");
145 nport
= ipc_port_alloc_kernel();
146 if (nport
== IP_NULL
)
147 panic("ipc_task_init");
150 task
->itk_self
= kport
;
151 task
->itk_nself
= nport
;
152 task
->itk_resume
= IP_NULL
; /* Lazily allocated on-demand */
153 if (task_is_a_corpse_fork(task
)) {
155 * No sender's notification for corpse would not
156 * work with a naked send right in kernel.
158 task
->itk_sself
= IP_NULL
;
160 task
->itk_sself
= ipc_port_make_send(kport
);
162 task
->itk_debug_control
= IP_NULL
;
163 task
->itk_space
= space
;
166 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
167 mac_exc_associate_action_label(&task
->exc_actions
[i
], mac_exc_create_label());
171 if (parent
== TASK_NULL
) {
174 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
175 task
->exc_actions
[i
].port
= IP_NULL
;
178 kr
= host_get_host_port(host_priv_self(), &port
);
179 assert(kr
== KERN_SUCCESS
);
180 task
->itk_host
= port
;
182 task
->itk_bootstrap
= IP_NULL
;
183 task
->itk_seatbelt
= IP_NULL
;
184 task
->itk_gssd
= IP_NULL
;
185 task
->itk_task_access
= IP_NULL
;
187 for (i
= 0; i
< TASK_PORT_REGISTER_MAX
; i
++)
188 task
->itk_registered
[i
] = IP_NULL
;
191 assert(parent
->itk_self
!= IP_NULL
);
193 /* inherit registered ports */
195 for (i
= 0; i
< TASK_PORT_REGISTER_MAX
; i
++)
196 task
->itk_registered
[i
] =
197 ipc_port_copy_send(parent
->itk_registered
[i
]);
199 /* inherit exception and bootstrap ports */
201 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
202 task
->exc_actions
[i
].port
=
203 ipc_port_copy_send(parent
->exc_actions
[i
].port
);
204 task
->exc_actions
[i
].flavor
=
205 parent
->exc_actions
[i
].flavor
;
206 task
->exc_actions
[i
].behavior
=
207 parent
->exc_actions
[i
].behavior
;
208 task
->exc_actions
[i
].privileged
=
209 parent
->exc_actions
[i
].privileged
;
211 mac_exc_inherit_action_label(parent
->exc_actions
+ i
, task
->exc_actions
+ i
);
215 ipc_port_copy_send(parent
->itk_host
);
217 task
->itk_bootstrap
=
218 ipc_port_copy_send(parent
->itk_bootstrap
);
221 ipc_port_copy_send(parent
->itk_seatbelt
);
224 ipc_port_copy_send(parent
->itk_gssd
);
226 task
->itk_task_access
=
227 ipc_port_copy_send(parent
->itk_task_access
);
234 * Routine: ipc_task_enable
236 * Enable a task for IPC access.
249 kport
= task
->itk_self
;
250 if (kport
!= IP_NULL
)
251 ipc_kobject_set(kport
, (ipc_kobject_t
) task
, IKOT_TASK
);
252 nport
= task
->itk_nself
;
253 if (nport
!= IP_NULL
)
254 ipc_kobject_set(nport
, (ipc_kobject_t
) task
, IKOT_TASK_NAME
);
259 * Routine: ipc_task_disable
261 * Disable IPC access to a task.
275 kport
= task
->itk_self
;
276 if (kport
!= IP_NULL
)
277 ipc_kobject_set(kport
, IKO_NULL
, IKOT_NONE
);
278 nport
= task
->itk_nself
;
279 if (nport
!= IP_NULL
)
280 ipc_kobject_set(nport
, IKO_NULL
, IKOT_NONE
);
282 rport
= task
->itk_resume
;
283 if (rport
!= IP_NULL
) {
285 * From this point onwards this task is no longer accepting
288 * There are still outstanding suspensions on this task,
289 * even as it is being torn down. Disconnect the task
290 * from the rport, thereby "orphaning" the rport. The rport
291 * itself will go away only when the last suspension holder
292 * destroys his SO right to it -- when he either
293 * exits, or tries to actually use that last SO right to
294 * resume this (now non-existent) task.
296 ipc_kobject_set(rport
, IKO_NULL
, IKOT_NONE
);
302 * Routine: ipc_task_terminate
304 * Clean up and destroy a task's IPC state.
306 * Nothing locked. The task must be suspended.
307 * (Or the current thread must be in the task.)
320 kport
= task
->itk_self
;
322 if (kport
== IP_NULL
) {
323 /* the task is already terminated (can this happen?) */
327 task
->itk_self
= IP_NULL
;
329 nport
= task
->itk_nself
;
330 assert(nport
!= IP_NULL
);
331 task
->itk_nself
= IP_NULL
;
333 rport
= task
->itk_resume
;
334 task
->itk_resume
= IP_NULL
;
338 /* release the naked send rights */
340 if (IP_VALID(task
->itk_sself
))
341 ipc_port_release_send(task
->itk_sself
);
343 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
344 if (IP_VALID(task
->exc_actions
[i
].port
)) {
345 ipc_port_release_send(task
->exc_actions
[i
].port
);
348 mac_exc_free_action_label(task
->exc_actions
+ i
);
352 if (IP_VALID(task
->itk_host
))
353 ipc_port_release_send(task
->itk_host
);
355 if (IP_VALID(task
->itk_bootstrap
))
356 ipc_port_release_send(task
->itk_bootstrap
);
358 if (IP_VALID(task
->itk_seatbelt
))
359 ipc_port_release_send(task
->itk_seatbelt
);
361 if (IP_VALID(task
->itk_gssd
))
362 ipc_port_release_send(task
->itk_gssd
);
364 if (IP_VALID(task
->itk_task_access
))
365 ipc_port_release_send(task
->itk_task_access
);
367 if (IP_VALID(task
->itk_debug_control
))
368 ipc_port_release_send(task
->itk_debug_control
);
370 for (i
= 0; i
< TASK_PORT_REGISTER_MAX
; i
++)
371 if (IP_VALID(task
->itk_registered
[i
]))
372 ipc_port_release_send(task
->itk_registered
[i
]);
374 /* destroy the kernel ports */
375 ipc_port_dealloc_kernel(kport
);
376 ipc_port_dealloc_kernel(nport
);
377 if (rport
!= IP_NULL
)
378 ipc_port_dealloc_kernel(rport
);
380 itk_lock_destroy(task
);
384 * Routine: ipc_task_reset
386 * Reset a task's IPC state to protect it when
387 * it enters an elevated security context. The
388 * task name port can remain the same - since
389 * it represents no specific privilege.
391 * Nothing locked. The task must be suspended.
392 * (Or the current thread must be in the task.)
399 ipc_port_t old_kport
, new_kport
;
400 ipc_port_t old_sself
;
401 ipc_port_t old_exc_actions
[EXC_TYPES_COUNT
];
405 /* Fresh label to unset credentials in existing labels. */
406 struct label
*unset_label
= mac_exc_create_label();
409 new_kport
= ipc_port_alloc_kernel();
410 if (new_kport
== IP_NULL
)
411 panic("ipc_task_reset");
415 old_kport
= task
->itk_self
;
417 if (old_kport
== IP_NULL
) {
418 /* the task is already terminated (can this happen?) */
420 ipc_port_dealloc_kernel(new_kport
);
422 mac_exc_free_label(unset_label
);
427 task
->itk_self
= new_kport
;
428 old_sself
= task
->itk_sself
;
429 task
->itk_sself
= ipc_port_make_send(new_kport
);
431 /* Set the old kport to IKOT_NONE and update the exec token while under the port lock */
433 ipc_kobject_set_atomically(old_kport
, IKO_NULL
, IKOT_NONE
);
434 task
->exec_token
+= 1;
435 ip_unlock(old_kport
);
437 ipc_kobject_set(new_kport
, (ipc_kobject_t
) task
, IKOT_TASK
);
439 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
440 old_exc_actions
[i
] = IP_NULL
;
442 if (i
== EXC_CORPSE_NOTIFY
&& task_corpse_pending_report(task
)) {
446 if (!task
->exc_actions
[i
].privileged
) {
448 mac_exc_update_action_label(task
->exc_actions
+ i
, unset_label
);
450 old_exc_actions
[i
] = task
->exc_actions
[i
].port
;
451 task
->exc_actions
[i
].port
= IP_NULL
;
455 if (IP_VALID(task
->itk_debug_control
)) {
456 ipc_port_release_send(task
->itk_debug_control
);
458 task
->itk_debug_control
= IP_NULL
;
463 mac_exc_free_label(unset_label
);
466 /* release the naked send rights */
468 if (IP_VALID(old_sself
))
469 ipc_port_release_send(old_sself
);
471 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
472 if (IP_VALID(old_exc_actions
[i
])) {
473 ipc_port_release_send(old_exc_actions
[i
]);
477 /* destroy the kernel port */
478 ipc_port_dealloc_kernel(old_kport
);
482 * Routine: ipc_thread_init
484 * Initialize a thread's IPC state.
495 kport
= ipc_port_alloc_kernel();
496 if (kport
== IP_NULL
)
497 panic("ipc_thread_init");
499 thread
->ith_self
= kport
;
500 thread
->ith_sself
= ipc_port_make_send(kport
);
501 thread
->ith_special_reply_port
= NULL
;
502 thread
->exc_actions
= NULL
;
504 ipc_kobject_set(kport
, (ipc_kobject_t
)thread
, IKOT_THREAD
);
506 #if IMPORTANCE_INHERITANCE
507 thread
->ith_assertions
= 0;
510 ipc_kmsg_queue_init(&thread
->ith_messages
);
512 thread
->ith_rpc_reply
= IP_NULL
;
516 ipc_thread_init_exc_actions(
519 assert(thread
->exc_actions
== NULL
);
521 thread
->exc_actions
= kalloc(sizeof(struct exception_action
) * EXC_TYPES_COUNT
);
522 bzero(thread
->exc_actions
, sizeof(struct exception_action
) * EXC_TYPES_COUNT
);
525 for (size_t i
= 0; i
< EXC_TYPES_COUNT
; ++i
) {
526 mac_exc_associate_action_label(thread
->exc_actions
+ i
, mac_exc_create_label());
532 ipc_thread_destroy_exc_actions(
535 if (thread
->exc_actions
!= NULL
) {
537 for (size_t i
= 0; i
< EXC_TYPES_COUNT
; ++i
) {
538 mac_exc_free_action_label(thread
->exc_actions
+ i
);
542 kfree(thread
->exc_actions
,
543 sizeof(struct exception_action
) * EXC_TYPES_COUNT
);
544 thread
->exc_actions
= NULL
;
552 ipc_port_t kport
= thread
->ith_self
;
554 if (kport
!= IP_NULL
)
555 ipc_kobject_set(kport
, IKO_NULL
, IKOT_NONE
);
559 * Routine: ipc_thread_terminate
561 * Clean up and destroy a thread's IPC state.
567 ipc_thread_terminate(
570 ipc_port_t kport
= thread
->ith_self
;
572 if (kport
!= IP_NULL
) {
575 if (IP_VALID(thread
->ith_sself
))
576 ipc_port_release_send(thread
->ith_sself
);
578 thread
->ith_sself
= thread
->ith_self
= IP_NULL
;
580 if (thread
->exc_actions
!= NULL
) {
581 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; ++i
) {
582 if (IP_VALID(thread
->exc_actions
[i
].port
))
583 ipc_port_release_send(thread
->exc_actions
[i
].port
);
585 ipc_thread_destroy_exc_actions(thread
);
588 ipc_port_dealloc_kernel(kport
);
591 #if IMPORTANCE_INHERITANCE
592 assert(thread
->ith_assertions
== 0);
595 /* unbind the thread special reply port */
596 if (IP_VALID(thread
->ith_special_reply_port
)) {
597 ipc_port_unbind_special_reply_port(thread
, TRUE
);
600 assert(ipc_kmsg_queue_empty(&thread
->ith_messages
));
602 if (thread
->ith_rpc_reply
!= IP_NULL
)
603 ipc_port_dealloc_reply(thread
->ith_rpc_reply
);
605 thread
->ith_rpc_reply
= IP_NULL
;
609 * Routine: ipc_thread_reset
611 * Reset the IPC state for a given Mach thread when
612 * its task enters an elevated security context.
613 * Both the thread port and its exception ports have
614 * to be reset. Its RPC reply port cannot have any
615 * rights outstanding, so it should be fine.
624 ipc_port_t old_kport
, new_kport
;
625 ipc_port_t old_sself
;
626 ipc_port_t old_exc_actions
[EXC_TYPES_COUNT
];
627 boolean_t has_old_exc_actions
= FALSE
;
631 struct label
*new_label
= mac_exc_create_label();
634 new_kport
= ipc_port_alloc_kernel();
635 if (new_kport
== IP_NULL
)
636 panic("ipc_task_reset");
638 thread_mtx_lock(thread
);
640 old_kport
= thread
->ith_self
;
642 if (old_kport
== IP_NULL
&& thread
->inspection
== FALSE
) {
643 /* the is already terminated (can this happen?) */
644 thread_mtx_unlock(thread
);
645 ipc_port_dealloc_kernel(new_kport
);
647 mac_exc_free_label(new_label
);
652 thread
->ith_self
= new_kport
;
653 old_sself
= thread
->ith_sself
;
654 thread
->ith_sself
= ipc_port_make_send(new_kport
);
655 if (old_kport
!= IP_NULL
) {
656 ipc_kobject_set(old_kport
, IKO_NULL
, IKOT_NONE
);
658 ipc_kobject_set(new_kport
, (ipc_kobject_t
) thread
, IKOT_THREAD
);
661 * Only ports that were set by root-owned processes
662 * (privileged ports) should survive
664 if (thread
->exc_actions
!= NULL
) {
665 has_old_exc_actions
= TRUE
;
666 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
667 if (thread
->exc_actions
[i
].privileged
) {
668 old_exc_actions
[i
] = IP_NULL
;
671 mac_exc_update_action_label(thread
->exc_actions
+ i
, new_label
);
673 old_exc_actions
[i
] = thread
->exc_actions
[i
].port
;
674 thread
->exc_actions
[i
].port
= IP_NULL
;
679 thread_mtx_unlock(thread
);
682 mac_exc_free_label(new_label
);
685 /* release the naked send rights */
687 if (IP_VALID(old_sself
))
688 ipc_port_release_send(old_sself
);
690 if (has_old_exc_actions
) {
691 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
692 ipc_port_release_send(old_exc_actions
[i
]);
696 /* destroy the kernel port */
697 if (old_kport
!= IP_NULL
) {
698 ipc_port_dealloc_kernel(old_kport
);
701 /* unbind the thread special reply port */
702 if (IP_VALID(thread
->ith_special_reply_port
)) {
703 ipc_port_unbind_special_reply_port(thread
, TRUE
);
708 * Routine: retrieve_task_self_fast
710 * Optimized version of retrieve_task_self,
711 * that only works for the current task.
713 * Return a send right (possibly null/dead)
714 * for the task's user-visible self port.
720 retrieve_task_self_fast(
725 assert(task
== current_task());
728 assert(task
->itk_self
!= IP_NULL
);
730 if ((port
= task
->itk_sself
) == task
->itk_self
) {
734 assert(ip_active(port
));
739 port
= ipc_port_copy_send(port
);
746 * Routine: retrieve_thread_self_fast
748 * Return a send right (possibly null/dead)
749 * for the thread's user-visible self port.
751 * Only works for the current thread.
758 retrieve_thread_self_fast(
763 assert(thread
== current_thread());
765 thread_mtx_lock(thread
);
767 assert(thread
->ith_self
!= IP_NULL
);
769 if ((port
= thread
->ith_sself
) == thread
->ith_self
) {
773 assert(ip_active(port
));
779 port
= ipc_port_copy_send(port
);
781 thread_mtx_unlock(thread
);
787 * Routine: task_self_trap [mach trap]
789 * Give the caller send rights for his own task port.
793 * MACH_PORT_NULL if there are any resource failures
799 __unused
struct task_self_trap_args
*args
)
801 task_t task
= current_task();
803 mach_port_name_t name
;
805 sright
= retrieve_task_self_fast(task
);
806 name
= ipc_port_copyout_send(sright
, task
->itk_space
);
811 * Routine: thread_self_trap [mach trap]
813 * Give the caller send rights for his own thread port.
817 * MACH_PORT_NULL if there are any resource failures
823 __unused
struct thread_self_trap_args
*args
)
825 thread_t thread
= current_thread();
826 task_t task
= thread
->task
;
828 mach_port_name_t name
;
830 sright
= retrieve_thread_self_fast(thread
);
831 name
= ipc_port_copyout_send(sright
, task
->itk_space
);
837 * Routine: mach_reply_port [mach trap]
839 * Allocate a port for the caller.
843 * MACH_PORT_NULL if there are any resource failures
849 __unused
struct mach_reply_port_args
*args
)
852 mach_port_name_t name
;
855 kr
= ipc_port_alloc(current_task()->itk_space
, &name
, &port
);
856 if (kr
== KERN_SUCCESS
)
859 name
= MACH_PORT_NULL
;
864 * Routine: thread_get_special_reply_port [mach trap]
866 * Allocate a special reply port for the calling thread.
870 * MACH_PORT_NULL if there are any resource failures
875 thread_get_special_reply_port(
876 __unused
struct thread_get_special_reply_port_args
*args
)
879 mach_port_name_t name
;
881 thread_t thread
= current_thread();
883 /* unbind the thread special reply port */
884 if (IP_VALID(thread
->ith_special_reply_port
)) {
885 kr
= ipc_port_unbind_special_reply_port(thread
, TRUE
);
886 if (kr
!= KERN_SUCCESS
) {
887 return MACH_PORT_NULL
;
891 kr
= ipc_port_alloc(current_task()->itk_space
, &name
, &port
);
892 if (kr
== KERN_SUCCESS
) {
893 ipc_port_bind_special_reply_port_locked(port
);
896 name
= MACH_PORT_NULL
;
902 * Routine: ipc_port_bind_special_reply_port_locked
904 * Bind the given port to current thread as a special reply port.
912 ipc_port_bind_special_reply_port_locked(
915 thread_t thread
= current_thread();
916 assert(thread
->ith_special_reply_port
== NULL
);
919 thread
->ith_special_reply_port
= port
;
920 port
->ip_specialreply
= 1;
921 port
->ip_link_sync_qos
= 1;
925 * Routine: ipc_port_unbind_special_reply_port
927 * Unbind the thread's special reply port.
928 * If the special port is linked to a port, adjust it's sync qos delta`.
935 ipc_port_unbind_special_reply_port(
937 boolean_t unbind_active_port
)
939 ipc_port_t special_reply_port
= thread
->ith_special_reply_port
;
941 ip_lock(special_reply_port
);
943 /* Return error if port active and unbind_active_port set to FALSE */
944 if (unbind_active_port
== FALSE
&& ip_active(special_reply_port
)) {
945 ip_unlock(special_reply_port
);
949 thread
->ith_special_reply_port
= NULL
;
950 ipc_port_unlink_special_reply_port_locked(special_reply_port
, NULL
,
951 IPC_PORT_UNLINK_SR_CLEAR_SPECIAL_REPLY
);
954 ip_release(special_reply_port
);
959 * Routine: thread_get_special_port [kernel call]
961 * Clones a send right for one of the thread's
966 * KERN_SUCCESS Extracted a send right.
967 * KERN_INVALID_ARGUMENT The thread is null.
968 * KERN_FAILURE The thread is dead.
969 * KERN_INVALID_ARGUMENT Invalid special port.
973 thread_get_special_port(
978 kern_return_t result
= KERN_SUCCESS
;
981 if (thread
== THREAD_NULL
)
982 return (KERN_INVALID_ARGUMENT
);
986 case THREAD_KERNEL_PORT
:
987 whichp
= &thread
->ith_sself
;
991 return (KERN_INVALID_ARGUMENT
);
994 thread_mtx_lock(thread
);
997 *portp
= ipc_port_copy_send(*whichp
);
999 result
= KERN_FAILURE
;
1001 thread_mtx_unlock(thread
);
1007 * Routine: thread_set_special_port [kernel call]
1009 * Changes one of the thread's special ports,
1010 * setting it to the supplied send right.
1012 * Nothing locked. If successful, consumes
1013 * the supplied send right.
1015 * KERN_SUCCESS Changed the special port.
1016 * KERN_INVALID_ARGUMENT The thread is null.
1017 * KERN_FAILURE The thread is dead.
1018 * KERN_INVALID_ARGUMENT Invalid special port.
1022 thread_set_special_port(
1027 kern_return_t result
= KERN_SUCCESS
;
1028 ipc_port_t
*whichp
, old
= IP_NULL
;
1030 if (thread
== THREAD_NULL
)
1031 return (KERN_INVALID_ARGUMENT
);
1035 case THREAD_KERNEL_PORT
:
1036 whichp
= &thread
->ith_sself
;
1040 return (KERN_INVALID_ARGUMENT
);
1043 thread_mtx_lock(thread
);
1045 if (thread
->active
) {
1050 result
= KERN_FAILURE
;
1052 thread_mtx_unlock(thread
);
1055 ipc_port_release_send(old
);
1061 * Routine: task_get_special_port [kernel call]
1063 * Clones a send right for one of the task's
1068 * KERN_SUCCESS Extracted a send right.
1069 * KERN_INVALID_ARGUMENT The task is null.
1070 * KERN_FAILURE The task/space is dead.
1071 * KERN_INVALID_ARGUMENT Invalid special port.
1075 task_get_special_port(
1082 if (task
== TASK_NULL
)
1083 return KERN_INVALID_ARGUMENT
;
1086 if (task
->itk_self
== IP_NULL
) {
1088 return KERN_FAILURE
;
1092 case TASK_KERNEL_PORT
:
1093 port
= ipc_port_copy_send(task
->itk_sself
);
1096 case TASK_NAME_PORT
:
1097 port
= ipc_port_make_send(task
->itk_nself
);
1100 case TASK_HOST_PORT
:
1101 port
= ipc_port_copy_send(task
->itk_host
);
1104 case TASK_BOOTSTRAP_PORT
:
1105 port
= ipc_port_copy_send(task
->itk_bootstrap
);
1108 case TASK_SEATBELT_PORT
:
1109 port
= ipc_port_copy_send(task
->itk_seatbelt
);
1112 case TASK_ACCESS_PORT
:
1113 port
= ipc_port_copy_send(task
->itk_task_access
);
1116 case TASK_DEBUG_CONTROL_PORT
:
1117 port
= ipc_port_copy_send(task
->itk_debug_control
);
1122 return KERN_INVALID_ARGUMENT
;
1127 return KERN_SUCCESS
;
1131 * Routine: task_set_special_port [kernel call]
1133 * Changes one of the task's special ports,
1134 * setting it to the supplied send right.
1136 * Nothing locked. If successful, consumes
1137 * the supplied send right.
1139 * KERN_SUCCESS Changed the special port.
1140 * KERN_INVALID_ARGUMENT The task is null.
1141 * KERN_FAILURE The task/space is dead.
1142 * KERN_INVALID_ARGUMENT Invalid special port.
1143 * KERN_NO_ACCESS Attempted overwrite of seatbelt port.
1147 task_set_special_port(
1155 if (task
== TASK_NULL
)
1156 return KERN_INVALID_ARGUMENT
;
1159 case TASK_KERNEL_PORT
:
1160 whichp
= &task
->itk_sself
;
1163 case TASK_HOST_PORT
:
1164 whichp
= &task
->itk_host
;
1167 case TASK_BOOTSTRAP_PORT
:
1168 whichp
= &task
->itk_bootstrap
;
1171 case TASK_SEATBELT_PORT
:
1172 whichp
= &task
->itk_seatbelt
;
1175 case TASK_ACCESS_PORT
:
1176 whichp
= &task
->itk_task_access
;
1179 case TASK_DEBUG_CONTROL_PORT
:
1180 whichp
= &task
->itk_debug_control
;
1184 return KERN_INVALID_ARGUMENT
;
1188 if (task
->itk_self
== IP_NULL
) {
1190 return KERN_FAILURE
;
1193 /* do not allow overwrite of seatbelt or task access ports */
1194 if ((TASK_SEATBELT_PORT
== which
|| TASK_ACCESS_PORT
== which
)
1195 && IP_VALID(*whichp
)) {
1197 return KERN_NO_ACCESS
;
1205 ipc_port_release_send(old
);
1206 return KERN_SUCCESS
;
1211 * Routine: mach_ports_register [kernel call]
1213 * Stash a handful of port send rights in the task.
1214 * Child tasks will inherit these rights, but they
1215 * must use mach_ports_lookup to acquire them.
1217 * The rights are supplied in a (wired) kalloc'd segment.
1218 * Rights which aren't supplied are assumed to be null.
1220 * Nothing locked. If successful, consumes
1221 * the supplied rights and memory.
1223 * KERN_SUCCESS Stashed the port rights.
1224 * KERN_INVALID_ARGUMENT The task is null.
1225 * KERN_INVALID_ARGUMENT The task is dead.
1226 * KERN_INVALID_ARGUMENT The memory param is null.
1227 * KERN_INVALID_ARGUMENT Too many port rights supplied.
1231 mach_ports_register(
1233 mach_port_array_t memory
,
1234 mach_msg_type_number_t portsCnt
)
1236 ipc_port_t ports
[TASK_PORT_REGISTER_MAX
];
1239 if ((task
== TASK_NULL
) ||
1240 (portsCnt
> TASK_PORT_REGISTER_MAX
) ||
1241 (portsCnt
&& memory
== NULL
))
1242 return KERN_INVALID_ARGUMENT
;
1245 * Pad the port rights with nulls.
1248 for (i
= 0; i
< portsCnt
; i
++)
1249 ports
[i
] = memory
[i
];
1250 for (; i
< TASK_PORT_REGISTER_MAX
; i
++)
1254 if (task
->itk_self
== IP_NULL
) {
1256 return KERN_INVALID_ARGUMENT
;
1260 * Replace the old send rights with the new.
1261 * Release the old rights after unlocking.
1264 for (i
= 0; i
< TASK_PORT_REGISTER_MAX
; i
++) {
1267 old
= task
->itk_registered
[i
];
1268 task
->itk_registered
[i
] = ports
[i
];
1274 for (i
= 0; i
< TASK_PORT_REGISTER_MAX
; i
++)
1275 if (IP_VALID(ports
[i
]))
1276 ipc_port_release_send(ports
[i
]);
1279 * Now that the operation is known to be successful,
1280 * we can free the memory.
1285 (vm_size_t
) (portsCnt
* sizeof(mach_port_t
)));
1287 return KERN_SUCCESS
;
1291 * Routine: mach_ports_lookup [kernel call]
1293 * Retrieves (clones) the stashed port send rights.
1295 * Nothing locked. If successful, the caller gets
1296 * rights and memory.
1298 * KERN_SUCCESS Retrieved the send rights.
1299 * KERN_INVALID_ARGUMENT The task is null.
1300 * KERN_INVALID_ARGUMENT The task is dead.
1301 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
1307 mach_port_array_t
*portsp
,
1308 mach_msg_type_number_t
*portsCnt
)
1315 if (task
== TASK_NULL
)
1316 return KERN_INVALID_ARGUMENT
;
1318 size
= (vm_size_t
) (TASK_PORT_REGISTER_MAX
* sizeof(ipc_port_t
));
1320 memory
= kalloc(size
);
1322 return KERN_RESOURCE_SHORTAGE
;
1325 if (task
->itk_self
== IP_NULL
) {
1328 kfree(memory
, size
);
1329 return KERN_INVALID_ARGUMENT
;
1332 ports
= (ipc_port_t
*) memory
;
1335 * Clone port rights. Because kalloc'd memory
1336 * is wired, we won't fault while holding the task lock.
1339 for (i
= 0; i
< TASK_PORT_REGISTER_MAX
; i
++)
1340 ports
[i
] = ipc_port_copy_send(task
->itk_registered
[i
]);
1344 *portsp
= (mach_port_array_t
) ports
;
1345 *portsCnt
= TASK_PORT_REGISTER_MAX
;
1346 return KERN_SUCCESS
;
1350 task_conversion_eval(task_t caller
, task_t victim
)
1353 * Tasks are allowed to resolve their own task ports, and the kernel is
1354 * allowed to resolve anyone's task port.
1356 if (caller
== kernel_task
) {
1357 return KERN_SUCCESS
;
1360 if (caller
== victim
) {
1361 return KERN_SUCCESS
;
1365 * Only the kernel can can resolve the kernel's task port. We've established
1366 * by this point that the caller is not kernel_task.
1368 if (victim
== kernel_task
) {
1369 return KERN_INVALID_SECURITY
;
1374 * On embedded platforms, only a platform binary can resolve the task port
1375 * of another platform binary.
1377 if ((victim
->t_flags
& TF_PLATFORM
) && !(caller
->t_flags
& TF_PLATFORM
)) {
1379 return KERN_INVALID_SECURITY
;
1381 if (cs_relax_platform_task_ports
) {
1382 return KERN_SUCCESS
;
1384 return KERN_INVALID_SECURITY
;
1386 #endif /* SECURE_KERNEL */
1388 #endif /* CONFIG_EMBEDDED */
1390 return KERN_SUCCESS
;
1394 * Routine: convert_port_to_locked_task
1396 * Internal helper routine to convert from a port to a locked
1397 * task. Used by several routines that try to convert from a
1398 * task port to a reference on some task related object.
1400 * Nothing locked, blocking OK.
1403 convert_port_to_locked_task(ipc_port_t port
)
1405 int try_failed_count
= 0;
1407 while (IP_VALID(port
)) {
1408 task_t ct
= current_task();
1412 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_TASK
)) {
1416 task
= (task_t
) port
->ip_kobject
;
1417 assert(task
!= TASK_NULL
);
1419 if (task_conversion_eval(ct
, task
)) {
1425 * Normal lock ordering puts task_lock() before ip_lock().
1426 * Attempt out-of-order locking here.
1428 if (task_lock_try(task
)) {
1435 mutex_pause(try_failed_count
);
1441 * Routine: convert_port_to_locked_task_inspect
1443 * Internal helper routine to convert from a port to a locked
1444 * task inspect right. Used by internal routines that try to convert from a
1445 * task inspect port to a reference on some task related object.
1447 * Nothing locked, blocking OK.
1450 convert_port_to_locked_task_inspect(ipc_port_t port
)
1452 int try_failed_count
= 0;
1454 while (IP_VALID(port
)) {
1455 task_inspect_t task
;
1458 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_TASK
)) {
1460 return TASK_INSPECT_NULL
;
1462 task
= (task_inspect_t
)port
->ip_kobject
;
1463 assert(task
!= TASK_INSPECT_NULL
);
1465 * Normal lock ordering puts task_lock() before ip_lock().
1466 * Attempt out-of-order locking here.
1468 if (task_lock_try((task_t
)task
)) {
1475 mutex_pause(try_failed_count
);
1477 return TASK_INSPECT_NULL
;
1482 * Routine: convert_port_to_task
1484 * Convert from a port to a task.
1485 * Doesn't consume the port ref; produces a task ref,
1486 * which may be null.
1491 convert_port_to_task(
1494 return convert_port_to_task_with_exec_token(port
, NULL
);
1498 * Routine: convert_port_to_task_with_exec_token
1500 * Convert from a port to a task and return
1501 * the exec token stored in the task.
1502 * Doesn't consume the port ref; produces a task ref,
1503 * which may be null.
1508 convert_port_to_task_with_exec_token(
1510 uint32_t *exec_token
)
1512 task_t task
= TASK_NULL
;
1514 if (IP_VALID(port
)) {
1517 if ( ip_active(port
) &&
1518 ip_kotype(port
) == IKOT_TASK
) {
1519 task_t ct
= current_task();
1520 task
= (task_t
)port
->ip_kobject
;
1521 assert(task
!= TASK_NULL
);
1523 if (task_conversion_eval(ct
, task
)) {
1529 *exec_token
= task
->exec_token
;
1531 task_reference_internal(task
);
1541 * Routine: convert_port_to_task_name
1543 * Convert from a port to a task name.
1544 * Doesn't consume the port ref; produces a task name ref,
1545 * which may be null.
1550 convert_port_to_task_name(
1553 task_name_t task
= TASK_NULL
;
1555 if (IP_VALID(port
)) {
1558 if ( ip_active(port
) &&
1559 (ip_kotype(port
) == IKOT_TASK
||
1560 ip_kotype(port
) == IKOT_TASK_NAME
)) {
1561 task
= (task_name_t
)port
->ip_kobject
;
1562 assert(task
!= TASK_NAME_NULL
);
1564 task_reference_internal(task
);
1574 * Routine: convert_port_to_task_inspect
1576 * Convert from a port to a task inspection right
1577 * Doesn't consume the port ref; produces a task ref,
1578 * which may be null.
1583 convert_port_to_task_inspect(
1586 task_inspect_t task
= TASK_INSPECT_NULL
;
1588 if (IP_VALID(port
)) {
1591 if (ip_active(port
) &&
1592 ip_kotype(port
) == IKOT_TASK
) {
1593 task
= (task_inspect_t
)port
->ip_kobject
;
1594 assert(task
!= TASK_INSPECT_NULL
);
1596 task_reference_internal(task
);
1606 * Routine: convert_port_to_task_suspension_token
1608 * Convert from a port to a task suspension token.
1609 * Doesn't consume the port ref; produces a suspension token ref,
1610 * which may be null.
1614 task_suspension_token_t
1615 convert_port_to_task_suspension_token(
1618 task_suspension_token_t task
= TASK_NULL
;
1620 if (IP_VALID(port
)) {
1623 if ( ip_active(port
) &&
1624 ip_kotype(port
) == IKOT_TASK_RESUME
) {
1625 task
= (task_suspension_token_t
)port
->ip_kobject
;
1626 assert(task
!= TASK_NULL
);
1628 task_reference_internal(task
);
1638 * Routine: convert_port_to_space
1640 * Convert from a port to a space.
1641 * Doesn't consume the port ref; produces a space ref,
1642 * which may be null.
1647 convert_port_to_space(
1653 task
= convert_port_to_locked_task(port
);
1655 if (task
== TASK_NULL
)
1656 return IPC_SPACE_NULL
;
1658 if (!task
->active
) {
1660 return IPC_SPACE_NULL
;
1663 space
= task
->itk_space
;
1664 is_reference(space
);
1670 * Routine: convert_port_to_space_inspect
1672 * Convert from a port to a space inspect right.
1673 * Doesn't consume the port ref; produces a space inspect ref,
1674 * which may be null.
1679 convert_port_to_space_inspect(
1682 ipc_space_inspect_t space
;
1683 task_inspect_t task
;
1685 task
= convert_port_to_locked_task_inspect(port
);
1687 if (task
== TASK_INSPECT_NULL
)
1688 return IPC_SPACE_INSPECT_NULL
;
1690 if (!task
->active
) {
1692 return IPC_SPACE_INSPECT_NULL
;
1695 space
= (ipc_space_inspect_t
)task
->itk_space
;
1696 is_reference((ipc_space_t
)space
);
1697 task_unlock((task_t
)task
);
1702 * Routine: convert_port_to_map
1704 * Convert from a port to a map.
1705 * Doesn't consume the port ref; produces a map ref,
1706 * which may be null.
1712 convert_port_to_map(
1718 task
= convert_port_to_locked_task(port
);
1720 if (task
== TASK_NULL
)
1723 if (!task
->active
) {
1729 vm_map_reference_swap(map
);
1736 * Routine: convert_port_to_thread
1738 * Convert from a port to a thread.
1739 * Doesn't consume the port ref; produces an thread ref,
1740 * which may be null.
1746 convert_port_to_thread(
1749 thread_t thread
= THREAD_NULL
;
1751 if (IP_VALID(port
)) {
1754 if ( ip_active(port
) &&
1755 ip_kotype(port
) == IKOT_THREAD
) {
1756 thread
= (thread_t
)port
->ip_kobject
;
1757 assert(thread
!= THREAD_NULL
);
1758 if (thread
->task
&& thread
->task
== kernel_task
&&
1759 current_task() != kernel_task
) {
1764 thread_reference_internal(thread
);
1774 * Routine: convert_port_to_thread_inspect
1776 * Convert from a port to a thread inspection right
1777 * Doesn't consume the port ref; produces a thread ref,
1778 * which may be null.
1783 convert_port_to_thread_inspect(
1786 thread_inspect_t thread
= THREAD_INSPECT_NULL
;
1788 if (IP_VALID(port
)) {
1791 if (ip_active(port
) &&
1792 ip_kotype(port
) == IKOT_THREAD
) {
1793 thread
= (thread_inspect_t
)port
->ip_kobject
;
1794 assert(thread
!= THREAD_INSPECT_NULL
);
1795 thread_reference_internal((thread_t
)thread
);
1804 * Routine: convert_thread_inspect_to_port
1806 * Convert from a thread inspect reference to a port.
1807 * Consumes a thread ref;
1808 * As we never export thread inspect ports, always
1809 * creates a NULL port.
1815 convert_thread_inspect_to_port(thread_inspect_t thread
)
1817 thread_deallocate(thread
);
1823 * Routine: port_name_to_thread
1825 * Convert from a port name to an thread reference
1826 * A name of MACH_PORT_NULL is valid for the null thread.
1830 * TODO: Could this be faster if it were ipc_port_translate_send based, like thread_switch?
1831 * We could avoid extra lock/unlock and extra ref operations on the port.
1834 port_name_to_thread(
1835 mach_port_name_t name
)
1837 thread_t thread
= THREAD_NULL
;
1840 if (MACH_PORT_VALID(name
)) {
1841 if (ipc_object_copyin(current_space(), name
,
1842 MACH_MSG_TYPE_COPY_SEND
,
1843 (ipc_object_t
*)&kport
) != KERN_SUCCESS
)
1844 return (THREAD_NULL
);
1846 thread
= convert_port_to_thread(kport
);
1848 if (IP_VALID(kport
))
1849 ipc_port_release_send(kport
);
1857 mach_port_name_t name
)
1859 ipc_port_t kern_port
;
1861 task_t task
= TASK_NULL
;
1863 if (MACH_PORT_VALID(name
)) {
1864 kr
= ipc_object_copyin(current_space(), name
,
1865 MACH_MSG_TYPE_COPY_SEND
,
1866 (ipc_object_t
*) &kern_port
);
1867 if (kr
!= KERN_SUCCESS
)
1870 task
= convert_port_to_task(kern_port
);
1872 if (IP_VALID(kern_port
))
1873 ipc_port_release_send(kern_port
);
1879 port_name_to_task_inspect(
1880 mach_port_name_t name
)
1882 ipc_port_t kern_port
;
1884 task_inspect_t ti
= TASK_INSPECT_NULL
;
1886 if (MACH_PORT_VALID(name
)) {
1887 kr
= ipc_object_copyin(current_space(), name
,
1888 MACH_MSG_TYPE_COPY_SEND
,
1889 (ipc_object_t
*)&kern_port
);
1890 if (kr
!= KERN_SUCCESS
)
1893 ti
= convert_port_to_task_inspect(kern_port
);
1895 if (IP_VALID(kern_port
))
1896 ipc_port_release_send(kern_port
);
1902 * Routine: port_name_to_host
1904 * Convert from a port name to a host pointer.
1905 * NOTE: This does _not_ return a +1 reference to the host_t
1911 mach_port_name_t name
)
1914 host_t host
= HOST_NULL
;
1918 if (MACH_PORT_VALID(name
)) {
1919 kr
= ipc_port_translate_send(current_space(), name
, &port
);
1920 if (kr
== KERN_SUCCESS
) {
1921 host
= convert_port_to_host(port
);
1929 * Routine: convert_task_to_port
1931 * Convert from a task to a port.
1932 * Consumes a task ref; produces a naked send right
1933 * which may be invalid.
1939 convert_task_to_port(
1946 if (task
->itk_self
!= IP_NULL
)
1947 port
= ipc_port_make_send(task
->itk_self
);
1953 task_deallocate(task
);
1958 * Routine: convert_task_inspect_to_port
1960 * Convert from a task inspect reference to a port.
1961 * Consumes a task ref;
1962 * As we never export task inspect ports, always
1963 * creates a NULL port.
1968 convert_task_inspect_to_port(
1969 task_inspect_t task
)
1971 task_deallocate(task
);
1977 * Routine: convert_task_suspend_token_to_port
1979 * Convert from a task suspension token to a port.
1980 * Consumes a task suspension token ref; produces a naked send-once right
1981 * which may be invalid.
1986 convert_task_suspension_token_to_port(
1987 task_suspension_token_t task
)
1993 if (task
->itk_resume
== IP_NULL
) {
1994 task
->itk_resume
= ipc_port_alloc_kernel();
1995 if (!IP_VALID(task
->itk_resume
)) {
1996 panic("failed to create resume port");
1999 ipc_kobject_set(task
->itk_resume
, (ipc_kobject_t
) task
, IKOT_TASK_RESUME
);
2003 * Create a send-once right for each instance of a direct user-called
2004 * task_suspend2 call. Each time one of these send-once rights is abandoned,
2005 * the notification handler will resume the target task.
2007 port
= ipc_port_make_sonce(task
->itk_resume
);
2008 assert(IP_VALID(port
));
2014 task_suspension_token_deallocate(task
);
2021 * Routine: convert_task_name_to_port
2023 * Convert from a task name ref to a port.
2024 * Consumes a task name ref; produces a naked send right
2025 * which may be invalid.
2031 convert_task_name_to_port(
2032 task_name_t task_name
)
2036 itk_lock(task_name
);
2037 if (task_name
->itk_nself
!= IP_NULL
)
2038 port
= ipc_port_make_send(task_name
->itk_nself
);
2041 itk_unlock(task_name
);
2043 task_name_deallocate(task_name
);
2048 * Routine: convert_thread_to_port
2050 * Convert from a thread to a port.
2051 * Consumes an thread ref; produces a naked send right
2052 * which may be invalid.
2058 convert_thread_to_port(
2063 thread_mtx_lock(thread
);
2065 if (thread
->ith_self
!= IP_NULL
)
2066 port
= ipc_port_make_send(thread
->ith_self
);
2070 thread_mtx_unlock(thread
);
2072 thread_deallocate(thread
);
2078 * Routine: space_deallocate
2080 * Deallocate a space ref produced by convert_port_to_space.
2089 if (space
!= IS_NULL
)
2094 * Routine: space_inspect_deallocate
2096 * Deallocate a space inspect ref produced by convert_port_to_space_inspect.
2102 space_inspect_deallocate(
2103 ipc_space_inspect_t space
)
2105 if (space
!= IS_INSPECT_NULL
)
2106 is_release((ipc_space_t
)space
);
2110 * Routine: thread/task_set_exception_ports [kernel call]
2112 * Sets the thread/task exception port, flavor and
2113 * behavior for the exception types specified by the mask.
2114 * There will be one send right per exception per valid
2117 * Nothing locked. If successful, consumes
2118 * the supplied send right.
2120 * KERN_SUCCESS Changed the special port.
2121 * KERN_INVALID_ARGUMENT The thread is null,
2122 * Illegal mask bit set.
2123 * Illegal exception behavior
2124 * KERN_FAILURE The thread is dead.
2128 thread_set_exception_ports(
2130 exception_mask_t exception_mask
,
2131 ipc_port_t new_port
,
2132 exception_behavior_t new_behavior
,
2133 thread_state_flavor_t new_flavor
)
2135 ipc_port_t old_port
[EXC_TYPES_COUNT
];
2136 boolean_t privileged
= current_task()->sec_token
.val
[0] == 0;
2140 struct label
*new_label
;
2143 if (thread
== THREAD_NULL
)
2144 return (KERN_INVALID_ARGUMENT
);
2146 if (exception_mask
& ~EXC_MASK_VALID
)
2147 return (KERN_INVALID_ARGUMENT
);
2149 if (IP_VALID(new_port
)) {
2150 switch (new_behavior
& ~MACH_EXCEPTION_CODES
) {
2152 case EXCEPTION_DEFAULT
:
2153 case EXCEPTION_STATE
:
2154 case EXCEPTION_STATE_IDENTITY
:
2158 return (KERN_INVALID_ARGUMENT
);
2163 * Check the validity of the thread_state_flavor by calling the
2164 * VALID_THREAD_STATE_FLAVOR architecture dependent macro defined in
2165 * osfmk/mach/ARCHITECTURE/thread_status.h
2167 if (new_flavor
!= 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor
))
2168 return (KERN_INVALID_ARGUMENT
);
2171 new_label
= mac_exc_create_label_for_current_proc();
2174 thread_mtx_lock(thread
);
2176 if (!thread
->active
) {
2177 thread_mtx_unlock(thread
);
2179 return (KERN_FAILURE
);
2182 if (thread
->exc_actions
== NULL
) {
2183 ipc_thread_init_exc_actions(thread
);
2185 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; ++i
) {
2186 if ((exception_mask
& (1 << i
))
2188 && mac_exc_update_action_label(&thread
->exc_actions
[i
], new_label
) == 0
2191 old_port
[i
] = thread
->exc_actions
[i
].port
;
2192 thread
->exc_actions
[i
].port
= ipc_port_copy_send(new_port
);
2193 thread
->exc_actions
[i
].behavior
= new_behavior
;
2194 thread
->exc_actions
[i
].flavor
= new_flavor
;
2195 thread
->exc_actions
[i
].privileged
= privileged
;
2198 old_port
[i
] = IP_NULL
;
2201 thread_mtx_unlock(thread
);
2204 mac_exc_free_label(new_label
);
2207 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; ++i
)
2208 if (IP_VALID(old_port
[i
]))
2209 ipc_port_release_send(old_port
[i
]);
2211 if (IP_VALID(new_port
)) /* consume send right */
2212 ipc_port_release_send(new_port
);
2214 return (KERN_SUCCESS
);
2218 task_set_exception_ports(
2220 exception_mask_t exception_mask
,
2221 ipc_port_t new_port
,
2222 exception_behavior_t new_behavior
,
2223 thread_state_flavor_t new_flavor
)
2225 ipc_port_t old_port
[EXC_TYPES_COUNT
];
2226 boolean_t privileged
= current_task()->sec_token
.val
[0] == 0;
2230 struct label
*new_label
;
2233 if (task
== TASK_NULL
)
2234 return (KERN_INVALID_ARGUMENT
);
2236 if (exception_mask
& ~EXC_MASK_VALID
)
2237 return (KERN_INVALID_ARGUMENT
);
2239 if (IP_VALID(new_port
)) {
2240 switch (new_behavior
& ~MACH_EXCEPTION_CODES
) {
2242 case EXCEPTION_DEFAULT
:
2243 case EXCEPTION_STATE
:
2244 case EXCEPTION_STATE_IDENTITY
:
2248 return (KERN_INVALID_ARGUMENT
);
2253 * Check the validity of the thread_state_flavor by calling the
2254 * VALID_THREAD_STATE_FLAVOR architecture dependent macro defined in
2255 * osfmk/mach/ARCHITECTURE/thread_status.h
2257 if (new_flavor
!= 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor
))
2258 return (KERN_INVALID_ARGUMENT
);
2261 new_label
= mac_exc_create_label_for_current_proc();
2266 if (task
->itk_self
== IP_NULL
) {
2269 return (KERN_FAILURE
);
2272 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; ++i
) {
2273 if ((exception_mask
& (1 << i
))
2275 && mac_exc_update_action_label(&task
->exc_actions
[i
], new_label
) == 0
2278 old_port
[i
] = task
->exc_actions
[i
].port
;
2279 task
->exc_actions
[i
].port
=
2280 ipc_port_copy_send(new_port
);
2281 task
->exc_actions
[i
].behavior
= new_behavior
;
2282 task
->exc_actions
[i
].flavor
= new_flavor
;
2283 task
->exc_actions
[i
].privileged
= privileged
;
2286 old_port
[i
] = IP_NULL
;
2292 mac_exc_free_label(new_label
);
2295 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; ++i
)
2296 if (IP_VALID(old_port
[i
]))
2297 ipc_port_release_send(old_port
[i
]);
2299 if (IP_VALID(new_port
)) /* consume send right */
2300 ipc_port_release_send(new_port
);
2302 return (KERN_SUCCESS
);
2306 * Routine: thread/task_swap_exception_ports [kernel call]
2308 * Sets the thread/task exception port, flavor and
2309 * behavior for the exception types specified by the
2312 * The old ports, behavior and flavors are returned
2313 * Count specifies the array sizes on input and
2314 * the number of returned ports etc. on output. The
2315 * arrays must be large enough to hold all the returned
2316 * data, MIG returnes an error otherwise. The masks
2317 * array specifies the corresponding exception type(s).
2320 * Nothing locked. If successful, consumes
2321 * the supplied send right.
2323 * Returns upto [in} CountCnt elements.
2325 * KERN_SUCCESS Changed the special port.
2326 * KERN_INVALID_ARGUMENT The thread is null,
2327 * Illegal mask bit set.
2328 * Illegal exception behavior
2329 * KERN_FAILURE The thread is dead.
2333 thread_swap_exception_ports(
2335 exception_mask_t exception_mask
,
2336 ipc_port_t new_port
,
2337 exception_behavior_t new_behavior
,
2338 thread_state_flavor_t new_flavor
,
2339 exception_mask_array_t masks
,
2340 mach_msg_type_number_t
*CountCnt
,
2341 exception_port_array_t ports
,
2342 exception_behavior_array_t behaviors
,
2343 thread_state_flavor_array_t flavors
)
2345 ipc_port_t old_port
[EXC_TYPES_COUNT
];
2346 boolean_t privileged
= current_task()->sec_token
.val
[0] == 0;
2347 unsigned int i
, j
, count
;
2350 struct label
*new_label
;
2353 if (thread
== THREAD_NULL
)
2354 return (KERN_INVALID_ARGUMENT
);
2356 if (exception_mask
& ~EXC_MASK_VALID
)
2357 return (KERN_INVALID_ARGUMENT
);
2359 if (IP_VALID(new_port
)) {
2360 switch (new_behavior
& ~MACH_EXCEPTION_CODES
) {
2362 case EXCEPTION_DEFAULT
:
2363 case EXCEPTION_STATE
:
2364 case EXCEPTION_STATE_IDENTITY
:
2368 return (KERN_INVALID_ARGUMENT
);
2372 if (new_flavor
!= 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor
))
2373 return (KERN_INVALID_ARGUMENT
);
2376 new_label
= mac_exc_create_label_for_current_proc();
2379 thread_mtx_lock(thread
);
2381 if (!thread
->active
) {
2382 thread_mtx_unlock(thread
);
2384 return (KERN_FAILURE
);
2387 if (thread
->exc_actions
== NULL
) {
2388 ipc_thread_init_exc_actions(thread
);
2391 assert(EXC_TYPES_COUNT
> FIRST_EXCEPTION
);
2392 for (count
= 0, i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
&& count
< *CountCnt
; ++i
) {
2393 if ((exception_mask
& (1 << i
))
2395 && mac_exc_update_action_label(&thread
->exc_actions
[i
], new_label
) == 0
2398 for (j
= 0; j
< count
; ++j
) {
2400 * search for an identical entry, if found
2401 * set corresponding mask for this exception.
2403 if ( thread
->exc_actions
[i
].port
== ports
[j
] &&
2404 thread
->exc_actions
[i
].behavior
== behaviors
[j
] &&
2405 thread
->exc_actions
[i
].flavor
== flavors
[j
] ) {
2406 masks
[j
] |= (1 << i
);
2412 masks
[j
] = (1 << i
);
2413 ports
[j
] = ipc_port_copy_send(thread
->exc_actions
[i
].port
);
2415 behaviors
[j
] = thread
->exc_actions
[i
].behavior
;
2416 flavors
[j
] = thread
->exc_actions
[i
].flavor
;
2420 old_port
[i
] = thread
->exc_actions
[i
].port
;
2421 thread
->exc_actions
[i
].port
= ipc_port_copy_send(new_port
);
2422 thread
->exc_actions
[i
].behavior
= new_behavior
;
2423 thread
->exc_actions
[i
].flavor
= new_flavor
;
2424 thread
->exc_actions
[i
].privileged
= privileged
;
2427 old_port
[i
] = IP_NULL
;
2430 thread_mtx_unlock(thread
);
2433 mac_exc_free_label(new_label
);
2436 while (--i
>= FIRST_EXCEPTION
) {
2437 if (IP_VALID(old_port
[i
]))
2438 ipc_port_release_send(old_port
[i
]);
2441 if (IP_VALID(new_port
)) /* consume send right */
2442 ipc_port_release_send(new_port
);
2446 return (KERN_SUCCESS
);
2450 task_swap_exception_ports(
2452 exception_mask_t exception_mask
,
2453 ipc_port_t new_port
,
2454 exception_behavior_t new_behavior
,
2455 thread_state_flavor_t new_flavor
,
2456 exception_mask_array_t masks
,
2457 mach_msg_type_number_t
*CountCnt
,
2458 exception_port_array_t ports
,
2459 exception_behavior_array_t behaviors
,
2460 thread_state_flavor_array_t flavors
)
2462 ipc_port_t old_port
[EXC_TYPES_COUNT
];
2463 boolean_t privileged
= current_task()->sec_token
.val
[0] == 0;
2464 unsigned int i
, j
, count
;
2467 struct label
*new_label
;
2470 if (task
== TASK_NULL
)
2471 return (KERN_INVALID_ARGUMENT
);
2473 if (exception_mask
& ~EXC_MASK_VALID
)
2474 return (KERN_INVALID_ARGUMENT
);
2476 if (IP_VALID(new_port
)) {
2477 switch (new_behavior
& ~MACH_EXCEPTION_CODES
) {
2479 case EXCEPTION_DEFAULT
:
2480 case EXCEPTION_STATE
:
2481 case EXCEPTION_STATE_IDENTITY
:
2485 return (KERN_INVALID_ARGUMENT
);
2489 if (new_flavor
!= 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor
))
2490 return (KERN_INVALID_ARGUMENT
);
2493 new_label
= mac_exc_create_label_for_current_proc();
2498 if (task
->itk_self
== IP_NULL
) {
2501 return (KERN_FAILURE
);
2504 assert(EXC_TYPES_COUNT
> FIRST_EXCEPTION
);
2505 for (count
= 0, i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
&& count
< *CountCnt
; ++i
) {
2506 if ((exception_mask
& (1 << i
))
2508 && mac_exc_update_action_label(&task
->exc_actions
[i
], new_label
) == 0
2511 for (j
= 0; j
< count
; j
++) {
2513 * search for an identical entry, if found
2514 * set corresponding mask for this exception.
2516 if ( task
->exc_actions
[i
].port
== ports
[j
] &&
2517 task
->exc_actions
[i
].behavior
== behaviors
[j
] &&
2518 task
->exc_actions
[i
].flavor
== flavors
[j
] ) {
2519 masks
[j
] |= (1 << i
);
2525 masks
[j
] = (1 << i
);
2526 ports
[j
] = ipc_port_copy_send(task
->exc_actions
[i
].port
);
2527 behaviors
[j
] = task
->exc_actions
[i
].behavior
;
2528 flavors
[j
] = task
->exc_actions
[i
].flavor
;
2532 old_port
[i
] = task
->exc_actions
[i
].port
;
2534 task
->exc_actions
[i
].port
= ipc_port_copy_send(new_port
);
2535 task
->exc_actions
[i
].behavior
= new_behavior
;
2536 task
->exc_actions
[i
].flavor
= new_flavor
;
2537 task
->exc_actions
[i
].privileged
= privileged
;
2540 old_port
[i
] = IP_NULL
;
2546 mac_exc_free_label(new_label
);
2549 while (--i
>= FIRST_EXCEPTION
) {
2550 if (IP_VALID(old_port
[i
]))
2551 ipc_port_release_send(old_port
[i
]);
2554 if (IP_VALID(new_port
)) /* consume send right */
2555 ipc_port_release_send(new_port
);
2559 return (KERN_SUCCESS
);
2563 * Routine: thread/task_get_exception_ports [kernel call]
2565 * Clones a send right for each of the thread/task's exception
2566 * ports specified in the mask and returns the behaviour
2567 * and flavor of said port.
2569 * Returns upto [in} CountCnt elements.
2574 * KERN_SUCCESS Extracted a send right.
2575 * KERN_INVALID_ARGUMENT The thread is null,
2576 * Invalid special port,
2577 * Illegal mask bit set.
2578 * KERN_FAILURE The thread is dead.
2582 thread_get_exception_ports(
2584 exception_mask_t exception_mask
,
2585 exception_mask_array_t masks
,
2586 mach_msg_type_number_t
*CountCnt
,
2587 exception_port_array_t ports
,
2588 exception_behavior_array_t behaviors
,
2589 thread_state_flavor_array_t flavors
)
2591 unsigned int i
, j
, count
;
2593 if (thread
== THREAD_NULL
)
2594 return (KERN_INVALID_ARGUMENT
);
2596 if (exception_mask
& ~EXC_MASK_VALID
)
2597 return (KERN_INVALID_ARGUMENT
);
2599 thread_mtx_lock(thread
);
2601 if (!thread
->active
) {
2602 thread_mtx_unlock(thread
);
2604 return (KERN_FAILURE
);
2609 if (thread
->exc_actions
== NULL
) {
2613 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; ++i
) {
2614 if (exception_mask
& (1 << i
)) {
2615 for (j
= 0; j
< count
; ++j
) {
2617 * search for an identical entry, if found
2618 * set corresponding mask for this exception.
2620 if ( thread
->exc_actions
[i
].port
== ports
[j
] &&
2621 thread
->exc_actions
[i
].behavior
==behaviors
[j
] &&
2622 thread
->exc_actions
[i
].flavor
== flavors
[j
] ) {
2623 masks
[j
] |= (1 << i
);
2629 masks
[j
] = (1 << i
);
2630 ports
[j
] = ipc_port_copy_send(thread
->exc_actions
[i
].port
);
2631 behaviors
[j
] = thread
->exc_actions
[i
].behavior
;
2632 flavors
[j
] = thread
->exc_actions
[i
].flavor
;
2634 if (count
>= *CountCnt
)
2641 thread_mtx_unlock(thread
);
2645 return (KERN_SUCCESS
);
2649 task_get_exception_ports(
2651 exception_mask_t exception_mask
,
2652 exception_mask_array_t masks
,
2653 mach_msg_type_number_t
*CountCnt
,
2654 exception_port_array_t ports
,
2655 exception_behavior_array_t behaviors
,
2656 thread_state_flavor_array_t flavors
)
2658 unsigned int i
, j
, count
;
2660 if (task
== TASK_NULL
)
2661 return (KERN_INVALID_ARGUMENT
);
2663 if (exception_mask
& ~EXC_MASK_VALID
)
2664 return (KERN_INVALID_ARGUMENT
);
2668 if (task
->itk_self
== IP_NULL
) {
2671 return (KERN_FAILURE
);
2676 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; ++i
) {
2677 if (exception_mask
& (1 << i
)) {
2678 for (j
= 0; j
< count
; ++j
) {
2680 * search for an identical entry, if found
2681 * set corresponding mask for this exception.
2683 if ( task
->exc_actions
[i
].port
== ports
[j
] &&
2684 task
->exc_actions
[i
].behavior
== behaviors
[j
] &&
2685 task
->exc_actions
[i
].flavor
== flavors
[j
] ) {
2686 masks
[j
] |= (1 << i
);
2692 masks
[j
] = (1 << i
);
2693 ports
[j
] = ipc_port_copy_send(task
->exc_actions
[i
].port
);
2694 behaviors
[j
] = task
->exc_actions
[i
].behavior
;
2695 flavors
[j
] = task
->exc_actions
[i
].flavor
;
2697 if (count
> *CountCnt
)
2707 return (KERN_SUCCESS
);