Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- * * Copyright (c) 2016 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <stdlib.h> #include <string.h> #include <limits.h> #include <stdio.h> #include <mach/shared_region.h> #include <mach/mach_vm.h> #include <libkern/OSAtomic.h> #include "dyld_process_info.h" #include "dyld_process_info_internal.h" #include "dyld_images.h" #include "dyld_priv.h" typedef void (^Notify)(bool unload, uint64_t timestamp, uint64_t machHeader, const uuid_t uuid, const char* path); typedef void (^NotifyExit)(); typedef void (^NotifyMain)(); // // Object used for monitoring another processes dyld loads // struct __attribute__((visibility("hidden"))) dyld_process_info_notify_base { static dyld_process_info_notify_base* make(task_t task, dispatch_queue_t queue, Notify notify, NotifyExit notifyExit, kern_return_t* kr); ~dyld_process_info_notify_base(); uint32_t& retainCount() const { return _retainCount; } void setNotifyMain(NotifyMain notifyMain) const { _notifyMain = notifyMain; } private: dyld_process_info_notify_base(dispatch_queue_t queue, Notify notify, NotifyExit notifyExit, task_t task); kern_return_t makePorts(); kern_return_t pokeSendPortIntoTarget(); kern_return_t unpokeSendPortInTarget(); void setMachSourceOnQueue(); void* operator new (size_t, void* buf) { return buf; } mutable uint32_t _retainCount; dispatch_queue_t _queue; Notify _notify; NotifyExit _notifyExit; mutable NotifyMain _notifyMain; task_t _targetTask; dispatch_source_t _machSource; uint64_t _portAddressInTarget; mach_port_t _sendPortInTarget; // target is process being watched for image loading/unloading mach_port_t _receivePortInMonitor; // monitor is process being notified of image loading/unloading }; dyld_process_info_notify_base::dyld_process_info_notify_base(dispatch_queue_t queue, Notify notify, NotifyExit notifyExit, task_t task) : _retainCount(1), _queue(queue), _notify(notify), _notifyExit(notifyExit), _notifyMain(NULL), _targetTask(task), _machSource(NULL), _portAddressInTarget(0), _sendPortInTarget(0), _receivePortInMonitor(0) { dispatch_retain(_queue); } dyld_process_info_notify_base::~dyld_process_info_notify_base() { if ( _machSource ) { dispatch_release(_machSource); _machSource = NULL; } if ( _portAddressInTarget ) { unpokeSendPortInTarget(); _portAddressInTarget = 0; } if ( _sendPortInTarget ) { _sendPortInTarget = 0; } dispatch_release(_queue); if ( _receivePortInMonitor != 0 ) { mach_port_deallocate(mach_task_self(), _receivePortInMonitor); _receivePortInMonitor = 0; } } dyld_process_info_notify_base* dyld_process_info_notify_base::make(task_t task, dispatch_queue_t queue, Notify notify, NotifyExit notifyExit, kern_return_t* kr) { void* storage = malloc(sizeof(dyld_process_info_notify_base)); dyld_process_info_notify_base* obj = new (storage) dyld_process_info_notify_base(queue, notify, notifyExit, task); if ( kern_return_t r = obj->makePorts() ) { if ( kr != NULL ) *kr = r; goto fail; } obj->setMachSourceOnQueue(); if ( kern_return_t r = obj->pokeSendPortIntoTarget() ) { if ( kr != NULL ) *kr = r; goto fail; } if ( kr != NULL ) *kr = KERN_SUCCESS; return obj; fail: free(obj); return NULL; } kern_return_t dyld_process_info_notify_base::makePorts() { // Allocate a port to listen on in this monitoring task if ( kern_return_t r = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &_receivePortInMonitor) ) return r; // Add send rights for replying if ( kern_return_t r = mach_port_insert_right(mach_task_self(), _receivePortInMonitor, _receivePortInMonitor, MACH_MSG_TYPE_MAKE_SEND) ) return r; // Allocate a name in the target. We need a new name to add send rights to if ( kern_return_t r = mach_port_allocate(_targetTask, MACH_PORT_RIGHT_DEAD_NAME, &_sendPortInTarget) ) return r; // Deallocate the dead name if ( kern_return_t r = mach_port_mod_refs(_targetTask, _sendPortInTarget, MACH_PORT_RIGHT_DEAD_NAME, -1) ) return r; // Make the dead name a send right to our listening port if ( kern_return_t r = mach_port_insert_right(_targetTask, _sendPortInTarget, _receivePortInMonitor, MACH_MSG_TYPE_MAKE_SEND) ) return r; // Notify us if the target dies mach_port_t previous = MACH_PORT_NULL; if ( kern_return_t r = mach_port_request_notification(_targetTask, _sendPortInTarget, MACH_NOTIFY_DEAD_NAME, 0, _receivePortInMonitor, MACH_MSG_TYPE_MAKE_SEND_ONCE, &previous)) return r; //fprintf(stderr, "_sendPortInTarget=%d, _receivePortInMonitor=%d\n", _sendPortInTarget, _receivePortInMonitor); return KERN_SUCCESS; } void dyld_process_info_notify_base::setMachSourceOnQueue() { NotifyExit exitHandler = _notifyExit; _machSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV, _receivePortInMonitor, 0, _queue); dispatch_source_set_event_handler(_machSource, ^{ uint8_t messageBuffer[DYLD_PROCESS_INFO_NOTIFY_MAX_BUFFER_SIZE]; mach_msg_header_t* h = (mach_msg_header_t*)messageBuffer; kern_return_t r = mach_msg(h, MACH_RCV_MSG, 0, sizeof(messageBuffer), _receivePortInMonitor, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); if ( r == KERN_SUCCESS ) { //fprintf(stderr, "received message id=0x%X, size=%d\n", h->msgh_id, h->msgh_size); if ( h->msgh_id == DYLD_PROCESS_INFO_NOTIFY_LOAD_ID || h->msgh_id == DYLD_PROCESS_INFO_NOTIFY_UNLOAD_ID ) { // run notifier block for each [un]load image const dyld_process_info_notify_header* header = (dyld_process_info_notify_header*)messageBuffer; const dyld_process_info_image_entry* entries = (dyld_process_info_image_entry*)&messageBuffer[header->imagesOffset]; const char* const stringPool = (char*)&messageBuffer[header->stringsOffset]; for (unsigned i=0; i < header->imageCount; ++i) { bool isUnload = (h->msgh_id == DYLD_PROCESS_INFO_NOTIFY_UNLOAD_ID); _notify(isUnload, header->timestamp, entries[i].loadAddress, entries[i].uuid, stringPool + entries[i].pathStringOffset); } // reply to dyld, so it can continue mach_msg_header_t replyHeader; replyHeader.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND); replyHeader.msgh_id = 0; replyHeader.msgh_local_port = MACH_PORT_NULL; replyHeader.msgh_remote_port = h->msgh_remote_port; replyHeader.msgh_reserved = 0; replyHeader.msgh_size = sizeof(replyHeader); mach_msg(&replyHeader, MACH_SEND_MSG | MACH_SEND_TIMEOUT, replyHeader.msgh_size, 0, MACH_PORT_NULL, 100, MACH_PORT_NULL); } else if ( h->msgh_id == DYLD_PROCESS_INFO_NOTIFY_MAIN_ID ) { if ( _notifyMain != NULL ) { _notifyMain(); } // reply to dyld, so it can continue mach_msg_header_t replyHeader; replyHeader.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND); replyHeader.msgh_id = 0; replyHeader.msgh_local_port = MACH_PORT_NULL; replyHeader.msgh_remote_port = h->msgh_remote_port; replyHeader.msgh_reserved = 0; replyHeader.msgh_size = sizeof(replyHeader); mach_msg(&replyHeader, MACH_SEND_MSG | MACH_SEND_TIMEOUT, replyHeader.msgh_size, 0, MACH_PORT_NULL, 100, MACH_PORT_NULL); } else if ( h->msgh_id == MACH_NOTIFY_PORT_DELETED ) { mach_port_t deadPort = ((mach_port_deleted_notification_t *)h)->not_port; //fprintf(stderr, "received message id=MACH_NOTIFY_PORT_DELETED, size=%d, deadPort=%d\n", h->msgh_size, deadPort); if ( deadPort == _sendPortInTarget ) { // target process died. Clean up ports _sendPortInTarget = 0; mach_port_deallocate(mach_task_self(), _receivePortInMonitor); _receivePortInMonitor = 0; _portAddressInTarget = 0; // notify that target is gone exitHandler(); } } else { fprintf(stderr, "received unknown message id=0x%X, size=%d\n", h->msgh_id, h->msgh_size); } } }); dispatch_resume(_machSource); } kern_return_t dyld_process_info_notify_base::pokeSendPortIntoTarget() { // get location on all_image_infos in target task task_dyld_info_data_t taskDyldInfo; mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; kern_return_t r = task_info(_targetTask, TASK_DYLD_INFO, (task_info_t)&taskDyldInfo, &count); if ( r ) return r; // remap the page containing all_image_infos into this process r/w mach_vm_address_t mappedAddress = 0; mach_vm_size_t mappedSize = taskDyldInfo.all_image_info_size; vm_prot_t curProt = VM_PROT_NONE; vm_prot_t maxProt = VM_PROT_NONE; r = mach_vm_remap(mach_task_self(), &mappedAddress, mappedSize, 0, VM_FLAGS_ANYWHERE | VM_FLAGS_RETURN_DATA_ADDR, _targetTask, taskDyldInfo.all_image_info_addr, false, &curProt, &maxProt, VM_INHERIT_NONE); if ( r ) return r; if ( curProt != (VM_PROT_READ|VM_PROT_WRITE) ) return KERN_PROTECTION_FAILURE; // atomically set port into all_image_info_struct static_assert(sizeof(mach_port_t) == sizeof(uint32_t), "machport size not 32-bits"); mach_vm_address_t mappedAddressToPokePort = 0; if ( taskDyldInfo.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_32 ) mappedAddressToPokePort = mappedAddress + offsetof(dyld_all_image_infos_32,notifyMachPorts); else mappedAddressToPokePort = mappedAddress + offsetof(dyld_all_image_infos_64,notifyMachPorts); // use first available slot bool slotFound = false; for (int slotIndex=0; slotIndex < DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT; ++slotIndex) { if ( OSAtomicCompareAndSwap32Barrier(0, _sendPortInTarget, (volatile int32_t*)mappedAddressToPokePort) ) { slotFound = true; break; } mappedAddressToPokePort += sizeof(uint32_t); } if ( !slotFound ) { mach_vm_deallocate(mach_task_self(), mappedAddress, mappedSize); return KERN_UREFS_OVERFLOW; } _portAddressInTarget = taskDyldInfo.all_image_info_addr + mappedAddressToPokePort - mappedAddress; //fprintf(stderr, "poked port %d into target at address 0x%llX\n", _sendPortInTarget, _portAddressInTarget); mach_vm_deallocate(mach_task_self(), mappedAddress, mappedSize); return r; } kern_return_t dyld_process_info_notify_base::unpokeSendPortInTarget() { // remap the page containing all_image_infos into this process r/w mach_vm_address_t mappedAddress = 0; mach_vm_size_t mappedSize = sizeof(mach_port_t); vm_prot_t curProt = VM_PROT_NONE; vm_prot_t maxProt = VM_PROT_NONE; kern_return_t r = mach_vm_remap(mach_task_self(), &mappedAddress, mappedSize, 0, VM_FLAGS_ANYWHERE | VM_FLAGS_RETURN_DATA_ADDR, _targetTask, _portAddressInTarget, false, &curProt, &maxProt, VM_INHERIT_NONE); if ( r ) return r; if ( curProt != (VM_PROT_READ|VM_PROT_WRITE) ) return KERN_PROTECTION_FAILURE; OSAtomicCompareAndSwap32Barrier(_sendPortInTarget, 0, (volatile int32_t*)mappedAddress); //fprintf(stderr, "cleared port %d from target\n", _sendPortInTarget); mach_vm_deallocate(mach_task_self(), mappedAddress, mappedSize); return r; } dyld_process_info_notify _dyld_process_info_notify(task_t task, dispatch_queue_t queue, void (^notify)(bool unload, uint64_t timestamp, uint64_t machHeader, const uuid_t uuid, const char* path), void (^notifyExit)(), kern_return_t* kr) { return dyld_process_info_notify_base::make(task, queue, notify, notifyExit, kr); } void _dyld_process_info_notify_main(dyld_process_info_notify object, void (^notifyMain)()) { object->setNotifyMain(notifyMain); } void _dyld_process_info_notify_retain(dyld_process_info_notify object) { object->retainCount() += 1; } void _dyld_process_info_notify_release(dyld_process_info_notify object) { object->retainCount() -= 1; if ( object->retainCount() == 0 ) { object->~dyld_process_info_notify_base(); free((void*)object); } } |