]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/ipc_tt.c
xnu-4570.71.2.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_tt.c
index 04071f778fa10baa83c80dffef08e0c7971d190a..920ac8fc5829471532ce2cfdd40c68b4dd835b87 100644 (file)
@@ -1,23 +1,29 @@
 /*
 /*
- * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
  *
  *
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
  * 
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
  * 
- * @APPLE_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
  * @OSF_COPYRIGHT@
  */
 /*
  * @OSF_COPYRIGHT@
  * any improvements or extensions that they make and grant Carnegie Mellon
  * the rights to redistribute these changes.
  */
  * any improvements or extensions that they make and grant Carnegie Mellon
  * the rights to redistribute these changes.
  */
+/*
+ * NOTICE: This file was modified by McAfee Research in 2004 to introduce
+ * support for mandatory and extensible security protections.  This notice
+ * is included in support of clause 2.2 (b) of the Apple Public License,
+ * Version 2.0.
+ */
 /*
  */
 
 /*
  */
 
 
 #include <mach/mach_types.h>
 #include <mach/boolean.h>
 
 #include <mach/mach_types.h>
 #include <mach/boolean.h>
-#include <mach_rt.h>
 #include <mach/kern_return.h>
 #include <mach/mach_param.h>
 #include <mach/task_special_ports.h>
 #include <mach/thread_special_ports.h>
 #include <mach/thread_status.h>
 #include <mach/exception_types.h>
 #include <mach/kern_return.h>
 #include <mach/mach_param.h>
 #include <mach/task_special_ports.h>
 #include <mach/thread_special_ports.h>
 #include <mach/thread_status.h>
 #include <mach/exception_types.h>
+#include <mach/memory_object_types.h>
 #include <mach/mach_traps.h>
 #include <mach/task_server.h>
 #include <mach/thread_act_server.h>
 #include <mach/mach_host_server.h>
 #include <mach/mach_traps.h>
 #include <mach/task_server.h>
 #include <mach/thread_act_server.h>
 #include <mach/mach_host_server.h>
+#include <mach/host_priv_server.h>
 #include <mach/vm_map_server.h>
 #include <mach/vm_map_server.h>
+
+#include <kern/kern_types.h>
 #include <kern/host.h>
 #include <kern/host.h>
+#include <kern/ipc_kobject.h>
 #include <kern/ipc_tt.h>
 #include <kern/ipc_tt.h>
-#include <kern/thread_act.h>
+#include <kern/kalloc.h>
+#include <kern/thread.h>
 #include <kern/misc_protos.h>
 #include <kern/misc_protos.h>
+
+#include <vm/vm_map.h>
 #include <vm/vm_pageout.h>
 #include <vm/vm_pageout.h>
+#include <vm/vm_protos.h>
+
+#include <security/mac_mach_internal.h>
+
+#if CONFIG_EMBEDDED && !SECURE_KERNEL
+extern int cs_relax_platform_task_ports;
+#endif
+
+/* forward declarations */
+task_t convert_port_to_locked_task(ipc_port_t port);
+task_inspect_t convert_port_to_locked_task_inspect(ipc_port_t port);
+static void ipc_port_bind_special_reply_port_locked(ipc_port_t port);
+static kern_return_t ipc_port_unbind_special_reply_port(thread_t thread, boolean_t unbind_active_port);
+kern_return_t task_conversion_eval(task_t caller, task_t victim);
 
 /*
  *     Routine:        ipc_task_init
 
 /*
  *     Routine:        ipc_task_init
@@ -94,6 +127,7 @@ ipc_task_init(
 {
        ipc_space_t space;
        ipc_port_t kport;
 {
        ipc_space_t space;
        ipc_port_t kport;
+       ipc_port_t nport;
        kern_return_t kr;
        int i;
 
        kern_return_t kr;
        int i;
 
@@ -102,17 +136,38 @@ ipc_task_init(
        if (kr != KERN_SUCCESS)
                panic("ipc_task_init");
 
        if (kr != KERN_SUCCESS)
                panic("ipc_task_init");
 
+       space->is_task = task;
 
        kport = ipc_port_alloc_kernel();
        if (kport == IP_NULL)
                panic("ipc_task_init");
 
 
        kport = ipc_port_alloc_kernel();
        if (kport == IP_NULL)
                panic("ipc_task_init");
 
+       nport = ipc_port_alloc_kernel();
+       if (nport == IP_NULL)
+               panic("ipc_task_init");
+
        itk_lock_init(task);
        task->itk_self = kport;
        itk_lock_init(task);
        task->itk_self = kport;
-       task->itk_sself = ipc_port_make_send(kport);
+       task->itk_nself = nport;
+       task->itk_resume = IP_NULL; /* Lazily allocated on-demand */
+       if (task_is_a_corpse_fork(task)) {
+               /*
+                * No sender's notification for corpse would not
+                * work with a naked send right in kernel.
+                */
+               task->itk_sself = IP_NULL;
+       } else {
+               task->itk_sself = ipc_port_make_send(kport);
+       }
+       task->itk_debug_control = IP_NULL;
        task->itk_space = space;
        task->itk_space = space;
-       space->is_fast = FALSE;
 
 
+#if CONFIG_MACF
+       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) {
+               mac_exc_associate_action_label(&task->exc_actions[i], mac_exc_create_label());
+       }
+#endif
+       
        if (parent == TASK_NULL) {
                ipc_port_t port;
 
        if (parent == TASK_NULL) {
                ipc_port_t port;
 
@@ -125,6 +180,9 @@ ipc_task_init(
                task->itk_host = port;
 
                task->itk_bootstrap = IP_NULL;
                task->itk_host = port;
 
                task->itk_bootstrap = IP_NULL;
+               task->itk_seatbelt = IP_NULL;
+               task->itk_gssd = IP_NULL;
+               task->itk_task_access = IP_NULL;
 
                for (i = 0; i < TASK_PORT_REGISTER_MAX; i++)
                        task->itk_registered[i] = IP_NULL;
 
                for (i = 0; i < TASK_PORT_REGISTER_MAX; i++)
                        task->itk_registered[i] = IP_NULL;
@@ -147,6 +205,11 @@ ipc_task_init(
                                parent->exc_actions[i].flavor;
                    task->exc_actions[i].behavior = 
                                parent->exc_actions[i].behavior;
                                parent->exc_actions[i].flavor;
                    task->exc_actions[i].behavior = 
                                parent->exc_actions[i].behavior;
+                   task->exc_actions[i].privileged =
+                               parent->exc_actions[i].privileged;
+#if CONFIG_MACF
+                   mac_exc_inherit_action_label(parent->exc_actions + i, task->exc_actions + i);
+#endif
                }/* for */
                task->itk_host =
                        ipc_port_copy_send(parent->itk_host);
                }/* for */
                task->itk_host =
                        ipc_port_copy_send(parent->itk_host);
@@ -154,6 +217,15 @@ ipc_task_init(
                task->itk_bootstrap =
                        ipc_port_copy_send(parent->itk_bootstrap);
 
                task->itk_bootstrap =
                        ipc_port_copy_send(parent->itk_bootstrap);
 
+               task->itk_seatbelt =
+                       ipc_port_copy_send(parent->itk_seatbelt);
+
+               task->itk_gssd =
+                       ipc_port_copy_send(parent->itk_gssd);
+
+               task->itk_task_access =
+                       ipc_port_copy_send(parent->itk_task_access);
+
                itk_unlock(parent);
        }
 }
                itk_unlock(parent);
        }
 }
@@ -171,11 +243,15 @@ ipc_task_enable(
        task_t          task)
 {
        ipc_port_t kport;
        task_t          task)
 {
        ipc_port_t kport;
+       ipc_port_t nport;
 
        itk_lock(task);
        kport = task->itk_self;
        if (kport != IP_NULL)
                ipc_kobject_set(kport, (ipc_kobject_t) task, IKOT_TASK);
 
        itk_lock(task);
        kport = task->itk_self;
        if (kport != IP_NULL)
                ipc_kobject_set(kport, (ipc_kobject_t) task, IKOT_TASK);
+       nport = task->itk_nself;
+       if (nport != IP_NULL)
+               ipc_kobject_set(nport, (ipc_kobject_t) task, IKOT_TASK_NAME);
        itk_unlock(task);
 }
 
        itk_unlock(task);
 }
 
@@ -192,11 +268,33 @@ ipc_task_disable(
        task_t          task)
 {
        ipc_port_t kport;
        task_t          task)
 {
        ipc_port_t kport;
+       ipc_port_t nport;
+       ipc_port_t rport;
 
        itk_lock(task);
        kport = task->itk_self;
        if (kport != IP_NULL)
                ipc_kobject_set(kport, IKO_NULL, IKOT_NONE);
 
        itk_lock(task);
        kport = task->itk_self;
        if (kport != IP_NULL)
                ipc_kobject_set(kport, IKO_NULL, IKOT_NONE);
+       nport = task->itk_nself;
+       if (nport != IP_NULL)
+               ipc_kobject_set(nport, IKO_NULL, IKOT_NONE);
+
+       rport = task->itk_resume;
+       if (rport != IP_NULL) {
+               /*
+                * From this point onwards this task is no longer accepting
+                * resumptions.
+                *
+                * There are still outstanding suspensions on this task,
+                * even as it is being torn down. Disconnect the task
+                * from the rport, thereby "orphaning" the rport. The rport 
+                * itself will go away only when the last suspension holder
+                * destroys his SO right to it -- when he either
+                * exits, or tries to actually use that last SO right to
+                * resume this (now non-existent) task.
+                */
+               ipc_kobject_set(rport, IKO_NULL, IKOT_NONE);
+       }
        itk_unlock(task);
 }
 
        itk_unlock(task);
 }
 
@@ -214,6 +312,8 @@ ipc_task_terminate(
        task_t          task)
 {
        ipc_port_t kport;
        task_t          task)
 {
        ipc_port_t kport;
+       ipc_port_t nport;
+       ipc_port_t rport;       
        int i;
 
        itk_lock(task);
        int i;
 
        itk_lock(task);
@@ -224,8 +324,15 @@ ipc_task_terminate(
                itk_unlock(task);
                return;
        }
                itk_unlock(task);
                return;
        }
-
        task->itk_self = IP_NULL;
        task->itk_self = IP_NULL;
+
+       nport = task->itk_nself;
+       assert(nport != IP_NULL);
+       task->itk_nself = IP_NULL;
+
+       rport = task->itk_resume;
+       task->itk_resume = IP_NULL;
+
        itk_unlock(task);
 
        /* release the naked send rights */
        itk_unlock(task);
 
        /* release the naked send rights */
@@ -237,29 +344,49 @@ ipc_task_terminate(
                if (IP_VALID(task->exc_actions[i].port)) {
                        ipc_port_release_send(task->exc_actions[i].port);
                }
                if (IP_VALID(task->exc_actions[i].port)) {
                        ipc_port_release_send(task->exc_actions[i].port);
                }
-       }/* for */
+#if CONFIG_MACF
+               mac_exc_free_action_label(task->exc_actions + i);
+#endif
+       }
+
        if (IP_VALID(task->itk_host))
                ipc_port_release_send(task->itk_host);
 
        if (IP_VALID(task->itk_bootstrap))
                ipc_port_release_send(task->itk_bootstrap);
 
        if (IP_VALID(task->itk_host))
                ipc_port_release_send(task->itk_host);
 
        if (IP_VALID(task->itk_bootstrap))
                ipc_port_release_send(task->itk_bootstrap);
 
+       if (IP_VALID(task->itk_seatbelt))
+               ipc_port_release_send(task->itk_seatbelt);
+       
+       if (IP_VALID(task->itk_gssd))
+               ipc_port_release_send(task->itk_gssd);
+
+       if (IP_VALID(task->itk_task_access))
+               ipc_port_release_send(task->itk_task_access);
+
+       if (IP_VALID(task->itk_debug_control))
+               ipc_port_release_send(task->itk_debug_control);
+
        for (i = 0; i < TASK_PORT_REGISTER_MAX; i++)
                if (IP_VALID(task->itk_registered[i]))
                        ipc_port_release_send(task->itk_registered[i]);
 
        for (i = 0; i < TASK_PORT_REGISTER_MAX; i++)
                if (IP_VALID(task->itk_registered[i]))
                        ipc_port_release_send(task->itk_registered[i]);
 
-       ipc_port_release_send(task->wired_ledger_port);
-       ipc_port_release_send(task->paged_ledger_port);
-
-       /* destroy the kernel port */
+       /* destroy the kernel ports */
        ipc_port_dealloc_kernel(kport);
        ipc_port_dealloc_kernel(kport);
+       ipc_port_dealloc_kernel(nport);
+       if (rport != IP_NULL)
+               ipc_port_dealloc_kernel(rport); 
+
+       itk_lock_destroy(task);
 }
 
 /*
  *     Routine:        ipc_task_reset
  *     Purpose:
  *             Reset a task's IPC state to protect it when
 }
 
 /*
  *     Routine:        ipc_task_reset
  *     Purpose:
  *             Reset a task's IPC state to protect it when
- *             it enters an elevated security context.
+ *             it enters an elevated security context. The
+ *             task name port can remain the same - since
+ *             it represents no specific privilege.
  *     Conditions:
  *             Nothing locked.  The task must be suspended.
  *             (Or the current thread must be in the task.)
  *     Conditions:
  *             Nothing locked.  The task must be suspended.
  *             (Or the current thread must be in the task.)
@@ -271,11 +398,14 @@ ipc_task_reset(
 {
        ipc_port_t old_kport, new_kport;
        ipc_port_t old_sself;
 {
        ipc_port_t old_kport, new_kport;
        ipc_port_t old_sself;
-#if 0
        ipc_port_t old_exc_actions[EXC_TYPES_COUNT];
        int i;
        ipc_port_t old_exc_actions[EXC_TYPES_COUNT];
        int i;
-#endif
 
 
+#if CONFIG_MACF
+       /* Fresh label to unset credentials in existing labels. */
+       struct label *unset_label = mac_exc_create_label();
+#endif
+       
        new_kport = ipc_port_alloc_kernel();
        if (new_kport == IP_NULL)
                panic("ipc_task_reset");
        new_kport = ipc_port_alloc_kernel();
        if (new_kport == IP_NULL)
                panic("ipc_task_reset");
