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