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