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