forked from t00sh/p-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p-sandbox.c
131 lines (106 loc) · 2.85 KB
/
p-sandbox.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
#include <sys/ptrace.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <err.h>
#include <sys/user.h>
#include <asm/ptrace.h>
#include <sys/wait.h>
#include <asm/unistd.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
struct sandbox {
pid_t child;
const char *progname;
};
struct sandb_syscall {
int syscall;
void (*callback)(struct sandbox*, struct user_regs_struct *regs);
};
struct sandb_syscall sandb_syscalls[] = {
{__NR_read, NULL},
{__NR_write, NULL},
{__NR_exit, NULL},
{__NR_brk, NULL},
{__NR_mmap, NULL},
{__NR_access, NULL},
{__NR_open, NULL},
{__NR_fstat, NULL},
{__NR_close, NULL},
{__NR_mprotect, NULL},
{__NR_munmap, NULL},
{__NR_arch_prctl, NULL},
{__NR_exit_group, NULL},
{__NR_getdents, NULL},
};
void sandb_kill(struct sandbox *sandb) {
kill(sandb->child, SIGKILL);
wait(NULL);
exit(EXIT_FAILURE);
}
void sandb_handle_syscall(struct sandbox *sandb) {
int i;
struct user_regs_struct regs;
if(ptrace(PTRACE_GETREGS, sandb->child, NULL, ®s) < 0)
err(EXIT_FAILURE, "[SANDBOX] Failed to PTRACE_GETREGS:");
for(i = 0; i < sizeof(sandb_syscalls)/sizeof(*sandb_syscalls); i++) {
if(regs.orig_rax == sandb_syscalls[i].syscall) {
if(sandb_syscalls[i].callback != NULL)
sandb_syscalls[i].callback(sandb, ®s);
return;
}
}
if(regs.orig_rax == -1) {
printf("[SANDBOX] Segfault ?! KILLING !!!\n");
} else {
printf("[SANDBOX] Trying to use devil syscall (%llu) ?!? KILLING !!!\n", regs.orig_rax);
}
sandb_kill(sandb);
}
void sandb_init(struct sandbox *sandb, int argc, char **argv) {
pid_t pid;
pid = fork();
if(pid == -1)
err(EXIT_FAILURE, "[SANDBOX] Error on fork:");
if(pid == 0) {
if(ptrace(PTRACE_TRACEME, 0, NULL, NULL) < 0)
err(EXIT_FAILURE, "[SANDBOX] Failed to PTRACE_TRACEME:");
if(execv(argv[0], argv) < 0)
err(EXIT_FAILURE, "[SANDBOX] Failed to execv:");
} else {
sandb->child = pid;
sandb->progname = argv[0];
wait(NULL);
}
}
void sandb_run(struct sandbox *sandb) {
int status;
if(ptrace(PTRACE_SYSCALL, sandb->child, NULL, NULL) < 0) {
if(errno == ESRCH) {
waitpid(sandb->child, &status, __WALL | WNOHANG);
sandb_kill(sandb);
} else {
err(EXIT_FAILURE, "[SANDBOX] Failed to PTRACE_SYSCALL:");
}
}
wait(&status);
if(WIFEXITED(status))
exit(EXIT_SUCCESS);
if(WIFSTOPPED(status)) {
sandb_handle_syscall(sandb);
}
}
int main(int argc, char **argv) {
struct sandbox sandb;
if(argc < 2) {
errx(EXIT_FAILURE, "[SANDBOX] Usage : %s <elf> [<arg1...>]", argv[0]);
}
sandb_init(&sandb, argc-1, argv+1);
for(;;) {
sandb_run(&sandb);
}
return EXIT_SUCCESS;
}