xref: /xnu-12377.81.4/tests/kernel_inspection.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions #ifdef T_NAMESPACE
2*043036a2SApple OSS Distributions #undef T_NAMESPACE
3*043036a2SApple OSS Distributions #endif
4*043036a2SApple OSS Distributions 
5*043036a2SApple OSS Distributions #include <darwintest.h>
6*043036a2SApple OSS Distributions 
7*043036a2SApple OSS Distributions #include <mach/host_priv.h>
8*043036a2SApple OSS Distributions #include <mach/mach.h>
9*043036a2SApple OSS Distributions #include <mach/mach_types.h>
10*043036a2SApple OSS Distributions #include <mach/mach_vm.h>
11*043036a2SApple OSS Distributions #include <mach/processor_set.h>
12*043036a2SApple OSS Distributions #include <mach/task.h>
13*043036a2SApple OSS Distributions #include <sys/sysctl.h>
14*043036a2SApple OSS Distributions #include <mach_debug/ipc_info.h>
15*043036a2SApple OSS Distributions #include <unistd.h>
16*043036a2SApple OSS Distributions 
17*043036a2SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.ipc"),
18*043036a2SApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("xnu"),
19*043036a2SApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("IPC"),
20*043036a2SApple OSS Distributions     T_META_RUN_CONCURRENTLY(true),
21*043036a2SApple OSS Distributions     T_META_TAG_VM_PREFERRED);
22*043036a2SApple OSS Distributions 
23*043036a2SApple OSS Distributions /*
24*043036a2SApple OSS Distributions  * Attempt to inspect kernel_task using a task_inspect_t.  Interact with the
25*043036a2SApple OSS Distributions  * kernel in the same way top(1) and lsmp(1) do.
26*043036a2SApple OSS Distributions  */
27*043036a2SApple OSS Distributions 
28*043036a2SApple OSS Distributions static int found_kernel_task = 0;
29*043036a2SApple OSS Distributions 
30*043036a2SApple OSS Distributions static void
check_secure_kernel(void)31*043036a2SApple OSS Distributions check_secure_kernel(void)
32*043036a2SApple OSS Distributions {
33*043036a2SApple OSS Distributions 	int secure_kern = 0;
34*043036a2SApple OSS Distributions 	size_t secure_kern_size = sizeof(secure_kern);
35*043036a2SApple OSS Distributions 
36*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.secure_kernel", &secure_kern,
37*043036a2SApple OSS Distributions 	    &secure_kern_size, NULL, 0), NULL);
38*043036a2SApple OSS Distributions 
39*043036a2SApple OSS Distributions 	if (secure_kern) {
40*043036a2SApple OSS Distributions 		T_SKIP("secure kernel: processor_set_tasks will not return kernel_task");
41*043036a2SApple OSS Distributions 	}
42*043036a2SApple OSS Distributions }
43*043036a2SApple OSS Distributions 
44*043036a2SApple OSS Distributions static void
attempt_kernel_inspection(task_t task)45*043036a2SApple OSS Distributions attempt_kernel_inspection(task_t task)
46*043036a2SApple OSS Distributions {
47*043036a2SApple OSS Distributions 	pid_t pid = (pid_t)-1;
48*043036a2SApple OSS Distributions 	mach_msg_type_number_t i, count, thcnt;
49*043036a2SApple OSS Distributions 	struct task_basic_info_64 ti;
50*043036a2SApple OSS Distributions 	thread_act_array_t threads;
51*043036a2SApple OSS Distributions 
52*043036a2SApple OSS Distributions 	if (pid_for_task(task, &pid)) {
53*043036a2SApple OSS Distributions 		return;
54*043036a2SApple OSS Distributions 	}
55*043036a2SApple OSS Distributions 
56*043036a2SApple OSS Distributions 	T_QUIET; T_LOG("Checking pid %d", pid);
57*043036a2SApple OSS Distributions 
58*043036a2SApple OSS Distributions 	if (pid != 0) {
59*043036a2SApple OSS Distributions 		return;
60*043036a2SApple OSS Distributions 	}
61*043036a2SApple OSS Distributions 
62*043036a2SApple OSS Distributions 	T_LOG("found kernel_task, attempting to inspect");
63*043036a2SApple OSS Distributions 	found_kernel_task++;
64*043036a2SApple OSS Distributions 
65*043036a2SApple OSS Distributions 	count = TASK_BASIC_INFO_64_COUNT;
66*043036a2SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(task_info(task, TASK_BASIC_INFO_64, (task_info_t)&ti,
67*043036a2SApple OSS Distributions 	    &count), "task_info(... TASK_BASIC_INFO_64 ...)");
68*043036a2SApple OSS Distributions 
69*043036a2SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(task_threads(task, &threads, &thcnt), "task_threads");
70*043036a2SApple OSS Distributions 	T_LOG("Found %d kernel threads.", thcnt);
71*043036a2SApple OSS Distributions 	for (i = 0; i < thcnt; i++) {
72*043036a2SApple OSS Distributions 		kern_return_t kr;
73*043036a2SApple OSS Distributions 		thread_basic_info_data_t basic_info;
74*043036a2SApple OSS Distributions 		mach_msg_type_number_t bi_count = THREAD_BASIC_INFO_COUNT;
75*043036a2SApple OSS Distributions 
76*043036a2SApple OSS Distributions 		kr = thread_info(threads[i], THREAD_BASIC_INFO,
77*043036a2SApple OSS Distributions 		    (thread_info_t)&basic_info, &bi_count);
78*043036a2SApple OSS Distributions 		/*
79*043036a2SApple OSS Distributions 		 * Ignore threads that have gone away.
80*043036a2SApple OSS Distributions 		 */
81*043036a2SApple OSS Distributions 		if (kr == MACH_SEND_INVALID_DEST) {
82*043036a2SApple OSS Distributions 			T_LOG("ignoring thread that has been destroyed");
83*043036a2SApple OSS Distributions 			continue;
84*043036a2SApple OSS Distributions 		}
85*043036a2SApple OSS Distributions 		T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "thread_info(... THREAD_BASIC_INFO ...)");
86*043036a2SApple OSS Distributions 
87*043036a2SApple OSS Distributions 		/* Now try out READ (skip eval) interfaces on kernel thread */
88*043036a2SApple OSS Distributions 		mach_msg_type_number_t msk_count = EXC_TYPES_COUNT;
89*043036a2SApple OSS Distributions 		exception_mask_t masks[EXC_TYPES_COUNT];
90*043036a2SApple OSS Distributions 		ipc_info_port_t ports_info[EXC_TYPES_COUNT];
91*043036a2SApple OSS Distributions 		exception_behavior_t behaviors[EXC_TYPES_COUNT];
92*043036a2SApple OSS Distributions 		thread_state_flavor_t flavors[EXC_TYPES_COUNT];
93*043036a2SApple OSS Distributions 		kr = thread_get_exception_ports_info(threads[i], EXC_MASK_ALL, masks, &msk_count, ports_info, behaviors, flavors);
94*043036a2SApple OSS Distributions 		T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "thread_get_exception_ports_info() on kernel thread: 0x%x", kr);
95*043036a2SApple OSS Distributions 
96*043036a2SApple OSS Distributions 		/* READ (with eval) interfaces should fail */
97*043036a2SApple OSS Distributions 		mach_port_t voucher;
98*043036a2SApple OSS Distributions 		kr = thread_get_mach_voucher(threads[i], 0, &voucher);
99*043036a2SApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(kr, KERN_INVALID_ARGUMENT, "thread_get_mach_voucher() should fail with KERN_INVALID_ARGUMENT");
100*043036a2SApple OSS Distributions 
101*043036a2SApple OSS Distributions 		(void)mach_port_deallocate(mach_task_self(), threads[i]);
102*043036a2SApple OSS Distributions 	}
103*043036a2SApple OSS Distributions 	mach_vm_deallocate(mach_task_self(),
104*043036a2SApple OSS Distributions 	    (mach_vm_address_t)(uintptr_t)threads,
105*043036a2SApple OSS Distributions 	    thcnt * sizeof(*threads));
106*043036a2SApple OSS Distributions 
107*043036a2SApple OSS Distributions 	ipc_info_space_basic_t basic_info;
108*043036a2SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(mach_port_space_basic_info(task, &basic_info), "mach_port_space_basic_info");
109*043036a2SApple OSS Distributions 
110*043036a2SApple OSS Distributions 	ipc_info_space_t info_space;
111*043036a2SApple OSS Distributions 	ipc_info_name_array_t table;
112*043036a2SApple OSS Distributions 	ipc_info_tree_name_array_t tree;
113*043036a2SApple OSS Distributions 	mach_msg_type_number_t tblcnt = 0, treecnt = 0;
114*043036a2SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(mach_port_space_info(task, &info_space, &table,
115*043036a2SApple OSS Distributions 	    &tblcnt, &tree, &treecnt), "mach_port_space_info");
116*043036a2SApple OSS Distributions 	if (tblcnt > 0) {
117*043036a2SApple OSS Distributions 		mach_vm_deallocate(mach_task_self(),
118*043036a2SApple OSS Distributions 		    (mach_vm_address_t)(uintptr_t)table,
119*043036a2SApple OSS Distributions 		    tblcnt * sizeof(*table));
120*043036a2SApple OSS Distributions 	}
121*043036a2SApple OSS Distributions 	if (treecnt > 0) {
122*043036a2SApple OSS Distributions 		mach_vm_deallocate(mach_task_self(),
123*043036a2SApple OSS Distributions 		    (mach_vm_address_t)(uintptr_t)tree,
124*043036a2SApple OSS Distributions 		    treecnt * sizeof(*tree));
125*043036a2SApple OSS Distributions 	}
126*043036a2SApple OSS Distributions 
127*043036a2SApple OSS Distributions 	/* Now try out READ (skip eval) interfaces on kernel task */
128*043036a2SApple OSS Distributions 	mach_msg_type_number_t msk_count = EXC_TYPES_COUNT;
129*043036a2SApple OSS Distributions 	exception_mask_t masks[EXC_TYPES_COUNT];
130*043036a2SApple OSS Distributions 	ipc_info_port_t ports_info[EXC_TYPES_COUNT];
131*043036a2SApple OSS Distributions 	exception_behavior_t behaviors[EXC_TYPES_COUNT];
132*043036a2SApple OSS Distributions 	thread_state_flavor_t flavors[EXC_TYPES_COUNT];
133*043036a2SApple OSS Distributions 	kern_return_t kr = task_get_exception_ports_info(task, EXC_MASK_ALL, masks, &msk_count, ports_info, behaviors, flavors);
134*043036a2SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "task_get_exception_ports_info() on kernel_task: 0x%x", kr);
135*043036a2SApple OSS Distributions 
136*043036a2SApple OSS Distributions 	/* READ (with eval) interfaces should fail */
137*043036a2SApple OSS Distributions 	vm_offset_t data;
138*043036a2SApple OSS Distributions 	mach_msg_type_number_t cnt;
139*043036a2SApple OSS Distributions 	mach_vm_address_t addr = 0x10000000; /* can be whatever, the call should fail before getting to VM */
140*043036a2SApple OSS Distributions 
141*043036a2SApple OSS Distributions 	kr = mach_vm_read(task, (mach_vm_address_t)addr, 8, &data, &cnt);
142*043036a2SApple OSS Distributions 	T_EXPECT_EQ(kr, KERN_INVALID_ARGUMENT, "mach_vm_read() should fail with KERN_INVALID_ARGUMENT");
143*043036a2SApple OSS Distributions 
144*043036a2SApple OSS Distributions 	mach_port_t voucher;
145*043036a2SApple OSS Distributions 	kr = task_get_mach_voucher(task, 0, &voucher);
146*043036a2SApple OSS Distributions 	T_EXPECT_EQ(kr, KERN_INVALID_TASK, "task_get_mach_voucher() should fail with KERN_INVALID_TASK");
147*043036a2SApple OSS Distributions 
148*043036a2SApple OSS Distributions 	/* Control interfaces should absolutely fail */
149*043036a2SApple OSS Distributions 	kr = task_set_mach_voucher(task, mach_task_self()); /* voucher arg is unused, can be whatever port */
150*043036a2SApple OSS Distributions 	T_EXPECT_EQ(kr, KERN_INVALID_TASK, "task_set_mach_voucher() should fail with KERN_INVALID_TASK");
151*043036a2SApple OSS Distributions }
152*043036a2SApple OSS Distributions 
153*043036a2SApple OSS Distributions T_DECL(inspect_kernel_task,
154*043036a2SApple OSS Distributions     "ensure that kernel task can be inspected",
155*043036a2SApple OSS Distributions     T_META_CHECK_LEAKS(false),
156*043036a2SApple OSS Distributions     T_META_ASROOT(true))
157*043036a2SApple OSS Distributions {
158*043036a2SApple OSS Distributions 	processor_set_name_array_t psets;
159*043036a2SApple OSS Distributions 	processor_set_t pset;
160*043036a2SApple OSS Distributions 	task_array_t tasks;
161*043036a2SApple OSS Distributions 	mach_msg_type_number_t i, j, tcnt, pcnt = 0;
162*043036a2SApple OSS Distributions 	mach_port_t self = mach_host_self();
163*043036a2SApple OSS Distributions 
164*043036a2SApple OSS Distributions 	check_secure_kernel();
165*043036a2SApple OSS Distributions 
166*043036a2SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(host_processor_sets(self, &psets, &pcnt),
167*043036a2SApple OSS Distributions 	    NULL);
168*043036a2SApple OSS Distributions 
169*043036a2SApple OSS Distributions 	for (i = 0; i < pcnt; i++) {
170*043036a2SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(host_processor_set_priv(self, psets[i], &pset), NULL);
171*043036a2SApple OSS Distributions 		T_LOG("Checking pset %d/%d", i, pcnt - 1);
172*043036a2SApple OSS Distributions 
173*043036a2SApple OSS Distributions 		tcnt = 0;
174*043036a2SApple OSS Distributions 		T_LOG("Attempting kernel inspection with control port...");
175*043036a2SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(processor_set_tasks(pset, &tasks, &tcnt), NULL);
176*043036a2SApple OSS Distributions 
177*043036a2SApple OSS Distributions 		for (j = 0; j < tcnt; j++) {
178*043036a2SApple OSS Distributions 			attempt_kernel_inspection(tasks[j]);
179*043036a2SApple OSS Distributions 			mach_port_deallocate(self, tasks[j]);
180*043036a2SApple OSS Distributions 		}
181*043036a2SApple OSS Distributions 
182*043036a2SApple OSS Distributions 		/* free tasks array */
183*043036a2SApple OSS Distributions 		mach_vm_deallocate(mach_task_self(),
184*043036a2SApple OSS Distributions 		    (mach_vm_address_t)(uintptr_t)tasks,
185*043036a2SApple OSS Distributions 		    tcnt * sizeof(*tasks));
186*043036a2SApple OSS Distributions 
187*043036a2SApple OSS Distributions 		T_LOG("Attempting kernel inspection with read port...");
188*043036a2SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(processor_set_tasks_with_flavor(pset, TASK_FLAVOR_READ, &tasks, &tcnt), NULL);
189*043036a2SApple OSS Distributions 
190*043036a2SApple OSS Distributions 		for (j = 0; j < tcnt; j++) {
191*043036a2SApple OSS Distributions 			attempt_kernel_inspection(tasks[j]);
192*043036a2SApple OSS Distributions 			mach_port_deallocate(self, tasks[j]);
193*043036a2SApple OSS Distributions 		}
194*043036a2SApple OSS Distributions 
195*043036a2SApple OSS Distributions 		mach_vm_deallocate(mach_task_self(),
196*043036a2SApple OSS Distributions 		    (mach_vm_address_t)(uintptr_t)tasks,
197*043036a2SApple OSS Distributions 		    tcnt * sizeof(*tasks));
198*043036a2SApple OSS Distributions 
199*043036a2SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), pset);
200*043036a2SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), psets[i]);
201*043036a2SApple OSS Distributions 	}
202*043036a2SApple OSS Distributions 	mach_vm_deallocate(mach_task_self(),
203*043036a2SApple OSS Distributions 	    (mach_vm_address_t)(uintptr_t)psets,
204*043036a2SApple OSS Distributions 	    pcnt * sizeof(*psets));
205*043036a2SApple OSS Distributions 
206*043036a2SApple OSS Distributions 	if (found_kernel_task != 2) {
207*043036a2SApple OSS Distributions 		/* One for kernel control port test, one for kernel read port test. */
208*043036a2SApple OSS Distributions 		T_FAIL("could not find kernel_task in list of tasks returned");
209*043036a2SApple OSS Distributions 	}
210*043036a2SApple OSS Distributions }
211