From: =?utf-8?b?TMOhc3psw7MgVsOhcmFkeQ==?= Date: Mon, 31 Jan 2022 00:16:21 +0100 Subject: boxfort: add BXF_DBG_FALLBACK debug flag The flag can be used to indicate that when the specified debugger can not be found, the sandboxed process should be suspended instead. --- include/boxfort.h | 6 ++++++ src/sandbox-posix.c | 24 ++++++++++++++++-------- src/sandbox-posix.h | 4 ++++ src/sandbox-windows.c | 2 +- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/include/boxfort.h b/include/boxfort.h index 1d7a40e..8c1c3a0 100644 --- a/include/boxfort.h +++ b/include/boxfort.h @@ -203,6 +203,8 @@ enum bxf_debugger { BXF_DBG_GDB, /* Spawn with gdbserver */ BXF_DBG_LLDB, /* Spawn with lldb-server */ BXF_DBG_WINDBG, /* Spawn with windbg (Windows only) */ + /* add up to 32 debuggers here */ + BXF_DBG_FALLBACK = 0x20 /* fallback to suspended sandbox */ }; #if defined (__clang__) @@ -213,6 +215,10 @@ enum bxf_debugger { # define BXF_DBG_NATIVE BXF_DBG_WINDBG #endif +#define BXF_DBG_ENABLE_FALLBACK(debugger) (debugger |= BXF_DBG_FALLBACK) +#define BXF_DBG_IS_FALLBACK_ENABLED(debugger) (debugger & BXF_DBG_FALLBACK) +#define BXF_DBG_GET_DEBUGGER(debugger) (debugger & ~BXF_DBG_FALLBACK) + struct bxf_debug { enum bxf_debugger debugger; int tcp; diff --git a/src/sandbox-posix.c b/src/sandbox-posix.c index 9e7b996..7870340 100644 --- a/src/sandbox-posix.c +++ b/src/sandbox-posix.c @@ -675,17 +675,22 @@ int bxfi_exec(bxf_instance **out, bxf_sandbox *sandbox, return -EINVAL; char dbg_full[PATH_MAX]; + int debug_falls_back_to_suspend = 0; if (sandbox->debug.debugger) { const char *dbg = NULL; - switch (sandbox->debug.debugger) { + switch (BXF_DBG_GET_DEBUGGER(sandbox->debug.debugger)) { case BXF_DBG_GDB: dbg = "gdbserver"; break; case BXF_DBG_LLDB: dbg = "lldb-server"; break; default: return -EINVAL; } - if (find_exe(dbg, dbg_full, sizeof (dbg_full)) < 0) - return -ENOENT; + if (find_exe(dbg, dbg_full, sizeof (dbg_full)) < 0) { + if(!BXF_DBG_IS_FALLBACK_ENABLED(sandbox->debug.debugger)) + return -ENOENT; + + debug_falls_back_to_suspend = 1; + } } errnum = -ENOMEM; @@ -694,6 +699,7 @@ int bxfi_exec(bxf_instance **out, bxf_sandbox *sandbox, if (!instance) goto err; *instance = (struct bxfi_sandbox) { + .debug_falls_back_to_suspend = debug_falls_back_to_suspend, .mantled = mantled, .callback = callback, .user = user, @@ -739,7 +745,7 @@ int bxfi_exec(bxf_instance **out, bxf_sandbox *sandbox, local_ctx.ctx->fn = addr.addr; local_ctx.ctx->seg = addr.seg; local_ctx.ctx->pid = pid; - local_ctx.ctx->suspend = sandbox->suspended; + local_ctx.ctx->suspend = sandbox->suspended || instance->debug_falls_back_to_suspend; bxf_context ictx = sandbox->inherit.context; if (ictx) { #ifdef BXF_ARENA_REOPEN_SHM @@ -788,7 +794,7 @@ int bxfi_exec(bxf_instance **out, bxf_sandbox *sandbox, kill(pid, SIGCONT); - if (sandbox->suspended) + if (sandbox->suspended || instance->debug_falls_back_to_suspend) instance->props.status.stopped = 1; *out = &instance->props; @@ -796,7 +802,9 @@ int bxfi_exec(bxf_instance **out, bxf_sandbox *sandbox, } #if defined (HAVE_PR_SET_PDEATHSIG) - int pdeathsig = sandbox->debug.debugger ? SIGTERM : SIGKILL; + int pdeathsig = (BXF_DBG_GET_DEBUGGER(sandbox->debug.debugger) && !instance->debug_falls_back_to_suspend) + ? SIGTERM : SIGKILL; + prctl(PR_SET_PDEATHSIG, pdeathsig); #endif @@ -831,10 +839,10 @@ int bxfi_exec(bxf_instance **out, bxf_sandbox *sandbox, char *argv[16] = { "boxfort-worker" }; size_t argc = 1; - if (sandbox->debug.debugger) { + if (sandbox->debug.debugger && !instance->debug_falls_back_to_suspend) { char port[11]; - switch (sandbox->debug.debugger) { + switch (BXF_DBG_GET_DEBUGGER(sandbox->debug.debugger)) { case BXF_DBG_GDB: snprintf(port, sizeof (port), ":%d", sandbox->debug.tcp); break; diff --git a/src/sandbox-posix.h b/src/sandbox-posix.h index 04db16e..f49fbaa 100644 --- a/src/sandbox-posix.h +++ b/src/sandbox-posix.h @@ -49,6 +49,10 @@ struct bxfi_map { struct bxfi_sandbox { struct bxf_instance_s props; + /* Set to 1 when BXF_DBG_ENABLE_FALLBACK is requested and the + selected debugger is not available */ + int debug_falls_back_to_suspend; + /* A sandbox is said to be mantled if there is an unique instance managing its memory. */ int mantled; diff --git a/src/sandbox-windows.c b/src/sandbox-windows.c index e67b019..381debf 100644 --- a/src/sandbox-windows.c +++ b/src/sandbox-windows.c @@ -410,7 +410,7 @@ int bxfi_exec(bxf_instance **out, bxf_sandbox *sandbox, TCHAR *cmdline = NULL; SIZE_T size; - switch (sandbox->debug.debugger) { + switch (BXF_DBG_GET_DEBUGGER(sandbox->debug.debugger)) { case BXF_DBG_WINDBG: { dbg = TEXT("windbg"); TCHAR *fmt = TEXT("boxfort-worker -server tcp:port=%d %s %s");