inital commit 1

This commit is contained in:
mxdyeah committed 2026-07-08 19:25:32 +08:00
commit 07738cacaf
76 files changed
+9477

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
#include <stdio.h>
#include <seccomp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdbool.h>
#include "../runner.h"
int _c_cpp_seccomp_rules(struct config *_config, bool allow_write_file) {
int syscalls_whitelist[] = {
SCMP_SYS(access),
SCMP_SYS(arch_prctl),
SCMP_SYS(brk),
SCMP_SYS(clock_gettime),
SCMP_SYS(close),
SCMP_SYS(exit_group),
SCMP_SYS(faccessat),
SCMP_SYS(fstat),
SCMP_SYS(futex),
SCMP_SYS(getrandom),
SCMP_SYS(lseek),
SCMP_SYS(mmap),
SCMP_SYS(mprotect),
SCMP_SYS(munmap),
SCMP_SYS(newfstatat),
SCMP_SYS(pread64),
SCMP_SYS(prlimit64),
SCMP_SYS(read),
SCMP_SYS(readlink),
SCMP_SYS(readv),
SCMP_SYS(rseq),
SCMP_SYS(set_robust_list),
SCMP_SYS(set_tid_address),
SCMP_SYS(write),
SCMP_SYS(writev)
};
int syscalls_whitelist_length = sizeof(syscalls_whitelist) / sizeof(int);
scmp_filter_ctx ctx = NULL;
// load seccomp rules
ctx = seccomp_init(SCMP_ACT_KILL);
if (!ctx) {
return LOAD_SECCOMP_FAILED;
}
for (int i = 0; i < syscalls_whitelist_length; i++) {
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, syscalls_whitelist[i], 0) != 0) {
return LOAD_SECCOMP_FAILED;
}
}
// extra rule for execve
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(execve), 1, SCMP_A0(SCMP_CMP_EQ, (scmp_datum_t)(_config->exe_path))) != 0) {
return LOAD_SECCOMP_FAILED;
}
if (allow_write_file) {
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 0) != 0) {
return LOAD_SECCOMP_FAILED;
}
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(openat), 0) != 0) {
return LOAD_SECCOMP_FAILED;
}
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(dup), 0) != 0) {
return LOAD_SECCOMP_FAILED;
}
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(dup2), 0) != 0) {
return LOAD_SECCOMP_FAILED;
}
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(dup3), 0) != 0) {
return LOAD_SECCOMP_FAILED;
}
} else {
// do not allow "w" and "rw"
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1, SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY | O_RDWR, 0)) != 0) {
return LOAD_SECCOMP_FAILED;
}
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(openat), 1, SCMP_CMP(2, SCMP_CMP_MASKED_EQ, O_WRONLY | O_RDWR, 0)) != 0) {
return LOAD_SECCOMP_FAILED;
}
}
if (seccomp_load(ctx) != 0) {
return LOAD_SECCOMP_FAILED;
}
seccomp_release(ctx);
return 0;
}
int c_cpp_seccomp_rules(struct config *_config, bool allow_write_file) {
return _c_cpp_seccomp_rules(_config, false);
}
+7
View File
@@ -0,0 +1,7 @@
#include <stdbool.h>
#include "seccomp_rules.h"
int c_cpp_file_io_seccomp_rules(struct config *_config) {
return _c_cpp_seccomp_rules(_config, true);
}
+59
View File
@@ -0,0 +1,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;
}
+51
View File
@@ -0,0 +1,51 @@
#include <stdio.h>
#include <seccomp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "../runner.h"
int golang_seccomp_rules(struct config *_config) {
int syscalls_blacklist[] = {SCMP_SYS(socket),
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;
}
}
// 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;
}
+36
View File
@@ -0,0 +1,36 @@
#include <stdio.h>
#include <seccomp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "../runner.h"
int node_seccomp_rules(struct config *_config) {
int syscalls_blacklist[] = {SCMP_SYS(socket),
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;
}
}
if (seccomp_load(ctx) != 0) {
return LOAD_SECCOMP_FAILED;
}
seccomp_release(ctx);
return 0;
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef JUDGER_SECCOMP_RULES_H
#define JUDGER_SECCOMP_RULES_H
#include <stdbool.h>
#include "../runner.h"
int _c_cpp_seccomp_rules(struct config *_config, bool allow_write_file);
int c_cpp_seccomp_rules(struct config *_config);
int general_seccomp_rules(struct config *_config);
int c_cpp_file_io_seccomp_rules(struct config *_config);
int golang_seccomp_rules(struct config *_config);
int node_seccomp_rules(struct config *_config);
#endif //JUDGER_SECCOMP_RULES_H