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