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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- vim: ft=cpp et ts=4 sw=4: * * Copyright (c) 2023 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 <fcntl.h> #include <libgen.h> #include <algorithm> #include <libproc.h> #include <sys/mman.h> #include <mach/task.h> #include <sys/malloc.h> #include <mach/mach_vm.h> #include <mach/mach_traps.h> #include <TargetConditionals.h> #include "AAREncoder.h" #include "Allocator.h" #include "PropertyList.h" #include "ProcessScavenger.h" #include "SnapshotShared.h" #include "Header.h" #include "DyldSharedCache.h" #include "Vector.h" #include "SafeVMPrimitives.h" #include <sys/fsgetpath.h> #include <mach-o/dyld_priv.h> #include <mach-o/dyld_process_info.h> #include "dyld_cache_format.h" #include "dyld_process_info_internal.h" using lsl::Allocator; using lsl::UniquePtr; using lsl::Vector; using mach_o::Header; using UUID = PropertyList::UUID; using Array = PropertyList::Array; using Data = PropertyList::Data; using Dictionary = PropertyList::Dictionary; using String = PropertyList::String; using Bitmap = PropertyList::Bitmap; using Integer = PropertyList::Integer; namespace { struct MmappedBuffer { MmappedBuffer() = default; MmappedBuffer(const MmappedBuffer&) = delete; MmappedBuffer(MmappedBuffer&& other) { swap(other); } MmappedBuffer& operator=(const MmappedBuffer&) = delete; MmappedBuffer& operator=(MmappedBuffer&& other) { swap(other); return *this; } MmappedBuffer(const char* path) { int fd = open(path, O_RDONLY); if (fd < 0 ) { return; } struct stat statBuf; if (fstat(fd, &statBuf) != 0) { return; } _size = statBuf.st_size; _data = (void*)mmap(nullptr, (size_t)_size, PROT_READ, MAP_PRIVATE, fd, 0); if (_data == MAP_FAILED) { return; } close(fd); } ~MmappedBuffer() { if (!_data) { return; } munmap(_data, (size_t)_size); } uint64_t size() const { return _size; } const std::span<const uint8_t> span() const { return std::span<const uint8_t>((const uint8_t*)_data, (size_t)_size); } private: void swap(MmappedBuffer& other) { if (this == &other) { return; } using std::swap; swap(_data, other._data); swap(_size, other._size); } void* _data = nullptr; uint64_t _size = 0; }; struct RemoteMap { RemoteMap(task_t task, mach_vm_address_t remote_address, vm_size_t size) : _size(size) { vm_prot_t cur_protection = VM_PROT_NONE; vm_prot_t max_protection = VM_PROT_READ; mach_vm_address_t localAddress = 0; auto kr = mach_vm_remap_new(mach_task_self(), &localAddress, _size, 0, // mask VM_FLAGS_ANYWHERE | VM_FLAGS_RESILIENT_CODESIGN | VM_FLAGS_RESILIENT_MEDIA, task, remote_address, true, &cur_protection, &max_protection, VM_INHERIT_NONE); // The call is not succesfull return if (kr != KERN_SUCCESS) { _data = nullptr; _size = 0; return; } // Copy into a local buffer so our results are coherent in the event the page goes way due to storage removal, // etc. We have to do this because even after we read the page the contents might go away of the object is paged // out and then the backing region is disconnected (for example, if we are copying some memory in the middle of // a mach-o that is on a USB drive that is disconnected after we perform the mapping). Once we copy them into a // local buffer the memory will be handled by the default pager instead of potentially being backed by the mmap // pager, and thus will be guaranteed not to mutate out from under us. _data = malloc(_size); if (_data == nullptr) { _size = 0; (void)vm_deallocate(mach_task_self(), (vm_address_t)localAddress, _size); return; } remote_memory_audit_start(); memcpy(_data, (void *)localAddress, _size); remote_memory_audit_end(); (void)vm_deallocate(mach_task_self(), (vm_address_t)localAddress, _size); } RemoteMap(const RemoteMap&) = delete; RemoteMap(RemoteMap&& other) { swap(other); } MmappedBuffer& operator=(const MmappedBuffer&) = delete; RemoteMap& operator=(RemoteMap&& other) { swap(other); return *this; } ~RemoteMap() { if (_data) { free(_data); } } operator bool() const { return ((_data != nullptr) && (_size != 0)); } const std::span<const uint8_t> span() const { return std::span<const uint8_t>((const uint8_t*)_data, (size_t)_size); } uint64_t size() const { return _size; } private: void swap(RemoteMap& other) { if (this == &other) { return; } using std::swap; swap(_data, other._data); swap(_size, other._size); } void* _data = nullptr; vm_size_t _size = 0; }; struct TaskSuspender { TaskSuspender(task_read_t task) : _task(task) { if (task != mach_task_self()) { task_suspend(_task); } else { kern_return_t kr = task_threads(_task, &_threads, &_threadCount); if (kr != KERN_SUCCESS) { return; } for (auto i = 0; i < _threadCount; ++i) { if (_threads[i] != mach_thread_self()) { thread_suspend(_threads[i]); } } } } ~TaskSuspender() { if (_task != mach_task_self()) { task_resume(_task); } else { for (auto i = 0; i < _threadCount; ++i) { if (_threads[i] != mach_thread_self()) { thread_resume(_threads[i]); } mach_port_deallocate(mach_task_self(), _threads[i]); } mach_vm_deallocate(mach_task_self(), (mach_vm_address_t) _threads, _threadCount * sizeof(*_threads)); } } private: task_read_t _task = 0; thread_act_array_t _threads = nullptr; mach_msg_type_number_t _threadCount = 0; }; static void addSegmentArray(PropertyList::Dictionary& image, const Header* header) { __block Array* segments = nullptr; header->forEachSegment(^(const mach_o::Header::SegmentInfo& info, bool& stop) { if (info.segmentName == "__PAGEZERO") { return; } if (!segments) { segments = &image.addObjectForKey<Array>(kDyldAtlasImageSegmentArrayKey); } auto segment = &segments->addObject<Dictionary>(); segment->addObjectForKey<String>(kDyldAtlasSegmentNameKey, info.segmentName); // Note: we use the std::string_view part of CString segment->addObjectForKey<Integer>(kDyldAtlasSegmentPreferredLoadAddressKey, info.vmaddr); segment->addObjectForKey<Integer>(kDyldAtlasSegmentSizeKey, info.vmsize); segment->addObjectForKey<Integer>(kDyldAtlasSegmentFileOffsetKey, info.fileOffset); segment->addObjectForKey<Integer>(kDyldAtlasSegmentFileSizeKey, info.fileSize); segment->addObjectForKey<Integer>(kDyldAtlasSegmentPermissionsKey, info.initProt); }); } bool scavengeProcessFromRegions(Allocator& allocator, task_read_t task, ByteStream& outputStream) { TaskSuspender suspender(task); pid_t pid; auto propertyListEncoder = PropertyList(allocator); auto& rootDictionary = propertyListEncoder.rootDictionary(); auto& images = rootDictionary.addObjectForKey<Array>(kDyldAtlasSnapshotImagesArrayKey); kern_return_t kr = pid_for_task(task, &pid); if ( kr != KERN_SUCCESS) { return false; } rootDictionary.addObjectForKey<Integer>(kDyldAtlasSnapshotPidKey, pid); auto snapshotFlags = rootDictionary.addObjectForKey<PropertyList::Flags<SnapshotFlags>>(kDyldAtlasSnapshotFlagsKey); // Set the timestamp to 1, which is earlier then any real timestamp, but not 0, since tools use 0 as a sign // the process is not running yet and the API call has failed. rootDictionary.addObjectForKey<Integer>(kDyldAtlasSnapshotTimestampKey, 1); UniquePtr<const std::byte> fullCacheHeader; UniquePtr<const std::byte> infoArrayBuffer; UniquePtr<const std::byte> aotInfoArrayBuffer; rootDictionary.addObjectForKey<Integer>(kDyldAtlasSnapshotPlatformTypeKey, 0); #if TARGET_OS_WATCH && !TARGET_OS_SIMULATOR snapshotFlags.setFlag(SnapshotFlagsPointerSize4Bytes, true); #endif mach_vm_size_t size; bool dyldFound = false; bool mainExecutableFound = false; for (mach_vm_address_t address = 0; ; address += size) { vm_region_basic_info_data_64_t info; mach_port_t objectName; unsigned int infoCount = VM_REGION_BASIC_INFO_COUNT_64; if ( mach_vm_region(task, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)&info, &infoCount, &objectName) != KERN_SUCCESS ) { break; } if ( info.protection != (VM_PROT_READ|VM_PROT_EXECUTE) ) { continue; } RemoteMap map(task, address, std::min((size_t)size, (size_t)PAGE_SIZE)); if (!map) { continue; } auto mf = Header::isMachO(map.span()); if (!mf) { continue; } uint32_t headerSize = mf->headerAndLoadCommandsSize(); if (headerSize > PAGE_SIZE) { size_t newSize = (size_t)lsl::roundToNextAligned(PAGE_SIZE, headerSize); auto newMap = RemoteMap(task, address, newSize); map = std::move(newMap); if (!map) { continue; } mf = Header::isMachO(map.span()); if (!mf) { continue; } } if (mf->isDylinker()) { dyldFound = true; } if (mf->isMainExecutable()) { mainExecutableFound = true; } // If this is not dyld or a main executable we don't need to scan the region if (!mf->isDylinker() && !mf->isMainExecutable()) { continue; } auto& image = images.addObject<Dictionary>(); uint64_t preferredLoadAddress = mf->preferredLoadAddress(); if (preferredLoadAddress) { image.addObjectForKey<Integer>(kDyldAtlasImagePreferredLoadAddressKey, preferredLoadAddress); } image.addObjectForKey<Integer>(kDyldAtlasImageLoadAddressKey, address); const char* installname = mf->installName(); if (installname) { image.addObjectForKey<String>(kDyldAtlasImageInstallnameKey, installname); } uuid_t uuid; if (mf->getUuid(uuid)) { image.addObjectForKey<UUID>(kDyldAtlasImageUUIDKey, uuid); } char executablePath[PATH_MAX+1]; int len = proc_regionfilename(pid, address, executablePath, PATH_MAX); if ( len != 0 ) { executablePath[len] = '\0'; image.addObjectForKey<String>(kDyldAtlasImageFilePathKey, executablePath); } addSegmentArray(image, mf); // If we have found dyld and the main executable we are done, exit early if (dyldFound && mainExecutableFound) { break; } } rootDictionary.addObjectForKey<Integer>(kDyldAtlasSnapshotInitialImageCount, 1); rootDictionary.addObjectForKey<Integer>(kDyldAtlasSnapshotState, dyld_process_state_not_started); ByteStream fileStream(allocator); propertyListEncoder.encode(fileStream); AAREncoder aarEncoder(allocator); aarEncoder.addFile("process.plist", fileStream); aarEncoder.encode(outputStream); return true; } }; // via bufferSize. If the size is larger than was passed in then the return value is false. Otherwise it is true. bool scavengeProcess(task_read_t task, void** buffer, uint64_t* bufferSize) { STACK_ALLOCATOR(allocator, 0); ByteStream outputStream(allocator); if (!scavengeProcessFromRegions(allocator, task, outputStream)) { return false; } *bufferSize = outputStream.size(); *buffer = malloc((size_t)(*bufferSize)); std::copy(outputStream.begin(), outputStream.end(), (std::byte*)*buffer); return true; } |