Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | /* * Copyright (c) 2025 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * 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. * * 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, * 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_OSREFERENCE_LICENSE_HEADER_END@ */ #include <darwintest.h> #include <mach/mach_port.h> #include <kern/task.h> #include "mocks/dt_proxy.h" #include <mach/message.h> #include <ipc/ipc_policy.h> #include <ipc/ipc_port.h> #include "mocks/osfmk/mock_ipc.h" #include "mocks/mock_dynamic.h" #include "mocks/osfmk/mock_thread.h" #include "ipc/utils/mach_port_construct_helpers.h" #include "ipc/utils/ipc_policy_helpers.h" #define UT_MODULE osfmk extern size_t proc_struct_size; T_GLOBAL_META( T_META_NAMESPACE("xnu.unit.ipc"), T_META_RUN_CONCURRENTLY(TRUE), T_META_RADAR_COMPONENT_NAME("xnu"), T_META_RADAR_COMPONENT_VERSION("IPC"), T_META_TAG_VM_PREFERRED); extern bool thread_set_state_allowed( thread_t thread, int flavor, thread_set_status_flags_t flags, audit_token_t *audit); T_DECL( thread_set_state_basic_policy, "Basic thread_set_state policy checks") { bool allowed; int flavor = ARM_THREAD_STATE; thread_set_status_flags_t flags = TSSF_FLAGS_NONE; /* Kernel allowed always */ allowed = thread_set_state_allowed(current_thread(), flavor, flags, NULL); T_EXPECT_TRUE(allowed, "thread_set_state should be allowed from kernel"); /* Everything allowed when no security policy set */ SET_IPC_POLICY(IPC_SPACE_POLICY_DEFAULT); allowed = thread_set_state_allowed(current_thread(), flavor, flags, NULL); T_EXPECT_TRUE(allowed, "thread_set_state should be allowed for thread with no security"); /* Everything allowed when opted out */ SET_IPC_POLICY(IPC_SPACE_POLICY_OPTED_OUT); allowed = thread_set_state_allowed(current_thread(), flavor, flags, NULL); T_EXPECT_TRUE(allowed, "thread_set_state should be allowed for opted out"); } T_DECL( thread_set_state_from_user_not_in_exception, "thread_set_state_from_user policy checks (NOT in mach exception)") { bool allowed = false; int flavor = ARM_THREAD_STATE; thread_set_status_flags_t flags = TSSF_CHECK_ENTITLEMENT; /* 3P allowed thread_set_state_from_user */ allowed = thread_set_state_allowed(current_thread(), flavor, flags, NULL); T_EXPECT_TRUE(allowed, "thread_set_state_from_user is allowed for 3P"); /* ES thread_set_state_from_user - allowed V0; disallowed >=V1 */ for (int i = 0; i < ENHANCED_VERSION_COUNT; i++) { ipc_space_policy_t policy = enhanced_versions[i]; SET_IPC_POLICY(policy); allowed = thread_set_state_allowed(current_thread(), flavor, flags, NULL); if (policy == IPC_POLICY_ENHANCED_V0) { T_EXPECT_TRUE(allowed, "thread_set_state_from_user should be allowed policy=%x", policy); } else { T_EXPECT_FALSE(allowed, "thread_set_state_from_user should be disallowed policy=%x", policy); } } } T_DECL( thread_set_state_from_user_in_exception, "thread_set_state_from_user policy checks (IN mach exception)") { bool allowed = false; int flavor = ARM_THREAD_STATE; thread_set_status_flags_t flags = TSSF_CHECK_ENTITLEMENT; current_thread()->options |= TH_IN_MACH_EXCEPTION; /* allowed for 3P */ allowed = thread_set_state_allowed(current_thread(), flavor, flags, NULL); T_EXPECT_TRUE(allowed, "thread_set_state_from_user is allowed for 3P"); /* Allowed (in telemetry mode) for all ES */ for (int i = 0; i < ENHANCED_VERSION_COUNT; i++) { ipc_space_policy_t policy = enhanced_versions[i]; SET_IPC_POLICY(policy); allowed = thread_set_state_allowed(current_thread(), flavor, flags, NULL); T_EXPECT_TRUE(allowed, "thread_set_state_from_user while in mach exception " "should be allowed policy=%x", policy); } current_thread()->options &= ~TH_IN_MACH_EXCEPTION; } T_DECL( mach_exception_state_flavor, "Mach exception handler modifying thread state") { bool allowed = false; int flavor = ARM_THREAD_STATE; thread_set_status_flags_t flags = TSSF_FLAGS_NONE; current_thread()->options |= TH_IN_MACH_EXCEPTION; /* 3P allowed setting via mach exception */ allowed = thread_set_state_allowed(current_thread(), flavor, flags, NULL); T_EXPECT_TRUE(allowed, "thread_set_state should be allowed for thread with no security"); /* All ES allows setting via mach exception */ for (int i = 0; i < ENHANCED_VERSION_COUNT; i++) { ipc_space_policy_t policy = enhanced_versions[i]; SET_IPC_POLICY(policy); flags = TSSF_CHECK_ENTITLEMENT; allowed = thread_set_state_allowed(current_thread(), flavor, flags, NULL); T_EXPECT_TRUE(allowed, "mach exception modifying thread state should be allowed policy=%x", policy); } current_thread()->options &= ~TH_IN_MACH_EXCEPTION; } T_DECL( thread_set_state_cross_thread_in_exception, "Cross-thread thread_set_state_from_user policy checks (target thread IN mach exception)") { bool allowed = false; int flavor = ARM_THREAD_STATE; thread_set_status_flags_t flags = TSSF_CHECK_ENTITLEMENT; /* Create a separate proc/task for the target thread */ extern task_t proc_get_task_raw(void *proc); void *proctask = calloc(1, proc_struct_size + sizeof(struct task)); T_ASSERT_NOTNULL(proctask, "proctask allocation"); proc_t proc = (proc_t)proctask; task_t task = proc_get_task_raw(proc); fake_init_task(task); /* Manually create a thread structure linked to the separate task */ thread_t target_thread = calloc(1, sizeof(struct thread)); T_ASSERT_NOTNULL(target_thread, "target_thread allocation"); target_thread->t_tro = calloc(1, sizeof(struct thread_ro)); T_ASSERT_NOTNULL(target_thread->t_tro, "target_thread t_tro allocation"); target_thread->t_tro->tro_owner = target_thread; target_thread->t_tro->tro_task = task; target_thread->t_tro->tro_proc = proc; /* Set target thread in mach exception */ target_thread->options |= TH_IN_MACH_EXCEPTION; /* allowed for 3P */ allowed = thread_set_state_allowed(target_thread, flavor, flags, NULL); T_EXPECT_TRUE(allowed, "cross-thread thread_set_state_from_user is allowed for 3P"); /* Cross-thread: V0 allows, V1+ blocks (even in telemetry mode) */ for (int i = 0; i < ENHANCED_VERSION_COUNT; i++) { ipc_space_policy_t policy = enhanced_versions[i]; SET_IPC_POLICY(policy); allowed = thread_set_state_allowed(target_thread, flavor, flags, NULL); if (policy == IPC_POLICY_ENHANCED_V0 || policy == IPC_POLICY_ENHANCED_V1) { T_EXPECT_TRUE(allowed, "cross-thread thread_set_state_from_user while target in mach exception " "should be allowed for V0 policy=%x", policy); } else { T_EXPECT_FALSE(allowed, "cross-thread thread_set_state_from_user while target in mach exception " "should be blocked for policy=%x", policy); } } /* Clean up */ free(target_thread->t_tro); free(target_thread); free(proctask); } |