-
Notifications
You must be signed in to change notification settings - Fork 211
/
util.c
266 lines (219 loc) · 6.66 KB
/
util.c
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
#include "common.h"
#include <linux/slab.h>
#include <asm/cacheflush.h>
#include <linux/kallsyms.h>
#if defined(_CONFIG_ARM_) && defined(CONFIG_STRICT_MEMORY_RWX)
#include <asm/mmu_writeable.h>
#endif
#if defined(_CONFIG_X86_)
#define HIJACK_SIZE 6
#elif defined(_CONFIG_X86_64_)
#define HIJACK_SIZE 12
#else // ARM
#define HIJACK_SIZE 12
#endif
struct sym_hook {
void *addr;
unsigned char o_code[HIJACK_SIZE];
unsigned char n_code[HIJACK_SIZE];
struct list_head list;
};
struct ksym {
char *name;
unsigned long addr;
};
LIST_HEAD(hooked_syms);
#if defined(_CONFIG_X86_) || defined(_CONFIG_X86_64_)
// Thanks Dan
inline unsigned long disable_wp ( void )
{
unsigned long cr0;
preempt_disable();
barrier();
cr0 = read_cr0();
write_cr0(cr0 & ~X86_CR0_WP);
return cr0;
}
inline void restore_wp ( unsigned long cr0 )
{
write_cr0(cr0);
barrier();
preempt_enable();
}
#else // ARM
void cacheflush ( void *begin, unsigned long size )
{
flush_icache_range((unsigned long)begin, (unsigned long)begin + size);
}
# if defined(CONFIG_STRICT_MEMORY_RWX)
inline void arm_write_hook ( void *target, char *code )
{
unsigned long *target_arm = (unsigned long *)target;
unsigned long *code_arm = (unsigned long *)code;
// We should have something more generalized here, but we'll
// get away with it since the ARM hook is always 12 bytes
mem_text_write_kernel_word(target_arm, *code_arm);
mem_text_write_kernel_word(target_arm + 1, *(code_arm + 1));
mem_text_write_kernel_word(target_arm + 2, *(code_arm + 2));
}
# else
inline void arm_write_hook ( void *target, char *code )
{
memcpy(target, code, HIJACK_SIZE);
cacheflush(target, HIJACK_SIZE);
}
# endif
#endif
void hijack_start ( void *target, void *new )
{
struct sym_hook *sa;
unsigned char o_code[HIJACK_SIZE], n_code[HIJACK_SIZE];
#if defined(_CONFIG_X86_)
unsigned long o_cr0;
// push $addr; ret
memcpy(n_code, "\x68\x00\x00\x00\x00\xc3", HIJACK_SIZE);
*(unsigned long *)&n_code[1] = (unsigned long)new;
#elif defined(_CONFIG_X86_64_)
unsigned long o_cr0;
// mov rax, $addr; jmp rax
memcpy(n_code, "\x48\xb8\x00\x00\x00\x00\x00\x00\x00\x00\xff\xe0", HIJACK_SIZE);
*(unsigned long *)&n_code[2] = (unsigned long)new;
#else // ARM
if ( (unsigned long)target % 4 == 0 )
{
// ldr pc, [pc, #0]; .long addr; .long addr
memcpy(n_code, "\x00\xf0\x9f\xe5\x00\x00\x00\x00\x00\x00\x00\x00", HIJACK_SIZE);
*(unsigned long *)&n_code[4] = (unsigned long)new;
*(unsigned long *)&n_code[8] = (unsigned long)new;
}
else // Thumb
{
// add r0, pc, #4; ldr r0, [r0, #0]; mov pc, r0; mov pc, r0; .long addr
memcpy(n_code, "\x01\xa0\x00\x68\x87\x46\x87\x46\x00\x00\x00\x00", HIJACK_SIZE);
*(unsigned long *)&n_code[8] = (unsigned long)new;
target--;
}
#endif
DEBUG_HOOK("Hooking function 0x%p with 0x%p\n", target, new);
memcpy(o_code, target, HIJACK_SIZE);
#if defined(_CONFIG_X86_) || defined(_CONFIG_X86_64_)
o_cr0 = disable_wp();
memcpy(target, n_code, HIJACK_SIZE);
restore_wp(o_cr0);
#else // ARM
arm_write_hook(target, n_code);
#endif
sa = kmalloc(sizeof(*sa), GFP_KERNEL);
if ( ! sa )
return;
sa->addr = target;
memcpy(sa->o_code, o_code, HIJACK_SIZE);
memcpy(sa->n_code, n_code, HIJACK_SIZE);
list_add(&sa->list, &hooked_syms);
}
void hijack_pause ( void *target )
{
struct sym_hook *sa;
DEBUG_HOOK("Pausing function hook 0x%p\n", target);
list_for_each_entry ( sa, &hooked_syms, list )
if ( target == sa->addr )
{
#if defined(_CONFIG_X86_) || defined(_CONFIG_X86_64_)
unsigned long o_cr0 = disable_wp();
memcpy(target, sa->o_code, HIJACK_SIZE);
restore_wp(o_cr0);
#else // ARM
arm_write_hook(target, sa->o_code);
#endif
}
}
void hijack_resume ( void *target )
{
struct sym_hook *sa;
DEBUG_HOOK("Resuming function hook 0x%p\n", target);
list_for_each_entry ( sa, &hooked_syms, list )
if ( target == sa->addr )
{
#if defined(_CONFIG_X86_) || defined(_CONFIG_X86_64_)
unsigned long o_cr0 = disable_wp();
memcpy(target, sa->n_code, HIJACK_SIZE);
restore_wp(o_cr0);
#else // ARM
arm_write_hook(target, sa->n_code);
#endif
}
}
void hijack_stop ( void *target )
{
struct sym_hook *sa;
DEBUG_HOOK("Unhooking function 0x%p\n", target);
list_for_each_entry ( sa, &hooked_syms, list )
if ( target == sa->addr )
{
#if defined(_CONFIG_X86_) || defined(_CONFIG_X86_64_)
unsigned long o_cr0 = disable_wp();
memcpy(target, sa->o_code, HIJACK_SIZE);
restore_wp(o_cr0);
#else // ARM
arm_write_hook(target, sa->o_code);
#endif
list_del(&sa->list);
kfree(sa);
break;
}
}
char *strnstr ( const char *haystack, const char *needle, size_t n )
{
char *s = strstr(haystack, needle);
if ( s == NULL )
return NULL;
if ( s - haystack + strlen(needle) <= n )
return s;
else
return NULL;
}
void *memmem ( const void *haystack, size_t haystack_size, const void *needle, size_t needle_size )
{
char *p;
for ( p = (char *)haystack; p <= ((char *)haystack - needle_size + haystack_size); p++ )
if ( memcmp(p, needle, needle_size) == 0 )
return (void *)p;
return NULL;
}
void *memstr ( const void *haystack, const char *needle, size_t size )
{
char *p;
size_t needle_size = strlen(needle);
for ( p = (char *)haystack; p <= ((char *)haystack - needle_size + size); p++ )
if ( memcmp(p, needle, needle_size) == 0 )
return (void *)p;
return NULL;
}
int find_ksym ( void *data, const char *name, struct module *module, unsigned long address )
{
struct ksym *ksym = (struct ksym *)data;
char *target = ksym->name;
if ( strncmp(target, name, KSYM_NAME_LEN) == 0 )
{
ksym->addr = address;
return 1;
}
return 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
unsigned long get_symbol ( char *name )
{
unsigned long symbol = 0;
struct ksym ksym;
/*
* kallsyms_lookup_name() is re-exported in 2.6.33, but there's no real
* benefit to using it instead of kallsyms_on_each_symbol(). We also get
* to remove one more LINUX_VERSION_CODE check.
*/
ksym.name = name;
ksym.addr = 0;
kallsyms_on_each_symbol(&find_ksym, &ksym);
symbol = ksym.addr;
return symbol;
}
#endif