forked from beehive-lab/mambo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
syscalls.c
514 lines (444 loc) · 16 KB
/
syscalls.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
This file is part of MAMBO, a low-overhead dynamic binary modification tool:
https://github.com/beehive-lab/mambo
Copyright 2013-2016 Cosmin Gorgovan <cosmin at linux-geek dot org>
Copyright 2017 The University of Manchester
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stdio.h>
#include <asm/unistd.h>
#include <pthread.h>
#include <sys/mman.h>
#include <unistd.h>
#include <linux/sched.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <inttypes.h>
#include "dbm.h"
#include "kernel_sigaction.h"
#include "scanner_common.h"
#include "syscalls.h"
#ifdef DEBUG
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
#ifdef __aarch64__
#define SIG_FRAG_OFFSET 4
#else
#define SIG_FRAG_OFFSET 0
#endif
void *dbm_start_thread_pth(void *ptr, void *mambo_sp) {
dbm_thread *thread_data = (dbm_thread *)ptr;
assert(thread_data->clone_args->child_stack);
current_thread = thread_data;
current_thread->mambo_sp = mambo_sp;
pid_t tid = syscall(__NR_gettid);
thread_data->tid = tid;
if (thread_data->clone_args->flags & CLONE_PARENT_SETTID) {
*thread_data->clone_args->ptid = tid;
}
if (thread_data->clone_args->flags & CLONE_CHILD_SETTID) {
*thread_data->clone_args->ctid = tid;
}
if (thread_data->clone_args->flags & CLONE_CHILD_CLEARTID) {
syscall(__NR_set_tid_address, thread_data->clone_args->ctid);
}
thread_data->tls = thread_data->clone_args->tls;
// Copy the parent's saved register values to the child's stack
#ifdef __arm__
uint32_t *child_stack = thread_data->clone_args->child_stack;
child_stack -= 15; // reserve 15 words on the child's stack
mambo_memcpy(child_stack, thread_data->clone_args, sizeof(uintptr_t) * 14);
child_stack[r0] = 0; // return 0
#endif
#ifdef __aarch64__
uint64_t *child_stack = thread_data->clone_args->child_stack;
child_stack -= 34;
mambo_memcpy(child_stack, (void *)thread_data->clone_args, sizeof(uintptr_t) * 32);
// move the values for X0 and X1 to the bottom of the stack
child_stack[32] = 0; // X0
child_stack[33] = child_stack[1]; // X1
child_stack += 2;
#endif
// Release the lock
asm volatile("DMB SY" ::: "memory");
*(thread_data->set_tid) = tid;
assert(register_thread(thread_data, false) == 0);
uintptr_t addr = scan(thread_data, thread_data->clone_ret_addr, ALLOCATE_BB);
th_enter(child_stack, addr);
return NULL;
}
dbm_thread *dbm_create_thread(dbm_thread *thread_data, void *next_inst, sys_clone_args *args, volatile pid_t *set_tid) {
pthread_t thread;
dbm_thread *new_thread_data;
if (!allocate_thread_data(&new_thread_data)) {
fprintf(stderr, "Failed to allocate thread data\n");
while(1);
}
init_thread(new_thread_data);
new_thread_data->clone_ret_addr = next_inst;
new_thread_data->set_tid = set_tid;
new_thread_data->clone_args = args;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
/* We're switching to the stack allocated by the application immediately, so make this
as small as possible. Our glibc stores data here, so we can't unmap it.
Also see man pthread_attr_setguardsize BUGS. */
pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 4096);
pthread_attr_setguardsize(&attr, 4096);
pthread_create(&thread, &attr, new_thread_trampoline, new_thread_data);
return new_thread_data;
}
uintptr_t emulate_brk(uintptr_t addr) {
int ret;
// Fast path
if (addr == 0 || addr == global_data.brk) {
return global_data.brk;
}
ret = pthread_mutex_lock(&global_data.brk_mutex);
assert(ret == 0);
/* We use mremap for non-overlapping re-allocation, therefore
we must always always keep at least one allocated page. */
if (addr >= (global_data.initial_brk + PAGE_SIZE)) {
void *map = mremap((void *)global_data.initial_brk,
global_data.brk - global_data.initial_brk,
addr - global_data.initial_brk, 0);
if (map != MAP_FAILED) {
vm_op_t op = VM_MAP;
size_t size = addr - global_data.brk;
if (addr < global_data.brk) {
op = VM_UNMAP;
size = global_data.brk - addr;
}
notify_vm_op(op, min(addr, global_data.brk), size, PROT_READ | PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
global_data.brk = addr;
}
}
ret = pthread_mutex_unlock(&global_data.brk_mutex);
assert(ret == 0);
return global_data.brk;
}
ssize_t readlink_handler(char *sys_path, char *sys_buf, ssize_t bufsize) {
const int proc_buflen = 100;
char buf[proc_buflen];
snprintf(buf, proc_buflen, "/proc/%d/exe", getpid());
if (strcmp(sys_path, buf) == 0 ||
strcmp(sys_path, "/proc/self/exe") == 0 ||
strcmp(sys_path, "/proc/thread-self/exe") == 0) {
char path[PATH_MAX];
char *rp = realpath(global_data.argv[1], path);
assert(rp != NULL);
size_t path_len = strlen(rp);
/* realpath() null-terminates strings, while readlinkat shouldn't.
Therefore, if PATH_MAX has been filled and bufsize == PATH_MAX, then it's possible
that we've lost a valid last character which realpath set to null. */
assert((bufsize < PATH_MAX) || (path_len < (PATH_MAX - 1)));
strncpy(sys_buf, path, bufsize);
return min(path_len, bufsize);
}
return -1;
}
int syscall_handler_pre(uintptr_t syscall_no, uintptr_t *args, uint16_t *next_inst, dbm_thread *thread_data) {
int do_syscall = 1;
sys_clone_args *clone_args;
debug("syscall pre %" PRIdPTR "\n", syscall_no);
#ifdef PLUGINS_NEW
mambo_context ctx;
if (global_data.free_plugin > 0) {
set_mambo_context_syscall(&ctx, thread_data, PRE_SYSCALL_C, syscall_no, args);
mambo_deliver_callbacks_for_ctx(&ctx);
}
if (ctx.syscall.replace) {
do_syscall = 0;
args[0] = ctx.syscall.ret;
} else {
#endif
switch(syscall_no) {
case __NR_brk:
args[0] = emulate_brk(args[0]);
do_syscall = 0;
break;
case __NR_clone:
clone_args = (sys_clone_args *)args;
if (clone_args->flags & CLONE_THREAD) {
assert(clone_args->flags & CLONE_VM);
if (!(clone_args->flags & CLONE_SETTLS)) {
clone_args->tls = thread_data->tls;
}
thread_data->clone_vm = true;
volatile pid_t child_tid = 0;
dbm_create_thread(thread_data, next_inst, clone_args, &child_tid);
while(child_tid == 0);
asm volatile("DMB SY" ::: "memory");
args[0] = child_tid;
do_syscall = 0;
break;
}
if (clone_args->flags & CLONE_VFORK) {
clone_args->flags &= ~CLONE_VM;
}
assert((clone_args->flags & CLONE_VM) == 0);
thread_data->clone_vm = false;
thread_data->child_tls = (clone_args->flags & CLONE_SETTLS) ? clone_args->tls : thread_data->tls;
clone_args->flags &= ~CLONE_SETTLS;
if (clone_args->child_stack != NULL) {
if (clone_args->child_stack == &args[SYSCALL_WRAPPER_STACK_OFFSET]) {
clone_args->child_stack = NULL;
} else {
const size_t copy_size = SYSCALL_WRAPPER_FRAME_SIZE * sizeof(uintptr_t);
clone_args->child_stack -= copy_size;
void *source = args + SYSCALL_WRAPPER_STACK_OFFSET - SYSCALL_WRAPPER_FRAME_SIZE;
mambo_memcpy(clone_args->child_stack, source, copy_size);
}
} // if child_stack != NULL
break;
case __NR_exit:
debug("thread exit\n");
void *sp = thread_data->mambo_sp;
assert(unregister_thread(thread_data, false) == 0);
assert(free_thread_data(thread_data) == 0);
return_with_sp(sp); // this should never return
while(1);
break;
#ifdef __arm__
case __NR_sigaction:
fprintf(stderr, "check sigaction()\n");
while(1);
#endif
case __NR_rt_sigaction: {
uintptr_t handler = 0xdead;
assert(args[3] == 8 && args[0] >= 0 && args[0] < _NSIG);
struct kernel_sigaction *act = (struct kernel_sigaction *)args[1];
if (act != NULL) {
handler = (uintptr_t)act->k_sa_handler;
// Never remove the UNLINK_SIGNAL handler, which is used internally by MAMBO
if (args[0] == UNLINK_SIGNAL || (act->k_sa_handler != SIG_IGN && act->k_sa_handler != SIG_DFL)) {
act->k_sa_handler = (__sighandler_t)signal_trampoline;
act->sa_flags |= SA_SIGINFO;
}
}
// A mutex is used to ensure that changes to the handler and all other options appear atomic
int ret = pthread_mutex_lock(&global_data.signal_handlers_mutex);
assert(ret == 0);
uintptr_t syscall_ret = raw_syscall(syscall_no, args[0], args[1], args[2], args[3]);
if (syscall_ret == 0) {
struct kernel_sigaction *oldact = (struct kernel_sigaction *)args[2];
if (oldact != NULL && oldact->k_sa_handler != SIG_IGN && oldact->k_sa_handler != SIG_DFL) {
oldact->k_sa_handler = (void *)global_data.signal_handlers[args[0]];
}
if (act != NULL) {
global_data.signal_handlers[args[0]] = handler;
}
}
ret = pthread_mutex_unlock(&global_data.signal_handlers_mutex);
assert(ret == 0);
args[0] = syscall_ret;
do_syscall = 0;
break;
}
case __NR_exit_group:
dbm_exit(thread_data, args[0]);
break;
case __NR_close:
if (args[0] <= 2) { // stdin, stdout, stderr
args[0] = 0;
do_syscall = 0;
}
break;
case __NR_readlinkat: {
ssize_t len = readlink_handler((char *)args[1], (char *)args[2], args[3]);
if (len >= 0) {
args[0] = (uintptr_t)len;
do_syscall = 0;
}
break;
}
/* Remove the execute permission from application mappings. At this point, this mostly acts
as a safeguard in case a translation bug causes a branch to unmodified application code.
Page permissions happen to be passed in the third argument both for mmap and mprotect. */
#ifdef __arm__
case __NR_mmap2:
#endif
#ifdef __aarch64__
case __NR_mmap:
#endif
case __NR_mprotect: {
uintptr_t syscall_ret, prot = args[2];
/* Ensure that code pages are readable by the code scanner. */
if (args[2] & PROT_EXEC) {
if (!(args[2] & PROT_READ)) {
debug("MAMBO: adding read permission to executable mapping at 0x%" PRIxPTR "\n", args[0]);
args[2] |= PROT_READ;
}
args[2] &= ~PROT_EXEC;
}
#ifdef __arm__
if (syscall_no == __NR_mmap2) {
#elif __aarch64__
if (syscall_no == __NR_mmap) {
#endif
syscall_ret = raw_syscall(syscall_no, args[0], args[1], args[2], args[3], args[4], args[5]);
if (syscall_ret <= -ERANGE) {
uintptr_t start = align_lower(syscall_ret, PAGE_SIZE);
uintptr_t end = align_higher(syscall_ret + args[1], PAGE_SIZE);
notify_vm_op(VM_MAP, start, end-start, prot, args[3], args[4], args[5]);
}
} else {
assert(syscall_no == __NR_mprotect);
syscall_ret = raw_syscall(syscall_no, args[0], args[1], args[2]);
if (syscall_ret == 0) {
uintptr_t start = align_lower(args[0], PAGE_SIZE);
uintptr_t end = align_higher(args[0] + args[1], PAGE_SIZE);
notify_vm_op(VM_PROT, start, end-start, args[2], 0, -1, 0);
}
}
args[0] = syscall_ret;
do_syscall = 0;
break;
}
case __NR_munmap: {
uintptr_t syscall_ret = raw_syscall(syscall_no, args[0], args[1]);
if (syscall_ret == 0) {
uintptr_t start = align_lower(args[0], PAGE_SIZE);
uintptr_t end = align_higher(args[0] + args[1], PAGE_SIZE);
notify_vm_op(VM_UNMAP, start, end-start, 0, 0, -1, 0);
}
args[0] = syscall_ret;
do_syscall = 0;
break;
}
case __NR_shmat: {
uintptr_t syscall_ret = raw_syscall(syscall_no, args[0], args[1], args[2]);
if (syscall_ret != -1) {
struct shmid_ds shm;
int prot = PROT_READ;
prot |= (args[2] & SHM_EXEC) ? PROT_EXEC : 0;
prot |= (args[2] & SHM_RDONLY) ? 0 : PROT_WRITE;
int ret = shmctl(args[0], IPC_STAT, &shm);
assert(ret == 0);
notify_vm_op(VM_MAP, syscall_ret, shm.shm_segsz, prot, 0, -1, 0);
}
args[0] = syscall_ret;
do_syscall = 0;
break;
}
case __NR_shmdt: {
struct shmid_ds shm;
int ret = shmctl(args[0], IPC_STAT, &shm);
if (ret == 0) {
uintptr_t syscall_ret = raw_syscall(syscall_no, args[0]);
if (syscall_ret == 0) {
notify_vm_op(VM_UNMAP, args[0], shm.shm_segsz, 0, 0, -1, 0);
}
args[0] = syscall_ret;
} else {
args[0] = -errno;
}
do_syscall = 0;
break;
}
#ifdef __arm__
case __NR_sigreturn:
#endif
case __NR_rt_sigreturn: {
void *app_sp = args;
#ifdef __arm__
/* We force all signal handler to the SA_SIGINFO type, which must return
with rt_sigreturn() and not sigreturn(). Some applications don't return
to the rt_sigreturn wrapper set by the kernel in the LR, so we need to
override it here. See linux/arm/kernel/signal.c for the difference
between the two types of signal handlers.
*/
args[7] = __NR_rt_sigreturn;
app_sp += 64;
#elif __aarch64__
app_sp += 64 + 144;
#endif
ucontext_t *cont = (ucontext_t *)(app_sp + sizeof(siginfo_t));
sigret_dispatcher_call(thread_data, cont, cont->context_pc);
// Don't mark the thread as executing a syscall
return 1;
}
#ifdef __arm__
case __NR_vfork:
// vfork without sharing the address space
args[0] = raw_syscall(__NR_clone, CLONE_VFORK, NULL, NULL, NULL, NULL);
if (args[0] == 0) {
reset_process(thread_data);
}
do_syscall = 0;
break;
case __ARM_NR_cacheflush:
debug("cache flush\n");
/* Returning to the calling BB is potentially unsafe because the remaining
contents of the BB or other basic blocks it is linked against could be stale */
flush_code_cache(thread_data);
break;
case __ARM_NR_set_tls:
debug("set tls to %x\n", args[0]);
thread_data->tls = args[0];
args[0] = 0;
do_syscall = 0;
break;
case __NR_readlink: {
ssize_t len = readlink_handler((char *)args[0], (char *)args[1], args[2]);
if (len >= 0) {
args[0] = (uintptr_t)len;
do_syscall = 0;
}
break;
}
#endif
}
#ifdef PLUGINS_NEW
} // if (!ctx.syscall.replace)
if (do_syscall == 0 && global_data.free_plugin > 0) {
set_mambo_context_syscall(&ctx, thread_data, POST_SYSCALL_C, syscall_no, (uintptr_t *)args);
mambo_deliver_callbacks_for_ctx(&ctx);
}
#endif
if (do_syscall) {
thread_data->status = THREAD_SYSCALL;
}
return do_syscall;
}
void syscall_handler_post(uintptr_t syscall_no, uintptr_t *args, uint16_t *next_inst, dbm_thread *thread_data) {
debug("syscall post %" PRIdPTR "\n", syscall_no);
if (global_data.exit_group) {
thread_abort(thread_data);
}
thread_data->status = THREAD_RUNNING;
switch(syscall_no) {
case __NR_clone:
debug("r0 (tid): %" PRIdPTR "\n", args[0]);
if (args[0] == 0) { // the child
assert(!thread_data->clone_vm);
/* Without CLONE_VM, the child runs in a separate memory space,
no synchronisation is needed.*/
thread_data->tls = thread_data->child_tls;
reset_process(thread_data);
}
break;
}
#ifdef PLUGINS_NEW
mambo_context ctx;
set_mambo_context_syscall(&ctx, thread_data, POST_SYSCALL_C, syscall_no, (uintptr_t *)args);
mambo_deliver_callbacks_for_ctx(&ctx);
#endif
}