xref: /xnu-8796.141.3/tests/mach_service_port.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <darwintest.h>
2*1b191cb5SApple OSS Distributions #include <servers/bootstrap.h>
3*1b191cb5SApple OSS Distributions #include <mach/mach.h>
4*1b191cb5SApple OSS Distributions #include <mach/message.h>
5*1b191cb5SApple OSS Distributions #include <stdlib.h>
6*1b191cb5SApple OSS Distributions #include <sys/sysctl.h>
7*1b191cb5SApple OSS Distributions #include <unistd.h>
8*1b191cb5SApple OSS Distributions #include <mach/port.h>
9*1b191cb5SApple OSS Distributions #include <mach/mach_port.h>
10*1b191cb5SApple OSS Distributions #include <stdint.h>
11*1b191cb5SApple OSS Distributions #include <stdio.h>
12*1b191cb5SApple OSS Distributions #include <unistd.h>
13*1b191cb5SApple OSS Distributions #include <pthread.h>
14*1b191cb5SApple OSS Distributions #include <err.h>
15*1b191cb5SApple OSS Distributions #include <sysexits.h>
16*1b191cb5SApple OSS Distributions 
17*1b191cb5SApple OSS Distributions #include "notifyServer.h"
18*1b191cb5SApple OSS Distributions 
19*1b191cb5SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true),
20*1b191cb5SApple OSS Distributions     T_META_NAMESPACE("xnu.ipc"),
21*1b191cb5SApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("xnu"),
22*1b191cb5SApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("IPC"));
23*1b191cb5SApple OSS Distributions 
24*1b191cb5SApple OSS Distributions static mach_port_t service_port = MACH_PORT_NULL;
25*1b191cb5SApple OSS Distributions 
26*1b191cb5SApple OSS Distributions #define SP_CONTEXT (0x1803)
27*1b191cb5SApple OSS Distributions #define NEW_SP_CONTEXT (0x0318)
28*1b191cb5SApple OSS Distributions #define SERVICE_NAME "com.apple.testservice"
29*1b191cb5SApple OSS Distributions #define SERVICE_DOMAIN (1)
30*1b191cb5SApple OSS Distributions 
31*1b191cb5SApple OSS Distributions T_DECL(mach_service_port, "Create a port with a service port label", T_META_CHECK_LEAKS(false)) {
32*1b191cb5SApple OSS Distributions 	mach_port_t connection_port;
33*1b191cb5SApple OSS Distributions 	mach_port_t notify_port;
34*1b191cb5SApple OSS Distributions 	mach_port_t previous;
35*1b191cb5SApple OSS Distributions 	uint64_t fpid = 0;
36*1b191cb5SApple OSS Distributions 
37*1b191cb5SApple OSS Distributions 	struct mach_service_port_info sp_info = {};
38*1b191cb5SApple OSS Distributions 
39*1b191cb5SApple OSS Distributions 	strcpy(sp_info.mspi_string_name, SERVICE_NAME);
40*1b191cb5SApple OSS Distributions 	sp_info.mspi_domain_type = (uint8_t)SERVICE_DOMAIN;
41*1b191cb5SApple OSS Distributions 	kern_return_t kr;
42*1b191cb5SApple OSS Distributions 
43*1b191cb5SApple OSS Distributions 	mach_port_options_t opts = {
44*1b191cb5SApple OSS Distributions 		.flags = MPO_SERVICE_PORT | MPO_INSERT_SEND_RIGHT | MPO_CONTEXT_AS_GUARD | MPO_STRICT,
45*1b191cb5SApple OSS Distributions 		.service_port_info = &sp_info,
46*1b191cb5SApple OSS Distributions 	};
47*1b191cb5SApple OSS Distributions 
48*1b191cb5SApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts, SP_CONTEXT, &service_port);
49*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct %u", service_port);
50*1b191cb5SApple OSS Distributions 
51*1b191cb5SApple OSS Distributions 	mach_port_options_t opts2 = {
52*1b191cb5SApple OSS Distributions 		.flags = MPO_CONNECTION_PORT,
53*1b191cb5SApple OSS Distributions 		.service_port_name = service_port,
54*1b191cb5SApple OSS Distributions 	};
55*1b191cb5SApple OSS Distributions 
56*1b191cb5SApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts2, 0x0, &connection_port);
57*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct %u", connection_port);
58*1b191cb5SApple OSS Distributions 
59*1b191cb5SApple OSS Distributions 	kr = mach_port_is_connection_for_service(mach_task_self(), connection_port, service_port, &fpid);
60*1b191cb5SApple OSS Distributions 	if (kr != KERN_SUCCESS || kr != KERN_NOT_SUPPORTED) {
61*1b191cb5SApple OSS Distributions 		T_LOG("mach_port_is_connection_for_service kr = %d, fpid = %llu", kr, fpid);
62*1b191cb5SApple OSS Distributions 	}
63*1b191cb5SApple OSS Distributions 
64*1b191cb5SApple OSS Distributions 	// notification port for the service port to come back on
65*1b191cb5SApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &notify_port);
66*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_allocate notify_port");
67*1b191cb5SApple OSS Distributions 
68*1b191cb5SApple OSS Distributions 	kr = mach_port_insert_right(mach_task_self(), notify_port, notify_port, MACH_MSG_TYPE_MAKE_SEND);
69*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_insert_right notify_port");
70*1b191cb5SApple OSS Distributions 
71*1b191cb5SApple OSS Distributions 	T_LOG("service port: 0x%x, notify port: 0x%x\n", service_port, notify_port);
72*1b191cb5SApple OSS Distributions 
73*1b191cb5SApple OSS Distributions 	kr = mach_port_request_notification(mach_task_self(), service_port, MACH_NOTIFY_PORT_DESTROYED, 0, notify_port,
74*1b191cb5SApple OSS Distributions 	    MACH_MSG_TYPE_MAKE_SEND_ONCE, &previous);
75*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_request_notification service_port");
76*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(previous, MACH_PORT_NULL, "previous null");
77*1b191cb5SApple OSS Distributions 
78*1b191cb5SApple OSS Distributions 	/* Attempt to destroy port */
79*1b191cb5SApple OSS Distributions 	kr = mach_port_destruct(mach_task_self(), service_port, 0, SP_CONTEXT);
80*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_destruct service_port");
81*1b191cb5SApple OSS Distributions 
82*1b191cb5SApple OSS Distributions 	/*
83*1b191cb5SApple OSS Distributions 	 * Recover the service port because the port must have been destroyed and sent the notification by now
84*1b191cb5SApple OSS Distributions 	 */
85*1b191cb5SApple OSS Distributions 	kr = mach_msg_server_once(notify_server, MACH_MSG_SIZE_RELIABLE, notify_port, MACH_RCV_TIMEOUT);
86*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_msg_server_once notify_port");
87*1b191cb5SApple OSS Distributions 
88*1b191cb5SApple OSS Distributions 	T_LOG("done");
89*1b191cb5SApple OSS Distributions }
90*1b191cb5SApple OSS Distributions 
91*1b191cb5SApple OSS Distributions kern_return_t
do_mach_notify_port_destroyed(mach_port_t notify,mach_port_t name)92*1b191cb5SApple OSS Distributions do_mach_notify_port_destroyed(mach_port_t notify, mach_port_t name)
93*1b191cb5SApple OSS Distributions {
94*1b191cb5SApple OSS Distributions 	kern_return_t kr;
95*1b191cb5SApple OSS Distributions 
96*1b191cb5SApple OSS Distributions 	T_LOG("Received a service port destroyed notification notify = 0x%x name = 0x%x", notify, name);
97*1b191cb5SApple OSS Distributions 	if (name == MACH_PORT_NULL) {
98*1b191cb5SApple OSS Distributions 		T_FAIL("do_mach_notify_port_destroyed: MACH_PORT_NULL?");
99*1b191cb5SApple OSS Distributions 	}
100*1b191cb5SApple OSS Distributions 
101*1b191cb5SApple OSS Distributions 	if (name != service_port) {
102*1b191cb5SApple OSS Distributions 		T_FAIL("do_mach_notify_port_destroyed: name 0x%x != service_port: 0x%x", name, service_port);
103*1b191cb5SApple OSS Distributions 	}
104*1b191cb5SApple OSS Distributions 
105*1b191cb5SApple OSS Distributions 	struct mach_service_port_info sp_info = {};
106*1b191cb5SApple OSS Distributions 	kr = mach_port_get_service_port_info(mach_task_self(), service_port, &sp_info);
107*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_get_service_port_info");
108*1b191cb5SApple OSS Distributions 
109*1b191cb5SApple OSS Distributions 	if (strcmp(sp_info.mspi_string_name, SERVICE_NAME)) {
110*1b191cb5SApple OSS Distributions 		T_FAIL("Service port name = %s is incorrect", sp_info.mspi_string_name);
111*1b191cb5SApple OSS Distributions 	}
112*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(sp_info.mspi_domain_type, SERVICE_DOMAIN, "Service domain = %u", sp_info.mspi_domain_type);
113*1b191cb5SApple OSS Distributions 
114*1b191cb5SApple OSS Distributions 	mach_port_guard_info_t mpgi = {SP_CONTEXT};
115*1b191cb5SApple OSS Distributions 	kr = mach_port_assert_attributes(mach_task_self(), service_port, MACH_PORT_GUARD_INFO, (mach_port_info_t)&mpgi, MACH_PORT_GUARD_INFO_COUNT);
116*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_assert_attributes");
117*1b191cb5SApple OSS Distributions 
118*1b191cb5SApple OSS Distributions 	return KERN_SUCCESS;
119*1b191cb5SApple OSS Distributions }
120*1b191cb5SApple OSS Distributions 
121*1b191cb5SApple OSS Distributions kern_return_t
do_mach_notify_port_deleted(__unused mach_port_t notify,__unused mach_port_name_t name)122*1b191cb5SApple OSS Distributions do_mach_notify_port_deleted(__unused mach_port_t notify, __unused mach_port_name_t name)
123*1b191cb5SApple OSS Distributions {
124*1b191cb5SApple OSS Distributions 	return KERN_SUCCESS;
125*1b191cb5SApple OSS Distributions }
126*1b191cb5SApple OSS Distributions 
127*1b191cb5SApple OSS Distributions kern_return_t
do_mach_notify_no_senders(__unused mach_port_t notify,__unused mach_port_mscount_t mscount)128*1b191cb5SApple OSS Distributions do_mach_notify_no_senders(__unused mach_port_t notify, __unused mach_port_mscount_t mscount)
129*1b191cb5SApple OSS Distributions {
130*1b191cb5SApple OSS Distributions 	return KERN_SUCCESS;
131*1b191cb5SApple OSS Distributions }
132*1b191cb5SApple OSS Distributions 
133*1b191cb5SApple OSS Distributions kern_return_t
do_mach_notify_send_once(__unused mach_port_t notify)134*1b191cb5SApple OSS Distributions do_mach_notify_send_once(__unused mach_port_t notify)
135*1b191cb5SApple OSS Distributions {
136*1b191cb5SApple OSS Distributions 	return KERN_SUCCESS;
137*1b191cb5SApple OSS Distributions }
138*1b191cb5SApple OSS Distributions 
139*1b191cb5SApple OSS Distributions kern_return_t
do_mach_notify_dead_name(__unused mach_port_t notify,__unused mach_port_name_t name)140*1b191cb5SApple OSS Distributions do_mach_notify_dead_name(__unused mach_port_t notify, __unused mach_port_name_t name)
141*1b191cb5SApple OSS Distributions {
142*1b191cb5SApple OSS Distributions 	return KERN_SUCCESS;
143*1b191cb5SApple OSS Distributions }
144*1b191cb5SApple OSS Distributions 
145*1b191cb5SApple OSS Distributions #define SERVICE_NAME_2 "com.apple.testservice2"
146*1b191cb5SApple OSS Distributions #define SERVICE_DOMAIN_2 (2)
147*1b191cb5SApple OSS Distributions 
148*1b191cb5SApple OSS Distributions T_DECL(mach_fake_service_port, "Create a connection port with a fake service port", T_META_CHECK_LEAKS(false))
149*1b191cb5SApple OSS Distributions {
150*1b191cb5SApple OSS Distributions 	mach_port_t connection_port;
151*1b191cb5SApple OSS Distributions 	mach_port_t fake_service_port;
152*1b191cb5SApple OSS Distributions 	mach_port_t service_port_2;
153*1b191cb5SApple OSS Distributions 
154*1b191cb5SApple OSS Distributions 	kern_return_t kr;
155*1b191cb5SApple OSS Distributions 
156*1b191cb5SApple OSS Distributions 	struct mach_service_port_info sp_info = {};
157*1b191cb5SApple OSS Distributions 
158*1b191cb5SApple OSS Distributions 	strcpy(sp_info.mspi_string_name, SERVICE_NAME_2);
159*1b191cb5SApple OSS Distributions 	sp_info.mspi_domain_type = (uint8_t)SERVICE_DOMAIN_2;
160*1b191cb5SApple OSS Distributions 
161*1b191cb5SApple OSS Distributions 	mach_port_options_t opts = {
162*1b191cb5SApple OSS Distributions 		.flags = MPO_CONNECTION_PORT | MPO_SERVICE_PORT | MPO_INSERT_SEND_RIGHT | MPO_CONTEXT_AS_GUARD,
163*1b191cb5SApple OSS Distributions 		.service_port_info = &sp_info,
164*1b191cb5SApple OSS Distributions 	};
165*1b191cb5SApple OSS Distributions 
166*1b191cb5SApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts, SP_CONTEXT, &service_port_2);
167*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_ERROR(kr, KERN_INVALID_ARGUMENT, "mach_port_construct with extra flags %u", service_port_2);
168*1b191cb5SApple OSS Distributions 
169*1b191cb5SApple OSS Distributions 	mach_port_options_t opts2 = {
170*1b191cb5SApple OSS Distributions 		.flags = MPO_SERVICE_PORT | MPO_INSERT_SEND_RIGHT | MPO_CONTEXT_AS_GUARD,
171*1b191cb5SApple OSS Distributions 		.service_port_info = NULL,
172*1b191cb5SApple OSS Distributions 	};
173*1b191cb5SApple OSS Distributions 
174*1b191cb5SApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts2, SP_CONTEXT, &service_port_2);
175*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_ERROR(kr, KERN_INVALID_ARGUMENT, "mach_port_construct with missing service port info %u", service_port_2);
176*1b191cb5SApple OSS Distributions 
177*1b191cb5SApple OSS Distributions 	mach_port_options_t opts3 = {
178*1b191cb5SApple OSS Distributions 		.flags = MPO_INSERT_SEND_RIGHT | MPO_CONTEXT_AS_GUARD,
179*1b191cb5SApple OSS Distributions 	};
180*1b191cb5SApple OSS Distributions 
181*1b191cb5SApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts3, SP_CONTEXT, &fake_service_port);
182*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct with missing flag %u", fake_service_port);
183*1b191cb5SApple OSS Distributions 
184*1b191cb5SApple OSS Distributions 	struct mach_service_port_info sp_info3 = {};
185*1b191cb5SApple OSS Distributions 	kr = mach_port_get_service_port_info(mach_task_self(), fake_service_port, &sp_info3);
186*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_ERROR(kr, KERN_INVALID_CAPABILITY, "mach_port_get_service_port_info");
187*1b191cb5SApple OSS Distributions 
188*1b191cb5SApple OSS Distributions 	mach_port_options_t opts4 = {
189*1b191cb5SApple OSS Distributions 		.flags = MPO_CONNECTION_PORT,
190*1b191cb5SApple OSS Distributions 		.service_port_name = fake_service_port,
191*1b191cb5SApple OSS Distributions 	};
192*1b191cb5SApple OSS Distributions 
193*1b191cb5SApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts4, 0x0, &connection_port);
194*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_ERROR(kr, KERN_INVALID_CAPABILITY, "mach_port_construct connection port %u", connection_port);
195*1b191cb5SApple OSS Distributions 
196*1b191cb5SApple OSS Distributions 	T_LOG("done");
197*1b191cb5SApple OSS Distributions }
198*1b191cb5SApple OSS Distributions 
199*1b191cb5SApple OSS Distributions T_DECL(mach_dead_service_port, "Create a connection port with a dead service port", T_META_CHECK_LEAKS(false))
200*1b191cb5SApple OSS Distributions {
201*1b191cb5SApple OSS Distributions 	mach_port_t connection_port;
202*1b191cb5SApple OSS Distributions 	mach_port_t service_port_2;
203*1b191cb5SApple OSS Distributions 
204*1b191cb5SApple OSS Distributions 	kern_return_t kr;
205*1b191cb5SApple OSS Distributions 
206*1b191cb5SApple OSS Distributions 	struct mach_service_port_info sp_info = {};
207*1b191cb5SApple OSS Distributions 
208*1b191cb5SApple OSS Distributions 	strcpy(sp_info.mspi_string_name, SERVICE_NAME_2);
209*1b191cb5SApple OSS Distributions 	sp_info.mspi_domain_type = (uint8_t)SERVICE_DOMAIN_2;
210*1b191cb5SApple OSS Distributions 
211*1b191cb5SApple OSS Distributions 	mach_port_options_t opts = {
212*1b191cb5SApple OSS Distributions 		.flags = MPO_SERVICE_PORT | MPO_INSERT_SEND_RIGHT,
213*1b191cb5SApple OSS Distributions 		.service_port_info = &sp_info,
214*1b191cb5SApple OSS Distributions 	};
215*1b191cb5SApple OSS Distributions 
216*1b191cb5SApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts, 0, &service_port_2);
217*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct %u", service_port_2);
218*1b191cb5SApple OSS Distributions 
219*1b191cb5SApple OSS Distributions 	kr = mach_port_mod_refs(mach_task_self(), service_port_2, MACH_PORT_RIGHT_RECEIVE, -1);
220*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_mod_refs");
221*1b191cb5SApple OSS Distributions 
222*1b191cb5SApple OSS Distributions 	mach_port_options_t opts3 = {
223*1b191cb5SApple OSS Distributions 		.flags = MPO_CONNECTION_PORT,
224*1b191cb5SApple OSS Distributions 		.service_port_name = service_port_2,
225*1b191cb5SApple OSS Distributions 	};
226*1b191cb5SApple OSS Distributions 
227*1b191cb5SApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts3, 0x0, &connection_port);
228*1b191cb5SApple OSS Distributions 	T_LOG("mach_port_construct connection port kr = %d", kr);
229*1b191cb5SApple OSS Distributions 
230*1b191cb5SApple OSS Distributions 	if (kr == KERN_INVALID_RIGHT || kr == KERN_INVALID_NAME) {
231*1b191cb5SApple OSS Distributions 		T_PASS("Invalid service port");
232*1b191cb5SApple OSS Distributions 	} else {
233*1b191cb5SApple OSS Distributions 		T_FAIL("mach_port_construct incorrect return value");
234*1b191cb5SApple OSS Distributions 	}
235*1b191cb5SApple OSS Distributions 
236*1b191cb5SApple OSS Distributions 	T_LOG("done");
237*1b191cb5SApple OSS Distributions }
238