#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>
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.
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: {
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;
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,
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,
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;
}
/*
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.
* 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;
panic("called exception_triage when it was forbidden by the boot environment");
}
- thread = current_thread();
-
/*
* Try to raise the exception at the activation level.
*/
/*
* 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);
*/
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);
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,
* 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);