xref: /xnu-12377.41.6/tests/arm_mte.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions  * Copyright (c) 2023 Apple Computer, Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions  *
4*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions  *
6*bbb1b6f9SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions  *
15*bbb1b6f9SApple OSS Distributions  * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions  *
18*bbb1b6f9SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions  * limitations under the License.
25*bbb1b6f9SApple OSS Distributions  *
26*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions  */
28*bbb1b6f9SApple OSS Distributions 
29*bbb1b6f9SApple OSS Distributions #include <arm_acle.h>
30*bbb1b6f9SApple OSS Distributions #include <darwintest.h>
31*bbb1b6f9SApple OSS Distributions #include <fcntl.h>
32*bbb1b6f9SApple OSS Distributions #include <mach/mach.h>
33*bbb1b6f9SApple OSS Distributions #include <mach/mach_vm.h>
34*bbb1b6f9SApple OSS Distributions #include <mach/vm_map.h>
35*bbb1b6f9SApple OSS Distributions #include <mach-o/dyld.h>
36*bbb1b6f9SApple OSS Distributions #include <spawn_private.h>
37*bbb1b6f9SApple OSS Distributions #include <sys/aio.h>
38*bbb1b6f9SApple OSS Distributions #include <sys/spawn_internal.h>
39*bbb1b6f9SApple OSS Distributions #include <stdlib.h>
40*bbb1b6f9SApple OSS Distributions #include <sys/mman.h>
41*bbb1b6f9SApple OSS Distributions #include <sys/sysctl.h>
42*bbb1b6f9SApple OSS Distributions #include <sys/wait.h>
43*bbb1b6f9SApple OSS Distributions #include <signal.h>
44*bbb1b6f9SApple OSS Distributions 
45*bbb1b6f9SApple OSS Distributions #include "arm_mte_utilities.h"
46*bbb1b6f9SApple OSS Distributions #include "test_utils.h"
47*bbb1b6f9SApple OSS Distributions 
48*bbb1b6f9SApple OSS Distributions #if (TARGET_OS_OSX || TARGET_OS_IOS) && defined(__arm64__)
49*bbb1b6f9SApple OSS Distributions // TODO(PT): It'd be nice to have this as an allow list rather than the inverse,
50*bbb1b6f9SApple OSS Distributions // but I wasn't able to restrict based on TARGET_OS_[IPHONE|IOS] as this is sometimes set even for XR_OS.
51*bbb1b6f9SApple OSS Distributions // For now, to keep things moving, just restrict this from being set on platforms where
52*bbb1b6f9SApple OSS Distributions // we know it's not the case.
53*bbb1b6f9SApple OSS Distributions #if !(TARGET_OS_XR || TARGET_OS_TV || TARGET_OS_WATCH || TARGET_OS_BRIDGE)
54*bbb1b6f9SApple OSS Distributions 	#define TARGET_SUPPORTS_MTE_EMULATION 1
55*bbb1b6f9SApple OSS Distributions #endif
56*bbb1b6f9SApple OSS Distributions #endif
57*bbb1b6f9SApple OSS Distributions 
58*bbb1b6f9SApple OSS Distributions T_GLOBAL_META(
59*bbb1b6f9SApple OSS Distributions 	T_META_NAMESPACE("xnu.arm"),
60*bbb1b6f9SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
61*bbb1b6f9SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("arm"),
62*bbb1b6f9SApple OSS Distributions 	T_META_OWNER("ghackmann"),
63*bbb1b6f9SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true),
64*bbb1b6f9SApple OSS Distributions 	T_META_IGNORECRASHES(".*arm_mte.*"),
65*bbb1b6f9SApple OSS Distributions 	T_META_CHECK_LEAKS(false));
66*bbb1b6f9SApple OSS Distributions 
67*bbb1b6f9SApple OSS Distributions static uint64_t
task_footprint(void)68*bbb1b6f9SApple OSS Distributions task_footprint(void)
69*bbb1b6f9SApple OSS Distributions {
70*bbb1b6f9SApple OSS Distributions 	task_vm_info_data_t ti;
71*bbb1b6f9SApple OSS Distributions 	kern_return_t kr;
72*bbb1b6f9SApple OSS Distributions 	mach_msg_type_number_t count;
73*bbb1b6f9SApple OSS Distributions 
74*bbb1b6f9SApple OSS Distributions 	count = TASK_VM_INFO_COUNT;
75*bbb1b6f9SApple OSS Distributions 	kr = task_info(mach_task_self(),
76*bbb1b6f9SApple OSS Distributions 	    TASK_VM_INFO,
77*bbb1b6f9SApple OSS Distributions 	    (task_info_t) &ti,
78*bbb1b6f9SApple OSS Distributions 	    &count);
79*bbb1b6f9SApple OSS Distributions 	T_QUIET;
80*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "task_info()");
81*bbb1b6f9SApple OSS Distributions #if defined(__arm64__)
82*bbb1b6f9SApple OSS Distributions 	T_QUIET;
83*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(count, TASK_VM_INFO_COUNT, "task_info() count = %d (expected %d)",
84*bbb1b6f9SApple OSS Distributions 	    count, TASK_VM_INFO_COUNT);
85*bbb1b6f9SApple OSS Distributions #endif /* defined(__arm64__) */
86*bbb1b6f9SApple OSS Distributions 	return ti.phys_footprint;
87*bbb1b6f9SApple OSS Distributions }
88*bbb1b6f9SApple OSS Distributions 
89*bbb1b6f9SApple OSS Distributions static void
do_mte_tag_check(void)90*bbb1b6f9SApple OSS Distributions do_mte_tag_check(void)
91*bbb1b6f9SApple OSS Distributions {
92*bbb1b6f9SApple OSS Distributions 	static const size_t ALLOC_SIZE = MTE_GRANULE_SIZE * 2;
93*bbb1b6f9SApple OSS Distributions 
94*bbb1b6f9SApple OSS Distributions 	vm_address_t address = 0;
95*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = vm_allocate(mach_task_self(), &address, ALLOC_SIZE, VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
96*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "allocate tagged memory");
97*bbb1b6f9SApple OSS Distributions 	char *untagged_ptr = (char *)address;
98*bbb1b6f9SApple OSS Distributions 
99*bbb1b6f9SApple OSS Distributions 	char *orig_tagged_ptr = __arm_mte_get_tag(untagged_ptr);
100*bbb1b6f9SApple OSS Distributions 	unsigned int orig_tag = extract_mte_tag(orig_tagged_ptr);
101*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ_UINT(orig_tag, 0U, "originally assigned tag is zero");
102*bbb1b6f9SApple OSS Distributions 
103*bbb1b6f9SApple OSS Distributions 	uint64_t mask = __arm_mte_exclude_tag(orig_tagged_ptr, 0);
104*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ_LLONG(mask, (1LL << 0), "zero tag is excluded");
105*bbb1b6f9SApple OSS Distributions 
106*bbb1b6f9SApple OSS Distributions 	char *random_tagged_ptr = NULL;
107*bbb1b6f9SApple OSS Distributions 	/*
108*bbb1b6f9SApple OSS Distributions 	 * Generate the random tag.  We've excluded the original tag, so it should never
109*bbb1b6f9SApple OSS Distributions 	 * reappear no matter how many times we regenerate a new tag.
110*bbb1b6f9SApple OSS Distributions 	 */
111*bbb1b6f9SApple OSS Distributions 	for (unsigned int i = 0; i < NUM_MTE_TAGS * 4; i++) {
112*bbb1b6f9SApple OSS Distributions 		random_tagged_ptr = __arm_mte_create_random_tag(untagged_ptr, mask);
113*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_EXPECT_NE_PTR(orig_tagged_ptr, random_tagged_ptr,
114*bbb1b6f9SApple OSS Distributions 		    "random tag was not taken from excluded tag set");
115*bbb1b6f9SApple OSS Distributions 
116*bbb1b6f9SApple OSS Distributions 		ptrdiff_t diff = __arm_mte_ptrdiff(untagged_ptr, random_tagged_ptr);
117*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_EXPECT_EQ_ULONG(diff, (ptrdiff_t)0, "untagged %p and tagged %p have identical address bits",
118*bbb1b6f9SApple OSS Distributions 		    untagged_ptr, random_tagged_ptr);
119*bbb1b6f9SApple OSS Distributions 	}
120*bbb1b6f9SApple OSS Distributions 
121*bbb1b6f9SApple OSS Distributions 	/* Time to make things real, commit the tag to memory */
122*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag(random_tagged_ptr);
123*bbb1b6f9SApple OSS Distributions 
124*bbb1b6f9SApple OSS Distributions 	/* Ensure that we can read back the tag */
125*bbb1b6f9SApple OSS Distributions 	char *read_back = __arm_mte_get_tag(untagged_ptr);
126*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ_PTR(read_back, random_tagged_ptr, "tag was committed to memory correctly");
127*bbb1b6f9SApple OSS Distributions 
128*bbb1b6f9SApple OSS Distributions 	/* Verify that accessing memory actually works */
129*bbb1b6f9SApple OSS Distributions 	random_tagged_ptr[0] = 't';
130*bbb1b6f9SApple OSS Distributions 	random_tagged_ptr[1] = 'e';
131*bbb1b6f9SApple OSS Distributions 	random_tagged_ptr[2] = 's';
132*bbb1b6f9SApple OSS Distributions 	random_tagged_ptr[3] = 't';
133*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ_STR(random_tagged_ptr, "test", "read/write from tagged memory");
134*bbb1b6f9SApple OSS Distributions 
135*bbb1b6f9SApple OSS Distributions 	/*
136*bbb1b6f9SApple OSS Distributions 	 * Confirm that the next MTE granule still has the default tag, and then
137*bbb1b6f9SApple OSS Distributions 	 * simulate an out-of-bounds access into that granule.
138*bbb1b6f9SApple OSS Distributions 	 */
139*bbb1b6f9SApple OSS Distributions 	void *next_granule_ptr = orig_tagged_ptr + MTE_GRANULE_SIZE;
140*bbb1b6f9SApple OSS Distributions 	unsigned int next_granule_tag = extract_mte_tag(next_granule_ptr);
141*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_UINT(next_granule_tag, 0U,
142*bbb1b6f9SApple OSS Distributions 	    "next MTE granule still has its originally assigned tag");
143*bbb1b6f9SApple OSS Distributions 
144*bbb1b6f9SApple OSS Distributions 	T_LOG("attempting out-of-bounds access to tagged memory");
145*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
146*bbb1b6f9SApple OSS Distributions 		random_tagged_ptr[MTE_GRANULE_SIZE] = '!';
147*bbb1b6f9SApple OSS Distributions 	}, "out-of-bounds access to tagged memory raises uncatchable exception");
148*bbb1b6f9SApple OSS Distributions 
149*bbb1b6f9SApple OSS Distributions 	/*
150*bbb1b6f9SApple OSS Distributions 	 * Simulate a use-after-free by accessing orig_tagged_ptr, which has an
151*bbb1b6f9SApple OSS Distributions 	 * out-of-date tag.
152*bbb1b6f9SApple OSS Distributions 	 */
153*bbb1b6f9SApple OSS Distributions 	T_LOG("attempting use-after-free access to tagged memory");
154*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
155*bbb1b6f9SApple OSS Distributions 		orig_tagged_ptr[0] = 'T';
156*bbb1b6f9SApple OSS Distributions 	}, "use-after-free access to tagged memory raises uncatchable exception");
157*bbb1b6f9SApple OSS Distributions 
158*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag(orig_tagged_ptr);
159*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag(orig_tagged_ptr + MTE_GRANULE_SIZE);
160*bbb1b6f9SApple OSS Distributions 	vm_deallocate(mach_task_self(), address, ALLOC_SIZE);
161*bbb1b6f9SApple OSS Distributions }
162*bbb1b6f9SApple OSS Distributions 
163*bbb1b6f9SApple OSS Distributions T_DECL(mte_tag_check,
164*bbb1b6f9SApple OSS Distributions     "Test MTE2 tag check fault handling",
165*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
166*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
167*bbb1b6f9SApple OSS Distributions {
168*bbb1b6f9SApple OSS Distributions #if !__arm64__
169*bbb1b6f9SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
170*bbb1b6f9SApple OSS Distributions #else /* !__arm64__ */
171*bbb1b6f9SApple OSS Distributions 	do_mte_tag_check();
172*bbb1b6f9SApple OSS Distributions #endif
173*bbb1b6f9SApple OSS Distributions }
174*bbb1b6f9SApple OSS Distributions 
175*bbb1b6f9SApple OSS Distributions T_DECL(mte_tag_check_child,
176*bbb1b6f9SApple OSS Distributions     "Test MTE2 tag check fault in a child process",
177*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
178*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
179*bbb1b6f9SApple OSS Distributions {
180*bbb1b6f9SApple OSS Distributions #if !__arm64__
181*bbb1b6f9SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
182*bbb1b6f9SApple OSS Distributions #else /* !__arm64__ */
183*bbb1b6f9SApple OSS Distributions 	pid_t pid = fork();
184*bbb1b6f9SApple OSS Distributions 	if (pid == 0) {
185*bbb1b6f9SApple OSS Distributions 		/*
186*bbb1b6f9SApple OSS Distributions 		 * Make sure the child process also has tag checks enabled.
187*bbb1b6f9SApple OSS Distributions 		 */
188*bbb1b6f9SApple OSS Distributions 		do_mte_tag_check();
189*bbb1b6f9SApple OSS Distributions 	} else {
190*bbb1b6f9SApple OSS Distributions 		T_ASSERT_TRUE(pid != -1, "Checking fork success in parent");
191*bbb1b6f9SApple OSS Distributions 
192*bbb1b6f9SApple OSS Distributions 		int status = 0;
193*bbb1b6f9SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(waitpid(pid, &status, 0), "waitpid");
194*bbb1b6f9SApple OSS Distributions 	}
195*bbb1b6f9SApple OSS Distributions #endif
196*bbb1b6f9SApple OSS Distributions }
197*bbb1b6f9SApple OSS Distributions 
198*bbb1b6f9SApple OSS Distributions T_DECL(mte_canonical_tag_check,
199*bbb1b6f9SApple OSS Distributions     "Test MTE4 Canonical Tag Check fault handling",
200*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
201*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
202*bbb1b6f9SApple OSS Distributions {
203*bbb1b6f9SApple OSS Distributions #if !__arm64__
204*bbb1b6f9SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
205*bbb1b6f9SApple OSS Distributions #else /* !__arm64__ */
206*bbb1b6f9SApple OSS Distributions 	vm_address_t address = 0;
207*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = vm_allocate(mach_task_self(), &address, MTE_GRANULE_SIZE, VM_FLAGS_ANYWHERE);
208*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "allocate a canonically-tagged page");
209*bbb1b6f9SApple OSS Distributions 	char *ptr = (char *)address;
210*bbb1b6f9SApple OSS Distributions 
211*bbb1b6f9SApple OSS Distributions 	T_LOG("attempting to set tag on canonically-tagged memory");
212*bbb1b6f9SApple OSS Distributions 	char *tagged_ptr = __arm_mte_increment_tag(ptr, 1);
213*bbb1b6f9SApple OSS Distributions 	expect_signal(SIGBUS, ^{
214*bbb1b6f9SApple OSS Distributions 		__arm_mte_set_tag(tagged_ptr);
215*bbb1b6f9SApple OSS Distributions 	}, "setting tag on canonically-tagged memory raises a canonical memory permission fault");
216*bbb1b6f9SApple OSS Distributions 
217*bbb1b6f9SApple OSS Distributions 	T_LOG("attempting to access canonically-tagged memory with a tagged address");
218*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
219*bbb1b6f9SApple OSS Distributions 		tagged_ptr[0] = '!';
220*bbb1b6f9SApple OSS Distributions 	}, "accessing canonically-tagged memory with a tagged address raises a canonical tag check fault");
221*bbb1b6f9SApple OSS Distributions 
222*bbb1b6f9SApple OSS Distributions 	vm_deallocate(mach_task_self(), address, MTE_GRANULE_SIZE);
223*bbb1b6f9SApple OSS Distributions #endif
224*bbb1b6f9SApple OSS Distributions }
225*bbb1b6f9SApple OSS Distributions 
226*bbb1b6f9SApple OSS Distributions static void
run_mte_copyio_tests(bool tag_check_faults_enabled)227*bbb1b6f9SApple OSS Distributions run_mte_copyio_tests(bool tag_check_faults_enabled)
228*bbb1b6f9SApple OSS Distributions {
229*bbb1b6f9SApple OSS Distributions 	static_assert(MAXTHREADNAMESIZE >= MTE_GRANULE_SIZE * 2, "kern.threadname parameter can span multiple MTE granules");
230*bbb1b6f9SApple OSS Distributions 
231*bbb1b6f9SApple OSS Distributions 	const size_t buf_size = MAXTHREADNAMESIZE;
232*bbb1b6f9SApple OSS Distributions 	const size_t threadname_len = MTE_GRANULE_SIZE * 2;
233*bbb1b6f9SApple OSS Distributions 	vm_address_t address = 0;
234*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = vm_allocate(mach_task_self(), &address, buf_size, VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
235*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "allocate tagged memory");
236*bbb1b6f9SApple OSS Distributions 
237*bbb1b6f9SApple OSS Distributions 	char *untagged_ptr = (char *)address;
238*bbb1b6f9SApple OSS Distributions 	/* n.b.: kern.threadname uses unterminated strings */
239*bbb1b6f9SApple OSS Distributions 	memset(untagged_ptr, 'A', threadname_len);
240*bbb1b6f9SApple OSS Distributions 
241*bbb1b6f9SApple OSS Distributions 	char *tagged_ptr = __arm_mte_create_random_tag(untagged_ptr, 0);
242*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag(tagged_ptr);
243*bbb1b6f9SApple OSS Distributions 	char *next_granule_ptr = tagged_ptr + MTE_GRANULE_SIZE;
244*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag(next_granule_ptr);
245*bbb1b6f9SApple OSS Distributions 
246*bbb1b6f9SApple OSS Distributions 	int err = sysctlbyname("kern.threadname", NULL, NULL, tagged_ptr, threadname_len);
247*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(err, "copyin using tagged pointer succeeds");
248*bbb1b6f9SApple OSS Distributions 
249*bbb1b6f9SApple OSS Distributions 	/* Simulate use-after-free by passing in obsolete tag */
250*bbb1b6f9SApple OSS Distributions 	if (tag_check_faults_enabled) {
251*bbb1b6f9SApple OSS Distributions 		expect_sigkill(^{
252*bbb1b6f9SApple OSS Distributions 			sysctlbyname("kern.threadname", NULL, NULL, untagged_ptr, threadname_len);
253*bbb1b6f9SApple OSS Distributions 		}, "copyin using incorrectly-tagged pointer");
254*bbb1b6f9SApple OSS Distributions 	} else {
255*bbb1b6f9SApple OSS Distributions 		err = sysctlbyname("kern.threadname", NULL, NULL, untagged_ptr, threadname_len);
256*bbb1b6f9SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(err, "bypass: copyin using incorrectly-tagged pointer succeeds");
257*bbb1b6f9SApple OSS Distributions 	}
258*bbb1b6f9SApple OSS Distributions 
259*bbb1b6f9SApple OSS Distributions 	/* Simulate out-of-bounds access by giving the second MTE granule a different tag */
260*bbb1b6f9SApple OSS Distributions 	char *different_tag_next_granule_ptr = __arm_mte_increment_tag(next_granule_ptr, 1);
261*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_NE(different_tag_next_granule_ptr, next_granule_ptr, "__arm_mte_increment_tag()");
262*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag(different_tag_next_granule_ptr);
263*bbb1b6f9SApple OSS Distributions 	if (tag_check_faults_enabled) {
264*bbb1b6f9SApple OSS Distributions 		expect_sigkill(^{
265*bbb1b6f9SApple OSS Distributions 			sysctlbyname("kern.threadname", NULL, NULL, tagged_ptr, threadname_len);
266*bbb1b6f9SApple OSS Distributions 		}, "copyin using inconsistently-tagged buffer");
267*bbb1b6f9SApple OSS Distributions 	} else {
268*bbb1b6f9SApple OSS Distributions 		err = sysctlbyname("kern.threadname", NULL, NULL, tagged_ptr, threadname_len);
269*bbb1b6f9SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(err, "bypass: copyin using inconsistently-tagged buffer succeeds");
270*bbb1b6f9SApple OSS Distributions 	}
271*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag(next_granule_ptr);
272*bbb1b6f9SApple OSS Distributions 
273*bbb1b6f9SApple OSS Distributions 	size_t oldlen = buf_size;
274*bbb1b6f9SApple OSS Distributions 	err = sysctlbyname("kern.threadname", tagged_ptr, &oldlen, NULL, 0);
275*bbb1b6f9SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(err, "copyout using tagged pointer succeeds");
276*bbb1b6f9SApple OSS Distributions 
277*bbb1b6f9SApple OSS Distributions #pragma clang diagnostic push
278*bbb1b6f9SApple OSS Distributions #pragma clang diagnostic ignored "-Wshadow"
279*bbb1b6f9SApple OSS Distributions 
280*bbb1b6f9SApple OSS Distributions 	if (tag_check_faults_enabled) {
281*bbb1b6f9SApple OSS Distributions 		expect_sigkill(^{
282*bbb1b6f9SApple OSS Distributions 			/* We need to repopulate kern.threadname since it isn't inherited across fork() */
283*bbb1b6f9SApple OSS Distributions 			int err = sysctlbyname("kern.threadname", NULL, NULL, tagged_ptr, threadname_len);
284*bbb1b6f9SApple OSS Distributions 			T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "sysctlbyname(kern.threadname)");
285*bbb1b6f9SApple OSS Distributions 
286*bbb1b6f9SApple OSS Distributions 			size_t oldlen = buf_size;
287*bbb1b6f9SApple OSS Distributions 			sysctlbyname("kern.threadname", untagged_ptr, &oldlen, NULL, 0);
288*bbb1b6f9SApple OSS Distributions 		}, "copyout using incorrectly-tagged pointer");
289*bbb1b6f9SApple OSS Distributions 	} else {
290*bbb1b6f9SApple OSS Distributions 		size_t oldlen = buf_size;
291*bbb1b6f9SApple OSS Distributions 		int err = sysctlbyname("kern.threadname", untagged_ptr, &oldlen, NULL, 0);
292*bbb1b6f9SApple OSS Distributions 		T_EXPECT_POSIX_SUCCESS(err, "bypass: copyout using incorrectly-tagged pointer succeeds");
293*bbb1b6f9SApple OSS Distributions 	}
294*bbb1b6f9SApple OSS Distributions 
295*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag(different_tag_next_granule_ptr);
296*bbb1b6f9SApple OSS Distributions 	if (tag_check_faults_enabled) {
297*bbb1b6f9SApple OSS Distributions 		expect_sigkill(^{
298*bbb1b6f9SApple OSS Distributions 			int err = sysctlbyname("kern.threadname", NULL, NULL, tagged_ptr, threadname_len);
299*bbb1b6f9SApple OSS Distributions 			T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "sysctlbyname(kern.threadname)");
300*bbb1b6f9SApple OSS Distributions 
301*bbb1b6f9SApple OSS Distributions 			size_t oldlen = buf_size;
302*bbb1b6f9SApple OSS Distributions 			sysctlbyname("kern.threadname", tagged_ptr, &oldlen, NULL, 0);
303*bbb1b6f9SApple OSS Distributions 		}, "copyout using inconsistently-tagged buffer");
304*bbb1b6f9SApple OSS Distributions 	} else {
305*bbb1b6f9SApple OSS Distributions 		size_t oldlen = buf_size;
306*bbb1b6f9SApple OSS Distributions 		int err = sysctlbyname("kern.threadname", tagged_ptr, &oldlen, NULL, 0);
307*bbb1b6f9SApple OSS Distributions 		T_EXPECT_POSIX_SUCCESS(err, "bypass: copyout using inconsistently-tagged buffer succeeds");
308*bbb1b6f9SApple OSS Distributions 	}
309*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag(next_granule_ptr);
310*bbb1b6f9SApple OSS Distributions 
311*bbb1b6f9SApple OSS Distributions #pragma clang diagnostic pop
312*bbb1b6f9SApple OSS Distributions 
313*bbb1b6f9SApple OSS Distributions 	vm_deallocate(mach_task_self(), address, buf_size);
314*bbb1b6f9SApple OSS Distributions }
315*bbb1b6f9SApple OSS Distributions 
316*bbb1b6f9SApple OSS Distributions T_DECL(mte_copyio,
317*bbb1b6f9SApple OSS Distributions     "Test MTE tag handling during copyin/copyout operations",
318*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
319*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
320*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
321*bbb1b6f9SApple OSS Distributions {
322*bbb1b6f9SApple OSS Distributions 	run_mte_copyio_tests(true);
323*bbb1b6f9SApple OSS Distributions }
324*bbb1b6f9SApple OSS Distributions 
325*bbb1b6f9SApple OSS Distributions T_DECL(mte_malloc_footprint_test,
326*bbb1b6f9SApple OSS Distributions     "Test footprint across malloc() and free()",
327*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
328*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
329*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(false) /* rdar://131390446 */)
330*bbb1b6f9SApple OSS Distributions {
331*bbb1b6f9SApple OSS Distributions #if !__arm64__
332*bbb1b6f9SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
333*bbb1b6f9SApple OSS Distributions #else /* !__arm64__ */
334*bbb1b6f9SApple OSS Distributions 	uint64_t count = 1024;
335*bbb1b6f9SApple OSS Distributions 	uint64_t margin = 4;
336*bbb1b6f9SApple OSS Distributions 	char* address[count];
337*bbb1b6f9SApple OSS Distributions 	uint64_t size = PAGE_SIZE;
338*bbb1b6f9SApple OSS Distributions 
339*bbb1b6f9SApple OSS Distributions 	for (unsigned int i = 0; i < count; i++) {
340*bbb1b6f9SApple OSS Distributions 		address[i] = (char *) malloc(size);
341*bbb1b6f9SApple OSS Distributions 
342*bbb1b6f9SApple OSS Distributions 		char *cp;
343*bbb1b6f9SApple OSS Distributions 		for (cp = (char *) (address[i]); cp < (char *) (address[i] + size); cp += PAGE_SIZE) {
344*bbb1b6f9SApple OSS Distributions 			*cp = 'x';
345*bbb1b6f9SApple OSS Distributions 		}
346*bbb1b6f9SApple OSS Distributions 	}
347*bbb1b6f9SApple OSS Distributions 
348*bbb1b6f9SApple OSS Distributions 	uint64_t fp1 = task_footprint();
349*bbb1b6f9SApple OSS Distributions 	T_LOG("Footprint after malloc(): %llu bytes", fp1);
350*bbb1b6f9SApple OSS Distributions 
351*bbb1b6f9SApple OSS Distributions 	for (unsigned int i = 0; i < count; i++) {
352*bbb1b6f9SApple OSS Distributions 		free(address[i]);
353*bbb1b6f9SApple OSS Distributions 	}
354*bbb1b6f9SApple OSS Distributions 	uint64_t fp2 = task_footprint();
355*bbb1b6f9SApple OSS Distributions 	T_LOG("Footprint after free(): %llu bytes", fp2);
356*bbb1b6f9SApple OSS Distributions 
357*bbb1b6f9SApple OSS Distributions 	T_EXPECT_TRUE(((fp2 + PAGE_SIZE * (count - margin)) <= fp1), "Footprint after free() is higher than expected.");
358*bbb1b6f9SApple OSS Distributions #endif
359*bbb1b6f9SApple OSS Distributions }
360*bbb1b6f9SApple OSS Distributions 
361*bbb1b6f9SApple OSS Distributions T_DECL(mte_tagged_memory_direct_io,
362*bbb1b6f9SApple OSS Distributions     "Test direct I/O on tagged memory",
363*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
364*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
365*bbb1b6f9SApple OSS Distributions {
366*bbb1b6f9SApple OSS Distributions #if !__arm64__
367*bbb1b6f9SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
368*bbb1b6f9SApple OSS Distributions #else /* !__arm64__ */
369*bbb1b6f9SApple OSS Distributions 
370*bbb1b6f9SApple OSS Distributions 	uint64_t size = PAGE_SIZE;
371*bbb1b6f9SApple OSS Distributions 	char* address = (char*) malloc(size);
372*bbb1b6f9SApple OSS Distributions 
373*bbb1b6f9SApple OSS Distributions 	char *cp;
374*bbb1b6f9SApple OSS Distributions 	for (cp = (char *) (address); cp < (char *) (address + size); cp += PAGE_SIZE) {
375*bbb1b6f9SApple OSS Distributions 		*cp = 'x';
376*bbb1b6f9SApple OSS Distributions 	}
377*bbb1b6f9SApple OSS Distributions 
378*bbb1b6f9SApple OSS Distributions 	int fd = open("/tmp/file1", O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0644);
379*bbb1b6f9SApple OSS Distributions 	T_ASSERT_TRUE(fd > 0, "File open successful");
380*bbb1b6f9SApple OSS Distributions 	T_ASSERT_TRUE(((fcntl(fd, F_NOCACHE, 1)) != -1), "Setting F_NOCACHE");
381*bbb1b6f9SApple OSS Distributions 	ssize_t ret = pwrite(fd, address, size, 0);
382*bbb1b6f9SApple OSS Distributions 	T_ASSERT_TRUE((uint64_t) ret == size, "pwrite() on tagged memory");
383*bbb1b6f9SApple OSS Distributions 
384*bbb1b6f9SApple OSS Distributions 	char *incorrectly_tagged = __arm_mte_increment_tag(address, 1);
385*bbb1b6f9SApple OSS Distributions 	ret = pwrite(fd, incorrectly_tagged, size, 0);
386*bbb1b6f9SApple OSS Distributions 	T_ASSERT_TRUE((uint64_t) ret == size, "pwrite() on incorrectly tagged memory passes with direct I/O");
387*bbb1b6f9SApple OSS Distributions 
388*bbb1b6f9SApple OSS Distributions 	free(address);
389*bbb1b6f9SApple OSS Distributions #endif
390*bbb1b6f9SApple OSS Distributions }
391*bbb1b6f9SApple OSS Distributions 
392*bbb1b6f9SApple OSS Distributions T_DECL(mte_tagged_memory_copy_io,
393*bbb1b6f9SApple OSS Distributions     "Test direct I/O on tagged memory",
394*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
395*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
396*bbb1b6f9SApple OSS Distributions {
397*bbb1b6f9SApple OSS Distributions #if !__arm64__
398*bbb1b6f9SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
399*bbb1b6f9SApple OSS Distributions #else /* !__arm64__ */
400*bbb1b6f9SApple OSS Distributions 
401*bbb1b6f9SApple OSS Distributions 	uint64_t size = PAGE_SIZE;
402*bbb1b6f9SApple OSS Distributions 	char* address = (char*) malloc(size);
403*bbb1b6f9SApple OSS Distributions 
404*bbb1b6f9SApple OSS Distributions 	char *cp;
405*bbb1b6f9SApple OSS Distributions 	for (cp = (char *) (address); cp < (char *) (address + size); cp += PAGE_SIZE) {
406*bbb1b6f9SApple OSS Distributions 		*cp = 'x';
407*bbb1b6f9SApple OSS Distributions 	}
408*bbb1b6f9SApple OSS Distributions 
409*bbb1b6f9SApple OSS Distributions 	int fd = open("/tmp/file1", O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0644);
410*bbb1b6f9SApple OSS Distributions 	T_ASSERT_TRUE(fd > 0, "File open successful");
411*bbb1b6f9SApple OSS Distributions 	ssize_t ret = pwrite(fd, address, size, 0);
412*bbb1b6f9SApple OSS Distributions 	T_ASSERT_TRUE((uint64_t) ret == size, "pwrite() on tagged memory");
413*bbb1b6f9SApple OSS Distributions 
414*bbb1b6f9SApple OSS Distributions 	char *incorrectly_tagged = __arm_mte_increment_tag(address, 1);
415*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
416*bbb1b6f9SApple OSS Distributions 		(void)pwrite(fd, incorrectly_tagged, size, 0);
417*bbb1b6f9SApple OSS Distributions 	}, "copy I/O on wrongly tagged memory");
418*bbb1b6f9SApple OSS Distributions 
419*bbb1b6f9SApple OSS Distributions 	free(address);
420*bbb1b6f9SApple OSS Distributions #endif
421*bbb1b6f9SApple OSS Distributions }
422*bbb1b6f9SApple OSS Distributions 
423*bbb1b6f9SApple OSS Distributions 
424*bbb1b6f9SApple OSS Distributions static int FORK_TEST_CHILD_WRITES_FIRST = 0x1;
425*bbb1b6f9SApple OSS Distributions static int FORK_TEST_CHILD_FORKS = 0x2;
426*bbb1b6f9SApple OSS Distributions static int FORK_TEST_CHILD_RETAGS = 0x4;
427*bbb1b6f9SApple OSS Distributions static void
do_fork_test(vm_size_t vm_alloc_sz,int flags)428*bbb1b6f9SApple OSS Distributions do_fork_test(vm_size_t vm_alloc_sz, int flags)
429*bbb1b6f9SApple OSS Distributions {
430*bbb1b6f9SApple OSS Distributions #if !__arm64__
431*bbb1b6f9SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
432*bbb1b6f9SApple OSS Distributions #else /* !__arm64__ */
433*bbb1b6f9SApple OSS Distributions 	vm_address_t address = 0;
434*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = vm_allocate(mach_task_self(), &address, vm_alloc_sz, VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
435*bbb1b6f9SApple OSS Distributions 
436*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "allocate tagged memory");
437*bbb1b6f9SApple OSS Distributions 
438*bbb1b6f9SApple OSS Distributions 	char *untagged_ptr = (char *)address;
439*bbb1b6f9SApple OSS Distributions 	char *orig_tagged_ptr = __arm_mte_get_tag(untagged_ptr);
440*bbb1b6f9SApple OSS Distributions 	uint64_t mask = __arm_mte_exclude_tag(orig_tagged_ptr, 0);
441*bbb1b6f9SApple OSS Distributions 
442*bbb1b6f9SApple OSS Distributions 	size_t count;
443*bbb1b6f9SApple OSS Distributions 	size_t offset;
444*bbb1b6f9SApple OSS Distributions 	const vm_size_t NUM_GRANULES = vm_alloc_sz / MTE_GRANULE_SIZE;
445*bbb1b6f9SApple OSS Distributions 	char *tagged_ptrs[NUM_GRANULES];
446*bbb1b6f9SApple OSS Distributions 
447*bbb1b6f9SApple OSS Distributions 	/*
448*bbb1b6f9SApple OSS Distributions 	 * Tag the entire page
449*bbb1b6f9SApple OSS Distributions 	 */
450*bbb1b6f9SApple OSS Distributions 	for (count = 0; count < NUM_GRANULES; count++) {
451*bbb1b6f9SApple OSS Distributions 		offset = count * MTE_GRANULE_SIZE;
452*bbb1b6f9SApple OSS Distributions 		tagged_ptrs[count] = __arm_mte_create_random_tag(untagged_ptr + offset, mask);
453*bbb1b6f9SApple OSS Distributions 		__arm_mte_set_tag(tagged_ptrs[count]);
454*bbb1b6f9SApple OSS Distributions 	}
455*bbb1b6f9SApple OSS Distributions 
456*bbb1b6f9SApple OSS Distributions 	if (!(flags & FORK_TEST_CHILD_WRITES_FIRST)) {
457*bbb1b6f9SApple OSS Distributions 		for (count = 0; count < NUM_GRANULES; count++) {
458*bbb1b6f9SApple OSS Distributions 			*(tagged_ptrs[count]) = 'a';
459*bbb1b6f9SApple OSS Distributions 		}
460*bbb1b6f9SApple OSS Distributions 	}
461*bbb1b6f9SApple OSS Distributions 
462*bbb1b6f9SApple OSS Distributions 	pid_t pid = fork();
463*bbb1b6f9SApple OSS Distributions 	if (pid == 0) {
464*bbb1b6f9SApple OSS Distributions 		T_LOG("Child forked");
465*bbb1b6f9SApple OSS Distributions 
466*bbb1b6f9SApple OSS Distributions 		if (flags & FORK_TEST_CHILD_RETAGS) {
467*bbb1b6f9SApple OSS Distributions 			T_LOG("Child editing tags");
468*bbb1b6f9SApple OSS Distributions 			/* re-tag the entire page */
469*bbb1b6f9SApple OSS Distributions 			for (count = 0; count < NUM_GRANULES; count++) {
470*bbb1b6f9SApple OSS Distributions 				tagged_ptrs[count] = __arm_mte_increment_tag(tagged_ptrs[count], 1);
471*bbb1b6f9SApple OSS Distributions 				__arm_mte_set_tag(tagged_ptrs[count]);
472*bbb1b6f9SApple OSS Distributions 			}
473*bbb1b6f9SApple OSS Distributions 		}
474*bbb1b6f9SApple OSS Distributions 
475*bbb1b6f9SApple OSS Distributions 		T_LOG("Accessing parent tagged memory");
476*bbb1b6f9SApple OSS Distributions 		/*
477*bbb1b6f9SApple OSS Distributions 		 * Make sure the child process also has tag checks enabled.
478*bbb1b6f9SApple OSS Distributions 		 */
479*bbb1b6f9SApple OSS Distributions 		for (count = 0; count < NUM_GRANULES; count++) {
480*bbb1b6f9SApple OSS Distributions 			*(tagged_ptrs[count]) = 'a';
481*bbb1b6f9SApple OSS Distributions 		}
482*bbb1b6f9SApple OSS Distributions 
483*bbb1b6f9SApple OSS Distributions 		T_LOG("Child access to tagged memory success");
484*bbb1b6f9SApple OSS Distributions 
485*bbb1b6f9SApple OSS Distributions 		expect_sigkill(^{
486*bbb1b6f9SApple OSS Distributions 			*untagged_ptr = 'b';
487*bbb1b6f9SApple OSS Distributions 		}, "Child access through untagged ptr");
488*bbb1b6f9SApple OSS Distributions 
489*bbb1b6f9SApple OSS Distributions 		if (flags & FORK_TEST_CHILD_FORKS) {
490*bbb1b6f9SApple OSS Distributions 			pid_t pid2 = fork();
491*bbb1b6f9SApple OSS Distributions 
492*bbb1b6f9SApple OSS Distributions 			if (pid2 == 0) {
493*bbb1b6f9SApple OSS Distributions 				T_LOG("Grandchild forked");
494*bbb1b6f9SApple OSS Distributions 
495*bbb1b6f9SApple OSS Distributions 				T_LOG("Accessing grandparent's tagged memory");
496*bbb1b6f9SApple OSS Distributions 
497*bbb1b6f9SApple OSS Distributions 				for (count = 0; count < NUM_GRANULES; count++) {
498*bbb1b6f9SApple OSS Distributions 					*(tagged_ptrs[count]) = 'a';
499*bbb1b6f9SApple OSS Distributions 				}
500*bbb1b6f9SApple OSS Distributions 
501*bbb1b6f9SApple OSS Distributions 				T_LOG("Grandchild access to tagged memory success");
502*bbb1b6f9SApple OSS Distributions 
503*bbb1b6f9SApple OSS Distributions 				pid_t pid3 = fork();
504*bbb1b6f9SApple OSS Distributions 
505*bbb1b6f9SApple OSS Distributions 				if (pid3 == 0) {
506*bbb1b6f9SApple OSS Distributions 					T_LOG("Great grandchild forked");
507*bbb1b6f9SApple OSS Distributions 
508*bbb1b6f9SApple OSS Distributions 					T_LOG("Accessing great grandparent's tagged memory");
509*bbb1b6f9SApple OSS Distributions 
510*bbb1b6f9SApple OSS Distributions 					for (count = 0; count < NUM_GRANULES; count++) {
511*bbb1b6f9SApple OSS Distributions 						*(tagged_ptrs[count]) = 'a';
512*bbb1b6f9SApple OSS Distributions 					}
513*bbb1b6f9SApple OSS Distributions 
514*bbb1b6f9SApple OSS Distributions 					T_LOG("Great grandchild access to tagged memory success");
515*bbb1b6f9SApple OSS Distributions 
516*bbb1b6f9SApple OSS Distributions 					kr = vm_deallocate(mach_task_self(), address, vm_alloc_sz);
517*bbb1b6f9SApple OSS Distributions 					T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Great grandchild vm_deallocate");
518*bbb1b6f9SApple OSS Distributions 					exit(0);
519*bbb1b6f9SApple OSS Distributions 				} else {
520*bbb1b6f9SApple OSS Distributions 					T_ASSERT_TRUE(pid3 != -1, "Checking fork success in grandchild");
521*bbb1b6f9SApple OSS Distributions 					int status2 = 0;
522*bbb1b6f9SApple OSS Distributions 
523*bbb1b6f9SApple OSS Distributions 					T_ASSERT_POSIX_SUCCESS(waitpid(pid3, &status2, 0), "waitpid");
524*bbb1b6f9SApple OSS Distributions 					T_ASSERT_TRUE(WIFEXITED(status2) > 0, "Great grandchild exited normally");
525*bbb1b6f9SApple OSS Distributions 				}
526*bbb1b6f9SApple OSS Distributions 
527*bbb1b6f9SApple OSS Distributions 				kr = vm_deallocate(mach_task_self(), address, vm_alloc_sz);
528*bbb1b6f9SApple OSS Distributions 				T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Grandchild vm_deallocate");
529*bbb1b6f9SApple OSS Distributions 				exit(0);
530*bbb1b6f9SApple OSS Distributions 			} else {
531*bbb1b6f9SApple OSS Distributions 				T_ASSERT_TRUE(pid2 != -1, "Checking fork success in child");
532*bbb1b6f9SApple OSS Distributions 				int status2 = 0;
533*bbb1b6f9SApple OSS Distributions 				T_ASSERT_POSIX_SUCCESS(waitpid(pid2, &status2, 0), "waitpid");
534*bbb1b6f9SApple OSS Distributions 				T_ASSERT_TRUE(WIFEXITED(status2) > 0, "Grandchild exited normally");
535*bbb1b6f9SApple OSS Distributions 			}
536*bbb1b6f9SApple OSS Distributions 		}
537*bbb1b6f9SApple OSS Distributions 
538*bbb1b6f9SApple OSS Distributions 		kr = vm_deallocate(mach_task_self(), address, vm_alloc_sz);
539*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Child vm_deallocate");
540*bbb1b6f9SApple OSS Distributions 		exit(0);
541*bbb1b6f9SApple OSS Distributions 	} else {
542*bbb1b6f9SApple OSS Distributions 		T_ASSERT_TRUE(pid != -1, "Checking fork success in parent");
543*bbb1b6f9SApple OSS Distributions 
544*bbb1b6f9SApple OSS Distributions 		int status = 0;
545*bbb1b6f9SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(waitpid(pid, &status, 0), "waitpid");
546*bbb1b6f9SApple OSS Distributions 
547*bbb1b6f9SApple OSS Distributions 		T_ASSERT_TRUE(WIFEXITED(status) > 0, "Child exited normally");
548*bbb1b6f9SApple OSS Distributions 
549*bbb1b6f9SApple OSS Distributions 		/* Verify that accessing memory actually works */
550*bbb1b6f9SApple OSS Distributions 		for (count = 0; count < NUM_GRANULES; count++) {
551*bbb1b6f9SApple OSS Distributions 			*(tagged_ptrs[count]) = 'a';
552*bbb1b6f9SApple OSS Distributions 		}
553*bbb1b6f9SApple OSS Distributions 
554*bbb1b6f9SApple OSS Distributions 		T_LOG("Parent access to tagged memory sucessfull");
555*bbb1b6f9SApple OSS Distributions 
556*bbb1b6f9SApple OSS Distributions 		expect_sigkill(^{
557*bbb1b6f9SApple OSS Distributions 			*untagged_ptr = 'b';
558*bbb1b6f9SApple OSS Distributions 		}, "Parent access through untagged ptr");
559*bbb1b6f9SApple OSS Distributions 	}
560*bbb1b6f9SApple OSS Distributions 
561*bbb1b6f9SApple OSS Distributions 	kr = vm_deallocate(mach_task_self(), address, vm_alloc_sz);
562*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Parent vm_deallocate");
563*bbb1b6f9SApple OSS Distributions #endif
564*bbb1b6f9SApple OSS Distributions }
565*bbb1b6f9SApple OSS Distributions 
566*bbb1b6f9SApple OSS Distributions T_DECL(mte_tag_check_fork_after_alloc_less_page_sz,
567*bbb1b6f9SApple OSS Distributions     "Test MTE2 tag check fault in a child process after vm_allocate(ALLOC_SIZE, MTE)",
568*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
569*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
570*bbb1b6f9SApple OSS Distributions {
571*bbb1b6f9SApple OSS Distributions 	static const size_t ALLOC_SIZE = MTE_GRANULE_SIZE * 2;
572*bbb1b6f9SApple OSS Distributions 	do_fork_test(ALLOC_SIZE, 0);
573*bbb1b6f9SApple OSS Distributions }
574*bbb1b6f9SApple OSS Distributions 
575*bbb1b6f9SApple OSS Distributions T_DECL(mte_tag_check_fork_after_alloc_page_sz,
576*bbb1b6f9SApple OSS Distributions     "Test MTE2 tag check fault in a child process after vm_allocate(PAGE_SIZE, MTE)",
577*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
578*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
579*bbb1b6f9SApple OSS Distributions {
580*bbb1b6f9SApple OSS Distributions 	do_fork_test(PAGE_SIZE, 0);
581*bbb1b6f9SApple OSS Distributions }
582*bbb1b6f9SApple OSS Distributions 
583*bbb1b6f9SApple OSS Distributions /* NOTE: These following tests matter for when we switch to MEMORY_OBJECT_COPY_DELAY_FORK */
584*bbb1b6f9SApple OSS Distributions T_DECL(mte_tag_check_fork_child_fault_write,
585*bbb1b6f9SApple OSS Distributions     "Test MTE2 tag check fault in a child process after vm_allocate(MTE) and child writes to tagged memory first",
586*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
587*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
588*bbb1b6f9SApple OSS Distributions {
589*bbb1b6f9SApple OSS Distributions 	do_fork_test(PAGE_SIZE, FORK_TEST_CHILD_WRITES_FIRST);
590*bbb1b6f9SApple OSS Distributions }
591*bbb1b6f9SApple OSS Distributions 
592*bbb1b6f9SApple OSS Distributions T_DECL(mte_tag_check_fork_child_double_fork,
593*bbb1b6f9SApple OSS Distributions     "Test MTE2 tag check fault in a child process after vm_allocate(MTE) and child writes to tagged memory first and then forks again",
594*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
595*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
596*bbb1b6f9SApple OSS Distributions {
597*bbb1b6f9SApple OSS Distributions 	do_fork_test(PAGE_SIZE, FORK_TEST_CHILD_WRITES_FIRST | FORK_TEST_CHILD_FORKS);
598*bbb1b6f9SApple OSS Distributions }
599*bbb1b6f9SApple OSS Distributions 
600*bbb1b6f9SApple OSS Distributions /*
601*bbb1b6f9SApple OSS Distributions  * These cases specifically test that tag setting instructions (STG) resolve CoW
602*bbb1b6f9SApple OSS Distributions  * on fork correctly, since the child doesn't fault in the mapping by writing first.
603*bbb1b6f9SApple OSS Distributions  */
604*bbb1b6f9SApple OSS Distributions T_DECL(mte_tag_check_fork_child_retag,
605*bbb1b6f9SApple OSS Distributions     "Test MTE2 tag check fault in a child process after vm_allocate(MTE) and child changes tags",
606*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
607*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
608*bbb1b6f9SApple OSS Distributions {
609*bbb1b6f9SApple OSS Distributions 	do_fork_test(PAGE_SIZE, FORK_TEST_CHILD_RETAGS);
610*bbb1b6f9SApple OSS Distributions }
611*bbb1b6f9SApple OSS Distributions 
612*bbb1b6f9SApple OSS Distributions T_DECL(mte_tag_check_fork_child_fault_write_retag,
613*bbb1b6f9SApple OSS Distributions     "Test MTE2 tag check fault in a child process after vm_allocate(MTE) and child changes tags and writes to tagged memory first",
614*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
615*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
616*bbb1b6f9SApple OSS Distributions {
617*bbb1b6f9SApple OSS Distributions 	do_fork_test(PAGE_SIZE, FORK_TEST_CHILD_WRITES_FIRST | FORK_TEST_CHILD_RETAGS);
618*bbb1b6f9SApple OSS Distributions }
619*bbb1b6f9SApple OSS Distributions 
620*bbb1b6f9SApple OSS Distributions T_DECL(mte_tag_check_fork_child_fault_write_retag_double_fork,
621*bbb1b6f9SApple OSS Distributions     "Test MTE2 tag check fault in a child process after vm_allocate(MTE) and child changes tags, writes to tagged memory first, and then forks again",
622*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
623*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
624*bbb1b6f9SApple OSS Distributions {
625*bbb1b6f9SApple OSS Distributions 	do_fork_test(PAGE_SIZE, FORK_TEST_CHILD_WRITES_FIRST | FORK_TEST_CHILD_RETAGS | FORK_TEST_CHILD_FORKS);
626*bbb1b6f9SApple OSS Distributions }
627*bbb1b6f9SApple OSS Distributions 
628*bbb1b6f9SApple OSS Distributions 
629*bbb1b6f9SApple OSS Distributions T_DECL(mte_userland_uses_fake_kernel_pointer,
630*bbb1b6f9SApple OSS Distributions     "Test that VM correctly rejects kernel-looking pointer from userspace",
631*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
632*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
633*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(__arm64__))
634*bbb1b6f9SApple OSS Distributions {
635*bbb1b6f9SApple OSS Distributions #if __arm64__
636*bbb1b6f9SApple OSS Distributions 	/*
637*bbb1b6f9SApple OSS Distributions 	 * When the VM is given a user address that looks like a kernel pointer,
638*bbb1b6f9SApple OSS Distributions 	 * we want to make sure that it still gets canonicalized as a user address
639*bbb1b6f9SApple OSS Distributions 	 * (rather than a valid kernel pointer).
640*bbb1b6f9SApple OSS Distributions 	 * This should result in a nonsensical pointer that shouldn't exist in any
641*bbb1b6f9SApple OSS Distributions 	 * VM map, so the memory access should fail.
642*bbb1b6f9SApple OSS Distributions 	 */
643*bbb1b6f9SApple OSS Distributions 	vm_address_t addr = 0;
644*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = vm_allocate(
645*bbb1b6f9SApple OSS Distributions 		mach_task_self(),
646*bbb1b6f9SApple OSS Distributions 		&addr,
647*bbb1b6f9SApple OSS Distributions 		MTE_GRANULE_SIZE,
648*bbb1b6f9SApple OSS Distributions 		VM_FLAGS_ANYWHERE);
649*bbb1b6f9SApple OSS Distributions 	T_QUIET;
650*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "allocate an untagged page");
651*bbb1b6f9SApple OSS Distributions 	T_LOG("Allocated untagged page at addr: 0x%lx", addr);
652*bbb1b6f9SApple OSS Distributions 
653*bbb1b6f9SApple OSS Distributions 	/* Create a kernel-like pointer in userspace */
654*bbb1b6f9SApple OSS Distributions 	char *tampered_ptr = (char *)(addr | VM_MIN_KERNEL_ADDRESS);
655*bbb1b6f9SApple OSS Distributions 	T_LOG("Tampered ptr: %p", tampered_ptr);
656*bbb1b6f9SApple OSS Distributions 
657*bbb1b6f9SApple OSS Distributions 	/* segfault is expected, since the pointer is not valid in the userspace map */
658*bbb1b6f9SApple OSS Distributions 	expect_signal(SIGSEGV, ^{
659*bbb1b6f9SApple OSS Distributions 		*tampered_ptr = 'a';
660*bbb1b6f9SApple OSS Distributions 	}, "Accessing kernel-like pointer from userspace");
661*bbb1b6f9SApple OSS Distributions 	vm_deallocate(mach_task_self(), addr, MTE_GRANULE_SIZE);
662*bbb1b6f9SApple OSS Distributions #endif /* __arm64__ */
663*bbb1b6f9SApple OSS Distributions }
664*bbb1b6f9SApple OSS Distributions 
665*bbb1b6f9SApple OSS Distributions /*
666*bbb1b6f9SApple OSS Distributions  * Allocates tagged memory, assigns the memory a tag, and attempts to
667*bbb1b6f9SApple OSS Distributions  * read the memory into its own address space via mach_vm_read().
668*bbb1b6f9SApple OSS Distributions  *
669*bbb1b6f9SApple OSS Distributions  * Also attempts to read the memory into its own address space with an untagged
670*bbb1b6f9SApple OSS Distributions  * pointer, which we expect to fail.
671*bbb1b6f9SApple OSS Distributions  */
672*bbb1b6f9SApple OSS Distributions static void
mte_mach_vm_read(mach_vm_size_t sz)673*bbb1b6f9SApple OSS Distributions mte_mach_vm_read(mach_vm_size_t sz)
674*bbb1b6f9SApple OSS Distributions {
675*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
676*bbb1b6f9SApple OSS Distributions 	__block mach_vm_address_t addr = 0;
677*bbb1b6f9SApple OSS Distributions 	__block vm_offset_t read_addr = 0;
678*bbb1b6f9SApple OSS Distributions 	__block mach_msg_type_number_t read_size = 0;
679*bbb1b6f9SApple OSS Distributions 
680*bbb1b6f9SApple OSS Distributions 	mach_vm_size_t sz_rounded = (sz + (MTE_GRANULE_SIZE - 1)) & (unsigned)~((signed)(MTE_GRANULE_SIZE - 1));
681*bbb1b6f9SApple OSS Distributions 	T_LOG("sz rounded: %llu", sz_rounded);
682*bbb1b6f9SApple OSS Distributions 	/* Allocate some tagged memory */
683*bbb1b6f9SApple OSS Distributions 	T_LOG("Allocate tagged memory");
684*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = mach_vm_allocate(
685*bbb1b6f9SApple OSS Distributions 		mach_task_self(),
686*bbb1b6f9SApple OSS Distributions 		&addr,
687*bbb1b6f9SApple OSS Distributions 		sz_rounded,
688*bbb1b6f9SApple OSS Distributions 		VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
689*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "Allocated tagged page");
690*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_NE_ULLONG(0ULL, addr, "Allocated address is not null");
691*bbb1b6f9SApple OSS Distributions 
692*bbb1b6f9SApple OSS Distributions 	uint64_t *untagged_ptr = (uint64_t *)addr;
693*bbb1b6f9SApple OSS Distributions 
694*bbb1b6f9SApple OSS Distributions 	uint64_t *orig_tagged_ptr = __arm_mte_get_tag(untagged_ptr);
695*bbb1b6f9SApple OSS Distributions 	unsigned int orig_tag = extract_mte_tag(orig_tagged_ptr);
696*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_UINT(orig_tag, 0U, "Originally assigned tag is zero");
697*bbb1b6f9SApple OSS Distributions 
698*bbb1b6f9SApple OSS Distributions 	uint64_t mask = __arm_mte_exclude_tag(orig_tagged_ptr, 0);
699*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_EXPECT_EQ_LLONG(mask, (1ULL << 0), "Zero tag is excluded");
700*bbb1b6f9SApple OSS Distributions 
701*bbb1b6f9SApple OSS Distributions 	/* Generate random tag */
702*bbb1b6f9SApple OSS Distributions 	uint64_t *tagged_ptr = NULL;
703*bbb1b6f9SApple OSS Distributions 	tagged_ptr = __arm_mte_create_random_tag(untagged_ptr, mask);
704*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_EXPECT_NE_PTR(orig_tagged_ptr, tagged_ptr,
705*bbb1b6f9SApple OSS Distributions 	    "Random tag was not taken from excluded tag set");
706*bbb1b6f9SApple OSS Distributions 
707*bbb1b6f9SApple OSS Distributions 	/* Time to make things real, commit the tag to memory */
708*bbb1b6f9SApple OSS Distributions 	for (uintptr_t cur_ptr = (uintptr_t)tagged_ptr;
709*bbb1b6f9SApple OSS Distributions 	    cur_ptr < (uintptr_t)tagged_ptr + sz_rounded;
710*bbb1b6f9SApple OSS Distributions 	    cur_ptr += MTE_GRANULE_SIZE) {
711*bbb1b6f9SApple OSS Distributions 		__arm_mte_set_tag((void *)cur_ptr);
712*bbb1b6f9SApple OSS Distributions 	}
713*bbb1b6f9SApple OSS Distributions 	T_LOG("Commited tagged pointer to memory: %p", tagged_ptr);
714*bbb1b6f9SApple OSS Distributions 
715*bbb1b6f9SApple OSS Distributions 	/* Write to the memory */
716*bbb1b6f9SApple OSS Distributions 	for (uint i = 0; i < sz_rounded / sizeof(uint64_t); ++i) {
717*bbb1b6f9SApple OSS Distributions 		tagged_ptr[i] = addr;
718*bbb1b6f9SApple OSS Distributions 	}
719*bbb1b6f9SApple OSS Distributions 	T_LOG("Wrote to memory");
720*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
721*bbb1b6f9SApple OSS Distributions 
722*bbb1b6f9SApple OSS Distributions 	T_LOG("Reading %llu bytes from %p", sz, tagged_ptr);
723*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_read(
724*bbb1b6f9SApple OSS Distributions 		mach_task_self(),
725*bbb1b6f9SApple OSS Distributions 		(mach_vm_address_t)tagged_ptr,
726*bbb1b6f9SApple OSS Distributions 		sz,
727*bbb1b6f9SApple OSS Distributions 		&read_addr,
728*bbb1b6f9SApple OSS Distributions 		&read_size);
729*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(kr, KERN_SUCCESS,
730*bbb1b6f9SApple OSS Distributions 	    "mach_vm_read %llu bytes from tagged ptr", sz);
731*bbb1b6f9SApple OSS Distributions 
732*bbb1b6f9SApple OSS Distributions 	/* Make sure we get the same thing back */
733*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ_UINT((unsigned int)sz, read_size,
734*bbb1b6f9SApple OSS Distributions 	    "sz:%llu == read_size:%d", sz, read_size);
735*bbb1b6f9SApple OSS Distributions 	int result = memcmp(tagged_ptr, (void *)read_addr, sz);
736*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(result, 0, "mach_vm_read back the same info");
737*bbb1b6f9SApple OSS Distributions 
738*bbb1b6f9SApple OSS Distributions 	/* Now try with incorrectly tagged pointer (aka, no tag) */
739*bbb1b6f9SApple OSS Distributions 	uint64_t *random_tagged_ptr = NULL;
740*bbb1b6f9SApple OSS Distributions 	/* Exclude the previous tag */
741*bbb1b6f9SApple OSS Distributions 	unsigned int previous_tag = extract_mte_tag(tagged_ptr);
742*bbb1b6f9SApple OSS Distributions 	mask = __arm_mte_exclude_tag(tagged_ptr, previous_tag);
743*bbb1b6f9SApple OSS Distributions 	random_tagged_ptr = __arm_mte_create_random_tag(untagged_ptr, mask);
744*bbb1b6f9SApple OSS Distributions 	T_LOG("random tagged ptr: %p", random_tagged_ptr);
745*bbb1b6f9SApple OSS Distributions 	T_EXPECT_NE_PTR(tagged_ptr, random_tagged_ptr,
746*bbb1b6f9SApple OSS Distributions 	    "Random tag was not taken from excluded tag set");
747*bbb1b6f9SApple OSS Distributions 
748*bbb1b6f9SApple OSS Distributions 	T_LOG("Reading %llu bytes from %p", sz, random_tagged_ptr);
749*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
750*bbb1b6f9SApple OSS Distributions 		T_LOG("tagged_ptr[0]: %llu", random_tagged_ptr[0]);
751*bbb1b6f9SApple OSS Distributions 	}, "Accessing memory with the wrong tag, should fail");
752*bbb1b6f9SApple OSS Distributions 
753*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
754*bbb1b6f9SApple OSS Distributions 		(void)mach_vm_read(
755*bbb1b6f9SApple OSS Distributions 			mach_task_self(),
756*bbb1b6f9SApple OSS Distributions 			(mach_vm_address_t)random_tagged_ptr,
757*bbb1b6f9SApple OSS Distributions 			KERNEL_BUFFER_COPY_THRESHOLD,
758*bbb1b6f9SApple OSS Distributions 			&read_addr,
759*bbb1b6f9SApple OSS Distributions 			&read_size);
760*bbb1b6f9SApple OSS Distributions 	}, "Untagged pointer access leads to tag check fault");
761*bbb1b6f9SApple OSS Distributions 
762*bbb1b6f9SApple OSS Distributions 	/* Reset tags to 0 before freeing */
763*bbb1b6f9SApple OSS Distributions 	for (uintptr_t cur_ptr = (uintptr_t)orig_tagged_ptr;
764*bbb1b6f9SApple OSS Distributions 	    cur_ptr < (uintptr_t)orig_tagged_ptr + sz_rounded;
765*bbb1b6f9SApple OSS Distributions 	    cur_ptr += MTE_GRANULE_SIZE) {
766*bbb1b6f9SApple OSS Distributions 		__arm_mte_set_tag((void *)cur_ptr);
767*bbb1b6f9SApple OSS Distributions 	}
768*bbb1b6f9SApple OSS Distributions 	vm_deallocate(mach_task_self(), addr, sz_rounded);
769*bbb1b6f9SApple OSS Distributions }
770*bbb1b6f9SApple OSS Distributions 
771*bbb1b6f9SApple OSS Distributions T_DECL(mte_mach_vm_read_16b,
772*bbb1b6f9SApple OSS Distributions     "mach_vm_read 16 bytes of tagged memory",
773*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
774*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
775*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(__arm64__))
776*bbb1b6f9SApple OSS Distributions {
777*bbb1b6f9SApple OSS Distributions #if __arm64__
778*bbb1b6f9SApple OSS Distributions 	mte_mach_vm_read(MTE_GRANULE_SIZE);
779*bbb1b6f9SApple OSS Distributions #endif /* __arm64__ */
780*bbb1b6f9SApple OSS Distributions }
781*bbb1b6f9SApple OSS Distributions 
782*bbb1b6f9SApple OSS Distributions T_DECL(mte_mach_vm_read_32k,
783*bbb1b6f9SApple OSS Distributions     "mach_vm_read 32k bytes of tagged memory",
784*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
785*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
786*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(__arm64__))
787*bbb1b6f9SApple OSS Distributions {
788*bbb1b6f9SApple OSS Distributions #if __arm64__
789*bbb1b6f9SApple OSS Distributions 	mte_mach_vm_read(KERNEL_BUFFER_COPY_THRESHOLD);
790*bbb1b6f9SApple OSS Distributions #endif /* __arm64__ */
791*bbb1b6f9SApple OSS Distributions }
792*bbb1b6f9SApple OSS Distributions 
793*bbb1b6f9SApple OSS Distributions T_DECL(mte_mach_vm_read_over_32k,
794*bbb1b6f9SApple OSS Distributions     "mach_vm_read 32k + 1 bytes of tagged memory",
795*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
796*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
797*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(__arm64__))
798*bbb1b6f9SApple OSS Distributions {
799*bbb1b6f9SApple OSS Distributions #if __arm64__
800*bbb1b6f9SApple OSS Distributions 	/* This will actually get rounded to 32K + 16 */
801*bbb1b6f9SApple OSS Distributions 	mte_mach_vm_read(KERNEL_BUFFER_COPY_THRESHOLD + 1);
802*bbb1b6f9SApple OSS Distributions #endif /* __arm64__ */
803*bbb1b6f9SApple OSS Distributions }
804*bbb1b6f9SApple OSS Distributions 
805*bbb1b6f9SApple OSS Distributions T_DECL(mte_vm_map_copyinout_in_kernel,
806*bbb1b6f9SApple OSS Distributions     "Test that the VM handles vm_map_copyin correctly for kernel-to-kernel tagged memory",
807*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
808*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
809*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true),
810*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(__arm64__))
811*bbb1b6f9SApple OSS Distributions {
812*bbb1b6f9SApple OSS Distributions #if __arm64__
813*bbb1b6f9SApple OSS Distributions 	T_SKIP("This test is expected to panic; comment this line to be able to run it at desk.");
814*bbb1b6f9SApple OSS Distributions 	(void) run_sysctl_test("vm_map_copyio", 0);
815*bbb1b6f9SApple OSS Distributions #endif /* __arm64__ */
816*bbb1b6f9SApple OSS Distributions }
817*bbb1b6f9SApple OSS Distributions 
818*bbb1b6f9SApple OSS Distributions #if __arm64__
819*bbb1b6f9SApple OSS Distributions static void
do_remap_test(bool own_memory)820*bbb1b6f9SApple OSS Distributions do_remap_test(bool own_memory)
821*bbb1b6f9SApple OSS Distributions {
822*bbb1b6f9SApple OSS Distributions 	mach_vm_address_t tagged_addr, untagged_addr;
823*bbb1b6f9SApple OSS Distributions 	mach_vm_size_t size = PAGE_SIZE;
824*bbb1b6f9SApple OSS Distributions 
825*bbb1b6f9SApple OSS Distributions 	T_LOG("Allocate tagged memory");
826*bbb1b6f9SApple OSS Distributions 	tagged_addr = allocate_and_tag_range(size, TAG_RANDOM);
827*bbb1b6f9SApple OSS Distributions 	char *tagged_ptr = (char*) tagged_addr;
828*bbb1b6f9SApple OSS Distributions 	untagged_addr = tagged_addr & ~MTE_TAG_MASK;
829*bbb1b6f9SApple OSS Distributions 
830*bbb1b6f9SApple OSS Distributions 	/* Write to the memory */
831*bbb1b6f9SApple OSS Distributions 	for (unsigned int i = 0; i < size; i++) {
832*bbb1b6f9SApple OSS Distributions 		tagged_ptr[i] = 'a';
833*bbb1b6f9SApple OSS Distributions 	}
834*bbb1b6f9SApple OSS Distributions 
835*bbb1b6f9SApple OSS Distributions 	T_LOG("Wrote to memory");
836*bbb1b6f9SApple OSS Distributions 
837*bbb1b6f9SApple OSS Distributions 	expect_normal_exit(^{
838*bbb1b6f9SApple OSS Distributions 		kern_return_t kr;
839*bbb1b6f9SApple OSS Distributions 		mach_port_t port;
840*bbb1b6f9SApple OSS Distributions 		if (own_memory) {
841*bbb1b6f9SApple OSS Distributions 		        port = mach_task_self();
842*bbb1b6f9SApple OSS Distributions 		} else {
843*bbb1b6f9SApple OSS Distributions 		        /* note: expect_normal_exit forks, so the parent has the allocation as well */
844*bbb1b6f9SApple OSS Distributions 		        kr = task_for_pid(mach_task_self(), getppid(), &port);
845*bbb1b6f9SApple OSS Distributions 		        T_ASSERT_MACH_SUCCESS(kr, "task_for_pid");
846*bbb1b6f9SApple OSS Distributions 		}
847*bbb1b6f9SApple OSS Distributions 
848*bbb1b6f9SApple OSS Distributions 		mach_vm_address_t remap_addr = 0;
849*bbb1b6f9SApple OSS Distributions 		vm_prot_t curprot = VM_PROT_WRITE | VM_PROT_READ;
850*bbb1b6f9SApple OSS Distributions 		vm_prot_t maxprot = VM_PROT_WRITE | VM_PROT_READ;
851*bbb1b6f9SApple OSS Distributions 		kr = mach_vm_remap_new(mach_task_self(), &remap_addr, size,
852*bbb1b6f9SApple OSS Distributions 		/* mask = */ 0, VM_FLAGS_ANYWHERE, port, untagged_addr,
853*bbb1b6f9SApple OSS Distributions 		/* copy = */ FALSE, &curprot, &maxprot, VM_INHERIT_DEFAULT);
854*bbb1b6f9SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(kr, "successfully remapped tagged memory");
855*bbb1b6f9SApple OSS Distributions 
856*bbb1b6f9SApple OSS Distributions 		T_ASSERT_EQ(remap_addr & MTE_TAG_MASK, 0ULL, "vm_remap returns an untagged pointer");
857*bbb1b6f9SApple OSS Distributions 
858*bbb1b6f9SApple OSS Distributions 		char *untagged_remap_ptr = (char*) remap_addr;
859*bbb1b6f9SApple OSS Distributions 		char *tagged_remap_ptr = __arm_mte_get_tag(untagged_remap_ptr);
860*bbb1b6f9SApple OSS Distributions 		char *incorrectly_tagged_remap_ptr = __arm_mte_increment_tag(tagged_remap_ptr, 1);
861*bbb1b6f9SApple OSS Distributions 
862*bbb1b6f9SApple OSS Distributions 		/* verify the data is correct; check every granule for speed */
863*bbb1b6f9SApple OSS Distributions 		for (unsigned int i = 0; i < size; i += MTE_GRANULE_SIZE) {
864*bbb1b6f9SApple OSS Distributions 		        T_QUIET; T_EXPECT_EQ(tagged_remap_ptr[i], 'a', "read value %u from array", i);
865*bbb1b6f9SApple OSS Distributions 		}
866*bbb1b6f9SApple OSS Distributions 		T_LOG("Verified data from child");
867*bbb1b6f9SApple OSS Distributions 
868*bbb1b6f9SApple OSS Distributions 		/* make sure the new mapping is also tagged */
869*bbb1b6f9SApple OSS Distributions 		expect_sigkill(^{
870*bbb1b6f9SApple OSS Distributions 			*untagged_remap_ptr = 'b';
871*bbb1b6f9SApple OSS Distributions 		}, "remapped MTE memory sends SIGKILL when accessed with canonical tag");
872*bbb1b6f9SApple OSS Distributions 		expect_sigkill(^{
873*bbb1b6f9SApple OSS Distributions 			*incorrectly_tagged_remap_ptr = 'b';
874*bbb1b6f9SApple OSS Distributions 		}, "remapped MTE memory sends SIGKILL when accessed with incorrect tag");
875*bbb1b6f9SApple OSS Distributions 		expect_normal_exit(^{
876*bbb1b6f9SApple OSS Distributions 			*tagged_remap_ptr = 'b';
877*bbb1b6f9SApple OSS Distributions 		}, "remapped MTE memory can be accessed with correct tag");
878*bbb1b6f9SApple OSS Distributions 
879*bbb1b6f9SApple OSS Distributions 		if (!own_memory) {
880*bbb1b6f9SApple OSS Distributions 		        kr = mach_port_deallocate(mach_task_self(), port);
881*bbb1b6f9SApple OSS Distributions 		        T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "deallocate parent port");
882*bbb1b6f9SApple OSS Distributions 		}
883*bbb1b6f9SApple OSS Distributions 		kr = mach_vm_deallocate(mach_task_self(), remap_addr, size);
884*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "deallocate remapped memory");
885*bbb1b6f9SApple OSS Distributions 		kr = mach_vm_deallocate(mach_task_self(), untagged_addr, size);
886*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "deallocate original memory from child");
887*bbb1b6f9SApple OSS Distributions 	}, "remap tagged memory");
888*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = mach_vm_deallocate(mach_task_self(), untagged_addr, size);
889*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "deallocate original memory");
890*bbb1b6f9SApple OSS Distributions }
891*bbb1b6f9SApple OSS Distributions 
892*bbb1b6f9SApple OSS Distributions T_DECL(mte_vm_map_remap_self,
893*bbb1b6f9SApple OSS Distributions     "mach_vm_remap_new() on a tagged memory of the same process",
894*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
895*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
896*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(__arm64__))
897*bbb1b6f9SApple OSS Distributions {
898*bbb1b6f9SApple OSS Distributions 	do_remap_test(true);
899*bbb1b6f9SApple OSS Distributions }
900*bbb1b6f9SApple OSS Distributions 
901*bbb1b6f9SApple OSS Distributions T_DECL(mte_vm_map_remap_other,
902*bbb1b6f9SApple OSS Distributions     "mach_vm_remap_new() on a tagged memory of a different process",
903*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
904*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
905*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(__arm64__))
906*bbb1b6f9SApple OSS Distributions {
907*bbb1b6f9SApple OSS Distributions 	do_remap_test(false);
908*bbb1b6f9SApple OSS Distributions }
909*bbb1b6f9SApple OSS Distributions 
910*bbb1b6f9SApple OSS Distributions #endif /* __arm64__ */
911*bbb1b6f9SApple OSS Distributions 
912*bbb1b6f9SApple OSS Distributions T_DECL(vm_allocate_zero_tags,
913*bbb1b6f9SApple OSS Distributions     "Ensure tags are zeroed when tagged memory is allocated from userspace",
914*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
915*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
916*bbb1b6f9SApple OSS Distributions {
917*bbb1b6f9SApple OSS Distributions #if !__arm64__
918*bbb1b6f9SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
919*bbb1b6f9SApple OSS Distributions #else /* !__arm64__ */
920*bbb1b6f9SApple OSS Distributions 	/*
921*bbb1b6f9SApple OSS Distributions 	 * Do a bunch of allocations and check that the returned tags are zeroed.
922*bbb1b6f9SApple OSS Distributions 	 * We do NUM_ALLOCATIONS_PER_ITERATION allocations, check the tags,
923*bbb1b6f9SApple OSS Distributions 	 * deallocate them, and then do it again for a total of NUM_ITERATIONS
924*bbb1b6f9SApple OSS Distributions 	 * iterations.
925*bbb1b6f9SApple OSS Distributions 	 * NUM_ALLOCATIONS_PER_ITERATION is equal to the array bound.
926*bbb1b6f9SApple OSS Distributions 	 */
927*bbb1b6f9SApple OSS Distributions 	vm_address_t addresses[1000];
928*bbb1b6f9SApple OSS Distributions 	const unsigned int NUM_ALLOCATIONS_PER_ITERATION = sizeof(addresses) / sizeof(addresses[0]);
929*bbb1b6f9SApple OSS Distributions 	const unsigned int NUM_ITERATIONS = 3;
930*bbb1b6f9SApple OSS Distributions 
931*bbb1b6f9SApple OSS Distributions 	kern_return_t kr;
932*bbb1b6f9SApple OSS Distributions 	for (size_t i = 0; i < NUM_ITERATIONS; i++) {
933*bbb1b6f9SApple OSS Distributions 		unsigned int failures = 0;
934*bbb1b6f9SApple OSS Distributions 		for (size_t j = 0; j < NUM_ALLOCATIONS_PER_ITERATION; j++) {
935*bbb1b6f9SApple OSS Distributions 			kr = vm_allocate(mach_task_self(), &addresses[j], MTE_GRANULE_SIZE, VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
936*bbb1b6f9SApple OSS Distributions 			T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "allocate tagged memory (%zu, %zu)", i, j);
937*bbb1b6f9SApple OSS Distributions 
938*bbb1b6f9SApple OSS Distributions 			/*
939*bbb1b6f9SApple OSS Distributions 			 * This is the actual test - we get the correctly tagged pointer and
940*bbb1b6f9SApple OSS Distributions 			 * verify that it is zero.
941*bbb1b6f9SApple OSS Distributions 			 */
942*bbb1b6f9SApple OSS Distributions 			char *tagged_ptr = __arm_mte_get_tag((char*) addresses[j]);
943*bbb1b6f9SApple OSS Distributions 			unsigned int orig_tag = extract_mte_tag(tagged_ptr);
944*bbb1b6f9SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(orig_tag, 0, "vm_allocate returns memory with zeroed tags (%zu, %zu)", i, j);
945*bbb1b6f9SApple OSS Distributions 			failures += (orig_tag != 0);
946*bbb1b6f9SApple OSS Distributions 
947*bbb1b6f9SApple OSS Distributions 			/* Assign an arbitrary nonzero tag and commit it to memory */
948*bbb1b6f9SApple OSS Distributions 			tagged_ptr = __arm_mte_create_random_tag(tagged_ptr, 1);
949*bbb1b6f9SApple OSS Distributions 			__arm_mte_set_tag(tagged_ptr);
950*bbb1b6f9SApple OSS Distributions 
951*bbb1b6f9SApple OSS Distributions 			/* Fail early if a zero tag was somehow assigned */
952*bbb1b6f9SApple OSS Distributions 			unsigned int new_tag = extract_mte_tag(tagged_ptr);
953*bbb1b6f9SApple OSS Distributions 			T_QUIET; T_ASSERT_NE(new_tag, 0, "random tag is nonzero (%zu, %zu)", i, j);
954*bbb1b6f9SApple OSS Distributions 		}
955*bbb1b6f9SApple OSS Distributions 
956*bbb1b6f9SApple OSS Distributions 		for (size_t j = 0; j < NUM_ALLOCATIONS_PER_ITERATION; j++) {
957*bbb1b6f9SApple OSS Distributions 			kr = vm_deallocate(mach_task_self(), addresses[j], MTE_GRANULE_SIZE);
958*bbb1b6f9SApple OSS Distributions 			T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "deallocate tagged memory (%zu, %zu)", i, j);
959*bbb1b6f9SApple OSS Distributions 		}
960*bbb1b6f9SApple OSS Distributions 		/* Aggregate results per iteration to avoid too much noise */
961*bbb1b6f9SApple OSS Distributions 		T_EXPECT_EQ(failures, 0, "Iteration %zu success", i);
962*bbb1b6f9SApple OSS Distributions 	}
963*bbb1b6f9SApple OSS Distributions #endif /* !__arm64__ */
964*bbb1b6f9SApple OSS Distributions }
965*bbb1b6f9SApple OSS Distributions 
966*bbb1b6f9SApple OSS Distributions /*
967*bbb1b6f9SApple OSS Distributions  * Policy (MTE_VMSEC_13): VM performed range-checks must be done with
968*bbb1b6f9SApple OSS Distributions  * canonicalized pointers, regardless of whether MTE is enabled
969*bbb1b6f9SApple OSS Distributions  *
970*bbb1b6f9SApple OSS Distributions  * Note that this specifically tests vm_map_copyin, vm_map_copy_overwrite,
971*bbb1b6f9SApple OSS Distributions  * since those kernel functions are intended to take tagged pointers.
972*bbb1b6f9SApple OSS Distributions  */
973*bbb1b6f9SApple OSS Distributions T_DECL(mte_copy_range_checks,
974*bbb1b6f9SApple OSS Distributions     "Test that VM range checks operate on canonicalized pointers",
975*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
976*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
977*bbb1b6f9SApple OSS Distributions {
978*bbb1b6f9SApple OSS Distributions #if !__arm64__
979*bbb1b6f9SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
980*bbb1b6f9SApple OSS Distributions #else /* !__arm64__ */
981*bbb1b6f9SApple OSS Distributions 	vm_address_t tagged_addr, incorrectly_tagged_addr;
982*bbb1b6f9SApple OSS Distributions 	/*
983*bbb1b6f9SApple OSS Distributions 	 * Test setup
984*bbb1b6f9SApple OSS Distributions 	 */
985*bbb1b6f9SApple OSS Distributions 	const mach_vm_size_t alloc_size = PAGE_SIZE;
986*bbb1b6f9SApple OSS Distributions 	tagged_addr = allocate_and_tag_range(alloc_size, 1);
987*bbb1b6f9SApple OSS Distributions 	incorrectly_tagged_addr = (tagged_addr & ~MTE_TAG_MASK) | (2LLU << MTE_TAG_SHIFT);
988*bbb1b6f9SApple OSS Distributions 
989*bbb1b6f9SApple OSS Distributions 	/*
990*bbb1b6f9SApple OSS Distributions 	 * mach_vm_copyin test:
991*bbb1b6f9SApple OSS Distributions 	 * If mach_vm_copyin canonicalizes the tagged pointer for its range checks
992*bbb1b6f9SApple OSS Distributions 	 * like it should, the range check will succeed and the actual "copy-in"
993*bbb1b6f9SApple OSS Distributions 	 * operation will be allowed to go through. This will result in a tag check
994*bbb1b6f9SApple OSS Distributions 	 * fault and the process being killed since the tag is incorrect.
995*bbb1b6f9SApple OSS Distributions 	 *
996*bbb1b6f9SApple OSS Distributions 	 * If, erroneously, the range check is done on tagged pointers, we expect
997*bbb1b6f9SApple OSS Distributions 	 * to see a failure since the "incorrect" tag is larger than the "correct"
998*bbb1b6f9SApple OSS Distributions 	 * one so it would be treated as out-of-bounds for the map.
999*bbb1b6f9SApple OSS Distributions 	 */
1000*bbb1b6f9SApple OSS Distributions 
1001*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
1002*bbb1b6f9SApple OSS Distributions 		pointer_t read_address;
1003*bbb1b6f9SApple OSS Distributions 		mach_msg_type_number_t read_size;
1004*bbb1b6f9SApple OSS Distributions 		kern_return_t kr = mach_vm_read(mach_task_self(), incorrectly_tagged_addr,
1005*bbb1b6f9SApple OSS Distributions 		alloc_size, &read_address, &read_size);
1006*bbb1b6f9SApple OSS Distributions 		T_LOG("SIGKILL not received, kr was %d", kr);
1007*bbb1b6f9SApple OSS Distributions 	}, "mach_vm_read with incorrectly tagged pointer should cause a tag check fault");
1008*bbb1b6f9SApple OSS Distributions 
1009*bbb1b6f9SApple OSS Distributions 	/*
1010*bbb1b6f9SApple OSS Distributions 	 * mach_vm_copy_overwrite test:
1011*bbb1b6f9SApple OSS Distributions 	 * Essentially the same logic using mach_vm_write instead of mach_vm_read.
1012*bbb1b6f9SApple OSS Distributions 	 * To be able to do a vm_map_write, we need to first set up a vm_map_copy_t,
1013*bbb1b6f9SApple OSS Distributions 	 * which we can get from a correctly-executed vm_map_read.
1014*bbb1b6f9SApple OSS Distributions 	 */
1015*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
1016*bbb1b6f9SApple OSS Distributions 	pointer_t copy_address;
1017*bbb1b6f9SApple OSS Distributions 	mach_msg_type_number_t copy_size;
1018*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = mach_vm_read(mach_task_self(), tagged_addr,
1019*bbb1b6f9SApple OSS Distributions 	    alloc_size, &copy_address, &copy_size);
1020*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "set up vm_map_copy_t for mach_vm_write test");
1021*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
1022*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
1023*bbb1b6f9SApple OSS Distributions 		kern_return_t kr2 = mach_vm_write(mach_task_self(), incorrectly_tagged_addr,
1024*bbb1b6f9SApple OSS Distributions 		copy_address, copy_size);
1025*bbb1b6f9SApple OSS Distributions 		T_LOG("SIGKILL not received, kr was %d", kr2);
1026*bbb1b6f9SApple OSS Distributions 	}, "mach_vm_write with incorrectly tagged pointer should cause a tag check fault");
1027*bbb1b6f9SApple OSS Distributions #endif /* !__arm64__ */
1028*bbb1b6f9SApple OSS Distributions }
1029*bbb1b6f9SApple OSS Distributions 
1030*bbb1b6f9SApple OSS Distributions /*
1031*bbb1b6f9SApple OSS Distributions  * Policy (MTE_VMSEC_14): VM performed range math must be done using canonical
1032*bbb1b6f9SApple OSS Distributions  * pointers, regardless of whether MTE is enabled.
1033*bbb1b6f9SApple OSS Distributions  *
1034*bbb1b6f9SApple OSS Distributions  * Note that this specifically tests vm_map_copyin, vm_map_copy_overwrite,
1035*bbb1b6f9SApple OSS Distributions  * since those kernel functions are intended to take tagged pointers.
1036*bbb1b6f9SApple OSS Distributions  */
1037*bbb1b6f9SApple OSS Distributions T_DECL(mte_copy_range_math,
1038*bbb1b6f9SApple OSS Distributions     "Test that pointer values are not canonicalized after range math",
1039*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1040*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
1041*bbb1b6f9SApple OSS Distributions {
1042*bbb1b6f9SApple OSS Distributions #if !__arm64__
1043*bbb1b6f9SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
1044*bbb1b6f9SApple OSS Distributions #else /* !__arm64__ */
1045*bbb1b6f9SApple OSS Distributions 	vm_address_t tagged_addr;
1046*bbb1b6f9SApple OSS Distributions 	kern_return_t kr;
1047*bbb1b6f9SApple OSS Distributions 
1048*bbb1b6f9SApple OSS Distributions 	/*
1049*bbb1b6f9SApple OSS Distributions 	 * Test setup
1050*bbb1b6f9SApple OSS Distributions 	 */
1051*bbb1b6f9SApple OSS Distributions 	const mach_vm_size_t alloc_size = MTE_GRANULE_SIZE;
1052*bbb1b6f9SApple OSS Distributions 	tagged_addr = allocate_and_tag_range(alloc_size, TAG_RANDOM);
1053*bbb1b6f9SApple OSS Distributions 
1054*bbb1b6f9SApple OSS Distributions 	vm_offset_t read_address;
1055*bbb1b6f9SApple OSS Distributions 	mach_msg_type_number_t read_size;
1056*bbb1b6f9SApple OSS Distributions 	mach_vm_size_t malformed_size;
1057*bbb1b6f9SApple OSS Distributions 
1058*bbb1b6f9SApple OSS Distributions 	/*
1059*bbb1b6f9SApple OSS Distributions 	 * A size which extends into the MTE tag bits is too large to fit in
1060*bbb1b6f9SApple OSS Distributions 	 * memory and should be rejected. If range math is operating on tagged
1061*bbb1b6f9SApple OSS Distributions 	 * pointers (and the tag bits get stripped later), then this would
1062*bbb1b6f9SApple OSS Distributions 	 * be accepted.
1063*bbb1b6f9SApple OSS Distributions 	 */
1064*bbb1b6f9SApple OSS Distributions 	// Test vm_map_copyin using mach_vm_read
1065*bbb1b6f9SApple OSS Distributions 	malformed_size = (mach_vm_size_t) alloc_size | (7LLU << MTE_TAG_SHIFT);
1066*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_read(mach_task_self(), tagged_addr, malformed_size,
1067*bbb1b6f9SApple OSS Distributions 	    &read_address, &read_size);
1068*bbb1b6f9SApple OSS Distributions 	T_EXPECT_MACH_ERROR_(kr, KERN_INVALID_ARGUMENT, "mach_vm_read should reject size which extends into tag bits");
1069*bbb1b6f9SApple OSS Distributions 
1070*bbb1b6f9SApple OSS Distributions 	/*
1071*bbb1b6f9SApple OSS Distributions 	 * Cannot test vm_map_copy_overwrite from userspace. The only entry point
1072*bbb1b6f9SApple OSS Distributions 	 * that hits this function without first hitting mach_vm_read is
1073*bbb1b6f9SApple OSS Distributions 	 * mach_vm_write, which takes its size as a 32-bit mach_msg_type_number_t.
1074*bbb1b6f9SApple OSS Distributions 	 */
1075*bbb1b6f9SApple OSS Distributions #endif /* !__arm64__ */
1076*bbb1b6f9SApple OSS Distributions }
1077*bbb1b6f9SApple OSS Distributions 
1078*bbb1b6f9SApple OSS Distributions /*
1079*bbb1b6f9SApple OSS Distributions  * Policy (MTE_VMSEC_16): if the parameter/target of a VM API is a range of
1080*bbb1b6f9SApple OSS Distributions  * memory, VM APIs must ensure that the address is not tagged
1081*bbb1b6f9SApple OSS Distributions  *
1082*bbb1b6f9SApple OSS Distributions  * Corollary: to ease adoption in cases in which pointers obtained from
1083*bbb1b6f9SApple OSS Distributions  * the memory allocator are directly passed to some of these functions,
1084*bbb1b6f9SApple OSS Distributions  * we implement stripping at the kernel API entrypoint for APIs that do
1085*bbb1b6f9SApple OSS Distributions  * not affect the VM state or that are safe and common enough to strip.
1086*bbb1b6f9SApple OSS Distributions  * This helps also clearing/making deterministic
1087*bbb1b6f9SApple OSS Distributions  * cases where addresses were passed along the VM subsystem just waiting
1088*bbb1b6f9SApple OSS Distributions  * to eventually be rejected.
1089*bbb1b6f9SApple OSS Distributions  *
1090*bbb1b6f9SApple OSS Distributions  * note: this does not apply to APIs which lead to vm_map_copy{in,out}, since
1091*bbb1b6f9SApple OSS Distributions  * these need tags to be able to read tagged memory.
1092*bbb1b6f9SApple OSS Distributions  */
1093*bbb1b6f9SApple OSS Distributions T_DECL(mte_vm_reject_tagged_pointers,
1094*bbb1b6f9SApple OSS Distributions     "Test that most VM APIs reject tagged pointers",
1095*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1096*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
1097*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true) /* to be able to get host_priv port for mach_vm_wire */)
1098*bbb1b6f9SApple OSS Distributions {
1099*bbb1b6f9SApple OSS Distributions #if !__arm64__
1100*bbb1b6f9SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
1101*bbb1b6f9SApple OSS Distributions #else /* !__arm64__ */
1102*bbb1b6f9SApple OSS Distributions 	vm_address_t untagged_addr, tagged_addr, tagged_addr_mprotect;
1103*bbb1b6f9SApple OSS Distributions 	void *untagged_ptr, *tagged_ptr, *tagged_ptr_mprotect;
1104*bbb1b6f9SApple OSS Distributions 	kern_return_t kr;
1105*bbb1b6f9SApple OSS Distributions 	int ret;
1106*bbb1b6f9SApple OSS Distributions 
1107*bbb1b6f9SApple OSS Distributions 	/*
1108*bbb1b6f9SApple OSS Distributions 	 * Test setup
1109*bbb1b6f9SApple OSS Distributions 	 */
1110*bbb1b6f9SApple OSS Distributions 	const size_t alloc_size = PAGE_SIZE;
1111*bbb1b6f9SApple OSS Distributions 	tagged_addr = allocate_and_tag_range(alloc_size, TAG_RANDOM);
1112*bbb1b6f9SApple OSS Distributions 	tagged_addr_mprotect = allocate_and_tag_range(alloc_size, TAG_RANDOM);
1113*bbb1b6f9SApple OSS Distributions 	untagged_addr = tagged_addr & ~MTE_TAG_MASK;
1114*bbb1b6f9SApple OSS Distributions 	untagged_ptr = (void*) untagged_addr;
1115*bbb1b6f9SApple OSS Distributions 	tagged_ptr = (void*) tagged_addr;
1116*bbb1b6f9SApple OSS Distributions 	tagged_ptr_mprotect = (void *)tagged_addr_mprotect;
1117*bbb1b6f9SApple OSS Distributions 
1118*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_NE(tagged_addr & MTE_TAG_MASK, 0ULL, "validate tagged_addr");
1119*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(untagged_addr & MTE_TAG_MASK, 0ULL, "validate untagged_addr");
1120*bbb1b6f9SApple OSS Distributions 
1121*bbb1b6f9SApple OSS Distributions 	__block struct vm_region_submap_info_64 region_info;
1122*bbb1b6f9SApple OSS Distributions 	void (^get_region_info)(void) = ^{
1123*bbb1b6f9SApple OSS Distributions 		vm_address_t address = untagged_addr;
1124*bbb1b6f9SApple OSS Distributions 		unsigned int depth = 1;
1125*bbb1b6f9SApple OSS Distributions 		vm_size_t size;
1126*bbb1b6f9SApple OSS Distributions 		mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;
1127*bbb1b6f9SApple OSS Distributions 		kern_return_t region_kr = vm_region_recurse_64(mach_task_self(), &address, &size,
1128*bbb1b6f9SApple OSS Distributions 		    &depth, (vm_region_info_t) &region_info, &count);
1129*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(region_kr, "get allocation region info");
1130*bbb1b6f9SApple OSS Distributions 	};
1131*bbb1b6f9SApple OSS Distributions 
1132*bbb1b6f9SApple OSS Distributions 	/*
1133*bbb1b6f9SApple OSS Distributions 	 * Test various APIs with tagged pointers
1134*bbb1b6f9SApple OSS Distributions 	 */
1135*bbb1b6f9SApple OSS Distributions 	/* mprotect, mach_vm_protect are common enough, we strip implicitly. */
1136*bbb1b6f9SApple OSS Distributions 	ret = mprotect(tagged_ptr_mprotect, alloc_size, PROT_NONE);
1137*bbb1b6f9SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(ret, "mprotect");
1138*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_protect(mach_task_self(), tagged_addr_mprotect, alloc_size, false, PROT_NONE);
1139*bbb1b6f9SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "mach_vm_protect");
1140*bbb1b6f9SApple OSS Distributions 
1141*bbb1b6f9SApple OSS Distributions 	/*
1142*bbb1b6f9SApple OSS Distributions 	 * mincore: SUCCESS
1143*bbb1b6f9SApple OSS Distributions 	 */
1144*bbb1b6f9SApple OSS Distributions 	char vec[100] = {0};
1145*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_LE(alloc_size, sizeof(vec) * PAGE_SIZE, "vec is large enough to fit mincore result");
1146*bbb1b6f9SApple OSS Distributions 	ret = mincore(tagged_ptr, alloc_size, vec);
1147*bbb1b6f9SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(ret, "mincore: return value");
1148*bbb1b6f9SApple OSS Distributions 
1149*bbb1b6f9SApple OSS Distributions 	/* msync, mach_vm_msync */
1150*bbb1b6f9SApple OSS Distributions 	ret = msync(tagged_ptr, alloc_size, MS_SYNC);
1151*bbb1b6f9SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(ret, "msync");
1152*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_msync(mach_task_self(), tagged_addr, alloc_size, VM_SYNC_SYNCHRONOUS | VM_SYNC_CONTIGUOUS);
1153*bbb1b6f9SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "mach_vm_msync");
1154*bbb1b6f9SApple OSS Distributions 
1155*bbb1b6f9SApple OSS Distributions 	/* madvise, mach_vm_behavior_set strip tagged addresses */
1156*bbb1b6f9SApple OSS Distributions 	ret = madvise(tagged_ptr, alloc_size, MADV_NORMAL);
1157*bbb1b6f9SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(ret, "madvise");
1158*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_behavior_set(mach_task_self(), tagged_addr, alloc_size,
1159*bbb1b6f9SApple OSS Distributions 	    VM_BEHAVIOR_DEFAULT);
1160*bbb1b6f9SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "mach_vm_behavior_set");
1161*bbb1b6f9SApple OSS Distributions 
1162*bbb1b6f9SApple OSS Distributions 	/*
1163*bbb1b6f9SApple OSS Distributions 	 * minherit, mach_vm_inherit:
1164*bbb1b6f9SApple OSS Distributions 	 * mach_vm_inherit would just silently succeed and do nothing if the range was tagged, so
1165*bbb1b6f9SApple OSS Distributions 	 * we strip addresses to have consistent behavior.
1166*bbb1b6f9SApple OSS Distributions 	 */
1167*bbb1b6f9SApple OSS Distributions 	const vm_inherit_t NEW_INHERIT = VM_INHERIT_NONE;
1168*bbb1b6f9SApple OSS Distributions 	ret = minherit(tagged_ptr, alloc_size, NEW_INHERIT);
1169*bbb1b6f9SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(ret, "minherit");
1170*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_inherit(mach_task_self(), tagged_addr, alloc_size, NEW_INHERIT);
1171*bbb1b6f9SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "mach_vm_inherit");
1172*bbb1b6f9SApple OSS Distributions 
1173*bbb1b6f9SApple OSS Distributions 	/*
1174*bbb1b6f9SApple OSS Distributions 	 * mlock, mach_vm_wire(prot != VM_PROT_NONE):
1175*bbb1b6f9SApple OSS Distributions 	 * Allow implicitly stripping to avoid no-op success that might confuse third parties.
1176*bbb1b6f9SApple OSS Distributions 	 */
1177*bbb1b6f9SApple OSS Distributions 	mach_port_t host_priv = HOST_PRIV_NULL;
1178*bbb1b6f9SApple OSS Distributions 	kr = host_get_host_priv_port(mach_host_self(), &host_priv); \
1179*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "get host_priv port");
1180*bbb1b6f9SApple OSS Distributions 
1181*bbb1b6f9SApple OSS Distributions 	ret = mlock(tagged_ptr, alloc_size);
1182*bbb1b6f9SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(ret, "mlock");
1183*bbb1b6f9SApple OSS Distributions 	get_region_info();
1184*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ(region_info.user_wired_count, (unsigned short) 1, "mlock on tagged pointer should wire memory");
1185*bbb1b6f9SApple OSS Distributions 	ret = munlock(tagged_ptr, alloc_size);
1186*bbb1b6f9SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(ret, "munlock");
1187*bbb1b6f9SApple OSS Distributions 	get_region_info();
1188*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ(region_info.user_wired_count, (unsigned short) 0, "munlock on tagged pointer should unwire memory");
1189*bbb1b6f9SApple OSS Distributions 
1190*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_wire(host_priv, mach_task_self(), tagged_addr,
1191*bbb1b6f9SApple OSS Distributions 	    alloc_size, VM_PROT_DEFAULT);
1192*bbb1b6f9SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "mach_vm_wire (wire)");
1193*bbb1b6f9SApple OSS Distributions 	get_region_info();
1194*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ(region_info.user_wired_count, (unsigned short) 1, "mach_vm_wire on tagged address should wire memory");
1195*bbb1b6f9SApple OSS Distributions 	ret = munlock(tagged_ptr, alloc_size);
1196*bbb1b6f9SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(ret, "munlock");
1197*bbb1b6f9SApple OSS Distributions 
1198*bbb1b6f9SApple OSS Distributions 	/* List of flags used to test vm_allocate, vm_map and vm_remap */
1199*bbb1b6f9SApple OSS Distributions 	const int ALLOCATE_FLAGS[] = {
1200*bbb1b6f9SApple OSS Distributions 		VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE,
1201*bbb1b6f9SApple OSS Distributions 		VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE | VM_FLAGS_MTE,
1202*bbb1b6f9SApple OSS Distributions 		VM_FLAGS_ANYWHERE,
1203*bbb1b6f9SApple OSS Distributions 		VM_FLAGS_ANYWHERE | VM_FLAGS_MTE
1204*bbb1b6f9SApple OSS Distributions 	};
1205*bbb1b6f9SApple OSS Distributions 	const size_t NUM_ALLOCATE_FLAGS = sizeof(ALLOCATE_FLAGS) / sizeof(*ALLOCATE_FLAGS);
1206*bbb1b6f9SApple OSS Distributions 
1207*bbb1b6f9SApple OSS Distributions 	/* vm_allocate tests: */
1208*bbb1b6f9SApple OSS Distributions 	for (size_t i = 0; i < NUM_ALLOCATE_FLAGS; i++) {
1209*bbb1b6f9SApple OSS Distributions 		mach_vm_address_t new_addr = tagged_addr;
1210*bbb1b6f9SApple OSS Distributions 		kr = mach_vm_allocate(mach_task_self(), &new_addr, alloc_size, ALLOCATE_FLAGS[i]);
1211*bbb1b6f9SApple OSS Distributions 		if (ALLOCATE_FLAGS[i] & VM_FLAGS_ANYWHERE) {
1212*bbb1b6f9SApple OSS Distributions 			T_EXPECT_MACH_SUCCESS(kr, "mach_vm_allocate %zu (%#x)", i, ALLOCATE_FLAGS[i]);
1213*bbb1b6f9SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(new_addr & MTE_TAG_MASK, 0ull, "mach_vm_allocate should return untagged pointer");
1214*bbb1b6f9SApple OSS Distributions 			T_QUIET; T_EXPECT_NE((vm_address_t) new_addr, untagged_addr, "allocate anywhere should return a new range");
1215*bbb1b6f9SApple OSS Distributions 
1216*bbb1b6f9SApple OSS Distributions 			/* clean up new allocation */
1217*bbb1b6f9SApple OSS Distributions 			if (kr == KERN_SUCCESS) {
1218*bbb1b6f9SApple OSS Distributions 				kr = mach_vm_deallocate(mach_task_self(), new_addr, alloc_size);
1219*bbb1b6f9SApple OSS Distributions 				T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "cleanup mach_vm_map");
1220*bbb1b6f9SApple OSS Distributions 			}
1221*bbb1b6f9SApple OSS Distributions 		} else {
1222*bbb1b6f9SApple OSS Distributions 			T_EXPECT_MACH_ERROR(kr, KERN_INVALID_ADDRESS, "mach_vm_allocate %zu (%#x)", i, ALLOCATE_FLAGS[i]);
1223*bbb1b6f9SApple OSS Distributions 		}
1224*bbb1b6f9SApple OSS Distributions 	}
1225*bbb1b6f9SApple OSS Distributions 
1226*bbb1b6f9SApple OSS Distributions 	/* mach_vm_machine_attribute: allow tagged addresses */
1227*bbb1b6f9SApple OSS Distributions 	vm_machine_attribute_val_t machine_attribute_val = MATTR_VAL_CACHE_FLUSH;
1228*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_machine_attribute(mach_task_self(), tagged_addr, alloc_size,
1229*bbb1b6f9SApple OSS Distributions 	    MATTR_CACHE, &machine_attribute_val);
1230*bbb1b6f9SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "mach_vm_machine_attribute");
1231*bbb1b6f9SApple OSS Distributions 
1232*bbb1b6f9SApple OSS Distributions 	/* mach_make_memory_entry_64: DO NOT allow tagged addresses */
1233*bbb1b6f9SApple OSS Distributions 	mach_port_t object_handle;
1234*bbb1b6f9SApple OSS Distributions 	memory_object_size_t object_size = alloc_size;
1235*bbb1b6f9SApple OSS Distributions 	kr = mach_make_memory_entry_64(mach_task_self(), &object_size, tagged_addr,
1236*bbb1b6f9SApple OSS Distributions 	    VM_PROT_DEFAULT, &object_handle, MACH_PORT_NULL);
1237*bbb1b6f9SApple OSS Distributions 	T_EXPECT_MACH_ERROR(kr, KERN_INVALID_ADDRESS, "mach_make_memory_entry_64");
1238*bbb1b6f9SApple OSS Distributions 
1239*bbb1b6f9SApple OSS Distributions 	/* mach_vm_map: DO NOT allow tagged addresses */
1240*bbb1b6f9SApple OSS Distributions 	/* setup: get a memory entry to map in */
1241*bbb1b6f9SApple OSS Distributions 	kr = mach_make_memory_entry_64(mach_task_self(), &object_size, untagged_addr,
1242*bbb1b6f9SApple OSS Distributions 	    VM_PROT_DEFAULT | MAP_MEM_NAMED_CREATE, &object_handle, MACH_PORT_NULL);
1243*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "create memory entry for mach_vm_map");
1244*bbb1b6f9SApple OSS Distributions 
1245*bbb1b6f9SApple OSS Distributions 	for (size_t i = 0; i < NUM_ALLOCATE_FLAGS; i++) {
1246*bbb1b6f9SApple OSS Distributions 		mach_vm_address_t new_addr = tagged_addr;
1247*bbb1b6f9SApple OSS Distributions 		kr = mach_vm_map(mach_task_self(), &new_addr, alloc_size, /* mask = */ 0,
1248*bbb1b6f9SApple OSS Distributions 		    ALLOCATE_FLAGS[i], object_handle, /* offset = */ 0, /* copy = */ true,
1249*bbb1b6f9SApple OSS Distributions 		    VM_PROT_DEFAULT, VM_PROT_DEFAULT, VM_INHERIT_DEFAULT);
1250*bbb1b6f9SApple OSS Distributions 		if (ALLOCATE_FLAGS[i] & VM_FLAGS_ANYWHERE) {
1251*bbb1b6f9SApple OSS Distributions 			/*
1252*bbb1b6f9SApple OSS Distributions 			 * VM_FLAGS_ANYWHERE uses the provided address as a location to start
1253*bbb1b6f9SApple OSS Distributions 			 * searching from. Since a tagged address is outside the map bounds,
1254*bbb1b6f9SApple OSS Distributions 			 * it won't be able to find any space for the allocation.
1255*bbb1b6f9SApple OSS Distributions 			 */
1256*bbb1b6f9SApple OSS Distributions 			T_EXPECT_MACH_ERROR(kr, KERN_NO_SPACE, "mach_vm_map %zu (%#x)", i, ALLOCATE_FLAGS[i]);
1257*bbb1b6f9SApple OSS Distributions 		} else {
1258*bbb1b6f9SApple OSS Distributions 			T_EXPECT_MACH_ERROR(kr, KERN_INVALID_ADDRESS, "mach_vm_map %zu (%#x)", i, ALLOCATE_FLAGS[i]);
1259*bbb1b6f9SApple OSS Distributions 		}
1260*bbb1b6f9SApple OSS Distributions 	}
1261*bbb1b6f9SApple OSS Distributions 
1262*bbb1b6f9SApple OSS Distributions 	/* clean up memory entry object handle */
1263*bbb1b6f9SApple OSS Distributions 	kr = mach_port_deallocate(mach_task_self(), object_handle);
1264*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_vm_map tests: clean up memory entry object handle");
1265*bbb1b6f9SApple OSS Distributions 
1266*bbb1b6f9SApple OSS Distributions 	/* mach_vm_purgable_control */
1267*bbb1b6f9SApple OSS Distributions 	int purgable_state;
1268*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_purgable_control(mach_task_self(), tagged_addr, VM_PURGABLE_GET_STATE, &purgable_state);
1269*bbb1b6f9SApple OSS Distributions 	T_EXPECT_MACH_ERROR(kr, KERN_INVALID_ADDRESS, "mach_vm_purgable_control");
1270*bbb1b6f9SApple OSS Distributions 
1271*bbb1b6f9SApple OSS Distributions 	/* mach_vm_region: reject tagged addresses */
1272*bbb1b6f9SApple OSS Distributions 	mach_vm_address_t region_addr = tagged_addr;
1273*bbb1b6f9SApple OSS Distributions 	mach_vm_size_t region_size;
1274*bbb1b6f9SApple OSS Distributions 	vm_region_basic_info_data_64_t region_info_64;
1275*bbb1b6f9SApple OSS Distributions 	mach_msg_type_number_t region_info_cnt = VM_REGION_BASIC_INFO_COUNT_64;
1276*bbb1b6f9SApple OSS Distributions 	mach_port_t unused;
1277*bbb1b6f9SApple OSS Distributions 
1278*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_region(mach_task_self(), &region_addr, &region_size,
1279*bbb1b6f9SApple OSS Distributions 	    VM_REGION_BASIC_INFO_64, (vm_region_info_t) &region_info_64,
1280*bbb1b6f9SApple OSS Distributions 	    &region_info_cnt, &unused);
1281*bbb1b6f9SApple OSS Distributions 	T_EXPECT_MACH_ERROR(kr, KERN_INVALID_ADDRESS, "mach_vm_region");
1282*bbb1b6f9SApple OSS Distributions 
1283*bbb1b6f9SApple OSS Distributions 	/* mach_vm_remap_new */
1284*bbb1b6f9SApple OSS Distributions 	mach_vm_address_t untagged_addr2, tagged_addr2;
1285*bbb1b6f9SApple OSS Distributions 	tagged_addr2 = allocate_and_tag_range(alloc_size, TAG_RANDOM);
1286*bbb1b6f9SApple OSS Distributions 	untagged_addr2 = tagged_addr2 & ~MTE_TAG_MASK;
1287*bbb1b6f9SApple OSS Distributions 
1288*bbb1b6f9SApple OSS Distributions 	/* Test each flag value twice, once with source tagged and once with destination tagged */
1289*bbb1b6f9SApple OSS Distributions 	for (size_t i = 0; i < 2 * NUM_ALLOCATE_FLAGS; i++) {
1290*bbb1b6f9SApple OSS Distributions 		int flags = ALLOCATE_FLAGS[i % NUM_ALLOCATE_FLAGS];
1291*bbb1b6f9SApple OSS Distributions 		bool source_tagged = i < NUM_ALLOCATE_FLAGS;
1292*bbb1b6f9SApple OSS Distributions 		char *msg = source_tagged ? "source tagged" : "dest tagged";
1293*bbb1b6f9SApple OSS Distributions 		mach_vm_address_t src_addr = source_tagged ? tagged_addr : untagged_addr;
1294*bbb1b6f9SApple OSS Distributions 		mach_vm_address_t dest_addr = source_tagged ? untagged_addr2 : tagged_addr2;
1295*bbb1b6f9SApple OSS Distributions 
1296*bbb1b6f9SApple OSS Distributions 		vm_prot_t cur_prot = VM_PROT_DEFAULT, max_prot = VM_PROT_DEFAULT;
1297*bbb1b6f9SApple OSS Distributions 		kr = mach_vm_remap_new(mach_task_self(), &dest_addr, alloc_size, /* mask = */ 0,
1298*bbb1b6f9SApple OSS Distributions 		    flags, mach_task_self(), src_addr, true, &cur_prot, &max_prot,
1299*bbb1b6f9SApple OSS Distributions 		    VM_INHERIT_DEFAULT);
1300*bbb1b6f9SApple OSS Distributions 
1301*bbb1b6f9SApple OSS Distributions 		if (flags & VM_FLAGS_MTE) {
1302*bbb1b6f9SApple OSS Distributions 			/* VM_FLAGS_USER_REMAP does not include VM_FLAGS_MTE */
1303*bbb1b6f9SApple OSS Distributions 			T_EXPECT_MACH_ERROR(kr, KERN_INVALID_ARGUMENT, "mach_vm_remap_new %zu (%s, %#x)", i, msg, flags);
1304*bbb1b6f9SApple OSS Distributions 		} else if (!source_tagged && flags & VM_FLAGS_ANYWHERE) {
1305*bbb1b6f9SApple OSS Distributions 			/*
1306*bbb1b6f9SApple OSS Distributions 			 * In this case, we pass vm_map_remap_extract since the source
1307*bbb1b6f9SApple OSS Distributions 			 * address is untagged. When we try to find a space to insert it
1308*bbb1b6f9SApple OSS Distributions 			 * into the map, we fail since VM_FLAGS_ANYWHERE uses the destination
1309*bbb1b6f9SApple OSS Distributions 			 * passed in as a location to start searching from.
1310*bbb1b6f9SApple OSS Distributions 			 */
1311*bbb1b6f9SApple OSS Distributions 			T_EXPECT_MACH_ERROR(kr, KERN_NO_SPACE, "mach_vm_remap_new %zu (%s, %#x)", i, msg, flags);
1312*bbb1b6f9SApple OSS Distributions 		} else {
1313*bbb1b6f9SApple OSS Distributions 			T_EXPECT_MACH_ERROR(kr, KERN_INVALID_ADDRESS, "mach_vm_remap_new %zu (%s, %#x)", i, msg, flags);
1314*bbb1b6f9SApple OSS Distributions 		}
1315*bbb1b6f9SApple OSS Distributions 
1316*bbb1b6f9SApple OSS Distributions 		if (kr == KERN_SUCCESS && (flags & VM_FLAGS_ANYWHERE)) {
1317*bbb1b6f9SApple OSS Distributions 			/* clean up the new allocation if we mistakenly suceeded */
1318*bbb1b6f9SApple OSS Distributions 			kr = mach_vm_deallocate(mach_task_self(), dest_addr, alloc_size);
1319*bbb1b6f9SApple OSS Distributions 			T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "cleanup mach_vm_remap_new %zu (%s, %#x)", i, msg, flags);
1320*bbb1b6f9SApple OSS Distributions 		}
1321*bbb1b6f9SApple OSS Distributions 	}
1322*bbb1b6f9SApple OSS Distributions 
1323*bbb1b6f9SApple OSS Distributions 	/* clean up our second allocation */
1324*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
1325*bbb1b6f9SApple OSS Distributions 	kr = vm_deallocate(mach_task_self(), untagged_addr2, alloc_size);
1326*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "clean up allocation for mach_vm_remap_new tests");
1327*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
1328*bbb1b6f9SApple OSS Distributions 
1329*bbb1b6f9SApple OSS Distributions 	/* vm_deallocate: vm_allocate() will return a canonical address, so we mandate a canonical address here */
1330*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
1331*bbb1b6f9SApple OSS Distributions 	kr = vm_deallocate(mach_task_self(), tagged_addr, alloc_size);
1332*bbb1b6f9SApple OSS Distributions 	T_EXPECT_MACH_ERROR(kr, KERN_INVALID_ARGUMENT, "vm_deallocate denies a non-canonical addresses");
1333*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
1334*bbb1b6f9SApple OSS Distributions 
1335*bbb1b6f9SApple OSS Distributions 	/* test cleanup */
1336*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
1337*bbb1b6f9SApple OSS Distributions 	kr = vm_deallocate(mach_task_self(), untagged_addr, alloc_size);
1338*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "test region cleanup");
1339*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
1340*bbb1b6f9SApple OSS Distributions #endif /* !__arm64__ */
1341*bbb1b6f9SApple OSS Distributions }
1342*bbb1b6f9SApple OSS Distributions 
1343*bbb1b6f9SApple OSS Distributions T_DECL(mte_tagged_page_relocation,
1344*bbb1b6f9SApple OSS Distributions     "Test that VM copies tags on page relocation for tagged memory",
1345*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
1346*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true),
1347*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
1348*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(__arm64__))
1349*bbb1b6f9SApple OSS Distributions {
1350*bbb1b6f9SApple OSS Distributions #if __arm64__
1351*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
1352*bbb1b6f9SApple OSS Distributions 	mach_vm_address_t addr = 0;
1353*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = mach_vm_allocate(
1354*bbb1b6f9SApple OSS Distributions 		mach_task_self(),
1355*bbb1b6f9SApple OSS Distributions 		&addr,
1356*bbb1b6f9SApple OSS Distributions 		PAGE_SIZE,
1357*bbb1b6f9SApple OSS Distributions 		VM_FLAGS_ANYWHERE | VM_FLAGS_MTE
1358*bbb1b6f9SApple OSS Distributions 		);
1359*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr,
1360*bbb1b6f9SApple OSS Distributions 	    "allocate 32 bytes of tagged memory at 0x%llx", addr);
1361*bbb1b6f9SApple OSS Distributions 
1362*bbb1b6f9SApple OSS Distributions 	/* Verify originally assigned tags are zero */
1363*bbb1b6f9SApple OSS Distributions 	for (uint i = 0; i < PAGE_SIZE / MTE_GRANULE_SIZE; ++i) {
1364*bbb1b6f9SApple OSS Distributions 		char *untagged_ptr = (char *)((uintptr_t)addr + i * MTE_GRANULE_SIZE);
1365*bbb1b6f9SApple OSS Distributions 		char *orig_tagged_ptr = __arm_mte_get_tag(untagged_ptr);
1366*bbb1b6f9SApple OSS Distributions 		unsigned int orig_tag = extract_mte_tag(orig_tagged_ptr);
1367*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ_UINT(orig_tag, 0U, "originally assigned tag is zero");
1368*bbb1b6f9SApple OSS Distributions 	}
1369*bbb1b6f9SApple OSS Distributions 
1370*bbb1b6f9SApple OSS Distributions 	/*
1371*bbb1b6f9SApple OSS Distributions 	 * Tag the first 16 bytes with non-zero tag, and
1372*bbb1b6f9SApple OSS Distributions 	 * leave the second 16 bytes as is
1373*bbb1b6f9SApple OSS Distributions 	 */
1374*bbb1b6f9SApple OSS Distributions 	char *untagged_ptr = (char *)addr;
1375*bbb1b6f9SApple OSS Distributions 	char *orig_tagged_ptr = __arm_mte_get_tag(untagged_ptr);
1376*bbb1b6f9SApple OSS Distributions 	uint64_t mask = __arm_mte_exclude_tag(orig_tagged_ptr, 0);
1377*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ_LLONG(mask, (1LL << 0), "zero tag is excluded");
1378*bbb1b6f9SApple OSS Distributions 
1379*bbb1b6f9SApple OSS Distributions 	char *random_tagged_ptr = __arm_mte_create_random_tag(untagged_ptr, mask);
1380*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_EXPECT_NE_PTR(orig_tagged_ptr, random_tagged_ptr,
1381*bbb1b6f9SApple OSS Distributions 	    "random tag was not taken from excluded tag set");
1382*bbb1b6f9SApple OSS Distributions 
1383*bbb1b6f9SApple OSS Distributions 	ptrdiff_t diff = __arm_mte_ptrdiff(untagged_ptr, random_tagged_ptr);
1384*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_EXPECT_EQ_ULONG(diff, (ptrdiff_t)0, "untagged %p and tagged %p have identical address bits",
1385*bbb1b6f9SApple OSS Distributions 	    untagged_ptr, random_tagged_ptr);
1386*bbb1b6f9SApple OSS Distributions 
1387*bbb1b6f9SApple OSS Distributions 	/* Time to make things real, commit the tag to memory */
1388*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag(random_tagged_ptr);
1389*bbb1b6f9SApple OSS Distributions 
1390*bbb1b6f9SApple OSS Distributions 	/* Ensure that we can read back the tag */
1391*bbb1b6f9SApple OSS Distributions 	char *read_back = __arm_mte_get_tag(untagged_ptr);
1392*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ_PTR(read_back, random_tagged_ptr, "tag was committed to memory correctly");
1393*bbb1b6f9SApple OSS Distributions 
1394*bbb1b6f9SApple OSS Distributions 	T_LOG("tagged pointer: %p", random_tagged_ptr);
1395*bbb1b6f9SApple OSS Distributions 	random_tagged_ptr[0] = 'a';
1396*bbb1b6f9SApple OSS Distributions 	untagged_ptr[MTE_GRANULE_SIZE] = 'b';
1397*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
1398*bbb1b6f9SApple OSS Distributions 
1399*bbb1b6f9SApple OSS Distributions 	/*
1400*bbb1b6f9SApple OSS Distributions 	 * Relocate the page.
1401*bbb1b6f9SApple OSS Distributions 	 * The kernel will also write 'b' and 'c' to the memory.
1402*bbb1b6f9SApple OSS Distributions 	 */
1403*bbb1b6f9SApple OSS Distributions 	int64_t ret = run_sysctl_test("vm_page_relocate", (int64_t)random_tagged_ptr);
1404*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ_LLONG(ret, 1LL, "sysctl: relocate page");
1405*bbb1b6f9SApple OSS Distributions 
1406*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ_CHAR(random_tagged_ptr[0], 'b',
1407*bbb1b6f9SApple OSS Distributions 	    "reading from tagged ptr after relocation");
1408*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ_CHAR(untagged_ptr[MTE_GRANULE_SIZE], 'c',
1409*bbb1b6f9SApple OSS Distributions 	    "reading from untagged ptr after relocation");
1410*bbb1b6f9SApple OSS Distributions #endif /* __arm64__ */
1411*bbb1b6f9SApple OSS Distributions }
1412*bbb1b6f9SApple OSS Distributions 
1413*bbb1b6f9SApple OSS Distributions T_HELPER_DECL(mte_tag_violate, "child process to trigger an MTE violation")
1414*bbb1b6f9SApple OSS Distributions {
1415*bbb1b6f9SApple OSS Distributions 	static const size_t ALLOC_SIZE = MTE_GRANULE_SIZE * 2;
1416*bbb1b6f9SApple OSS Distributions 
1417*bbb1b6f9SApple OSS Distributions 	vm_address_t address = 0;
1418*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = vm_allocate(mach_task_self(), &address, ALLOC_SIZE, VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
1419*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "allocate tagged memory");
1420*bbb1b6f9SApple OSS Distributions 	char *untagged_ptr = (char *) address;
1421*bbb1b6f9SApple OSS Distributions 
1422*bbb1b6f9SApple OSS Distributions 	char *orig_tagged_ptr = __arm_mte_get_tag(untagged_ptr);
1423*bbb1b6f9SApple OSS Distributions 	unsigned int orig_tag = extract_mte_tag(orig_tagged_ptr);
1424*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ_UINT(orig_tag, 0U, "originally assigned tag is zero");
1425*bbb1b6f9SApple OSS Distributions 
1426*bbb1b6f9SApple OSS Distributions 	uint64_t mask = __arm_mte_exclude_tag(orig_tagged_ptr, 0);
1427*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ_LLONG(mask, (1LL << 0), "zero tag is excluded");
1428*bbb1b6f9SApple OSS Distributions 
1429*bbb1b6f9SApple OSS Distributions 	char *random_tagged_ptr = NULL;
1430*bbb1b6f9SApple OSS Distributions 	for (unsigned int i = 0; i < NUM_MTE_TAGS * 4; i++) {
1431*bbb1b6f9SApple OSS Distributions 		random_tagged_ptr = __arm_mte_create_random_tag(untagged_ptr, mask);
1432*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_EXPECT_NE_PTR(orig_tagged_ptr, random_tagged_ptr,
1433*bbb1b6f9SApple OSS Distributions 		    "random tag was not taken from excluded tag set");
1434*bbb1b6f9SApple OSS Distributions 
1435*bbb1b6f9SApple OSS Distributions 		ptrdiff_t diff = __arm_mte_ptrdiff(untagged_ptr, random_tagged_ptr);
1436*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_EXPECT_EQ_ULONG(diff, (ptrdiff_t)0, "untagged %p and tagged %p have identical address bits",
1437*bbb1b6f9SApple OSS Distributions 		    untagged_ptr, random_tagged_ptr);
1438*bbb1b6f9SApple OSS Distributions 	}
1439*bbb1b6f9SApple OSS Distributions 
1440*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag(random_tagged_ptr);
1441*bbb1b6f9SApple OSS Distributions 
1442*bbb1b6f9SApple OSS Distributions 	char *read_back = __arm_mte_get_tag(untagged_ptr);
1443*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ_PTR(read_back, random_tagged_ptr, "tag was committed to memory correctly");
1444*bbb1b6f9SApple OSS Distributions 
1445*bbb1b6f9SApple OSS Distributions 	random_tagged_ptr[0] = 't';
1446*bbb1b6f9SApple OSS Distributions 	random_tagged_ptr[1] = 'e';
1447*bbb1b6f9SApple OSS Distributions 	random_tagged_ptr[2] = 's';
1448*bbb1b6f9SApple OSS Distributions 	random_tagged_ptr[3] = 't';
1449*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ_STR(random_tagged_ptr, "test", "read/write from tagged memory");
1450*bbb1b6f9SApple OSS Distributions 
1451*bbb1b6f9SApple OSS Distributions 	void *next_granule_ptr = orig_tagged_ptr + MTE_GRANULE_SIZE;
1452*bbb1b6f9SApple OSS Distributions 	unsigned int next_granule_tag = extract_mte_tag(next_granule_ptr);
1453*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_UINT(next_granule_tag, 0U,
1454*bbb1b6f9SApple OSS Distributions 	    "next MTE granule still has its originally assigned tag");
1455*bbb1b6f9SApple OSS Distributions 
1456*bbb1b6f9SApple OSS Distributions 	T_LOG("attempting out-of-bounds access to tagged memory");
1457*bbb1b6f9SApple OSS Distributions 	random_tagged_ptr[MTE_GRANULE_SIZE] = '!';
1458*bbb1b6f9SApple OSS Distributions 	T_LOG("bypass: survived OOB access");
1459*bbb1b6f9SApple OSS Distributions 
1460*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag(orig_tagged_ptr);
1461*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag(orig_tagged_ptr + MTE_GRANULE_SIZE);
1462*bbb1b6f9SApple OSS Distributions 	vm_deallocate(mach_task_self(), address, ALLOC_SIZE);
1463*bbb1b6f9SApple OSS Distributions 	exit(0);
1464*bbb1b6f9SApple OSS Distributions }
1465*bbb1b6f9SApple OSS Distributions 
1466*bbb1b6f9SApple OSS Distributions T_HELPER_DECL(mte_copyio_bypass_helper, "child process to test copyio in MTE tag check bypass mode")
1467*bbb1b6f9SApple OSS Distributions {
1468*bbb1b6f9SApple OSS Distributions 	run_mte_copyio_tests(false);
1469*bbb1b6f9SApple OSS Distributions }
1470*bbb1b6f9SApple OSS Distributions 
1471*bbb1b6f9SApple OSS Distributions static void
run_helper_with_sec_bypass(char * helper_name)1472*bbb1b6f9SApple OSS Distributions run_helper_with_sec_bypass(char *helper_name)
1473*bbb1b6f9SApple OSS Distributions {
1474*bbb1b6f9SApple OSS Distributions 	char path[PATH_MAX];
1475*bbb1b6f9SApple OSS Distributions 	uint32_t path_size = sizeof(path);
1476*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath");
1477*bbb1b6f9SApple OSS Distributions 	char *args[] = { path, "-n", helper_name, NULL };
1478*bbb1b6f9SApple OSS Distributions 
1479*bbb1b6f9SApple OSS Distributions 	pid_t child_pid = 0;
1480*bbb1b6f9SApple OSS Distributions 	posix_spawnattr_t attr;
1481*bbb1b6f9SApple OSS Distributions 	errno_t ret = posix_spawnattr_init(&attr);
1482*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_init");
1483*bbb1b6f9SApple OSS Distributions 
1484*bbb1b6f9SApple OSS Distributions 	ret = posix_spawnattr_set_use_sec_transition_shims_np(&attr, POSIX_SPAWN_SECFLAG_EXPLICIT_ENABLE | POSIX_SPAWN_SECFLAG_EXPLICIT_CHECK_BYPASS);
1485*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_set_use_sec_transition_shims_np");
1486*bbb1b6f9SApple OSS Distributions 
1487*bbb1b6f9SApple OSS Distributions 	ret = posix_spawn(&child_pid, path, NULL, &attr, args, NULL);
1488*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ret, "posix_spawn");
1489*bbb1b6f9SApple OSS Distributions 	T_ASSERT_NE(child_pid, 0, "posix_spawn");
1490*bbb1b6f9SApple OSS Distributions 
1491*bbb1b6f9SApple OSS Distributions 	ret = posix_spawnattr_destroy(&attr);
1492*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_destroy");
1493*bbb1b6f9SApple OSS Distributions 
1494*bbb1b6f9SApple OSS Distributions 	int status = 0;
1495*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(waitpid(child_pid, &status, 0), "waitpid");
1496*bbb1b6f9SApple OSS Distributions 	T_EXPECT_TRUE(WIFEXITED(status), "exited successfully");
1497*bbb1b6f9SApple OSS Distributions 	T_EXPECT_TRUE(WEXITSTATUS(status) == 0, "exited with status %d", WEXITSTATUS(status));
1498*bbb1b6f9SApple OSS Distributions }
1499*bbb1b6f9SApple OSS Distributions 
1500*bbb1b6f9SApple OSS Distributions T_DECL(mte_tag_bypass,
1501*bbb1b6f9SApple OSS Distributions     "Test MTE2 tag check bypass works with posix_spawnattr",
1502*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
1503*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
1504*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
1505*bbb1b6f9SApple OSS Distributions {
1506*bbb1b6f9SApple OSS Distributions 	run_helper_with_sec_bypass("mte_tag_violate");
1507*bbb1b6f9SApple OSS Distributions }
1508*bbb1b6f9SApple OSS Distributions 
1509*bbb1b6f9SApple OSS Distributions T_DECL(mte_copyio_bypass,
1510*bbb1b6f9SApple OSS Distributions     "Test MTE2 tag check bypass with copyio operations",
1511*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
1512*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
1513*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
1514*bbb1b6f9SApple OSS Distributions {
1515*bbb1b6f9SApple OSS Distributions 	run_helper_with_sec_bypass("mte_copyio_bypass_helper");
1516*bbb1b6f9SApple OSS Distributions }
1517*bbb1b6f9SApple OSS Distributions 
1518*bbb1b6f9SApple OSS Distributions #ifdef __arm64__
1519*bbb1b6f9SApple OSS Distributions T_DECL(mte_read_only,
1520*bbb1b6f9SApple OSS Distributions     "Verify that setting tags on a read-only mapping results in SIGBUS",
1521*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1522*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
1523*bbb1b6f9SApple OSS Distributions {
1524*bbb1b6f9SApple OSS Distributions 	uint64_t mask;
1525*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
1526*bbb1b6f9SApple OSS Distributions 	void* untagged_ptr = allocate_tagged_memory(MTE_GRANULE_SIZE, &mask);
1527*bbb1b6f9SApple OSS Distributions 	void *tagged_ptr = __arm_mte_create_random_tag(untagged_ptr, mask);
1528*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
1529*bbb1b6f9SApple OSS Distributions 
1530*bbb1b6f9SApple OSS Distributions 	assert_normal_exit(^{
1531*bbb1b6f9SApple OSS Distributions 		__arm_mte_set_tag(tagged_ptr);
1532*bbb1b6f9SApple OSS Distributions 	}, "can set tags on writable memory");
1533*bbb1b6f9SApple OSS Distributions 
1534*bbb1b6f9SApple OSS Distributions 	int ret = mprotect(untagged_ptr, MTE_GRANULE_SIZE, PROT_READ);
1535*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "mprotect");
1536*bbb1b6f9SApple OSS Distributions 
1537*bbb1b6f9SApple OSS Distributions 	tagged_ptr = __arm_mte_increment_tag(tagged_ptr, 1);
1538*bbb1b6f9SApple OSS Distributions 
1539*bbb1b6f9SApple OSS Distributions 	expect_signal(SIGBUS, ^{
1540*bbb1b6f9SApple OSS Distributions 		__arm_mte_set_tag(tagged_ptr);
1541*bbb1b6f9SApple OSS Distributions 	}, "set tag on read-only memory");
1542*bbb1b6f9SApple OSS Distributions 
1543*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
1544*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = vm_deallocate(mach_task_self(), (vm_address_t) untagged_ptr, MTE_GRANULE_SIZE);
1545*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "clean up tagged allocation");
1546*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
1547*bbb1b6f9SApple OSS Distributions }
1548*bbb1b6f9SApple OSS Distributions 
1549*bbb1b6f9SApple OSS Distributions T_DECL(mte_inherit_share,
1550*bbb1b6f9SApple OSS Distributions     "Verify that you can't set VM_INHERIT_SHARE on tagged memory",
1551*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1552*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
1553*bbb1b6f9SApple OSS Distributions {
1554*bbb1b6f9SApple OSS Distributions 	const mach_vm_size_t ALLOC_SIZE = PAGE_SIZE;
1555*bbb1b6f9SApple OSS Distributions 	__block kern_return_t kr;
1556*bbb1b6f9SApple OSS Distributions 
1557*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
1558*bbb1b6f9SApple OSS Distributions 	vm_address_t tagged_addr = allocate_and_tag_range(ALLOC_SIZE, TAG_RANDOM);
1559*bbb1b6f9SApple OSS Distributions 	vm_address_t untagged_addr = tagged_addr & ~MTE_TAG_MASK;
1560*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
1561*bbb1b6f9SApple OSS Distributions 
1562*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
1563*bbb1b6f9SApple OSS Distributions 		int ret = minherit((void*) untagged_addr, ALLOC_SIZE, VM_INHERIT_SHARE);
1564*bbb1b6f9SApple OSS Distributions 		T_LOG("minherit: was not killed and returned %d", ret);
1565*bbb1b6f9SApple OSS Distributions 	}, "minherit(VM_INHERIT_SHARE) on tagged memory");
1566*bbb1b6f9SApple OSS Distributions 
1567*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
1568*bbb1b6f9SApple OSS Distributions 		kr = mach_vm_inherit(mach_task_self(), untagged_addr,
1569*bbb1b6f9SApple OSS Distributions 		ALLOC_SIZE, VM_INHERIT_SHARE);
1570*bbb1b6f9SApple OSS Distributions 		T_LOG("mach_vm_inherit: was not killed and returned %d", kr);
1571*bbb1b6f9SApple OSS Distributions 	}, "mach_vm_inherit(VM_INHERIT_SHARE) on tagged memory");
1572*bbb1b6f9SApple OSS Distributions 
1573*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
1574*bbb1b6f9SApple OSS Distributions 	kr = vm_deallocate(mach_task_self(), untagged_addr, ALLOC_SIZE);
1575*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "clean up tagged allocation");
1576*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
1577*bbb1b6f9SApple OSS Distributions 
1578*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
1579*bbb1b6f9SApple OSS Distributions 		mach_vm_address_t addr = 0;
1580*bbb1b6f9SApple OSS Distributions 		kr = mach_vm_map(mach_task_self(), &addr, ALLOC_SIZE, /* mask = */ 0,
1581*bbb1b6f9SApple OSS Distributions 		VM_FLAGS_ANYWHERE | VM_FLAGS_MTE, MACH_PORT_NULL, /* offset = */ 0,
1582*bbb1b6f9SApple OSS Distributions 		/* copy = */ false, VM_PROT_DEFAULT, VM_PROT_ALL, VM_INHERIT_SHARE);
1583*bbb1b6f9SApple OSS Distributions 		T_LOG("mach_vm_map: was not killed and returned %d", kr);
1584*bbb1b6f9SApple OSS Distributions 
1585*bbb1b6f9SApple OSS Distributions 		T_SETUPBEGIN;
1586*bbb1b6f9SApple OSS Distributions 		kr = vm_deallocate(mach_task_self(), addr, ALLOC_SIZE);
1587*bbb1b6f9SApple OSS Distributions 		T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "clean up mach_vm_map allocation");
1588*bbb1b6f9SApple OSS Distributions 		T_SETUPEND;
1589*bbb1b6f9SApple OSS Distributions 	}, "mach_vm_map(VM_INHERIT_SHARE) to create new tagged memory");
1590*bbb1b6f9SApple OSS Distributions }
1591*bbb1b6f9SApple OSS Distributions 
1592*bbb1b6f9SApple OSS Distributions static vm_object_id_t
get_object_id(mach_port_t task,vm_address_t addr)1593*bbb1b6f9SApple OSS Distributions get_object_id(mach_port_t task, vm_address_t addr)
1594*bbb1b6f9SApple OSS Distributions {
1595*bbb1b6f9SApple OSS Distributions 	unsigned int depth = 1;
1596*bbb1b6f9SApple OSS Distributions 	vm_size_t size;
1597*bbb1b6f9SApple OSS Distributions 	struct vm_region_submap_info_64 info;
1598*bbb1b6f9SApple OSS Distributions 	mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;
1599*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = vm_region_recurse_64(task, &addr, &size, &depth,
1600*bbb1b6f9SApple OSS Distributions 	    (vm_region_info_t) &info, &count);
1601*bbb1b6f9SApple OSS Distributions 	/*
1602*bbb1b6f9SApple OSS Distributions 	 * I'm not sure why it returns KERN_INVALID_ADDRESS in this case, but this
1603*bbb1b6f9SApple OSS Distributions 	 * can happen if the corpse task goes away. That happens if a jetsam event
1604*bbb1b6f9SApple OSS Distributions 	 * occurs (even on an unrelated process) while the test is running.
1605*bbb1b6f9SApple OSS Distributions 	 */
1606*bbb1b6f9SApple OSS Distributions 	if (task != mach_task_self() && kr == KERN_INVALID_ADDRESS) {
1607*bbb1b6f9SApple OSS Distributions 		T_SKIP("corpse port disappeared, bailing...");
1608*bbb1b6f9SApple OSS Distributions 	}
1609*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "get_object_id: vm_region_recurse_64");
1610*bbb1b6f9SApple OSS Distributions 	return info.object_id_full;
1611*bbb1b6f9SApple OSS Distributions }
1612*bbb1b6f9SApple OSS Distributions 
1613*bbb1b6f9SApple OSS Distributions T_DECL(mte_corpse_fork,
1614*bbb1b6f9SApple OSS Distributions     "Verify that corpse-fork sharing paths work normally on tagged memory",
1615*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1616*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
1617*bbb1b6f9SApple OSS Distributions     /* rdar://138528295 (Provide a mechanism to guarantee availability of corpse slots for tests) */
1618*bbb1b6f9SApple OSS Distributions     T_META_RUN_CONCURRENTLY(false))
1619*bbb1b6f9SApple OSS Distributions {
1620*bbb1b6f9SApple OSS Distributions 	/*
1621*bbb1b6f9SApple OSS Distributions 	 * The corpse-fork path shares memory in two additional cases:
1622*bbb1b6f9SApple OSS Distributions 	 * (1) if the entry has INHERIT_NONE, and
1623*bbb1b6f9SApple OSS Distributions 	 * (2) if the memory is "owned" by the process for accounting purposes. This
1624*bbb1b6f9SApple OSS Distributions 	 * essentially means that it is purgeable & volatile.
1625*bbb1b6f9SApple OSS Distributions 	 * We want to ensure that these cases are unaffected by MTE restrictions on
1626*bbb1b6f9SApple OSS Distributions 	 * VM_INHERIT_SHARE.
1627*bbb1b6f9SApple OSS Distributions 	 */
1628*bbb1b6f9SApple OSS Distributions 	kern_return_t kr;
1629*bbb1b6f9SApple OSS Distributions 	mach_vm_size_t alloc_size = PAGE_SIZE;
1630*bbb1b6f9SApple OSS Distributions 	mach_vm_address_t inherit_none_addr, owned_addr, regular_addr;
1631*bbb1b6f9SApple OSS Distributions 
1632*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
1633*bbb1b6f9SApple OSS Distributions 
1634*bbb1b6f9SApple OSS Distributions 	/* First up, expand the system's corpse pool size.
1635*bbb1b6f9SApple OSS Distributions 	 * Otherwise, this test sporadically can't secure the corpse slots it needs.
1636*bbb1b6f9SApple OSS Distributions 	 */
1637*bbb1b6f9SApple OSS Distributions 	int original_total_corpses_allowed;
1638*bbb1b6f9SApple OSS Distributions 	size_t original_total_corpses_allowed_sizeof = sizeof(original_total_corpses_allowed);
1639*bbb1b6f9SApple OSS Distributions 	int total_corpses_allowed = 20;
1640*bbb1b6f9SApple OSS Distributions 	int ret = sysctlbyname("kern.total_corpses_allowed",
1641*bbb1b6f9SApple OSS Distributions 	    &original_total_corpses_allowed, &original_total_corpses_allowed_sizeof,
1642*bbb1b6f9SApple OSS Distributions 	    &total_corpses_allowed, sizeof(total_corpses_allowed));
1643*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_ZERO(ret, "sysctl kern.total_corpses_allowed");
1644*bbb1b6f9SApple OSS Distributions 
1645*bbb1b6f9SApple OSS Distributions 	/* set up regular MTE-tagged region */
1646*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &regular_addr, alloc_size,
1647*bbb1b6f9SApple OSS Distributions 	    VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
1648*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_allocate regular region");
1649*bbb1b6f9SApple OSS Distributions 
1650*bbb1b6f9SApple OSS Distributions 	/* set up region for testing INHERIT_NONE */
1651*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &inherit_none_addr, alloc_size,
1652*bbb1b6f9SApple OSS Distributions 	    VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
1653*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_allocate INHERIT_NONE region");
1654*bbb1b6f9SApple OSS Distributions 
1655*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_inherit(mach_task_self(), inherit_none_addr, alloc_size,
1656*bbb1b6f9SApple OSS Distributions 	    VM_INHERIT_NONE);
1657*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_inherit(INHERIT_NONE)");
1658*bbb1b6f9SApple OSS Distributions 
1659*bbb1b6f9SApple OSS Distributions 	/* set up region for testing "owned" memory */
1660*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &owned_addr, alloc_size,
1661*bbb1b6f9SApple OSS Distributions 	    VM_FLAGS_ANYWHERE | VM_FLAGS_MTE | VM_FLAGS_PURGABLE);
1662*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_allocate owned region");
1663*bbb1b6f9SApple OSS Distributions 
1664*bbb1b6f9SApple OSS Distributions 	int purgable_state = VM_PURGABLE_VOLATILE;
1665*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_purgable_control(mach_task_self(), owned_addr, VM_PURGABLE_SET_STATE,
1666*bbb1b6f9SApple OSS Distributions 	    &purgable_state);
1667*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_purgable_control(VM_PURGABLE_VOLATILE)");
1668*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
1669*bbb1b6f9SApple OSS Distributions 
1670*bbb1b6f9SApple OSS Distributions 	/* Write in some data and tags */
1671*bbb1b6f9SApple OSS Distributions 	char *regular_ptr = __arm_mte_increment_tag((char*) regular_addr, 1);
1672*bbb1b6f9SApple OSS Distributions 	char *inherit_none_ptr = __arm_mte_increment_tag((char*) inherit_none_addr, 2);
1673*bbb1b6f9SApple OSS Distributions 	char *owned_ptr = __arm_mte_increment_tag((char*) owned_addr, 3);
1674*bbb1b6f9SApple OSS Distributions 	for (size_t i = 0; i < alloc_size; i++) {
1675*bbb1b6f9SApple OSS Distributions 		if (i % MTE_GRANULE_SIZE == 0) {
1676*bbb1b6f9SApple OSS Distributions 			__arm_mte_set_tag(&regular_ptr[i]);
1677*bbb1b6f9SApple OSS Distributions 			__arm_mte_set_tag(&inherit_none_ptr[i]);
1678*bbb1b6f9SApple OSS Distributions 			__arm_mte_set_tag(&owned_ptr[i]);
1679*bbb1b6f9SApple OSS Distributions 		}
1680*bbb1b6f9SApple OSS Distributions 		regular_ptr[i] = 'a';
1681*bbb1b6f9SApple OSS Distributions 		inherit_none_ptr[i] = 'b';
1682*bbb1b6f9SApple OSS Distributions 		owned_ptr[i] = 'c';
1683*bbb1b6f9SApple OSS Distributions 	}
1684*bbb1b6f9SApple OSS Distributions 	T_LOG("wrote data and tags");
1685*bbb1b6f9SApple OSS Distributions 
1686*bbb1b6f9SApple OSS Distributions 	mach_port_t corpse_port;
1687*bbb1b6f9SApple OSS Distributions 	size_t NUM_RETRIES = 5;
1688*bbb1b6f9SApple OSS Distributions 	for (size_t i = 0;; i++) {
1689*bbb1b6f9SApple OSS Distributions 		kr = task_generate_corpse(mach_task_self(), &corpse_port);
1690*bbb1b6f9SApple OSS Distributions 		if (kr == KERN_RESOURCE_SHORTAGE) {
1691*bbb1b6f9SApple OSS Distributions 			T_LOG("hit system corpse limit");
1692*bbb1b6f9SApple OSS Distributions 			if (i == NUM_RETRIES) {
1693*bbb1b6f9SApple OSS Distributions 				T_SKIP("retried too many times, bailing...");
1694*bbb1b6f9SApple OSS Distributions 			} else {
1695*bbb1b6f9SApple OSS Distributions 				/* give ReportCrash some time to finish handling some corpses */
1696*bbb1b6f9SApple OSS Distributions 				sleep(2);
1697*bbb1b6f9SApple OSS Distributions 				/* ... then retry */
1698*bbb1b6f9SApple OSS Distributions 				T_LOG("retrying... (%lu/%lu)", i + 1, NUM_RETRIES);
1699*bbb1b6f9SApple OSS Distributions 				continue;
1700*bbb1b6f9SApple OSS Distributions 			}
1701*bbb1b6f9SApple OSS Distributions 		}
1702*bbb1b6f9SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(kr, "task_generate_corpse");
1703*bbb1b6f9SApple OSS Distributions 		break;
1704*bbb1b6f9SApple OSS Distributions 	}
1705*bbb1b6f9SApple OSS Distributions 
1706*bbb1b6f9SApple OSS Distributions 	/*
1707*bbb1b6f9SApple OSS Distributions 	 * Make sure the "regular" region was not shared.
1708*bbb1b6f9SApple OSS Distributions 	 * Note: in the case of symmetric CoW, the object IDs may match even if
1709*bbb1b6f9SApple OSS Distributions 	 * there is no true sharing happening. However, since we only expect delayed
1710*bbb1b6f9SApple OSS Distributions 	 * CoW or eager copies for MTE objects, this isn't a concern here.
1711*bbb1b6f9SApple OSS Distributions 	 */
1712*bbb1b6f9SApple OSS Distributions 	vm_object_id_t regular_id = get_object_id(mach_task_self(), regular_addr);
1713*bbb1b6f9SApple OSS Distributions 	vm_object_id_t regular_corpse_id = get_object_id(corpse_port, regular_addr);
1714*bbb1b6f9SApple OSS Distributions 	T_EXPECT_NE(regular_id, regular_corpse_id, "regular region was not shared");
1715*bbb1b6f9SApple OSS Distributions 
1716*bbb1b6f9SApple OSS Distributions 	/* Make sure the INHERIT_NONE region was shared */
1717*bbb1b6f9SApple OSS Distributions 	vm_object_id_t inherit_none_id = get_object_id(mach_task_self(), inherit_none_addr);
1718*bbb1b6f9SApple OSS Distributions 	vm_object_id_t inherit_none_corpse_id = get_object_id(corpse_port, inherit_none_addr);
1719*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ(inherit_none_id, inherit_none_corpse_id, "INHERIT_NONE region was shared");
1720*bbb1b6f9SApple OSS Distributions 
1721*bbb1b6f9SApple OSS Distributions 	/* Make sure the owned region was shared */
1722*bbb1b6f9SApple OSS Distributions 	vm_object_id_t owned_id = get_object_id(mach_task_self(), owned_addr);
1723*bbb1b6f9SApple OSS Distributions 	vm_object_id_t owned_corpse_id = get_object_id(corpse_port, owned_addr);
1724*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ(owned_id, owned_corpse_id, "owned region was shared");
1725*bbb1b6f9SApple OSS Distributions 
1726*bbb1b6f9SApple OSS Distributions 	/* Cleanup */
1727*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
1728*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), regular_addr, alloc_size);
1729*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "deallocate regular allocation");
1730*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), inherit_none_addr, alloc_size);
1731*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "deallocate INHERIT_NONE allocation");
1732*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), owned_addr, alloc_size);
1733*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "deallocate owned allocation");
1734*bbb1b6f9SApple OSS Distributions 	kr = mach_port_deallocate(mach_task_self(), corpse_port);
1735*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "deallocate corpse port");
1736*bbb1b6f9SApple OSS Distributions 
1737*bbb1b6f9SApple OSS Distributions 	/* Reduce the corpse pool size back to its original value */
1738*bbb1b6f9SApple OSS Distributions 	ret = sysctlbyname("kern.total_corpses_allowed",
1739*bbb1b6f9SApple OSS Distributions 	    NULL, 0,
1740*bbb1b6f9SApple OSS Distributions 	    &original_total_corpses_allowed, sizeof(original_total_corpses_allowed));
1741*bbb1b6f9SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_ZERO(ret, "sysctl kern.total_corpses_allowed");
1742*bbb1b6f9SApple OSS Distributions 
1743*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
1744*bbb1b6f9SApple OSS Distributions }
1745*bbb1b6f9SApple OSS Distributions 
1746*bbb1b6f9SApple OSS Distributions T_DECL(mte_aio,
1747*bbb1b6f9SApple OSS Distributions     "Test MTE asynchronous access faults when the kernel does copyio on behalf of a process",
1748*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1749*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
1750*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(false) /* rdar://154801490 */) {
1751*bbb1b6f9SApple OSS Distributions 	const mach_vm_size_t BUF_SIZE = MTE_GRANULE_SIZE;
1752*bbb1b6f9SApple OSS Distributions 	uint64_t mask;
1753*bbb1b6f9SApple OSS Distributions 
1754*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
1755*bbb1b6f9SApple OSS Distributions 	char *buf_untagged = allocate_tagged_memory(BUF_SIZE, &mask);
1756*bbb1b6f9SApple OSS Distributions 	char *buf_tagged = __arm_mte_create_random_tag(buf_untagged, mask);
1757*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag(buf_tagged);
1758*bbb1b6f9SApple OSS Distributions 	strncpy(buf_tagged, "ABCDEFG", BUF_SIZE);
1759*bbb1b6f9SApple OSS Distributions 
1760*bbb1b6f9SApple OSS Distributions 	char *buf_incorrectly_tagged = __arm_mte_increment_tag(buf_tagged, 1);
1761*bbb1b6f9SApple OSS Distributions 	int fd = fileno(tmpfile());
1762*bbb1b6f9SApple OSS Distributions 
1763*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
1764*bbb1b6f9SApple OSS Distributions 
1765*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
1766*bbb1b6f9SApple OSS Distributions 		struct aiocb aiocb = {
1767*bbb1b6f9SApple OSS Distributions 		        .aio_fildes = fd,
1768*bbb1b6f9SApple OSS Distributions 		        .aio_offset = 0,
1769*bbb1b6f9SApple OSS Distributions 		        .aio_buf = buf_incorrectly_tagged,
1770*bbb1b6f9SApple OSS Distributions 		        .aio_nbytes = strlen(buf_tagged),
1771*bbb1b6f9SApple OSS Distributions 		};
1772*bbb1b6f9SApple OSS Distributions 		int ret = aio_write(&aiocb);
1773*bbb1b6f9SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(ret, "aio_write");
1774*bbb1b6f9SApple OSS Distributions 
1775*bbb1b6f9SApple OSS Distributions 		/* wait for the kernel to handle our async I/O */
1776*bbb1b6f9SApple OSS Distributions 		/* we should be killed at some point while this happens */
1777*bbb1b6f9SApple OSS Distributions 		const struct aiocb *aio_list[1] = { &aiocb };
1778*bbb1b6f9SApple OSS Distributions 		(void)aio_suspend(aio_list, 1, NULL);
1779*bbb1b6f9SApple OSS Distributions 
1780*bbb1b6f9SApple OSS Distributions 		/* we were not killed: */
1781*bbb1b6f9SApple OSS Distributions 		close(fd);
1782*bbb1b6f9SApple OSS Distributions 		T_ASSERT_FAIL("aio write with untagged pointer completed successfully");
1783*bbb1b6f9SApple OSS Distributions 	}, "asynchronous I/O write from tagged buffer with incorrect MTE tags");
1784*bbb1b6f9SApple OSS Distributions 
1785*bbb1b6f9SApple OSS Distributions 	char read_buf[BUF_SIZE];
1786*bbb1b6f9SApple OSS Distributions 	ssize_t bytes_read = read(fd, read_buf, sizeof(read_buf));
1787*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(bytes_read, "read from tmpfile");
1788*bbb1b6f9SApple OSS Distributions 
1789*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ(bytes_read, 0L, "no bytes sent over tmpfile");
1790*bbb1b6f9SApple OSS Distributions 
1791*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
1792*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = vm_deallocate(mach_task_self(), (vm_address_t) buf_untagged, BUF_SIZE);
1793*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "deallocate tagged buffer");
1794*bbb1b6f9SApple OSS Distributions 
1795*bbb1b6f9SApple OSS Distributions 	close(fd);
1796*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
1797*bbb1b6f9SApple OSS Distributions }
1798*bbb1b6f9SApple OSS Distributions 
1799*bbb1b6f9SApple OSS Distributions T_HELPER_DECL(mte_tag_violate_aio, "child process to trigger an asynchronous MTE violation via AIO") {
1800*bbb1b6f9SApple OSS Distributions 	const mach_vm_size_t BUF_SIZE = MTE_GRANULE_SIZE;
1801*bbb1b6f9SApple OSS Distributions 	uint64_t mask;
1802*bbb1b6f9SApple OSS Distributions 
1803*bbb1b6f9SApple OSS Distributions 	char *buf_untagged = allocate_tagged_memory(BUF_SIZE, &mask);
1804*bbb1b6f9SApple OSS Distributions 	char *buf_tagged = __arm_mte_create_random_tag(buf_untagged, mask);
1805*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag(buf_tagged);
1806*bbb1b6f9SApple OSS Distributions 
1807*bbb1b6f9SApple OSS Distributions 	strncpy(buf_tagged, "ABCDEFG", BUF_SIZE);
1808*bbb1b6f9SApple OSS Distributions 	size_t length = strlen(buf_tagged);
1809*bbb1b6f9SApple OSS Distributions 
1810*bbb1b6f9SApple OSS Distributions 	char *buf_incorrectly_tagged = __arm_mte_increment_tag(buf_tagged, 1);
1811*bbb1b6f9SApple OSS Distributions 	int fd = fileno(tmpfile());
1812*bbb1b6f9SApple OSS Distributions 
1813*bbb1b6f9SApple OSS Distributions 	struct aiocb aiocb = {
1814*bbb1b6f9SApple OSS Distributions 		.aio_fildes = fd,
1815*bbb1b6f9SApple OSS Distributions 		.aio_offset = 0,
1816*bbb1b6f9SApple OSS Distributions 		.aio_buf = buf_incorrectly_tagged,
1817*bbb1b6f9SApple OSS Distributions 		.aio_nbytes = length,
1818*bbb1b6f9SApple OSS Distributions 	};
1819*bbb1b6f9SApple OSS Distributions 	int ret = aio_write(&aiocb);
1820*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "aio_write");
1821*bbb1b6f9SApple OSS Distributions 
1822*bbb1b6f9SApple OSS Distributions 	/* wait for the kernel to handle our async I/O */
1823*bbb1b6f9SApple OSS Distributions 	const struct aiocb *aio_list[1] = { &aiocb };
1824*bbb1b6f9SApple OSS Distributions 	ret = aio_suspend(aio_list, 1, NULL);
1825*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "aio_suspend");
1826*bbb1b6f9SApple OSS Distributions 
1827*bbb1b6f9SApple OSS Distributions 	char read_buf[BUF_SIZE];
1828*bbb1b6f9SApple OSS Distributions 	ssize_t bytes_read = read(fd, read_buf, sizeof(read_buf));
1829*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(bytes_read, "read from tmpfile");
1830*bbb1b6f9SApple OSS Distributions 
1831*bbb1b6f9SApple OSS Distributions 	/* these have to be "may fail" instead of "expect fail" due to rdar://136258500 */
1832*bbb1b6f9SApple OSS Distributions 	T_MAYFAIL_WITH_RADAR(136300841);
1833*bbb1b6f9SApple OSS Distributions 	T_EXPECT_EQ(bytes_read, (ssize_t)length, "bytes sent over tmpfile");
1834*bbb1b6f9SApple OSS Distributions 
1835*bbb1b6f9SApple OSS Distributions 	for (size_t i = 0; i < length; i++) {
1836*bbb1b6f9SApple OSS Distributions 		T_MAYFAIL_WITH_RADAR(136300841);
1837*bbb1b6f9SApple OSS Distributions 		T_EXPECT_EQ(buf_tagged[i], read_buf[i], "character %lu matches", i);
1838*bbb1b6f9SApple OSS Distributions 	}
1839*bbb1b6f9SApple OSS Distributions 
1840*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = vm_deallocate(mach_task_self(), (vm_address_t) buf_untagged, BUF_SIZE);
1841*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "deallocate tagged buffer");
1842*bbb1b6f9SApple OSS Distributions 
1843*bbb1b6f9SApple OSS Distributions 	close(fd);
1844*bbb1b6f9SApple OSS Distributions }
1845*bbb1b6f9SApple OSS Distributions 
1846*bbb1b6f9SApple OSS Distributions T_DECL(mte_aio_tag_bypass,
1847*bbb1b6f9SApple OSS Distributions     "Test nonfatal MTE asynchronous access faults with tag check bypass",
1848*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1849*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC) {
1850*bbb1b6f9SApple OSS Distributions 	run_helper_with_sec_bypass("mte_tag_violate_aio");
1851*bbb1b6f9SApple OSS Distributions }
1852*bbb1b6f9SApple OSS Distributions #endif /* __arm64__ */
1853*bbb1b6f9SApple OSS Distributions 
1854*bbb1b6f9SApple OSS Distributions static void
run_iokit_sysctl_test(int vector)1855*bbb1b6f9SApple OSS Distributions run_iokit_sysctl_test(int vector)
1856*bbb1b6f9SApple OSS Distributions {
1857*bbb1b6f9SApple OSS Distributions 	int ret = sysctlbyname("kern.iokittest", NULL, 0, &vector, sizeof(vector));
1858*bbb1b6f9SApple OSS Distributions 	T_EXPECT_POSIX_ZERO(ret, "sysctl kern.iokittest(%d)", vector);
1859*bbb1b6f9SApple OSS Distributions }
1860*bbb1b6f9SApple OSS Distributions 
1861*bbb1b6f9SApple OSS Distributions T_DECL(mte_iomd_cpu_map,
1862*bbb1b6f9SApple OSS Distributions     "Test that IOMemoryDescriptor::map() of userspace memory is mapped as untagged in the kernel",
1863*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
1864*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1865*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true),
1866*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
1867*bbb1b6f9SApple OSS Distributions {
1868*bbb1b6f9SApple OSS Distributions 	run_iokit_sysctl_test(333);
1869*bbb1b6f9SApple OSS Distributions }
1870*bbb1b6f9SApple OSS Distributions 
1871*bbb1b6f9SApple OSS Distributions T_DECL(mte_iomd_read_write_bytes,
1872*bbb1b6f9SApple OSS Distributions     "Test that IOMemoryDescriptor::read/writeBytes() of tagged memory works",
1873*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
1874*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1875*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true),
1876*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC) {
1877*bbb1b6f9SApple OSS Distributions 	run_iokit_sysctl_test(334);
1878*bbb1b6f9SApple OSS Distributions }
1879*bbb1b6f9SApple OSS Distributions 
1880*bbb1b6f9SApple OSS Distributions T_DECL(iomd_read_write_bytes_non_mte,
1881*bbb1b6f9SApple OSS Distributions     "Test that IOMemoryDescriptor::read/writeBytes() of untagged memory works",
1882*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
1883*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1884*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true),
1885*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC) {
1886*bbb1b6f9SApple OSS Distributions 	run_iokit_sysctl_test(335);
1887*bbb1b6f9SApple OSS Distributions }
1888*bbb1b6f9SApple OSS Distributions 
1889*bbb1b6f9SApple OSS Distributions T_DECL(iomd_read_bytes_with_tcf,
1890*bbb1b6f9SApple OSS Distributions     "Test that tag mismatches during IOMemoryDescriptor::readBytes() get detected",
1891*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
1892*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1893*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true),
1894*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC) {
1895*bbb1b6f9SApple OSS Distributions 	/* The iokit test will generate an artificial tag check mismatch midway through the buffer */
1896*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
1897*bbb1b6f9SApple OSS Distributions 		run_iokit_sysctl_test(336);
1898*bbb1b6f9SApple OSS Distributions 		T_ASSERT_FAIL("Expected this process to get killed");
1899*bbb1b6f9SApple OSS Distributions 	}, "asynchronous TCF in readBytes()");
1900*bbb1b6f9SApple OSS Distributions }
1901*bbb1b6f9SApple OSS Distributions 
1902*bbb1b6f9SApple OSS Distributions T_DECL(iomd_write_bytes_with_tcf,
1903*bbb1b6f9SApple OSS Distributions     "Test that tag mismatches during IOMemoryDescriptor::writeBytes() continue to work out of the box",
1904*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
1905*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1906*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true),
1907*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC) {
1908*bbb1b6f9SApple OSS Distributions 	/* The iokit test will generate an artificial tag check mismatch midway through the buffer */
1909*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
1910*bbb1b6f9SApple OSS Distributions 		run_iokit_sysctl_test(337);
1911*bbb1b6f9SApple OSS Distributions 		T_ASSERT_FAIL("Expected this process to get killed");
1912*bbb1b6f9SApple OSS Distributions 	}, "asynchronous TCF in writeBytes()");
1913*bbb1b6f9SApple OSS Distributions }
1914*bbb1b6f9SApple OSS Distributions 
1915*bbb1b6f9SApple OSS Distributions T_DECL(iomd_create_alias_mapping_in_this_map,
1916*bbb1b6f9SApple OSS Distributions     "Test that IOMemoryDescriptor::createMappingInTask() of tagged memory in the current task works",
1917*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
1918*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1919*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true),
1920*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC) {
1921*bbb1b6f9SApple OSS Distributions 	run_iokit_sysctl_test(340);
1922*bbb1b6f9SApple OSS Distributions }
1923*bbb1b6f9SApple OSS Distributions 
1924*bbb1b6f9SApple OSS Distributions T_DECL(iomd_create_alias_mapping_in_kernel_map,
1925*bbb1b6f9SApple OSS Distributions     "Test that IOMemoryDescriptor::createMappingInTask() of tagged memory in the kernel is allowed",
1926*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
1927*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1928*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true),
1929*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC) {
1930*bbb1b6f9SApple OSS Distributions 	run_iokit_sysctl_test(342);
1931*bbb1b6f9SApple OSS Distributions }
1932*bbb1b6f9SApple OSS Distributions 
1933*bbb1b6f9SApple OSS Distributions T_DECL(mte_cpu_map_pageout,
1934*bbb1b6f9SApple OSS Distributions     "Test correct behavior of kernel CPU mapping after userspace mapping is paged out",
1935*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
1936*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1937*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true),
1938*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
1939*bbb1b6f9SApple OSS Distributions {
1940*bbb1b6f9SApple OSS Distributions 	mach_vm_size_t alloc_size = PAGE_SIZE;
1941*bbb1b6f9SApple OSS Distributions 	char *ptr = (char*)(allocate_and_tag_range(alloc_size, TAG_RANDOM_EXCLUDE(0xF)));
1942*bbb1b6f9SApple OSS Distributions 	char value = 'A';
1943*bbb1b6f9SApple OSS Distributions 	memset(ptr, value, alloc_size);
1944*bbb1b6f9SApple OSS Distributions 
1945*bbb1b6f9SApple OSS Distributions 	struct {
1946*bbb1b6f9SApple OSS Distributions 		mach_vm_size_t size;
1947*bbb1b6f9SApple OSS Distributions 		char *ptr;
1948*bbb1b6f9SApple OSS Distributions 		char value;
1949*bbb1b6f9SApple OSS Distributions 	} args = { alloc_size, ptr, value };
1950*bbb1b6f9SApple OSS Distributions 	run_sysctl_test("vm_cpu_map_pageout", (int64_t)(&args));
1951*bbb1b6f9SApple OSS Distributions }
1952*bbb1b6f9SApple OSS Distributions 
1953*bbb1b6f9SApple OSS Distributions T_DECL(vm_region_recurse_mte_info,
1954*bbb1b6f9SApple OSS Distributions     "Ensure metadata returned by vm_region_recurse correct reflects MTE status",
1955*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
1956*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
1957*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
1958*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true))
1959*bbb1b6f9SApple OSS Distributions {
1960*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
1961*bbb1b6f9SApple OSS Distributions 
1962*bbb1b6f9SApple OSS Distributions 	/* Given an MTE-enabled region */
1963*bbb1b6f9SApple OSS Distributions 	const mach_vm_size_t alloc_size = PAGE_SIZE;
1964*bbb1b6f9SApple OSS Distributions 	vm_address_t tagged_buffer_addr = allocate_and_tag_range(alloc_size, 0xa);
1965*bbb1b6f9SApple OSS Distributions 	vm_address_t untagged_handle_to_tagged_address = tagged_buffer_addr & ~MTE_TAG_MASK;
1966*bbb1b6f9SApple OSS Distributions 
1967*bbb1b6f9SApple OSS Distributions 	/* And a non-MTE-enabled region */
1968*bbb1b6f9SApple OSS Distributions 	/* (Manually select an address to be sure we're placed in a new region from the tagged region) */
1969*bbb1b6f9SApple OSS Distributions 	mach_vm_address_t untagged_buffer_addr = untagged_handle_to_tagged_address + (32 * 1024);
1970*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = mach_vm_allocate(
1971*bbb1b6f9SApple OSS Distributions 		mach_task_self(),
1972*bbb1b6f9SApple OSS Distributions 		&untagged_buffer_addr,
1973*bbb1b6f9SApple OSS Distributions 		alloc_size,
1974*bbb1b6f9SApple OSS Distributions 		VM_FLAGS_FIXED );
1975*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "Allocated untagged page");
1976*bbb1b6f9SApple OSS Distributions 	/* (And write to it to be sure we populate a VM object) */
1977*bbb1b6f9SApple OSS Distributions 	memset((uint8_t*)untagged_buffer_addr, 0, alloc_size);
1978*bbb1b6f9SApple OSS Distributions 
1979*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
1980*bbb1b6f9SApple OSS Distributions 
1981*bbb1b6f9SApple OSS Distributions 	/* When we query the attributes of the region covering the MTE-enabled buffer */
1982*bbb1b6f9SApple OSS Distributions 	mach_vm_address_t addr = untagged_handle_to_tagged_address;
1983*bbb1b6f9SApple OSS Distributions 	mach_vm_size_t addr_size = alloc_size;
1984*bbb1b6f9SApple OSS Distributions 	uint32_t nesting_depth = UINT_MAX;
1985*bbb1b6f9SApple OSS Distributions 	mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_V2_COUNT_64;
1986*bbb1b6f9SApple OSS Distributions 	vm_region_submap_info_data_64_t region_info;
1987*bbb1b6f9SApple OSS Distributions 	kr = vm_region_recurse_64(mach_task_self(), (vm_address_t*)&addr, (vm_size_t*)&addr_size, &nesting_depth, (vm_region_recurse_info_t)&region_info, &count);
1988*bbb1b6f9SApple OSS Distributions 
1989*bbb1b6f9SApple OSS Distributions 	/* Then our metadata confirms that the region contains an MTE-mappable object */
1990*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "Query MTE-enabled region");
1991*bbb1b6f9SApple OSS Distributions 	T_ASSERT_TRUE(region_info.flags & VM_REGION_FLAG_MTE_ENABLED, "Expected metadata to reflect an MTE mappable object");
1992*bbb1b6f9SApple OSS Distributions 
1993*bbb1b6f9SApple OSS Distributions 	/* And when we query the same thing via the 'short' info */
1994*bbb1b6f9SApple OSS Distributions 	addr = untagged_handle_to_tagged_address;
1995*bbb1b6f9SApple OSS Distributions 	addr_size = alloc_size;
1996*bbb1b6f9SApple OSS Distributions 	nesting_depth = UINT_MAX;
1997*bbb1b6f9SApple OSS Distributions 	count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64;
1998*bbb1b6f9SApple OSS Distributions 	vm_region_submap_short_info_data_64_t short_info;
1999*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_region_recurse(mach_task_self(), (mach_vm_address_t*)&addr, (mach_vm_size_t*)&addr_size, &nesting_depth, (vm_region_info_t)&short_info, &count);
2000*bbb1b6f9SApple OSS Distributions 
2001*bbb1b6f9SApple OSS Distributions 	/* Then the short metadata also confirms that the region contains an MTE-mappable object */
2002*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "Query MTE-enabled region");
2003*bbb1b6f9SApple OSS Distributions 	T_ASSERT_TRUE(short_info.flags & VM_REGION_FLAG_MTE_ENABLED, "Expected metadata to reflect an MTE mappable object");
2004*bbb1b6f9SApple OSS Distributions 
2005*bbb1b6f9SApple OSS Distributions 	/* And when we query the attributes of the region covering the non-MTE-enabled buffer */
2006*bbb1b6f9SApple OSS Distributions 	addr = untagged_buffer_addr;
2007*bbb1b6f9SApple OSS Distributions 	addr_size = alloc_size;
2008*bbb1b6f9SApple OSS Distributions 	nesting_depth = UINT_MAX;
2009*bbb1b6f9SApple OSS Distributions 	count = VM_REGION_SUBMAP_INFO_V2_COUNT_64;
2010*bbb1b6f9SApple OSS Distributions 	memset(&region_info, 0, sizeof(region_info));
2011*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_region_recurse(mach_task_self(), (mach_vm_address_t*)&addr, (mach_vm_size_t*)&addr_size, &nesting_depth, (vm_region_info_t)&region_info, &count);
2012*bbb1b6f9SApple OSS Distributions 
2013*bbb1b6f9SApple OSS Distributions 	/* Then our metadata confirm that the region does not contain an MTE-mappable object */
2014*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "Query MTE-disabled region");
2015*bbb1b6f9SApple OSS Distributions 	T_ASSERT_FALSE(region_info.flags & VM_REGION_FLAG_MTE_ENABLED, "Expected metadata to reflect no MTE mappable object");
2016*bbb1b6f9SApple OSS Distributions 
2017*bbb1b6f9SApple OSS Distributions 	/* And when we query the same thing via the 'short' info */
2018*bbb1b6f9SApple OSS Distributions 	addr = untagged_buffer_addr;
2019*bbb1b6f9SApple OSS Distributions 	addr_size = alloc_size;
2020*bbb1b6f9SApple OSS Distributions 	nesting_depth = UINT_MAX;
2021*bbb1b6f9SApple OSS Distributions 	count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64;
2022*bbb1b6f9SApple OSS Distributions 	memset(&short_info, 0, sizeof(short_info));
2023*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_region_recurse(mach_task_self(), (mach_vm_address_t*)&addr, (mach_vm_size_t*)&addr_size, &nesting_depth, (vm_region_info_t)&short_info, &count);
2024*bbb1b6f9SApple OSS Distributions 
2025*bbb1b6f9SApple OSS Distributions 	/* Then the short metadata also confirms that the region does not contain an MTE-mappable object */
2026*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "Query MTE-disabled region");
2027*bbb1b6f9SApple OSS Distributions 	T_ASSERT_FALSE(short_info.flags & VM_REGION_FLAG_MTE_ENABLED, "Expected metadata to reflect no MTE mappable object");
2028*bbb1b6f9SApple OSS Distributions 
2029*bbb1b6f9SApple OSS Distributions 	/* Cleanup */
2030*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), untagged_handle_to_tagged_address, alloc_size);
2031*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "deallocate tagged memory");
2032*bbb1b6f9SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), untagged_buffer_addr, alloc_size);
2033*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "deallocate untagged memory");
2034*bbb1b6f9SApple OSS Distributions }
2035*bbb1b6f9SApple OSS Distributions 
2036*bbb1b6f9SApple OSS Distributions T_DECL(mach_vm_read_of_remote_proc,
2037*bbb1b6f9SApple OSS Distributions     "Verify that mach_vm_read of a remote MTE-enabled process works",
2038*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
2039*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
2040*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
2041*bbb1b6f9SApple OSS Distributions     /* rdar://151142487: gcore won't work on iOS without unrestricting task_read_for_pid */
2042*bbb1b6f9SApple OSS Distributions     T_META_BOOTARGS_SET("amfi_unrestrict_task_for_pid=1"),
2043*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true))
2044*bbb1b6f9SApple OSS Distributions {
2045*bbb1b6f9SApple OSS Distributions 	/* Given a process that is launched as MTE-enabled */
2046*bbb1b6f9SApple OSS Distributions 	char* sleep_args[] = { "/bin/sleep", "5000", NULL};
2047*bbb1b6f9SApple OSS Distributions 	posix_spawnattr_t attr;
2048*bbb1b6f9SApple OSS Distributions 	errno_t ret = posix_spawnattr_init(&attr);
2049*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_init");
2050*bbb1b6f9SApple OSS Distributions 	ret = posix_spawnattr_set_use_sec_transition_shims_np(&attr, POSIX_SPAWN_SECFLAG_EXPLICIT_ENABLE);
2051*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_set_use_sec_transition_shims_np");
2052*bbb1b6f9SApple OSS Distributions 	pid_t child_pid = 0;
2053*bbb1b6f9SApple OSS Distributions 	ret = posix_spawn(&child_pid, sleep_args[0], NULL, &attr, sleep_args, NULL);
2054*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ret, "posix_spawn");
2055*bbb1b6f9SApple OSS Distributions 	T_ASSERT_NE(child_pid, 0, "posix_spawn");
2056*bbb1b6f9SApple OSS Distributions 	ret = posix_spawnattr_destroy(&attr);
2057*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_destroy");
2058*bbb1b6f9SApple OSS Distributions 
2059*bbb1b6f9SApple OSS Distributions 	/* And it's MTE-enabled as expected */
2060*bbb1b6f9SApple OSS Distributions 	validate_proc_pidinfo_mte_status(child_pid, true);
2061*bbb1b6f9SApple OSS Distributions 
2062*bbb1b6f9SApple OSS Distributions 	/* And gcore attempts to mach_vm_read some of its memory */
2063*bbb1b6f9SApple OSS Distributions 	char pid_buf[64];
2064*bbb1b6f9SApple OSS Distributions 	snprintf(pid_buf, sizeof(pid_buf), "%d", child_pid);
2065*bbb1b6f9SApple OSS Distributions 	char* gcore_args[] = { "/usr/bin/gcore", pid_buf, NULL};
2066*bbb1b6f9SApple OSS Distributions 	/* Then gcore (and its implicit mach_vm_read()) succeeds */
2067*bbb1b6f9SApple OSS Distributions 	posix_spawn_with_flags_and_assert_successful_exit(gcore_args, POSIX_SPAWN_SECFLAG_EXPLICIT_DISABLE, false, false);
2068*bbb1b6f9SApple OSS Distributions 
2069*bbb1b6f9SApple OSS Distributions 	kill_child(child_pid);
2070*bbb1b6f9SApple OSS Distributions }
2071*bbb1b6f9SApple OSS Distributions 
2072*bbb1b6f9SApple OSS Distributions void
do_local_vm_copyin_with_invalid_tag_test(vm_size_t size)2073*bbb1b6f9SApple OSS Distributions do_local_vm_copyin_with_invalid_tag_test(vm_size_t size)
2074*bbb1b6f9SApple OSS Distributions {
2075*bbb1b6f9SApple OSS Distributions 	T_SETUPBEGIN;
2076*bbb1b6f9SApple OSS Distributions 
2077*bbb1b6f9SApple OSS Distributions 	/* Given an MTE-enabled region */
2078*bbb1b6f9SApple OSS Distributions 	vm_address_t mte_region = 0;
2079*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = vm_allocate(mach_task_self(), &mte_region, size, VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
2080*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(MTE)");
2081*bbb1b6f9SApple OSS Distributions 	memset((void *)mte_region, 0, size);
2082*bbb1b6f9SApple OSS Distributions 
2083*bbb1b6f9SApple OSS Distributions 	/* And an MTE-disabled region */
2084*bbb1b6f9SApple OSS Distributions 	vm_address_t non_mte_region = 0;
2085*bbb1b6f9SApple OSS Distributions 	kr = vm_allocate(mach_task_self(), &non_mte_region, size, VM_FLAGS_ANYWHERE);
2086*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(non-MTE)");
2087*bbb1b6f9SApple OSS Distributions 
2088*bbb1b6f9SApple OSS Distributions 	/* And the MTE region has tag 0x4, but our pointer is incorrectly tagged 0x5 */
2089*bbb1b6f9SApple OSS Distributions 	mte_region |= 0x0400000000000000;
2090*bbb1b6f9SApple OSS Distributions 	__arm_mte_set_tag((void *)mte_region);
2091*bbb1b6f9SApple OSS Distributions 	mte_region |= 0x0500000000000000;
2092*bbb1b6f9SApple OSS Distributions 
2093*bbb1b6f9SApple OSS Distributions 	T_SETUPEND;
2094*bbb1b6f9SApple OSS Distributions 
2095*bbb1b6f9SApple OSS Distributions 	/* When we use `vm_read_overwrite` */
2096*bbb1b6f9SApple OSS Distributions 	/* Then the system terminates us due to our incorrectly tagged request */
2097*bbb1b6f9SApple OSS Distributions 	vm_size_t out_size;
2098*bbb1b6f9SApple OSS Distributions 	vm_read_overwrite(mach_task_self(), mte_region, size, non_mte_region, &out_size);
2099*bbb1b6f9SApple OSS Distributions 	T_FAIL("Expected to be SIGKILLED");
2100*bbb1b6f9SApple OSS Distributions }
2101*bbb1b6f9SApple OSS Distributions 
2102*bbb1b6f9SApple OSS Distributions T_DECL(local_vm_copyin_with_invalid_tag,
2103*bbb1b6f9SApple OSS Distributions     "Verify that copyin of local memory with an invalid tag is denied",
2104*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
2105*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
2106*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
2107*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true))
2108*bbb1b6f9SApple OSS Distributions {
2109*bbb1b6f9SApple OSS Distributions 	/*
2110*bbb1b6f9SApple OSS Distributions 	 * We go down different code paths depending on the size,
2111*bbb1b6f9SApple OSS Distributions 	 * so test both and ensure they're handled consistently.
2112*bbb1b6f9SApple OSS Distributions 	 */
2113*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
2114*bbb1b6f9SApple OSS Distributions 		do_local_vm_copyin_with_invalid_tag_test(PAGE_SIZE);
2115*bbb1b6f9SApple OSS Distributions 	}, "local_vm_copyin(PAGE_SIZE)");
2116*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
2117*bbb1b6f9SApple OSS Distributions 		do_local_vm_copyin_with_invalid_tag_test(PAGE_SIZE * 10);
2118*bbb1b6f9SApple OSS Distributions 	}, "local_vm_copyin(PAGE_SIZE * 10)");
2119*bbb1b6f9SApple OSS Distributions }
2120*bbb1b6f9SApple OSS Distributions 
2121*bbb1b6f9SApple OSS Distributions T_DECL(local_vm_copyin_with_large_non_mte_object_with_adjacent_mte_object,
2122*bbb1b6f9SApple OSS Distributions     "Ensure a large copyin with a non-MTE object and adjacent MTE object fails",
2123*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
2124*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
2125*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
2126*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true))
2127*bbb1b6f9SApple OSS Distributions {
2128*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
2129*bbb1b6f9SApple OSS Distributions 		/* Given a non-MTE-enabled object */
2130*bbb1b6f9SApple OSS Distributions 		vm_address_t non_mte_object_address = 0;
2131*bbb1b6f9SApple OSS Distributions 		vm_size_t non_mte_object_size = PAGE_SIZE;
2132*bbb1b6f9SApple OSS Distributions 		kern_return_t kr = vm_allocate(mach_task_self(), &non_mte_object_address, non_mte_object_size, VM_FLAGS_ANYWHERE);
2133*bbb1b6f9SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(non-MTE)");
2134*bbb1b6f9SApple OSS Distributions 		/* And ensure it's present */
2135*bbb1b6f9SApple OSS Distributions 		memset((void *)non_mte_object_address, 0, non_mte_object_size);
2136*bbb1b6f9SApple OSS Distributions 
2137*bbb1b6f9SApple OSS Distributions 		/* And an adjacent MTE object (which is large enough that the total region will definitely be above `msg_ool_size_small`) */
2138*bbb1b6f9SApple OSS Distributions 		vm_address_t mte_object_address = non_mte_object_address + non_mte_object_size;
2139*bbb1b6f9SApple OSS Distributions 		vm_size_t mte_object_size = PAGE_SIZE * 2;
2140*bbb1b6f9SApple OSS Distributions 		kr = vm_allocate(mach_task_self(), &mte_object_address, mte_object_size, VM_FLAGS_FIXED | VM_FLAGS_MTE);
2141*bbb1b6f9SApple OSS Distributions 		if (kr == KERN_NO_SPACE) {
2142*bbb1b6f9SApple OSS Distributions 		        /*
2143*bbb1b6f9SApple OSS Distributions 		         * Skip gracefully if we fail to grab the VA space we need.
2144*bbb1b6f9SApple OSS Distributions 		         * Note that we send ourselves a SIGKILL so the expect_sigkill() wrapper
2145*bbb1b6f9SApple OSS Distributions 		         * is happy. We can't use T_SKIP or the like because that would elide the
2146*bbb1b6f9SApple OSS Distributions 		         * SIGKILL.
2147*bbb1b6f9SApple OSS Distributions 		         */
2148*bbb1b6f9SApple OSS Distributions 		        T_LOG("Cannot grab required VA space, skipping...");
2149*bbb1b6f9SApple OSS Distributions 		        kill(getpid(), SIGKILL);
2150*bbb1b6f9SApple OSS Distributions 		        return;
2151*bbb1b6f9SApple OSS Distributions 		}
2152*bbb1b6f9SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(adjacent MTE)");
2153*bbb1b6f9SApple OSS Distributions 		/* And ensure it's present */
2154*bbb1b6f9SApple OSS Distributions 		memset((void *)mte_object_address, 0, mte_object_size);
2155*bbb1b6f9SApple OSS Distributions 		/* And the MTE object has a non-zero tag (so we TCF when crossing it) */
2156*bbb1b6f9SApple OSS Distributions 		mte_object_address |= 0x0400000000000000;
2157*bbb1b6f9SApple OSS Distributions 		for (mach_vm_size_t offset = 0; offset < mte_object_size; offset += MTE_GRANULE_SIZE) {
2158*bbb1b6f9SApple OSS Distributions 		        __arm_mte_set_tag(&((uint8_t*)mte_object_address)[offset]);
2159*bbb1b6f9SApple OSS Distributions 		}
2160*bbb1b6f9SApple OSS Distributions 
2161*bbb1b6f9SApple OSS Distributions 		/* When we try to copyin the entire region, spanning both objects */
2162*bbb1b6f9SApple OSS Distributions 		vm_size_t total_region_size = mte_object_size + non_mte_object_size;
2163*bbb1b6f9SApple OSS Distributions 		vm_address_t region_to_overwrite = 0;
2164*bbb1b6f9SApple OSS Distributions 		kr = vm_allocate(mach_task_self(), &region_to_overwrite, total_region_size, VM_FLAGS_ANYWHERE);
2165*bbb1b6f9SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(scribble region)");
2166*bbb1b6f9SApple OSS Distributions 
2167*bbb1b6f9SApple OSS Distributions 		vm_size_t out_size;
2168*bbb1b6f9SApple OSS Distributions 		/* Then we take a TCF during the copyin */
2169*bbb1b6f9SApple OSS Distributions 		vm_read_overwrite(mach_task_self(), non_mte_object_address, total_region_size, region_to_overwrite, &out_size);
2170*bbb1b6f9SApple OSS Distributions 	}, "Trigger a TCF during copyin");
2171*bbb1b6f9SApple OSS Distributions }
2172*bbb1b6f9SApple OSS Distributions 
2173*bbb1b6f9SApple OSS Distributions T_DECL(local_vm_copyin_with_large_mte_object_with_invalid_size,
2174*bbb1b6f9SApple OSS Distributions     "Ensure a large copyin with a non-MTE object but an invalid size fails",
2175*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
2176*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
2177*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
2178*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true))
2179*bbb1b6f9SApple OSS Distributions {
2180*bbb1b6f9SApple OSS Distributions 	/* Given an MTE-enabled object (which is large enough that it exceeds `msg_ool_size_small`) */
2181*bbb1b6f9SApple OSS Distributions 	vm_address_t mte_object_address = 0;
2182*bbb1b6f9SApple OSS Distributions 	vm_size_t mte_object_size = PAGE_SIZE * 3;
2183*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = vm_allocate(mach_task_self(), &mte_object_address, mte_object_size, VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
2184*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(MTE)");
2185*bbb1b6f9SApple OSS Distributions 	/* And ensure it's present */
2186*bbb1b6f9SApple OSS Distributions 	memset((void *)mte_object_address, 0, mte_object_size);
2187*bbb1b6f9SApple OSS Distributions 
2188*bbb1b6f9SApple OSS Distributions 	/* When we try to copyin the region, but specify a size that's too large */
2189*bbb1b6f9SApple OSS Distributions 	/* And we ensure this object is not coalesced with the above object */
2190*bbb1b6f9SApple OSS Distributions 	vm_size_t invalid_size = mte_object_size + PAGE_SIZE * 16;
2191*bbb1b6f9SApple OSS Distributions 	vm_address_t region_to_overwrite = mte_object_address + (PAGE_SIZE * 8);
2192*bbb1b6f9SApple OSS Distributions 	kr = vm_allocate(mach_task_self(), &region_to_overwrite, invalid_size, VM_FLAGS_FIXED);
2193*bbb1b6f9SApple OSS Distributions 	if (kr == KERN_NO_SPACE) {
2194*bbb1b6f9SApple OSS Distributions 		/* Skip gracefully if we fail to grab the VA space we need */
2195*bbb1b6f9SApple OSS Distributions 		T_SKIP("Cannot grab required VA space, skipping...");
2196*bbb1b6f9SApple OSS Distributions 		return;
2197*bbb1b6f9SApple OSS Distributions 	}
2198*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(scribble region)");
2199*bbb1b6f9SApple OSS Distributions 
2200*bbb1b6f9SApple OSS Distributions 	vm_size_t out_size;
2201*bbb1b6f9SApple OSS Distributions 	kr = vm_read_overwrite(mach_task_self(), mte_object_address, invalid_size, region_to_overwrite, &out_size);
2202*bbb1b6f9SApple OSS Distributions 	/* Then it fails */
2203*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_ERROR(kr, KERN_INVALID_ADDRESS, "copyin fails");
2204*bbb1b6f9SApple OSS Distributions }
2205*bbb1b6f9SApple OSS Distributions 
2206*bbb1b6f9SApple OSS Distributions T_DECL(local_vm_copyin_with_large_mte_object_with_hole_in_region,
2207*bbb1b6f9SApple OSS Distributions     "Ensure a large copyin with an MTE object, but with a hole in the middle, is rejected",
2208*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
2209*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
2210*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
2211*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true))
2212*bbb1b6f9SApple OSS Distributions {
2213*bbb1b6f9SApple OSS Distributions 	/* Given an MTE-enabled object (which is large enough that it exceeds `msg_ool_size_small`) */
2214*bbb1b6f9SApple OSS Distributions 	vm_address_t mte_object_address = 0;
2215*bbb1b6f9SApple OSS Distributions 	vm_size_t mte_object_size = PAGE_SIZE * 3;
2216*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = vm_allocate(mach_task_self(), &mte_object_address, mte_object_size, VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
2217*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(MTE)");
2218*bbb1b6f9SApple OSS Distributions 	/* And ensure it's present */
2219*bbb1b6f9SApple OSS Distributions 	memset((void *)mte_object_address, 0, mte_object_size);
2220*bbb1b6f9SApple OSS Distributions 
2221*bbb1b6f9SApple OSS Distributions 	/* And a nearby non-MTE object, but we leave a hole in the middle */
2222*bbb1b6f9SApple OSS Distributions 	vm_size_t padding = PAGE_SIZE;
2223*bbb1b6f9SApple OSS Distributions 	vm_address_t non_mte_object_address = mte_object_address + mte_object_size + padding;
2224*bbb1b6f9SApple OSS Distributions 	vm_size_t non_mte_object_size = PAGE_SIZE;
2225*bbb1b6f9SApple OSS Distributions 	kr = vm_allocate(mach_task_self(), &non_mte_object_address, non_mte_object_size, VM_FLAGS_FIXED);
2226*bbb1b6f9SApple OSS Distributions 	if (kr == KERN_NO_SPACE) {
2227*bbb1b6f9SApple OSS Distributions 		/* Skip gracefully if we fail to grab the VA space we need */
2228*bbb1b6f9SApple OSS Distributions 		T_SKIP("Cannot grab required VA space, skipping...");
2229*bbb1b6f9SApple OSS Distributions 		return;
2230*bbb1b6f9SApple OSS Distributions 	}
2231*bbb1b6f9SApple OSS Distributions 
2232*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(nearby non-MTE)");
2233*bbb1b6f9SApple OSS Distributions 	/* And ensure it's present */
2234*bbb1b6f9SApple OSS Distributions 	memset((void *)non_mte_object_address, 0, non_mte_object_size);
2235*bbb1b6f9SApple OSS Distributions 
2236*bbb1b6f9SApple OSS Distributions 	/* When we try to copyin the whole region, including the hole */
2237*bbb1b6f9SApple OSS Distributions 	vm_size_t region_size = mte_object_size + padding + non_mte_object_size;
2238*bbb1b6f9SApple OSS Distributions 	vm_address_t region_to_overwrite = 0;
2239*bbb1b6f9SApple OSS Distributions 	kr = vm_allocate(mach_task_self(), &region_to_overwrite, region_size, VM_FLAGS_ANYWHERE);
2240*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(scribble region)");
2241*bbb1b6f9SApple OSS Distributions 
2242*bbb1b6f9SApple OSS Distributions 	vm_size_t out_size;
2243*bbb1b6f9SApple OSS Distributions 	kr = vm_read_overwrite(mach_task_self(), mte_object_address, region_size, region_to_overwrite, &out_size);
2244*bbb1b6f9SApple OSS Distributions 	/* Then it fails */
2245*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_ERROR(kr, KERN_INVALID_ADDRESS, "copyin fails");
2246*bbb1b6f9SApple OSS Distributions }
2247*bbb1b6f9SApple OSS Distributions 
2248*bbb1b6f9SApple OSS Distributions T_DECL(local_vm_copyin_with_large_mte_object_with_adjacent_large_mte_object_same_tags,
2249*bbb1b6f9SApple OSS Distributions     "Ensure a large copyin with two MTE objects with the same tag succeeds",
2250*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
2251*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
2252*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
2253*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true))
2254*bbb1b6f9SApple OSS Distributions {
2255*bbb1b6f9SApple OSS Distributions 	/* Given an MTE-enabled object */
2256*bbb1b6f9SApple OSS Distributions 	vm_address_t mte_object1_address = 0;
2257*bbb1b6f9SApple OSS Distributions 	vm_size_t mte_object1_size = PAGE_SIZE;
2258*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = vm_allocate(mach_task_self(), &mte_object1_address, mte_object1_size, VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
2259*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(MTE)");
2260*bbb1b6f9SApple OSS Distributions 	/* And ensure it's present */
2261*bbb1b6f9SApple OSS Distributions 	memset((void *)mte_object1_address, 0, mte_object1_size);
2262*bbb1b6f9SApple OSS Distributions 
2263*bbb1b6f9SApple OSS Distributions 	/* And an adjacent MTE object (which is large enough that the total region will definitely be above `msg_ool_size_small`) */
2264*bbb1b6f9SApple OSS Distributions 	vm_address_t mte_object2_address = mte_object1_address + mte_object1_size;
2265*bbb1b6f9SApple OSS Distributions 	vm_size_t mte_object2_size = PAGE_SIZE * 2;
2266*bbb1b6f9SApple OSS Distributions 	kr = vm_allocate(mach_task_self(), &mte_object2_address, mte_object2_size, VM_FLAGS_FIXED | VM_FLAGS_MTE);
2267*bbb1b6f9SApple OSS Distributions 	if (kr == KERN_NO_SPACE) {
2268*bbb1b6f9SApple OSS Distributions 		/* Skip gracefully if we fail to grab the VA space we need */
2269*bbb1b6f9SApple OSS Distributions 		T_SKIP("Cannot grab required VA space, skipping...");
2270*bbb1b6f9SApple OSS Distributions 		return;
2271*bbb1b6f9SApple OSS Distributions 	}
2272*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(MTE)");
2273*bbb1b6f9SApple OSS Distributions 	/* And ensure it's present */
2274*bbb1b6f9SApple OSS Distributions 	memset((void *)mte_object2_address, 0, mte_object2_size);
2275*bbb1b6f9SApple OSS Distributions 
2276*bbb1b6f9SApple OSS Distributions 	/* And both objects share the same tag */
2277*bbb1b6f9SApple OSS Distributions 	vm_size_t total_region_size = mte_object1_size + mte_object2_size;
2278*bbb1b6f9SApple OSS Distributions 	mte_object1_address |= 0x0400000000000000;
2279*bbb1b6f9SApple OSS Distributions 	for (mach_vm_size_t offset = 0; offset < total_region_size; offset += MTE_GRANULE_SIZE) {
2280*bbb1b6f9SApple OSS Distributions 		__arm_mte_set_tag(&((uint8_t*)mte_object1_address)[offset]);
2281*bbb1b6f9SApple OSS Distributions 	}
2282*bbb1b6f9SApple OSS Distributions 
2283*bbb1b6f9SApple OSS Distributions 	/* When we try to copyin the entire region, spanning both objects */
2284*bbb1b6f9SApple OSS Distributions 	vm_address_t region_to_overwrite = 0;
2285*bbb1b6f9SApple OSS Distributions 	kr = vm_allocate(mach_task_self(), &region_to_overwrite, total_region_size, VM_FLAGS_ANYWHERE);
2286*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(scribble region)");
2287*bbb1b6f9SApple OSS Distributions 
2288*bbb1b6f9SApple OSS Distributions 	vm_size_t out_size;
2289*bbb1b6f9SApple OSS Distributions 	kr = vm_read_overwrite(mach_task_self(), mte_object1_address, total_region_size, region_to_overwrite, &out_size);
2290*bbb1b6f9SApple OSS Distributions 	/* Then it succeeds */
2291*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "copyin");
2292*bbb1b6f9SApple OSS Distributions }
2293*bbb1b6f9SApple OSS Distributions 
2294*bbb1b6f9SApple OSS Distributions T_DECL(local_vm_copyin_with_large_mte_object_with_adjacent_large_mte_object_different_tags,
2295*bbb1b6f9SApple OSS Distributions     "Ensure a large copyin with two MTE objects with a different tag in the second object fails",
2296*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
2297*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
2298*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
2299*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true))
2300*bbb1b6f9SApple OSS Distributions {
2301*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
2302*bbb1b6f9SApple OSS Distributions 		/* Given an MTE-enabled object */
2303*bbb1b6f9SApple OSS Distributions 		vm_address_t mte_object1_address = 0;
2304*bbb1b6f9SApple OSS Distributions 		vm_size_t mte_object1_size = PAGE_SIZE;
2305*bbb1b6f9SApple OSS Distributions 		kern_return_t kr = vm_allocate(mach_task_self(), &mte_object1_address, mte_object1_size, VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
2306*bbb1b6f9SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(MTE)");
2307*bbb1b6f9SApple OSS Distributions 		/* And ensure it's present */
2308*bbb1b6f9SApple OSS Distributions 		memset((void *)mte_object1_address, 0, mte_object1_size);
2309*bbb1b6f9SApple OSS Distributions 
2310*bbb1b6f9SApple OSS Distributions 		/* And an adjacent MTE object (which is large enough that the total region will definitely be above `msg_ool_size_small`) */
2311*bbb1b6f9SApple OSS Distributions 		vm_address_t mte_object2_address = mte_object1_address + mte_object1_size;
2312*bbb1b6f9SApple OSS Distributions 		vm_size_t mte_object2_size = PAGE_SIZE * 2;
2313*bbb1b6f9SApple OSS Distributions 		kr = vm_allocate(mach_task_self(), &mte_object2_address, mte_object2_size, VM_FLAGS_FIXED | VM_FLAGS_MTE);
2314*bbb1b6f9SApple OSS Distributions 		if (kr == KERN_NO_SPACE) {
2315*bbb1b6f9SApple OSS Distributions 		        /*
2316*bbb1b6f9SApple OSS Distributions 		         * Skip gracefully if we fail to grab the VA space we need.
2317*bbb1b6f9SApple OSS Distributions 		         * Note that we send ourselves a SIGKILL so the expect_sigkill() wrapper
2318*bbb1b6f9SApple OSS Distributions 		         * is happy. We can't use T_SKIP or the like because that would elide the
2319*bbb1b6f9SApple OSS Distributions 		         * SIGKILL.
2320*bbb1b6f9SApple OSS Distributions 		         */
2321*bbb1b6f9SApple OSS Distributions 		        T_LOG("Cannot grab required VA space, skipping...");
2322*bbb1b6f9SApple OSS Distributions 		        kill(getpid(), SIGKILL);
2323*bbb1b6f9SApple OSS Distributions 		        return;
2324*bbb1b6f9SApple OSS Distributions 		}
2325*bbb1b6f9SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(adjacent MTE)");
2326*bbb1b6f9SApple OSS Distributions 		/* And ensure it's present */
2327*bbb1b6f9SApple OSS Distributions 		memset((void *)mte_object2_address, 0, mte_object2_size);
2328*bbb1b6f9SApple OSS Distributions 
2329*bbb1b6f9SApple OSS Distributions 		/* And the objects have different tags */
2330*bbb1b6f9SApple OSS Distributions 		mte_object1_address |= 0x0400000000000000;
2331*bbb1b6f9SApple OSS Distributions 		for (mach_vm_size_t offset = 0; offset < mte_object1_size; offset += MTE_GRANULE_SIZE) {
2332*bbb1b6f9SApple OSS Distributions 		        __arm_mte_set_tag(&((uint8_t*)mte_object1_address)[offset]);
2333*bbb1b6f9SApple OSS Distributions 		}
2334*bbb1b6f9SApple OSS Distributions 		mte_object2_address |= 0x0500000000000000;
2335*bbb1b6f9SApple OSS Distributions 		for (mach_vm_size_t offset = 0; offset < mte_object2_size; offset += MTE_GRANULE_SIZE) {
2336*bbb1b6f9SApple OSS Distributions 		        __arm_mte_set_tag(&((uint8_t*)mte_object2_address)[offset]);
2337*bbb1b6f9SApple OSS Distributions 		}
2338*bbb1b6f9SApple OSS Distributions 
2339*bbb1b6f9SApple OSS Distributions 		/* When we try to copyin the entire region, spanning both objects */
2340*bbb1b6f9SApple OSS Distributions 		vm_address_t region_to_overwrite = 0;
2341*bbb1b6f9SApple OSS Distributions 		vm_size_t total_region_size = mte_object1_size + mte_object2_size;
2342*bbb1b6f9SApple OSS Distributions 		kr = vm_allocate(mach_task_self(), &region_to_overwrite, total_region_size, VM_FLAGS_ANYWHERE);
2343*bbb1b6f9SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(scribble region)");
2344*bbb1b6f9SApple OSS Distributions 
2345*bbb1b6f9SApple OSS Distributions 		/* And we use a pointer that only has a valid tag for the first object */
2346*bbb1b6f9SApple OSS Distributions 		/* Then we get a SIGKILL (because we take a TCF) */
2347*bbb1b6f9SApple OSS Distributions 		vm_size_t out_size;
2348*bbb1b6f9SApple OSS Distributions 		vm_read_overwrite(mach_task_self(), mte_object1_address, total_region_size, region_to_overwrite, &out_size);
2349*bbb1b6f9SApple OSS Distributions 	}, "Trigger a TCF during copyin");
2350*bbb1b6f9SApple OSS Distributions }
2351*bbb1b6f9SApple OSS Distributions 
2352*bbb1b6f9SApple OSS Distributions T_DECL(local_vm_copyin_with_large_mte_object_with_adjacent_non_mte_object,
2353*bbb1b6f9SApple OSS Distributions     "Ensure a large copyin with an MTE object and adjacent non-MTE object fails",
2354*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
2355*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
2356*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
2357*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true))
2358*bbb1b6f9SApple OSS Distributions {
2359*bbb1b6f9SApple OSS Distributions 	expect_sigkill(^{
2360*bbb1b6f9SApple OSS Distributions 		/* Given an MTE-enabled object */
2361*bbb1b6f9SApple OSS Distributions 		vm_address_t mte_object_address = 0;
2362*bbb1b6f9SApple OSS Distributions 		vm_size_t mte_object_size = PAGE_SIZE;
2363*bbb1b6f9SApple OSS Distributions 		kern_return_t kr = vm_allocate(mach_task_self(), &mte_object_address, mte_object_size, VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
2364*bbb1b6f9SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(MTE)");
2365*bbb1b6f9SApple OSS Distributions 		/* And ensure it's present */
2366*bbb1b6f9SApple OSS Distributions 		memset((void *)mte_object_address, 0, mte_object_size);
2367*bbb1b6f9SApple OSS Distributions 		/* And the MTE object has a non-zero tag (so we CTCF when crossing to an untagged region) */
2368*bbb1b6f9SApple OSS Distributions 		vm_address_t tagged_mte_object_address = mte_object_address | 0x0400000000000000;
2369*bbb1b6f9SApple OSS Distributions 		for (mach_vm_size_t offset = 0; offset < mte_object_size; offset += MTE_GRANULE_SIZE) {
2370*bbb1b6f9SApple OSS Distributions 		        __arm_mte_set_tag(&((uint8_t*)tagged_mte_object_address)[offset]);
2371*bbb1b6f9SApple OSS Distributions 		}
2372*bbb1b6f9SApple OSS Distributions 
2373*bbb1b6f9SApple OSS Distributions 		/* And an adjacent non-MTE object (which is large enough that the total region will definitely be above `msg_ool_size_small`) */
2374*bbb1b6f9SApple OSS Distributions 		vm_address_t non_mte_object_address = mte_object_address + mte_object_size;
2375*bbb1b6f9SApple OSS Distributions 		vm_size_t non_mte_object_size = PAGE_SIZE * 2;
2376*bbb1b6f9SApple OSS Distributions 		kr = vm_allocate(mach_task_self(), &non_mte_object_address, non_mte_object_size, VM_FLAGS_FIXED);
2377*bbb1b6f9SApple OSS Distributions 		if (kr == KERN_NO_SPACE) {
2378*bbb1b6f9SApple OSS Distributions 		        /*
2379*bbb1b6f9SApple OSS Distributions 		         * Skip gracefully if we fail to grab the VA space we need.
2380*bbb1b6f9SApple OSS Distributions 		         * Note that we send ourselves a SIGKILL so the expect_sigkill() wrapper
2381*bbb1b6f9SApple OSS Distributions 		         * is happy. We can't use T_SKIP or the like because that would elide the
2382*bbb1b6f9SApple OSS Distributions 		         * SIGKILL.
2383*bbb1b6f9SApple OSS Distributions 		         */
2384*bbb1b6f9SApple OSS Distributions 		        T_LOG("Cannot grab required VA space, skipping...");
2385*bbb1b6f9SApple OSS Distributions 		        kill(getpid(), SIGKILL);
2386*bbb1b6f9SApple OSS Distributions 		        return;
2387*bbb1b6f9SApple OSS Distributions 		}
2388*bbb1b6f9SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(adjacent non-MTE)");
2389*bbb1b6f9SApple OSS Distributions 		/* And ensure it's present */
2390*bbb1b6f9SApple OSS Distributions 		memset((void *)non_mte_object_address, 0, non_mte_object_size);
2391*bbb1b6f9SApple OSS Distributions 
2392*bbb1b6f9SApple OSS Distributions 		/* When we try to copyin the entire region, spanning both objects */
2393*bbb1b6f9SApple OSS Distributions 		vm_size_t total_region_size = mte_object_size + non_mte_object_size;
2394*bbb1b6f9SApple OSS Distributions 		vm_address_t region_to_overwrite = 0;
2395*bbb1b6f9SApple OSS Distributions 		kr = vm_allocate(mach_task_self(), &region_to_overwrite, total_region_size, VM_FLAGS_ANYWHERE);
2396*bbb1b6f9SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(scribble region)");
2397*bbb1b6f9SApple OSS Distributions 
2398*bbb1b6f9SApple OSS Distributions 		vm_size_t out_size;
2399*bbb1b6f9SApple OSS Distributions 		vm_read_overwrite(mach_task_self(), mte_object_address, total_region_size, region_to_overwrite, &out_size);
2400*bbb1b6f9SApple OSS Distributions 		/* Then we're killed due to a CTCF */
2401*bbb1b6f9SApple OSS Distributions 	}, "Trigger a CTCF during copyin");
2402*bbb1b6f9SApple OSS Distributions }
2403*bbb1b6f9SApple OSS Distributions 
2404*bbb1b6f9SApple OSS Distributions T_DECL(make_memory_entry_handles_kernel_buffers,
2405*bbb1b6f9SApple OSS Distributions     "Ensure mach_make_memory_entry does not panic when handed an MTE copy",
2406*bbb1b6f9SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64),
2407*bbb1b6f9SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1),
2408*bbb1b6f9SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC,
2409*bbb1b6f9SApple OSS Distributions     T_META_ASROOT(true))
2410*bbb1b6f9SApple OSS Distributions {
2411*bbb1b6f9SApple OSS Distributions 	/* Given an MTE-enabled object */
2412*bbb1b6f9SApple OSS Distributions 	vm_address_t mte_object_address = 0;
2413*bbb1b6f9SApple OSS Distributions 	vm_size_t mte_object_size = PAGE_SIZE;
2414*bbb1b6f9SApple OSS Distributions 	kern_return_t kr = vm_allocate(mach_task_self(), &mte_object_address, mte_object_size, VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
2415*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(MTE)");
2416*bbb1b6f9SApple OSS Distributions 	/* And ensure it's present */
2417*bbb1b6f9SApple OSS Distributions 	memset((void *)mte_object_address, 0, mte_object_size);
2418*bbb1b6f9SApple OSS Distributions 	/* And assign a non-zero tag just for authenticity */
2419*bbb1b6f9SApple OSS Distributions 	vm_address_t tagged_mte_object_address = mte_object_address | 0x0400000000000000;
2420*bbb1b6f9SApple OSS Distributions 	for (mach_vm_size_t offset = 0; offset < mte_object_size; offset += MTE_GRANULE_SIZE) {
2421*bbb1b6f9SApple OSS Distributions 		__arm_mte_set_tag(&((uint8_t*)tagged_mte_object_address)[offset]);
2422*bbb1b6f9SApple OSS Distributions 	}
2423*bbb1b6f9SApple OSS Distributions 
2424*bbb1b6f9SApple OSS Distributions 	/* When I use mach_make_memory_entry_64(MAP_MEM_VM_COPY) */
2425*bbb1b6f9SApple OSS Distributions 	mach_vm_size_t size = mte_object_size;
2426*bbb1b6f9SApple OSS Distributions 	mach_port_t memory_entry_port;
2427*bbb1b6f9SApple OSS Distributions 	kr = mach_make_memory_entry_64(mach_task_self(),
2428*bbb1b6f9SApple OSS Distributions 	    &size,
2429*bbb1b6f9SApple OSS Distributions 	    tagged_mte_object_address,
2430*bbb1b6f9SApple OSS Distributions 	    VM_PROT_DEFAULT | MAP_MEM_VM_COPY | MAP_MEM_USE_DATA_ADDR,
2431*bbb1b6f9SApple OSS Distributions 	    &memory_entry_port, MEMORY_OBJECT_NULL);
2432*bbb1b6f9SApple OSS Distributions 	/* Then the system does not panic... */
2433*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_make_memory_entry_64(MTE object)");
2434*bbb1b6f9SApple OSS Distributions }
2435