@@ -288,36 +418,61 @@ ipc_task_reset(
                /* the task is already terminated (can this happen?) */
                itk_unlock(task);
                ipc_port_dealloc_kernel(new_kport);
                /* the task is already terminated (can this happen?) */
                itk_unlock(task);
                ipc_port_dealloc_kernel(new_kport);
+#if CONFIG_MACF
+               mac_exc_free_label(unset_label);
+#endif
                return;
        }
 
        task->itk_self = new_kport;
        old_sself = task->itk_sself;
        task->itk_sself = ipc_port_make_send(new_kport);
                return;
        }
 
        task->itk_self = new_kport;
        old_sself = task->itk_sself;
        task->itk_sself = ipc_port_make_send(new_kport);
-       ipc_kobject_set(old_kport, IKO_NULL, IKOT_NONE);
+
+       /* Set the old kport to IKOT_NONE and update the exec token while under the port lock */
+       ip_lock(old_kport);
+       ipc_kobject_set_atomically(old_kport, IKO_NULL, IKOT_NONE);
+       task->exec_token += 1;
+       ip_unlock(old_kport);
+
        ipc_kobject_set(new_kport, (ipc_kobject_t) task, IKOT_TASK);
 
        ipc_kobject_set(new_kport, (ipc_kobject_t) task, IKOT_TASK);
 
-#if 0
        for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) {
        for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) {
-               old_exc_actions[i] = task->exc_action[i].port;
-               task->exc_actions[i].port = IP_NULL;
-       }/* for */
-#endif
+               old_exc_actions[i] = IP_NULL;
+
+               if (i == EXC_CORPSE_NOTIFY && task_corpse_pending_report(task)) {
+                       continue;
+               }
 
 
+               if (!task->exc_actions[i].privileged) {
+#if CONFIG_MACF
+                       mac_exc_update_action_label(task->exc_actions + i, unset_label);
+#endif
+                       old_exc_actions[i] = task->exc_actions[i].port;
+                       task->exc_actions[i].port = IP_NULL;
+               }
+       }/* for */
+       
+       if (IP_VALID(task->itk_debug_control)) {
+               ipc_port_release_send(task->itk_debug_control);
+       }
+       task->itk_debug_control = IP_NULL;
+       
        itk_unlock(task);
 
        itk_unlock(task);
 
+#if CONFIG_MACF
+       mac_exc_free_label(unset_label);
+#endif
+
        /* release the naked send rights */
 
        if (IP_VALID(old_sself))
                ipc_port_release_send(old_sself);
 
        /* release the naked send rights */
 
        if (IP_VALID(old_sself))
                ipc_port_release_send(old_sself);
 
-#if 0
        for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) {
                if (IP_VALID(old_exc_actions[i])) {
                        ipc_port_release_send(old_exc_actions[i]);
                }
        }/* for */
        for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) {
                if (IP_VALID(old_exc_actions[i])) {
                        ipc_port_release_send(old_exc_actions[i]);
                }
        }/* for */
-#endif
 
        /* destroy the kernel port */
        ipc_port_dealloc_kernel(old_kport);
 
        /* destroy the kernel port */
        ipc_port_dealloc_kernel(old_kport);
@@ -335,94 +490,218 @@ void
 ipc_thread_init(
        thread_t        thread)
 {
 ipc_thread_init(
        thread_t        thread)
 {
+       ipc_port_t      kport;
+
+       kport = ipc_port_alloc_kernel();
+       if (kport == IP_NULL)
+               panic("ipc_thread_init");
+
+       thread->ith_self = kport;
+       thread->ith_sself = ipc_port_make_send(kport);
+       thread->ith_special_reply_port = NULL;
+       thread->exc_actions = NULL;
+
+       ipc_kobject_set(kport, (ipc_kobject_t)thread, IKOT_THREAD);
+
+#if IMPORTANCE_INHERITANCE
+       thread->ith_assertions = 0;
+#endif
+
        ipc_kmsg_queue_init(&thread->ith_messages);
        ipc_kmsg_queue_init(&thread->ith_messages);
-       thread->ith_mig_reply = MACH_PORT_NULL;
+
        thread->ith_rpc_reply = IP_NULL;
 }
 
        thread->ith_rpc_reply = IP_NULL;
 }
 
+void
+ipc_thread_init_exc_actions(
+       thread_t        thread)
+{
+       assert(thread->exc_actions == NULL);
+
+       thread->exc_actions = kalloc(sizeof(struct exception_action) * EXC_TYPES_COUNT);
+       bzero(thread->exc_actions, sizeof(struct exception_action) * EXC_TYPES_COUNT);
+
+#if CONFIG_MACF
+       for (size_t i = 0; i < EXC_TYPES_COUNT; ++i) {
+               mac_exc_associate_action_label(thread->exc_actions + i, mac_exc_create_label());
+       }
+#endif
+}
+
+void
+ipc_thread_destroy_exc_actions(
+       thread_t        thread)
+{
+       if (thread->exc_actions != NULL) {
+#if CONFIG_MACF
+               for (size_t i = 0; i < EXC_TYPES_COUNT; ++i) {
+                       mac_exc_free_action_label(thread->exc_actions + i);
+               }
+#endif
+
+               kfree(thread->exc_actions, 
+                     sizeof(struct exception_action) * EXC_TYPES_COUNT);
+               thread->exc_actions = NULL;
+       }
+}
+
+void
+ipc_thread_disable(
+       thread_t        thread)
+{
+       ipc_port_t      kport = thread->ith_self;
+
+       if (kport != IP_NULL)
+               ipc_kobject_set(kport, IKO_NULL, IKOT_NONE);
+}
+
 /*
  *     Routine:        ipc_thread_terminate
  *     Purpose:
  *             Clean up and destroy a thread's IPC state.
  *     Conditions:
 /*
  *     Routine:        ipc_thread_terminate
  *     Purpose:
  *             Clean up and destroy a thread's IPC state.
  *     Conditions:
- *             Nothing locked.  The thread must be suspended.
- *             (Or be the current thread.)
+ *             Nothing locked.
  */
 
 void
 ipc_thread_terminate(
        thread_t        thread)
 {
  */
 
 void
 ipc_thread_terminate(
        thread_t        thread)
 {
+       ipc_port_t      kport = thread->ith_self;
+
+       if (kport != IP_NULL) {
+               int                     i;
+
+               if (IP_VALID(thread->ith_sself))
+                       ipc_port_release_send(thread->ith_sself);
+
+               thread->ith_sself = thread->ith_self = IP_NULL;
+
+               if (thread->exc_actions != NULL) {
+                       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; ++i) {
+                               if (IP_VALID(thread->exc_actions[i].port))
+                                       ipc_port_release_send(thread->exc_actions[i].port);
+                       }
+                       ipc_thread_destroy_exc_actions(thread);
+               }
+
+               ipc_port_dealloc_kernel(kport);
+       }
+
+#if IMPORTANCE_INHERITANCE
+       assert(thread->ith_assertions == 0);
+#endif
+
+       /* unbind the thread special reply port */
+       if (IP_VALID(thread->ith_special_reply_port)) {
+               ipc_port_unbind_special_reply_port(thread, TRUE);
+       }
+
        assert(ipc_kmsg_queue_empty(&thread->ith_messages));
 
        assert(ipc_kmsg_queue_empty(&thread->ith_messages));
 
-        if (thread->ith_rpc_reply != IP_NULL)
-            ipc_port_dealloc_reply(thread->ith_rpc_reply);
+       if (thread->ith_rpc_reply != IP_NULL)
+               ipc_port_dealloc_reply(thread->ith_rpc_reply);
+
        thread->ith_rpc_reply = IP_NULL;
 }
 
 /*
        thread->ith_rpc_reply = IP_NULL;
 }
 
 /*
- *     Routine:        ipc_thr_act_init
+ *     Routine:        ipc_thread_reset
  *     Purpose:
  *     Purpose:
- *             Initialize an thr_act's IPC state.
+ *             Reset the IPC state for a given Mach thread when
+ *             its task enters an elevated security context.
+ *             Both the thread port and its exception ports have
+ *             to be reset.  Its RPC reply port cannot have any
+ *             rights outstanding, so it should be fine.
  *     Conditions:
  *             Nothing locked.
  */
 
 void
  *     Conditions:
  *             Nothing locked.
  */
 
 void
-ipc_thr_act_init(task_t task, thread_act_t thr_act)
+ipc_thread_reset(
+       thread_t        thread)
 {
 {
-       ipc_port_t kport; int i;
-
-       kport = ipc_port_alloc_kernel();
-       if (kport == IP_NULL)
-               panic("ipc_thr_act_init");
-
-       thr_act->ith_self = kport;
-       thr_act->ith_sself = ipc_port_make_send(kport);
-
-       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++)
-               thr_act->exc_actions[i].port = IP_NULL;
-
-       ipc_kobject_set(kport, (ipc_kobject_t) thr_act, IKOT_ACT);
-}
+       ipc_port_t old_kport, new_kport;
+       ipc_port_t old_sself;
+       ipc_port_t old_exc_actions[EXC_TYPES_COUNT];
+       boolean_t  has_old_exc_actions = FALSE; 
+       int                i;
 
 
-void
-ipc_thr_act_disable(thread_act_t thr_act)
-{
-       int i;
-       ipc_port_t kport;
+#if CONFIG_MACF
+       struct label *new_label = mac_exc_create_label();
+#endif
+       
+       new_kport = ipc_port_alloc_kernel();
+       if (new_kport == IP_NULL)
+               panic("ipc_task_reset");
 
 
-       kport = thr_act->ith_self;
+       thread_mtx_lock(thread);
 
 
-       if (kport != IP_NULL)
-               ipc_kobject_set(kport, IKO_NULL, IKOT_NONE);
-}
+       old_kport = thread->ith_self;
 
 
-void
-ipc_thr_act_terminate(thread_act_t thr_act)
-{
-       ipc_port_t kport; int i;
+       if (old_kport == IP_NULL && thread->inspection == FALSE) {
+               /* the  is already terminated (can this happen?) */
+               thread_mtx_unlock(thread);
+               ipc_port_dealloc_kernel(new_kport);
+#if CONFIG_MACF
+               mac_exc_free_label(new_label);
+#endif
+               return;
+       }
 
 
-       kport = thr_act->ith_self;
+       thread->ith_self = new_kport;
+       old_sself = thread->ith_sself;
+       thread->ith_sself = ipc_port_make_send(new_kport);
+       if (old_kport != IP_NULL) {
+               ipc_kobject_set(old_kport, IKO_NULL, IKOT_NONE);
+       }
+       ipc_kobject_set(new_kport, (ipc_kobject_t) thread, IKOT_THREAD);
 
 
-       if (kport == IP_NULL) {
-               /* the thread is already terminated (can this happen?) */
-               return;
+       /*
+        * Only ports that were set by root-owned processes
+        * (privileged ports) should survive 
+        */
+       if (thread->exc_actions != NULL) {
+               has_old_exc_actions = TRUE;
+               for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) {
+                       if (thread->exc_actions[i].privileged) {
+                               old_exc_actions[i] = IP_NULL;
+                       } else {
+#if CONFIG_MACF
+                               mac_exc_update_action_label(thread->exc_actions + i, new_label);
+#endif
+                               old_exc_actions[i] = thread->exc_actions[i].port;
+                               thread->exc_actions[i].port = IP_NULL;          
+                       }
+               }
        }
 
        }
 
-       thr_act->ith_self = IP_NULL;
+       thread_mtx_unlock(thread);
 
 
+#if CONFIG_MACF
+       mac_exc_free_label(new_label);
+#endif
+       
        /* release the naked send rights */
 
        /* release the naked send rights */
 
-       if (IP_VALID(thr_act->ith_sself))
-               ipc_port_release_send(thr_act->ith_sself);
-       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) {
-           if (IP_VALID(thr_act->exc_actions[i].port))
-               ipc_port_release_send(thr_act->exc_actions[i].port);
-        }
+       if (IP_VALID(old_sself))
+               ipc_port_release_send(old_sself);
+
+       if (has_old_exc_actions) {
+               for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) {
+                       ipc_port_release_send(old_exc_actions[i]);
+               }
+       }
 
        /* destroy the kernel port */
 
        /* destroy the kernel port */
