This repository has been archived by the owner on May 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjail.c
88 lines (76 loc) · 2.09 KB
/
jail.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
#include <stdio.h>
#include <unistd.h>
#include <asm/unistd.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "common.h"
#include "jail.h"
#include "helper.h"
#include "inject.h"
void syscall_proxy(void) {
char buf[8*4];
unsigned int nrsyscall;
int ret = 0;
int wrote;
asm ("pushl %eax\n");
asm ("movl $1, (%%eax)\n"
"popl 4(%%eax)\n"
"movl %%ebx, 8(%%eax)\n"
"movl %%ecx, 12(%%eax)\n"
"movl %%edx, 16(%%eax)\n"
"movl %%esi, 20(%%eax)\n"
"movl %%edi, 24(%%eax)\n"
"movl (%%ebp), %%ebx\n" // gcc prologue pushed ebp, and I want its initial value
"movl %%ebx, 28(%%eax)\n"
:: "a" (buf) );
xwrite(CONTROL_FD, buf, sizeof buf);
ret = wait_for_orders(CONTROL_FD);
asm("movl %0, %%eax\n" : : "m" (ret));
}
void (*syscall_proxy_addr)(void) = syscall_proxy;
void handler(void) {
asm("movl (%%ebp), %%ebp\n" // ignore the gcc prologue
"cmpl " ivalue(__NR_write) ", %%eax\n"
// "je wrap_read\n"
"je do_syscall\n"
"cmpl " ivalue(__NR_read) ", %%eax\n"
// "je wrap_read\n"
"je do_syscall\n"
"cmpl " ivalue(__NR_exit_group) ", %%eax\n"
"jne wrapper\n"
"movl " ivalue(__NR_exit) ", %%eax\n"
"wrapper:\n"
" pushl %%ebx\n"
" pushl %%ecx\n"
" pushl %%edx\n"
" pushl %%esi\n"
" pushl %%edi\n"
" call *%0\n"
" popl %%edi\n"
" popl %%esi\n"
" popl %%edx\n"
" popl %%ecx\n"
" popl %%ebx\n"
" jmp out\n"
"do_syscall:\n"
" int $0x80\n"
" jmp out\n"
"wrap_write:\n"
" cmp " ivalue(__NR_write) ", %%ebx\n"
" jle do_syscall\n"
" jmp wrapper\n"
"wrap_read:\n"
" cmpl $4, %%ebx\n" /* master socket? */
" je do_syscall\n"
" jmp wrapper\n"
" \n"
"out: nop\n"
: /* output */
: "m" (syscall_proxy_addr),
"m" (real_handler));
}