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