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