xref: /xnu-10002.1.13/tests/ipc_read_inspect.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions #include <darwintest.h>
2*1031c584SApple OSS Distributions 
3*1031c584SApple OSS Distributions #include <mach/host_priv.h>
4*1031c584SApple OSS Distributions #include <mach/mach.h>
5*1031c584SApple OSS Distributions #include <mach/mach_types.h>
6*1031c584SApple OSS Distributions #include <mach/mach_vm.h>
7*1031c584SApple OSS Distributions #include <mach_debug/ipc_info.h>
8*1031c584SApple OSS Distributions #include <mach/processor_set.h>
9*1031c584SApple OSS Distributions #include <mach/task.h>
10*1031c584SApple OSS Distributions #include <signal.h>
11*1031c584SApple OSS Distributions #include <sys/wait.h>
12*1031c584SApple OSS Distributions #include <sys/proc.h>
13*1031c584SApple OSS Distributions #include <sys/sysctl.h>
14*1031c584SApple OSS Distributions #include <unistd.h>
15*1031c584SApple OSS Distributions #include <TargetConditionals.h>
16*1031c584SApple OSS Distributions 
17*1031c584SApple OSS Distributions #define IKOT_THREAD_CONTROL             1
18*1031c584SApple OSS Distributions #define IKOT_THREAD_READ                47
19*1031c584SApple OSS Distributions #define IKOT_THREAD_INSPECT             46
20*1031c584SApple OSS Distributions 
21*1031c584SApple OSS Distributions #define IKOT_TASK_CONTROL               2
22*1031c584SApple OSS Distributions #define IKOT_TASK_READ                  45
23*1031c584SApple OSS Distributions #define IKOT_TASK_INSPECT               44
24*1031c584SApple OSS Distributions #define IKOT_TASK_NAME                  20
25*1031c584SApple OSS Distributions 
26*1031c584SApple OSS Distributions 
27*1031c584SApple OSS Distributions /*
28*1031c584SApple OSS Distributions  * This test verifies various security properties for task and thread
29*1031c584SApple OSS Distributions  * read/inspect interfaces. Specifically, it checks and makes sure:
30*1031c584SApple OSS Distributions  *
31*1031c584SApple OSS Distributions  * 1. Task/thread can't get higher priv'ed ports from lower ones through
32*1031c584SApple OSS Distributions  * {task, thread}_get_special_port()
33*1031c584SApple OSS Distributions  * 2. Correct level of thread ports are returned from task_threads() with
34*1031c584SApple OSS Distributions  * a given task port flavor
35*1031c584SApple OSS Distributions  * 3. Correct level of task ports are returned from processor_set_tasks()
36*1031c584SApple OSS Distributions  * 4. MIG intrans conversion and enforcement for task/thread port does not break.
37*1031c584SApple OSS Distributions  * 5. task_{, read, inspect, name}_for_pid() works for self and other process
38*1031c584SApple OSS Distributions  * 6. The new mach_vm_remap_new interface behaves correctly
39*1031c584SApple OSS Distributions  */
40*1031c584SApple OSS Distributions 
41*1031c584SApple OSS Distributions T_GLOBAL_META(
42*1031c584SApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
43*1031c584SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
44*1031c584SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"),
45*1031c584SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(TRUE));
46*1031c584SApple OSS Distributions 
47*1031c584SApple OSS Distributions static void
RESULT_CHECK(kern_return_t kr,unsigned int flavor,unsigned int required,char * f_name)48*1031c584SApple OSS Distributions RESULT_CHECK(
49*1031c584SApple OSS Distributions 	kern_return_t kr,
50*1031c584SApple OSS Distributions 	unsigned int flavor,  /* task_flavor_t or thread_flavor_t */
51*1031c584SApple OSS Distributions 	unsigned int required, /* task_flavor_t or thread_flavor_t */
52*1031c584SApple OSS Distributions 	char *f_name)
53*1031c584SApple OSS Distributions {
54*1031c584SApple OSS Distributions 	if (flavor <= required) {
55*1031c584SApple OSS Distributions 		T_EXPECT_EQ(kr, KERN_SUCCESS, "%s should succeed with task/thread flavor %d, kr: 0x%x", f_name, flavor, kr);
56*1031c584SApple OSS Distributions 	} else {
57*1031c584SApple OSS Distributions 		T_EXPECT_NE(kr, KERN_SUCCESS, "%s should fail with task/thread flavor %d, kr: 0x%x", f_name, flavor, kr);
58*1031c584SApple OSS Distributions 	}
59*1031c584SApple OSS Distributions }
60*1031c584SApple OSS Distributions 
61*1031c584SApple OSS Distributions static void
test_task_get_special_port(task_t tport,task_flavor_t flavor)62*1031c584SApple OSS Distributions test_task_get_special_port(
63*1031c584SApple OSS Distributions 	task_t  tport,
64*1031c584SApple OSS Distributions 	task_flavor_t flavor)
65*1031c584SApple OSS Distributions {
66*1031c584SApple OSS Distributions 	kern_return_t kr;
67*1031c584SApple OSS Distributions 	mach_port_t special_port = MACH_PORT_NULL;
68*1031c584SApple OSS Distributions 	mach_port_t tfp_port = MACH_PORT_NULL;
69*1031c584SApple OSS Distributions 
70*1031c584SApple OSS Distributions 	T_LOG("Testing task_get_special_port() with task flavor %d", flavor);
71*1031c584SApple OSS Distributions 	/* gettable with at least control port */
72*1031c584SApple OSS Distributions 	kr = task_get_special_port(tport, TASK_KERNEL_PORT, &special_port);
73*1031c584SApple OSS Distributions 	RESULT_CHECK(kr, flavor, TASK_FLAVOR_CONTROL, "task_get_special_port(TASK_KERNEL_PORT)");
74*1031c584SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), special_port);
75*1031c584SApple OSS Distributions 	special_port = MACH_PORT_NULL;
76*1031c584SApple OSS Distributions 
77*1031c584SApple OSS Distributions 	kr = task_get_special_port(tport, TASK_BOOTSTRAP_PORT, &special_port);
78*1031c584SApple OSS Distributions 	RESULT_CHECK(kr, flavor, TASK_FLAVOR_CONTROL, "task_get_special_port(TASK_BOOTSTRAP_PORT)");
79*1031c584SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), special_port);
80*1031c584SApple OSS Distributions 	special_port = MACH_PORT_NULL;
81*1031c584SApple OSS Distributions 
82*1031c584SApple OSS Distributions 	kr = task_get_special_port(tport, TASK_HOST_PORT, &special_port);
83*1031c584SApple OSS Distributions 	RESULT_CHECK(kr, flavor, TASK_FLAVOR_CONTROL, "task_get_special_port(TASK_HOST_PORT)");
84*1031c584SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), special_port);
85*1031c584SApple OSS Distributions 	special_port = MACH_PORT_NULL;
86*1031c584SApple OSS Distributions 
87*1031c584SApple OSS Distributions 	/* gettable with at least read port */
88*1031c584SApple OSS Distributions 	kr = task_get_special_port(tport, TASK_READ_PORT, &special_port);
89*1031c584SApple OSS Distributions 	RESULT_CHECK(kr, flavor, TASK_FLAVOR_READ, "task_get_special_port(TASK_READ_PORT)");
90*1031c584SApple OSS Distributions 	if (KERN_SUCCESS == kr) {
91*1031c584SApple OSS Distributions 		kr = task_read_for_pid(mach_task_self(), getpid(), &tfp_port);
92*1031c584SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_read_for_pid()");
93*1031c584SApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(tfp_port, special_port, "task_read_for_pid() should match TASK_READ_PORT");
94*1031c584SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), tfp_port);
95*1031c584SApple OSS Distributions 	}
96*1031c584SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), special_port);
97*1031c584SApple OSS Distributions 	special_port = MACH_PORT_NULL;
98*1031c584SApple OSS Distributions 
99*1031c584SApple OSS Distributions 	/* gettable with at least inspect port */
100*1031c584SApple OSS Distributions 	kr = task_get_special_port(tport, TASK_INSPECT_PORT, &special_port);
101*1031c584SApple OSS Distributions 	RESULT_CHECK(kr, flavor, TASK_FLAVOR_INSPECT, "task_get_special_port(TASK_INSPECT_PORT)");
102*1031c584SApple OSS Distributions 	if (KERN_SUCCESS == kr) {
103*1031c584SApple OSS Distributions 		kr = task_inspect_for_pid(mach_task_self(), getpid(), &tfp_port);
104*1031c584SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_inspect_for_pid()");
105*1031c584SApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(tfp_port, special_port, "task_inspect_for_pid() should match TASK_INSPECT_PORT");
106*1031c584SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), tfp_port);
107*1031c584SApple OSS Distributions 	}
108*1031c584SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), special_port);
109*1031c584SApple OSS Distributions 	special_port = MACH_PORT_NULL;
110*1031c584SApple OSS Distributions 
111*1031c584SApple OSS Distributions 	/* gettable with at least name port */
112*1031c584SApple OSS Distributions 	kr = task_get_special_port(tport, TASK_NAME_PORT, &special_port);
113*1031c584SApple OSS Distributions 	RESULT_CHECK(kr, flavor, TASK_FLAVOR_INSPECT, "task_get_special_port(TASK_NAME_PORT)");
114*1031c584SApple OSS Distributions 	if (KERN_SUCCESS == kr) {
115*1031c584SApple OSS Distributions 		kr = task_name_for_pid(mach_task_self(), getpid(), &tfp_port);
116*1031c584SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_name_for_pid()");
117*1031c584SApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(tfp_port, special_port, "task_name_for_pid() should match TASK_NAME_PORT");
118*1031c584SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), tfp_port);
119*1031c584SApple OSS Distributions 	}
120*1031c584SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), special_port);
121*1031c584SApple OSS Distributions 	special_port = MACH_PORT_NULL;
122*1031c584SApple OSS Distributions }
123*1031c584SApple OSS Distributions 
124*1031c584SApple OSS Distributions static void
test_thread_get_special_port(thread_t tport,thread_flavor_t flavor)125*1031c584SApple OSS Distributions test_thread_get_special_port(
126*1031c584SApple OSS Distributions 	thread_t  tport,
127*1031c584SApple OSS Distributions 	thread_flavor_t flavor)
128*1031c584SApple OSS Distributions {
129*1031c584SApple OSS Distributions 	kern_return_t kr;
130*1031c584SApple OSS Distributions 	mach_port_t special_port = MACH_PORT_NULL;
131*1031c584SApple OSS Distributions 
132*1031c584SApple OSS Distributions 	T_LOG("Testing thread_get_special_port() with thread flavor %d", flavor);
133*1031c584SApple OSS Distributions 	/* gettable with at least control port */
134*1031c584SApple OSS Distributions 	kr = thread_get_special_port(tport, THREAD_KERNEL_PORT, &special_port);
135*1031c584SApple OSS Distributions 	RESULT_CHECK(kr, flavor, THREAD_FLAVOR_CONTROL, "thread_get_special_port(THREAD_KERNEL_PORT)");
136*1031c584SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), special_port);
137*1031c584SApple OSS Distributions 	special_port = MACH_PORT_NULL;
138*1031c584SApple OSS Distributions 
139*1031c584SApple OSS Distributions 	/* gettable with at least read port */
140*1031c584SApple OSS Distributions 	kr = thread_get_special_port(tport, THREAD_READ_PORT, &special_port);
141*1031c584SApple OSS Distributions 	RESULT_CHECK(kr, flavor, THREAD_FLAVOR_READ, "thread_get_special_port(THREAD_READ_PORT)");
142*1031c584SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), special_port);
143*1031c584SApple OSS Distributions 	special_port = MACH_PORT_NULL;
144*1031c584SApple OSS Distributions 
145*1031c584SApple OSS Distributions 	/* gettable with at least inspect port */
146*1031c584SApple OSS Distributions 	kr = thread_get_special_port(tport, THREAD_INSPECT_PORT, &special_port);
147*1031c584SApple OSS Distributions 	RESULT_CHECK(kr, flavor, THREAD_FLAVOR_INSPECT, "thread_get_special_port(THREAD_INSPECT_PORT)");
148*1031c584SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), special_port);
149*1031c584SApple OSS Distributions 	special_port = MACH_PORT_NULL;
150*1031c584SApple OSS Distributions }
151*1031c584SApple OSS Distributions 
152*1031c584SApple OSS Distributions static void
test_task_threads(task_t tport,task_flavor_t flavor)153*1031c584SApple OSS Distributions test_task_threads(
154*1031c584SApple OSS Distributions 	task_t  tport,
155*1031c584SApple OSS Distributions 	task_flavor_t flavor)
156*1031c584SApple OSS Distributions {
157*1031c584SApple OSS Distributions 	kern_return_t kr;
158*1031c584SApple OSS Distributions 	thread_array_t threadList;
159*1031c584SApple OSS Distributions 	mach_msg_type_number_t threadCount = 0;
160*1031c584SApple OSS Distributions 
161*1031c584SApple OSS Distributions 	unsigned int kotype;
162*1031c584SApple OSS Distributions 	unsigned int kaddr;
163*1031c584SApple OSS Distributions 
164*1031c584SApple OSS Distributions 	T_LOG("Testing task_threads() with task flavor %d", flavor);
165*1031c584SApple OSS Distributions 
166*1031c584SApple OSS Distributions 	kr = task_threads(tport, &threadList, &threadCount);
167*1031c584SApple OSS Distributions 	RESULT_CHECK(kr, flavor, TASK_FLAVOR_INSPECT, "task_threads");
168*1031c584SApple OSS Distributions 
169*1031c584SApple OSS Distributions 	if (kr) {
170*1031c584SApple OSS Distributions 		T_LOG("task_threads failed, skipping test_task_threads()");
171*1031c584SApple OSS Distributions 		return;
172*1031c584SApple OSS Distributions 	}
173*1031c584SApple OSS Distributions 
174*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_GE(threadCount, 1, "threadCount should be at least 1");
175*1031c584SApple OSS Distributions 
176*1031c584SApple OSS Distributions 	/*
177*1031c584SApple OSS Distributions 	 * TASK_FLAVOR_CONTROL -> THREAD_FLAVOR_CONTROL
178*1031c584SApple OSS Distributions 	 * TASK_FLAVOR_READ    -> THREAD_FLAVOR_READ
179*1031c584SApple OSS Distributions 	 * TASK_FLAVOR_INSPECT -> THREAD_FLAVOR_INSPECT
180*1031c584SApple OSS Distributions 	 * TASK_FLAOVR_NAME    -> KERN_FAILURE
181*1031c584SApple OSS Distributions 	 */
182*1031c584SApple OSS Distributions 	for (size_t i = 0; i < threadCount; i++) {
183*1031c584SApple OSS Distributions 		kr = mach_port_kernel_object(mach_task_self(), threadList[i], &kotype, &kaddr);
184*1031c584SApple OSS Distributions 		if (kr == KERN_INVALID_RIGHT || kr == KERN_INVALID_NAME) {
185*1031c584SApple OSS Distributions 			/* thread port is inactive */
186*1031c584SApple OSS Distributions 			T_LOG("thread port name 0x%x is inactive", threadList[i]);
187*1031c584SApple OSS Distributions 			continue;
188*1031c584SApple OSS Distributions 		} else if (kr) {
189*1031c584SApple OSS Distributions 			T_FAIL("mach_port_kernel_object() failed with kr: 0x%x", kr);
190*1031c584SApple OSS Distributions 		}
191*1031c584SApple OSS Distributions 		switch (flavor) {
192*1031c584SApple OSS Distributions 		case TASK_FLAVOR_CONTROL:
193*1031c584SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(kotype, IKOT_THREAD_CONTROL, "Task control port should yield thread control port");
194*1031c584SApple OSS Distributions 			break;
195*1031c584SApple OSS Distributions 		case TASK_FLAVOR_READ:
196*1031c584SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(kotype, IKOT_THREAD_READ, "Task read port should yield thread read port");
197*1031c584SApple OSS Distributions 			break;
198*1031c584SApple OSS Distributions 		case TASK_FLAVOR_INSPECT:
199*1031c584SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(kotype, IKOT_THREAD_INSPECT, "Task inspect port should yield thread inspect port");
200*1031c584SApple OSS Distributions 			break;
201*1031c584SApple OSS Distributions 		default:
202*1031c584SApple OSS Distributions 			T_FAIL("task_threads() returned thread ports with task name port??");
203*1031c584SApple OSS Distributions 			break;
204*1031c584SApple OSS Distributions 		}
205*1031c584SApple OSS Distributions 	}
206*1031c584SApple OSS Distributions 
207*1031c584SApple OSS Distributions 	for (size_t i = 0; i < threadCount; i++) {
208*1031c584SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), threadList[i]);
209*1031c584SApple OSS Distributions 	}
210*1031c584SApple OSS Distributions 	vm_deallocate(mach_task_self(), (vm_address_t)threadList,
211*1031c584SApple OSS Distributions 	    sizeof(threadList[0]) * threadCount);
212*1031c584SApple OSS Distributions }
213*1031c584SApple OSS Distributions 
214*1031c584SApple OSS Distributions static void
test_processor_set_tasks(task_flavor_t flavor)215*1031c584SApple OSS Distributions test_processor_set_tasks(
216*1031c584SApple OSS Distributions 	task_flavor_t flavor)
217*1031c584SApple OSS Distributions {
218*1031c584SApple OSS Distributions 	kern_return_t kr;
219*1031c584SApple OSS Distributions 	processor_set_name_array_t psets;
220*1031c584SApple OSS Distributions 	processor_set_t        pset_priv;
221*1031c584SApple OSS Distributions 	task_array_t taskList;
222*1031c584SApple OSS Distributions 	mach_msg_type_number_t pcnt = 0, tcnt = 0;
223*1031c584SApple OSS Distributions 	mach_port_t host = mach_host_self();
224*1031c584SApple OSS Distributions 
225*1031c584SApple OSS Distributions 	unsigned int kotype;
226*1031c584SApple OSS Distributions 	unsigned int kaddr;
227*1031c584SApple OSS Distributions 
228*1031c584SApple OSS Distributions 	T_LOG("Testing processor_set_tasks() with task flavor %d", flavor);
229*1031c584SApple OSS Distributions 
230*1031c584SApple OSS Distributions 	kr = host_processor_sets(host, &psets, &pcnt);
231*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "host_processor_sets");
232*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_GE(pcnt, 1, "should have at least 1 processor set");
233*1031c584SApple OSS Distributions 
234*1031c584SApple OSS Distributions 	kr = host_processor_set_priv(host, psets[0], &pset_priv);
235*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "host_processor_set_priv");
236*1031c584SApple OSS Distributions 	for (size_t i = 0; i < pcnt; i++) {
237*1031c584SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), psets[i]);
238*1031c584SApple OSS Distributions 	}
239*1031c584SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), host);
240*1031c584SApple OSS Distributions 
241*1031c584SApple OSS Distributions 	kr = processor_set_tasks_with_flavor(pset_priv, flavor, &taskList, &tcnt);
242*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "processor_set_tasks_with_flavor");
243*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_GE(tcnt, 1, "should have at least 1 task");
244*1031c584SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), pset_priv);
245*1031c584SApple OSS Distributions 
246*1031c584SApple OSS Distributions 	for (size_t i = 0; i < tcnt; i++) {
247*1031c584SApple OSS Distributions 		kr = mach_port_kernel_object(mach_task_self(), taskList[i], &kotype, &kaddr);
248*1031c584SApple OSS Distributions 		if (kr == KERN_INVALID_RIGHT || kr == KERN_INVALID_NAME) {
249*1031c584SApple OSS Distributions 			/* task port is inactive */
250*1031c584SApple OSS Distributions 			T_LOG("task port name 0x%x is inactive", taskList[i]);
251*1031c584SApple OSS Distributions 			continue;
252*1031c584SApple OSS Distributions 		} else if (kr) {
253*1031c584SApple OSS Distributions 			T_FAIL("mach_port_kernel_object() failed with kr: 0x%x", kr);
254*1031c584SApple OSS Distributions 		}
255*1031c584SApple OSS Distributions 		switch (flavor) {
256*1031c584SApple OSS Distributions 		case TASK_FLAVOR_CONTROL:
257*1031c584SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(kotype, IKOT_TASK_CONTROL, "TASK_FLAVOR_CONTROL should yield control ports");
258*1031c584SApple OSS Distributions 			break;
259*1031c584SApple OSS Distributions 		case TASK_FLAVOR_READ:
260*1031c584SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(kotype, IKOT_TASK_READ, "TASK_FLAVOR_READ should yield read ports");
261*1031c584SApple OSS Distributions 			break;
262*1031c584SApple OSS Distributions 		case TASK_FLAVOR_INSPECT:
263*1031c584SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(kotype, IKOT_TASK_INSPECT, "TASK_FLAVOR_INSPECT should yield inspect ports");
264*1031c584SApple OSS Distributions 			break;
265*1031c584SApple OSS Distributions 		case TASK_FLAVOR_NAME:
266*1031c584SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(kotype, IKOT_TASK_NAME, "TASK_FLAVOR_NAME should yield name ports");
267*1031c584SApple OSS Distributions 			break;
268*1031c584SApple OSS Distributions 		default:
269*1031c584SApple OSS Distributions 			T_FAIL("strange flavor");
270*1031c584SApple OSS Distributions 			break;
271*1031c584SApple OSS Distributions 		}
272*1031c584SApple OSS Distributions 	}
273*1031c584SApple OSS Distributions 
274*1031c584SApple OSS Distributions 	for (size_t i = 0; i < tcnt; i++) {
275*1031c584SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), taskList[i]);
276*1031c584SApple OSS Distributions 	}
277*1031c584SApple OSS Distributions }
278*1031c584SApple OSS Distributions 
279*1031c584SApple OSS Distributions static void
test_task_port_mig_intrans(task_t tport,task_flavor_t flavor)280*1031c584SApple OSS Distributions test_task_port_mig_intrans(
281*1031c584SApple OSS Distributions 	task_t  tport,
282*1031c584SApple OSS Distributions 	task_flavor_t   flavor)
283*1031c584SApple OSS Distributions {
284*1031c584SApple OSS Distributions 	kern_return_t kr;
285*1031c584SApple OSS Distributions 
286*1031c584SApple OSS Distributions 	T_LOG("Testing various MIG/manual intrans task interfaces with task flavor %d", flavor);
287*1031c584SApple OSS Distributions 
288*1031c584SApple OSS Distributions 	{
289*1031c584SApple OSS Distributions 		/* 1. Test some control port interfaces */
290*1031c584SApple OSS Distributions 		int data = 0x41;
291*1031c584SApple OSS Distributions 		int new_value = 0x42;
292*1031c584SApple OSS Distributions 		kr = mach_vm_write(tport,
293*1031c584SApple OSS Distributions 		    (mach_vm_address_t)&data,
294*1031c584SApple OSS Distributions 		    (vm_offset_t)&new_value,
295*1031c584SApple OSS Distributions 		    (mach_msg_type_number_t)sizeof(int));
296*1031c584SApple OSS Distributions 		RESULT_CHECK(kr, flavor, TASK_FLAVOR_CONTROL, "mach_vm_write");
297*1031c584SApple OSS Distributions 
298*1031c584SApple OSS Distributions 		/* mach_vm_remap_new with max_protection VM_PROT_WRITE | VM_PROT_READ */
299*1031c584SApple OSS Distributions 		int *localAddress = 0;
300*1031c584SApple OSS Distributions 		mach_vm_address_t localMachVMAddress = 0;
301*1031c584SApple OSS Distributions 		vm_prot_t cur_protection = VM_PROT_WRITE | VM_PROT_READ;
302*1031c584SApple OSS Distributions 		vm_prot_t max_protection = VM_PROT_WRITE | VM_PROT_READ;
303*1031c584SApple OSS Distributions 		/* rdar://67706101 (mach_vm_remap flag that allows restricting protection of remapped region) */
304*1031c584SApple OSS Distributions 		kr = mach_vm_remap_new(mach_task_self(),
305*1031c584SApple OSS Distributions 		    &localMachVMAddress,
306*1031c584SApple OSS Distributions 		    sizeof(int),
307*1031c584SApple OSS Distributions 		    0,
308*1031c584SApple OSS Distributions 		    VM_FLAGS_ANYWHERE,
309*1031c584SApple OSS Distributions 		    tport, /* remote task, use self task port */
310*1031c584SApple OSS Distributions 		    (mach_vm_address_t)&data,
311*1031c584SApple OSS Distributions 		    false,
312*1031c584SApple OSS Distributions 		    &cur_protection,
313*1031c584SApple OSS Distributions 		    &max_protection,
314*1031c584SApple OSS Distributions 		    VM_INHERIT_NONE);
315*1031c584SApple OSS Distributions 		localAddress = (int *)(uintptr_t)localMachVMAddress;
316*1031c584SApple OSS Distributions 
317*1031c584SApple OSS Distributions 		RESULT_CHECK(kr, flavor, TASK_FLAVOR_CONTROL, "mach_vm_remap_new - VM_PROT_WRITE");
318*1031c584SApple OSS Distributions 		if (KERN_SUCCESS == kr) {
319*1031c584SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(max_protection, VM_PROT_READ | VM_PROT_WRITE, NULL);
320*1031c584SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(cur_protection, VM_PROT_READ | VM_PROT_WRITE, NULL);
321*1031c584SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(*localAddress, data, NULL); /* read */
322*1031c584SApple OSS Distributions 			*localAddress = 0; /* write */
323*1031c584SApple OSS Distributions 		}
324*1031c584SApple OSS Distributions 
325*1031c584SApple OSS Distributions 		exception_mask_t masks[EXC_TYPES_COUNT] = {};
326*1031c584SApple OSS Distributions 		mach_msg_type_number_t nmasks = 0;
327*1031c584SApple OSS Distributions 		exception_port_t ports[EXC_TYPES_COUNT] = {};
328*1031c584SApple OSS Distributions 		exception_behavior_t behaviors[EXC_TYPES_COUNT] = {};
329*1031c584SApple OSS Distributions 		thread_state_flavor_t flavors[EXC_TYPES_COUNT] = {};
330*1031c584SApple OSS Distributions 		kr = task_get_exception_ports(tport, EXC_MASK_ALL,
331*1031c584SApple OSS Distributions 		    masks, &nmasks, ports, behaviors, flavors);
332*1031c584SApple OSS Distributions 		RESULT_CHECK(kr, flavor, TASK_FLAVOR_CONTROL, "task_get_exception_ports");
333*1031c584SApple OSS Distributions 		for (size_t i = 0; i < EXC_TYPES_COUNT; i++) {
334*1031c584SApple OSS Distributions 			mach_port_deallocate(mach_task_self(), ports[i]);
335*1031c584SApple OSS Distributions 		}
336*1031c584SApple OSS Distributions 	}
337*1031c584SApple OSS Distributions 
338*1031c584SApple OSS Distributions 	{
339*1031c584SApple OSS Distributions 		/* 2. Test some read port interfaces */
340*1031c584SApple OSS Distributions 		vm_offset_t read_value = 0;
341*1031c584SApple OSS Distributions 		mach_msg_type_number_t read_cnt = 0;
342*1031c584SApple OSS Distributions 		int data = 0x41;
343*1031c584SApple OSS Distributions 		kr = mach_vm_read(tport,
344*1031c584SApple OSS Distributions 		    (mach_vm_address_t)&data,
345*1031c584SApple OSS Distributions 		    (mach_msg_type_number_t)sizeof(int),
346*1031c584SApple OSS Distributions 		    &read_value,
347*1031c584SApple OSS Distributions 		    &read_cnt);
348*1031c584SApple OSS Distributions 		RESULT_CHECK(kr, flavor, TASK_FLAVOR_READ, "mach_vm_read");
349*1031c584SApple OSS Distributions 
350*1031c584SApple OSS Distributions 		/* mach_vm_remap_new with max_protection VM_PROT_READ */
351*1031c584SApple OSS Distributions 		int *localAddress = 0;
352*1031c584SApple OSS Distributions 		mach_vm_address_t localMachVMAddress = 0;
353*1031c584SApple OSS Distributions 		vm_prot_t cur_protection = VM_PROT_READ;
354*1031c584SApple OSS Distributions 		vm_prot_t max_protection = VM_PROT_READ;
355*1031c584SApple OSS Distributions 		/* rdar://67706101 (mach_vm_remap flag that allows restricting protection of remapped region) */
356*1031c584SApple OSS Distributions 		kr = mach_vm_remap_new(mach_task_self(),
357*1031c584SApple OSS Distributions 		    &localMachVMAddress,
358*1031c584SApple OSS Distributions 		    sizeof(int),
359*1031c584SApple OSS Distributions 		    0,
360*1031c584SApple OSS Distributions 		    VM_FLAGS_ANYWHERE,
361*1031c584SApple OSS Distributions 		    tport, /* remote task, use self task port */
362*1031c584SApple OSS Distributions 		    (mach_vm_address_t)&data,
363*1031c584SApple OSS Distributions 		    false,
364*1031c584SApple OSS Distributions 		    &cur_protection,
365*1031c584SApple OSS Distributions 		    &max_protection,
366*1031c584SApple OSS Distributions 		    VM_INHERIT_NONE);
367*1031c584SApple OSS Distributions 		localAddress = (int *)(uintptr_t)localMachVMAddress;
368*1031c584SApple OSS Distributions 
369*1031c584SApple OSS Distributions 		RESULT_CHECK(kr, flavor, TASK_FLAVOR_READ, "mach_vm_remap_new - VM_PROT_READ");
370*1031c584SApple OSS Distributions 		if (KERN_SUCCESS == kr) {
371*1031c584SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(max_protection, VM_PROT_READ, NULL);
372*1031c584SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(cur_protection, VM_PROT_READ, NULL);
373*1031c584SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(*localAddress, data, NULL); /* read */
374*1031c584SApple OSS Distributions 		}
375*1031c584SApple OSS Distributions 
376*1031c584SApple OSS Distributions 		/* mach_vm_remap_new with copy == TRUE */
377*1031c584SApple OSS Distributions 		int data2 = 0x42;
378*1031c584SApple OSS Distributions 		localAddress = 0;
379*1031c584SApple OSS Distributions 		localMachVMAddress = 0;
380*1031c584SApple OSS Distributions 		cur_protection = VM_PROT_WRITE | VM_PROT_READ;
381*1031c584SApple OSS Distributions 		max_protection = VM_PROT_WRITE | VM_PROT_READ;
382*1031c584SApple OSS Distributions 
383*1031c584SApple OSS Distributions 		kr = mach_vm_remap_new(mach_task_self(),
384*1031c584SApple OSS Distributions 		    &localMachVMAddress,
385*1031c584SApple OSS Distributions 		    sizeof(int),
386*1031c584SApple OSS Distributions 		    0,
387*1031c584SApple OSS Distributions 		    VM_FLAGS_ANYWHERE,
388*1031c584SApple OSS Distributions 		    tport, /* remote task, use self task port */
389*1031c584SApple OSS Distributions 		    (mach_vm_address_t)&data2,
390*1031c584SApple OSS Distributions 		    true,
391*1031c584SApple OSS Distributions 		    &cur_protection,
392*1031c584SApple OSS Distributions 		    &max_protection,
393*1031c584SApple OSS Distributions 		    VM_INHERIT_NONE);
394*1031c584SApple OSS Distributions 		localAddress = (int *)(uintptr_t)localMachVMAddress;
395*1031c584SApple OSS Distributions 
396*1031c584SApple OSS Distributions 		RESULT_CHECK(kr, flavor, TASK_FLAVOR_READ, "mach_vm_remap_new - copy==TRUE");
397*1031c584SApple OSS Distributions 		if (KERN_SUCCESS == kr) {
398*1031c584SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(max_protection, VM_PROT_READ | VM_PROT_WRITE, NULL);
399*1031c584SApple OSS Distributions 			T_QUIET; T_EXPECT_EQ(cur_protection, VM_PROT_READ | VM_PROT_WRITE, NULL);
400*1031c584SApple OSS Distributions 			/* Following is causing bus error tracked by rdar://71616700 (Unexpected BUS ERROR in mach_vm_remap_new()) */
401*1031c584SApple OSS Distributions 			// T_QUIET; T_EXPECT_EQ(*localAddress, data2, NULL); /* read */
402*1031c584SApple OSS Distributions 			// *localAddress = 0; /* write */
403*1031c584SApple OSS Distributions 		}
404*1031c584SApple OSS Distributions 
405*1031c584SApple OSS Distributions 		/* */
406*1031c584SApple OSS Distributions 		mach_port_t voucher = MACH_PORT_NULL;
407*1031c584SApple OSS Distributions 		kr = task_get_mach_voucher(tport, 0, &voucher);
408*1031c584SApple OSS Distributions 		RESULT_CHECK(kr, flavor, TASK_FLAVOR_READ, "task_get_mach_voucher");
409*1031c584SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), voucher);
410*1031c584SApple OSS Distributions 
411*1031c584SApple OSS Distributions 		/* */
412*1031c584SApple OSS Distributions 		ipc_info_space_t space_info;
413*1031c584SApple OSS Distributions 		ipc_info_name_array_t table;
414*1031c584SApple OSS Distributions 		mach_msg_type_number_t tableCount;
415*1031c584SApple OSS Distributions 		ipc_info_tree_name_array_t tree; /* unused */
416*1031c584SApple OSS Distributions 		mach_msg_type_number_t treeCount; /* unused */
417*1031c584SApple OSS Distributions 		kr = mach_port_space_info(tport, &space_info, &table, &tableCount, &tree, &treeCount);
418*1031c584SApple OSS Distributions 		RESULT_CHECK(kr, flavor, TASK_FLAVOR_READ, "mach_port_space_info");
419*1031c584SApple OSS Distributions 	}
420*1031c584SApple OSS Distributions 
421*1031c584SApple OSS Distributions 	{
422*1031c584SApple OSS Distributions 		/* 3. Test some inspect port interfaces */
423*1031c584SApple OSS Distributions 		task_exc_guard_behavior_t exc_behavior;
424*1031c584SApple OSS Distributions 		kr = task_get_exc_guard_behavior(tport, &exc_behavior);
425*1031c584SApple OSS Distributions 		RESULT_CHECK(kr, flavor, TASK_FLAVOR_INSPECT, "task_get_exc_guard_behavior");
426*1031c584SApple OSS Distributions 	}
427*1031c584SApple OSS Distributions 
428*1031c584SApple OSS Distributions 	{
429*1031c584SApple OSS Distributions 		/* 4. Test some name port interfaces */
430*1031c584SApple OSS Distributions 		struct task_basic_info info;
431*1031c584SApple OSS Distributions 		mach_msg_type_number_t size = sizeof(info);
432*1031c584SApple OSS Distributions 		kr = task_info(tport,
433*1031c584SApple OSS Distributions 		    TASK_BASIC_INFO,
434*1031c584SApple OSS Distributions 		    (task_info_t)&info,
435*1031c584SApple OSS Distributions 		    &size);
436*1031c584SApple OSS Distributions 		RESULT_CHECK(kr, flavor, TASK_FLAVOR_NAME, "task_info");
437*1031c584SApple OSS Distributions 	}
438*1031c584SApple OSS Distributions }
439*1031c584SApple OSS Distributions 
440*1031c584SApple OSS Distributions static void
test_thread_port_mig_intrans(thread_t tport,thread_flavor_t flavor)441*1031c584SApple OSS Distributions test_thread_port_mig_intrans(
442*1031c584SApple OSS Distributions 	thread_t  tport,
443*1031c584SApple OSS Distributions 	thread_flavor_t   flavor)
444*1031c584SApple OSS Distributions {
445*1031c584SApple OSS Distributions 	kern_return_t kr;
446*1031c584SApple OSS Distributions 
447*1031c584SApple OSS Distributions 	T_LOG("Testing various MIG/manual intrans thread interfaces with thread flavor %d", flavor);
448*1031c584SApple OSS Distributions 
449*1031c584SApple OSS Distributions 	{
450*1031c584SApple OSS Distributions 		/* 1. Test some control port interfaces */
451*1031c584SApple OSS Distributions 		exception_mask_t masks[EXC_TYPES_COUNT] = {};
452*1031c584SApple OSS Distributions 		mach_msg_type_number_t nmasks = 0;
453*1031c584SApple OSS Distributions 		exception_port_t ports[EXC_TYPES_COUNT] = {};
454*1031c584SApple OSS Distributions 		exception_behavior_t behaviors[EXC_TYPES_COUNT] = {};;
455*1031c584SApple OSS Distributions 		thread_state_flavor_t flavors[EXC_TYPES_COUNT] = {};;
456*1031c584SApple OSS Distributions 		kr = thread_get_exception_ports(tport, EXC_MASK_ALL,
457*1031c584SApple OSS Distributions 		    masks, &nmasks, ports, behaviors, flavors);
458*1031c584SApple OSS Distributions 		RESULT_CHECK(kr, flavor, THREAD_FLAVOR_CONTROL, "thread_get_exception_ports");
459*1031c584SApple OSS Distributions 		for (size_t i = 0; i < EXC_TYPES_COUNT; i++) {
460*1031c584SApple OSS Distributions 			mach_port_deallocate(mach_task_self(), ports[i]);
461*1031c584SApple OSS Distributions 		}
462*1031c584SApple OSS Distributions 	}
463*1031c584SApple OSS Distributions 
464*1031c584SApple OSS Distributions 	{
465*1031c584SApple OSS Distributions 		/* 2. Test some read port interfaces */
466*1031c584SApple OSS Distributions 		mach_voucher_t voucher = MACH_PORT_NULL;
467*1031c584SApple OSS Distributions 		kr = thread_get_mach_voucher(tport, 0, &voucher);
468*1031c584SApple OSS Distributions 		RESULT_CHECK(kr, flavor, THREAD_FLAVOR_READ, "thread_get_mach_voucher");
469*1031c584SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), voucher);
470*1031c584SApple OSS Distributions 	}
471*1031c584SApple OSS Distributions 
472*1031c584SApple OSS Distributions 	{
473*1031c584SApple OSS Distributions 		/* 3. Test some inspect port interfaces */
474*1031c584SApple OSS Distributions 		thread_qos_policy_t info;
475*1031c584SApple OSS Distributions 		mach_msg_type_number_t count = THREAD_QOS_POLICY_COUNT;
476*1031c584SApple OSS Distributions 		boolean_t get_default = FALSE;
477*1031c584SApple OSS Distributions 
478*1031c584SApple OSS Distributions 		processor_set_name_t name = MACH_PORT_NULL;
479*1031c584SApple OSS Distributions 		kr = thread_policy_get(tport, THREAD_QOS_POLICY,
480*1031c584SApple OSS Distributions 		    (thread_policy_t)&info, &count, &get_default);
481*1031c584SApple OSS Distributions 		RESULT_CHECK(kr, flavor, THREAD_FLAVOR_INSPECT, "thread_policy_get");
482*1031c584SApple OSS Distributions 	}
483*1031c584SApple OSS Distributions }
484*1031c584SApple OSS Distributions 
485*1031c584SApple OSS Distributions static void
test_get_child_task_port(void)486*1031c584SApple OSS Distributions test_get_child_task_port(void)
487*1031c584SApple OSS Distributions {
488*1031c584SApple OSS Distributions 	pid_t child_pid;
489*1031c584SApple OSS Distributions 	kern_return_t kr;
490*1031c584SApple OSS Distributions 	mach_port_name_t tr, ti, tp, tn;
491*1031c584SApple OSS Distributions 
492*1031c584SApple OSS Distributions 	child_pid = fork();
493*1031c584SApple OSS Distributions 
494*1031c584SApple OSS Distributions 	T_LOG("Testing get child task ports");
495*1031c584SApple OSS Distributions 
496*1031c584SApple OSS Distributions 	if (child_pid < 0) {
497*1031c584SApple OSS Distributions 		T_FAIL("fork failed in test_get_child_port.");
498*1031c584SApple OSS Distributions 	}
499*1031c584SApple OSS Distributions 
500*1031c584SApple OSS Distributions 	if (child_pid == 0) {
501*1031c584SApple OSS Distributions 		/* hang the child */
502*1031c584SApple OSS Distributions 		while (1) {
503*1031c584SApple OSS Distributions 			sleep(10);
504*1031c584SApple OSS Distributions 		}
505*1031c584SApple OSS Distributions 	}
506*1031c584SApple OSS Distributions 
507*1031c584SApple OSS Distributions 	kr = task_for_pid(mach_task_self(), child_pid, &tp);
508*1031c584SApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "task_for_pid for child %u", child_pid);
509*1031c584SApple OSS Distributions 
510*1031c584SApple OSS Distributions 	kr = task_read_for_pid(mach_task_self(), child_pid, &tr);
511*1031c584SApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "task_read_for_pid for child %u", child_pid);
512*1031c584SApple OSS Distributions 
513*1031c584SApple OSS Distributions 	kr = task_inspect_for_pid(mach_task_self(), child_pid, &ti);
514*1031c584SApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "task_inspect_for_pid for child %u", child_pid);
515*1031c584SApple OSS Distributions 
516*1031c584SApple OSS Distributions 	kr = task_name_for_pid(mach_task_self(), child_pid, &tn);
517*1031c584SApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "task_name_for_pid for child %u", child_pid);
518*1031c584SApple OSS Distributions 
519*1031c584SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), tp);
520*1031c584SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), tr);
521*1031c584SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), ti);
522*1031c584SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), tn);
523*1031c584SApple OSS Distributions 
524*1031c584SApple OSS Distributions 	kill(child_pid, SIGKILL);
525*1031c584SApple OSS Distributions 	int status;
526*1031c584SApple OSS Distributions 	wait(&status);
527*1031c584SApple OSS Distributions }
528*1031c584SApple OSS Distributions 
529*1031c584SApple OSS Distributions T_DECL(read_inspect, "Test critical read and inspect port interfaces")
530*1031c584SApple OSS Distributions {
531*1031c584SApple OSS Distributions 	mach_port_t control_port, movable_port, read_port, inspect_port, name_port;
532*1031c584SApple OSS Distributions 	mach_port_t th_control_port, th_movable_port, th_read_port, th_inspect_port;
533*1031c584SApple OSS Distributions #define TASK_PORT_COUNT 5
534*1031c584SApple OSS Distributions #define THREAD_PORT_COUNT 4
535*1031c584SApple OSS Distributions 	mach_port_t task_ports[TASK_PORT_COUNT];
536*1031c584SApple OSS Distributions 	task_flavor_t task_flavors[TASK_PORT_COUNT];
537*1031c584SApple OSS Distributions 	mach_port_t thread_ports[THREAD_PORT_COUNT];
538*1031c584SApple OSS Distributions 	thread_flavor_t thread_flavors[THREAD_PORT_COUNT];
539*1031c584SApple OSS Distributions 	kern_return_t kr;
540*1031c584SApple OSS Distributions 
541*1031c584SApple OSS Distributions 	/* first, try getting all flavors of task port for self */
542*1031c584SApple OSS Distributions 	kr = task_for_pid(mach_task_self(), getpid(), &control_port);
543*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_for_pid()");
544*1031c584SApple OSS Distributions 	task_ports[0] = control_port;
545*1031c584SApple OSS Distributions 	task_flavors[0] = TASK_FLAVOR_CONTROL;
546*1031c584SApple OSS Distributions 
547*1031c584SApple OSS Distributions 	kr = task_get_special_port(mach_task_self(), TASK_KERNEL_PORT, &movable_port);
548*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_get_special_port(..TASK_KERNEL_PORT..)");
549*1031c584SApple OSS Distributions 	task_ports[1] = movable_port;
550*1031c584SApple OSS Distributions 	task_flavors[1] = TASK_FLAVOR_CONTROL;
551*1031c584SApple OSS Distributions 
552*1031c584SApple OSS Distributions 	kr = task_read_for_pid(mach_task_self(), getpid(), &read_port);
553*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_read_for_pid()");
554*1031c584SApple OSS Distributions 	task_ports[2] = read_port;
555*1031c584SApple OSS Distributions 	task_flavors[2] = TASK_FLAVOR_READ;
556*1031c584SApple OSS Distributions 
557*1031c584SApple OSS Distributions 	kr = task_inspect_for_pid(mach_task_self(), getpid(), &inspect_port);
558*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_inspect_for_pid()");
559*1031c584SApple OSS Distributions 	task_ports[3] = inspect_port;
560*1031c584SApple OSS Distributions 	task_flavors[3] = TASK_FLAVOR_INSPECT;
561*1031c584SApple OSS Distributions 
562*1031c584SApple OSS Distributions 	kr = task_name_for_pid(mach_task_self(), getpid(), &name_port);
563*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_name_for_pid()");
564*1031c584SApple OSS Distributions 	task_ports[4] = name_port;
565*1031c584SApple OSS Distributions 	task_flavors[4] = TASK_FLAVOR_NAME;
566*1031c584SApple OSS Distributions 
567*1031c584SApple OSS Distributions 
568*1031c584SApple OSS Distributions 	for (size_t i = 0; i < TASK_PORT_COUNT; i++) {
569*1031c584SApple OSS Distributions 		/*
570*1031c584SApple OSS Distributions 		 * 1. Make sure can't get higher priv'ed ports from lower ones through
571*1031c584SApple OSS Distributions 		 * task_get_special_port()
572*1031c584SApple OSS Distributions 		 */
573*1031c584SApple OSS Distributions 		test_task_get_special_port(task_ports[i], task_flavors[i]);
574*1031c584SApple OSS Distributions 
575*1031c584SApple OSS Distributions 		/*
576*1031c584SApple OSS Distributions 		 * 2. Make sure correct level of thread ports are returned from task_threads
577*1031c584SApple OSS Distributions 		 */
578*1031c584SApple OSS Distributions 		test_task_threads(task_ports[i], task_flavors[i]);
579*1031c584SApple OSS Distributions 
580*1031c584SApple OSS Distributions 		/*
581*1031c584SApple OSS Distributions 		 * 3. Make sure correct level of task ports are returned from processor_set_tasks
582*1031c584SApple OSS Distributions 		 */
583*1031c584SApple OSS Distributions 		if (i >= 1) {
584*1031c584SApple OSS Distributions 			test_processor_set_tasks(task_flavors[i]);
585*1031c584SApple OSS Distributions 		}
586*1031c584SApple OSS Distributions 
587*1031c584SApple OSS Distributions 		/*
588*1031c584SApple OSS Distributions 		 * 4. Make sure our MIG intrans enforcement for tasks does not break.
589*1031c584SApple OSS Distributions 		 */
590*1031c584SApple OSS Distributions 		test_task_port_mig_intrans(task_ports[i], task_flavors[i]);
591*1031c584SApple OSS Distributions 	}
592*1031c584SApple OSS Distributions 
593*1031c584SApple OSS Distributions 
594*1031c584SApple OSS Distributions 	for (size_t i = 0; i < TASK_PORT_COUNT; i++) {
595*1031c584SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), task_ports[i]);
596*1031c584SApple OSS Distributions 	}
597*1031c584SApple OSS Distributions 
598*1031c584SApple OSS Distributions 	/* 4. Try spawning a child an get its task ports */
599*1031c584SApple OSS Distributions 	test_get_child_task_port();
600*1031c584SApple OSS Distributions 
601*1031c584SApple OSS Distributions 	/* Now, test thread read/inspect ports */
602*1031c584SApple OSS Distributions 	th_control_port = mach_thread_self();
603*1031c584SApple OSS Distributions 	thread_ports[0] = th_control_port;
604*1031c584SApple OSS Distributions 	thread_flavors[0] = THREAD_FLAVOR_CONTROL;
605*1031c584SApple OSS Distributions 
606*1031c584SApple OSS Distributions 	kr = thread_get_special_port(th_control_port, THREAD_KERNEL_PORT, &th_movable_port);
607*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_get_special_port(..THREAD_KERNEL_PORT..)");
608*1031c584SApple OSS Distributions 	thread_ports[1] = th_movable_port;
609*1031c584SApple OSS Distributions 	thread_flavors[1] = THREAD_FLAVOR_CONTROL;
610*1031c584SApple OSS Distributions 
611*1031c584SApple OSS Distributions 	kr = thread_get_special_port(th_control_port, THREAD_READ_PORT, &th_read_port);
612*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_get_special_port(..THREAD_READ_PORT..)");
613*1031c584SApple OSS Distributions 	thread_ports[2] = th_read_port;
614*1031c584SApple OSS Distributions 	thread_flavors[2] = THREAD_FLAVOR_READ;
615*1031c584SApple OSS Distributions 
616*1031c584SApple OSS Distributions 	kr = thread_get_special_port(th_control_port, THREAD_INSPECT_PORT, &th_inspect_port);
617*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_get_special_port(..THREAD_INSPECT_PORT..)");
618*1031c584SApple OSS Distributions 	thread_ports[3] = th_inspect_port;
619*1031c584SApple OSS Distributions 	thread_flavors[3] = THREAD_FLAVOR_INSPECT;
620*1031c584SApple OSS Distributions 
621*1031c584SApple OSS Distributions 
622*1031c584SApple OSS Distributions 	for (size_t i = 0; i < THREAD_PORT_COUNT; i++) {
623*1031c584SApple OSS Distributions 		/*
624*1031c584SApple OSS Distributions 		 * 1. Make sure can't get higher priv'ed ports from lower ones through
625*1031c584SApple OSS Distributions 		 * thread_get_special_port()
626*1031c584SApple OSS Distributions 		 */
627*1031c584SApple OSS Distributions 		test_thread_get_special_port(thread_ports[i], thread_flavors[i]);
628*1031c584SApple OSS Distributions 
629*1031c584SApple OSS Distributions 		/*
630*1031c584SApple OSS Distributions 		 * 2. Make sure our MIG intrans enforcement for threads does not break.
631*1031c584SApple OSS Distributions 		 */
632*1031c584SApple OSS Distributions 		test_thread_port_mig_intrans(thread_ports[i], thread_flavors[i]);
633*1031c584SApple OSS Distributions 	}
634*1031c584SApple OSS Distributions 
635*1031c584SApple OSS Distributions 	for (size_t i = 0; i < THREAD_PORT_COUNT; i++) {
636*1031c584SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), thread_ports[i]);
637*1031c584SApple OSS Distributions 	}
638*1031c584SApple OSS Distributions }
639