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