Example #1
0
 def __call__(self, argv, executable=None, *args, **kwargs):
     executable = executable or find_exe_in_path(argv[0])
     arch = file_arch(executable)
     debugger = _arch_map.get((INTERPRETER_ARCH, arch))
     if debugger is None:
         raise RuntimeError('Executable type %s could not be debugged on Python type %s' % (arch, INTERPRETER_ARCH))
     return super().__call__(debugger, self.debugger_type, argv, executable, *args, **kwargs)
Example #2
0
    def __init__(self,
                 debugger,
                 _,
                 args,
                 executable=None,
                 security=None,
                 time=0,
                 memory=0,
                 stdin=PIPE,
                 stdout=PIPE,
                 stderr=None,
                 env=None,
                 nproc=0,
                 fsize=0,
                 address_grace=4096,
                 data_grace=0,
                 personality=0,
                 cwd='',
                 fds=None,
                 wall_time=None):
        self._debugger_type = debugger
        self._syscall_index = index = _SYSCALL_INDICIES[debugger]
        self._executable = executable or find_exe_in_path(args[0])
        self._args = args
        self._chdir = cwd
        self._env = [
            utf8bytes('%s=%s' % (arg, val))
            for arg, val in (env if env is not None else os.environ).items()
            if val is not None
        ]
        self._time = time
        self._wall_time = time * 3 if wall_time is None else wall_time
        self._cpu_time = time + 5 if time else 0
        self._memory = memory
        self._child_personality = personality
        self._child_memory = memory * 1024 + data_grace * 1024
        self._child_address = memory * 1024 + address_grace * 1024 if memory else 0
        self._nproc = nproc
        self._fsize = fsize
        self._tle = False
        self._fds = fds
        self.__init_streams(stdin, stdout, stderr)
        self.protection_fault = None

        self.debugger._syscall_index = index
        self.debugger.address_bits = 64 if debugger in (DEBUGGER_X64,
                                                        DEBUGGER_ARM64) else 32

        self._security = security
        self._callbacks = [None] * MAX_SYSCALL_NUMBER
        self._syscall_whitelist = [False] * MAX_SYSCALL_NUMBER
        if security is None:
            self._trace_syscalls = False
        else:
            for i in range(SYSCALL_COUNT):
                handler = security.get(i, DISALLOW)
                for call in translator[i][index]:
                    if call is None:
                        continue
                    if isinstance(handler, int):
                        self._syscall_whitelist[call] = handler == ALLOW
                    else:
                        if not callable(handler):
                            raise ValueError('Handler not callable: ' +
                                             handler)
                        self._callbacks[call] = handler
                        handler = _CALLBACK
                    self._handler(call, handler)

        self._started = threading.Event()
        self._died = threading.Event()
        if time:
            # Spawn thread to kill process after it times out
            self._shocker = threading.Thread(target=self._shocker_thread)
            self._shocker.start()
        self._worker = threading.Thread(target=self._run_process)
        self._worker.start()
Example #3
0
    def __init__(
            self,
            args,
            avoid_seccomp=False,
            executable=None,
            security=None,
            time=0,
            memory=0,
            stdin=PIPE,
            stdout=PIPE,
            stderr=None,
            env=None,
            nproc=0,
            fsize=0,
            address_grace=4096,
            data_grace=0,
            personality=0,
            cwd='',
            wall_time=None,
    ):
        self._executable = executable or find_exe_in_path(args[0])
        self.use_seccomp = security is not None and not avoid_seccomp

        self._args = args
        self._chdir = cwd
        self._env = [
            utf8bytes('%s=%s' % (arg, val))
            for arg, val in (env if env is not None else os.environ).items()
            if val is not None
        ]
        self._time = time
        self._wall_time = time * 3 if wall_time is None else wall_time
        self._cpu_time = time + 5 if time else 0
        self._memory = memory
        self._child_personality = personality
        self._child_memory = memory * 1024 + data_grace * 1024
        self._child_address = memory * 1024 + address_grace * 1024 if memory else 0
        self._nproc = nproc
        self._fsize = fsize
        self._is_tle = False
        self._is_ole = False
        self.__init_streams(stdin, stdout, stderr)
        self._last_ptrace_errno = None
        self.protection_fault = None

        self._security = security
        self._callbacks = [[None] * MAX_SYSCALL_NUMBER for _ in range(PTBOX_ABI_COUNT)]
        if security is None:
            self._trace_syscalls = False
        else:
            for abi in SUPPORTED_ABIS:
                index = _SYSCALL_INDICIES[abi]
                for i in range(SYSCALL_COUNT):
                    for call in translator[i][index]:
                        if call is None:
                            continue
                        handler = security.get(i, DISALLOW)
                        if not isinstance(handler, int):
                            if not callable(handler):
                                raise ValueError('Handler not callable: ' + handler)
                            self._callbacks[abi][call] = handler
                            handler = _CALLBACK
                        self._handler(abi, call, handler)

        self._died = threading.Event()
        self._spawned_or_errored = threading.Event()
        self._spawn_error = None

        if time:
            # Spawn thread to kill process after it times out
            self._shocker = threading.Thread(target=self._shocker_thread)
            self._shocker.start()
        self._worker = threading.Thread(target=self._run_process)
        self._worker.start()

        self._spawned_or_errored.wait()
        if self._spawn_error:
            raise self._spawn_error