1*2c2f96dcSApple OSS Distributions #include <stdio.h>
2*2c2f96dcSApple OSS Distributions #include <stdlib.h>
3*2c2f96dcSApple OSS Distributions #include <unistd.h>
4*2c2f96dcSApple OSS Distributions #include <darwintest.h>
5*2c2f96dcSApple OSS Distributions #include <mach/mach.h>
6*2c2f96dcSApple OSS Distributions #include <mach/mach_vm.h>
7*2c2f96dcSApple OSS Distributions #include <sys/sysctl.h>
8*2c2f96dcSApple OSS Distributions #include <spawn.h>
9*2c2f96dcSApple OSS Distributions #include <signal.h>
10*2c2f96dcSApple OSS Distributions #import <System/sys/codesign.h>
11*2c2f96dcSApple OSS Distributions
12*2c2f96dcSApple OSS Distributions #define IKOT_TASK_CONTROL 2
13*2c2f96dcSApple OSS Distributions
14*2c2f96dcSApple OSS Distributions T_GLOBAL_META(
15*2c2f96dcSApple OSS Distributions T_META_NAMESPACE("xnu.ipc"),
16*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
17*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("IPC"),
18*2c2f96dcSApple OSS Distributions T_META_RUN_CONCURRENTLY(TRUE));
19*2c2f96dcSApple OSS Distributions
20*2c2f96dcSApple OSS Distributions static void
test_extract_immovable_task_port(pid_t pid)21*2c2f96dcSApple OSS Distributions test_extract_immovable_task_port(pid_t pid)
22*2c2f96dcSApple OSS Distributions {
23*2c2f96dcSApple OSS Distributions kern_return_t kr;
24*2c2f96dcSApple OSS Distributions mach_port_t tport = MACH_PORT_NULL;
25*2c2f96dcSApple OSS Distributions ipc_info_space_t space_info;
26*2c2f96dcSApple OSS Distributions ipc_info_name_array_t table;
27*2c2f96dcSApple OSS Distributions mach_msg_type_number_t tableCount;
28*2c2f96dcSApple OSS Distributions ipc_info_tree_name_array_t tree; /* unused */
29*2c2f96dcSApple OSS Distributions mach_msg_type_number_t treeCount; /* unused */
30*2c2f96dcSApple OSS Distributions
31*2c2f96dcSApple OSS Distributions mach_port_t extracted;
32*2c2f96dcSApple OSS Distributions mach_msg_type_name_t right;
33*2c2f96dcSApple OSS Distributions
34*2c2f96dcSApple OSS Distributions
35*2c2f96dcSApple OSS Distributions kr = task_for_pid(mach_task_self(), pid, &tport);
36*2c2f96dcSApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "task_for_pid(), tport: 0x%x", tport);
37*2c2f96dcSApple OSS Distributions
38*2c2f96dcSApple OSS Distributions T_LOG("Target pid: %d", pid);
39*2c2f96dcSApple OSS Distributions
40*2c2f96dcSApple OSS Distributions if (pid == getpid()) {
41*2c2f96dcSApple OSS Distributions /* self extraction should succeed */
42*2c2f96dcSApple OSS Distributions kr = mach_port_extract_right(mach_task_self(), mach_task_self(), MACH_MSG_TYPE_COPY_SEND, &extracted, &right);
43*2c2f96dcSApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "mach_port_extract_right() on immovable port in current space should succeed");
44*2c2f96dcSApple OSS Distributions } else {
45*2c2f96dcSApple OSS Distributions unsigned int kotype = 0, kobject = 0;
46*2c2f96dcSApple OSS Distributions mach_port_name_t tport_name = MACH_PORT_NULL;
47*2c2f96dcSApple OSS Distributions int tport_idx = 0;
48*2c2f96dcSApple OSS Distributions kr = mach_port_space_info(tport, &space_info, &table, &tableCount, &tree, &treeCount);
49*2c2f96dcSApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "mach_port_space_info()");
50*2c2f96dcSApple OSS Distributions
51*2c2f96dcSApple OSS Distributions for (int i = 0; i < tableCount; i++) {
52*2c2f96dcSApple OSS Distributions T_LOG("Searching for task port..name: 0x%x", table[i].iin_name);
53*2c2f96dcSApple OSS Distributions kr = mach_port_kernel_object(tport, table[i].iin_name, &kotype, &kobject);
54*2c2f96dcSApple OSS Distributions if (KERN_SUCCESS == kr && kotype == IKOT_TASK_CONTROL) {
55*2c2f96dcSApple OSS Distributions tport_name = table[i].iin_name;
56*2c2f96dcSApple OSS Distributions tport_idx = i;
57*2c2f96dcSApple OSS Distributions break;
58*2c2f96dcSApple OSS Distributions } else if (kr) {
59*2c2f96dcSApple OSS Distributions T_LOG("mach_port_kernel_object() failed on name 0x%x, kr: 0x%x", table[i].iin_name, kr);
60*2c2f96dcSApple OSS Distributions }
61*2c2f96dcSApple OSS Distributions }
62*2c2f96dcSApple OSS Distributions
63*2c2f96dcSApple OSS Distributions if (!tport_name) {
64*2c2f96dcSApple OSS Distributions T_FAIL("Did not find task port in child's space");
65*2c2f96dcSApple OSS Distributions }
66*2c2f96dcSApple OSS Distributions T_LOG("Remote tport name: 0x%x", tport_name);
67*2c2f96dcSApple OSS Distributions kr = mach_port_extract_right(tport, tport_name, MACH_MSG_TYPE_COPY_SEND, &extracted, &right);
68*2c2f96dcSApple OSS Distributions T_EXPECT_EQ(kr, KERN_INVALID_CAPABILITY, "mach_port_extract_right() on immovable port in child's space should fail (no crash): 0x%x", kr);
69*2c2f96dcSApple OSS Distributions
70*2c2f96dcSApple OSS Distributions T_LOG("Still alive after extract right..");
71*2c2f96dcSApple OSS Distributions
72*2c2f96dcSApple OSS Distributions kr = mach_port_mod_refs(tport, tport_name, MACH_PORT_RIGHT_SEND, -table[tport_idx].iin_urefs);
73*2c2f96dcSApple OSS Distributions T_EXPECT_EQ(kr, KERN_INVALID_CAPABILITY, "mach_port_mod_refs() on pinned port in child's space should fail (no crash): 0x%x", kr);
74*2c2f96dcSApple OSS Distributions
75*2c2f96dcSApple OSS Distributions T_LOG("Still alive after deallocate..");
76*2c2f96dcSApple OSS Distributions }
77*2c2f96dcSApple OSS Distributions }
78*2c2f96dcSApple OSS Distributions
79*2c2f96dcSApple OSS Distributions T_DECL(extract_right_soft_fail, "Immovable/pinned violation on foreign task's space should not crash caller",
80*2c2f96dcSApple OSS Distributions T_META_CHECK_LEAKS(false))
81*2c2f96dcSApple OSS Distributions {
82*2c2f96dcSApple OSS Distributions uint32_t opts = 0;
83*2c2f96dcSApple OSS Distributions size_t size = sizeof(&opts);
84*2c2f96dcSApple OSS Distributions pid_t child_pid;
85*2c2f96dcSApple OSS Distributions kern_return_t ret;
86*2c2f96dcSApple OSS Distributions int status, fd[2], fd2[2];
87*2c2f96dcSApple OSS Distributions
88*2c2f96dcSApple OSS Distributions T_LOG("Check if immovable control port has been enabled\n");
89*2c2f96dcSApple OSS Distributions ret = sysctlbyname("kern.ipc_control_port_options", &opts, &size, NULL, 0);
90*2c2f96dcSApple OSS Distributions
91*2c2f96dcSApple OSS Distributions if (!ret && (opts & 0x08) == 0) {
92*2c2f96dcSApple OSS Distributions T_SKIP("1p immovable control port hard enforcement isn't enabled");
93*2c2f96dcSApple OSS Distributions }
94*2c2f96dcSApple OSS Distributions
95*2c2f96dcSApple OSS Distributions /* extracting mach_task_self() should succeed */
96*2c2f96dcSApple OSS Distributions test_extract_immovable_task_port(getpid());
97*2c2f96dcSApple OSS Distributions
98*2c2f96dcSApple OSS Distributions ret = pipe(fd);
99*2c2f96dcSApple OSS Distributions T_EXPECT_NE(ret, -1, "pipe creation");
100*2c2f96dcSApple OSS Distributions
101*2c2f96dcSApple OSS Distributions ret = pipe(fd2);
102*2c2f96dcSApple OSS Distributions T_EXPECT_NE(ret, -1, "pipe creation2");
103*2c2f96dcSApple OSS Distributions
104*2c2f96dcSApple OSS Distributions child_pid = fork();
105*2c2f96dcSApple OSS Distributions
106*2c2f96dcSApple OSS Distributions if (child_pid < 0) {
107*2c2f96dcSApple OSS Distributions T_FAIL("fork failed()");
108*2c2f96dcSApple OSS Distributions }
109*2c2f96dcSApple OSS Distributions
110*2c2f96dcSApple OSS Distributions if (child_pid == 0) {
111*2c2f96dcSApple OSS Distributions char data[6];
112*2c2f96dcSApple OSS Distributions close(fd[0]);
113*2c2f96dcSApple OSS Distributions close(fd2[1]);
114*2c2f96dcSApple OSS Distributions write(fd[1], "wakeup", 6); /* Sync point 1 */
115*2c2f96dcSApple OSS Distributions close(fd[1]);
116*2c2f96dcSApple OSS Distributions
117*2c2f96dcSApple OSS Distributions read(fd2[0], data, 6); /* Sync point 2 */
118*2c2f96dcSApple OSS Distributions close(fd2[0]);
119*2c2f96dcSApple OSS Distributions } else {
120*2c2f96dcSApple OSS Distributions char data[6];
121*2c2f96dcSApple OSS Distributions close(fd[1]);
122*2c2f96dcSApple OSS Distributions close(fd2[0]);
123*2c2f96dcSApple OSS Distributions read(fd[0], data, 6); /* Sync point 1 */
124*2c2f96dcSApple OSS Distributions close(fd[0]);
125*2c2f96dcSApple OSS Distributions
126*2c2f96dcSApple OSS Distributions /* extracting child's immovable task port should fail without crash */
127*2c2f96dcSApple OSS Distributions test_extract_immovable_task_port(child_pid);
128*2c2f96dcSApple OSS Distributions
129*2c2f96dcSApple OSS Distributions write(fd2[1], "wakeup", 6); /* Sync point 2 */
130*2c2f96dcSApple OSS Distributions close(fd2[1]);
131*2c2f96dcSApple OSS Distributions
132*2c2f96dcSApple OSS Distributions kill(child_pid, SIGKILL);
133*2c2f96dcSApple OSS Distributions wait(&status);
134*2c2f96dcSApple OSS Distributions }
135*2c2f96dcSApple OSS Distributions }
136