|
| 1 | +#include "../include/tinyhook.h" |
| 2 | +#include "private.h" |
| 3 | + |
| 4 | +#include <mach-o/dyld.h> |
| 5 | +#include <mach-o/loader.h> |
| 6 | +#include <mach-o/nlist.h> |
| 7 | +#include <mach/mach_init.h> // mach_task_self() |
| 8 | +#include <mach/mach_vm.h> // mach_vm_* |
| 9 | +#include <stdint.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +static int update_pointer(struct section_64 *sym_sec, intptr_t slide, char *str_tbl, struct nlist_64 *nl_tbl, |
| 13 | + uint32_t *indirect_sym_tbl, const char *symbol_name, void *replacement) { |
| 14 | + if (!sym_sec) return 1; |
| 15 | + void **sym_ptrs = (void *)sym_sec->addr + slide; |
| 16 | + uint32_t *indirect_sym_entry = indirect_sym_tbl + sym_sec->reserved1; // reserved1 is an index! |
| 17 | + int nptrs = sym_sec->size / sizeof(void *); |
| 18 | + for (int i = 0; i < nptrs; i++) { |
| 19 | + uint32_t symtab_index = indirect_sym_entry[i]; |
| 20 | + if (symtab_index == INDIRECT_SYMBOL_LOCAL || symtab_index == (INDIRECT_SYMBOL_LOCAL | INDIRECT_SYMBOL_ABS)) { |
| 21 | + continue; |
| 22 | + } |
| 23 | + if (strcmp(symbol_name, str_tbl + nl_tbl[symtab_index].n_un.n_strx) == 0) { |
| 24 | + int kr = mach_vm_protect(mach_task_self(), (mach_vm_address_t)sym_ptrs, sym_sec->size, FALSE, |
| 25 | + VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY); |
| 26 | + if (kr != 0) return 1; |
| 27 | + sym_ptrs[i] = replacement; |
| 28 | + return 0; |
| 29 | + } |
| 30 | + } |
| 31 | + return 1; |
| 32 | +} |
| 33 | + |
| 34 | +int tiny_interpose(uint32_t image_index, const char *symbol_name, void *replacement) { |
| 35 | + intptr_t image_slide = _dyld_get_image_vmaddr_slide(image_index); |
| 36 | + struct mach_header_64 *mh_header = (struct mach_header_64 *)_dyld_get_image_header(image_index); |
| 37 | + struct load_command *ld_command = (void *)mh_header + sizeof(struct mach_header_64); |
| 38 | + struct section_64 *nl_sym_sect = NULL; |
| 39 | + struct section_64 *la_sym_sect = NULL; |
| 40 | + struct symtab_command *symtab_cmd = NULL; |
| 41 | + struct dysymtab_command *dysymtab_cmd = NULL; |
| 42 | + struct segment_command_64 *linkedit_cmd = NULL; |
| 43 | + for (int i = 0; i < mh_header->ncmds; i++) { |
| 44 | + if (ld_command->cmd == LC_SEGMENT_64) { |
| 45 | + const struct segment_command_64 *segment = (struct segment_command_64 *)ld_command; |
| 46 | + if (strcmp(segment->segname, "__DATA_CONST") == 0) { |
| 47 | + if (nl_sym_sect) continue; |
| 48 | + struct section_64 *data_const_sect = (void *)(segment + 1); |
| 49 | + for (int i = 0; i < segment->nsects; i++) { |
| 50 | + if ((data_const_sect[i].flags & SECTION_TYPE) == S_NON_LAZY_SYMBOL_POINTERS) { |
| 51 | + nl_sym_sect = data_const_sect + i; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + else if (strcmp(segment->segname, "__DATA") == 0) { |
| 56 | + if (la_sym_sect) continue; |
| 57 | + struct section_64 *data_sect = (void *)(segment + 1); |
| 58 | + for (int i = 0; i < segment->nsects; i++) { |
| 59 | + if ((data_sect[i].flags & SECTION_TYPE) == S_LAZY_SYMBOL_POINTERS) { |
| 60 | + la_sym_sect = data_sect + i; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + else if (strcmp(segment->segname, "__LINKEDIT") == 0) { |
| 65 | + linkedit_cmd = (struct segment_command_64 *)ld_command; |
| 66 | + } |
| 67 | + } |
| 68 | + else if (ld_command->cmd == LC_SYMTAB) { |
| 69 | + symtab_cmd = (struct symtab_command *)ld_command; |
| 70 | + } |
| 71 | + else if (ld_command->cmd == LC_DYSYMTAB) { |
| 72 | + dysymtab_cmd = (struct dysymtab_command *)ld_command; |
| 73 | + } |
| 74 | + ld_command = (void *)ld_command + ld_command->cmdsize; |
| 75 | + } |
| 76 | + intptr_t linkedit_base = image_slide + linkedit_cmd->vmaddr - linkedit_cmd->fileoff; |
| 77 | + char *str_tbl = (void *)linkedit_base + symtab_cmd->stroff; |
| 78 | + struct nlist_64 *nl_tbl = (void *)linkedit_base + symtab_cmd->symoff; |
| 79 | + uint32_t *indirect_sym_tbl = (void *)linkedit_base + dysymtab_cmd->indirectsymoff; |
| 80 | + |
| 81 | + if ((update_pointer(nl_sym_sect, image_slide, str_tbl, nl_tbl, indirect_sym_tbl, symbol_name, replacement) != 0) && |
| 82 | + (update_pointer(la_sym_sect, image_slide, str_tbl, nl_tbl, indirect_sym_tbl, symbol_name, replacement) != 0)) { |
| 83 | + LOG_ERROR("tiny_interpose: no matching indirect symbol found!"); |
| 84 | + return 1; |
| 85 | + } |
| 86 | + return 0; |
| 87 | +} |
0 commit comments