-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsandbox.cpp
177 lines (159 loc) · 6.64 KB
/
sandbox.cpp
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
//
// Created by Junyi Hou on 2/2/21.
//
#include "sandbox.h"
namespace JuicerSandbox {
int white_list[] = {
SCMP_SYS(exit_group),
SCMP_SYS(brk),
SCMP_SYS(arch_prctl),
SCMP_SYS(access),
SCMP_SYS(openat),
SCMP_SYS(fstat),
SCMP_SYS(mmap),
SCMP_SYS(munmap),
SCMP_SYS(close),
SCMP_SYS(read),
SCMP_SYS(pread64),
SCMP_SYS(write),
SCMP_SYS(mprotect),
};
struct arg_struct {
pid_t pgid; // process group id
uint32_t sleep_time; // ms
};
void *timeout_killer(void *args) {
auto *p = (struct arg_struct *) args;
if (pthread_detach(pthread_self()) != 0) {
killpg(p->pgid, SIGKILL);
std::string details("SYSTEM_ERROR");
details += "pthread_detach failed: " + std::string(strerror(errno));
throw ResultException(ResultType::SYSTEM_ERROR, details);
}
usleep(p->sleep_time * 1000);
printf("times up! wake up and kill -%d\n", p->pgid);
killpg(p->pgid, SIGKILL);
return nullptr;
}
void sandbox(const string &path) {
scmp_filter_ctx ctx;
if ((ctx = seccomp_init(SCMP_ACT_TRAP)) == nullptr) {
std::string details("SYSTEM_ERROR");
details += "seccomp_init failed: " + std::string(strerror(errno));
throw ResultException(ResultType::SYSTEM_ERROR, details);
}
for (int i : white_list) {
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, i, 0) != 0) {
std::string details("SYSTEM_ERROR");
details += "seccomp_rule_add failed: " + std::string(strerror(errno));
throw ResultException(ResultType::SYSTEM_ERROR, details);
}
}
// TODO: return value check
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(execve), 1,
SCMP_A0(SCMP_CMP_EQ, (scmp_datum_t) (path.c_str())));
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(openat), 1,
SCMP_CMP(2, SCMP_CMP_MASKED_EQ, O_WRONLY | O_RDWR, 0));
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY | O_RDWR, 0));
if (seccomp_load(ctx) != 0) {
std::string details("SYSTEM_ERROR");
details += "seccomp_load failed: " + std::string(strerror(errno));
throw ResultException(ResultType::SYSTEM_ERROR, details);
}
seccomp_release(ctx); // does not return a value
}
int run_with_constrains(int fd_in, int fd_out, int fd_err,
const string &path, char *const argv[],
char *const envp[],
uint32_t limit_time, uint32_t limit_stack,
uint32_t limit_memory, uint32_t limit_output, bool enable_sandbox) {
//int saved_stdin = dup(STDIN_FILENO);
pid_t pid = fork();
if (pid < 0) {
std::string details("SYSTEM_ERROR\n");
details += "fork failed: " + std::string(strerror(errno));
throw ResultException(ResultType::SYSTEM_ERROR, details);
} else if (pid == 0) {
// child
struct rlimit rlimit_nproc{
10000,
10000
};
struct rlimit rlimit_time{
(limit_time + 1000) / 1000,
(limit_time + 1000) / 1000
};
struct rlimit rlimit_stack{
limit_stack * 1024,
limit_stack * 1024,
};
struct rlimit rlimit_memory{
limit_memory * 1024,
limit_memory * 1024
};
struct rlimit rlimit_output{
limit_output * 1024,
limit_output * 1024
};
int ret[5] = {
setrlimit(RLIMIT_CPU, &rlimit_time),
setrlimit(RLIMIT_STACK, &rlimit_stack),
setrlimit(RLIMIT_DATA, &rlimit_memory),
setrlimit(RLIMIT_NPROC, &rlimit_nproc),
setrlimit(RLIMIT_FSIZE, &rlimit_output),
};
/* RLIMIT_CPU 不统计使用了 sleep() 的程序,所以必须靠另外线程来侦测运行时间 */
for (int i : ret) {
if (i != 0) {
std::string details("SYSTEM_ERROR\n");
details += "setrlimit failed: " + std::string(strerror(errno));
throw ResultException(ResultType::SYSTEM_ERROR, details);
}
}
if (fd_out != STDOUT_FILENO)
dup2(fd_out, STDOUT_FILENO);
if (fd_in != STDIN_FILENO)
dup2(fd_in, STDIN_FILENO);
if (fd_err != STDERR_FILENO)
dup2(fd_err, STDERR_FILENO);
setpgid(getpid(), getpid());
if (enable_sandbox) sandbox(path);
execvp(path.c_str(), argv);
} else {
// parent
int ret, status;
struct rusage usage{};
pthread_t monitor = 0;
struct arg_struct args{
pid,
limit_time
};
ret = pthread_create(&monitor, nullptr, timeout_killer, reinterpret_cast<void *>(&args));
if (ret != 0) {
killpg(pid, SIGKILL);
std::string details("SYSTEM_ERROR\n");
details += "pthread_create failed: " + std::string(strerror(errno));
throw ResultException(ResultType::SYSTEM_ERROR, details);
}
if (wait4(pid, &status, WSTOPPED, &usage) == -1) {
killpg(pid, SIGKILL);
std::string details("SYSTEM_ERROR\n");
details += "wait4 failed: " + std::string(strerror(errno));
throw ResultException(ResultType::SYSTEM_ERROR, details);
}
strerror(errno);
// if the child process was terminated by a signal
if (WIFSIGNALED(status)) {
printf("term sig: %d (%s)\n", WTERMSIG(status), strsignal(WTERMSIG(status)));
}
printf("exit code: \t%d\n", WEXITSTATUS(status));
printf("user mode: \t%ld ms\n", usage.ru_utime.tv_sec * 1000 + usage.ru_utime.tv_usec / 1000);
printf("kernel mode: \t%ld ms\n", usage.ru_stime.tv_sec * 1000 + usage.ru_stime.tv_usec / 1000);
printf("memory:\t\t%ld KB\n", usage.ru_maxrss);
return status;
}
// no one can execute here.
exit(1);
}
}