-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandbox.c
80 lines (69 loc) · 1.83 KB
/
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
#include "sandbox.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <linux/seccomp.h>
#include <seccomp.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include "sce.h"
const int rules_default[] = {
SCMP_SYS(mkdir),
INT_MAX,
};
int ssc_seccomp_init(scmp_filter_ctx ctx, const int *rules, int whitelist) {
/* Create Filter */
int action;
if (whitelist) {
action = SCMP_ACT_ALLOW;
} else {
action = SCMP_ACT_KILL;
}
/* Add rules */
if (ssc_seccomp_add(ctx, rules, action)) {
return SCE_LDSCMP;
}
/* Load Filter */
if (seccomp_load(ctx) != 0) {
return SCE_LDSCMP;
}
return 0;
}
int ssc_seccomp_add(scmp_filter_ctx ctx, const int *rules, int action) {
for (int i = 0; rules[i] != INT_MAX; ++i) {
if (seccomp_rule_add(ctx, action, rules[i], 0) != 0) {
seccomp_release(ctx);
return SCE_LDSCMP;
}
}
return 0;
}
int ssc_seccomp_load_manually(char *path, int argc, char **argv) {
scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_ALLOW);
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(execve), 1,
SCMP_A0(SCMP_CMP_NE, (scmp_datum_t)(path))) != 0) {
seccomp_release(ctx);
return SCE_LDSCMP;
}
for (int i = 0; i < argc; i++) {
if (!argv[i]) break;
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(execve), 1,
SCMP_A0(SCMP_CMP_NE, (scmp_datum_t)(argv[i]))) !=
0) {
seccomp_release(ctx);
return SCE_LDSCMP;
}
}
/* Load */
if (ssc_seccomp_init(ctx, rules_default, 0)) {
seccomp_release(ctx);
return SCE_LDSCMP;
}
seccomp_release(ctx);
return 0;
}
void kill_childprocess() {
extern pid_t pid;
kill(pid, SIGKILL);
}