File size: 1,868 Bytes
49aa045
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
"""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)