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 | #ifndef KERNEL_FIXUPS_H #define KERNEL_FIXUPS_H #include <mach-o/fixup-chains.h> #include <mach-o/loader.h> typedef int (*FixupsLogFunc)(const char*, ...); static const int LogFixups = 0; // We may not have strcmp, so make our own #if __x86_64__ __attribute__((section(("__HIB, __text")))) #else __attribute__((section(("__TEXT_EXEC, __text")))) #endif static int areEqual(const char* a, const char* b) { while (*a && *b) { if (*a != *b) return 0; ++a; ++b; } return *a == *b; } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wmissing-declarations" union ChainedFixupPointerOnDisk { uint64_t raw64; struct dyld_chained_ptr_64_kernel_cache_rebase fixup64; }; #pragma clang diagnostic pop // Temporary until we have <rdar://problem/57025372> #if __x86_64__ __attribute__((section(("__HIB, __text")))) #else __attribute__((section(("__TEXT_EXEC, __text")))) #endif static uint64_t signPointer(struct dyld_chained_ptr_64_kernel_cache_rebase pointer, void* loc, uint64_t target) { #if __has_feature(ptrauth_calls) uint64_t discriminator = pointer.diversity; if ( pointer.addrDiv ) { if ( discriminator != 0 ) { discriminator = __builtin_ptrauth_blend_discriminator(loc, discriminator); } else { discriminator = (uint64_t)(uintptr_t)loc; } } switch ( pointer.key ) { case 0: // IA return (uint64_t)__builtin_ptrauth_sign_unauthenticated((void*)target, 0, discriminator); case 1: // IB return (uint64_t)__builtin_ptrauth_sign_unauthenticated((void*)target, 1, discriminator); case 2: // DA return (uint64_t)__builtin_ptrauth_sign_unauthenticated((void*)target, 2, discriminator); case 3: // DB return (uint64_t)__builtin_ptrauth_sign_unauthenticated((void*)target, 3, discriminator); } #endif return target; } // Temporary until we have <rdar://problem/57025372> #if __x86_64__ __attribute__((section(("__HIB, __text")))) #else __attribute__((section(("__TEXT_EXEC, __text")))) #endif void fixupValue(union ChainedFixupPointerOnDisk* fixupLoc, const struct dyld_chained_starts_in_segment* segInfo, uintptr_t slide, const void* basePointers[4], int* stop, FixupsLogFunc logFunc) { if (LogFixups) { logFunc("[LOG] kernel-fixups: fixupValue %p\n", fixupLoc); } switch (segInfo->pointer_format) { #if __LP64__ case DYLD_CHAINED_PTR_64_KERNEL_CACHE: case DYLD_CHAINED_PTR_X86_64_KERNEL_CACHE: { const void* baseAddress = basePointers[fixupLoc->fixup64.cacheLevel]; if ( baseAddress == 0 ) { logFunc("Invalid cache level: %d\n", fixupLoc->fixup64.cacheLevel); *stop = 1; return; } uintptr_t slidValue = (uintptr_t)baseAddress + fixupLoc->fixup64.target; if (LogFixups) { logFunc("[LOG] kernel-fixups: slidValue %p = %p + %p\n", (void*)slidValue, (void*)baseAddress, (void*)fixupLoc->fixup64.target); } #if __has_feature(ptrauth_calls) if ( fixupLoc->fixup64.isAuth ) { slidValue = signPointer(fixupLoc->fixup64, fixupLoc, slidValue); } #else if ( fixupLoc->fixup64.isAuth ) { logFunc("Unexpected authenticated fixup\n"); *stop = 1; return; } #endif // __has_feature(ptrauth_calls) fixupLoc->raw64 = slidValue; break; } #endif // __LP64__ default: logFunc("unsupported pointer chain format: 0x%04X", segInfo->pointer_format); *stop = 1; break; } } // Temporary until we have <rdar://problem/57025372> #if __x86_64__ __attribute__((section(("__HIB, __text")))) #else __attribute__((section(("__TEXT_EXEC, __text")))) #endif int walkChain(const struct mach_header* mh, const struct dyld_chained_starts_in_segment* segInfo, uint32_t pageIndex, uint16_t offsetInPage, uintptr_t slide, const void* basePointers[4], FixupsLogFunc logFunc) { if (LogFixups) { logFunc("[LOG] kernel-fixups: walkChain page[%d]\n", pageIndex); } int stop = 0; uint8_t* pageContentStart = (uint8_t*)mh + segInfo->segment_offset + (pageIndex * segInfo->page_size); union ChainedFixupPointerOnDisk* chain = (union ChainedFixupPointerOnDisk*)(pageContentStart+offsetInPage); int chainEnd = 0; while (!stop && !chainEnd) { // copy chain content, in case handler modifies location to final value union ChainedFixupPointerOnDisk chainContent = *chain; fixupValue(chain, segInfo, slide, basePointers, &stop, logFunc); if ( !stop ) { switch (segInfo->pointer_format) { #if __LP64__ case DYLD_CHAINED_PTR_64_KERNEL_CACHE: if ( chainContent.fixup64.next == 0 ) chainEnd = 1; else chain = (union ChainedFixupPointerOnDisk*)((uint8_t*)chain + chainContent.fixup64.next*4); break; case DYLD_CHAINED_PTR_X86_64_KERNEL_CACHE: if ( chainContent.fixup64.next == 0 ) chainEnd = 1; else chain = (union ChainedFixupPointerOnDisk*)((uint8_t*)chain + chainContent.fixup64.next); break; #endif // __LP64__ default: logFunc("unknown pointer format 0x%04X", segInfo->pointer_format); stop = 1; } } } return stop; } // Temporary until we have <rdar://problem/57025372> #if __x86_64__ __attribute__((section(("__HIB, __text")))) #else __attribute__((section(("__TEXT_EXEC, __text")))) #endif int slide(const struct mach_header* mh, const void* basePointers[4], FixupsLogFunc logFunc) { // First find the slide and chained fixups load command uint64_t textVMAddr = 0; const struct linkedit_data_command* chainedFixups = 0; uint64_t linkeditVMAddr = 0; uint64_t linkeditFileOffset = 0; if (LogFixups) { logFunc("[LOG] kernel-fixups: mh %p\n", mh); } if (LogFixups) { logFunc("[LOG] kernel-fixups: parsing load commands\n"); } const struct load_command* startCmds = 0; if ( mh->magic == MH_MAGIC_64 ) startCmds = (struct load_command*)((char *)mh + sizeof(struct mach_header_64)); else if ( mh->magic == MH_MAGIC ) startCmds = (struct load_command*)((char *)mh + sizeof(struct mach_header)); else { const uint32_t* h = (uint32_t*)mh; logFunc("[LOG] kernel-fixups: file does not start with MH_MAGIC[_64] 0x%08X 0x%08X\n", h[0], h [1]); //diag.error("file does not start with MH_MAGIC[_64]: 0x%08X 0x%08X", h[0], h [1]); return 1; // not a mach-o file } const struct load_command* const cmdsEnd = (struct load_command*)((char*)startCmds + mh->sizeofcmds); const struct load_command* cmd = startCmds; for (uint32_t i = 0; i < mh->ncmds; ++i) { if (LogFixups) { logFunc("[LOG] kernel-fixups: parsing load command %d with cmd=0x%x\n", i, cmd->cmd); } const struct load_command* nextCmd = (struct load_command*)((char *)cmd + cmd->cmdsize); if ( cmd->cmdsize < 8 ) { //diag.error("malformed load command #%d of %d at %p with mh=%p, size (0x%X) too small", i, this->ncmds, cmd, this, cmd->cmdsize); return 1; } if ( (nextCmd > cmdsEnd) || (nextCmd < startCmds) ) { //diag.error("malformed load command #%d of %d at %p with mh=%p, size (0x%X) is too large, load commands end at %p", i, this->ncmds, cmd, this, cmd->cmdsize, cmdsEnd); return 1; } if ( cmd->cmd == LC_DYLD_CHAINED_FIXUPS ) { chainedFixups = (const struct linkedit_data_command*)cmd; } else if ( cmd->cmd == LC_SEGMENT_64 ) { const struct segment_command_64* seg = (const struct segment_command_64*)cmd; if ( areEqual(seg->segname, "__TEXT") ) { textVMAddr = seg->vmaddr; } else if ( areEqual(seg->segname, "__LINKEDIT") ) { linkeditVMAddr = seg->vmaddr; linkeditFileOffset = seg->fileoff; } } cmd = nextCmd; } uintptr_t slide = (uintptr_t)mh - textVMAddr; if (LogFixups) { logFunc("[LOG] kernel-fixups: slide 0x%llx\n", slide); } if ( chainedFixups == 0 ) return 0; if (LogFixups) { logFunc("[LOG] kernel-fixups: found chained fixups %p\n", chainedFixups); logFunc("[LOG] kernel-fixups: found linkeditVMAddr %p\n", (void*)linkeditVMAddr); logFunc("[LOG] kernel-fixups: found linkeditFileOffset %p\n", (void*)linkeditFileOffset); } // Now we have the chained fixups, walk it to apply all the rebases uint32_t offsetInLinkedit = (uint32_t)(chainedFixups->dataoff - linkeditFileOffset); uintptr_t linkeditStartAddr = linkeditVMAddr + slide; if (LogFixups) { logFunc("[LOG] kernel-fixups: offsetInLinkedit 0x%x\n", offsetInLinkedit); logFunc("[LOG] kernel-fixups: linkeditStartAddr %p\n", (void*)linkeditStartAddr); } const struct dyld_chained_fixups_header* fixupsHeader = (const struct dyld_chained_fixups_header*)(linkeditStartAddr + offsetInLinkedit); const struct dyld_chained_starts_in_image* fixupStarts = (const struct dyld_chained_starts_in_image*)((uint8_t*)fixupsHeader + fixupsHeader->starts_offset); if (LogFixups) { logFunc("[LOG] kernel-fixups: fixupsHeader %p\n", fixupsHeader); logFunc("[LOG] kernel-fixups: fixupStarts %p\n", fixupStarts); } int stopped = 0; for (uint32_t segIndex=0; segIndex < fixupStarts->seg_count && !stopped; ++segIndex) { if (LogFixups) { logFunc("[LOG] kernel-fixups: segment %d\n", segIndex); } if ( fixupStarts->seg_info_offset[segIndex] == 0 ) continue; const struct dyld_chained_starts_in_segment* segInfo = (const struct dyld_chained_starts_in_segment*)((uint8_t*)fixupStarts + fixupStarts->seg_info_offset[segIndex]); for (uint32_t pageIndex=0; pageIndex < segInfo->page_count && !stopped; ++pageIndex) { uint16_t offsetInPage = segInfo->page_start[pageIndex]; if ( offsetInPage == DYLD_CHAINED_PTR_START_NONE ) continue; if ( offsetInPage & DYLD_CHAINED_PTR_START_MULTI ) { // FIXME: Implement this return 1; } else { // one chain per page if ( walkChain(mh, segInfo, pageIndex, offsetInPage, slide, basePointers, logFunc) ) stopped = 1; } } } return stopped; } #endif /* KERNEL_FIXUPS_H */ |