]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/exception.c
xnu-4570.41.2.tar.gz
[apple/xnu.git] / osfmk / kern / exception.c
index a47544027c0cdbdf142f464ebe798da49e2dc1be..4042b91b318ab8f05c4f64e83abdcb85eec61875 100644 (file)
@@ -83,6 +83,7 @@
 #include <kern/sched_prim.h>
 #include <kern/host.h>
 #include <kern/misc_protos.h>
+#include <security/mac_mach_internal.h>
 #include <string.h>
 #include <pexpert/pexpert.h>
 
@@ -138,12 +139,14 @@ exception_deliver(
        struct exception_action *excp,
        lck_mtx_t                       *mutex)
 {
-       ipc_port_t              exc_port;
+       ipc_port_t              exc_port = IPC_PORT_NULL;
        exception_data_type_t   small_code[EXCEPTION_CODE_MAX];
        int                     code64;
        int                     behavior;
        int                     flavor;
        kern_return_t           kr;
+       task_t task;
+       ipc_port_t thread_port = IPC_PORT_NULL, task_port = IPC_PORT_NULL;
 
        /*
         *  Save work if we are terminating.
@@ -200,6 +203,36 @@ exception_deliver(
                small_code[1] = CAST_DOWN_EXPLICIT(exception_data_type_t, code[1]);
        }
 
+       task = thread->task;
+
+#if CONFIG_MACF
+       /* Now is a reasonably good time to check if the exception action is
+        * permitted for this process, because after this point we will send
+        * the message out almost certainly.
+        * As with other failures, exception_triage_thread will go on
+        * to the next level.
+        */
+       if (mac_exc_action_check_exception_send(task, excp) != 0) {
+               kr = KERN_FAILURE;
+               goto out_release_right;
+       }
+#endif
+
+       if (behavior != EXCEPTION_STATE) {
+               if (thread != current_thread() || exception == EXC_CORPSE_NOTIFY) {
+
+                       task_reference(task);
+                       task_port = convert_task_to_port(task);
+                       /* task ref consumed */
+                       thread_reference(thread);
+                       thread_port = convert_thread_to_port(thread);
+                       /* thread ref consumed */
+               }
+               else {
+                       task_port = retrieve_task_self_fast(thread->task);
+                       thread_port = retrieve_thread_self_fast(thread);
+               }
+       }
 
        switch (behavior) {
        case EXCEPTION_STATE: {
@@ -228,34 +261,38 @@ exception_deliver(
                                                state, state_cnt,
                                                state, &state_cnt);
                        }
-                       if (kr == MACH_MSG_SUCCESS && exception != EXC_CORPSE_NOTIFY)
-                               kr = thread_setstatus(thread, flavor, 
-                                               (thread_state_t)state,
-                                               state_cnt);
+                       if (kr == KERN_SUCCESS) {
+                               if (exception != EXC_CORPSE_NOTIFY)
+                                       kr = thread_setstatus(thread, flavor,
+                                                       (thread_state_t)state,
+                                                       state_cnt);
+                               goto out_release_right;
+                       }
+
                }
 
-               return kr;
+               goto out_release_right;
        }
 
        case EXCEPTION_DEFAULT:
                c_thr_exc_raise++;
                if (code64) {
                        kr = mach_exception_raise(exc_port,
-                                       retrieve_thread_self_fast(thread),
-                                       retrieve_task_self_fast(thread->task),
+                                       thread_port,
+                                       task_port,
                                        exception,
                                        code, 
                                        codeCnt);
                } else {
                        kr = exception_raise(exc_port,
-                                       retrieve_thread_self_fast(thread),
-                                       retrieve_task_self_fast(thread->task),
+                                       thread_port,
+                                       task_port,
                                        exception,
                                        small_code, 
                                        codeCnt);
                }
 
