-
Notifications
You must be signed in to change notification settings - Fork 259
/
general.c
59 lines (55 loc) · 2.13 KB
/
general.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
#include <stdio.h>
#include <seccomp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "../runner.h"
int general_seccomp_rules(struct config *_config) {
int syscalls_blacklist[] = {SCMP_SYS(clone),
SCMP_SYS(fork), SCMP_SYS(vfork),
SCMP_SYS(kill),
#ifdef __NR_execveat
SCMP_SYS(execveat)
#endif
};
int syscalls_blacklist_length = sizeof(syscalls_blacklist) / sizeof(int);
scmp_filter_ctx ctx = NULL;
// load seccomp rules
ctx = seccomp_init(SCMP_ACT_ALLOW);
if (!ctx) {
return LOAD_SECCOMP_FAILED;
}
for (int i = 0; i < syscalls_blacklist_length; i++) {
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, syscalls_blacklist[i], 0) != 0) {
return LOAD_SECCOMP_FAILED;
}
}
// use SCMP_ACT_KILL for socket, python will be killed immediately
if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(socket), 0) != 0) {
return LOAD_SECCOMP_FAILED;
}
// add extra rule for execve
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(execve), 1, SCMP_A0(SCMP_CMP_NE, (scmp_datum_t)(_config->exe_path))) != 0) {
return LOAD_SECCOMP_FAILED;
}
// do not allow "w" and "rw" using open
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(open), 1, SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY, O_WRONLY)) != 0) {
return LOAD_SECCOMP_FAILED;
}
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(open), 1, SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_RDWR, O_RDWR)) != 0) {
return LOAD_SECCOMP_FAILED;
}
// do not allow "w" and "rw" using openat
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(openat), 1, SCMP_CMP(2, SCMP_CMP_MASKED_EQ, O_WRONLY, O_WRONLY)) != 0) {
return LOAD_SECCOMP_FAILED;
}
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(openat), 1, SCMP_CMP(2, SCMP_CMP_MASKED_EQ, O_RDWR, O_RDWR)) != 0) {
return LOAD_SECCOMP_FAILED;
}
if (seccomp_load(ctx) != 0) {
return LOAD_SECCOMP_FAILED;
}
seccomp_release(ctx);
return 0;
}