Spaces:
Running
Running
feat: use seccomp to block network (#39)
Browse files- Dockerfile +2 -1
- Makefile +6 -5
- competitions/evaluate.py +4 -7
- sandbox.c +53 -0
- socket-kit.c +0 -8
Dockerfile
CHANGED
|
@@ -31,6 +31,7 @@ RUN apt-get update && \
|
|
| 31 |
libgl1 \
|
| 32 |
unzip \
|
| 33 |
openjdk-11-jre-headless \
|
|
|
|
| 34 |
&& rm -rf /var/lib/apt/lists/* && \
|
| 35 |
apt-get clean
|
| 36 |
|
|
@@ -68,7 +69,7 @@ RUN conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c
|
|
| 68 |
conda install -c "nvidia/label/cuda-12.1.0" cuda-nvcc && conda clean -ya
|
| 69 |
|
| 70 |
COPY --chown=1000:1000 . /app/
|
| 71 |
-
RUN make
|
| 72 |
|
| 73 |
ENV PATH="/app:${PATH}"
|
| 74 |
|
|
|
|
| 31 |
libgl1 \
|
| 32 |
unzip \
|
| 33 |
openjdk-11-jre-headless \
|
| 34 |
+
libseccomp-dev \
|
| 35 |
&& rm -rf /var/lib/apt/lists/* && \
|
| 36 |
apt-get clean
|
| 37 |
|
|
|
|
| 69 |
conda install -c "nvidia/label/cuda-12.1.0" cuda-nvcc && conda clean -ya
|
| 70 |
|
| 71 |
COPY --chown=1000:1000 . /app/
|
| 72 |
+
RUN make sandbox
|
| 73 |
|
| 74 |
ENV PATH="/app:${PATH}"
|
| 75 |
|
Makefile
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
CFLAGS += -std=c99 -Wall
|
|
|
|
| 2 |
.PHONY: quality style test
|
| 3 |
|
| 4 |
quality:
|
|
@@ -18,15 +19,15 @@ docker:
|
|
| 18 |
test:
|
| 19 |
pytest -sv .
|
| 20 |
|
| 21 |
-
|
| 22 |
-
gcc $(CFLAGS)
|
| 23 |
|
| 24 |
clean:
|
| 25 |
-
rm *.so
|
| 26 |
|
| 27 |
pip:
|
| 28 |
rm -rf build/
|
| 29 |
rm -rf dist/
|
| 30 |
make style && make quality
|
| 31 |
python setup.py sdist bdist_wheel
|
| 32 |
-
twine upload dist/* --verbose
|
|
|
|
| 1 |
+
CFLAGS += -std=c99 -Wall -O2
|
| 2 |
+
LDFLAGS += -lseccomp
|
| 3 |
.PHONY: quality style test
|
| 4 |
|
| 5 |
quality:
|
|
|
|
| 19 |
test:
|
| 20 |
pytest -sv .
|
| 21 |
|
| 22 |
+
sandbox: sandbox.c
|
| 23 |
+
gcc $(CFLAGS) $^ -o $@ $(LDFLAGS)
|
| 24 |
|
| 25 |
clean:
|
| 26 |
+
rm *.so sandbox
|
| 27 |
|
| 28 |
pip:
|
| 29 |
rm -rf build/
|
| 30 |
rm -rf dist/
|
| 31 |
make style && make quality
|
| 32 |
python setup.py sdist bdist_wheel
|
| 33 |
+
twine upload dist/* --verbose
|
competitions/evaluate.py
CHANGED
|
@@ -43,18 +43,15 @@ def generate_submission_file(params):
|
|
| 43 |
# invalidate USER_TOKEN env var
|
| 44 |
os.environ["USER_TOKEN"] = ""
|
| 45 |
|
| 46 |
-
# Copy
|
| 47 |
-
shutil.copyfile("
|
| 48 |
|
| 49 |
# Define your command
|
| 50 |
-
cmd = "python script.py"
|
| 51 |
-
|
| 52 |
|
| 53 |
# Copy the current environment and modify it
|
| 54 |
env = os.environ.copy()
|
| 55 |
-
env["LD_PRELOAD"] = socket_kit_path
|
| 56 |
-
|
| 57 |
-
cmd = shlex.split(cmd)
|
| 58 |
|
| 59 |
# Start the subprocess
|
| 60 |
process = subprocess.Popen(cmd, cwd=submission_dir, env=env)
|
|
|
|
| 43 |
# invalidate USER_TOKEN env var
|
| 44 |
os.environ["USER_TOKEN"] = ""
|
| 45 |
|
| 46 |
+
# Copy sandbox to submission_dir
|
| 47 |
+
shutil.copyfile("sandbox", f"{submission_dir}/sandbox")
|
| 48 |
|
| 49 |
# Define your command
|
| 50 |
+
cmd = "./sandbox python script.py"
|
| 51 |
+
cmd = shlex.split(cmd)
|
| 52 |
|
| 53 |
# Copy the current environment and modify it
|
| 54 |
env = os.environ.copy()
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# Start the subprocess
|
| 57 |
process = subprocess.Popen(cmd, cwd=submission_dir, env=env)
|
sandbox.c
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <errno.h>
|
| 2 |
+
#include <seccomp.h>
|
| 3 |
+
#include <stdio.h>
|
| 4 |
+
#include <stdlib.h>
|
| 5 |
+
#include <unistd.h>
|
| 6 |
+
|
| 7 |
+
int main(int argc, char* argv[]) {
|
| 8 |
+
if (argc < 2) {
|
| 9 |
+
fprintf(stderr, "Usage: %s <command> [args...]\n", argv[0]);
|
| 10 |
+
return EXIT_FAILURE;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
scmp_filter_ctx ctx;
|
| 14 |
+
|
| 15 |
+
// Initialize the seccomp filter in blocklist mode
|
| 16 |
+
ctx = seccomp_init(SCMP_ACT_ALLOW);
|
| 17 |
+
if (ctx == NULL) {
|
| 18 |
+
perror("seccomp_init");
|
| 19 |
+
return EXIT_FAILURE;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
// Block network-related syscalls
|
| 23 |
+
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(socket), 0);
|
| 24 |
+
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(connect), 0);
|
| 25 |
+
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(bind), 0);
|
| 26 |
+
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(listen), 0);
|
| 27 |
+
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(accept), 0);
|
| 28 |
+
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(send), 0);
|
| 29 |
+
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(sendto), 0);
|
| 30 |
+
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(sendmsg), 0);
|
| 31 |
+
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(recv), 0);
|
| 32 |
+
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(recvfrom), 0);
|
| 33 |
+
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(recvmsg), 0);
|
| 34 |
+
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(setsockopt), 0);
|
| 35 |
+
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(getsockopt), 0);
|
| 36 |
+
|
| 37 |
+
// Load the filter into the kernel
|
| 38 |
+
if (seccomp_load(ctx) < 0) {
|
| 39 |
+
perror("seccomp_load");
|
| 40 |
+
seccomp_release(ctx);
|
| 41 |
+
return EXIT_FAILURE;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
#ifdef DEBUG
|
| 45 |
+
printf("seccomp filter installed. Network access is blocked.\n");
|
| 46 |
+
#endif
|
| 47 |
+
|
| 48 |
+
// Execute the target program
|
| 49 |
+
execvp(argv[1], argv + 1);
|
| 50 |
+
|
| 51 |
+
seccomp_release(ctx);
|
| 52 |
+
return EXIT_SUCCESS;
|
| 53 |
+
}
|
socket-kit.c
DELETED
|
@@ -1,8 +0,0 @@
|
|
| 1 |
-
#include <errno.h>
|
| 2 |
-
#include <sys/socket.h>
|
| 3 |
-
|
| 4 |
-
int connect(int fd, const struct sockaddr *addr, socklen_t len)
|
| 5 |
-
{
|
| 6 |
-
errno = ENETDOWN;
|
| 7 |
-
return -1;
|
| 8 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|