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