1*27b03b36SApple OSS Distributions #include <darwintest.h>
2*27b03b36SApple OSS Distributions #include <servers/bootstrap.h>
3*27b03b36SApple OSS Distributions #include <mach/mach.h>
4*27b03b36SApple OSS Distributions #include <mach/message.h>
5*27b03b36SApple OSS Distributions #include <stdlib.h>
6*27b03b36SApple OSS Distributions #include <sys/sysctl.h>
7*27b03b36SApple OSS Distributions #include <unistd.h>
8*27b03b36SApple OSS Distributions
9*27b03b36SApple OSS Distributions T_GLOBAL_META(
10*27b03b36SApple OSS Distributions T_META_NAMESPACE("xnu.ipc"),
11*27b03b36SApple OSS Distributions T_META_RUN_CONCURRENTLY(TRUE),
12*27b03b36SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
13*27b03b36SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("IPC"));
14*27b03b36SApple OSS Distributions
15*27b03b36SApple OSS Distributions typedef struct {
16*27b03b36SApple OSS Distributions mach_msg_header_t header;
17*27b03b36SApple OSS Distributions mach_msg_body_t body;
18*27b03b36SApple OSS Distributions mach_msg_guarded_port_descriptor_t guarded_port_descriptor1;
19*27b03b36SApple OSS Distributions mach_msg_guarded_port_descriptor_t guarded_port_descriptor2;
20*27b03b36SApple OSS Distributions mach_msg_trailer_t trailer; // subtract this when sending
21*27b03b36SApple OSS Distributions } ipc_complex_message;
22*27b03b36SApple OSS Distributions
23*27b03b36SApple OSS Distributions static ipc_complex_message icm_request = {};
24*27b03b36SApple OSS Distributions
25*27b03b36SApple OSS Distributions struct args {
26*27b03b36SApple OSS Distributions const char *progname;
27*27b03b36SApple OSS Distributions int verbose;
28*27b03b36SApple OSS Distributions int voucher;
29*27b03b36SApple OSS Distributions int num_msgs;
30*27b03b36SApple OSS Distributions const char *server_port_name;
31*27b03b36SApple OSS Distributions mach_port_t server_port;
32*27b03b36SApple OSS Distributions mach_port_t reply_port;
33*27b03b36SApple OSS Distributions mach_port_t voucher_port;
34*27b03b36SApple OSS Distributions int request_msg_size;
35*27b03b36SApple OSS Distributions void *request_msg;
36*27b03b36SApple OSS Distributions int reply_msg_size;
37*27b03b36SApple OSS Distributions void *reply_msg;
38*27b03b36SApple OSS Distributions mach_port_t sp_voucher_port;
39*27b03b36SApple OSS Distributions uint32_t persona_id;
40*27b03b36SApple OSS Distributions long client_pid;
41*27b03b36SApple OSS Distributions };
42*27b03b36SApple OSS Distributions
43*27b03b36SApple OSS Distributions void parse_args(struct args *args);
44*27b03b36SApple OSS Distributions void* create_buffer(int *buffer_size);
45*27b03b36SApple OSS Distributions void client(struct args *args);
46*27b03b36SApple OSS Distributions void server_setup(struct args* args);
47*27b03b36SApple OSS Distributions void server(struct args *args);
48*27b03b36SApple OSS Distributions
49*27b03b36SApple OSS Distributions void
parse_args(struct args * args)50*27b03b36SApple OSS Distributions parse_args(struct args *args)
51*27b03b36SApple OSS Distributions {
52*27b03b36SApple OSS Distributions args->verbose = 0;
53*27b03b36SApple OSS Distributions args->voucher = 0;
54*27b03b36SApple OSS Distributions args->server_port_name = "TEST";
55*27b03b36SApple OSS Distributions args->server_port = MACH_PORT_NULL;
56*27b03b36SApple OSS Distributions args->reply_port = MACH_PORT_NULL;
57*27b03b36SApple OSS Distributions args->voucher_port = MACH_PORT_NULL;
58*27b03b36SApple OSS Distributions args->num_msgs = 1;
59*27b03b36SApple OSS Distributions args->request_msg_size = sizeof(ipc_complex_message);
60*27b03b36SApple OSS Distributions args->request_msg = &icm_request;
61*27b03b36SApple OSS Distributions args->client_pid = getpid();
62*27b03b36SApple OSS Distributions }
63*27b03b36SApple OSS Distributions
64*27b03b36SApple OSS Distributions /* Create a mach IPC listener which will respond to the client's message */
65*27b03b36SApple OSS Distributions void
server_setup(struct args * args)66*27b03b36SApple OSS Distributions server_setup(struct args* args)
67*27b03b36SApple OSS Distributions {
68*27b03b36SApple OSS Distributions kern_return_t ret;
69*27b03b36SApple OSS Distributions mach_port_t bsport;
70*27b03b36SApple OSS Distributions
71*27b03b36SApple OSS Distributions ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE,
72*27b03b36SApple OSS Distributions &args->server_port);
73*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "server: mach_port_allocate()");
74*27b03b36SApple OSS Distributions
75*27b03b36SApple OSS Distributions ret = mach_port_insert_right(mach_task_self(), args->server_port, args->server_port,
76*27b03b36SApple OSS Distributions MACH_MSG_TYPE_MAKE_SEND);
77*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "server: mach_port_insert_right()");
78*27b03b36SApple OSS Distributions
79*27b03b36SApple OSS Distributions ret = task_get_bootstrap_port(mach_task_self(), &bsport);
80*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "server: task_get_bootstrap_port()");
81*27b03b36SApple OSS Distributions
82*27b03b36SApple OSS Distributions ret = bootstrap_register(bsport, args->server_port_name, args->server_port);
83*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "server: bootstrap_register()");
84*27b03b36SApple OSS Distributions
85*27b03b36SApple OSS Distributions T_LOG("server: waiting for IPC messages from client on port '%s'.\n",
86*27b03b36SApple OSS Distributions args->server_port_name);
87*27b03b36SApple OSS Distributions }
88*27b03b36SApple OSS Distributions
89*27b03b36SApple OSS Distributions /* Server process loop
90*27b03b36SApple OSS Distributions *
91*27b03b36SApple OSS Distributions * Listens for message.
92*27b03b36SApple OSS Distributions *
93*27b03b36SApple OSS Distributions */
94*27b03b36SApple OSS Distributions void
server(struct args * args)95*27b03b36SApple OSS Distributions server(struct args *args)
96*27b03b36SApple OSS Distributions {
97*27b03b36SApple OSS Distributions mach_msg_header_t *request;
98*27b03b36SApple OSS Distributions mach_msg_option_t rcvoption;
99*27b03b36SApple OSS Distributions kern_return_t ret;
100*27b03b36SApple OSS Distributions
101*27b03b36SApple OSS Distributions request = (mach_msg_header_t *)args->request_msg;
102*27b03b36SApple OSS Distributions
103*27b03b36SApple OSS Distributions rcvoption = MACH_RCV_MSG | MACH_RCV_INTERRUPT | MACH_RCV_GUARDED_DESC;
104*27b03b36SApple OSS Distributions
105*27b03b36SApple OSS Distributions T_LOG("server: Awaiting message\n");
106*27b03b36SApple OSS Distributions ret = mach_msg(request,
107*27b03b36SApple OSS Distributions rcvoption,
108*27b03b36SApple OSS Distributions 0,
109*27b03b36SApple OSS Distributions sizeof(ipc_complex_message),
110*27b03b36SApple OSS Distributions args->server_port,
111*27b03b36SApple OSS Distributions MACH_MSG_TIMEOUT_NONE,
112*27b03b36SApple OSS Distributions MACH_PORT_NULL);
113*27b03b36SApple OSS Distributions
114*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "server: mach_msg receive");
115*27b03b36SApple OSS Distributions
116*27b03b36SApple OSS Distributions ipc_complex_message *request_complexmsg = (ipc_complex_message *)request;
117*27b03b36SApple OSS Distributions T_ASSERT_NE(request_complexmsg->guarded_port_descriptor1.name, 0, "server: Should not receive mach_port_null; name = %x", request_complexmsg->guarded_port_descriptor1.name);
118*27b03b36SApple OSS Distributions T_ASSERT_EQ(request_complexmsg->guarded_port_descriptor1.type, MACH_MSG_GUARDED_PORT_DESCRIPTOR, "server: Received a guarded port descriptor");
119*27b03b36SApple OSS Distributions T_ASSERT_EQ(request_complexmsg->guarded_port_descriptor1.disposition, MACH_MSG_TYPE_PORT_RECEIVE, "server: Received a receive right");
120*27b03b36SApple OSS Distributions T_ASSERT_EQ(request_complexmsg->guarded_port_descriptor1.context, (unsigned long)request, "server: Received a port with correct context = %p", request);
121*27b03b36SApple OSS Distributions T_LOG("Guard flags = %d", request_complexmsg->guarded_port_descriptor1.flags);
122*27b03b36SApple OSS Distributions
123*27b03b36SApple OSS Distributions T_ASSERT_NE(request_complexmsg->guarded_port_descriptor2.name, 0, "server: Should not receive mach_port_null; name = %x", request_complexmsg->guarded_port_descriptor2.name);
124*27b03b36SApple OSS Distributions T_ASSERT_EQ(request_complexmsg->guarded_port_descriptor2.type, MACH_MSG_GUARDED_PORT_DESCRIPTOR, "server: Received a guarded port descriptor");
125*27b03b36SApple OSS Distributions T_ASSERT_EQ(request_complexmsg->guarded_port_descriptor2.disposition, MACH_MSG_TYPE_PORT_RECEIVE, "server: Received a receive right");
126*27b03b36SApple OSS Distributions T_ASSERT_EQ(request_complexmsg->guarded_port_descriptor2.context, (unsigned long)request, "server: Received a port with correct context = %p", request);
127*27b03b36SApple OSS Distributions
128*27b03b36SApple OSS Distributions mach_port_status_t status;
129*27b03b36SApple OSS Distributions mach_msg_type_number_t status_size = MACH_PORT_RECEIVE_STATUS_COUNT;
130*27b03b36SApple OSS Distributions
131*27b03b36SApple OSS Distributions kern_return_t kr = mach_port_get_attributes(mach_task_self(), request_complexmsg->guarded_port_descriptor1.name,
132*27b03b36SApple OSS Distributions MACH_PORT_RECEIVE_STATUS, (mach_port_info_t)&status, &status_size);
133*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_get_attributes for descriptor 1");
134*27b03b36SApple OSS Distributions T_LOG("Status flags %d", status.mps_flags);
135*27b03b36SApple OSS Distributions T_ASSERT_NE(0, (status.mps_flags & MACH_PORT_STATUS_FLAG_GUARD_IMMOVABLE_RECEIVE), "Imm rcv bit is set for descriptor1");
136*27b03b36SApple OSS Distributions
137*27b03b36SApple OSS Distributions kr = mach_port_get_attributes(mach_task_self(), request_complexmsg->guarded_port_descriptor2.name,
138*27b03b36SApple OSS Distributions MACH_PORT_RECEIVE_STATUS, (mach_port_info_t)&status, &status_size);
139*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_get_attributes for descriptor 2");
140*27b03b36SApple OSS Distributions T_LOG("Status flags %d", status.mps_flags);
141*27b03b36SApple OSS Distributions T_ASSERT_NE(0, (status.mps_flags & MACH_PORT_STATUS_FLAG_GUARD_IMMOVABLE_RECEIVE), "Imm rcv bit is set for descriptor2");
142*27b03b36SApple OSS Distributions
143*27b03b36SApple OSS Distributions mach_msg_destroy(request);
144*27b03b36SApple OSS Distributions }
145*27b03b36SApple OSS Distributions
146*27b03b36SApple OSS Distributions void
client(struct args * args)147*27b03b36SApple OSS Distributions client(struct args *args)
148*27b03b36SApple OSS Distributions {
149*27b03b36SApple OSS Distributions //Find the bootstrap port
150*27b03b36SApple OSS Distributions mach_port_t bsport;
151*27b03b36SApple OSS Distributions mach_port_t guarded_port;
152*27b03b36SApple OSS Distributions mach_port_t unguarded_port;
153*27b03b36SApple OSS Distributions
154*27b03b36SApple OSS Distributions kern_return_t ret = task_get_bootstrap_port(mach_task_self(), &bsport);
155*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "client: task_get_bootstrap_port()");
156*27b03b36SApple OSS Distributions
157*27b03b36SApple OSS Distributions //Look up the service port
158*27b03b36SApple OSS Distributions ret = bootstrap_look_up(bsport, (char *)args->server_port_name,
159*27b03b36SApple OSS Distributions &args->server_port);
160*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "client: bootstrap_look_up()");
161*27b03b36SApple OSS Distributions
162*27b03b36SApple OSS Distributions //Create the unguarded port
163*27b03b36SApple OSS Distributions ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE,
164*27b03b36SApple OSS Distributions &unguarded_port);
165*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "client: mach_port_allocate() reply port");
166*27b03b36SApple OSS Distributions
167*27b03b36SApple OSS Distributions mach_port_options_t opts = {
168*27b03b36SApple OSS Distributions .flags = MPO_CONTEXT_AS_GUARD
169*27b03b36SApple OSS Distributions };
170*27b03b36SApple OSS Distributions
171*27b03b36SApple OSS Distributions ret = mach_port_construct(mach_task_self(), &opts, 0x10, &guarded_port);
172*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "mach_port_construct");
173*27b03b36SApple OSS Distributions
174*27b03b36SApple OSS Distributions //Construct the message
175*27b03b36SApple OSS Distributions mach_msg_header_t *request = (mach_msg_header_t *)args->request_msg;
176*27b03b36SApple OSS Distributions request->msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE,
177*27b03b36SApple OSS Distributions 0, 0) | MACH_MSGH_BITS_COMPLEX;
178*27b03b36SApple OSS Distributions request->msgh_size = (mach_msg_size_t)args->request_msg_size;
179*27b03b36SApple OSS Distributions request->msgh_remote_port = args->server_port;
180*27b03b36SApple OSS Distributions request->msgh_local_port = args->reply_port;
181*27b03b36SApple OSS Distributions request->msgh_id = 1;
182*27b03b36SApple OSS Distributions
183*27b03b36SApple OSS Distributions ipc_complex_message *complexmsg = (ipc_complex_message *)request;
184*27b03b36SApple OSS Distributions complexmsg->body.msgh_descriptor_count = 2;
185*27b03b36SApple OSS Distributions complexmsg->guarded_port_descriptor1.name = guarded_port;
186*27b03b36SApple OSS Distributions complexmsg->guarded_port_descriptor1.disposition = MACH_MSG_TYPE_MOVE_RECEIVE;
187*27b03b36SApple OSS Distributions complexmsg->guarded_port_descriptor1.flags = MACH_MSG_GUARD_FLAGS_IMMOVABLE_RECEIVE;
188*27b03b36SApple OSS Distributions complexmsg->guarded_port_descriptor1.context = 0x10;
189*27b03b36SApple OSS Distributions complexmsg->guarded_port_descriptor1.type = MACH_MSG_GUARDED_PORT_DESCRIPTOR;
190*27b03b36SApple OSS Distributions
191*27b03b36SApple OSS Distributions complexmsg->guarded_port_descriptor2.name = unguarded_port;
192*27b03b36SApple OSS Distributions complexmsg->guarded_port_descriptor2.disposition = MACH_MSG_TYPE_MOVE_RECEIVE;
193*27b03b36SApple OSS Distributions complexmsg->guarded_port_descriptor2.flags = MACH_MSG_GUARD_FLAGS_IMMOVABLE_RECEIVE | MACH_MSG_GUARD_FLAGS_UNGUARDED_ON_SEND;
194*27b03b36SApple OSS Distributions complexmsg->guarded_port_descriptor2.context = 0;
195*27b03b36SApple OSS Distributions complexmsg->guarded_port_descriptor2.type = MACH_MSG_GUARDED_PORT_DESCRIPTOR;
196*27b03b36SApple OSS Distributions
197*27b03b36SApple OSS Distributions mach_msg_option_t option = MACH_SEND_MSG;
198*27b03b36SApple OSS Distributions
199*27b03b36SApple OSS Distributions //Listen for the reply on the reply port
200*27b03b36SApple OSS Distributions T_LOG("client: Sending request\n");
201*27b03b36SApple OSS Distributions ret = mach_msg(request,
202*27b03b36SApple OSS Distributions option,
203*27b03b36SApple OSS Distributions (mach_msg_size_t)args->request_msg_size,
204*27b03b36SApple OSS Distributions 0,
205*27b03b36SApple OSS Distributions MACH_PORT_NULL,
206*27b03b36SApple OSS Distributions MACH_MSG_TIMEOUT_NONE,
207*27b03b36SApple OSS Distributions MACH_PORT_NULL);
208*27b03b36SApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "client: mach_msg_overwrite()");
209*27b03b36SApple OSS Distributions }
210*27b03b36SApple OSS Distributions
211*27b03b36SApple OSS Distributions T_DECL(mo_immovable_receive, "Send a message containing a guard port descriptor for an immovable receive right")
212*27b03b36SApple OSS Distributions {
213*27b03b36SApple OSS Distributions struct args args = {};
214*27b03b36SApple OSS Distributions parse_args(&args);
215*27b03b36SApple OSS Distributions args.request_msg_size -= sizeof(mach_msg_trailer_t);
216*27b03b36SApple OSS Distributions args.reply_msg_size -= sizeof(mach_msg_trailer_t);
217*27b03b36SApple OSS Distributions
218*27b03b36SApple OSS Distributions //Create the server
219*27b03b36SApple OSS Distributions pid_t pid = fork();
220*27b03b36SApple OSS Distributions if (pid == 0) {
221*27b03b36SApple OSS Distributions T_LOG("Server is up");
222*27b03b36SApple OSS Distributions server_setup(&args);
223*27b03b36SApple OSS Distributions server(&args);
224*27b03b36SApple OSS Distributions exit(0);
225*27b03b36SApple OSS Distributions }
226*27b03b36SApple OSS Distributions
227*27b03b36SApple OSS Distributions sleep(2);
228*27b03b36SApple OSS Distributions T_LOG("Preparing client to send a request");
229*27b03b36SApple OSS Distributions client(&args);
230*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid(pid, NULL, 0), "waitpid()");
231*27b03b36SApple OSS Distributions }
232