-       ipc_port_dealloc_kernel(kport);
+       if (old_kport != IP_NULL) {
+               ipc_port_dealloc_kernel(old_kport);
+       }
+
+       /* unbind the thread special reply port */
+       if (IP_VALID(thread->ith_special_reply_port)) {
+               ipc_port_unbind_special_reply_port(thread, TRUE);
+       }
 }
 
 /*
 }
 
 /*
@@ -439,9 +718,9 @@ ipc_thr_act_terminate(thread_act_t thr_act)
 
 ipc_port_t
 retrieve_task_self_fast(
 
 ipc_port_t
 retrieve_task_self_fast(
-       register task_t         task)
+       task_t          task)
 {
 {
-       register ipc_port_t port;
+       ipc_port_t port;
 
        assert(task == current_task());
 
 
        assert(task == current_task());
 
@@ -464,27 +743,30 @@ retrieve_task_self_fast(
 }
 
 /*
 }
 
 /*
- *     Routine:        retrieve_act_self_fast
+ *     Routine:        retrieve_thread_self_fast
  *     Purpose:
  *     Purpose:
- *             Optimized version of retrieve_thread_self,
- *             that only works for the current thread.
- *
  *             Return a send right (possibly null/dead)
  *             for the thread's user-visible self port.
  *             Return a send right (possibly null/dead)
  *             for the thread's user-visible self port.
+ *
+ *             Only works for the current thread.
+ *
  *     Conditions:
  *             Nothing locked.
  */
 
 ipc_port_t
  *     Conditions:
  *             Nothing locked.
  */
 
 ipc_port_t
-retrieve_act_self_fast(thread_act_t thr_act)
+retrieve_thread_self_fast(
+       thread_t                thread)
 {
 {
-       register ipc_port_t port;
+       ipc_port_t port;
+
+       assert(thread == current_thread());
+
+       thread_mtx_lock(thread);
 
 
-       assert(thr_act == current_act());
-       act_lock(thr_act);
-       assert(thr_act->ith_self != IP_NULL);
+       assert(thread->ith_self != IP_NULL);
 
 
-       if ((port = thr_act->ith_sself) == thr_act->ith_self) {
+       if ((port = thread->ith_sself) == thread->ith_self) {
                /* no interposing */
 
                ip_lock(port);
                /* no interposing */
 
                ip_lock(port);
@@ -492,9 +774,11 @@ retrieve_act_self_fast(thread_act_t thr_act)
                ip_reference(port);
                port->ip_srights++;
                ip_unlock(port);
                ip_reference(port);
                port->ip_srights++;
                ip_unlock(port);
-       } else
+       }
+       else
                port = ipc_port_copy_send(port);
                port = ipc_port_copy_send(port);
-       act_unlock(thr_act);
+
+       thread_mtx_unlock(thread);
 
        return port;
 }
 
        return port;
 }
@@ -511,13 +795,16 @@ retrieve_act_self_fast(thread_act_t thr_act)
  */
 
 mach_port_name_t
  */
 
 mach_port_name_t
