"""Run the original Python tool after reducing privileges and blocking networking.""" import ctypes import errno import json import resource import sys resource.setrlimit(resource.RLIMIT_CPU, (3, 3)) resource.setrlimit(resource.RLIMIT_AS, (256 * 1024 * 1024, 256 * 1024 * 1024)) resource.setrlimit(resource.RLIMIT_FSIZE, (65536, 65536)) resource.setrlimit(resource.RLIMIT_NOFILE, (32, 32)) resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) # Filters survive fork/exec. Fail closed if seccomp cannot be loaded. lib = ctypes.CDLL("libseccomp.so.2", use_errno=True) lib.seccomp_init.argtypes = [ctypes.c_uint32] lib.seccomp_init.restype = ctypes.c_void_p lib.seccomp_rule_add.argtypes = [ ctypes.c_void_p, ctypes.c_uint32, ctypes.c_int, ctypes.c_uint, ] lib.seccomp_syscall_resolve_name.argtypes = [ctypes.c_char_p] lib.seccomp_load.argtypes = [ctypes.c_void_p] lib.seccomp_release.argtypes = [ctypes.c_void_p] ctx = lib.seccomp_init(0x7FFF0000) # SCMP_ACT_ALLOW if not ctx: raise RuntimeError("Cannot initialize execution isolation") try: for name in ( "socket", "socketpair", "connect", "ptrace", "process_vm_readv", "process_vm_writev", "mount", "unshare", "setns", "bpf", ): number = lib.seccomp_syscall_resolve_name(name.encode()) if ( number >= 0 and lib.seccomp_rule_add(ctx, 0x00050000 | errno.EPERM, number, 0) != 0 ): raise RuntimeError("Cannot install execution isolation rule") if lib.seccomp_load(ctx) != 0: raise RuntimeError("Cannot activate execution isolation") finally: lib.seccomp_release(ctx) sys.path.insert(0, "/app") from simulator import ToolSimulator # noqa: E402 request = json.load(sys.stdin) json.dump(ToolSimulator().code_py_exec(request), sys.stdout)