xref: /xnu-8796.121.2/tests/immovable_send_client.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 #include <darwintest_multiprocess.h>
9*c54f35caSApple OSS Distributions #include <IOKit/IOKitLib.h>
10*c54f35caSApple OSS Distributions 
11*c54f35caSApple OSS Distributions typedef struct {
12*c54f35caSApple OSS Distributions 	mach_msg_header_t   header;
13*c54f35caSApple OSS Distributions 	mach_msg_body_t     body;
14*c54f35caSApple OSS Distributions 	mach_msg_port_descriptor_t port_descriptor;
15*c54f35caSApple OSS Distributions 	mach_msg_trailer_t  trailer;            // subtract this when sending
16*c54f35caSApple OSS Distributions } ipc_complex_message;
17*c54f35caSApple OSS Distributions 
18*c54f35caSApple OSS Distributions static ipc_complex_message icm_request = {};
19*c54f35caSApple OSS Distributions 
20*c54f35caSApple OSS Distributions struct args {
21*c54f35caSApple OSS Distributions 	const char *progname;
22*c54f35caSApple OSS Distributions 	int verbose;
23*c54f35caSApple OSS Distributions 	int voucher;
24*c54f35caSApple OSS Distributions 	int num_msgs;
25*c54f35caSApple OSS Distributions 	const char *server_port_name;
26*c54f35caSApple OSS Distributions 	mach_port_t server_port;
27*c54f35caSApple OSS Distributions 	mach_port_t reply_port;
28*c54f35caSApple OSS Distributions 	mach_port_t voucher_port;
29*c54f35caSApple OSS Distributions 	int request_msg_size;
30*c54f35caSApple OSS Distributions 	void *request_msg;
31*c54f35caSApple OSS Distributions 	int reply_msg_size;
32*c54f35caSApple OSS Distributions 	void *reply_msg;
33*c54f35caSApple OSS Distributions 	mach_port_t sp_voucher_port;
34*c54f35caSApple OSS Distributions 	uint32_t persona_id;
35*c54f35caSApple OSS Distributions 	long client_pid;
36*c54f35caSApple OSS Distributions };
37*c54f35caSApple OSS Distributions 
38*c54f35caSApple OSS Distributions static void
parse_args(struct args * args)39*c54f35caSApple OSS Distributions parse_args(struct args *args)
40*c54f35caSApple OSS Distributions {
41*c54f35caSApple OSS Distributions 	args->verbose = 0;
42*c54f35caSApple OSS Distributions 	args->voucher = 0;
43*c54f35caSApple OSS Distributions 	args->server_port_name = "TEST_IMMOVABLE_SEND";
44*c54f35caSApple OSS Distributions 	args->server_port = MACH_PORT_NULL;
45*c54f35caSApple OSS Distributions 	args->reply_port = MACH_PORT_NULL;
46*c54f35caSApple OSS Distributions 	args->voucher_port = MACH_PORT_NULL;
47*c54f35caSApple OSS Distributions 	args->num_msgs = 1;
48*c54f35caSApple OSS Distributions 	args->request_msg_size = sizeof(ipc_complex_message) - sizeof(mach_msg_trailer_t);
49*c54f35caSApple OSS Distributions 	//args->reply_msg_size = sizeof(ipc_complex_message2) - sizeof(mach_msg_trailer_t);
50*c54f35caSApple OSS Distributions 	args->request_msg = &icm_request;
51*c54f35caSApple OSS Distributions 	args->reply_msg = NULL;
52*c54f35caSApple OSS Distributions 	args->client_pid = getpid();
53*c54f35caSApple OSS Distributions }
54*c54f35caSApple OSS Distributions 
55*c54f35caSApple OSS Distributions int
main()56*c54f35caSApple OSS Distributions main()
57*c54f35caSApple OSS Distributions {
58*c54f35caSApple OSS Distributions 	struct args client_args = {};
59*c54f35caSApple OSS Distributions 	parse_args(&client_args);
60*c54f35caSApple OSS Distributions 
61*c54f35caSApple OSS Distributions 	/* Find the bootstrap port */
62*c54f35caSApple OSS Distributions 	mach_port_t bsport;
63*c54f35caSApple OSS Distributions 	kern_return_t ret = task_get_bootstrap_port(mach_task_self(), &bsport);
64*c54f35caSApple OSS Distributions 	if (ret) {
65*c54f35caSApple OSS Distributions 		mach_error("client: task_get_bootstrap_port()", ret);
66*c54f35caSApple OSS Distributions 		exit(1);
67*c54f35caSApple OSS Distributions 	}
68*c54f35caSApple OSS Distributions 
69*c54f35caSApple OSS Distributions 	printf("client: Look up bootstrap service port\n");
70*c54f35caSApple OSS Distributions 	ret = bootstrap_look_up(bsport, client_args.server_port_name,
71*c54f35caSApple OSS Distributions 	    &client_args.server_port);
72*c54f35caSApple OSS Distributions 	if (ret) {
73*c54f35caSApple OSS Distributions 		mach_error("client: bootstrap_look_up()", ret);
74*c54f35caSApple OSS Distributions 		exit(1);
75*c54f35caSApple OSS Distributions 	}
76*c54f35caSApple OSS Distributions 
77*c54f35caSApple OSS Distributions 	printf("client: Look up the ioconnect service port to be sent\n");
78*c54f35caSApple OSS Distributions 	io_service_t amfi = IO_OBJECT_NULL;
79*c54f35caSApple OSS Distributions 	io_connect_t connect = IO_OBJECT_NULL;
80*c54f35caSApple OSS Distributions 	IOReturn ioret;
81*c54f35caSApple OSS Distributions 
82*c54f35caSApple OSS Distributions 	amfi = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleMobileFileIntegrity"));
83*c54f35caSApple OSS Distributions 	if (amfi == IO_OBJECT_NULL) {
84*c54f35caSApple OSS Distributions 		fprintf(stderr, "client: unable to find AppleMobileFileIntegrity service\n");
85*c54f35caSApple OSS Distributions 		exit(1);
86*c54f35caSApple OSS Distributions 	}
87*c54f35caSApple OSS Distributions 	ioret = IOServiceOpen(amfi, mach_task_self(), 0, &connect);
88*c54f35caSApple OSS Distributions 	if (ioret != kIOReturnSuccess) {
89*c54f35caSApple OSS Distributions 		fprintf(stderr, "client: unable to open user client: 0x%x\n", ret);
90*c54f35caSApple OSS Distributions 		exit(1);
91*c54f35caSApple OSS Distributions 	}
92*c54f35caSApple OSS Distributions 
93*c54f35caSApple OSS Distributions 	printf("client: Found the matching io_connect port = %d\n", connect);
94*c54f35caSApple OSS Distributions 
95*c54f35caSApple OSS Distributions 	/* Construct the message */
96*c54f35caSApple OSS Distributions 	mach_msg_header_t *request = (mach_msg_header_t *)client_args.request_msg;
97*c54f35caSApple OSS Distributions 	request->msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, 0,
98*c54f35caSApple OSS Distributions 	    0, 0) | MACH_MSGH_BITS_COMPLEX;
99*c54f35caSApple OSS Distributions 	request->msgh_size = (mach_msg_size_t)client_args.request_msg_size;
100*c54f35caSApple OSS Distributions 	request->msgh_remote_port = client_args.server_port;
101*c54f35caSApple OSS Distributions 	request->msgh_local_port = MACH_PORT_NULL;
102*c54f35caSApple OSS Distributions 	request->msgh_id = 1;
103*c54f35caSApple OSS Distributions 
104*c54f35caSApple OSS Distributions 	ipc_complex_message *complexmsg = (ipc_complex_message *)request;
105*c54f35caSApple OSS Distributions 	complexmsg->body.msgh_descriptor_count = 1;
106*c54f35caSApple OSS Distributions 	complexmsg->port_descriptor.name = connect;
107*c54f35caSApple OSS Distributions 	complexmsg->port_descriptor.disposition = MACH_MSG_TYPE_MOVE_SEND;
108*c54f35caSApple OSS Distributions 	complexmsg->port_descriptor.type = MACH_MSG_PORT_DESCRIPTOR;
109*c54f35caSApple OSS Distributions 
110*c54f35caSApple OSS Distributions 	mach_msg_option_t option = MACH_SEND_MSG;
111*c54f35caSApple OSS Distributions 
112*c54f35caSApple OSS Distributions 	printf("client: Sending request (expecting it to fail) \n");
113*c54f35caSApple OSS Distributions 	mach_msg_return_t mret = mach_msg(request,
114*c54f35caSApple OSS Distributions 	    option,
115*c54f35caSApple OSS Distributions 	    (mach_msg_size_t)client_args.request_msg_size,
116*c54f35caSApple OSS Distributions 	    0,
117*c54f35caSApple OSS Distributions 	    MACH_PORT_NULL,
118*c54f35caSApple OSS Distributions 	    MACH_MSG_TIMEOUT_NONE,
119*c54f35caSApple OSS Distributions 	    MACH_PORT_NULL);
120*c54f35caSApple OSS Distributions 
121*c54f35caSApple OSS Distributions 	printf("client: mach_msg returned %x\n", mret);
122*c54f35caSApple OSS Distributions 	if (mret != MACH_SEND_INVALID_RIGHT) {
123*c54f35caSApple OSS Distributions 		mach_error("client: mach_msg", mret);
124*c54f35caSApple OSS Distributions 		exit(1);
125*c54f35caSApple OSS Distributions 	}
126*c54f35caSApple OSS Distributions 
127*c54f35caSApple OSS Distributions 	printf("It should never reach here\n");
128*c54f35caSApple OSS Distributions 
129*c54f35caSApple OSS Distributions 	return 0;
130*c54f35caSApple OSS Distributions }
131