-task_self_trap(void)
+task_self_trap(
+       __unused struct task_self_trap_args *args)
 {
        task_t task = current_task();
        ipc_port_t sright;
 {
        task_t task = current_task();
        ipc_port_t sright;
+       mach_port_name_t name;
 
        sright = retrieve_task_self_fast(task);
 
        sright = retrieve_task_self_fast(task);
-       return ipc_port_copyout_send(sright, task->itk_space);
+       name = ipc_port_copyout_send(sright, task->itk_space);
+       return name;
 }
 
 /*
 }
 
 /*
@@ -532,14 +819,18 @@ task_self_trap(void)
  */
 
 mach_port_name_t
  */
 
 mach_port_name_t
-thread_self_trap(void)
+thread_self_trap(
+       __unused struct thread_self_trap_args *args)
 {
 {
-       thread_act_t  thr_act  = current_act();
-       task_t task = thr_act->task;
+       thread_t  thread = current_thread();
+       task_t task = thread->task;
        ipc_port_t sright;
        ipc_port_t sright;
+       mach_port_name_t name;
+
+       sright = retrieve_thread_self_fast(thread);
+       name = ipc_port_copyout_send(sright, task->itk_space);
+       return name;
 
 
-       sright = retrieve_act_self_fast(thr_act);
-       return ipc_port_copyout_send(sright, task->itk_space);
 }
 
 /*
 }
 
 /*
@@ -554,7 +845,8 @@ thread_self_trap(void)
  */
 
 mach_port_name_t
  */
 
 mach_port_name_t
-mach_reply_port(void)
+mach_reply_port(
+       __unused struct mach_reply_port_args *args)
 {
        ipc_port_t port;
        mach_port_name_t name;
 {
        ipc_port_t port;
        mach_port_name_t name;
@@ -565,124 +857,331 @@ mach_reply_port(void)
                ip_unlock(port);
        else
                name = MACH_PORT_NULL;
                ip_unlock(port);
        else
                name = MACH_PORT_NULL;
-
        return name;
 }
 
 /*
        return name;
 }
 
 /*
- *     Routine:        task_get_special_port [kernel call]
+ *     Routine:        thread_get_special_reply_port [mach trap]
  *     Purpose:
  *     Purpose:
- *             Clones a send right for one of the task's
- *             special ports.
+ *             Allocate a special reply port for the calling thread.
  *     Conditions:
  *             Nothing locked.
  *     Returns:
  *     Conditions:
  *             Nothing locked.
  *     Returns:
- *             KERN_SUCCESS            Extracted a send right.
- *             KERN_INVALID_ARGUMENT   The task is null.
- *             KERN_FAILURE            The task/space is dead.
- *             KERN_INVALID_ARGUMENT   Invalid special port.
+ *             MACH_PORT_NULL if there are any resource failures
+ *             or other errors.
  */
 
  */
 
-kern_return_t
-task_get_special_port(
-       task_t          task,
-       int             which,
-       ipc_port_t      *portp)
+mach_port_name_t
+thread_get_special_reply_port(
+       __unused struct thread_get_special_reply_port_args *args)
 {
 {
-       ipc_port_t *whichp;
        ipc_port_t port;
        ipc_port_t port;
+       mach_port_name_t name;
+       kern_return_t kr;
+       thread_t thread = current_thread();
 
 
-       if (task == TASK_NULL)
-               return KERN_INVALID_ARGUMENT;
+       /* unbind the thread special reply port */
+       if (IP_VALID(thread->ith_special_reply_port)) {
+               kr = ipc_port_unbind_special_reply_port(thread, TRUE);
+               if (kr != KERN_SUCCESS) {
+                       return MACH_PORT_NULL;
+               }
+       }
 
 
-       switch (which) {
-           case TASK_KERNEL_PORT:
-               whichp = &task->itk_sself;
-               break;
+       kr = ipc_port_alloc(current_task()->itk_space, &name, &port);
+       if (kr == KERN_SUCCESS) {
+               ipc_port_bind_special_reply_port_locked(port);
+               ip_unlock(port);
+       } else {
+               name = MACH_PORT_NULL;
+       }
+       return name;
+}
 
 
-           case TASK_HOST_PORT:
-               whichp = &task->itk_host;
-               break;
+/*
+ *     Routine:        ipc_port_bind_special_reply_port_locked
+ *     Purpose:
+ *             Bind the given port to current thread as a special reply port.
+ *     Conditions:
+ *             Port locked.
+ *     Returns:
+ *             None.
+ */
 
 
-           case TASK_BOOTSTRAP_PORT:
-               whichp = &task->itk_bootstrap;
-               break;
+static void
+ipc_port_bind_special_reply_port_locked(
+       ipc_port_t port)
+{
+       thread_t thread = current_thread();
+       assert(thread->ith_special_reply_port == NULL);
 
 
-            case TASK_WIRED_LEDGER_PORT:
-                whichp = &task->wired_ledger_port;
-                break;
+       ip_reference(port);
+       thread->ith_special_reply_port = port;
+       port->ip_specialreply = 1;
+       port->ip_link_sync_qos = 1;
+}
 
 
-            case TASK_PAGED_LEDGER_PORT:
-                whichp = &task->paged_ledger_port;
-                break;
-                    
-           default:
-               return KERN_INVALID_ARGUMENT;
-       }
+/*
+ *     Routine:        ipc_port_unbind_special_reply_port
+ *     Purpose:
+ *             Unbind the thread's special reply port.
+ *             If the special port is linked to a port, adjust it's sync qos delta`.
+ *     Condition:
+ *             Nothing locked.
+ *     Returns:
+ *             None.
+ */
+static kern_return_t
+ipc_port_unbind_special_reply_port(
+       thread_t thread,
+       boolean_t unbind_active_port)
+{
+       ipc_port_t special_reply_port = thread->ith_special_reply_port;
 
 
-       itk_lock(task);
-       if (task->itk_self == IP_NULL) {
-               itk_unlock(task);
+       ip_lock(special_reply_port);
+
+       /* Return error if port active and unbind_active_port set to FALSE */
+       if (unbind_active_port == FALSE && ip_active(special_reply_port)) {
+               ip_unlock(special_reply_port);
                return KERN_FAILURE;
        }
 
                return KERN_FAILURE;
        }
 
-       port = ipc_port_copy_send(*whichp);
-       itk_unlock(task);
+       thread->ith_special_reply_port = NULL;
+       ipc_port_unlink_special_reply_port_locked(special_reply_port, NULL,
+               IPC_PORT_UNLINK_SR_CLEAR_SPECIAL_REPLY);
+       /* port unlocked */
 
 
-       *portp = port;
+       ip_release(special_reply_port);
        return KERN_SUCCESS;
 }
 
 /*
        return KERN_SUCCESS;
 }
 
 /*
- *     Routine:        task_set_special_port [kernel call]
+ *     Routine:        thread_get_special_port [kernel call]
  *     Purpose:
  *     Purpose:
- *             Changes one of the task's special ports,
- *             setting it to the supplied send right.
+ *             Clones a send right for one of the thread's
+ *             special ports.
  *     Conditions:
  *     Conditions:
- *             Nothing locked.  If successful, consumes
- *             the supplied send right.
+ *             Nothing locked.
  *     Returns:
  *     Returns:
- *             KERN_SUCCESS            Changed the special port.
- *             KERN_INVALID_ARGUMENT   The task is null.
- *             KERN_FAILURE            The task/space is dead.
+ *             KERN_SUCCESS            Extracted a send right.
+ *             KERN_INVALID_ARGUMENT   The thread is null.
+ *             KERN_FAILURE            The thread is dead.
  *             KERN_INVALID_ARGUMENT   Invalid special port.
  */
 
 kern_return_t
  *             KERN_INVALID_ARGUMENT   Invalid special port.
  */
 
 kern_return_t
-task_set_special_port(
-       task_t          task,
-       int             which,
-       ipc_port_t      port)
+thread_get_special_port(
+       thread_t                thread,
+       int                             which,
+       ipc_port_t              *portp)
 {
 {
-       ipc_port_t *whichp;
-       ipc_port_t old;
+       kern_return_t   result = KERN_SUCCESS;
+       ipc_port_t              *whichp;
 
 
-       if (task == TASK_NULL)
-               return KERN_INVALID_ARGUMENT;
+       if (thread == THREAD_NULL)
+               return (KERN_INVALID_ARGUMENT);
 
        switch (which) {
 
        switch (which) {
-           case TASK_KERNEL_PORT:
-               whichp = &task->itk_sself;
+
+       case THREAD_KERNEL_PORT:
+               whichp = &thread->ith_sself;
                break;
 
                break;
 
-           case TASK_HOST_PORT:
-               whichp = &task->itk_host;
+       default:
+               return (KERN_INVALID_ARGUMENT);
+       }
+
+       thread_mtx_lock(thread);
+
+       if (thread->active)
+               *portp = ipc_port_copy_send(*whichp);
+       else
+               result = KERN_FAILURE;
+
+       thread_mtx_unlock(thread);
+
+       return (result);
+}
+
+/*
+ *     Routine:        thread_set_special_port [kernel call]
+ *     Purpose:
+ *             Changes one of the thread's special ports,
+ *             setting it to the supplied send right.
+ *     Conditions:
+ *             Nothing locked.  If successful, consumes
+ *             the supplied send right.
+ *     Returns:
+ *             KERN_SUCCESS            Changed the special port.
+ *             KERN_INVALID_ARGUMENT   The thread is null.
+ *             KERN_FAILURE            The thread is dead.
+ *             KERN_INVALID_ARGUMENT   Invalid special port.
+ */
+
+kern_return_t
+thread_set_special_port(
+       thread_t                thread,
+       int                     which,
+       ipc_port_t      port)
+{
+       kern_return_t   result = KERN_SUCCESS;
+       ipc_port_t              *whichp, old = IP_NULL;
+
+       if (thread == THREAD_NULL)
+               return (KERN_INVALID_ARGUMENT);
+
+       switch (which) {
+
+       case THREAD_KERNEL_PORT:
+               whichp = &thread->ith_sself;
                break;
 
                break;
 
-           case TASK_BOOTSTRAP_PORT:
-               whichp = &task->itk_bootstrap;
+       default:
+               return (KERN_INVALID_ARGUMENT);
+       }
+
+       thread_mtx_lock(thread);
+
+       if (thread->active) {
+               old = *whichp;
+               *whichp = port;
+       }
+       else
+               result = KERN_FAILURE;
+
+       thread_mtx_unlock(thread);
+
+       if (IP_VALID(old))
+               ipc_port_release_send(old);
+
+       return (result);
+}
+
+/*
+ *     Routine:        task_get_special_port [kernel call]
+ *     Purpose:
+ *             Clones a send right for one of the task's
+ *             special ports.
+ *     Conditions:
+ *             Nothing locked.
+ *     Returns:
+ *             KERN_SUCCESS            Extracted a send right.
+ *             KERN_INVALID_ARGUMENT   The task is null.
+ *             KERN_FAILURE            The task/space is dead.
+ *             KERN_INVALID_ARGUMENT   Invalid special port.
+ */
+
+kern_return_t
+task_get_special_port(
+       task_t          task,
+       int             which,
+       ipc_port_t      *portp)
+{
+       ipc_port_t port;
+
+       if (task == TASK_NULL)
+               return KERN_INVALID_ARGUMENT;
+
+       itk_lock(task);
+       if (task->itk_self == IP_NULL) {
+               itk_unlock(task);
+               return KERN_FAILURE;
+       }
+
+       switch (which) {
+       case TASK_KERNEL_PORT:
+               port = ipc_port_copy_send(task->itk_sself);
+               break;
+
+       case TASK_NAME_PORT:
+               port = ipc_port_make_send(task->itk_nself);
+               break;
+
+       case TASK_HOST_PORT:
+               port = ipc_port_copy_send(task->itk_host);
+               break;
+
+       case TASK_BOOTSTRAP_PORT:
+               port = ipc_port_copy_send(task->itk_bootstrap);
+               break;
+
+       case TASK_SEATBELT_PORT:
+               port = ipc_port_copy_send(task->itk_seatbelt);
+               break;
+
+       case TASK_ACCESS_PORT:
+               port = ipc_port_copy_send(task->itk_task_access);
                break;
 
                break;
 
-            case TASK_WIRED_LEDGER_PORT:
-                whichp = &task->wired_ledger_port;
-                break;
+       case TASK_DEBUG_CONTROL_PORT:
+               port = ipc_port_copy_send(task->itk_debug_control);
+               break;
 
 
-            case TASK_PAGED_LEDGER_PORT:
-                whichp = &task->paged_ledger_port;
-                break;
-                    
-           default:
+       default:
+           itk_unlock(task);
                return KERN_INVALID_ARGUMENT;
                return KERN_INVALID_ARGUMENT;
+       }
+       itk_unlock(task);
+
+       *portp = port;
+       return KERN_SUCCESS;
+}
+
+/*
+ *     Routine:        task_set_special_port [kernel call]
+ *     Purpose:
+ *             Changes one of the task's special ports,
+ *             setting it to the supplied send right.
+ *     Conditions:
+ *             Nothing locked.  If successful, consumes
+ *             the supplied send right.
+ *     Returns:
+ *             KERN_SUCCESS            Changed the special port.
+ *             KERN_INVALID_ARGUMENT   The task is null.
+ *             KERN_FAILURE            The task/space is dead.
+ *             KERN_INVALID_ARGUMENT   Invalid special port.
+ *             KERN_NO_ACCESS          Attempted overwrite of seatbelt port.
+ */
+
+kern_return_t
+task_set_special_port(
+       task_t          task,
+       int             which,
+       ipc_port_t      port)
+{
+       ipc_port_t *whichp;
+       ipc_port_t old;
+
+       if (task == TASK_NULL)
+               return KERN_INVALID_ARGUMENT;
+
+       switch (which) {
+       case TASK_KERNEL_PORT:
+           whichp = &task->itk_sself;
+           break;
+
+       case TASK_HOST_PORT:
+           whichp = &task->itk_host;
+           break;
+
+       case TASK_BOOTSTRAP_PORT:
+           whichp = &task->itk_bootstrap;
+           break;
+
+       case TASK_SEATBELT_PORT:
+           whichp = &task->itk_seatbelt;
+           break;
+
+       case TASK_ACCESS_PORT:
+           whichp = &task->itk_task_access;
+           break;
+
+       case TASK_DEBUG_CONTROL_PORT:
+           whichp = &task->itk_debug_control;
+           break;
+
+       default:
+           return KERN_INVALID_ARGUMENT;
        }/* switch */
 
        itk_lock(task);
        }/* switch */
 
        itk_lock(task);
@@ -691,6 +1190,13 @@ task_set_special_port(
                return KERN_FAILURE;
        }
 
                return KERN_FAILURE;
        }
 
+       /* do not allow overwrite of seatbelt or task access ports */
+       if ((TASK_SEATBELT_PORT == which  || TASK_ACCESS_PORT == which) 
+               && IP_VALID(*whichp)) {
+                       itk_unlock(task);
+                       return KERN_NO_ACCESS;
+       }
+
        old = *whichp;
        *whichp = port;
        itk_unlock(task);
        old = *whichp;
        *whichp = port;
        itk_unlock(task);
@@ -717,6 +1223,7 @@ task_set_special_port(
  *             KERN_SUCCESS            Stashed the port rights.
  *             KERN_INVALID_ARGUMENT   The task is null.
  *             KERN_INVALID_ARGUMENT   The task is dead.
  *             KERN_SUCCESS            Stashed the port rights.
  *             KERN_INVALID_ARGUMENT   The task is null.
  *             KERN_INVALID_ARGUMENT   The task is dead.
+ *             KERN_INVALID_ARGUMENT   The memory param is null.
  *             KERN_INVALID_ARGUMENT   Too many port rights supplied.
  */
 
  *             KERN_INVALID_ARGUMENT   Too many port rights supplied.
  */
 
@@ -727,10 +1234,11 @@ mach_ports_register(
        mach_msg_type_number_t  portsCnt)
 {
        ipc_port_t ports[TASK_PORT_REGISTER_MAX];
        mach_msg_type_number_t  portsCnt)
 {
        ipc_port_t ports[TASK_PORT_REGISTER_MAX];
-       int i;
+       unsigned int i;
 
        if ((task == TASK_NULL) ||
 
        if ((task == TASK_NULL) ||
-           (portsCnt > TASK_PORT_REGISTER_MAX))
+           (portsCnt > TASK_PORT_REGISTER_MAX) ||
+           (portsCnt && memory == NULL))
                return KERN_INVALID_ARGUMENT;
 
        /*
                return KERN_INVALID_ARGUMENT;
 
        /*
@@ -773,7 +1281,7 @@ mach_ports_register(
         */
 
        if (portsCnt != 0)
         */
 
        if (portsCnt != 0)
-               kfree((vm_offset_t) memory,
+               kfree(memory,
                      (vm_size_t) (portsCnt * sizeof(mach_port_t)));
 
        return KERN_SUCCESS;
                      (vm_size_t) (portsCnt * sizeof(mach_port_t)));
 
        return KERN_SUCCESS;
@@ -799,13 +1307,11 @@ mach_ports_lookup(
        mach_port_array_t       *portsp,
        mach_msg_type_number_t  *portsCnt)
 {
        mach_port_array_t       *portsp,
        mach_msg_type_number_t  *portsCnt)
 {
-       vm_offset_t memory;
+       void  *memory;
        vm_size_t size;
        ipc_port_t *ports;
        int i;
 
        vm_size_t size;
        ipc_port_t *ports;
        int i;
 
-       kern_return_t kr;
-       
        if (task == TASK_NULL)
                return KERN_INVALID_ARGUMENT;
 
        if (task == TASK_NULL)
                return KERN_INVALID_ARGUMENT;
 
@@ -840,6 +1346,50 @@ mach_ports_lookup(
        return KERN_SUCCESS;
 }
 
        return KERN_SUCCESS;
 }
 
+kern_return_t
+task_conversion_eval(task_t caller, task_t victim)
+{
+       /*
+        * Tasks are allowed to resolve their own task ports, and the kernel is
+        * allowed to resolve anyone's task port.
+        */
+       if (caller == kernel_task) {
+               return KERN_SUCCESS;
+       }
+
+       if (caller == victim) {
+               return KERN_SUCCESS;
+       }
+
+       /*
+        * Only the kernel can can resolve the kernel's task port. We've established
+        * by this point that the caller is not kernel_task.
+        */
+       if (victim == kernel_task) {
+               return KERN_INVALID_SECURITY;
+       }
+
+#if CONFIG_EMBEDDED
+       /*
+        * On embedded platforms, only a platform binary can resolve the task port
+        * of another platform binary.
+        */
+       if ((victim->t_flags & TF_PLATFORM) && !(caller->t_flags & TF_PLATFORM)) {
+#if SECURE_KERNEL
+               return KERN_INVALID_SECURITY;
+#else
+               if (cs_relax_platform_task_ports) {
+                       return KERN_SUCCESS;
+               } else {
+                       return KERN_INVALID_SECURITY;
+               }
+#endif /* SECURE_KERNEL */
+       }
+#endif /* CONFIG_EMBEDDED */
+
+       return KERN_SUCCESS;
+}
+
 /*
  *     Routine: convert_port_to_locked_task
  *     Purpose:
 /*
  *     Routine: convert_port_to_locked_task
  *     Purpose:
@@ -852,7 +1402,10 @@ mach_ports_lookup(
 task_t
 convert_port_to_locked_task(ipc_port_t port)
 {
 task_t
 convert_port_to_locked_task(ipc_port_t port)
 {
+       int try_failed_count = 0;
+
        while (IP_VALID(port)) {
        while (IP_VALID(port)) {
+               task_t ct = current_task();
                task_t task;
 
                ip_lock(port);
                task_t task;
 
                ip_lock(port);
@@ -863,6 +1416,11 @@ convert_port_to_locked_task(ipc_port_t port)
                task = (task_t) port->ip_kobject;
                assert(task != TASK_NULL);
 
                task = (task_t) port->ip_kobject;
                assert(task != TASK_NULL);
 
+               if (task_conversion_eval(ct, task)) {
+                       ip_unlock(port);
+                       return TASK_NULL;
+               }
+
                /*
                 * Normal lock ordering puts task_lock() before ip_lock().
                 * Attempt out-of-order locking here.
                /*
                 * Normal lock ordering puts task_lock() before ip_lock().
                 * Attempt out-of-order locking here.
@@ -871,13 +1429,55 @@ convert_port_to_locked_task(ipc_port_t port)
                        ip_unlock(port);
                        return(task);
                }
                        ip_unlock(port);
                        return(task);
                }
+               try_failed_count++;
 
                ip_unlock(port);
 
                ip_unlock(port);
-               mutex_pause();
+               mutex_pause(try_failed_count);
        }
        return TASK_NULL;
 }
 
        }
        return TASK_NULL;
 }
 
+/*
+ *     Routine: convert_port_to_locked_task_inspect
+ *     Purpose:
+ *             Internal helper routine to convert from a port to a locked
+ *             task inspect right. Used by internal routines that try to convert from a
+ *             task inspect port to a reference on some task related object.
+ *     Conditions:
+ *             Nothing locked, blocking OK.
+ */
+task_inspect_t
+convert_port_to_locked_task_inspect(ipc_port_t port)
+{
+        int try_failed_count = 0;
+
+       while (IP_VALID(port)) {
+               task_inspect_t task;
+
+               ip_lock(port);
+               if (!ip_active(port) || (ip_kotype(port) != IKOT_TASK)) {
+                       ip_unlock(port);
+                       return TASK_INSPECT_NULL;
+               }
+               task = (task_inspect_t)port->ip_kobject;
+               assert(task != TASK_INSPECT_NULL);
+               /*
+                * Normal lock ordering puts task_lock() before ip_lock().
+                * Attempt out-of-order locking here.
+                */
+               if (task_lock_try((task_t)task)) {
+                       ip_unlock(port);
+                       return task;
+               }
+               try_failed_count++;
+
+               ip_unlock(port);
+               mutex_pause(try_failed_count);
+       }
+       return TASK_INSPECT_NULL;
+}
+
+
 /*
  *     Routine:        convert_port_to_task
  *     Purpose:
 /*
  *     Routine:        convert_port_to_task
  *     Purpose:
@@ -889,208 +1489,213 @@ convert_port_to_locked_task(ipc_port_t port)
  */
 task_t
 convert_port_to_task(
  */
 task_t
 convert_port_to_task(
-       ipc_port_t      port)
+       ipc_port_t              port)
 {
 {
-       task_t task;
-
-       task = convert_port_to_locked_task(port);
-       if (task) {
-               task->ref_count++;
-               task_unlock(task);
-       }
-       return task;
+       return convert_port_to_task_with_exec_token(port, NULL);
 }
 
 /*
 }
 
 /*
- *     Routine:        convert_port_to_space
+ *     Routine:        convert_port_to_task_with_exec_token
  *     Purpose:
  *     Purpose:
- *             Convert from a port to a space.
- *             Doesn't consume the port ref; produces a space ref,
+ *             Convert from a port to a task and return
+ *             the exec token stored in the task.
+ *             Doesn't consume the port ref; produces a task ref,
  *             which may be null.
  *     Conditions:
  *             Nothing locked.
  */
  *             which may be null.
  *     Conditions:
  *             Nothing locked.
  */
-ipc_space_t
-convert_port_to_space(
-       ipc_port_t      port)
+task_t
+convert_port_to_task_with_exec_token(
+       ipc_port_t              port,
+       uint32_t                *exec_token)
 {
 {
-       ipc_space_t space;
-       task_t task;
+       task_t          task = TASK_NULL;
 
 
-       task = convert_port_to_locked_task(port);
+       if (IP_VALID(port)) {
+               ip_lock(port);
 
 
-       if (task == TASK_NULL)
-               return IPC_SPACE_NULL;
+               if (    ip_active(port)                                 &&
+                               ip_kotype(port) == IKOT_TASK            ) {
+                       task_t ct = current_task();
+                       task = (task_t)port->ip_kobject;
+                       assert(task != TASK_NULL);
 
 
-       if (!task->active) {
-               task_unlock(task);
-               return IPC_SPACE_NULL;
+                       if (task_conversion_eval(ct, task)) {
+                               ip_unlock(port);
+                               return TASK_NULL;
+                       }
+
+                       if (exec_token) {
+                               *exec_token = task->exec_token;
+                       }
+                       task_reference_internal(task);
+               }
+
+               ip_unlock(port);
        }
        }
-               
-       space = task->itk_space;
-       is_reference(space);
-       task_unlock(task);
-       return (space);
+
+       return (task);
 }
 
 }
 
-upl_t
-convert_port_to_upl(
-       ipc_port_t      port)
+/*
+ *     Routine:        convert_port_to_task_name
+ *     Purpose:
+ *             Convert from a port to a task name.
+ *             Doesn't consume the port ref; produces a task name ref,
+ *             which may be null.
+ *     Conditions:
+ *             Nothing locked.
+ */
+task_name_t
+convert_port_to_task_name(
+       ipc_port_t              port)
 {
 {
-       upl_t upl;
+       task_name_t             task = TASK_NULL;
 
 
-       ip_lock(port);
-       if (!ip_active(port) || (ip_kotype(port) != IKOT_UPL)) {
-                       ip_unlock(port);
-                       return (upl_t)NULL;
-       }
-       upl = (upl_t) port->ip_kobject;
-       ip_unlock(port);
-       upl_lock(upl);
-       upl->ref_count+=1;
-       upl_unlock(upl);
-       return upl;
-}
+       if (IP_VALID(port)) {
+               ip_lock(port);
 
 
-mach_port_t
-convert_upl_to_port(
-       upl_t           upl)
-{
-       return MACH_PORT_NULL;
+               if (    ip_active(port)                                 &&
+                               (ip_kotype(port) == IKOT_TASK   ||
+                                ip_kotype(port) == IKOT_TASK_NAME)) {
+                       task = (task_name_t)port->ip_kobject;
+                       assert(task != TASK_NAME_NULL);
+
+                       task_reference_internal(task);
+               }
+
+               ip_unlock(port);
+       }
+
+       return (task);
 }
 
 }
 
-__private_extern__ void
-upl_no_senders(
-       upl_t                   upl,
-       mach_port_mscount_t     mscount)
+/*
+ *     Routine:        convert_port_to_task_inspect
+ *     Purpose:
+ *             Convert from a port to a task inspection right
+ *             Doesn't consume the port ref; produces a task ref,
+ *             which may be null.
+ *     Conditions:
+ *             Nothing locked.
+ */
+task_inspect_t
+convert_port_to_task_inspect(
+       ipc_port_t              port)
 {
 {
-       return;
+       task_inspect_t task = TASK_INSPECT_NULL;
+
+       if (IP_VALID(port)) {
+               ip_lock(port);
+
+               if (ip_active(port)     &&
+                   ip_kotype(port) == IKOT_TASK) {
+                       task = (task_inspect_t)port->ip_kobject;
+                       assert(task != TASK_INSPECT_NULL);
+
+                       task_reference_internal(task);
+               }
+
+               ip_unlock(port);
+       }
+
+       return (task);
 }
 
 /*
 }
 
 /*
- *     Routine:        convert_port_entry_to_map
+ *     Routine:        convert_port_to_task_suspension_token
  *     Purpose:
  *     Purpose:
- *             Convert from a port specifying an entry or a task
- *             to a map. Doesn't consume the port ref; produces a map ref,
- *             which may be null.  Unlike convert_port_to_map, the
- *             port may be task or a named entry backed.
+ *             Convert from a port to a task suspension token.
+ *             Doesn't consume the port ref; produces a suspension token ref,
+ *             which may be null.
  *     Conditions:
  *             Nothing locked.
  */
  *     Conditions:
  *             Nothing locked.
  */
+task_suspension_token_t
+convert_port_to_task_suspension_token(
+       ipc_port_t              port)
+{
+       task_suspension_token_t         task = TASK_NULL;
 
 
+       if (IP_VALID(port)) {
+               ip_lock(port);
 
 
-vm_map_t
-convert_port_entry_to_map(
+               if (    ip_active(port)                                 &&
+                               ip_kotype(port) == IKOT_TASK_RESUME) {
+                       task = (task_suspension_token_t)port->ip_kobject;
+                       assert(task != TASK_NULL);
+
+                       task_reference_internal(task);
+               }
+
+               ip_unlock(port);
+       }
+
+       return (task);
+}
+
+/*
+ *     Routine:        convert_port_to_space
+ *     Purpose:
+ *             Convert from a port to a space.
+ *             Doesn't consume the port ref; produces a space ref,
+ *             which may be null.
+ *     Conditions:
+ *             Nothing locked.
+ */
+ipc_space_t
+convert_port_to_space(
        ipc_port_t      port)
 {
        ipc_port_t      port)
 {
+       ipc_space_t space;
        task_t task;
        task_t task;
-       vm_map_t map;
-       vm_named_entry_t        named_entry;
-
-       if(IP_VALID(port) && (ip_kotype(port) == IKOT_NAMED_ENTRY)) {
-               while(TRUE) {
-                       ip_lock(port);
-                       if(ip_active(port) && (ip_kotype(port) 
-                                               == IKOT_NAMED_ENTRY)) {
-                               named_entry =
-                                        (vm_named_entry_t)port->ip_kobject;
-                               if (!(mutex_try(&(named_entry)->Lock))) {
-                                               ip_unlock(port);
-                                               mutex_pause();
-                                               continue;
-                               }
-                               named_entry->ref_count++;
-                               mutex_unlock(&(named_entry)->Lock);
-                               ip_unlock(port);
-                               if ((named_entry->is_sub_map) &&
-                                       (named_entry->protection 
-                                       & VM_PROT_WRITE)) {
-                                       map = named_entry->backing.map;
-                               } else {
-                                       mach_destroy_memory_entry(port);
-                                       return VM_MAP_NULL;
-                               }
-                               vm_map_reference_swap(map);
-                               mach_destroy_memory_entry(port);
-                               break;
-                       }
-                       else 
-                               return VM_MAP_NULL;
-               }
-       } else {
-               task_t task;
 
 
-               task = convert_port_to_locked_task(port);
-               
-               if (task == TASK_NULL)
-                       return VM_MAP_NULL;
+       task = convert_port_to_locked_task(port);
 
 
-               if (!task->active) {
-                       task_unlock(task);
-                       return VM_MAP_NULL;
-               }
-               
-               map = task->map;
-               vm_map_reference_swap(map);
+       if (task == TASK_NULL)
+               return IPC_SPACE_NULL;
+
+       if (!task->active) {
                task_unlock(task);
                task_unlock(task);
+               return IPC_SPACE_NULL;
        }
        }
-
-       return map;
+               
+       space = task->itk_space;
+       is_reference(space);
+       task_unlock(task);
+       return (space);
 }
 
 /*
 }
 
 /*
- *     Routine:        convert_port_entry_to_object
+ *     Routine:        convert_port_to_space_inspect
  *     Purpose:
  *     Purpose:
- *             Convert from a port specifying a named entry to an
- *             object. Doesn't consume the port ref; produces a map ref,
- *             which may be null. 
+ *             Convert from a port to a space inspect right.
+ *             Doesn't consume the port ref; produces a space inspect ref,
+ *             which may be null.
  *     Conditions:
  *             Nothing locked.
  */
  *     Conditions:
  *             Nothing locked.
  */
-
-
-vm_object_t
-convert_port_entry_to_object(
+ipc_space_inspect_t
+convert_port_to_space_inspect(
        ipc_port_t      port)
 {
        ipc_port_t      port)
 {
-       vm_object_t object;
-       vm_named_entry_t        named_entry;
-
-       if(IP_VALID(port) && (ip_kotype(port) == IKOT_NAMED_ENTRY)) {
-               while(TRUE) {
-                       ip_lock(port);
-                       if(ip_active(port) && (ip_kotype(port) 
-                                               == IKOT_NAMED_ENTRY)) {
-                               named_entry =
-                                        (vm_named_entry_t)port->ip_kobject;
-                               if (!(mutex_try(&(named_entry)->Lock))) {
-                                               ip_unlock(port);
-                                               mutex_pause();
-                                               continue;
-                               }
-                               named_entry->ref_count++;
-                               mutex_unlock(&(named_entry)->Lock);
-                               ip_unlock(port);
-                               if ((!named_entry->is_sub_map) &&
-                                       (named_entry->protection 
-                                       & VM_PROT_WRITE)) {
-                                       object = named_entry->object;
-                               } else {
-                                       mach_destroy_memory_entry(port);
-                                       return (vm_object_t)NULL;
-                               }
-                               vm_object_reference(named_entry->object);
-                               mach_destroy_memory_entry(port);
-                               break;
-                       }
-                       else 
-                               return (vm_object_t)NULL;
-               }
-       } else {
-               return (vm_object_t)NULL;
+       ipc_space_inspect_t space;
+       task_inspect_t task;
+
+       task = convert_port_to_locked_task_inspect(port);
+
+       if (task == TASK_INSPECT_NULL)
+               return IPC_SPACE_INSPECT_NULL;
+
+       if (!task->active) {
+               task_unlock(task);
+               return IPC_SPACE_INSPECT_NULL;
        }
 
        }
 
-       return object;
+       space = (ipc_space_inspect_t)task->itk_space;
+       is_reference((ipc_space_t)space);
+       task_unlock((task_t)task);
+       return space;
 }
 
 /*
 }
 
 /*
@@ -1128,170 +1733,345 @@ convert_port_to_map(
 
 
 /*
 
 
 /*
- *     Routine:        convert_port_to_act
+ *     Routine:        convert_port_to_thread
  *     Purpose:
  *     Purpose:
- *             Convert from a port to a thr_act.
- *             Doesn't consume the port ref; produces an thr_act ref,
+ *             Convert from a port to a thread.
+ *             Doesn't consume the port ref; produces an thread ref,
  *             which may be null.
  *     Conditions:
  *             Nothing locked.
  */
 
  *             which may be null.
  *     Conditions:
  *             Nothing locked.
  */
 
-thread_act_t
-convert_port_to_act( ipc_port_t port )
+thread_t
+convert_port_to_thread(
+       ipc_port_t              port)
 {
 {
-       boolean_t r;
-       thread_act_t thr_act = 0;
+       thread_t        thread = THREAD_NULL;
 
 
-       r = FALSE;
-       while (!r && IP_VALID(port)) {
+       if (IP_VALID(port)) {
                ip_lock(port);
                ip_lock(port);
-               r = ref_act_port_locked(port, &thr_act);
-               /* port unlocked */
+
+               if (    ip_active(port)                                 &&
+                               ip_kotype(port) == IKOT_THREAD          ) {
+                       thread = (thread_t)port->ip_kobject;
+                       assert(thread != THREAD_NULL);
+                       if (thread->task && thread->task == kernel_task &&
+                           current_task() != kernel_task) {
+                               ip_unlock(port);
+                               return THREAD_NULL;
+                       }
+
+                       thread_reference_internal(thread);
+               }
+
+               ip_unlock(port);
        }
        }
-       return (thr_act);
+
+       return (thread);
 }
 
 }
 
-boolean_t
-ref_act_port_locked( ipc_port_t port, thread_act_t *pthr_act )
+/*
+ *     Routine:        convert_port_to_thread_inspect
+ *     Purpose:
+ *             Convert from a port to a thread inspection right
+ *             Doesn't consume the port ref; produces a thread ref,
+ *             which may be null.
+ *     Conditions:
+ *             Nothing locked.
+ */
+thread_inspect_t
+convert_port_to_thread_inspect(
+       ipc_port_t              port)
 {
 {
-       thread_act_t thr_act;
+       thread_inspect_t thread = THREAD_INSPECT_NULL;
 
 
-       thr_act = 0;
-       if (ip_active(port) &&
-               (ip_kotype(port) == IKOT_ACT)) {
-               thr_act = (thread_act_t) port->ip_kobject;
-               assert(thr_act != THR_ACT_NULL);
+       if (IP_VALID(port)) {
+               ip_lock(port);
 
 
-               /*
-                * Out of order locking here, normal
-                * ordering is act_lock(), then ip_lock().
-                */
-               if (!act_lock_try(thr_act)) {
-                       ip_unlock(port);
-                       mutex_pause();
-                       return (FALSE);
+               if (ip_active(port) &&
+                   ip_kotype(port) == IKOT_THREAD) {
+                       thread = (thread_inspect_t)port->ip_kobject;
+                       assert(thread != THREAD_INSPECT_NULL);
+                       thread_reference_internal((thread_t)thread);
                }
                }
-               act_reference_locked(thr_act);
-               act_unlock(thr_act);
+               ip_unlock(port);
        }
        }
-       *pthr_act = thr_act;
-       ip_unlock(port);
-       return (TRUE);
+
+       return thread;
 }
 
 /*
 }
 
 /*
- *     Routine:        port_name_to_act
+ *     Routine:        convert_thread_inspect_to_port
  *     Purpose:
  *     Purpose:
- *             Convert from a port name to an act reference
- *             A name of MACH_PORT_NULL is valid for the null act
+ *             Convert from a thread inspect reference to a port.
+ *             Consumes a thread ref;
+ *             As we never export thread inspect ports, always
+ *             creates a NULL port.
  *     Conditions:
  *             Nothing locked.
  */
  *     Conditions:
  *             Nothing locked.
  */
-thread_act_t
-port_name_to_act(
+
+ipc_port_t
+convert_thread_inspect_to_port(thread_inspect_t thread)
+{
+       thread_deallocate(thread);
+       return IP_NULL;
+}
+
+
+/*
+ *     Routine:        port_name_to_thread
+ *     Purpose:
+ *             Convert from a port name to an thread reference
+ *             A name of MACH_PORT_NULL is valid for the null thread.
+ *     Conditions:
+ *             Nothing locked.
+ *
+ *     TODO: Could this be faster if it were ipc_port_translate_send based, like thread_switch?
+ *           We could avoid extra lock/unlock and extra ref operations on the port.
+ */
+thread_t
+port_name_to_thread(
        mach_port_name_t        name)
 {
        mach_port_name_t        name)
 {
-       thread_act_t thr_act = THR_ACT_NULL;
+       thread_t        thread = THREAD_NULL;
+       ipc_port_t      kport;
+
+       if (MACH_PORT_VALID(name)) {
+               if (ipc_object_copyin(current_space(), name,
+                                              MACH_MSG_TYPE_COPY_SEND,
+                                                         (ipc_object_t *)&kport) != KERN_SUCCESS)
+                       return (THREAD_NULL);
+
+               thread = convert_port_to_thread(kport);
+               
+               if (IP_VALID(kport))
+                       ipc_port_release_send(kport);
+       }
+
+       return (thread);
+}
+
+task_t
+port_name_to_task(
+       mach_port_name_t name)
+{
        ipc_port_t kern_port;
        kern_return_t kr;
        ipc_port_t kern_port;
        kern_return_t kr;
+       task_t task = TASK_NULL;
 
        if (MACH_PORT_VALID(name)) {
                kr = ipc_object_copyin(current_space(), name,
                                       MACH_MSG_TYPE_COPY_SEND,
                                       (ipc_object_t *) &kern_port);
                if (kr != KERN_SUCCESS)
 
        if (MACH_PORT_VALID(name)) {
                kr = ipc_object_copyin(current_space(), name,
                                       MACH_MSG_TYPE_COPY_SEND,
                                       (ipc_object_t *) &kern_port);
                if (kr != KERN_SUCCESS)
-                       return THR_ACT_NULL;
+                       return TASK_NULL;
+
+               task = convert_port_to_task(kern_port);
+
+               if (IP_VALID(kern_port))
+                       ipc_port_release_send(kern_port);
+       }
+       return task;
+}
+
+task_inspect_t
+port_name_to_task_inspect(
+       mach_port_name_t name)
+{
+       ipc_port_t kern_port;
+       kern_return_t kr;
+       task_inspect_t ti = TASK_INSPECT_NULL;
+
+       if (MACH_PORT_VALID(name)) {
+               kr = ipc_object_copyin(current_space(), name,
+                                      MACH_MSG_TYPE_COPY_SEND,
+                                      (ipc_object_t *)&kern_port);
+               if (kr != KERN_SUCCESS)
+                       return TASK_NULL;
+
+               ti = convert_port_to_task_inspect(kern_port);
+
+               if (IP_VALID(kern_port))
+                       ipc_port_release_send(kern_port);
+       }
+       return ti;
+}
+
+/*
+ *     Routine:        port_name_to_host
+ *     Purpose:
+ *             Convert from a port name to a host pointer.
+ *             NOTE: This does _not_ return a +1 reference to the host_t
+ *     Conditions:
+ *             Nothing locked.
+ */
+host_t
+port_name_to_host(
+       mach_port_name_t name)
+{
+
+       host_t host = HOST_NULL;
+       kern_return_t kr;
+       ipc_port_t port;
+
+       if (MACH_PORT_VALID(name)) {
+               kr = ipc_port_translate_send(current_space(), name, &port);
+               if (kr == KERN_SUCCESS) {
+                       host = convert_port_to_host(port);
+                       ip_unlock(port);
+               }
+       }
+       return host;
+}
+
+/*
+ *     Routine:        convert_task_to_port
+ *     Purpose:
+ *             Convert from a task to a port.
+ *             Consumes a task ref; produces a naked send right
+ *             which may be invalid.  
+ *     Conditions:
+ *             Nothing locked.
+ */
+
+ipc_port_t
+convert_task_to_port(
+       task_t          task)
+{
+       ipc_port_t port;
+
+       itk_lock(task);
+
+       if (task->itk_self != IP_NULL)
+               port = ipc_port_make_send(task->itk_self);
+       else
+               port = IP_NULL;
+
+       itk_unlock(task);
+
+       task_deallocate(task);
+       return port;
+}
+
+/*
+ *     Routine:        convert_task_inspect_to_port
+ *     Purpose:
+ *             Convert from a task inspect reference to a port.
+ *             Consumes a task ref;
+ *             As we never export task inspect ports, always
+ *             creates a NULL port.
+ *     Conditions:
+ *             Nothing locked.
+ */
+ipc_port_t
+convert_task_inspect_to_port(
+       task_inspect_t          task)
+{
+       task_deallocate(task);
 
 
-               thr_act = convert_port_to_act(kern_port);
-               
-               if (IP_VALID(kern_port))
-                       ipc_port_release_send(kern_port);
-       }
-       return thr_act;
+       return IP_NULL;
 }
 
 }
 
-task_t
-port_name_to_task(
-       mach_port_name_t name)
+/*
+ *     Routine:        convert_task_suspend_token_to_port
+ *     Purpose:
+ *             Convert from a task suspension token to a port.
+ *             Consumes a task suspension token ref; produces a naked send-once right
+ *             which may be invalid.  
+ *     Conditions:
+ *             Nothing locked.
+ */
+ipc_port_t
+convert_task_suspension_token_to_port(
+       task_suspension_token_t         task)
 {
 {
-       ipc_port_t kern_port;
-       kern_return_t kr;
-       task_t task = TASK_NULL;
+       ipc_port_t port;
 
 
-       if (MACH_PORT_VALID(name)) {
-               kr = ipc_object_copyin(current_space(), name,
-                                      MACH_MSG_TYPE_COPY_SEND,
-                                      (ipc_object_t *) &kern_port);
-               if (kr != KERN_SUCCESS)
-                       return TASK_NULL;
+       task_lock(task);
+       if (task->active) {
+               if (task->itk_resume == IP_NULL) {
+                       task->itk_resume = ipc_port_alloc_kernel();
+                       if (!IP_VALID(task->itk_resume)) {
+                               panic("failed to create resume port");
+                       }
 
 
-               task = convert_port_to_task(kern_port);
+                       ipc_kobject_set(task->itk_resume, (ipc_kobject_t) task, IKOT_TASK_RESUME);
+               }
 
 
-               if (IP_VALID(kern_port))
-                       ipc_port_release_send(kern_port);
+               /*
+                * Create a send-once right for each instance of a direct user-called
+                * task_suspend2 call. Each time one of these send-once rights is abandoned,
+                * the notification handler will resume the target task.
+                */
+               port = ipc_port_make_sonce(task->itk_resume);
+               assert(IP_VALID(port));
+       } else {
+               port = IP_NULL;
        }
        }
-       return task;
+
+       task_unlock(task);
+       task_suspension_token_deallocate(task);
+
+       return port;
 }
 
 }
 
+
 /*
 /*
- *     Routine:        convert_task_to_port
+ *     Routine:        convert_task_name_to_port
  *     Purpose:
  *     Purpose:
- *             Convert from a task to a port.
- *             Consumes a task ref; produces a naked send right
+ *             Convert from a task name ref to a port.
+ *             Consumes a task name ref; produces a naked send right
  *             which may be invalid.  
  *     Conditions:
  *             Nothing locked.
  */
 
 ipc_port_t
  *             which may be invalid.  
  *     Conditions:
  *             Nothing locked.
  */
 
 ipc_port_t
-convert_task_to_port(
-       task_t          task)
+convert_task_name_to_port(
+       task_name_t             task_name)
 {
        ipc_port_t port;
 
 {
        ipc_port_t port;
 
-       itk_lock(task);
-       if (task->itk_self != IP_NULL)
-#if    NORMA_TASK
-               if (task->map == VM_MAP_NULL)
-                       /* norma placeholder task */
-                       port = ipc_port_copy_send(task->itk_self);
-               else
-#endif /* NORMA_TASK */
-               port = ipc_port_make_send(task->itk_self);
+       itk_lock(task_name);
+       if (task_name->itk_nself != IP_NULL)
+               port = ipc_port_make_send(task_name->itk_nself);
        else
                port = IP_NULL;
        else
                port = IP_NULL;
-       itk_unlock(task);
+       itk_unlock(task_name);
 
 
-       task_deallocate(task);
+       task_name_deallocate(task_name);
        return port;
 }
 
 /*
        return port;
 }
 
 /*
- *     Routine:        convert_act_to_port
+ *     Routine:        convert_thread_to_port
  *     Purpose:
  *     Purpose:
- *             Convert from a thr_act to a port.
- *             Consumes an thr_act ref; produces a naked send right
+ *             Convert from a thread to a port.
+ *             Consumes an thread ref; produces a naked send right
  *             which may be invalid.
  *     Conditions:
  *             Nothing locked.
  */
 
 ipc_port_t
  *             which may be invalid.
  *     Conditions:
  *             Nothing locked.
  */
 
 ipc_port_t
-convert_act_to_port(thr_act)
-       thread_act_t thr_act;
+convert_thread_to_port(
+       thread_t                thread)
 {
 {
-       ipc_port_t port;
+       ipc_port_t              port;
 
 
-       act_lock(thr_act);
-       if (thr_act->ith_self != IP_NULL)
-               port = ipc_port_make_send(thr_act->ith_self);
+       thread_mtx_lock(thread);
+
+       if (thread->ith_self != IP_NULL)
+               port = ipc_port_make_send(thread->ith_self);
        else
                port = IP_NULL;
        else
                port = IP_NULL;
-       act_unlock(thr_act);
 
 
-       act_deallocate(thr_act);
-       return port;
+       thread_mtx_unlock(thread);
+
+       thread_deallocate(thread);
+
+       return (port);
 }
 
 /*
 }
 
 /*
@@ -1310,6 +2090,22 @@ space_deallocate(
                is_release(space);
 }
 
                is_release(space);
 }
 
+/*
+ *     Routine:        space_inspect_deallocate
+ *     Purpose:
+ *             Deallocate a space inspect ref produced by convert_port_to_space_inspect.
+ *     Conditions:
+ *             Nothing locked.
+ */
+
+void
+space_inspect_deallocate(
+       ipc_space_inspect_t     space)
+{
+       if (space != IS_INSPECT_NULL)
+               is_release((ipc_space_t)space);
+}
+
 /*
  *     Routine:        thread/task_set_exception_ports [kernel call]
  *     Purpose:
 /*
  *     Routine:        thread/task_set_exception_ports [kernel call]
  *     Purpose:
@@ -1330,29 +2126,36 @@ space_deallocate(
 
 kern_return_t
 thread_set_exception_ports(
 
 kern_return_t
 thread_set_exception_ports(
-       thread_act_t                    thr_act,
+       thread_t                                thread,
        exception_mask_t                exception_mask,
        exception_mask_t                exception_mask,
-       ipc_port_t                      new_port,
-       exception_behavior_t            new_behavior,
-       thread_state_flavor_t           new_flavor)
+       ipc_port_t                              new_port,
+       exception_behavior_t    new_behavior,
+       thread_state_flavor_t   new_flavor)
 {
 {
+       ipc_port_t              old_port[EXC_TYPES_COUNT];
+       boolean_t privileged = current_task()->sec_token.val[0] == 0;
        register int    i;
        register int    i;
-       ipc_port_t      old_port[EXC_TYPES_COUNT];
 
 
-       if (!thr_act)
-               return KERN_INVALID_ARGUMENT;
+#if CONFIG_MACF
+       struct label *new_label;
+#endif
+       
+       if (thread == THREAD_NULL)
+               return (KERN_INVALID_ARGUMENT);
 
 
-       if (exception_mask & ~EXC_MASK_ALL)
-               return KERN_INVALID_ARGUMENT;
+       if (exception_mask & ~EXC_MASK_VALID)
+               return (KERN_INVALID_ARGUMENT);
 
        if (IP_VALID(new_port)) {
 
        if (IP_VALID(new_port)) {
-               switch (new_behavior) {
+               switch (new_behavior & ~MACH_EXCEPTION_CODES) {
+
                case EXCEPTION_DEFAULT:
                case EXCEPTION_STATE:
                case EXCEPTION_STATE_IDENTITY:
                        break;
                case EXCEPTION_DEFAULT:
                case EXCEPTION_STATE:
                case EXCEPTION_STATE_IDENTITY:
                        break;
+
                default:
                default:
-                       return KERN_INVALID_ARGUMENT;
+                       return (KERN_INVALID_ARGUMENT);
                }
        }
 
                }
        }
 
@@ -1361,101 +2164,143 @@ thread_set_exception_ports(
         * VALID_THREAD_STATE_FLAVOR architecture dependent macro defined in
         * osfmk/mach/ARCHITECTURE/thread_status.h
         */
         * VALID_THREAD_STATE_FLAVOR architecture dependent macro defined in
         * osfmk/mach/ARCHITECTURE/thread_status.h
         */
-       if (!VALID_THREAD_STATE_FLAVOR(new_flavor)) {
-               return KERN_INVALID_ARGUMENT;
-       }
+       if (new_flavor != 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor))
+               return (KERN_INVALID_ARGUMENT);
 
 
-       act_lock(thr_act);
-       if (!thr_act->active) {
-               act_unlock(thr_act);
-               return KERN_FAILURE;
+#if CONFIG_MACF
+       new_label = mac_exc_create_label_for_current_proc();
+#endif
+       
+       thread_mtx_lock(thread);
+
+       if (!thread->active) {
+               thread_mtx_unlock(thread);
+
+               return (KERN_FAILURE);
        }
 
        }
 
-       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) {
-               if (exception_mask & (1 << i)) {
-                       old_port[i] = thr_act->exc_actions[i].port;
-                       thr_act->exc_actions[i].port =
-                               ipc_port_copy_send(new_port);
-                       thr_act->exc_actions[i].behavior = new_behavior;
-                       thr_act->exc_actions[i].flavor = new_flavor;
-               } else
+       if (thread->exc_actions == NULL) {
+               ipc_thread_init_exc_actions(thread);
+       }
+       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; ++i) {
+               if ((exception_mask & (1 << i))
+#if CONFIG_MACF
+                       && mac_exc_update_action_label(&thread->exc_actions[i], new_label) == 0
+#endif
+                       ) {
+                       old_port[i] = thread->exc_actions[i].port;
+                       thread->exc_actions[i].port = ipc_port_copy_send(new_port);
+                       thread->exc_actions[i].behavior = new_behavior;
+                       thread->exc_actions[i].flavor = new_flavor;
+                       thread->exc_actions[i].privileged = privileged;
+               }
+               else
                        old_port[i] = IP_NULL;
                        old_port[i] = IP_NULL;
-       }/* for */
-       /*
-        * Consume send rights without any lock held.
-        */
-       act_unlock(thr_act);
-       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++)
+       }
+
+       thread_mtx_unlock(thread);
+
+#if CONFIG_MACF
+       mac_exc_free_label(new_label);
+#endif
+       
+       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; ++i)
                if (IP_VALID(old_port[i]))
                        ipc_port_release_send(old_port[i]);
                if (IP_VALID(old_port[i]))
                        ipc_port_release_send(old_port[i]);
+
        if (IP_VALID(new_port))          /* consume send right */
                ipc_port_release_send(new_port);
 
        if (IP_VALID(new_port))          /* consume send right */
                ipc_port_release_send(new_port);
 
-        return KERN_SUCCESS;
-}/* thread_set_exception_port */
+       return (KERN_SUCCESS);
+}
 
 kern_return_t
 task_set_exception_ports(
 
 kern_return_t
 task_set_exception_ports(
-       task_t                          task,
+       task_t                                  task,
        exception_mask_t                exception_mask,
        exception_mask_t                exception_mask,
-       ipc_port_t                      new_port,
-       exception_behavior_t            new_behavior,
-       thread_state_flavor_t           new_flavor)
+       ipc_port_t                              new_port,
+       exception_behavior_t    new_behavior,
+       thread_state_flavor_t   new_flavor)
 {
 {
+       ipc_port_t              old_port[EXC_TYPES_COUNT];
+       boolean_t privileged = current_task()->sec_token.val[0] == 0;
        register int    i;
        register int    i;
-       ipc_port_t      old_port[EXC_TYPES_COUNT];
 
 
-       if (task == TASK_NULL) {
-               return KERN_INVALID_ARGUMENT;
-       }
+#if CONFIG_MACF
+       struct label *new_label;
+#endif 
 
 
-       if (exception_mask & ~EXC_MASK_ALL) {
-               return KERN_INVALID_ARGUMENT;
-       }
+       if (task == TASK_NULL)
+               return (KERN_INVALID_ARGUMENT);
+
+       if (exception_mask & ~EXC_MASK_VALID)
+               return (KERN_INVALID_ARGUMENT);
 
        if (IP_VALID(new_port)) {
 
        if (IP_VALID(new_port)) {
-               switch (new_behavior) {
+               switch (new_behavior & ~MACH_EXCEPTION_CODES) {
+
                case EXCEPTION_DEFAULT:
                case EXCEPTION_STATE:
                case EXCEPTION_STATE_IDENTITY:
                        break;
                case EXCEPTION_DEFAULT:
                case EXCEPTION_STATE:
                case EXCEPTION_STATE_IDENTITY:
                        break;
+
                default:
                default:
-                       return KERN_INVALID_ARGUMENT;
+                       return (KERN_INVALID_ARGUMENT);
                }
        }
                }
        }
-       /* Cannot easily check "new_flavor", but that just means that
-        * the flavor in the generated exception message might be garbage:
-        * GIGO */
 
 
-        itk_lock(task);
-        if (task->itk_self == IP_NULL) {
-                itk_unlock(task);
-                return KERN_FAILURE;
-        }
+       /*
+        * Check the validity of the thread_state_flavor by calling the
+        * VALID_THREAD_STATE_FLAVOR architecture dependent macro defined in
+        * osfmk/mach/ARCHITECTURE/thread_status.h
+        */
+       if (new_flavor != 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor))
+               return (KERN_INVALID_ARGUMENT);
 
 
-       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) {
-               if (exception_mask & (1 << i)) {
+#if CONFIG_MACF
+       new_label = mac_exc_create_label_for_current_proc();
+#endif
+       
+       itk_lock(task);
+
+       if (task->itk_self == IP_NULL) {
+               itk_unlock(task);
+
+               return (KERN_FAILURE);
+       }
+
+       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; ++i) {
+               if ((exception_mask & (1 << i))
+#if CONFIG_MACF
+                       && mac_exc_update_action_label(&task->exc_actions[i], new_label) == 0
+#endif
+                       ) {
                        old_port[i] = task->exc_actions[i].port;
                        task->exc_actions[i].port =
                                ipc_port_copy_send(new_port);
                        task->exc_actions[i].behavior = new_behavior;
                        task->exc_actions[i].flavor = new_flavor;
                        old_port[i] = task->exc_actions[i].port;
                        task->exc_actions[i].port =
                                ipc_port_copy_send(new_port);
                        task->exc_actions[i].behavior = new_behavior;
                        task->exc_actions[i].flavor = new_flavor;
-               } else
+                       task->exc_actions[i].privileged = privileged;
+               }
+               else
                        old_port[i] = IP_NULL;
                        old_port[i] = IP_NULL;
-       }/* for */
+       }
 
 
-       /*
-        * Consume send rights without any lock held.
-        */
-        itk_unlock(task);
-       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++)
+       itk_unlock(task);
+
+#if CONFIG_MACF
+       mac_exc_free_label(new_label);
+#endif
+       
+       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; ++i)
                if (IP_VALID(old_port[i]))
                        ipc_port_release_send(old_port[i]);
                if (IP_VALID(old_port[i]))
                        ipc_port_release_send(old_port[i]);
+
        if (IP_VALID(new_port))          /* consume send right */
                ipc_port_release_send(new_port);
 
        if (IP_VALID(new_port))          /* consume send right */
                ipc_port_release_send(new_port);
 
-        return KERN_SUCCESS;
-}/* task_set_exception_port */
+       return (KERN_SUCCESS);
+}
 
 /*
  *     Routine:        thread/task_swap_exception_ports [kernel call]
 
 /*
  *     Routine:        thread/task_swap_exception_ports [kernel call]
@@ -1486,197 +2331,233 @@ task_set_exception_ports(
 
 kern_return_t
 thread_swap_exception_ports(
 
 kern_return_t
 thread_swap_exception_ports(
-       thread_act_t                    thr_act,
-       exception_mask_t                exception_mask,
-       ipc_port_t                      new_port,
+       thread_t                                        thread,
+       exception_mask_t                        exception_mask,
+       ipc_port_t                                      new_port,
        exception_behavior_t            new_behavior,
        thread_state_flavor_t           new_flavor,
        exception_mask_array_t          masks,
        exception_behavior_t            new_behavior,
        thread_state_flavor_t           new_flavor,
        exception_mask_array_t          masks,
-       mach_msg_type_number_t          * CountCnt,
+       mach_msg_type_number_t          *CountCnt,
        exception_port_array_t          ports,
        exception_port_array_t          ports,
-       exception_behavior_array_t      behaviors,
-       thread_state_flavor_array_t     flavors )
+       exception_behavior_array_t      behaviors,
+       thread_state_flavor_array_t     flavors)
 {
 {
-       register int    i,
-                       j,
-                       count;
-       ipc_port_t      old_port[EXC_TYPES_COUNT];
+       ipc_port_t              old_port[EXC_TYPES_COUNT];
+       boolean_t privileged = current_task()->sec_token.val[0] == 0;
+       unsigned int    i, j, count;
 
 
-       if (!thr_act)
-               return KERN_INVALID_ARGUMENT;
+#if CONFIG_MACF
+       struct label *new_label;
+#endif
 
 
-       if (exception_mask & ~EXC_MASK_ALL) {
-               return KERN_INVALID_ARGUMENT;
-       }
+       if (thread == THREAD_NULL)
+               return (KERN_INVALID_ARGUMENT);
+
+       if (exception_mask & ~EXC_MASK_VALID)
+               return (KERN_INVALID_ARGUMENT);
 
        if (IP_VALID(new_port)) {
 
        if (IP_VALID(new_port)) {
-               switch (new_behavior) {
+               switch (new_behavior & ~MACH_EXCEPTION_CODES) {
+
                case EXCEPTION_DEFAULT:
                case EXCEPTION_STATE:
                case EXCEPTION_STATE_IDENTITY:
                        break;
                case EXCEPTION_DEFAULT:
                case EXCEPTION_STATE:
                case EXCEPTION_STATE_IDENTITY:
                        break;
+
                default:
                default:
-                       return KERN_INVALID_ARGUMENT;
+                       return (KERN_INVALID_ARGUMENT);
                }
        }
                }
        }
-       /* Cannot easily check "new_flavor", but that just means that
-        * the flavor in the generated exception message might be garbage:
-        * GIGO */
 
 
-       act_lock(thr_act);
-       if (!thr_act->active) {
-               act_unlock(thr_act);
-               return KERN_FAILURE;
+       if (new_flavor != 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor))
+               return (KERN_INVALID_ARGUMENT);
+
+#if CONFIG_MACF
+       new_label = mac_exc_create_label_for_current_proc();
+#endif
+
+       thread_mtx_lock(thread);
+
+       if (!thread->active) {
+               thread_mtx_unlock(thread);
+
+               return (KERN_FAILURE);
        }
 
        }
 
-       count = 0;
+       if (thread->exc_actions == NULL) {
+               ipc_thread_init_exc_actions(thread);
+       }
 
 
-       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) {
-               if (exception_mask & (1 << i)) {
-                       for (j = 0; j < count; j++) {
-/*
- *                             search for an identical entry, if found
- *                             set corresponding mask for this exception.
- */
-                               if (thr_act->exc_actions[i].port == ports[j] &&
-                                 thr_act->exc_actions[i].behavior ==behaviors[j]
-                                 && thr_act->exc_actions[i].flavor ==flavors[j])
-                               {
+       assert(EXC_TYPES_COUNT > FIRST_EXCEPTION);
+       for (count = 0, i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT && count < *CountCnt; ++i) {
+               if ((exception_mask & (1 << i))
+#if CONFIG_MACF
+                       && mac_exc_update_action_label(&thread->exc_actions[i], new_label) == 0
+#endif
+                       ) {
+                       for (j = 0; j < count; ++j) {
+                               /*
+                                * search for an identical entry, if found
+                                * set corresponding mask for this exception.
+                                */
+                               if (    thread->exc_actions[i].port == ports[j]                         &&
+                                               thread->exc_actions[i].behavior == behaviors[j]         &&
+                                               thread->exc_actions[i].flavor == flavors[j]                     ) {
                                        masks[j] |= (1 << i);
                                        break;
                                }
                                        masks[j] |= (1 << i);
                                        break;
                                }
-                       }/* for */
+                       }
+
                        if (j == count) {
                                masks[j] = (1 << i);
                        if (j == count) {
                                masks[j] = (1 << i);
-                               ports[j] =
-                               ipc_port_copy_send(thr_act->exc_actions[i].port);
+                               ports[j] = ipc_port_copy_send(thread->exc_actions[i].port);
 
 
-                               behaviors[j] = thr_act->exc_actions[i].behavior;
-                               flavors[j] = thr_act->exc_actions[i].flavor;
-                               count++;
+                               behaviors[j] = thread->exc_actions[i].behavior;
+                               flavors[j] = thread->exc_actions[i].flavor;
+                               ++count;
                        }
 
                        }
 
-                       old_port[i] = thr_act->exc_actions[i].port;
-                       thr_act->exc_actions[i].port =
-                               ipc_port_copy_send(new_port);
-                       thr_act->exc_actions[i].behavior = new_behavior;
-                       thr_act->exc_actions[i].flavor = new_flavor;
-                       if (count > *CountCnt) {
-                               break;
-                       }
-               } else
+                       old_port[i] = thread->exc_actions[i].port;
+                       thread->exc_actions[i].port = ipc_port_copy_send(new_port);
+                       thread->exc_actions[i].behavior = new_behavior;
+                       thread->exc_actions[i].flavor = new_flavor;
+                       thread->exc_actions[i].privileged = privileged;
+               }
+               else
                        old_port[i] = IP_NULL;
                        old_port[i] = IP_NULL;
-       }/* for */
+       }
 
 
-       /*
-        * Consume send rights without any lock held.
-        */
-       act_unlock(thr_act);
-       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++)
+       thread_mtx_unlock(thread);
+
+#if CONFIG_MACF
+       mac_exc_free_label(new_label);
+#endif
+       
+       while (--i >= FIRST_EXCEPTION) {
                if (IP_VALID(old_port[i]))
                        ipc_port_release_send(old_port[i]);
                if (IP_VALID(old_port[i]))
                        ipc_port_release_send(old_port[i]);
+       }
+
        if (IP_VALID(new_port))          /* consume send right */
                ipc_port_release_send(new_port);
        if (IP_VALID(new_port))          /* consume send right */
                ipc_port_release_send(new_port);
