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 | #include <mach-o/fixup-chains.h> #include <mach-o/loader.h> #include "kernel-fixups.h" #ifndef LC_FILESET_ENTRY #define LC_FILESET_ENTRY (0x35 | LC_REQ_DYLD) /* used with fileset_entry_command */ struct fileset_entry_command { uint32_t cmd; /* LC_FILESET_ENTRY */ uint32_t cmdsize; /* includes id string */ uint64_t vmaddr; /* memory address of the dylib */ uint64_t fileoff; /* file offset of the dylib */ union lc_str entry_id; /* contained entry id */ uint32_t reserved; /* entry_id is 32-bits long, so this is the reserved padding */ }; #endif typedef int (*ModInitLogFunc)(const char*, ...); static const int LogModInits = 0; struct TestRunnerFunctions; typedef int (*InitializerFunc)(const TestRunnerFunctions*); #if __x86_64__ __attribute__((section(("__HIB, __text")))) #else __attribute__((section(("__TEXT_EXEC, __text")))) #endif static int getSlide(const struct mach_header* mh, ModInitLogFunc logFunc, uintptr_t* slide) { uint64_t textVMAddr = 0; if (LogFixups) { logFunc("[LOG] kernel-slide: mh %p\n", mh); } if (LogFixups) { logFunc("[LOG] kernel-slide: 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; //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-slide: 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_SEGMENT_64 ) { const struct segment_command_64* seg = (const struct segment_command_64*)cmd; if ( areEqual(seg->segname, "__TEXT") ) { textVMAddr = seg->vmaddr; } } cmd = nextCmd; } *slide = (uintptr_t)mh - textVMAddr; return 0; } #if __x86_64__ __attribute__((section(("__HIB, __text")))) #else __attribute__((section(("__TEXT_EXEC, __text")))) #endif static int runAllModInitFunctions(const struct mach_header* mh, ModInitLogFunc logFunc, const TestRunnerFunctions* funcs) { uintptr_t slide = 0; if ( getSlide(mh, logFunc, &slide) != 0 ) { return 1; } 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; //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 (LogModInits) { logFunc("[LOG] kernel-mod-inits: 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_SEGMENT_64 ) { const struct segment_command_64* seg = (const struct segment_command_64*)cmd; const struct section_64* const sectionsStart = (struct section_64*)((char*)seg + sizeof(struct segment_command_64)); const struct section_64* const sectionsEnd = §ionsStart[seg->nsects]; for (const struct section_64* sect = sectionsStart; sect < sectionsEnd; ++sect) { const uint8_t type = sect->flags & SECTION_TYPE; if ( LogModInits ) { logFunc("[LOG] kernel-mod-inits: section: %s %s\n", sect->segname, sect->sectname); } if ( type == S_MOD_INIT_FUNC_POINTERS ) { InitializerFunc* inits = (InitializerFunc*)(sect->addr + slide); const uintptr_t count = sect->size / sizeof(uintptr_t); // Ensure __mod_init_func section is within segment if ( (sect->addr < seg->vmaddr) || (sect->addr+sect->size > seg->vmaddr+seg->vmsize) || (sect->addr+sect->size < sect->addr) ) { logFunc("[LOG] kernel-mod-inits: __mod_init_funcs section has malformed address range\n"); return 1; } for (uintptr_t j = 0; j < count; ++j) { InitializerFunc func = inits[j]; #if __has_feature(ptrauth_calls) func = (InitializerFunc)__builtin_ptrauth_sign_unauthenticated((void*)func, ptrauth_key_asia, 0); #endif if ( LogModInits ) { logFunc("[LOG] kernel-mod-inits: running mod init %p\n", (const void*)func); } int initResult = func(funcs); if ( initResult != 0 ) { logFunc("[LOG] kernel-mod-inits: mod init %p, result = %d\n", (const void*)func, initResult); return 1; } } } } } cmd = nextCmd; } return 0; } #if __x86_64__ __attribute__((section(("__HIB, __text")))) #else __attribute__((section(("__TEXT_EXEC, __text")))) #endif static int runAllModInitFunctionsForAppCache(const struct mach_header* appCacheMH, ModInitLogFunc logFunc, const TestRunnerFunctions* funcs) { uintptr_t slide = 0; if ( getSlide(appCacheMH, logFunc, &slide) != 0 ) { return 1; } if (LogFixups) { logFunc("[LOG] mod-init: appCacheMH %p\n", appCacheMH); } if (LogFixups) { logFunc("[LOG] mod-init: parsing load commands\n"); } const struct load_command* startCmds = 0; if ( appCacheMH->magic == MH_MAGIC_64 ) startCmds = (struct load_command*)((char *)appCacheMH + sizeof(struct mach_header_64)); else if ( appCacheMH->magic == MH_MAGIC ) startCmds = (struct load_command*)((char *)appCacheMH + sizeof(struct mach_header)); else { const uint32_t* h = (uint32_t*)appCacheMH; //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 + appCacheMH->sizeofcmds); const struct load_command* cmd = startCmds; for (uint32_t i = 0; i < appCacheMH->ncmds; ++i) { if (LogFixups) { logFunc("[LOG] mod-init: 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_FILESET_ENTRY ) { const struct fileset_entry_command* app_cache_cmd = (const struct fileset_entry_command*)cmd; const char* name = (char*)app_cache_cmd + app_cache_cmd->entry_id.offset; const struct mach_header* mh = (const struct mach_header*)(app_cache_cmd->vmaddr + slide); if ( LogModInits ) { logFunc("[LOG] mod-init: Running mod inits for %p: %s\n", mh, name); } int result = runAllModInitFunctions(mh, logFunc, funcs); if (result != 0) { return 1; } } cmd = nextCmd; } return 0; } #if __x86_64__ __attribute__((section(("__HIB, __text")))) #else __attribute__((section(("__TEXT_EXEC, __text")))) #endif static int slideKextsInsideKernelCollection(const struct mach_header* appCacheMH, const void* basePointers[4], FixupsLogFunc logFunc, const TestRunnerFunctions* funcs) { uintptr_t slideAmount = 0; if ( getSlide(appCacheMH, logFunc, &slideAmount) != 0 ) { return 1; } if (LogFixups) { logFunc("[LOG] slide-pageable: appCacheMH %p\n", appCacheMH); } if (LogFixups) { logFunc("[LOG] slide-pageable: parsing load commands\n"); } const struct load_command* startCmds = 0; if ( appCacheMH->magic == MH_MAGIC_64 ) startCmds = (struct load_command*)((char *)appCacheMH + sizeof(struct mach_header_64)); else if ( appCacheMH->magic == MH_MAGIC ) startCmds = (struct load_command*)((char *)appCacheMH + sizeof(struct mach_header)); else { const uint32_t* h = (uint32_t*)appCacheMH; //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 + appCacheMH->sizeofcmds); const struct load_command* cmd = startCmds; for (uint32_t i = 0; i < appCacheMH->ncmds; ++i) { if (LogFixups) { logFunc("[LOG] slide-pageable: 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_FILESET_ENTRY ) { const struct fileset_entry_command* app_cache_cmd = (const struct fileset_entry_command*)cmd; const char* name = (char*)app_cache_cmd + app_cache_cmd->entry_id.offset; const struct mach_header* mh = (const struct mach_header*)(app_cache_cmd->vmaddr + slideAmount); if ( LogModInits ) { logFunc("[LOG] slide-pageable: Sliding %p: %s\n", mh, name); } int slideReturnCode = slide(mh, basePointers, logFunc); if ( slideReturnCode != 0 ) { FAIL("mh slide = %d\n", slideReturnCode); return 1; } } cmd = nextCmd; } return 0; } |