|
| 1 | +// Copyright (c) 2013, Facebook, Inc. |
| 2 | +// All rights reserved. |
| 3 | +// Redistribution and use in source and binary forms, with or without |
| 4 | +// modification, are permitted provided that the following conditions are met: |
| 5 | +// * Redistributions of source code must retain the above copyright notice, |
| 6 | +// this list of conditions and the following disclaimer. |
| 7 | +// * Redistributions in binary form must reproduce the above copyright notice, |
| 8 | +// this list of conditions and the following disclaimer in the documentation |
| 9 | +// and/or other materials provided with the distribution. |
| 10 | +// * Neither the name Facebook nor the names of its contributors may be used to |
| 11 | +// endorse or promote products derived from this software without specific |
| 12 | +// prior written permission. |
| 13 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 14 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 16 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 17 | +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 18 | +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 19 | +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 20 | +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 21 | +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 22 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | + |
| 24 | +#include "fishhook.h" |
| 25 | + |
| 26 | +#include <dlfcn.h> |
| 27 | +#include <stdbool.h> |
| 28 | +#include <stdlib.h> |
| 29 | +#include <string.h> |
| 30 | +#include <sys/mman.h> |
| 31 | +#include <sys/types.h> |
| 32 | +#include <mach/mach.h> |
| 33 | +#include <mach/vm_map.h> |
| 34 | +#include <mach/vm_region.h> |
| 35 | +#include <mach-o/dyld.h> |
| 36 | +#include <mach-o/loader.h> |
| 37 | +#include <mach-o/nlist.h> |
| 38 | + |
| 39 | +#ifdef __LP64__ |
| 40 | +typedef struct mach_header_64 mach_header_t; |
| 41 | +typedef struct segment_command_64 segment_command_t; |
| 42 | +typedef struct section_64 section_t; |
| 43 | +typedef struct nlist_64 nlist_t; |
| 44 | +#define LC_SEGMENT_ARCH_DEPENDENT LC_SEGMENT_64 |
| 45 | +#else |
| 46 | +typedef struct mach_header mach_header_t; |
| 47 | +typedef struct segment_command segment_command_t; |
| 48 | +typedef struct section section_t; |
| 49 | +typedef struct nlist nlist_t; |
| 50 | +#define LC_SEGMENT_ARCH_DEPENDENT LC_SEGMENT |
| 51 | +#endif |
| 52 | + |
| 53 | +#ifndef SEG_DATA_CONST |
| 54 | +#define SEG_DATA_CONST "__DATA_CONST" |
| 55 | +#endif |
| 56 | + |
| 57 | +struct rebindings_entry { |
| 58 | + struct rebinding *rebindings; |
| 59 | + size_t rebindings_nel; |
| 60 | + struct rebindings_entry *next; |
| 61 | +}; |
| 62 | + |
| 63 | +static struct rebindings_entry *_rebindings_head; |
| 64 | + |
| 65 | +/// @return 0 on success |
| 66 | +static int prepend_rebindings(struct rebindings_entry **rebindings_head, |
| 67 | + struct rebinding rebindings[], |
| 68 | + size_t nel) { |
| 69 | + struct rebindings_entry *new_entry = (struct rebindings_entry *) malloc(sizeof(struct rebindings_entry)); |
| 70 | + if (!new_entry) { |
| 71 | + return -1; |
| 72 | + } |
| 73 | + |
| 74 | + new_entry->rebindings = (struct rebinding *) malloc(sizeof(struct rebinding) * nel); |
| 75 | + if (!new_entry->rebindings) { |
| 76 | + free(new_entry); |
| 77 | + return -1; |
| 78 | + } |
| 79 | + |
| 80 | + memcpy(new_entry->rebindings, rebindings, sizeof(struct rebinding) * nel); |
| 81 | + new_entry->rebindings_nel = nel; |
| 82 | + new_entry->next = *rebindings_head; |
| 83 | + *rebindings_head = new_entry; |
| 84 | + |
| 85 | + return 0; |
| 86 | +} |
| 87 | + |
| 88 | +static vm_prot_t get_protection(void *sectionStart) { |
| 89 | + mach_port_t task = mach_task_self(); |
| 90 | + vm_size_t size = 0; |
| 91 | + vm_address_t address = (vm_address_t)sectionStart; |
| 92 | + memory_object_name_t object; |
| 93 | +#if __LP64__ |
| 94 | + mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64; |
| 95 | + vm_region_basic_info_data_64_t info; |
| 96 | + kern_return_t info_ret = vm_region_64( |
| 97 | + task, &address, &size, VM_REGION_BASIC_INFO_64, |
| 98 | + (vm_region_info_64_t)&info, &count, &object |
| 99 | + ); |
| 100 | +#else |
| 101 | + mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT; |
| 102 | + vm_region_basic_info_data_t info; |
| 103 | + kern_return_t info_ret = vm_region( |
| 104 | + task, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)&info, &count, &object |
| 105 | + ); |
| 106 | +#endif |
| 107 | + if (info_ret == KERN_SUCCESS) { |
| 108 | + return info.protection; |
| 109 | + } else { |
| 110 | + return VM_PROT_READ; |
| 111 | + } |
| 112 | +} |
| 113 | +static void perform_rebinding_with_section(struct rebindings_entry *rebindings, |
| 114 | + section_t *section, |
| 115 | + intptr_t slide, |
| 116 | + nlist_t *symtab, |
| 117 | + char *strtab, |
| 118 | + uint32_t *indirect_symtab) { |
| 119 | + const bool isDataConst = strcmp(section->segname, "__DATA_CONST") == 0; |
| 120 | + uint32_t *indirect_symbol_indices = indirect_symtab + section->reserved1; |
| 121 | + void **indirect_symbol_bindings = (void **)((uintptr_t)slide + section->addr); |
| 122 | + vm_prot_t oldProtection = VM_PROT_READ; |
| 123 | + |
| 124 | + if (isDataConst) { |
| 125 | + oldProtection = get_protection(rebindings); |
| 126 | + mprotect(indirect_symbol_bindings, section->size, PROT_READ | PROT_WRITE); |
| 127 | + } |
| 128 | + |
| 129 | + for (uint i = 0; i < section->size / sizeof(void *); i++) { |
| 130 | + uint32_t symtab_index = indirect_symbol_indices[i]; |
| 131 | + |
| 132 | + if (symtab_index == INDIRECT_SYMBOL_ABS || symtab_index == INDIRECT_SYMBOL_LOCAL || |
| 133 | + symtab_index == (INDIRECT_SYMBOL_LOCAL | INDIRECT_SYMBOL_ABS)) { |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + uint32_t strtab_offset = symtab[symtab_index].n_un.n_strx; |
| 138 | + char *symbol_name = strtab + strtab_offset; |
| 139 | + bool symbol_name_longer_than_1 = symbol_name[0] && symbol_name[1]; |
| 140 | + struct rebindings_entry *cur = rebindings; |
| 141 | + |
| 142 | + while (cur) { |
| 143 | + for (uint j = 0; j < cur->rebindings_nel; j++) { |
| 144 | + if (symbol_name_longer_than_1 && |
| 145 | + strcmp(&symbol_name[1], cur->rebindings[j].name) == 0) { |
| 146 | + |
| 147 | + if (cur->rebindings[j].replaced != NULL && |
| 148 | + indirect_symbol_bindings[i] != cur->rebindings[j].replacement) { |
| 149 | + |
| 150 | + *(cur->rebindings[j].replaced) = indirect_symbol_bindings[i]; |
| 151 | + } |
| 152 | + |
| 153 | + indirect_symbol_bindings[i] = cur->rebindings[j].replacement; |
| 154 | + goto symbol_loop; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + cur = cur->next; |
| 159 | + } |
| 160 | + |
| 161 | + symbol_loop:; |
| 162 | + } |
| 163 | + |
| 164 | + if (isDataConst) { |
| 165 | + int protection = 0; |
| 166 | + if (oldProtection & VM_PROT_READ) { |
| 167 | + protection |= PROT_READ; |
| 168 | + } |
| 169 | + if (oldProtection & VM_PROT_WRITE) { |
| 170 | + protection |= PROT_WRITE; |
| 171 | + } |
| 172 | + if (oldProtection & VM_PROT_EXECUTE) { |
| 173 | + protection |= PROT_EXEC; |
| 174 | + } |
| 175 | + |
| 176 | + mprotect(indirect_symbol_bindings, section->size, protection); |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +static void rebind_symbols_for_image(struct rebindings_entry *rebindings, |
| 181 | + const struct mach_header *header, |
| 182 | + intptr_t slide) { |
| 183 | + Dl_info info; |
| 184 | + if (dladdr(header, &info) == 0) { |
| 185 | + return; |
| 186 | + } |
| 187 | + |
| 188 | + segment_command_t *cur_seg_cmd; |
| 189 | + segment_command_t *linkedit_segment = NULL; |
| 190 | + struct symtab_command* symtab_cmd = NULL; |
| 191 | + struct dysymtab_command* dysymtab_cmd = NULL; |
| 192 | + |
| 193 | + uintptr_t cur = (uintptr_t)header + sizeof(mach_header_t); |
| 194 | + for (uint i = 0; i < header->ncmds; i++, cur += cur_seg_cmd->cmdsize) { |
| 195 | + cur_seg_cmd = (segment_command_t *)cur; |
| 196 | + |
| 197 | + if (cur_seg_cmd->cmd == LC_SEGMENT_ARCH_DEPENDENT) { |
| 198 | + if (strcmp(cur_seg_cmd->segname, SEG_LINKEDIT) == 0) { |
| 199 | + linkedit_segment = cur_seg_cmd; |
| 200 | + } |
| 201 | + } else if (cur_seg_cmd->cmd == LC_SYMTAB) { |
| 202 | + symtab_cmd = (struct symtab_command*)cur_seg_cmd; |
| 203 | + } else if (cur_seg_cmd->cmd == LC_DYSYMTAB) { |
| 204 | + dysymtab_cmd = (struct dysymtab_command*)cur_seg_cmd; |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + if (!symtab_cmd || !dysymtab_cmd || !linkedit_segment || |
| 209 | + !dysymtab_cmd->nindirectsyms) { |
| 210 | + return; |
| 211 | + } |
| 212 | + |
| 213 | + // Find base symbol/string table addresses |
| 214 | + uintptr_t linkedit_base = (uintptr_t)slide + linkedit_segment->vmaddr - linkedit_segment->fileoff; |
| 215 | + nlist_t *symtab = (nlist_t *)(linkedit_base + symtab_cmd->symoff); |
| 216 | + char *strtab = (char *)(linkedit_base + symtab_cmd->stroff); |
| 217 | + |
| 218 | + // Get indirect symbol table (array of uint32_t indices into symbol table) |
| 219 | + uint32_t *indirect_symtab = (uint32_t *)(linkedit_base + dysymtab_cmd->indirectsymoff); |
| 220 | + |
| 221 | + cur = (uintptr_t)header + sizeof(mach_header_t); |
| 222 | + for (uint i = 0; i < header->ncmds; i++, cur += cur_seg_cmd->cmdsize) { |
| 223 | + cur_seg_cmd = (segment_command_t *)cur; |
| 224 | + |
| 225 | + if (cur_seg_cmd->cmd == LC_SEGMENT_ARCH_DEPENDENT) { |
| 226 | + if (strcmp(cur_seg_cmd->segname, SEG_DATA) != 0 && |
| 227 | + strcmp(cur_seg_cmd->segname, SEG_DATA_CONST) != 0) { |
| 228 | + continue; |
| 229 | + } |
| 230 | + |
| 231 | + for (uint j = 0; j < cur_seg_cmd->nsects; j++) { |
| 232 | + section_t *sect = (section_t *)(cur + sizeof(segment_command_t)) + j; |
| 233 | + |
| 234 | + if ((sect->flags & SECTION_TYPE) == S_LAZY_SYMBOL_POINTERS) { |
| 235 | + perform_rebinding_with_section( |
| 236 | + rebindings, sect, slide, symtab, strtab, indirect_symtab |
| 237 | + ); |
| 238 | + } |
| 239 | + if ((sect->flags & SECTION_TYPE) == S_NON_LAZY_SYMBOL_POINTERS) { |
| 240 | + perform_rebinding_with_section( |
| 241 | + rebindings, sect, slide, symtab, strtab, indirect_symtab |
| 242 | + ); |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | +} |
| 248 | + |
| 249 | +static void _rebind_symbols_for_image(const struct mach_header *header, |
| 250 | + intptr_t slide) { |
| 251 | + rebind_symbols_for_image(_rebindings_head, header, slide); |
| 252 | +} |
| 253 | + |
| 254 | +int rebind_symbols_image(void *header, |
| 255 | + intptr_t slide, |
| 256 | + struct rebinding rebindings[], |
| 257 | + size_t rebindings_nel) { |
| 258 | + struct rebindings_entry *rebindings_head = NULL; |
| 259 | + |
| 260 | + int retval = prepend_rebindings(&rebindings_head, rebindings, rebindings_nel); |
| 261 | + rebind_symbols_for_image(rebindings_head, (const struct mach_header *) header, slide); |
| 262 | + |
| 263 | + if (rebindings_head) { |
| 264 | + free(rebindings_head->rebindings); |
| 265 | + } |
| 266 | + |
| 267 | + free(rebindings_head); |
| 268 | + return retval; |
| 269 | +} |
| 270 | + |
| 271 | +/// @return 0 on success |
| 272 | +int rebind_symbols(struct rebinding rebindings[], size_t rebindings_nel) { |
| 273 | + int retval = prepend_rebindings(&_rebindings_head, rebindings, rebindings_nel); |
| 274 | + if (retval < 0) { |
| 275 | + return retval; |
| 276 | + } |
| 277 | + |
| 278 | + // If this was the first call, register callback for image additions (which is also invoked for |
| 279 | + // existing images, otherwise, just run on existing images |
| 280 | + if (!_rebindings_head->next) { |
| 281 | + _dyld_register_func_for_add_image(_rebind_symbols_for_image); |
| 282 | + } else { |
| 283 | + uint32_t c = _dyld_image_count(); |
| 284 | + for (uint32_t i = 0; i < c; i++) { |
| 285 | + _rebind_symbols_for_image(_dyld_get_image_header(i), _dyld_get_image_vmaddr_slide(i)); |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + return retval; |
| 290 | +} |
0 commit comments