+
        *CountCnt = count;
        *CountCnt = count;
-       return KERN_SUCCESS;
-}/* thread_swap_exception_ports */
+
+       return (KERN_SUCCESS);
+}
 
 kern_return_t
 task_swap_exception_ports(
 
 kern_return_t
 task_swap_exception_ports(
-       task_t                          task,
-       exception_mask_t                exception_mask,
-       ipc_port_t                      new_port,
+       task_t                                          task,
+       exception_mask_t                        exception_mask,
+       ipc_port_t                                      new_port,
        exception_behavior_t            new_behavior,
        thread_state_flavor_t           new_flavor,
        exception_mask_array_t          masks,
        exception_behavior_t            new_behavior,
        thread_state_flavor_t           new_flavor,
        exception_mask_array_t          masks,
-       mach_msg_type_number_t          * CountCnt,
+       mach_msg_type_number_t          *CountCnt,
        exception_port_array_t          ports,
        exception_port_array_t          ports,
-       exception_behavior_array_t      behaviors,
-       thread_state_flavor_array_t     flavors         )
+       exception_behavior_array_t      behaviors,
+       thread_state_flavor_array_t     flavors)
 {
 {
-       register int    i,
-                       j,
-                       count;
-       ipc_port_t      old_port[EXC_TYPES_COUNT];
+       ipc_port_t              old_port[EXC_TYPES_COUNT];
+       boolean_t privileged = current_task()->sec_token.val[0] == 0;
+       unsigned int    i, j, count;
 
 
+#if CONFIG_MACF
+       struct label *new_label;
+#endif 
+       
        if (task == TASK_NULL)
        if (task == TASK_NULL)
-               return KERN_INVALID_ARGUMENT;
+               return (KERN_INVALID_ARGUMENT);
 
 
-       if (exception_mask & ~EXC_MASK_ALL) {
-               return KERN_INVALID_ARGUMENT;
-       }
+       if (exception_mask & ~EXC_MASK_VALID)
+               return (KERN_INVALID_ARGUMENT);
 
        if (IP_VALID(new_port)) {
 
        if (IP_VALID(new_port)) {
-               switch (new_behavior) {
+               switch (new_behavior & ~MACH_EXCEPTION_CODES) {
+
                case EXCEPTION_DEFAULT:
                case EXCEPTION_STATE:
                case EXCEPTION_STATE_IDENTITY:
                        break;
                case EXCEPTION_DEFAULT:
                case EXCEPTION_STATE:
                case EXCEPTION_STATE_IDENTITY:
                        break;
+
                default:
                default:
-                       return KERN_INVALID_ARGUMENT;
+                       return (KERN_INVALID_ARGUMENT);
                }
        }
                }
        }
-       /* Cannot easily check "new_flavor", but that just means that
-        * the flavor in the generated exception message might be garbage:
-        * GIGO */
 
 
+       if (new_flavor != 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor))
+               return (KERN_INVALID_ARGUMENT);
+
+#if CONFIG_MACF
+       new_label = mac_exc_create_label_for_current_proc();
+#endif
+       
        itk_lock(task);
        itk_lock(task);
+
        if (task->itk_self == IP_NULL) {
                itk_unlock(task);
        if (task->itk_self == IP_NULL) {
                itk_unlock(task);
-               return KERN_FAILURE;
-       }
 
 
-       count = 0;
+               return (KERN_FAILURE);
+       }
 
 
-       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) {
-               if (exception_mask & (1 << i)) {
+       assert(EXC_TYPES_COUNT > FIRST_EXCEPTION);
+       for (count = 0, i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT && count < *CountCnt; ++i) {
+               if ((exception_mask & (1 << i))
+#if CONFIG_MACF
+                       && mac_exc_update_action_label(&task->exc_actions[i], new_label) == 0
+#endif
+                       ) {
                        for (j = 0; j < count; j++) {
                        for (j = 0; j < count; j++) {
-/*
- *                             search for an identical entry, if found
- *                             set corresponding mask for this exception.
- */
-                               if (task->exc_actions[i].port == ports[j] &&
-                                 task->exc_actions[i].behavior == behaviors[j]
-                                 && task->exc_actions[i].flavor == flavors[j])
-                               {
+                               /*
+                                * search for an identical entry, if found
+                                * set corresponding mask for this exception.
+                                */
+                               if (    task->exc_actions[i].port == ports[j]                   &&
+                                               task->exc_actions[i].behavior == behaviors[j]   &&
+                                               task->exc_actions[i].flavor == flavors[j]               ) {
                                        masks[j] |= (1 << i);
                                        break;
                                }
                                        masks[j] |= (1 << i);
                                        break;
                                }
-                       }/* for */
+                       }
+
                        if (j == count) {
                                masks[j] = (1 << i);
                        if (j == count) {
                                masks[j] = (1 << i);
-                               ports[j] =
-                               ipc_port_copy_send(task->exc_actions[i].port);
+                               ports[j] = ipc_port_copy_send(task->exc_actions[i].port);
                                behaviors[j] = task->exc_actions[i].behavior;
                                flavors[j] = task->exc_actions[i].flavor;
                                behaviors[j] = task->exc_actions[i].behavior;
                                flavors[j] = task->exc_actions[i].flavor;
-                               count++;
+                               ++count;
                        }
                        }
+
                        old_port[i] = task->exc_actions[i].port;
                        old_port[i] = task->exc_actions[i].port;
-                       task->exc_actions[i].port =
-                               ipc_port_copy_send(new_port);
+
+                       task->exc_actions[i].port =     ipc_port_copy_send(new_port);
                        task->exc_actions[i].behavior = new_behavior;
                        task->exc_actions[i].flavor = new_flavor;
                        task->exc_actions[i].behavior = new_behavior;
                        task->exc_actions[i].flavor = new_flavor;
-                       if (count > *CountCnt) {
-                               break;
-                       }
-               } else
+                       task->exc_actions[i].privileged = privileged;
+               }
+               else
                        old_port[i] = IP_NULL;
                        old_port[i] = IP_NULL;
-       }/* for */
-
+       }
 
 
-       /*
-        * Consume send rights without any lock held.
-        */
        itk_unlock(task);
        itk_unlock(task);
