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