Skip to content

Commit a834186

Browse files
NSExceptionalTim Johnsen
authored and
Tim Johnsen
committed
Add fishhook, disable OS log — close FLEXTool#372
iOS 10 and its associated SDK deprecated *ASL and replaced it with *os_log. This change is widely considered unfavorable and made it extremely tedious for FLEX to intercept log messages reliably. @Ram4096 has brought to my attention that the os_log functionality is actually just a shim which is conditionally enabled based on what SDK version your binary links with. With a little reverse engineering, I was able to hook the function that tells `NSLog` (well, `CFLogv`) whether os_log should be used or not. This commit uses fishhook to hook `os_log_shim_enabled` to always return `NO` so that the old ASL library is used instead. Prior to this commit we had code in place to conditionally intercept messages from os_log or ASL based on the iOS version. These checks are not semantically correct since ASL would still be used on iOS 10+ if the binary was built with the iOS 9 SDK. For now, this doesn't matter going forward since we are going to always use ASL, but it might be worth updating the check to instead check for the linked SDK version instead of the OS version. - *ASL: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/asl.3.html - *os_log: https://developer.apple.com/documentation/os/logging?language=objc
1 parent aa31745 commit a834186

File tree

5 files changed

+410
-18
lines changed

5 files changed

+410
-18
lines changed

Classes/GlobalStateExplorers/SystemLog/FLEXASLLogController.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#if TARGET_IPHONE_SIMULATOR
1414
#define updateInterval 5.0
1515
#else
16-
#define updateInterval 1.0
16+
#define updateInterval 0.5
1717
#endif
1818

1919
@interface FLEXASLLogController ()

Classes/GlobalStateExplorers/SystemLog/FLEXSystemLogViewController.m

+18-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import "FLEXASLLogController.h"
1313
#import "FLEXOSLogController.h"
1414
#import "FLEXSystemLogCell.h"
15+
#import "fishhook.h"
1516

1617
@interface FLEXSystemLogViewController ()
1718

@@ -21,8 +22,24 @@ @interface FLEXSystemLogViewController ()
2122

2223
@end
2324

25+
static BOOL FLEXDidHookNSLog = NO;
26+
void (*orig_os_log_shim_enabled)() = nil;
27+
BOOL my_os_log_shim_enabled() {
28+
return NO;
29+
}
30+
2431
@implementation FLEXSystemLogViewController
2532

33+
+ (void)load {
34+
// Thanks to @Ram4096 on GitHub for telling me that
35+
// os_log is conditionally enabled by the SDK version
36+
FLEXDidHookNSLog = rebind_symbols((struct rebinding[1]) {
37+
"os_log_shim_enabled",
38+
(void *)my_os_log_shim_enabled,
39+
(void **)&orig_os_log_shim_enabled
40+
}, 1) == 0;
41+
}
42+
2643
- (id)init {
2744
return [super initWithStyle:UITableViewStylePlain];
2845
}
@@ -39,7 +56,7 @@ - (void)viewDidLoad {
3956
};
4057

4158
_logMessages = [NSMutableArray array];
42-
if (FLEXOSLogAvailable()) {
59+
if (FLEXOSLogAvailable() && !FLEXDidHookNSLog) {
4360
_logController = [FLEXOSLogController withUpdateHandler:logHandler];
4461
} else {
4562
_logController = [FLEXASLLogController withUpdateHandler:logHandler];

Classes/Utility/Runtime/fishhook.c

+290
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
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

Comments
 (0)