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