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