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