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