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