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