-               return kr;
+               goto out_release_right;
 
        case EXCEPTION_STATE_IDENTITY: {
                mach_msg_type_number_t state_cnt;
@@ -270,8 +307,8 @@ exception_deliver(
                        if (code64) {
                                kr = mach_exception_raise_state_identity(
                                                exc_port,
-                                               retrieve_thread_self_fast(thread),
-                                               retrieve_task_self_fast(thread->task),
+                                               thread_port,
+                                               task_port,
                                                exception,
                                                code, 
                                                codeCnt,
@@ -280,8 +317,8 @@ exception_deliver(
                                                state, &state_cnt);
                        } else {
                                kr = exception_raise_state_identity(exc_port,
-                                               retrieve_thread_self_fast(thread),
-                                               retrieve_task_self_fast(thread->task),
+                                               thread_port,
+                                               task_port,
                                                exception,
                                                small_code, 
                                                codeCnt,
@@ -289,19 +326,40 @@ exception_deliver(
                                                state, state_cnt,
                                                state, &state_cnt);
                        }
-                       if (kr == MACH_MSG_SUCCESS && exception != EXC_CORPSE_NOTIFY)
-                               kr = thread_setstatus(thread, flavor,
-                                               (thread_state_t)state,
-                                               state_cnt);
+
+                       if (kr == KERN_SUCCESS) {
+                               if (exception != EXC_CORPSE_NOTIFY)
+                                       kr = thread_setstatus(thread, flavor,
+                                                       (thread_state_t)state,
+                                                       state_cnt);
+                               goto out_release_right;
+                       }
+
                }
 
-               return kr;
+               goto out_release_right;
        }
 
        default:
               panic ("bad exception behavior!");
               return KERN_FAILURE; 
        }/* switch */
+
+out_release_right:
+
+       if (task_port) {
+               ipc_port_release_send(task_port);
+       }
+
+       if (thread_port) {
+               ipc_port_release_send(thread_port);
+       }
+
+       if (exc_port) {
+               ipc_port_release_send(exc_port);
+       }
+
+       return kr;
 }
 
 /*
@@ -340,10 +398,11 @@ check_exc_receiver_dependency(
        return retval;
 }
 
+
 /*
- *     Routine:        exception_triage
+ *     Routine:        exception_triage_thread
  *     Purpose:
- *             The current thread caught an exception.
+ *             The thread caught an exception.
  *             We make an up-call to the thread's exception server.
  *     Conditions:
  *             Nothing locked and no resources held.
@@ -354,12 +413,12 @@ check_exc_receiver_dependency(
  *             KERN_SUCCESS if exception is handled by any of the handlers.
  */
 kern_return_t
-exception_triage(
+exception_triage_thread(
        exception_type_t        exception,
        mach_exception_data_t   code,
-       mach_msg_type_number_t  codeCnt)
+       mach_msg_type_number_t  codeCnt,
+       thread_t                thread)
 {
-       thread_t                thread;
        task_t                  task;
        host_priv_t             host_priv;
        lck_mtx_t               *mutex;
@@ -379,8 +438,6 @@ exception_triage(
                panic("called exception_triage when it was forbidden by the boot environment");
        }
 
-       thread = current_thread();
-
        /*
         * Try to raise the exception at the activation level.
         */
@@ -395,8 +452,8 @@ exception_triage(
        /*
         * Maybe the task level will handle it.
         */
-       task = current_task();
-       mutex = &task->lock;
+       task = thread->task;
+       mutex = &task->itk_lock_data;
        if (KERN_SUCCESS == check_exc_receiver_dependency(exception, task->exc_actions, mutex))
        {
                kr = exception_deliver(thread, exception, code, codeCnt, task->exc_actions, mutex);
@@ -409,7 +466,7 @@ exception_triage(
         */
        host_priv = host_priv_self();
        mutex = &host_priv->lock;
-       
+
        if (KERN_SUCCESS == check_exc_receiver_dependency(exception, host_priv->exc_actions, mutex))
        {
                kr = exception_deliver(thread, exception, code, codeCnt, host_priv->exc_actions, mutex);
@@ -424,6 +481,29 @@ out:
        return kr;
 }
 
+/*
+ *     Routine:        exception_triage
+ *     Purpose:
+ *             The current thread caught an exception.
+ *             We make an up-call to the thread's exception server.
+ *     Conditions:
+ *             Nothing locked and no resources held.
+ *             Called from an exception context, so
+ *             thread_exception_return and thread_kdb_return
+ *             are possible.
+ *     Returns:
+ *             KERN_SUCCESS if exception is handled by any of the handlers.
+ */
+kern_return_t
+exception_triage(
+       exception_type_t        exception,
+       mach_exception_data_t   code,
+       mach_msg_type_number_t  codeCnt)
+{
+       thread_t thread = current_thread();
+       return exception_triage_thread(exception, code, codeCnt, thread);
+}
+
 kern_return_t
 bsd_exception(
        exception_type_t        exception,
@@ -439,7 +519,7 @@ bsd_exception(
         * Maybe the task level will handle it.
         */
        task = current_task();
-       mutex = &task->lock;
+       mutex = &task->itk_lock_data;
 
        kr = exception_deliver(self, exception, code, codeCnt, task->exc_actions, mutex);