-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathban_CLONE_NEWUSER.c
44 lines (34 loc) · 1.42 KB
/
ban_CLONE_NEWUSER.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
#include <linux/sched.h> // for CLONE_NEWUSER
#include <seccomp.h> // for seccomp_rule_add, etc
#include <stdint.h> // for uint32_t
#include <stdio.h> // for fprintf, perror, stderr
#include <unistd.h> // for execve
// gcc ban_CLONE_NEWUSER.c -lseccomp -o ban_CLONE_NEWUSER
int main(int argc, char* argv[], char* envp[]) {
if (argc<2) {
fprintf(stderr, "Usage: ban_CLONE_NEWUSER program [arguments]\n");
fprintf(stderr, "Bans unshare(2) with any flags and clone(2) with CLONE_NEWUSER flag\n");
return 126;
}
uint32_t default_action = SCMP_ACT_ALLOW;
scmp_filter_ctx ctx = seccomp_init(default_action);
if (!ctx) {
perror("seccomp_init");
return 126;
}
int ret = 0;
ret |= seccomp_rule_add(ctx, SCMP_ACT_ERRNO(1), seccomp_syscall_resolve_name("unshare"), 0);
ret |= seccomp_rule_add(ctx, SCMP_ACT_ERRNO(1), seccomp_syscall_resolve_name("clone"), 1, SCMP_CMP(0, SCMP_CMP_MASKED_EQ, CLONE_NEWUSER, CLONE_NEWUSER));
if (ret!=0) {
fprintf(stderr, "seccomp_rule_add returned %d\n", ret);
return 124;
}
ret = seccomp_load(ctx);
if (ret!=0) {
fprintf(stderr, "seccomp_load returned %d\n", ret);
return 124;
}
execve(argv[1], argv+1, envp);
perror("execve");
return 127;
}