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