-       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++)
+
+#if CONFIG_MACF
+       mac_exc_free_label(new_label);
+#endif
+       
+       while (--i >= FIRST_EXCEPTION) {
                if (IP_VALID(old_port[i]))
                        ipc_port_release_send(old_port[i]);
                if (IP_VALID(old_port[i]))
                        ipc_port_release_send(old_port[i]);
+       }
+
        if (IP_VALID(new_port))          /* consume send right */
                ipc_port_release_send(new_port);
        if (IP_VALID(new_port))          /* consume send right */
                ipc_port_release_send(new_port);
+
        *CountCnt = count;
 
        *CountCnt = count;
 
-       return KERN_SUCCESS;
-}/* task_swap_exception_ports */
+       return (KERN_SUCCESS);
+}
 
 /*
  *     Routine:        thread/task_get_exception_ports [kernel call]
 
 /*
  *     Routine:        thread/task_get_exception_ports [kernel call]
@@ -1699,128 +2580,129 @@ task_swap_exception_ports(
 
 kern_return_t
 thread_get_exception_ports(
 
 kern_return_t
 thread_get_exception_ports(
-       thread_act_t                    thr_act,
-       exception_mask_t                exception_mask,
+       thread_t                                        thread,
+       exception_mask_t                        exception_mask,
        exception_mask_array_t          masks,
        exception_mask_array_t          masks,
-       mach_msg_type_number_t          * CountCnt,
+       mach_msg_type_number_t          *CountCnt,
        exception_port_array_t          ports,
        exception_port_array_t          ports,
-       exception_behavior_array_t      behaviors,
-       thread_state_flavor_array_t     flavors         )
+       exception_behavior_array_t      behaviors,
+       thread_state_flavor_array_t     flavors)
 {
 {
-       register int    i,
-                       j,
-                       count;
+       unsigned int    i, j, count;
 
 
-       if (!thr_act)
-               return KERN_INVALID_ARGUMENT;
+       if (thread == THREAD_NULL)
+               return (KERN_INVALID_ARGUMENT);
 
 
-       if (exception_mask & ~EXC_MASK_ALL) {
-               return KERN_INVALID_ARGUMENT;
-       }
+       if (exception_mask & ~EXC_MASK_VALID)
+               return (KERN_INVALID_ARGUMENT);
 
 
-       act_lock(thr_act);
-       if (!thr_act->active) {
-               act_unlock(thr_act);
-               return KERN_FAILURE;
+       thread_mtx_lock(thread);
+
+       if (!thread->active) {
+               thread_mtx_unlock(thread);
+
+               return (KERN_FAILURE);
        }
 
        count = 0;
 
        }
 
        count = 0;
 
-       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) {
+       if (thread->exc_actions == NULL) {
+               goto done;
+       }
+
+       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; ++i) {
                if (exception_mask & (1 << i)) {
                if (exception_mask & (1 << i)) {
-                       for (j = 0; j < count; j++) {
-/*
- *                             search for an identical entry, if found
- *                             set corresponding mask for this exception.
- */
-                               if (thr_act->exc_actions[i].port == ports[j] &&
-                                 thr_act->exc_actions[i].behavior ==behaviors[j]
-                                 && thr_act->exc_actions[i].flavor == flavors[j])
-                               {
+                       for (j = 0; j < count; ++j) {
+                               /*
+                                * search for an identical entry, if found
+                                * set corresponding mask for this exception.
+                                */
+                               if (    thread->exc_actions[i].port == ports[j]                 &&
+                                               thread->exc_actions[i].behavior ==behaviors[j]  &&
+                                               thread->exc_actions[i].flavor == flavors[j]             ) {
                                        masks[j] |= (1 << i);
                                        break;
                                }
                                        masks[j] |= (1 << i);
                                        break;
                                }
-                       }/* for */
+                       }
+
                        if (j == count) {
                                masks[j] = (1 << i);
                        if (j == count) {
                                masks[j] = (1 << i);
-                               ports[j] =
-                               ipc_port_copy_send(thr_act->exc_actions[i].port);
-                               behaviors[j] = thr_act->exc_actions[i].behavior;
-                               flavors[j] = thr_act->exc_actions[i].flavor;
-                               count++;
-                               if (count >= *CountCnt) {
+                               ports[j] = ipc_port_copy_send(thread->exc_actions[i].port);
+                               behaviors[j] = thread->exc_actions[i].behavior;
+                               flavors[j] = thread->exc_actions[i].flavor;
+                               ++count;
+                               if (count >= *CountCnt)
                                        break;
                                        break;
-                               }
                        }
                }
                        }
                }
