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