From bb1a827bed7ef75240ef967efe42a5c08bb08572 Mon Sep 17 00:00:00 2001 From: Quantum Date: Sun, 19 Sep 2021 19:26:13 -0400 Subject: [PATCH] cptbox: format code according to clang-format rule --- dmoj/cptbox/ext_freebsd.h | 2 +- dmoj/cptbox/helper.cpp | 80 ++++++++++++++------------- dmoj/cptbox/helper.h | 10 ++-- dmoj/cptbox/ptbox.h | 83 +++++++++++++++-------------- dmoj/cptbox/ptdebug.cpp | 14 +++-- dmoj/cptbox/ptdebug_arm.cpp | 44 +++++++-------- dmoj/cptbox/ptdebug_arm64.cpp | 56 +++++++++---------- dmoj/cptbox/ptdebug_freebsd_x64.cpp | 50 ++++++++--------- dmoj/cptbox/ptdebug_x64.cpp | 60 ++++++++++----------- dmoj/cptbox/ptdebug_x86.cpp | 44 +++++++-------- dmoj/cptbox/ptproc.cpp | 55 +++++++++---------- 11 files changed, 251 insertions(+), 247 deletions(-) diff --git a/dmoj/cptbox/ext_freebsd.h b/dmoj/cptbox/ext_freebsd.h index f1bf900a0..787c57ebe 100644 --- a/dmoj/cptbox/ext_freebsd.h +++ b/dmoj/cptbox/ext_freebsd.h @@ -7,7 +7,7 @@ inline long ptrace_traceme() { // Debian GNU/kFreeBSD neglected to define this in their libc. #if defined(__FreeBSD_kernel__) && !defined(PT_FOLLOW_FORK) -# define PT_FOLLOW_FORK 23 +#define PT_FOLLOW_FORK 23 #endif // Constant for wait4 diff --git a/dmoj/cptbox/helper.cpp b/dmoj/cptbox/helper.cpp index 2a72171e7..5e9b2d682 100644 --- a/dmoj/cptbox/helper.cpp +++ b/dmoj/cptbox/helper.cpp @@ -1,34 +1,34 @@ -#include "ptbox.h" #include "helper.h" +#include "ptbox.h" #include #include #include #include #include -#include -#include #include +#include +#include #include #include -#include +#include #ifdef __FreeBSD__ -# include -# include -# include -# include -# include +#include +#include +#include +#include +#include #else // No ASLR on FreeBSD... not as of 11.0, anyway -# include -# include +#include +#include #endif #if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__)) -# define FD_DIR "/dev/fd" +#define FD_DIR "/dev/fd" #else -# define FD_DIR "/proc/self/fd" +#define FD_DIR "/proc/self/fd" #endif inline void setrlimit2(int resource, rlim_t cur, rlim_t max) { @@ -60,9 +60,12 @@ int cptbox_child_run(const struct child_config *config) { #endif #endif - if (config->stdin_ >= 0) dup2(config->stdin_, 0); - if (config->stdout_ >= 0) dup2(config->stdout_, 1); - if (config->stderr_ >= 0) dup2(config->stderr_, 2); + if (config->stdin_ >= 0) + dup2(config->stdin_, 0); + if (config->stdout_ >= 0) + dup2(config->stdout_, 1); + if (config->stderr_ >= 0) + dup2(config->stderr_, 2); cptbox_closefrom(3); if (ptrace_traceme()) { @@ -99,8 +102,7 @@ int cptbox_child_run(const struct child_config *config) { } } else if (handler > 0) { if ((rc = seccomp_rule_add(ctx, SCMP_ACT_ERRNO(handler), syscall, 0))) { - fprintf(stderr, "seccomp_rule_add(..., SCMP_ACT_ERRNO(%d), %d): %s\n", - handler, syscall, strerror(-rc)); + fprintf(stderr, "seccomp_rule_add(..., SCMP_ACT_ERRNO(%d), %d): %s\n", handler, syscall, strerror(-rc)); // This failure is not fatal, it'll just cause the syscall to trap anyway. } } @@ -154,17 +156,19 @@ static int pos_int_from_ascii(char *name) { ++name; } if (*name) - return -1; /* Non digit found, not a number. */ + return -1; /* Non digit found, not a number. */ return num; } static inline void cptbox_close_fd(int fd) { - while (close(fd) < 0 && errno == EINTR); + while (close(fd) < 0 && errno == EINTR) + ; } static void cptbox_closefrom_brute(int lowfd) { int max_fd = sysconf(_SC_OPEN_MAX); - if (max_fd < 0) max_fd = 16384; + if (max_fd < 0) + max_fd = 16384; for (; lowfd <= max_fd; ++lowfd) cptbox_close_fd(lowfd); } @@ -178,13 +182,16 @@ static inline void cptbox_closefrom_dirent(int lowfd) { errno = 0; while ((dir = readdir(d))) { int fd = pos_int_from_ascii(dir->d_name); - if (fd < lowfd || fd == fd_dirent) continue; + if (fd < lowfd || fd == fd_dirent) + continue; cptbox_close_fd(fd); errno = 0; } - if (errno) cptbox_closefrom_brute(lowfd); + if (errno) + cptbox_closefrom_brute(lowfd); closedir(d); - } else cptbox_closefrom_brute(lowfd); + } else + cptbox_closefrom_brute(lowfd); } // Borrowing some SYS_getdents64 magic from python's _posixsubprocess. @@ -193,9 +200,9 @@ static inline void cptbox_closefrom_dirent(int lowfd) { // possibly be exec'd before we close the fd. If it is, we have // bigger problems than leaking the directory fd. #ifdef __linux__ -#include -#include #include +#include +#include struct linux_dirent64 { unsigned long long d_ino; @@ -212,16 +219,14 @@ static inline void cptbox_closefrom_getdents(int lowfd) { } else { char buffer[sizeof(struct linux_dirent64)]; int bytes; - while ((bytes = syscall(SYS_getdents64, fd_dir, - (struct linux_dirent64 *)buffer, - sizeof(buffer))) > 0) { + while ((bytes = syscall(SYS_getdents64, fd_dir, (struct linux_dirent64 *) buffer, sizeof(buffer))) > 0) { struct linux_dirent64 *entry; int offset; for (offset = 0; offset < bytes; offset += entry->d_reclen) { int fd; - entry = (struct linux_dirent64 *)(buffer + offset); + entry = (struct linux_dirent64 *) (buffer + offset); if ((fd = pos_int_from_ascii(entry->d_name)) < 0) - continue; /* Not a number. */ + continue; /* Not a number. */ if (fd != fd_dir && fd >= lowfd) cptbox_close_fd(fd); } @@ -258,20 +263,21 @@ char *bsd_get_proc_fd(pid_t pid, int fdflags, int fdno) { if (kp) { head = procstat_getfiles(procstat, kp, 0); if (head) { - err = EPERM; // Most likely you have no access + err = EPERM; // Most likely you have no access STAILQ_FOREACH(fst, head, next) { - if ((fdflags && fst->fs_uflags & fdflags) || - (!fdflags && fst->fs_fd == fdno)) { - buf = (char*) malloc(strlen(fst->fs_path) + 1); + if ((fdflags && fst->fs_uflags & fdflags) || (!fdflags && fst->fs_fd == fdno)) { + buf = (char *) malloc(strlen(fst->fs_path) + 1); if (buf) strcpy(buf, fst->fs_path); err = buf ? 0 : ENOMEM; break; } } - } else err = errno; + } else + err = errno; procstat_freeprocs(procstat, kp); - } else err = errno; + } else + err = errno; procstat_close(procstat); errno = err; } diff --git a/dmoj/cptbox/helper.h b/dmoj/cptbox/helper.h index 7af6393b4..d519ffcea 100644 --- a/dmoj/cptbox/helper.h +++ b/dmoj/cptbox/helper.h @@ -2,10 +2,12 @@ #ifndef idABBEC9C1_3EF3_4A45_B187B10060CB9F85 #define idABBEC9C1_3EF3_4A45_B187B10060CB9F85 -#define PTBOX_SPAWN_FAIL_NO_NEW_PRIVS 202 -#define PTBOX_SPAWN_FAIL_SECCOMP 203 -#define PTBOX_SPAWN_FAIL_TRACEME 204 -#define PTBOX_SPAWN_FAIL_EXECVE 205 +#include + +#define PTBOX_SPAWN_FAIL_NO_NEW_PRIVS 202 +#define PTBOX_SPAWN_FAIL_SECCOMP 203 +#define PTBOX_SPAWN_FAIL_TRACEME 204 +#define PTBOX_SPAWN_FAIL_EXECVE 205 struct child_config { unsigned long memory; diff --git a/dmoj/cptbox/ptbox.h b/dmoj/cptbox/ptbox.h index 5c7f21ede..d37bb1140 100644 --- a/dmoj/cptbox/ptbox.h +++ b/dmoj/cptbox/ptbox.h @@ -2,46 +2,46 @@ #ifndef idA6398CB6_D711_4634_9D89FF6B1D215169 #define idA6398CB6_D711_4634_9D89FF6B1D215169 -#include #include -#include -#include -#include +#include #include #include +#include +#include +#include #include #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) -# define PTBOX_FREEBSD 1 +#define PTBOX_FREEBSD 1 #else -# define PTBOX_FREEBSD 0 +#define PTBOX_FREEBSD 0 #endif #if PTBOX_FREEBSD #include "ext_freebsd.h" #else -#include #include "ext_linux.h" +#include #endif -#define MAX_SYSCALL 568 -#define PTBOX_HANDLER_DENY 0 -#define PTBOX_HANDLER_ALLOW 1 -#define PTBOX_HANDLER_CALLBACK 2 +#define MAX_SYSCALL 568 +#define PTBOX_HANDLER_DENY 0 +#define PTBOX_HANDLER_ALLOW 1 +#define PTBOX_HANDLER_CALLBACK 2 #define PTBOX_HANDLER_STDOUTERR 3 -#define PTBOX_EVENT_ATTACH 0 -#define PTBOX_EVENT_EXITING 1 -#define PTBOX_EVENT_EXITED 2 -#define PTBOX_EVENT_SIGNAL 3 -#define PTBOX_EVENT_PROTECTION 4 +#define PTBOX_EVENT_ATTACH 0 +#define PTBOX_EVENT_EXITING 1 +#define PTBOX_EVENT_EXITED 2 +#define PTBOX_EVENT_SIGNAL 3 +#define PTBOX_EVENT_PROTECTION 4 #define PTBOX_EVENT_PTRACE_ERROR 5 -#define PTBOX_EVENT_UPDATE_FAIL 6 +#define PTBOX_EVENT_UPDATE_FAIL 6 -#define PTBOX_EXIT_NORMAL 0 +#define PTBOX_EXIT_NORMAL 0 #define PTBOX_EXIT_PROTECTION 1 -#define PTBOX_EXIT_SEGFAULT 2 +#define PTBOX_EXIT_SEGFAULT 2 enum { PTBOX_ABI_X86 = 0, @@ -55,38 +55,36 @@ enum { }; #if PTBOX_FREEBSD && defined(__amd64__) -# include "ptdebug_freebsd_x64.h" +#include "ptdebug_freebsd_x64.h" #elif !PTBOX_FREEBSD && defined(__amd64__) -# include "ptdebug_x64.h" +#include "ptdebug_x64.h" #elif !PTBOX_FREEBSD && defined(__i386__) -# include "ptdebug_x86.h" +#include "ptdebug_x86.h" #elif !PTBOX_FREEBSD && defined(__arm__) -# include "ptdebug_arm.h" +#include "ptdebug_arm.h" #elif !PTBOX_FREEBSD && (defined(__arm64__) || defined(__aarch64__)) -# include "ptdebug_arm64.h" +#include "ptdebug_arm64.h" #endif inline void timespec_add(struct timespec *a, struct timespec *b, struct timespec *result) { - result->tv_sec = a->tv_sec + b->tv_sec ; - result->tv_nsec = a->tv_nsec + b->tv_nsec ; + result->tv_sec = a->tv_sec + b->tv_sec; + result->tv_nsec = a->tv_nsec + b->tv_nsec; if (result->tv_nsec >= 1000000000L) { result->tv_sec++; - result->tv_nsec = result->tv_nsec - 1000000000L ; + result->tv_nsec = result->tv_nsec - 1000000000L; } } inline void timespec_sub(struct timespec *a, struct timespec *b, struct timespec *result) { - if ((a->tv_sec < b->tv_sec) || - ((a->tv_sec == b->tv_sec) && - (a->tv_nsec <= b->tv_nsec))) { /* a <= b? */ - result->tv_sec = result->tv_nsec = 0 ; + if ((a->tv_sec < b->tv_sec) || ((a->tv_sec == b->tv_sec) && (a->tv_nsec <= b->tv_nsec))) { /* a <= b? */ + result->tv_sec = result->tv_nsec = 0; } else { /* a > b */ result->tv_sec = a->tv_sec - b->tv_sec; if (a->tv_nsec < b->tv_nsec) { - result->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec ; + result->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec; result->tv_sec--; /* Borrow a second-> */ } else { - result->tv_nsec = a->tv_nsec - b->tv_nsec ; + result->tv_nsec = a->tv_nsec - b->tv_nsec; } } } @@ -99,7 +97,7 @@ typedef int (*pt_fork_handler)(void *context); typedef int (*pt_event_callback)(void *context, int event, unsigned long param); class pt_process { -public: + public: pt_process(pt_debugger *debugger); void set_callback(pt_handler_callback, void *context); void set_event_proc(pt_event_callback, void *context); @@ -113,10 +111,12 @@ class pt_process { double wall_clock_time(); const rusage *getrusage() { return &_rusage; } bool was_initialized() { return _initialized; } -protected: + + protected: int dispatch(int event, unsigned long param); int protection_fault(int syscall, int type = PTBOX_EVENT_PROTECTION); -private: + + private: pid_t pid; int handler[PTBOX_ABI_COUNT][MAX_SYSCALL]; pt_handler_callback callback; @@ -131,14 +131,14 @@ class pt_process { }; class pt_debugger { -public: + public: pt_debugger(); int syscall(); int syscall(int); long result(); void result(long); - long error(); // would name this errno, but it conflicts with the errno macro + long error(); // would name this errno, but it conflicts with the errno macro void error(long); long arg0(); long arg1(); @@ -162,7 +162,7 @@ class pt_debugger { bool readbytes(unsigned long addr, char *buffer, size_t size); pid_t gettid() { return tid; } - pid_t tid; // TODO maybe call super instead + pid_t tid; // TODO maybe call super instead pid_t getpid() { return process->getpid(); } #if PTBOX_FREEBSD @@ -187,9 +187,10 @@ class pt_debugger { void on_return(pt_syscall_return_callback callback, void *context) { on_return_[tid] = std::make_pair(callback, context); } -private: + + private: pt_process *process; - std::map> on_return_; + std::map> on_return_; int execve_id; int abi_; int abi_from_reg_size(size_t); diff --git a/dmoj/cptbox/ptdebug.cpp b/dmoj/cptbox/ptdebug.cpp index b95a9f0bf..26c2f2c53 100644 --- a/dmoj/cptbox/ptdebug.cpp +++ b/dmoj/cptbox/ptdebug.cpp @@ -1,17 +1,17 @@ #define _DEFAULT_SOURCE #define _BSD_SOURCE +#include "ptbox.h" #include +#include +#include #include #include #include -#include -#include -#include #include +#include #include #include -#include "ptbox.h" #if !PTBOX_FREEBSD #include @@ -130,10 +130,8 @@ typedef long ptrace_read_t; // Android's bionic C library doesn't have this system call wrapper. // Therefore, we use a weak symbol so it could be used when it doesn't exist. -ssize_t __attribute__((weak)) process_vm_readv( - pid_t pid, const struct iovec *lvec, unsigned long liovcnt, - const struct iovec *rvec, unsigned long riovcnt, unsigned long flags -) { +ssize_t __attribute__((weak)) process_vm_readv(pid_t pid, const struct iovec *lvec, unsigned long liovcnt, + const struct iovec *rvec, unsigned long riovcnt, unsigned long flags) { return syscall(SYS_process_vm_readv, (long) pid, lvec, liovcnt, rvec, riovcnt, flags); } #endif // __BIONIC__ diff --git a/dmoj/cptbox/ptdebug_arm.cpp b/dmoj/cptbox/ptdebug_arm.cpp index e9999c2b7..b02ec3cf8 100644 --- a/dmoj/cptbox/ptdebug_arm.cpp +++ b/dmoj/cptbox/ptdebug_arm.cpp @@ -45,28 +45,28 @@ int pt_debugger::syscall(int id) { return 0; } -#define MAKE_ACCESSOR(method, reg_name) \ - long pt_debugger::method() { \ - switch (abi_) { \ - case PTBOX_ABI_ARM: \ - return regs.reg_name; \ - case PTBOX_ABI_INVALID: \ - return -1; \ - default: \ - UNKNOWN_ABI("ptdebug_arm.cpp:" #method " getter"); \ - } \ - } \ - \ - void pt_debugger::method(long value) { \ - regs_changed = true; \ - switch (abi_) { \ - case PTBOX_ABI_ARM: \ - regs.reg_name = value; \ - case PTBOX_ABI_INVALID: \ - return; \ - default: \ - UNKNOWN_ABI("ptdebug_arm.cpp:" #method " setter"); \ - } \ +#define MAKE_ACCESSOR(method, reg_name) \ + long pt_debugger::method() { \ + switch (abi_) { \ + case PTBOX_ABI_ARM: \ + return regs.reg_name; \ + case PTBOX_ABI_INVALID: \ + return -1; \ + default: \ + UNKNOWN_ABI("ptdebug_arm.cpp:" #method " getter"); \ + } \ + } \ + \ + void pt_debugger::method(long value) { \ + regs_changed = true; \ + switch (abi_) { \ + case PTBOX_ABI_ARM: \ + regs.reg_name = value; \ + case PTBOX_ABI_INVALID: \ + return; \ + default: \ + UNKNOWN_ABI("ptdebug_arm.cpp:" #method " setter"); \ + } \ } MAKE_ACCESSOR(result, ARM_r0) diff --git a/dmoj/cptbox/ptdebug_arm64.cpp b/dmoj/cptbox/ptdebug_arm64.cpp index ba9b97d2c..14b330931 100644 --- a/dmoj/cptbox/ptdebug_arm64.cpp +++ b/dmoj/cptbox/ptdebug_arm64.cpp @@ -59,34 +59,34 @@ int pt_debugger::syscall(int id) { return 0; } -#define MAKE_ACCESSOR(method, arm32_name, arm64_name) \ - long pt_debugger::method() { \ - switch (abi_) { \ - case PTBOX_ABI_ARM: \ - return regs.arm32.arm32_name; \ - case PTBOX_ABI_ARM64: \ - return regs.arm64.arm64_name; \ - case PTBOX_ABI_INVALID: \ - return -1; \ - default: \ - UNKNOWN_ABI("ptdebug_arm64.cpp:" #method " getter"); \ - } \ - } \ - \ - void pt_debugger::method(long value) { \ - regs_changed = true; \ - switch (abi_) { \ - case PTBOX_ABI_ARM: \ - regs.arm32.arm32_name = value; \ - return; \ - case PTBOX_ABI_ARM64: \ - regs.arm64.arm64_name = value; \ - return; \ - case PTBOX_ABI_INVALID: \ - return; \ - default: \ - UNKNOWN_ABI("ptdebug_arm64.cpp:" #method " setter"); \ - } \ +#define MAKE_ACCESSOR(method, arm32_name, arm64_name) \ + long pt_debugger::method() { \ + switch (abi_) { \ + case PTBOX_ABI_ARM: \ + return regs.arm32.arm32_name; \ + case PTBOX_ABI_ARM64: \ + return regs.arm64.arm64_name; \ + case PTBOX_ABI_INVALID: \ + return -1; \ + default: \ + UNKNOWN_ABI("ptdebug_arm64.cpp:" #method " getter"); \ + } \ + } \ + \ + void pt_debugger::method(long value) { \ + regs_changed = true; \ + switch (abi_) { \ + case PTBOX_ABI_ARM: \ + regs.arm32.arm32_name = value; \ + return; \ + case PTBOX_ABI_ARM64: \ + regs.arm64.arm64_name = value; \ + return; \ + case PTBOX_ABI_INVALID: \ + return; \ + default: \ + UNKNOWN_ABI("ptdebug_arm64.cpp:" #method " setter"); \ + } \ } MAKE_ACCESSOR(result, r0, regs[0]) diff --git a/dmoj/cptbox/ptdebug_freebsd_x64.cpp b/dmoj/cptbox/ptdebug_freebsd_x64.cpp index 9041fe799..e343adbda 100644 --- a/dmoj/cptbox/ptdebug_freebsd_x64.cpp +++ b/dmoj/cptbox/ptdebug_freebsd_x64.cpp @@ -30,10 +30,10 @@ int pt_debugger::syscall(int id) { switch (abi_) { case PTBOX_ABI_FREEBSD_X64: regs.r_rax = id; - return 0; \ + return 0; case PTBOX_ABI_INVALID: return EINVAL; - default: \ + default: UNKNOWN_ABI("ptdebug_freebsd_x64.cpp:syscall setter"); } } @@ -88,29 +88,29 @@ void pt_debugger::error(long value) { } } -#define MAKE_ACCESSOR(method, reg_name) \ - long pt_debugger::method() { \ - switch (abi_) { \ - case PTBOX_ABI_FREEBSD_X64: \ - return regs.r_##reg_name; \ - case PTBOX_ABI_INVALID: \ - return -1; \ - default: \ - UNKNOWN_ABI("ptdebug_freebsd_x64.cpp:" #method " getter"); \ - } \ - } \ - \ - void pt_debugger::method(long value) { \ - regs_changed = true; \ - switch (abi_) { \ - case PTBOX_ABI_FREEBSD_X64: \ - regs.r_##reg_name = value; \ - return; \ - case PTBOX_ABI_INVALID: \ - return; \ - default: \ - UNKNOWN_ABI("ptdebug_freebsd_x64.cpp:" #method " setter"); \ - } \ +#define MAKE_ACCESSOR(method, reg_name) \ + long pt_debugger::method() { \ + switch (abi_) { \ + case PTBOX_ABI_FREEBSD_X64: \ + return regs.r_##reg_name; \ + case PTBOX_ABI_INVALID: \ + return -1; \ + default: \ + UNKNOWN_ABI("ptdebug_freebsd_x64.cpp:" #method " getter"); \ + } \ + } \ + \ + void pt_debugger::method(long value) { \ + regs_changed = true; \ + switch (abi_) { \ + case PTBOX_ABI_FREEBSD_X64: \ + regs.r_##reg_name = value; \ + return; \ + case PTBOX_ABI_INVALID: \ + return; \ + default: \ + UNKNOWN_ABI("ptdebug_freebsd_x64.cpp:" #method " setter"); \ + } \ } MAKE_ACCESSOR(arg0, rdi) diff --git a/dmoj/cptbox/ptdebug_x64.cpp b/dmoj/cptbox/ptdebug_x64.cpp index feb0b6626..41a3956e3 100644 --- a/dmoj/cptbox/ptdebug_x64.cpp +++ b/dmoj/cptbox/ptdebug_x64.cpp @@ -72,36 +72,36 @@ int pt_debugger::syscall(int id) { } } -#define MAKE_ACCESSOR(method, x86_name, x64_name) \ - long pt_debugger::method() { \ - switch (abi_) { \ - case PTBOX_ABI_X86: \ - return regs.x86.x86_name; \ - case PTBOX_ABI_X32: \ - case PTBOX_ABI_X64: \ - return regs.x64.x64_name; \ - case PTBOX_ABI_INVALID: \ - return -1; \ - default: \ - UNKNOWN_ABI("ptdebug_x64.cpp:" #method " getter"); \ - } \ - } \ - \ - void pt_debugger::method(long value) { \ - regs_changed = true; \ - switch (abi_) { \ - case PTBOX_ABI_X86: \ - regs.x86.x86_name = value; \ - return; \ - case PTBOX_ABI_X32: \ - case PTBOX_ABI_X64: \ - regs.x64.x64_name = value; \ - return; \ - case PTBOX_ABI_INVALID: \ - return; \ - default: \ - UNKNOWN_ABI("ptdebug_x64.cpp:" #method " setter"); \ - } \ +#define MAKE_ACCESSOR(method, x86_name, x64_name) \ + long pt_debugger::method() { \ + switch (abi_) { \ + case PTBOX_ABI_X86: \ + return regs.x86.x86_name; \ + case PTBOX_ABI_X32: \ + case PTBOX_ABI_X64: \ + return regs.x64.x64_name; \ + case PTBOX_ABI_INVALID: \ + return -1; \ + default: \ + UNKNOWN_ABI("ptdebug_x64.cpp:" #method " getter"); \ + } \ + } \ + \ + void pt_debugger::method(long value) { \ + regs_changed = true; \ + switch (abi_) { \ + case PTBOX_ABI_X86: \ + regs.x86.x86_name = value; \ + return; \ + case PTBOX_ABI_X32: \ + case PTBOX_ABI_X64: \ + regs.x64.x64_name = value; \ + return; \ + case PTBOX_ABI_INVALID: \ + return; \ + default: \ + UNKNOWN_ABI("ptdebug_x64.cpp:" #method " setter"); \ + } \ } MAKE_ACCESSOR(result, eax, rax) diff --git a/dmoj/cptbox/ptdebug_x86.cpp b/dmoj/cptbox/ptdebug_x86.cpp index e06708748..1877883ed 100644 --- a/dmoj/cptbox/ptdebug_x86.cpp +++ b/dmoj/cptbox/ptdebug_x86.cpp @@ -49,28 +49,28 @@ int pt_debugger::syscall(int id) { } } -#define MAKE_ACCESSOR(method, reg_name) \ - long pt_debugger::method() { \ - switch (abi_) { \ - case PTBOX_ABI_X86: \ - return regs.reg_name; \ - case PTBOX_ABI_INVALID: \ - return -1; \ - default: \ - UNKNOWN_ABI("ptdebug_x86.cpp:" #method " getter"); \ - } \ - } \ - \ - void pt_debugger::method(long value) { \ - regs_changed = true; \ - switch (abi_) { \ - case PTBOX_ABI_X86: \ - regs.reg_name = value; \ - case PTBOX_ABI_INVALID: \ - return; \ - default: \ - UNKNOWN_ABI("ptdebug_x86.cpp:" #method " setter"); \ - } \ +#define MAKE_ACCESSOR(method, reg_name) \ + long pt_debugger::method() { \ + switch (abi_) { \ + case PTBOX_ABI_X86: \ + return regs.reg_name; \ + case PTBOX_ABI_INVALID: \ + return -1; \ + default: \ + UNKNOWN_ABI("ptdebug_x86.cpp:" #method " getter"); \ + } \ + } \ + \ + void pt_debugger::method(long value) { \ + regs_changed = true; \ + switch (abi_) { \ + case PTBOX_ABI_X86: \ + regs.reg_name = value; \ + case PTBOX_ABI_INVALID: \ + return; \ + default: \ + UNKNOWN_ABI("ptdebug_x86.cpp:" #method " setter"); \ + } \ } MAKE_ACCESSOR(result, eax) diff --git a/dmoj/cptbox/ptproc.cpp b/dmoj/cptbox/ptproc.cpp index fce1a3610..6efe646e2 100644 --- a/dmoj/cptbox/ptproc.cpp +++ b/dmoj/cptbox/ptproc.cpp @@ -2,26 +2,24 @@ #define _BSD_SOURCE #include +#include #include #include -#include #include -#include -#include +#include #include +#include #include +#include #include -#include #include #include "ptbox.h" -pt_process::pt_process(pt_debugger *debugger) : - pid(0), callback(NULL), context(NULL), debugger(debugger), - event_proc(NULL), event_context(NULL), _trace_syscalls(true), - _initialized(false) -{ +pt_process::pt_process(pt_debugger *debugger) + : pid(0), callback(NULL), context(NULL), debugger(debugger), event_proc(NULL), event_context(NULL), + _trace_syscalls(true), _initialized(false) { memset(&exec_time, 0, sizeof exec_time); memset(&start_time, 0, sizeof exec_time); memset(&end_time, 0, sizeof exec_time); @@ -121,14 +119,14 @@ int pt_process::monitor() { int signal = 0; bool trap_next_syscall_event = _trace_syscalls && PTBOX_FREEBSD; - //printf("pid: %d (%d)\n", pid, this->pid); + // printf("pid: %d (%d)\n", pid, this->pid); if (WIFEXITED(status) || WIFSIGNALED(status)) { if (first || pid == pgid) break; else { children.erase(pid); - //printf("Thread/Process exit: %d\n", pid); + // printf("Thread/Process exit: %d\n", pid); continue; } } @@ -136,13 +134,13 @@ int pt_process::monitor() { #if PTBOX_FREEBSD ptrace(PT_LWPINFO, pid, (caddr_t) &lwpi, sizeof lwpi); - //if (lwpi.pl_flags & PL_FLAG_FORKED) + // if (lwpi.pl_flags & PL_FLAG_FORKED) // printf("Created process: %d\n", lwpi.pl_child_pid); if (lwpi.pl_flags & PL_FLAG_CHILD) { ptrace(PT_FOLLOW_FORK, pid, 0, 1); children.insert(pid); - //printf("Started process: %d\n", pid); + // printf("Started process: %d\n", pid); } #endif @@ -160,7 +158,7 @@ int pt_process::monitor() { // This is right after SIGSTOP is received: ptrace(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEEXIT | PTRACE_O_TRACESECCOMP | PTRACE_O_TRACESYSGOOD | PTRACE_O_EXITKILL | - PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK); + PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK); #endif // We now set the process group to the actual pgid. pgid = pid; @@ -170,14 +168,13 @@ int pt_process::monitor() { goto resume_process; } - //printf("%d: WSTOPSIG(status): %d\n", pid, WSTOPSIG(status)); + // printf("%d: WSTOPSIG(status): %d\n", pid, WSTOPSIG(status)); #if PTBOX_FREEBSD if (WSTOPSIG(status) == SIGTRAP && lwpi.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX)) { debugger->setpid(pid); debugger->update_syscall(&lwpi); #else - if ((status >> 8) == (SIGTRAP | PTRACE_EVENT_SECCOMP << 8) || - WSTOPSIG(status) == (0x80 | SIGTRAP)) { + if ((status >> 8) == (SIGTRAP | PTRACE_EVENT_SECCOMP << 8) || WSTOPSIG(status) == (0x80 | SIGTRAP)) { debugger->settid(pid); #endif if ((err = debugger->pre_syscall()) != 0) { @@ -212,7 +209,7 @@ int pt_process::monitor() { in_syscall = (status >> 8) == (SIGTRAP | PTRACE_EVENT_SECCOMP << 8); #endif - //printf("%d: %s syscall %d\n", pid, in_syscall ? "Enter" : "Exit", syscall); + // printf("%d: %s syscall %d\n", pid, in_syscall ? "Enter" : "Exit", syscall); if (!spawned) { if (debugger->is_end_of_first_execve()) { spawned = this->_initialized = true; @@ -239,19 +236,19 @@ int pt_process::monitor() { case PTBOX_HANDLER_CALLBACK: if (callback(context, syscall)) break; - //printf("Killed by callback: %d\n", syscall); + // printf("Killed by callback: %d\n", syscall); exit_reason = protection_fault(syscall); continue; default: // Default is to kill, safety first. - //printf("Killed by DISALLOW or None: %d\n", syscall); + // printf("Killed by DISALLOW or None: %d\n", syscall); exit_reason = protection_fault(syscall); continue; } - // We pass any system call that we can't record in our fixed-size array to python. - // Python will decide your fate. + // We pass any system call that we can't record in our fixed-size array to python. + // Python will decide your fate. } else if (!callback(context, syscall)) { - //printf("Killed by callback: %d\n", syscall); + // printf("Killed by callback: %d\n", syscall); exit_reason = protection_fault(syscall); continue; } @@ -264,7 +261,7 @@ int pt_process::monitor() { trap_next_syscall_event = true; } else { // Fire the on_return handler if we are in a syscall-exit-stop. - std::pair callback = debugger->on_return_[pid]; + std::pair callback = debugger->on_return_[pid]; callback.first(callback.second, pid, syscall); debugger->on_return_.erase(pid); } @@ -312,7 +309,7 @@ int pt_process::monitor() { case PTRACE_EVENT_CLONE: { unsigned long tid; ptrace(PTRACE_GETEVENTMSG, pid, NULL, &tid); - //printf("Created thread: %d\n", tid); + // printf("Created thread: %d\n", tid); break; } case PTRACE_EVENT_FORK: @@ -320,7 +317,7 @@ int pt_process::monitor() { unsigned long npid; ptrace(PTRACE_GETEVENTMSG, pid, NULL, &npid); children.insert(npid); - //printf("Created process: %d\n", npid); + // printf("Created process: %d\n", npid); break; } } @@ -331,10 +328,10 @@ int pt_process::monitor() { #endif // Only main process signals are meaningful. - if (!first && pid == pgid) // *** Don't set _signal to SIGSTOP if this is the /first/ SIGSTOP + if (!first && pid == pgid) // *** Don't set _signal to SIGSTOP if this is the /first/ SIGSTOP dispatch(PTBOX_EVENT_SIGNAL, WSTOPSIG(status)); } -resume_process: + resume_process: // Pass NULL as signal in case of our first SIGSTOP because the runtime tends to resend it, making all our // work for naught. Like abort(), it catches the signal, prints something (^Z?) and then resends it. // Doing this prevents a second SIGSTOP from being dispatched to our event handler above. *** @@ -342,7 +339,7 @@ int pt_process::monitor() { ptrace(trap_next_syscall_event ? PT_SYSCALL : PT_CONTINUE, pid, (caddr_t) 1, first ? 0 : signal); #else ptrace(trap_next_syscall_event ? PTRACE_SYSCALL : PTRACE_CONT, pid, NULL, - first ? NULL : (void*) (intptr_t) signal); + first ? NULL : (void *) (intptr_t) signal); #endif first = false; }