-       }/* for */
+       }
 
 
-       act_unlock(thr_act);
+done:
+       thread_mtx_unlock(thread);
 
        *CountCnt = count;
 
        *CountCnt = count;
-       return KERN_SUCCESS;
-}/* thread_get_exception_ports */
+
+       return (KERN_SUCCESS);
+}
 
 kern_return_t
 task_get_exception_ports(
 
 kern_return_t
 task_get_exception_ports(
-       task_t                          task,
-       exception_mask_t                exception_mask,
+       task_t                                          task,
+       exception_mask_t                        exception_mask,
        exception_mask_array_t          masks,
        exception_mask_array_t          masks,
-       mach_msg_type_number_t          * CountCnt,
+       mach_msg_type_number_t          *CountCnt,
        exception_port_array_t          ports,
        exception_port_array_t          ports,
-       exception_behavior_array_t      behaviors,
-       thread_state_flavor_array_t     flavors         )
+       exception_behavior_array_t      behaviors,
+       thread_state_flavor_array_t     flavors)
 {
 {
-       register int    i,
-                       j,
-                       count;
+       unsigned int    i, j, count;
 
        if (task == TASK_NULL)
 
        if (task == TASK_NULL)
-               return KERN_INVALID_ARGUMENT;
+               return (KERN_INVALID_ARGUMENT);
 
 
-       if (exception_mask & ~EXC_MASK_ALL) {
-               return KERN_INVALID_ARGUMENT;
-       }
+       if (exception_mask & ~EXC_MASK_VALID)
+               return (KERN_INVALID_ARGUMENT);
 
        itk_lock(task);
 
        itk_lock(task);
+
        if (task->itk_self == IP_NULL) {
                itk_unlock(task);
        if (task->itk_self == IP_NULL) {
                itk_unlock(task);
-               return KERN_FAILURE;
+
+               return (KERN_FAILURE);
        }
 
        count = 0;
 
        }
 
        count = 0;
 
-       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) {
+       for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; ++i) {
                if (exception_mask & (1 << i)) {
                if (exception_mask & (1 << i)) {
-                       for (j = 0; j < count; j++) {
-/*
- *                             search for an identical entry, if found
- *                             set corresponding mask for this exception.
- */
-                               if (task->exc_actions[i].port == ports[j] &&
-                                 task->exc_actions[i].behavior == behaviors[j]
-                                 && task->exc_actions[i].flavor == flavors[j])
-                               {
+                       for (j = 0; j < count; ++j) {
+                               /*
+                                * search for an identical entry, if found
+                                * set corresponding mask for this exception.
+                                */
+                               if (    task->exc_actions[i].port == ports[j]                   &&
+                                               task->exc_actions[i].behavior == behaviors[j]   &&
+                                               task->exc_actions[i].flavor == flavors[j]               ) {
                                        masks[j] |= (1 << i);
                                        break;
                                }
                                        masks[j] |= (1 << i);
                                        break;
                                }
-                       }/* for */
+                       }
+
                        if (j == count) {
                                masks[j] = (1 << i);
                        if (j == count) {
                                masks[j] = (1 << i);
-                               ports[j] =
-                                 ipc_port_copy_send(task->exc_actions[i].port);
+                               ports[j] = ipc_port_copy_send(task->exc_actions[i].port);
                                behaviors[j] = task->exc_actions[i].behavior;
                                flavors[j] = task->exc_actions[i].flavor;
                                behaviors[j] = task->exc_actions[i].behavior;
                                flavors[j] = task->exc_actions[i].flavor;
-                               count++;
-                               if (count > *CountCnt) {
+                               ++count;
+                               if (count > *CountCnt)
                                        break;
                                        break;
-                               }
                        }
                }
                        }
                }
-       }/* for */
+       }
 
        itk_unlock(task);
 
        *CountCnt = count;
 
        itk_unlock(task);
 
        *CountCnt = count;
-       return KERN_SUCCESS;
-}/* task_get_exception_ports */
+
+       return (KERN_SUCCESS);
+}