xref: /xnu-12377.61.12/tests/arm_mte_spawn_policies.c (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1*4d495c6eSApple OSS Distributions /*
2*4d495c6eSApple OSS Distributions  * Copyright (c) 2024 Apple Inc. All rights reserved.
3*4d495c6eSApple OSS Distributions  *
4*4d495c6eSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*4d495c6eSApple OSS Distributions  *
6*4d495c6eSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*4d495c6eSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*4d495c6eSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*4d495c6eSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*4d495c6eSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*4d495c6eSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*4d495c6eSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*4d495c6eSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*4d495c6eSApple OSS Distributions  *
15*4d495c6eSApple OSS Distributions  * Please obtain a copy of the License at
16*4d495c6eSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*4d495c6eSApple OSS Distributions  *
18*4d495c6eSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*4d495c6eSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*4d495c6eSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*4d495c6eSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*4d495c6eSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*4d495c6eSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*4d495c6eSApple OSS Distributions  * limitations under the License.
25*4d495c6eSApple OSS Distributions  *
26*4d495c6eSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*4d495c6eSApple OSS Distributions  */
28*4d495c6eSApple OSS Distributions 
29*4d495c6eSApple OSS Distributions #include <darwintest.h>
30*4d495c6eSApple OSS Distributions #include <stdbool.h>
31*4d495c6eSApple OSS Distributions #include <spawn_private.h>
32*4d495c6eSApple OSS Distributions #include <libproc.h>
33*4d495c6eSApple OSS Distributions 
34*4d495c6eSApple OSS Distributions #include "arm_mte_utilities.h"
35*4d495c6eSApple OSS Distributions #include "test_utils.h"
36*4d495c6eSApple OSS Distributions 
37*4d495c6eSApple OSS Distributions #if (TARGET_OS_OSX || TARGET_OS_IOS) && defined(__arm64__)
38*4d495c6eSApple OSS Distributions #if !(TARGET_OS_XR || TARGET_OS_TV || TARGET_OS_WATCH || TARGET_OS_BRIDGE)
39*4d495c6eSApple OSS Distributions #define TARGET_SUPPORTS_MTE_EMULATION 1
40*4d495c6eSApple OSS Distributions #endif
41*4d495c6eSApple OSS Distributions #endif
42*4d495c6eSApple OSS Distributions 
43*4d495c6eSApple OSS Distributions /*
44*4d495c6eSApple OSS Distributions  * These tests verify whether the expected MTE state is found on target processes,
45*4d495c6eSApple OSS Distributions  * exercising the various enablement rules and system APIs.
46*4d495c6eSApple OSS Distributions  * Kernel behavior is extensively documented in kern_exec.c, but we recap here
47*4d495c6eSApple OSS Distributions  * the key points:
48*4d495c6eSApple OSS Distributions  *
49*4d495c6eSApple OSS Distributions  * 1) MTE can be enabled on a target process, in order of preference, by inheritance,
50*4d495c6eSApple OSS Distributions  *    spawn flags and entitlements.
51*4d495c6eSApple OSS Distributions  *    1.1) Inheritance can only be enabled via the dedicated POSIX_SPAWN_SECFLAG_EXPLICIT_ENABLE_INHERIT
52*4d495c6eSApple OSS Distributions  *         flag.
53*4d495c6eSApple OSS Distributions  *    1.2) posix_spawn flags trump entitlements. Inheritance trumps posix_spawn flags.
54*4d495c6eSApple OSS Distributions  *    1.3) entitlements are the desired and expected way to enable MTE in production. With the
55*4d495c6eSApple OSS Distributions  *         exception of launchd, no other entity on the system is expected to use posix_spawn
56*4d495c6eSApple OSS Distributions  *         as an enablement vector. (XCode uses it to provide a run-as-MTE feature)
57*4d495c6eSApple OSS Distributions  *
58*4d495c6eSApple OSS Distributions  * 2) posix_spawnattr_set_use_sec_transition_shims_np() API predates several of the rules at (1)
59*4d495c6eSApple OSS Distributions  *    and is therefore maintained in its legacy behavior of enabling both MTE and INHERITANCE
60*4d495c6eSApple OSS Distributions  *    by default. They both can be switched off via disablement flags: POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE
61*4d495c6eSApple OSS Distributions  *    and POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE_INHERIT.
62*4d495c6eSApple OSS Distributions  *
63*4d495c6eSApple OSS Distributions  * 3) POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE is _not_ supported on RELEASE to prevent attackers from
64*4d495c6eSApple OSS Distributions  *    using the posix_spawn API to disable MTE on a target. POSIX_SPAWN_SECFLAG_EXPLICIT_NEVER_CHECK_ENABLE,
65*4d495c6eSApple OSS Distributions  *    POSIX_SPAWN_SECFLAG_EXPLICIT_VM_POLICY_BYPASS and POSIX_SPAWN_SECFLAG_EXPLICIT_CHECK_BYPASS share the
66*4d495c6eSApple OSS Distributions  *    same destiny.
67*4d495c6eSApple OSS Distributions  *
68*4d495c6eSApple OSS Distributions  * Testing goals:
69*4d495c6eSApple OSS Distributions  * TG1 - ensure that, in absence of spawn flags, entitlements are respected downstream and no inheritance
70*4d495c6eSApple OSS Distributions  *       is present.
71*4d495c6eSApple OSS Distributions  * TG2 - ensure that posix_spawnattr_set_use_sec_transition_shims_np() still respects legacy behavior.
72*4d495c6eSApple OSS Distributions  * TG3 - ensure that posix_spawnattr_set_use_sec_transition_shims_np() is properly affected by
73*4d495c6eSApple OSS Distributions  *       POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE and POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE_INHERIT.
74*4d495c6eSApple OSS Distributions  *
75*4d495c6eSApple OSS Distributions  * <subject to rdar://145396237>
76*4d495c6eSApple OSS Distributions  * TG4 - ensure that the direct manipulation API works as expected.
77*4d495c6eSApple OSS Distributions  * <subject to having RELEASE behavior>
78*4d495c6eSApple OSS Distributions  * TG5 - ensure that on RELEASE POSIX_SPAWN_SECFLAG_EXPLICIT_NEVER_CHECK_ENABLE,
79*4d495c6eSApple OSS Distributions  *       POSIX_SPAWN_SECFLAG_EXPLICIT_VM_POLICY_BYPASS and POSIX_SPAWN_SECFLAG_EXPLICIT_CHECK_BYPASS are
80*4d495c6eSApple OSS Distributions  *       correctly ignored.
81*4d495c6eSApple OSS Distributions  * TG6 - ensure that a first-party process signed with com.apple.developer.driverkit is
82*4d495c6eSApple OSS Distributions  *		 sufficient for the system to apply MTE.
83*4d495c6eSApple OSS Distributions  */
84*4d495c6eSApple OSS Distributions 
85*4d495c6eSApple OSS Distributions #define INITIAL_ITERATION "0"
86*4d495c6eSApple OSS Distributions 
87*4d495c6eSApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.arm.mte"),
88*4d495c6eSApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("Darwin Testing"),
89*4d495c6eSApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("all"), T_META_OWNER("n_sabo"),
90*4d495c6eSApple OSS Distributions     T_META_RUN_CONCURRENTLY(false));
91*4d495c6eSApple OSS Distributions 
92*4d495c6eSApple OSS Distributions #define MTE_TOTAL_ENABLEMENT_TESTS 3
93*4d495c6eSApple OSS Distributions struct _mte_entitlement_process_expectation {
94*4d495c6eSApple OSS Distributions 	char *test_to_run;
95*4d495c6eSApple OSS Distributions 	char *expected_state;
96*4d495c6eSApple OSS Distributions 	char *test_name;
97*4d495c6eSApple OSS Distributions } mte_entitlement_process_expectation[MTE_TOTAL_ENABLEMENT_TESTS] = {
98*4d495c6eSApple OSS Distributions 	{ MTE_ENABLEMENT_TEST_VANILLA_PROCESS_STR, DO_NOT_EXPECT_MTE, "vanilla" },
99*4d495c6eSApple OSS Distributions 	{ MTE_ENABLEMENT_TEST_HARDENED_PROCESS_STR, EXPECT_MTE, "hardened-process"},
100*4d495c6eSApple OSS Distributions 	{ MTE_ENABLEMENT_TEST_OPTED_OUT_PROCESS_STR, DO_NOT_EXPECT_MTE, "AMFI opt-out"},
101*4d495c6eSApple OSS Distributions };
102*4d495c6eSApple OSS Distributions 
103*4d495c6eSApple OSS Distributions static void
do_entitlement_test(char * binary_to_launch,char * expected_mte_state)104*4d495c6eSApple OSS Distributions do_entitlement_test(char *binary_to_launch, char *expected_mte_state)
105*4d495c6eSApple OSS Distributions {
106*4d495c6eSApple OSS Distributions 	for (int i = 0; i < MTE_TOTAL_ENABLEMENT_TESTS; i++) {
107*4d495c6eSApple OSS Distributions 		T_LOG("Running %s that will spawn %s\n", binary_to_launch, mte_entitlement_process_expectation[i].test_name);
108*4d495c6eSApple OSS Distributions 		char *test_argv[] = {
109*4d495c6eSApple OSS Distributions 			binary_to_launch,
110*4d495c6eSApple OSS Distributions 			expected_mte_state,
111*4d495c6eSApple OSS Distributions 			mte_entitlement_process_expectation[i].test_to_run,
112*4d495c6eSApple OSS Distributions 			mte_entitlement_process_expectation[i].expected_state,
113*4d495c6eSApple OSS Distributions 			NULL
114*4d495c6eSApple OSS Distributions 		};
115*4d495c6eSApple OSS Distributions 
116*4d495c6eSApple OSS Distributions 		bool test_succeeded = fork_and_exec_new_process(test_argv);
117*4d495c6eSApple OSS Distributions 		T_ASSERT_TRUE(test_succeeded, "fork/exec entitlement test");
118*4d495c6eSApple OSS Distributions 
119*4d495c6eSApple OSS Distributions 		test_succeeded = posix_spawn_then_perform_action_from_process(test_argv, MTE_SPAWN_USE_VANILLA, 0);
120*4d495c6eSApple OSS Distributions 		T_ASSERT_TRUE(test_succeeded, "vanilla posix_spawn + entitlements test");
121*4d495c6eSApple OSS Distributions 	}
122*4d495c6eSApple OSS Distributions }
123*4d495c6eSApple OSS Distributions 
124*4d495c6eSApple OSS Distributions static void
do_spawn_flags_test(char * binary_to_launch,char * expected_mte_state,char * test_to_perform,char * expected_next_test_mte_state,uint16_t sec_flags)125*4d495c6eSApple OSS Distributions do_spawn_flags_test(
126*4d495c6eSApple OSS Distributions 	char *binary_to_launch,
127*4d495c6eSApple OSS Distributions 	char *expected_mte_state,
128*4d495c6eSApple OSS Distributions 	char *test_to_perform,
129*4d495c6eSApple OSS Distributions 	char *expected_next_test_mte_state,
130*4d495c6eSApple OSS Distributions 	uint16_t sec_flags)
131*4d495c6eSApple OSS Distributions {
132*4d495c6eSApple OSS Distributions 	char *test_argv[] = {
133*4d495c6eSApple OSS Distributions 		binary_to_launch,
134*4d495c6eSApple OSS Distributions 		expected_mte_state,
135*4d495c6eSApple OSS Distributions 		test_to_perform,
136*4d495c6eSApple OSS Distributions 		expected_next_test_mte_state,
137*4d495c6eSApple OSS Distributions 		NULL
138*4d495c6eSApple OSS Distributions 	};
139*4d495c6eSApple OSS Distributions 
140*4d495c6eSApple OSS Distributions 	bool test_succeeded = posix_spawn_then_perform_action_from_process(test_argv, MTE_SPAWN_USE_LEGACY_API, sec_flags);
141*4d495c6eSApple OSS Distributions 	T_ASSERT_TRUE(test_succeeded, "vanilla posix_spawn + entitlements test");
142*4d495c6eSApple OSS Distributions }
143*4d495c6eSApple OSS Distributions 
144*4d495c6eSApple OSS Distributions /*
145*4d495c6eSApple OSS Distributions  * TG1.
146*4d495c6eSApple OSS Distributions  *
147*4d495c6eSApple OSS Distributions  * Start different entitled processes that execute the whole spectrum of entitlement possibilities in a process
148*4d495c6eSApple OSS Distributions  * tree and ensure that expectations are matched.
149*4d495c6eSApple OSS Distributions  */
150*4d495c6eSApple OSS Distributions 
151*4d495c6eSApple OSS Distributions T_DECL(non_mte_enabled_binary_enablement_test,
152*4d495c6eSApple OSS Distributions     "Verify enablement rules against a process tree that starts with "
153*4d495c6eSApple OSS Distributions     "a non-MTE enabled binary",
154*4d495c6eSApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
155*4d495c6eSApple OSS Distributions     XNU_T_META_SOC_SPECIFIC) {
156*4d495c6eSApple OSS Distributions 	do_entitlement_test(SPAWN_HELPER_WITHOUT_ENTITLEMENT, DO_NOT_EXPECT_MTE);
157*4d495c6eSApple OSS Distributions }
158*4d495c6eSApple OSS Distributions 
159*4d495c6eSApple OSS Distributions T_DECL(mte_enabled_binary_enablement_test,
160*4d495c6eSApple OSS Distributions     "Verify enablement rules against a process tree that starts with "
161*4d495c6eSApple OSS Distributions     "a MTE-enabled binary.",
162*4d495c6eSApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
163*4d495c6eSApple OSS Distributions     XNU_T_META_SOC_SPECIFIC) {
164*4d495c6eSApple OSS Distributions 	do_entitlement_test(SPAWN_HELPER_WITH_ENTITLEMENT, EXPECT_MTE);
165*4d495c6eSApple OSS Distributions }
166*4d495c6eSApple OSS Distributions 
167*4d495c6eSApple OSS Distributions T_DECL(mte_opted_out_binary_enablement_test,
168*4d495c6eSApple OSS Distributions     "Verify enablement rules against a process tree that starts with "
169*4d495c6eSApple OSS Distributions     "a MTE-opted-out binary.",
170*4d495c6eSApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
171*4d495c6eSApple OSS Distributions     XNU_T_META_SOC_SPECIFIC) {
172*4d495c6eSApple OSS Distributions 	T_SKIP("skip until Monorail doesn't resign binaries fooling our ID checks");
173*4d495c6eSApple OSS Distributions 	do_entitlement_test(HARDENED_PROCESS_TOP_LEVEL_ONLY_AND_IN_AMFI_MTE_OPT_OUT_HELPER, DO_NOT_EXPECT_MTE);
174*4d495c6eSApple OSS Distributions }
175*4d495c6eSApple OSS Distributions 
176*4d495c6eSApple OSS Distributions /*
177*4d495c6eSApple OSS Distributions  * TG2.
178*4d495c6eSApple OSS Distributions  *
179*4d495c6eSApple OSS Distributions  * Verify that posix_spawnattr_set_use_sec_transition_shims_np() still maintains the original
180*4d495c6eSApple OSS Distributions  * behavior of enabling MTE and enabling inheritance whenever invoked without flags.
181*4d495c6eSApple OSS Distributions  */
182*4d495c6eSApple OSS Distributions T_DECL(mte_legacy_spawn_api_default_behavior,
183*4d495c6eSApple OSS Distributions     "Call posix_spawnattr_set_use_sec_transition_shims_np() and verify that "
184*4d495c6eSApple OSS Distributions     "MTE is enabled AND inheritance is present.",
185*4d495c6eSApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
186*4d495c6eSApple OSS Distributions     XNU_T_META_SOC_SPECIFIC) {
187*4d495c6eSApple OSS Distributions 	/* spawn flags and inheritance take precedence over the entitlement state */
188*4d495c6eSApple OSS Distributions 	for (int i = 0; i < MTE_TOTAL_ENABLEMENT_TESTS; i++) {
189*4d495c6eSApple OSS Distributions 		do_spawn_flags_test(SPAWN_HELPER_WITHOUT_ENTITLEMENT, EXPECT_MTE, mte_entitlement_process_expectation[i].test_to_run,
190*4d495c6eSApple OSS Distributions 		    EXPECT_MTE, 0);
191*4d495c6eSApple OSS Distributions 		do_spawn_flags_test(SPAWN_HELPER_WITH_ENTITLEMENT, EXPECT_MTE, mte_entitlement_process_expectation[i].test_to_run,
192*4d495c6eSApple OSS Distributions 		    EXPECT_MTE, 0);
193*4d495c6eSApple OSS Distributions #if MONORAIL_DOESNT_RESIGN
194*4d495c6eSApple OSS Distributions 		do_spawn_flags_test(HARDENED_PROCESS_TOP_LEVEL_ONLY_AND_IN_AMFI_MTE_OPT_OUT_HELPER, EXPECT_MTE, mte_entitlement_process_expectation[i].test_to_run,
195*4d495c6eSApple OSS Distributions 		    EXPECT_MTE, 0);
196*4d495c6eSApple OSS Distributions #endif /* MONORAIL_DOESNT_RESIGN */
197*4d495c6eSApple OSS Distributions 	}
198*4d495c6eSApple OSS Distributions }
199*4d495c6eSApple OSS Distributions 
200*4d495c6eSApple OSS Distributions /*
201*4d495c6eSApple OSS Distributions  * TG3.
202*4d495c6eSApple OSS Distributions  *
203*4d495c6eSApple OSS Distributions  * Verify that posix_spawnattr_set_use_sec_transition_shims_np() correctly handles
204*4d495c6eSApple OSS Distributions  * POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE and POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE_INHERIT
205*4d495c6eSApple OSS Distributions  * for internal usecases.
206*4d495c6eSApple OSS Distributions  */
207*4d495c6eSApple OSS Distributions T_DECL(mte_legacy_spawn_api_disable_flag_development,
208*4d495c6eSApple OSS Distributions     "Call posix_spawnattr_set_use_sec_transition_shims_np() passing the"
209*4d495c6eSApple OSS Distributions     "POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE flag and verify that MTE is disabled.",
210*4d495c6eSApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
211*4d495c6eSApple OSS Distributions     XNU_T_META_REQUIRES_DEVELOPMENT_KERNEL,
212*4d495c6eSApple OSS Distributions     XNU_T_META_SOC_SPECIFIC) {
213*4d495c6eSApple OSS Distributions 	uint16_t sec_flags = POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE;
214*4d495c6eSApple OSS Distributions 
215*4d495c6eSApple OSS Distributions 	T_LOG("posix_spawnattr_set_use_sec_transition_shims_np(POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE)\n");
216*4d495c6eSApple OSS Distributions 
217*4d495c6eSApple OSS Distributions 	/* posix_spawnattr_set_use_sec_transition_shims_np(POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE) implies inheritance */
218*4d495c6eSApple OSS Distributions 	for (int i = 0; i < MTE_TOTAL_ENABLEMENT_TESTS; i++) {
219*4d495c6eSApple OSS Distributions 		do_spawn_flags_test(SPAWN_HELPER_WITHOUT_ENTITLEMENT, DO_NOT_EXPECT_MTE, mte_entitlement_process_expectation[i].test_to_run,
220*4d495c6eSApple OSS Distributions 		    DO_NOT_EXPECT_MTE, sec_flags);
221*4d495c6eSApple OSS Distributions 		do_spawn_flags_test(SPAWN_HELPER_WITH_ENTITLEMENT, DO_NOT_EXPECT_MTE, mte_entitlement_process_expectation[i].test_to_run,
222*4d495c6eSApple OSS Distributions 		    DO_NOT_EXPECT_MTE, sec_flags);
223*4d495c6eSApple OSS Distributions #if MONORAIL_DOESNT_RESIGN
224*4d495c6eSApple OSS Distributions 		do_spawn_flags_test(HARDENED_PROCESS_TOP_LEVEL_ONLY_AND_IN_AMFI_MTE_OPT_OUT_HELPER, DO_NOT_EXPECT_MTE, mte_entitlement_process_expectation[i].test_to_run,
225*4d495c6eSApple OSS Distributions 		    DO_NOT_EXPECT_MTE, sec_flags);
226*4d495c6eSApple OSS Distributions #endif /* MONORAIL_DOESNT_RESIGN */
227*4d495c6eSApple OSS Distributions 	}
228*4d495c6eSApple OSS Distributions 
229*4d495c6eSApple OSS Distributions 	/* Now with inheritance disabled. */
230*4d495c6eSApple OSS Distributions 	T_LOG("posix_spawnattr_set_use_sec_transition_shims_np(POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE_INHERIT|EXPLICIT_DISABLE)\n");
231*4d495c6eSApple OSS Distributions 	sec_flags = POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE_INHERIT | POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE;
232*4d495c6eSApple OSS Distributions 	for (int i = 0; i < MTE_TOTAL_ENABLEMENT_TESTS; i++) {
233*4d495c6eSApple OSS Distributions 		do_spawn_flags_test(SPAWN_HELPER_WITHOUT_ENTITLEMENT, DO_NOT_EXPECT_MTE, mte_entitlement_process_expectation[i].test_to_run,
234*4d495c6eSApple OSS Distributions 		    mte_entitlement_process_expectation[i].expected_state, sec_flags);
235*4d495c6eSApple OSS Distributions 		do_spawn_flags_test(SPAWN_HELPER_WITH_ENTITLEMENT, DO_NOT_EXPECT_MTE, mte_entitlement_process_expectation[i].test_to_run,
236*4d495c6eSApple OSS Distributions 		    mte_entitlement_process_expectation[i].expected_state, sec_flags);
237*4d495c6eSApple OSS Distributions #if MONORAIL_DOESNT_RESIGN
238*4d495c6eSApple OSS Distributions 		do_spawn_flags_test(HARDENED_PROCESS_TOP_LEVEL_ONLY_AND_IN_AMFI_MTE_OPT_OUT_HELPER, DO_NOT_EXPECT_MTE, mte_entitlement_process_expectation[i].test_to_run,
239*4d495c6eSApple OSS Distributions 		    mte_entitlement_process_expectation[i].expected_state, sec_flags);
240*4d495c6eSApple OSS Distributions #endif /* MONORAIL_DOESNT_RESIGN */
241*4d495c6eSApple OSS Distributions 	}
242*4d495c6eSApple OSS Distributions }
243*4d495c6eSApple OSS Distributions 
244*4d495c6eSApple OSS Distributions T_DECL(mte_legacy_spawn_api_disable_flag_release,
245*4d495c6eSApple OSS Distributions     "Call posix_spawnattr_set_use_sec_transition_shims_np() passing the"
246*4d495c6eSApple OSS Distributions     "POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE flag and verify that on RELEASE we fail the call.",
247*4d495c6eSApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
248*4d495c6eSApple OSS Distributions     XNU_T_META_REQUIRES_RELEASE_KERNEL,
249*4d495c6eSApple OSS Distributions     XNU_T_META_SOC_SPECIFIC) {
250*4d495c6eSApple OSS Distributions 	posix_spawnattr_t attr;
251*4d495c6eSApple OSS Distributions 	pid_t child_pid = 0;
252*4d495c6eSApple OSS Distributions 	errno_t ret = posix_spawnattr_init(&attr);
253*4d495c6eSApple OSS Distributions 	/* We should not get to execute the binary at all, so no need to have the right arguments. */
254*4d495c6eSApple OSS Distributions 	char *args[] = { SPAWN_HELPER_WITH_ENTITLEMENT, NULL};
255*4d495c6eSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_init");
256*4d495c6eSApple OSS Distributions 
257*4d495c6eSApple OSS Distributions 	ret = posix_spawnattr_set_use_sec_transition_shims_np(&attr, POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE);
258*4d495c6eSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_set_use_sec_transition_shims_np");
259*4d495c6eSApple OSS Distributions 
260*4d495c6eSApple OSS Distributions 	ret = posix_spawn(&child_pid, args[0], NULL, &attr, args, NULL);
261*4d495c6eSApple OSS Distributions 	T_ASSERT_POSIX_FAILURE(ret, EINVAL, "posix_spawn DISABLE on RELEASE");
262*4d495c6eSApple OSS Distributions }
263*4d495c6eSApple OSS Distributions 
264*4d495c6eSApple OSS Distributions /*
265*4d495c6eSApple OSS Distributions  * TG6.
266*4d495c6eSApple OSS Distributions  *
267*4d495c6eSApple OSS Distributions  * Verify that a first-party dext will get MTE out of the box.
268*4d495c6eSApple OSS Distributions  */
269*4d495c6eSApple OSS Distributions T_DECL(first_party_dext_spawns_with_mte,
270*4d495c6eSApple OSS Distributions     "Ensure first-party dexts receive MTE",
271*4d495c6eSApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
272*4d495c6eSApple OSS Distributions     XNU_T_META_SOC_SPECIFIC) {
273*4d495c6eSApple OSS Distributions 	/* Given a first-party binary signed with com.apple.developer.driverkit */
274*4d495c6eSApple OSS Distributions 	pid_t target_pid;
275*4d495c6eSApple OSS Distributions 	char* target_argv[] = {"arm_mte_driverkit_standin", NULL};
276*4d495c6eSApple OSS Distributions 
277*4d495c6eSApple OSS Distributions 	/* When the binary is spawned */
278*4d495c6eSApple OSS Distributions 	int ret = posix_spawn(&target_pid, target_argv[0], NULL, NULL, target_argv, NULL);
279*4d495c6eSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ret, "posix_spawn(%s)", target_argv[0]);
280*4d495c6eSApple OSS Distributions 	T_ASSERT_NE(target_pid, 0, "posix_spawn(%s)", target_argv[0]);
281*4d495c6eSApple OSS Distributions 
282*4d495c6eSApple OSS Distributions 	/* And we interrogate its MTE state */
283*4d495c6eSApple OSS Distributions 	struct proc_bsdinfowithuniqid info;
284*4d495c6eSApple OSS Distributions 	ret = proc_pidinfo(target_pid, PROC_PIDT_BSDINFOWITHUNIQID, 1, &info,
285*4d495c6eSApple OSS Distributions 	    PROC_PIDT_BSDINFOWITHUNIQID_SIZE);
286*4d495c6eSApple OSS Distributions 	T_ASSERT_EQ(ret, (int)sizeof(info), "proc_pidinfo");
287*4d495c6eSApple OSS Distributions 	bool is_proc_mte_enabled = (info.pbsd.pbi_flags & PROC_FLAG_SEC_ENABLED) != 0;
288*4d495c6eSApple OSS Distributions 
289*4d495c6eSApple OSS Distributions 	/* Then we observe that the process is MTE-enabled, despite us not doing anything special */
290*4d495c6eSApple OSS Distributions 	T_ASSERT_TRUE(is_proc_mte_enabled, "Expected 1p dexts to be MTE-enabled by default");
291*4d495c6eSApple OSS Distributions }
292*4d495c6eSApple OSS Distributions 
293*4d495c6eSApple OSS Distributions T_DECL(mte_double_entitlement_setting_failure,
294*4d495c6eSApple OSS Distributions     "Execute a binary which has both the com.apple.developer and com.apple.security"
295*4d495c6eSApple OSS Distributions     " set of entitlements and verify that we fail execution.",
296*4d495c6eSApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
297*4d495c6eSApple OSS Distributions     XNU_T_META_SOC_SPECIFIC) {
298*4d495c6eSApple OSS Distributions 	pid_t child_pid = 0;
299*4d495c6eSApple OSS Distributions 	/* We should not get to execute the binary at all, so no need to have the right arguments. */
300*4d495c6eSApple OSS Distributions 	char *args[] = { "arm_mte_spawn_client_with_invalid_entitlement_setting", NULL};
301*4d495c6eSApple OSS Distributions 
302*4d495c6eSApple OSS Distributions 	int ret = posix_spawn(&child_pid, args[0], NULL, NULL, args, NULL);
303*4d495c6eSApple OSS Distributions 	T_ASSERT_NE(0, ret, "poisx_spawn with double entitlement must fail");
304*4d495c6eSApple OSS Distributions }
305