-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunix.cc
364 lines (325 loc) · 11 KB
/
unix.cc
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
#include "sandbox.h"
#include "defer.h"
#include "seccomp_filter/seccomp_filter.h"
#include <assert.h>
#include <chrono>
#include <fcntl.h>
#include <math.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
class UnixSandbox : public Sandbox {
public:
int Priority() override {
// This sandbox is not very secure.
return 0;
}
bool CanUse() override { return true; }
ExecutionResults Execute(const options::Options &) override;
};
namespace {
#ifdef __APPLE__
int GetProcessMemoryUsageFromProc(pid_t pid, uint64_t *memory_usage_kb) {
int fd = open(("/proc/" + std::to_string(pid) + "/statm").c_str(),
O_RDONLY | O_CLOEXEC);
if (fd == -1)
return -1;
char buf[64 * 1024] = {};
size_t num_read = 0;
ssize_t cur = 0;
do {
cur = read(fd, buf + num_read, 64 * 1024 - num_read);
if (cur < 0) {
close(fd);
return -1;
}
num_read += cur;
} while (cur > 0);
close(fd);
if (sscanf(buf, "%" PRIu64, memory_usage_kb) != 1) {
fprintf(stderr, "Unable to get memory usage from /proc: %s", buf);
exit(1);
}
*memory_usage_kb *= 4;
return 0;
}
int GetProcessMemoryUsage(pid_t pid, uint64_t *memory_usage_kb) {
if (GetProcessMemoryUsageFromProc(pid, memory_usage_kb) == 0)
return 0;
int pipe_fds[2];
if (pipe(pipe_fds) == -1)
return errno;
posix_spawn_file_actions_t actions;
int ret = posix_spawn_file_actions_init(&actions);
if (ret != 0)
return ret;
ret = posix_spawn_file_actions_addclose(&actions, pipe_fds[0]);
if (ret != 0)
return ret;
ret = posix_spawn_file_actions_addclose(&actions, STDIN_FILENO);
if (ret != 0)
return ret;
ret = posix_spawn_file_actions_adddup2(&actions, pipe_fds[1], STDOUT_FILENO);
if (ret != 0)
return ret;
ret = posix_spawn_file_actions_addclose(&actions, pipe_fds[1]);
if (ret != 0)
return ret;
std::vector<std::vector<char>> args;
auto add_arg = [&args](std::string s) {
std::vector<char> arg(s.size() + 1);
std::copy(s.begin(), s.end(), arg.begin());
arg.back() = '\0';
args.push_back(std::move(arg));
};
add_arg("ps");
add_arg("-o");
add_arg("rss=");
add_arg(std::to_string(pid));
std::vector<char *> args_list(args.size() + 1);
for (size_t i = 0; i < args.size(); i++)
args_list[i] = args[i].data();
args_list.back() = nullptr;
char **environ = {nullptr};
int child_pid = 0;
ret = posix_spawnp(&child_pid, "ps", &actions, nullptr, args_list.data(),
environ);
close(pipe_fds[1]);
if (ret != 0) {
close(pipe_fds[0]);
return ret;
}
int child_status = 0;
if (waitpid(child_pid, &child_status, 0) == -1) {
close(pipe_fds[0]);
return errno;
}
if (child_status != 0) {
close(pipe_fds[0]);
*memory_usage_kb = 0;
return 0;
}
char memory_usage_buf[1024] = {};
if (read(pipe_fds[0], memory_usage_buf, 1024) == -1) {
close(pipe_fds[0]);
*memory_usage_kb = 0;
return 0;
}
close(pipe_fds[0]);
if (sscanf(memory_usage_buf, "%" PRIu64, memory_usage_kb) != 1) {
*memory_usage_kb = 0;
}
return 0;
}
#endif
} // namespace
#define SYSCALL(call) \
if ((call) == -1) { \
auto saved_errno = errno; \
results.error = true; \
results.message = #call; \
results.message += ": "; \
results.message += strerror(saved_errno); \
return results; \
}
#define CSYSCALL(call) \
if ((call) == -1) { \
auto saved_errno = errno; \
const char *prefix = #call ": "; \
int plen = strlen(prefix); \
write(fd, prefix, plen); \
const char *err = strerror(saved_errno); \
int elen = strlen(err); \
write(fd, err, elen); \
_Exit(1); \
}
#define OPTION(opt) options.Get<options::opt>()
namespace {
[[noreturn]] void Child(const options::Options &options, int fd) {
// Change process group.
CSYSCALL(setsid());
// Working directory
if (!OPTION(WorkingDirectory).empty()) {
CSYSCALL(chdir(OPTION(WorkingDirectory).c_str()));
}
// IO redirection
if (!OPTION(Stdin).empty()) {
int fd;
CSYSCALL(fd = open(OPTION(Stdin).c_str(), O_RDONLY | O_CLOEXEC));
CSYSCALL(dup2(fd, STDIN_FILENO));
}
if (!OPTION(Stdout).empty()) {
int fd;
CSYSCALL(fd = open(OPTION(Stdout).c_str(),
O_WRONLY | O_CLOEXEC | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR));
CSYSCALL(dup2(fd, STDOUT_FILENO));
}
if (!OPTION(Stderr).empty()) {
int fd;
CSYSCALL(fd = open(OPTION(Stderr).c_str(),
O_WRONLY | O_CLOEXEC | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR));
CSYSCALL(dup2(fd, STDERR_FILENO));
}
// Argument list
auto &exe = OPTION(Executable);
auto &other_args = OPTION(Args);
std::vector<char *> args(2 + other_args.size());
args[0] = const_cast<char *>(exe.c_str());
args.back() = nullptr;
for (size_t i = 0; i < other_args.size(); i++) {
args[i + 1] = const_cast<char *>(other_args[i].c_str());
}
// Resource limits
#define SET_RLIM(res, value) \
{ \
rlim_t limit = value; \
if (limit) { \
struct rlimit rlim {}; \
rlim.rlim_cur = limit; \
rlim.rlim_max = limit; \
CSYSCALL(setrlimit(RLIMIT_##res, &rlim)); \
} \
}
SET_RLIM(AS, OPTION(MemoryLimit) * 1024);
SET_RLIM(CPU, ceil(OPTION(TimeLimit)));
SET_RLIM(FSIZE, ceil(OPTION(FsizeLimit)) * 1024);
SET_RLIM(CORE, 0);
// Setting stack size does not seem to work on MAC.
#ifndef __APPLE__
SET_RLIM(STACK, RLIM_INFINITY);
#endif
#undef SET_RLIM
// Environment variables
auto &env = OPTION(Environment);
std::vector<std::string> env_mem;
std::vector<char *> envp;
for (size_t i = 0; i < env.size(); i++) {
if (env[i].value.has_value()) {
env_mem.push_back(env[i].name + "=" + *env[i].value);
} else {
char *current = getenv(env[i].name.c_str());
if (current == nullptr)
continue;
env_mem.push_back(env[i].name + "=" + current);
}
envp.push_back(const_cast<char *>(env_mem.back().c_str()));
}
envp.push_back(nullptr);
// Block disallowed syscalls.
SyscallsToBlock to_block;
to_block.chmod = !OPTION(AllowChmod);
to_block.fork = !OPTION(Multiprocess);
CSYSCALL(seccomp_filter(to_block));
CSYSCALL(execve(exe.c_str(), args.data(), envp.data()));
// execve either fails or does not return.
fprintf(stderr, "The impossible happened!\n");
_Exit(123);
}
#ifdef __APPLE__
[[noreturn]] void MemoryWatcher(int child_pid, uint64_t memory_limit_kb) {
while (true) {
uint64_t mem = 0;
if (GetProcessMemoryUsage(child_pid, &mem) == 0) {
if (memory_limit_kb != 0 && mem > memory_limit_kb) {
kill(child_pid, SIGKILL);
}
}
usleep(100);
}
}
#endif
sig_atomic_t have_signal = 0;
void sig_hdl(int /*sig*/, siginfo_t * /*siginfo*/, void * /*context*/) {
have_signal = 1;
}
} // namespace
ExecutionResults UnixSandbox::Execute(const options::Options &options) {
ExecutionResults results;
int pipefd[2];
SYSCALL(pipe(pipefd));
SYSCALL(fcntl(pipefd[1], F_SETFD, FD_CLOEXEC));
int fork_result;
SYSCALL(fork_result = fork());
if (fork_result == 0) {
close(pipefd[0]);
Child(options, pipefd[1]);
}
int child_pid = fork_result;
// Check that child started properly.
SYSCALL(close(pipefd[1]));
constexpr size_t kChildBufSize = 2048;
char child_buf[kChildBufSize + 1] = {};
size_t len = 0;
while (true) {
assert(len < kChildBufSize);
ssize_t increment;
SYSCALL(increment = read(pipefd[0], child_buf + len, kChildBufSize - len));
if (increment == 0)
break;
len += increment;
}
if (len > 0) {
waitpid(child_pid, nullptr, 0);
results.error = true;
results.message = "Child process: ";
results.message += child_buf;
return results;
}
// Set signal handlers for TERM and INT.
struct sigaction act {};
memset(&act, 0, sizeof(act));
act.sa_sigaction = &sig_hdl;
act.sa_flags = SA_SIGINFO;
SYSCALL(sigaction(SIGTERM, &act, nullptr));
SYSCALL(sigaction(SIGINT, &act, nullptr));
// Apple only: start memory watcher.
#ifdef __APPLE__
int memory_watcher_pid;
SYSCALL(memory_watcher_pid = fork());
if (memory_watcher_pid == 0) {
MemoryWatcher(child_pid, OPTION(MemoryLimit));
}
Defer defer([&memory_watcher_pid]() {
kill(memory_watcher_pid, SIGKILL);
waitpid(memory_watcher_pid, nullptr, 0);
});
#endif
// Child process started correctly: wait loop.
auto program_start = std::chrono::high_resolution_clock::now();
auto elapsed_seconds = [&program_start]() {
return std::chrono::duration_cast<
std::chrono::duration<double, std::ratio<1>>>(
std::chrono::high_resolution_clock::now() - program_start)
.count();
};
bool has_exited = false;
int child_status = 0;
while ((OPTION(WallLimit) < 1e-6 || elapsed_seconds() < OPTION(WallLimit)) &&
!have_signal) {
int wait_ret;
SYSCALL(wait_ret = waitpid(child_pid, &child_status, WNOHANG));
if (wait_ret == child_pid) {
has_exited = true;
break;
}
usleep(100);
}
if (!has_exited) {
results.killed_by_sandbox = true;
SYSCALL(kill(child_pid, SIGKILL));
SYSCALL(waitpid(child_pid, &child_status, 0));
}
struct rusage rusage {};
getrusage(RUSAGE_CHILDREN, &rusage);
results.memory_usage = rusage.ru_maxrss;
results.status_code = WIFEXITED(child_status) ? WEXITSTATUS(child_status) : 0;
results.signal = WIFSIGNALED(child_status) ? WTERMSIG(child_status) : 0;
results.wall_time = elapsed_seconds();
results.cpu_time = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec * 1e-6;
results.sys_time = rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec * 1e-6;
return results;
}
REGISTER_SANDBOX(UnixSandbox);