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