flex_fishhook.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. #include "flex_fishhook.h"
  24. #include <dlfcn.h>
  25. #include <stdbool.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/mman.h>
  29. #include <sys/types.h>
  30. #include <mach/mach.h>
  31. #include <mach/vm_map.h>
  32. #include <mach/vm_region.h>
  33. #include <mach-o/dyld.h>
  34. #include <mach-o/loader.h>
  35. #include <mach-o/nlist.h>
  36. #ifdef __LP64__
  37. typedef struct mach_header_64 mach_header_t;
  38. typedef struct segment_command_64 segment_command_t;
  39. typedef struct section_64 section_t;
  40. typedef struct nlist_64 nlist_t;
  41. #define LC_SEGMENT_ARCH_DEPENDENT LC_SEGMENT_64
  42. #else
  43. typedef struct mach_header mach_header_t;
  44. typedef struct segment_command segment_command_t;
  45. typedef struct section section_t;
  46. typedef struct nlist nlist_t;
  47. #define LC_SEGMENT_ARCH_DEPENDENT LC_SEGMENT
  48. #endif
  49. #ifndef SEG_DATA_CONST
  50. #define SEG_DATA_CONST "__DATA_CONST"
  51. #endif
  52. struct rebindings_entry {
  53. struct rebinding *rebindings;
  54. size_t rebindings_nel;
  55. struct rebindings_entry *next;
  56. };
  57. static struct rebindings_entry *_flex_rebindings_head;
  58. /// @return 0 on success
  59. static int flex_prepend_rebindings(struct rebindings_entry **rebindings_head,
  60. struct rebinding rebindings[],
  61. size_t nel) {
  62. struct rebindings_entry *new_entry = (struct rebindings_entry *) malloc(sizeof(struct rebindings_entry));
  63. if (!new_entry) {
  64. return -1;
  65. }
  66. new_entry->rebindings = (struct rebinding *) malloc(sizeof(struct rebinding) * nel);
  67. if (!new_entry->rebindings) {
  68. free(new_entry);
  69. return -1;
  70. }
  71. memcpy(new_entry->rebindings, rebindings, sizeof(struct rebinding) * nel);
  72. new_entry->rebindings_nel = nel;
  73. new_entry->next = *rebindings_head;
  74. *rebindings_head = new_entry;
  75. return 0;
  76. }
  77. static vm_prot_t flex_get_protection(void *sectionStart) {
  78. mach_port_t task = mach_task_self();
  79. vm_size_t size = 0;
  80. vm_address_t address = (vm_address_t)sectionStart;
  81. memory_object_name_t object;
  82. #if __LP64__
  83. mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64;
  84. vm_region_basic_info_data_64_t info;
  85. kern_return_t info_ret = vm_region_64(
  86. task, &address, &size, VM_REGION_BASIC_INFO_64,
  87. (vm_region_info_64_t)&info, &count, &object
  88. );
  89. #else
  90. mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT;
  91. vm_region_basic_info_data_t info;
  92. kern_return_t info_ret = vm_region(
  93. task, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)&info, &count, &object
  94. );
  95. #endif
  96. if (info_ret == KERN_SUCCESS) {
  97. return info.protection;
  98. } else {
  99. return VM_PROT_READ;
  100. }
  101. }
  102. static void flex_perform_rebinding_with_section(struct rebindings_entry *rebindings,
  103. section_t *section,
  104. intptr_t slide,
  105. nlist_t *symtab,
  106. char *strtab,
  107. uint32_t *indirect_symtab) {
  108. const bool isDataConst = strcmp(section->segname, "__DATA_CONST") == 0;
  109. uint32_t *indirect_symbol_indices = indirect_symtab + section->reserved1;
  110. void **indirect_symbol_bindings = (void **)((uintptr_t)slide + section->addr);
  111. vm_prot_t oldProtection = VM_PROT_READ;
  112. if (isDataConst) {
  113. oldProtection = flex_get_protection(rebindings);
  114. mprotect(indirect_symbol_bindings, section->size, PROT_READ | PROT_WRITE);
  115. }
  116. for (uint i = 0; i < section->size / sizeof(void *); i++) {
  117. uint32_t symtab_index = indirect_symbol_indices[i];
  118. if (symtab_index == INDIRECT_SYMBOL_ABS || symtab_index == INDIRECT_SYMBOL_LOCAL ||
  119. symtab_index == (INDIRECT_SYMBOL_LOCAL | INDIRECT_SYMBOL_ABS)) {
  120. continue;
  121. }
  122. uint32_t strtab_offset = symtab[symtab_index].n_un.n_strx;
  123. char *symbol_name = strtab + strtab_offset;
  124. bool symbol_name_longer_than_1 = symbol_name[0] && symbol_name[1];
  125. struct rebindings_entry *cur = rebindings;
  126. while (cur) {
  127. for (uint j = 0; j < cur->rebindings_nel; j++) {
  128. if (symbol_name_longer_than_1 &&
  129. strcmp(&symbol_name[1], cur->rebindings[j].name) == 0) {
  130. if (cur->rebindings[j].replaced != NULL &&
  131. indirect_symbol_bindings[i] != cur->rebindings[j].replacement) {
  132. *(cur->rebindings[j].replaced) = indirect_symbol_bindings[i];
  133. }
  134. indirect_symbol_bindings[i] = cur->rebindings[j].replacement;
  135. goto symbol_loop;
  136. }
  137. }
  138. cur = cur->next;
  139. }
  140. symbol_loop:;
  141. }
  142. if (isDataConst) {
  143. int protection = 0;
  144. if (oldProtection & VM_PROT_READ) {
  145. protection |= PROT_READ;
  146. }
  147. if (oldProtection & VM_PROT_WRITE) {
  148. protection |= PROT_WRITE;
  149. }
  150. if (oldProtection & VM_PROT_EXECUTE) {
  151. protection |= PROT_EXEC;
  152. }
  153. mprotect(indirect_symbol_bindings, section->size, protection);
  154. }
  155. }
  156. static void flex_rebind_symbols_for_image(struct rebindings_entry *rebindings,
  157. const struct mach_header *header,
  158. intptr_t slide) {
  159. Dl_info info;
  160. if (dladdr(header, &info) == 0) {
  161. return;
  162. }
  163. segment_command_t *cur_seg_cmd;
  164. segment_command_t *linkedit_segment = NULL;
  165. struct symtab_command* symtab_cmd = NULL;
  166. struct dysymtab_command* dysymtab_cmd = NULL;
  167. uintptr_t cur = (uintptr_t)header + sizeof(mach_header_t);
  168. for (uint i = 0; i < header->ncmds; i++, cur += cur_seg_cmd->cmdsize) {
  169. cur_seg_cmd = (segment_command_t *)cur;
  170. if (cur_seg_cmd->cmd == LC_SEGMENT_ARCH_DEPENDENT) {
  171. if (strcmp(cur_seg_cmd->segname, SEG_LINKEDIT) == 0) {
  172. linkedit_segment = cur_seg_cmd;
  173. }
  174. } else if (cur_seg_cmd->cmd == LC_SYMTAB) {
  175. symtab_cmd = (struct symtab_command*)cur_seg_cmd;
  176. } else if (cur_seg_cmd->cmd == LC_DYSYMTAB) {
  177. dysymtab_cmd = (struct dysymtab_command*)cur_seg_cmd;
  178. }
  179. }
  180. if (!symtab_cmd || !dysymtab_cmd || !linkedit_segment ||
  181. !dysymtab_cmd->nindirectsyms) {
  182. return;
  183. }
  184. // Find base symbol/string table addresses
  185. uintptr_t linkedit_base = (uintptr_t)slide + linkedit_segment->vmaddr - linkedit_segment->fileoff;
  186. nlist_t *symtab = (nlist_t *)(linkedit_base + symtab_cmd->symoff);
  187. char *strtab = (char *)(linkedit_base + symtab_cmd->stroff);
  188. // Get indirect symbol table (array of uint32_t indices into symbol table)
  189. uint32_t *indirect_symtab = (uint32_t *)(linkedit_base + dysymtab_cmd->indirectsymoff);
  190. cur = (uintptr_t)header + sizeof(mach_header_t);
  191. for (uint i = 0; i < header->ncmds; i++, cur += cur_seg_cmd->cmdsize) {
  192. cur_seg_cmd = (segment_command_t *)cur;
  193. if (cur_seg_cmd->cmd == LC_SEGMENT_ARCH_DEPENDENT) {
  194. if (strcmp(cur_seg_cmd->segname, SEG_DATA) != 0 &&
  195. strcmp(cur_seg_cmd->segname, SEG_DATA_CONST) != 0) {
  196. continue;
  197. }
  198. for (uint j = 0; j < cur_seg_cmd->nsects; j++) {
  199. section_t *sect = (section_t *)(cur + sizeof(segment_command_t)) + j;
  200. if ((sect->flags & SECTION_TYPE) == S_LAZY_SYMBOL_POINTERS) {
  201. flex_perform_rebinding_with_section(
  202. rebindings, sect, slide, symtab, strtab, indirect_symtab
  203. );
  204. }
  205. if ((sect->flags & SECTION_TYPE) == S_NON_LAZY_SYMBOL_POINTERS) {
  206. flex_perform_rebinding_with_section(
  207. rebindings, sect, slide, symtab, strtab, indirect_symtab
  208. );
  209. }
  210. }
  211. }
  212. }
  213. }
  214. static void _flex_rebind_symbols_for_image(const struct mach_header *header,
  215. intptr_t slide) {
  216. flex_rebind_symbols_for_image(_flex_rebindings_head, header, slide);
  217. }
  218. int flex_rebind_symbols_image(void *header,
  219. intptr_t slide,
  220. struct rebinding rebindings[],
  221. size_t rebindings_nel) {
  222. struct rebindings_entry *rebindings_head = NULL;
  223. int retval = flex_prepend_rebindings(&rebindings_head, rebindings, rebindings_nel);
  224. flex_rebind_symbols_for_image(rebindings_head, (const struct mach_header *) header, slide);
  225. if (rebindings_head) {
  226. free(rebindings_head->rebindings);
  227. }
  228. free(rebindings_head);
  229. return retval;
  230. }
  231. /// @return 0 on success
  232. int flex_rebind_symbols(struct rebinding rebindings[], size_t rebindings_nel) {
  233. int retval = flex_prepend_rebindings(&_flex_rebindings_head, rebindings, rebindings_nel);
  234. if (retval < 0) {
  235. return retval;
  236. }
  237. // If this was the first call, register callback for image additions (which is also invoked for
  238. // existing images, otherwise, just run on existing images
  239. if (!_flex_rebindings_head->next) {
  240. _dyld_register_func_for_add_image(_flex_rebind_symbols_for_image);
  241. } else {
  242. uint32_t c = _dyld_image_count();
  243. for (uint32_t i = 0; i < c; i++) {
  244. _flex_rebind_symbols_for_image(_dyld_get_image_header(i), _dyld_get_image_vmaddr_slide(i));
  245. }
  246. }
  247. return retval;
  248. }