From 812c24b5b87218f1e68a1c50e62fefc28c5d1c79 Mon Sep 17 00:00:00 2001 From: "Stuart B. Wilkins" Date: Sun, 8 Nov 2026 16:10:38 -0500 Subject: [PATCH 01/37] feat(bwopencode2): add --serve to run the web UI server in the sandbox Runs opencode2 serve inside the sandbox on 127.0.0.1, so tools stay sandboxed, with one server per invocation on the first free port of a fixed range (24700-24709 by default) so one ssh_config block covers every session. The password is random for each session unless BWOPENCODE2_SERVE_PASSWORD is set, and reaches the server only as OPENCODE_PASSWORD. Ports are claimed with a host-side file so two launchers cannot pick the same one. --serve-ssh-config prints the matching ssh_config block. Assisted-by: opencode:claude-sonnet-5-5 --- bin/bwopencode2 | 343 +++++++++++++++++++++++++++++++++++++++++++- docs/bw-wrappers.md | 72 +++++++++- 2 files changed, 413 insertions(+), 2 deletions(-) diff --git a/bin/bwopencode2 b/bin/bwopencode2 index c39bdc2..9906405 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -79,6 +79,12 @@ # commands that accept it and refuses --server and the server-related # commands (service, serve, api, pair, reload). See --help. # +# --serve is the one deliberate exception: it runs `opencode2 serve` INSIDE this +# sandbox (tools stay sandboxed), bound to 127.0.0.1 on a free port from a fixed +# range and protected by a password, so the OpenCode web UI can be opened from a +# browser (over an ssh -L tunnel). The launcher builds those arguments itself; +# nothing from the command line reaches `serve`. See docs/bw-wrappers.md. +# # Usage: # bwopencode2 [bwopencode2-options] [opencode2 arguments...] # bwopencode2 --help @@ -93,6 +99,13 @@ # --new-session Force bwrap --new-session even on kernels with the TIOCSTI cap # guard. Detaches the controlling tty for stricter isolation at the # cost of SIGWINCH (terminal resize) propagation. +# --serve Run the OpenCode web UI server inside the sandbox instead of the +# TUI (see above). One server per invocation. +# --serve-port N With --serve: use port N (must be in the range) instead of the +# first free one. +# --serve-ssh-config +# Print the ssh_config block that forwards the whole port range, then +# exit. # --n2c-token NAME Mount the N2C access token NAME read-only (for NSLS-II API # skills). May be repeated; `all` mounts every token present. # Off by default. The refresh token is never mounted. @@ -145,6 +158,24 @@ source "${SCRIPT_DIR}/../lib/bwrap_sandbox_lib.sh" FORWARD_GH_TOKENS=0 OC2_CONFIG_BINDS=() +# --serve state. The password is passed to the server only as OPENCODE_PASSWORD +# (see build_opencode2_env) and is never written to a file. +SERVE=0 +SERVE_SSH_CONFIG=0 +SERVE_PORT_REQUEST="" +SERVE_PORT="" +SERVE_PASSWORD="" +SERVE_PASSWORD_RANDOM=0 +SERVE_PORT_LO="" +SERVE_PORT_HI="" +SERVE_STATE_DIR="" +# Default range: below the kernel's ephemeral range (32768-60999 on Linux, so no +# outgoing connection takes a server port), clear of the shared OpenCode service +# (49374) and of the ports in use on the workstations. Keep ssh_config in step +# with it: --serve-ssh-config prints the matching block. +SERVE_DEFAULT_PORTS="24700-24709" +SERVE_SHARED_SERVICE_PORT=49374 + # ═══════════════════════════════════════════════════════════════════ # Tool-specific functions # ═══════════════════════════════════════════════════════════════════ @@ -190,6 +221,22 @@ parse_wrapper_args() { SANDBOX_CONTEXT_ENABLED=0 shift ;; + --serve) + SERVE=1 + shift + ;; + --serve-port) + if [[ $# -lt 2 ]]; then + echo "Error: --serve-port requires a port number." >&2 + exit 1 + fi + SERVE_PORT_REQUEST="$2" + shift 2 + ;; + --serve-ssh-config) + SERVE_SSH_CONFIG=1 + shift + ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 @@ -237,6 +284,21 @@ Wrapper options (consumed before opencode2 sees the command line): (< 5.14); on newer kernels we omit it so that SIGWINCH (terminal resize) propagates from tmux/screen. Use this flag to opt back into stricter terminal isolation when SIGWINCH is not important. + --serve Run the OpenCode web UI server inside the sandbox instead of the + TUI: `opencode2 serve` on 127.0.0.1 (never another address), on + the first free port of the range 24700-24709 (override the range + with BWOPENCODE2_SERVE_PORTS=LO-HI), tools still sandboxed. Open + it from a browser through an ssh tunnel; the command and the + password are printed at start. The password is random for each + session unless BWOPENCODE2_SERVE_PASSWORD is set (at least 12 + characters; a fixed password is shared by every session that + uses it). Takes no opencode2 arguments and cannot be combined + with --exec. One server per invocation. + --serve-port N With --serve, use port N (inside the range) instead of the first + free one; fails if it is taken. + --serve-ssh-config + Print the ssh_config block (one alias, one LocalForward per port + of the range) for the machine your browser runs on, then exit. --n2c-token NAME Mount the N2C access token NAME (read-only) so skills inside the sandbox can call NSLS-II APIs, for example observability, @@ -288,6 +350,7 @@ that take a value are only recognised for --session/-s, --prompt, --log-level and --completions. Arguments after `--` are never inspected. --exec runs an arbitrary command and therefore bypasses this argument guard. +--serve is the one way to run `serve`: the wrapper builds its arguments itself. Database guard: the OpenCode 2 database is on NFS, where SQLite's WAL mode is only safe from one host. bwopencode2 refuses to start while another host, or @@ -311,6 +374,257 @@ HELP_EOF bwrap_show_tool_help opencode2 } +# ── --serve: OpenCode web UI server inside the sandbox ─────────── +# `opencode2 serve` is refused as a command-line argument (see the guard below) +# because the shared service it could reach runs tools outside any sandbox. +# --serve runs the server inside THIS sandbox instead, so tools stay sandboxed: +# - bound to 127.0.0.1 only; reach it from a browser with an ssh -L tunnel; +# - one server per invocation, on the first free port of a fixed range, so one +# ssh_config block covers every session (--serve-ssh-config); +# - protected by a password that reaches the server only as OPENCODE_PASSWORD. +# It is random for each session unless BWOPENCODE2_SERVE_PASSWORD is set. +# A fixed password is shared by all sessions that use it: any process that +# holds it (including a sandboxed agent that can read its own environment) +# can drive every one of them. +# The launcher execs bwrap, so it cannot clean up or retry after the server +# starts. A port is claimed with a file in the host-side state directory (not +# mounted in the sandbox), which stops two launchers picking one port; a claim +# whose process is gone is stale and is removed. The remaining race is another +# program taking the port in the moment before the server binds it. + +serve_host_name() { + hostname -f 2> /dev/null || hostname +} + +serve_ssh_alias() { + local _h + _h="$(hostname -s 2> /dev/null || hostname)" + printf '%s-oc' "${_h%%.*}" +} + +# Validate BWOPENCODE2_SERVE_PORTS (LO-HI) into SERVE_PORT_LO / SERVE_PORT_HI. +serve_parse_range() { + local _spec="${BWOPENCODE2_SERVE_PORTS:-${SERVE_DEFAULT_PORTS}}" + if [[ ! "${_spec}" =~ ^([0-9]+)-([0-9]+)$ ]]; then + echo "Error: BWOPENCODE2_SERVE_PORTS must look like 24700-24709 (got '${_spec}')." >&2 + exit 1 + fi + SERVE_PORT_LO=$((10#${BASH_REMATCH[1]})) + SERVE_PORT_HI=$((10#${BASH_REMATCH[2]})) + if ((SERVE_PORT_LO < 1024 || SERVE_PORT_HI > 65535 || SERVE_PORT_LO > SERVE_PORT_HI)); then + echo "Error: BWOPENCODE2_SERVE_PORTS must be an ascending range within 1024-65535 (got '${_spec}')." >&2 + exit 1 + fi + if ((SERVE_PORT_HI - SERVE_PORT_LO > 99)); then + echo "Error: BWOPENCODE2_SERVE_PORTS may hold at most 100 ports (got '${_spec}')." >&2 + exit 1 + fi + if ((SERVE_SHARED_SERVICE_PORT >= SERVE_PORT_LO && SERVE_SHARED_SERVICE_PORT <= SERVE_PORT_HI)); then + echo "Error: BWOPENCODE2_SERVE_PORTS must not include ${SERVE_SHARED_SERVICE_PORT}, the port of the shared OpenCode service." >&2 + exit 1 + fi + local _eph_lo _eph_hi + if read -r _eph_lo _eph_hi < /proc/sys/net/ipv4/ip_local_port_range 2> /dev/null && + [[ "${_eph_lo}" =~ ^[0-9]+$ && "${_eph_hi}" =~ ^[0-9]+$ ]] && + ((SERVE_PORT_HI >= _eph_lo && SERVE_PORT_LO <= _eph_hi)); then + printf 'bwopencode2: warning: ports %s-%s overlap the kernel ephemeral range %s-%s; an outgoing connection can take a server port.\n' \ + "${SERVE_PORT_LO}" "${SERVE_PORT_HI}" "${_eph_lo}" "${_eph_hi}" >&2 + fi +} + +# Print the ssh_config block for the port range (--serve-ssh-config). +serve_print_ssh_config() { + local _p + printf '# Add to ~/.ssh/config on the machine your browser runs on, then: ssh %s\n' "$(serve_ssh_alias)" + printf '# One forward per port of the bwopencode2 --serve range %s-%s. A port with no\n' "${SERVE_PORT_LO}" "${SERVE_PORT_HI}" + printf '# session behind it just refuses connections. Add User / ProxyJump as needed.\n' + printf 'Host %s\n' "$(serve_ssh_alias)" + printf ' HostName %s\n' "$(serve_host_name)" + printf ' ExitOnForwardFailure no\n' + for ((_p = SERVE_PORT_LO; _p <= SERVE_PORT_HI; _p++)); do + printf ' LocalForward %s 127.0.0.1:%s\n' "${_p}" "${_p}" + done +} + +# Refuse combinations that make no sense; validate --serve-port. +serve_check_args() { + if [[ "${SERVE}" -eq 0 ]]; then + if [[ -n "${SERVE_PORT_REQUEST}" ]]; then + echo "Error: --serve-port needs --serve." >&2 + exit 1 + fi + return 0 + fi + if [[ -n "${TEST_CMD}" ]]; then + echo "Error: --serve cannot be combined with --exec." >&2 + exit 1 + fi + if [[ ${#TOOL_ARGS[@]} -gt 0 ]]; then + echo "Error: --serve takes no opencode2 arguments (got '${TOOL_ARGS[*]}')." >&2 + exit 1 + fi + if [[ -n "${SERVE_PORT_REQUEST}" ]]; then + if [[ ! "${SERVE_PORT_REQUEST}" =~ ^[0-9]+$ ]] || + ((10#${SERVE_PORT_REQUEST} < SERVE_PORT_LO || 10#${SERVE_PORT_REQUEST} > SERVE_PORT_HI)); then + echo "Error: --serve-port must be a port in ${SERVE_PORT_LO}-${SERVE_PORT_HI} (the ssh_config range), got '${SERVE_PORT_REQUEST}'." >&2 + exit 1 + fi + SERVE_PORT_REQUEST=$((10#${SERVE_PORT_REQUEST})) + fi +} + +# True when nothing listens on 127.0.0.1:PORT (the bind succeeds, as the server's +# would; SO_REUSEADDR as a server sets it, so a lingering TIME_WAIT does not count). +serve_port_is_free() { + local _port="$1" + if command -v python3 > /dev/null 2>&1; then + python3 -c ' +import socket, sys +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) +s.bind(("127.0.0.1", int(sys.argv[1]))) +' "${_port}" 2> /dev/null + else + # No python3: a successful connection means something listens. + if (exec 3<> "/dev/tcp/127.0.0.1/${_port}") 2> /dev/null; then + return 1 + fi + fi +} + +# True when claim file F belongs to a launcher that is still running. A file +# without a pid is being written by another launcher: live unless it is old. +serve_claim_is_live() { + local _f="$1" _pid _mtime _now + [[ -e "${_f}" ]] || return 1 + _pid="$(sed -n 's/^pid=//p' "${_f}" 2> /dev/null | head -n 1)" + if [[ "${_pid}" =~ ^[0-9]+$ ]]; then + kill -0 "${_pid}" 2> /dev/null + return + fi + _mtime="$(stat -c %Y "${_f}" 2> /dev/null || echo 0)" + _now="$(date +%s)" + ((_now - _mtime < 10)) +} + +# Claim PORT atomically. The file holds no secret: the pid is this launcher's, +# which stays the pid of the bwrap process because the launch ends in exec. +serve_claim_port() { + local _port="$1" _f="${SERVE_STATE_DIR}/$1.session" + if serve_claim_is_live "${_f}"; then + return 1 + fi + rm -f "${_f}" + (set -o noclobber && : > "${_f}") 2> /dev/null || return 1 + { + printf 'pid=%s\n' "$$" + printf 'port=%s\n' "${_port}" + printf 'host=%s\n' "$(serve_host_name)" + printf 'dir=%s\n' "${_PWD}" + printf 'started=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" + } > "${_f}" +} + +# Pick the port (the requested one, or the first free one of the range). On +# --dry-run nothing is claimed and no directory is created. +serve_choose_port() { + local _p _f + SERVE_STATE_DIR="${_HOME}/.local/state/bwopencode2/serve" + local -a _candidates=() + if [[ -n "${SERVE_PORT_REQUEST}" ]]; then + _candidates=("${SERVE_PORT_REQUEST}") + else + for ((_p = SERVE_PORT_LO; _p <= SERVE_PORT_HI; _p++)); do + _candidates+=("${_p}") + done + fi + + if [[ "${DRY_RUN}" -eq 0 ]]; then + mkdir -p "${SERVE_STATE_DIR}" + chmod 700 "${SERVE_STATE_DIR}" + for _f in "${SERVE_STATE_DIR}"/*.session; do + [[ -e "${_f}" ]] || continue + serve_claim_is_live "${_f}" || rm -f "${_f}" + done + fi + + for _p in "${_candidates[@]}"; do + _f="${SERVE_STATE_DIR}/${_p}.session" + if [[ "${DRY_RUN}" -eq 1 ]]; then + serve_claim_is_live "${_f}" && continue + serve_port_is_free "${_p}" || continue + else + serve_claim_port "${_p}" || continue + if ! serve_port_is_free "${_p}"; then + rm -f "${_f}" + continue + fi + fi + SERVE_PORT="${_p}" + return 0 + done + + if [[ -n "${SERVE_PORT_REQUEST}" ]]; then + echo "Error: port ${SERVE_PORT_REQUEST} is in use." >&2 + else + echo "Error: no free port in ${SERVE_PORT_LO}-${SERVE_PORT_HI}; stop a session or see ${SERVE_STATE_DIR}/." >&2 + fi + exit 1 +} + +# Set SERVE_PASSWORD: BWOPENCODE2_SERVE_PASSWORD, else a random one. +serve_make_password() { + if [[ -n "${BWOPENCODE2_SERVE_PASSWORD:-}" ]]; then + if ((${#BWOPENCODE2_SERVE_PASSWORD} < 12)); then + echo "Error: BWOPENCODE2_SERVE_PASSWORD must have at least 12 characters." >&2 + exit 1 + fi + SERVE_PASSWORD="${BWOPENCODE2_SERVE_PASSWORD}" + SERVE_PASSWORD_RANDOM=0 + return 0 + fi + if command -v openssl > /dev/null 2>&1; then + SERVE_PASSWORD="$(openssl rand -hex 16)" + else + SERVE_PASSWORD="$(head -c 16 /dev/urandom | od -An -tx1 | tr -d ' \n')" + fi + if [[ ${#SERVE_PASSWORD} -lt 32 ]]; then + echo "Error: could not generate a random password." >&2 + exit 1 + fi + SERVE_PASSWORD_RANDOM=1 +} + +# Choose port and password, and give the server its arguments. +serve_prepare() { + serve_choose_port + serve_make_password + TOOL_ARGS=(serve --hostname 127.0.0.1 --port "${SERVE_PORT}") +} + +# Tell the user how to reach the server (stderr, before the exec). +serve_print_info() { + local _p="${SERVE_PORT}" _host _alias + _host="$(serve_host_name)" + _alias="$(serve_ssh_alias)" + { + printf '\nbwopencode2: sandboxed OpenCode 2 web UI on 127.0.0.1:%s (this host only)\n' "${_p}" + printf ' 1. On the machine with your browser, forward the port:\n' + printf ' ssh -L %s:127.0.0.1:%s %s\n' "${_p}" "${_p}" "${_host}" + printf ' or use the ssh_config alias (bwopencode2 --serve-ssh-config): ssh %s\n' "${_alias}" + printf ' (on this host itself, skip this step)\n' + printf ' 2. Open http://127.0.0.1:%s/ and enter the server password when asked.\n' "${_p}" + if [[ "${DRY_RUN}" -eq 1 ]]; then + printf ' Password: (not shown on --dry-run)\n' + elif [[ "${SERVE_PASSWORD_RANDOM}" -eq 1 ]]; then + printf ' Password (new for this session): %s\n' "${SERVE_PASSWORD}" + else + printf ' Password: the value of BWOPENCODE2_SERVE_PASSWORD (shared by every session that uses it)\n' + fi + printf ' Keep the same port number at both ends of the tunnel: the browser cookie is named by it.\n\n' + } >&2 +} + # ── Sandbox-escape guard (arguments) ───────────────────────────── # The OpenCode 2 background service listens on a fixed, shared port and runs # tools outside any sandbox. Because the sandbox shares the host network, a @@ -786,6 +1100,13 @@ build_opencode2_env() { printf 'bwopencode2: dry-run: dropped host OPENCODE_* variables (not in allowlist): %s\n' "${_dropped[*]}" >&2 fi + # --serve: the password of the server this sandbox itself runs. It is set + # here, after the host variables were dropped above, so it is the only + # OPENCODE_PASSWORD the sandbox sees and never one of the host's. + if [[ "${SERVE}" -eq 1 ]]; then + SANDBOX_ENV+=("OPENCODE_PASSWORD=${SERVE_PASSWORD}") + fi + # GitHub CLI token passthrough (opt-in via --github-tokens). In # --dry-run, print_dry_run redacts the values (the names match *TOKEN*). if [[ "${FORWARD_GH_TOKENS}" -eq 1 ]]; then @@ -805,6 +1126,11 @@ build_opencode2_context() { "Only run \`opencode2\` with \`--standalone\`. Without it the CLI talks to a shared background service (port 49374) that runs tools OUTSIDE this sandbox." "Plan mode writes plan files to \`~/.opencode/plan/\`, which is not mounted: they are lost at exit unless the plan-mirror plugin keeps them." ) + if [[ "${SERVE}" -eq 1 ]]; then + SANDBOX_CONTEXT_NOTES+=( + "This session is a sandboxed \`opencode2 serve\` (the OpenCode web UI), started by \`bwopencode2 --serve\`. Do not start or connect to any other OpenCode server." + ) + fi build_sandbox_context bwopencode2 [[ -n "${SANDBOX_CONTEXT_PATH}" && -z "${TEST_CMD}" ]] || return 0 @@ -829,17 +1155,29 @@ main() { # show_help never returns fi + serve_parse_range + if [[ "${SERVE_SSH_CONFIG}" -eq 1 ]]; then + serve_print_ssh_config + exit 0 + fi + serve_check_args + bwrap_resolve_tool opencode2 "OpenCode 2 is installed by the ansible base role." use_opencode2_binary # --exec runs an arbitrary command, so its arguments are not opencode2's. - if [[ -z "${TEST_CMD}" ]]; then + # --serve builds its own arguments (serve_prepare), so the guard, which + # refuses `serve`, does not apply to them. + if [[ -z "${TEST_CMD}" && "${SERVE}" -eq 0 ]]; then guard_tool_args fi detect_kernel_capabilities resolve_common_paths resolve_opencode2_paths + if [[ "${SERVE}" -eq 1 ]]; then + serve_prepare + fi # Fail before any host-state change (ensure_host_dirs) if nothing is configured. require_opencode_config bwopencode2 "${OC2_CONFIG_SRC}" detect_shell @@ -889,6 +1227,9 @@ main() { build_binary_masks # Launch + if [[ "${SERVE}" -eq 1 ]]; then + serve_print_info + fi if [[ "${DRY_RUN}" -eq 1 ]]; then print_dry_run else diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 56e1aca..f2b869d 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -602,7 +602,9 @@ execution to that unsandboxed server. `bwopencode2` therefore: `opencode2` is started with no subcommand or a directory argument, or with `run`, `mini`, `models`, or `stats`; - refuses (exit status 1) `--server`, `--standalone=...`, `--no-standalone`, - and the `service`, `serve`, `api`, `pair`, and `reload` subcommands; + and the `service`, `serve`, `api`, `pair`, and `reload` subcommands (to run + a server, use [`--serve`](#--serve-the-web-ui-from-a-browser), which runs it + inside the sandbox); - passes the other subcommands (`auth`, `debug`, `mcp`, `plugin`, `session`, `upgrade`, `uninstall`, `acp`) through unchanged, since they do not accept `--standalone`. @@ -616,6 +618,74 @@ server password is no longer in the sandbox environment, see above). The config read-only, so commands that write global config (for example `plugin add`) fail; edit config on the host. +### `--serve`: the web UI from a browser + +`bwopencode2 --serve` is the one supported way to run `serve`. It runs +`opencode2 serve` **inside** the sandbox, so tools stay sandboxed (the refusal +above is about servers that run outside it), and the OpenCode web UI is then +reachable from a browser. + +```text +bwopencode2 --serve [--serve-port N] +bwopencode2 --serve-ssh-config +``` + +- **One server per invocation.** Each `--serve` is its own sandbox with its own + server; stop it with Ctrl-C in its terminal. +- **Bound to `127.0.0.1` only**, never another address. Reach it from another + machine through an ssh tunnel. +- **Free port from a fixed range**, `24700-24709` by default (ten sessions at + once). The range sits below the kernel's ephemeral range (32768-60999 on + Linux), so no outgoing connection takes a server port, and clear of 49374 + (the shared OpenCode service). `BWOPENCODE2_SERVE_PORTS=LO-HI` changes it + (at most 100 ports, 1024-65535, never including 49374; a warning is printed + if it overlaps the ephemeral range). `--serve-port N` asks for one port in + the range and fails if it is taken, so a saved tunnel always matches. +- **The ssh tunnel.** At start the launcher prints + `ssh -L P:127.0.0.1:P ` with the host from `hostname -f`. For all + ports at once, `bwopencode2 --serve-ssh-config` prints a block for + `~/.ssh/config` on the machine with the browser (alias `-oc`, one + `LocalForward` per port of the range, `ExitOnForwardFailure no` so a busy local + port does not stop the others). Keep the same port number at both ends: the + browser cookie is named by it (see below). Add `User` and `ProxyJump` to the + alias as needed. +- **Password.** The server needs one (Basic auth, user `opencode`; the web UI + asks for the password). The launcher uses a **random password for each + session** and prints it at start. To use a fixed one, export + `BWOPENCODE2_SERVE_PASSWORD` (at least 12 characters, for example from + `load-env`); the launcher prints nothing about it. Either way it reaches the + server only as `OPENCODE_PASSWORD` in the sandbox environment (the host's + own `OPENCODE_PASSWORD` is still dropped) and is never written to a file. + Trade-off: a sandboxed agent can read its own environment, so it can read its + password. With a random password that only reaches its own session. With a + fixed one it reaches every session that shares it, and any sandboxed agent on + the host could drive them all. Prefer the random default. +- **Port claims.** A file `~/.local/state/bwopencode2/serve/.session` (host + side, not mounted in the sandbox; the port, launcher pid, host, directory and + start time, no secret) stops two launchers picking the same port. A claim + whose process is gone is stale and is removed on the next `--serve`. The + launcher ends in `exec`, so it cannot retry or clean up afterwards; the + remaining race is another program taking the port in the moment before the + server binds it, and then the server fails to start with an error: run it + again. +- **Limits.** `--serve` takes no opencode2 arguments and cannot be combined with + `--exec`; `serve`, `--server`, `pair` and the other refused arguments stay + refused. `--dry-run` shows the command (password redacted) and claims + nothing. +- **Cookie.** The server names its login cookie `opencode_session_` from + the port in the address the browser used (checked with 2.2.6 using curl), so + sessions on different ports do not log each other out. The cookie holds an + expiry (30 days) and a hash made with the server password: with a fixed + password a cookie is accepted by any session that shares it, with a random + one it stops working when the session ends. + +Checked against OpenCode 2.2.6 with curl and an isolated data directory: the +server answers 401 without the password and 200 with it, serves the web UI page +without a password, and `POST /api/pair` with the password gives a one-time link +that sets the cookie. Not checked, because it needs a browser: the password +prompt in the web UI, and two sessions open in one browser. Whether the agent +can read `OPENCODE_PASSWORD` in tool output (`printenv`) is also unchecked. + ## `bwclaude` Launches the [Claude CLI](https://docs.claude.com/en/docs/claude-code/overview) inside a bubblewrap sandbox. From afa05683935d2ea06c81ef71658847cfc8b8095a Mon Sep 17 00:00:00 2001 From: "Stuart B. Wilkins" Date: Sun, 8 Nov 2026 18:23:00 -0500 Subject: [PATCH 02/37] feat(bwopencode2): print a one-time pairing link for --serve The web UI opens on a page that asks for a pairing link or the password, and opencode pair points at the shared service, which the wrapper refuses. Inside the sandbox a small wrapper now starts the server, waits for it, asks it for a pairing code and prints the link, so the browser can sign in without typing the password. The password reaches curl on stdin, never on a command line. Use password still works after the link expires. Assisted-by: opencode:claude-sonnet-5-5 --- bin/bwopencode2 | 63 +++++++++++++++++++++++++++++++++++++++++---- docs/bw-wrappers.md | 29 +++++++++++++++++---- 2 files changed, 82 insertions(+), 10 deletions(-) diff --git a/bin/bwopencode2 b/bin/bwopencode2 index 9906405..ad7a5be 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -288,8 +288,10 @@ Wrapper options (consumed before opencode2 sees the command line): TUI: `opencode2 serve` on 127.0.0.1 (never another address), on the first free port of the range 24700-24709 (override the range with BWOPENCODE2_SERVE_PORTS=LO-HI), tools still sandboxed. Open - it from a browser through an ssh tunnel; the command and the - password are printed at start. The password is random for each + it from a browser through an ssh tunnel; the command, a one-time + pairing link (valid 5 minutes) and the password are printed at + start; the web UI's "Use password" button also works. The + password is random for each session unless BWOPENCODE2_SERVE_PASSWORD is set (at least 12 characters; a fixed password is shared by every session that uses it). Takes no opencode2 arguments and cannot be combined @@ -595,11 +597,60 @@ serve_make_password() { SERVE_PASSWORD_RANDOM=1 } -# Choose port and password, and give the server its arguments. +# The command run INSIDE the sandbox for --serve: start the server, then print a +# one-time pairing link so the browser can sign in without typing the password +# (the web UI's "Use password" button still works). It runs as +# `bash -c SERVE_WRAPPER bwopencode2-serve BINARY PORT`. OPENCODE_PASSWORD is in +# its environment. The password reaches curl on stdin (-K -), never on a command +# line: a process in the sandbox is visible to `ps` on the host. printf is a +# builtin, so it does not show either. Needs only bash and curl. +read -r -d '' SERVE_WRAPPER << 'SERVE_WRAPPER_EOF' || true +set -u +bin=$1 port=$2 +base="http://127.0.0.1:${port}" + +"$bin" serve --hostname 127.0.0.1 --port "$port" & +srv=$! +trap 'kill "$srv" 2>/dev/null' INT TERM HUP + +# Wait up to about 30 s for the server to answer; without credentials /api/info +# gives 401, which is enough to know it is listening. +ready=0 +for _ in $(seq 60); do + kill -0 "$srv" 2> /dev/null || break + code=$(curl -s -o /dev/null -w '%{http_code}' "${base}/api/info") || code=000 + if [[ $code == 401 || $code == 200 ]]; then + ready=1 + break + fi + sleep 0.5 +done + +{ + if [[ $ready -eq 1 ]]; then + resp=$(printf 'user = "opencode:%s"\n' "$OPENCODE_PASSWORD" | + curl -s -K - -X POST "${base}/api/pair") || resp="" + if [[ $resp =~ \"code\":\"([A-Za-z0-9_-]+)\" ]]; then + printf '\nbwopencode2: pairing link (works once, expires in 5 minutes):\n' + printf ' %s/auth/connect/%s\n' "$base" "${BASH_REMATCH[1]}" + printf ' After it expires, click "Use password" in the web UI instead.\n\n' + else + printf 'bwopencode2: could not get a pairing link; click "Use password" in the web UI.\n' + fi + else + printf 'bwopencode2: the server did not start listening on %s.\n' "$base" + fi +} >&2 + +wait "$srv" +SERVE_WRAPPER_EOF + +# Choose port and password, and give the sandbox its command. serve_prepare() { serve_choose_port serve_make_password - TOOL_ARGS=(serve --hostname 127.0.0.1 --port "${SERVE_PORT}") + _TOOL_CMD=(/usr/bin/bash -c "${SERVE_WRAPPER}" bwopencode2-serve "${_TOOL_BIN}" "${SERVE_PORT}") + TOOL_ARGS=() } # Tell the user how to reach the server (stderr, before the exec). @@ -613,7 +664,9 @@ serve_print_info() { printf ' ssh -L %s:127.0.0.1:%s %s\n' "${_p}" "${_p}" "${_host}" printf ' or use the ssh_config alias (bwopencode2 --serve-ssh-config): ssh %s\n' "${_alias}" printf ' (on this host itself, skip this step)\n' - printf ' 2. Open http://127.0.0.1:%s/ and enter the server password when asked.\n' "${_p}" + printf ' 2. Open the pairing link printed below once the server is up (it works once and\n' + printf ' expires in 5 minutes), or open http://127.0.0.1:%s/, click "Use password"\n' "${_p}" + printf ' and enter the server password.\n' if [[ "${DRY_RUN}" -eq 1 ]]; then printf ' Password: (not shown on --dry-run)\n' elif [[ "${SERVE_PASSWORD_RANDOM}" -eq 1 ]]; then diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index f2b869d..5159890 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -649,8 +649,21 @@ bwopencode2 --serve-ssh-config port does not stop the others). Keep the same port number at both ends: the browser cookie is named by it (see below). Add `User` and `ProxyJump` to the alias as needed. -- **Password.** The server needs one (Basic auth, user `opencode`; the web UI - asks for the password). The launcher uses a **random password for each +- **Signing in.** The web UI opens on a "Connect to a server" page that asks + for a pairing link and offers **Use password**. Do not run `opencode pair` + (the wrapper refuses it, and outside the sandbox it points at the shared + service). Instead the sandbox runs a small wrapper, not `opencode2 serve` + directly: it starts the server, waits until it answers, asks it for a + one-time pairing code and prints + `http://127.0.0.1:/auth/connect/`. Open that link (through the + tunnel) to be signed in, with the 30-day cookie, without typing the password. + The link works once and expires after 5 minutes; after that, or if the + printed link failed, click **Use password** and enter the password. The + password reaches `curl` on its standard input (`-K -`), never on a command + line, because a process in the sandbox is visible to `ps` on the host. The + wrapper stays in the foreground and stops the server on Ctrl-C or when the + sandbox ends. +- **Password.** The server needs one (Basic auth, user `opencode`). The launcher uses a **random password for each session** and prints it at start. To use a fixed one, export `BWOPENCODE2_SERVE_PASSWORD` (at least 12 characters, for example from `load-env`); the launcher prints nothing about it. Either way it reaches the @@ -682,9 +695,15 @@ bwopencode2 --serve-ssh-config Checked against OpenCode 2.2.6 with curl and an isolated data directory: the server answers 401 without the password and 200 with it, serves the web UI page without a password, and `POST /api/pair` with the password gives a one-time link -that sets the cookie. Not checked, because it needs a browser: the password -prompt in the web UI, and two sessions open in one browser. Whether the agent -can read `OPENCODE_PASSWORD` in tool output (`printenv`) is also unchecked. +that sets the cookie. The pairing wrapper was run in a sandbox against a +throwaway server (isolated data directories): it printed a link that signed in +with `Set-Cookie: opencode_session_` and worked only once, the password +appeared in no process command line that was sampled, and TERM stopped the +server. The web UI's "Use password" page was seen in a screenshot. Not checked, +because it needs a browser or the real launcher: signing in with the link in a +browser, entering the password, a Ctrl-C from the terminal, and two sessions +open in one browser. Whether the agent can read `OPENCODE_PASSWORD` in tool +output (`printenv`) is also unchecked. ## `bwclaude` From 1f47dff2056dc7e010a7096da894babd198ecfb1 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 29 Oct 2026 22:55:43 -0400 Subject: [PATCH 03/37] feat(bwrap): open bwrap's descriptors in a relay before exec pasta, which will start bwrap in a private network namespace, closes every inherited descriptor above 2, so the --file and --ro-bind-data descriptors opened by the launcher would never reach bwrap. The launcher now only reserves descriptor numbers and records their source (a host file, or generated text carried in the environment); a fixed bash relay opens them at those numbers and execs bwrap. bwcodex no longer stages temporary copies of its config and daemon settings, and --dry-run shows real descriptor numbers instead of placeholders. Assisted-by: opencode:claude-opus-5-5 --- bin/bwclaude | 19 +--- bin/bwcodex | 46 +++------- bin/bwcopilot | 12 +-- lib/bwrap_sandbox_lib.sh | 190 +++++++++++++++++++++++++++++++++++---- 4 files changed, 192 insertions(+), 75 deletions(-) diff --git a/bin/bwclaude b/bin/bwclaude index 34549a8..64d9d38 100755 --- a/bin/bwclaude +++ b/bin/bwclaude @@ -354,14 +354,8 @@ build_claude_home_mount() { BWRAP_ARGS+=(--bind "${_settings_path}" "${_settings_path}") fi elif [[ -f "${_settings_path}" ]]; then - if [[ "${DRY_RUN}" -eq 1 ]]; then - # A real fd number could not be reproduced by copy-paste. - BWRAP_ARGS+=(--file "" "${_settings_path}") - else - local _settings_fd - exec {_settings_fd}< "${_settings_path}" - BWRAP_ARGS+=(--file "${_settings_fd}" "${_settings_path}") - fi + bwrap_fd_from_file "${_settings_path}" + BWRAP_ARGS+=(--file "${_BWRAP_FD}" "${_settings_path}") fi # Persistent read-only config file (if it exists) @@ -383,13 +377,8 @@ build_claude_home_mount() { elif [[ -f "${CLAUDE_CREDENTIALS_FILE}" ]]; then # Normal mode: copy credentials into tmpfs via --file so host auth is # available but changes don't persist. - if [[ "${DRY_RUN}" -eq 1 ]]; then - BWRAP_ARGS+=(--file "" "${CLAUDE_CREDENTIALS_FILE}") - else - local _fd - exec {_fd}< "${CLAUDE_CREDENTIALS_FILE}" - BWRAP_ARGS+=(--file "${_fd}" "${CLAUDE_CREDENTIALS_FILE}") - fi + bwrap_fd_from_file "${CLAUDE_CREDENTIALS_FILE}" + BWRAP_ARGS+=(--file "${_BWRAP_FD}" "${CLAUDE_CREDENTIALS_FILE}") fi # State file (~/.claude.json, or $CLAUDE_CONFIG_DIR/.claude.json): bind RW diff --git a/bin/bwcodex b/bin/bwcodex index 58bbecb..01009f1 100755 --- a/bin/bwcodex +++ b/bin/bwcodex @@ -25,8 +25,8 @@ # - history.jsonl: bind-mounted read-write (prompt history) # - config.toml: by default, opened as a file descriptor and passed to # bwrap via --file so Codex can persist project-trust grants for the -# session without touching the host file (the temp copy is unlinked -# immediately after opening; no cleanup needed on exit). +# session without touching the host file (nothing is staged on the +# host). # With --persist-config, the host config.toml is # bind-mounted RW so writes persist across sessions at the cost of # weaker isolation. Either the user file or the system file @@ -406,36 +406,20 @@ build_codex_home_mount() { fi # User config.toml mount strategy (see file header): - # - Default: open a temporary copy of config.toml as an fd and pass it - # to bwrap via --file. bwrap creates a plain (writable) file in the - # sandbox tmpfs from the fd content, so Codex can persist project-trust - # grants for the session without touching the host file. The temp - # file is unlinked immediately after the fd is opened, so no cleanup - # is needed on exit — the fd holds the only reference and closes when - # bwrap exec's into the tool. + # - Default: bwrap --file copies config.toml, from a descriptor the + # descriptor relay opens on the host file, into a plain (writable) + # file in the sandbox tmpfs, so Codex can persist project-trust + # grants for the session without touching the host file. Nothing is + # staged on the host. # - --persist-config: bind host config.toml RW so writes persist. # If the user file does not exist (managed host using only system # config), skip the user mount entirely. if [[ -f "${CODEX_CONFIG_FILE}" ]]; then if [[ "${PERSIST_CONFIG}" -eq 1 ]]; then BWRAP_ARGS+=(--bind "${CODEX_CONFIG_FILE}" "${CODEX_CONFIG_FILE}") - elif [[ "${DRY_RUN}" -eq 1 ]]; then - # --dry-run stages nothing on the host, and a real fd number could - # not be reproduced by copy-paste: print a placeholder instead. - BWRAP_ARGS+=(--file "" "${CODEX_CONFIG_FILE}") else - local _config_fd _staged_config - _staged_config="$(mktemp --tmpdir bwcodex-config.XXXXXX.toml)" - # Plain cp (not cp -p) keeps mktemp's 0600 mode; -p would apply - # the source's mode (typically 0644), leaving a world-readable - # copy in the shared temp directory until the unlink below. - cp "${CODEX_CONFIG_FILE}" "${_staged_config}" - # Open the copy as an fd then immediately unlink it. The kernel - # keeps the data alive via the open fd; the filename disappears - # from /tmp so there is nothing to clean up on exit or signal. - exec {_config_fd}< "${_staged_config}" - rm -f "${_staged_config}" - BWRAP_ARGS+=(--file "${_config_fd}" "${CODEX_CONFIG_FILE}") + bwrap_fd_from_file "${CODEX_CONFIG_FILE}" + BWRAP_ARGS+=(--file "${_BWRAP_FD}" "${CODEX_CONFIG_FILE}") fi fi @@ -447,16 +431,8 @@ build_codex_home_mount() { # mounted by build_codex_bin_mount seeds it on each launch. Disable the # daemon's updater before Codex starts so it does not fetch a newer copy. BWRAP_ARGS+=(--perms 0700 --dir "${CODEX_HOME_DIR}/app-server-daemon") - if [[ "${DRY_RUN}" -eq 1 ]]; then - BWRAP_ARGS+=(--perms 0600 --file "" "${CODEX_DAEMON_SETTINGS_FILE}") - else - local _settings_fd _staged_settings - _staged_settings="$(mktemp --tmpdir bwcodex-daemon-settings.XXXXXX.json)" - printf '{"updater":{"autoUpdateEnabled":false}}\n' > "${_staged_settings}" - exec {_settings_fd}< "${_staged_settings}" - rm -f "${_staged_settings}" - BWRAP_ARGS+=(--perms 0600 --file "${_settings_fd}" "${CODEX_DAEMON_SETTINGS_FILE}") - fi + bwrap_fd_from_data "Codex daemon settings" '{"updater":{"autoUpdateEnabled":false}}' + BWRAP_ARGS+=(--perms 0600 --file "${_BWRAP_FD}" "${CODEX_DAEMON_SETTINGS_FILE}") # auth.json: only persist if it already exists on the host. # Use --init-auth on first run to bootstrap persistent ChatGPT-login auth. diff --git a/bin/bwcopilot b/bin/bwcopilot index 915d519..696c2d9 100755 --- a/bin/bwcopilot +++ b/bin/bwcopilot @@ -351,16 +351,8 @@ build_copilot_home_mount() { elif [[ -f "${COPILOT_CONFIG_FILE}" ]]; then # Normal mode: copy config.json into tmpfs via --file # This makes host auth available but changes don't persist - if [[ "${DRY_RUN}" -eq 1 ]]; then - # A real fd number could not be reproduced by copy-paste: print a - # placeholder for the --file mount instead. - BWRAP_ARGS+=(--file "" "${COPILOT_CONFIG_FILE}") - else - local _fd - exec {_fd}< "${COPILOT_CONFIG_FILE}" - BWRAP_ARGS+=(--file "${_fd}" "${COPILOT_CONFIG_FILE}") - # Note: fd stays open until bwrap exec's, then it's inherited and used - fi + bwrap_fd_from_file "${COPILOT_CONFIG_FILE}" + BWRAP_ARGS+=(--file "${_BWRAP_FD}" "${COPILOT_CONFIG_FILE}") fi } diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index 0c1fad1..931fab7 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -83,6 +83,14 @@ BWRAP_ARGS=() declare -A _MOUNTED_PREFIXES=() _EXTRA_PATH_DIRS=() +# Descriptors that bwrap reads (--file, --ro-bind-data), opened by the fd +# relay just before bwrap starts (see "File descriptor relay"). +_BWRAP_FD="" +_BWRAP_FD_NEXT=10 +_BWRAP_FD_RELAY=() +declare -A _BWRAP_FD_DATA=() +declare -A _BWRAP_FD_LABEL=() + # Cached result of `npm prefix -g` (resolved at most once per run). # _NPM_PREFIX_RESOLVED flips to 1 after the first lookup; _NPM_PREFIX holds # the value (empty string if npm is absent or the lookup failed). @@ -3019,8 +3027,9 @@ build_env_vars() { # then makes its tool load the file in addition to the user's own # instruction files (never in place of them; no user file is touched). # -# The file travels as --ro-bind-data on a file descriptor, so nothing is -# written to the host. Variables set inside the sandbox: +# The file travels as --ro-bind-data on a file descriptor (see "File +# descriptor relay"), so nothing is written to the host. Variables set +# inside the sandbox: # BWRAP_SANDBOX launcher name (for example bwopencode2) # BWRAP_SANDBOX_CONTEXT path of the context file @@ -3386,7 +3395,7 @@ _bwrap_context_render() { # SANDBOX_CONTEXT_PATH (and _SANDBOX_CONTEXT_TEXT) when the file is mounted. build_sandbox_context() { local _tool="$1" - local _dir="${_HOME}/.sandbox" _file="${_HOME}/.sandbox/AGENTS.md" _fd + local _dir="${_HOME}/.sandbox" _file="${_HOME}/.sandbox/AGENTS.md" SANDBOX_CONTEXT_PATH="" [[ "${SANDBOX_CONTEXT_ENABLED}" -eq 1 ]] || return 0 @@ -3397,17 +3406,16 @@ build_sandbox_context() { _bwrap_context_render "${_tool}" "${_file}" - # The descriptor stays open in this shell until launch_sandbox execs - # bwrap, which reads it for --ro-bind-data and closes it. - exec {_fd}<<< "${_SANDBOX_CONTEXT_TEXT}" + bwrap_fd_from_data "sandbox context file, mounted at ${_file}" "${_SANDBOX_CONTEXT_TEXT}" BWRAP_ARGS+=( --dir "${_dir}" - --ro-bind-data "${_fd}" "${_file}" + --ro-bind-data "${_BWRAP_FD}" "${_file}" ) SANDBOX_ENV+=( "BWRAP_SANDBOX=${_tool}" "BWRAP_SANDBOX_CONTEXT=${_file}" ) + # shellcheck disable=SC2034 # read by the launchers' context loaders SANDBOX_CONTEXT_PATH="${_file}" } @@ -3436,6 +3444,124 @@ _bwrap_toml_string() { printf '"%s"' "${_s}" } +# ═══════════════════════════════════════════════════════════════════ +# File descriptor relay +# ═══════════════════════════════════════════════════════════════════ +# bwrap reads some files from descriptors: --file FD for writable copies of +# credentials and config, --ro-bind-data FD for generated files such as the +# sandbox context. The launcher does not open them itself, because bwrap is +# started by pasta (see "Network isolation"), and pasta closes every +# descriptor above 2 that it inherits. The launcher only reserves the +# numbers and records where each one's content comes from. A short fixed +# bash script, the relay, runs where bwrap used to (as pasta's command), opens +# each descriptor at its reserved number and execs bwrap. Sources: +# file PATH the relay opens PATH read-only. +# data generated text, carried in the environment variable +# _BWRAP_FD_DATA_; the relay turns it into a here-string +# (which appends one newline) and unsets the variable before +# exec'ing bwrap. Not for secrets: the variable stays in +# pasta's environment for the whole session. + +# bwrap_fd_from_file PATH +# Reserve a descriptor that the relay opens on PATH and set _BWRAP_FD to its +# number. Exits 1 when PATH cannot be read now. +bwrap_fd_from_file() { + local _path="$1" + if [[ ! -f "${_path}" || ! -r "${_path}" ]]; then + echo "Error: cannot read '${_path}'." >&2 + exit 1 + fi + _BWRAP_FD="${_BWRAP_FD_NEXT}" + _BWRAP_FD_NEXT=$((_BWRAP_FD_NEXT + 1)) + _BWRAP_FD_RELAY+=("${_BWRAP_FD}" file "${_path}") +} + +# bwrap_fd_from_data LABEL TEXT +# Reserve a descriptor that the relay fills with TEXT and a newline, and set +# _BWRAP_FD to its number. LABEL describes the content in --dry-run output. +# The kernel limits one environment string to 128 KiB, so TEXT must be +# shorter than that. +bwrap_fd_from_data() { + local _label="$1" _text="$2" + if [[ ${#_text} -gt 120000 ]]; then + echo "Error: the ${_label} is too large to pass to the sandbox (${#_text} characters)." >&2 + exit 1 + fi + _BWRAP_FD="${_BWRAP_FD_NEXT}" + _BWRAP_FD_NEXT=$((_BWRAP_FD_NEXT + 1)) + _BWRAP_FD_RELAY+=("${_BWRAP_FD}" data) + _BWRAP_FD_DATA["${_BWRAP_FD}"]="${_text}" + _BWRAP_FD_LABEL["${_BWRAP_FD}"]="${_label}" +} + +# The relay, run as +# bash -c "${_BWRAP_FD_RELAY_SHIM}" bwrap-fd-relay ENTRY... -- COMMAND ARGS... +# with each ENTRY either `N file PATH` or `N data`. N is checked to be a +# number before it reaches eval; PATH and the data are only expanded there, +# never pasted into the evaluated text. bash exports SHLVL to the command it +# execs, so it is unset; PWD is overridden by bwrap's --setenv. +# shellcheck disable=SC2016 # expanded by the relay, not here +_BWRAP_FD_RELAY_SHIM=' +while [[ $# -gt 0 && "$1" != -- ]]; do + if [[ ! "$1" =~ ^[0-9]+$ ]]; then + echo "$0: bad descriptor number: $1" >&2 + exit 126 + fi + case "$2" in + file) + eval "exec $1<\"\$3\"" || { echo "$0: cannot open $3" >&2; exit 126; } + shift 3 + ;; + data) + _v="_BWRAP_FD_DATA_$1" + eval "exec $1<<<\"\${!_v}\"" || { echo "$0: cannot fill descriptor $1" >&2; exit 126; } + unset "${_v}" + shift 2 + ;; + *) + echo "$0: bad relay entry: $2" >&2 + exit 126 + ;; + esac +done +shift +unset _v SHLVL +exec "$@" +' + +# resolve_bash_bin +# Print the bash that runs the relay: /usr/bin/bash or /bin/bash (labelled +# shell_exec_t, so SELinux moves it out of pasta's domain), else the bash +# running this script. +resolve_bash_bin() { + local _b + for _b in /usr/bin/bash /bin/bash "${BASH}"; do + if [[ -n "${_b}" && -x "${_b}" ]]; then + echo "${_b}" + return 0 + fi + done + echo "Error: cannot locate bash for the descriptor relay." >&2 + exit 1 +} + +# _bwrap_fd_data_numbers +# Set _BWRAP_FD_DATA_NUMS to the numbers of the data descriptors, in the +# order they were reserved. +_BWRAP_FD_DATA_NUMS=() +_bwrap_fd_data_numbers() { + local _r=0 + _BWRAP_FD_DATA_NUMS=() + while [[ ${_r} -lt ${#_BWRAP_FD_RELAY[@]} ]]; do + if [[ "${_BWRAP_FD_RELAY[$((_r + 1))]}" == file ]]; then + _r=$((_r + 3)) + else + _BWRAP_FD_DATA_NUMS+=("${_BWRAP_FD_RELAY[${_r}]}") + _r=$((_r + 2)) + fi + done +} + # ═══════════════════════════════════════════════════════════════════ # Shared launch functions # ═══════════════════════════════════════════════════════════════════ @@ -3559,14 +3685,19 @@ _is_secret_env_name() { print_dry_run() { verify_n2c_state_not_exposed - local _BWRAP_BIN _ENV_BIN + local _BWRAP_BIN _ENV_BIN _BASH_BIN _BWRAP_BIN="$(resolve_bwrap_bin)" _ENV_BIN="$(resolve_env_bin)" + _BASH_BIN="$(resolve_bash_bin)" # Mirror the exact invocation used by launch_sandbox: - # exec -i -- <_TOOL_CMD> + # exec -i + # -c bwrap-fd-relay -- + # -- <_TOOL_CMD> # Values of secret-looking variables (_is_secret_env_name) are printed as # REDACTED, on the env line and in --setenv; only the printed text changes. + # The relay script and its data are shown as placeholders, the data in + # full after the command. printf "exec %s -i \\\\\n" "${_ENV_BIN}" printf " HOME=%q \\\\\n" "${_HOME}" printf " USER=%q \\\\\n" "${_USER}" @@ -3581,6 +3712,23 @@ print_dry_run() { printf " %s=%q \\\\\n" "${_name}" "${_entry#*=}" fi done + local _n + _bwrap_fd_data_numbers + for _n in ${_BWRAP_FD_DATA_NUMS[@]+"${_BWRAP_FD_DATA_NUMS[@]}"}; do + printf " _BWRAP_FD_DATA_%s='<%s, shown below>' \\\\\n" "${_n}" "${_BWRAP_FD_LABEL["${_n}"]}" + done + printf " %s -c '<_BWRAP_FD_RELAY_SHIM from bwrap_sandbox_lib.sh>' bwrap-fd-relay \\\\\n" "${_BASH_BIN}" + local _r=0 + while [[ ${_r} -lt ${#_BWRAP_FD_RELAY[@]} ]]; do + if [[ "${_BWRAP_FD_RELAY[$((_r + 1))]}" == file ]]; then + printf " %s file %q \\\\\n" "${_BWRAP_FD_RELAY[${_r}]}" "${_BWRAP_FD_RELAY[$((_r + 2))]}" + _r=$((_r + 3)) + else + printf " %s data \\\\\n" "${_BWRAP_FD_RELAY[${_r}]}" + _r=$((_r + 2)) + fi + done + printf " -- \\\\\n" printf " %s \\\\\n" "${_BWRAP_BIN}" # Print bwrap arguments one flag-plus-values per line. Each flag's @@ -3614,11 +3762,10 @@ print_dry_run() { local _arg for _arg in ${TOOL_ARGS[@]+"${TOOL_ARGS[@]}"}; do printf " %q" "${_arg}"; done printf "\n" - if [[ -n "${SANDBOX_CONTEXT_PATH}" ]]; then - printf '\n# --ro-bind-data above reads a file descriptor that a real run fills with this\n' - printf '# sandbox context file, mounted at %s:\n#\n' "${SANDBOX_CONTEXT_PATH}" - printf '%s\n' "${_SANDBOX_CONTEXT_TEXT}" | sed 's/^/# /; s/ *$//' - fi + for _n in ${_BWRAP_FD_DATA_NUMS[@]+"${_BWRAP_FD_DATA_NUMS[@]}"}; do + printf '\n# Descriptor %s, filled by the relay with the %s:\n#\n' "${_n}" "${_BWRAP_FD_LABEL["${_n}"]}" + printf '%s\n' "${_BWRAP_FD_DATA["${_n}"]}" | sed 's/^/# /; s/ *$//' + done exit 0 } @@ -3634,11 +3781,21 @@ print_dry_run() { # `env` is resolved via resolve_env_bin (prefers /usr/bin/env) rather than # via PATH lookup at exec time — this prevents a user shell script named # "env" earlier on PATH from hijacking the launch. See resolve_env_bin. +# +# bwrap is started through the descriptor relay (see "File descriptor +# relay"), which opens the descriptors that BWRAP_ARGS refers to. launch_sandbox() { verify_n2c_state_not_exposed - local _BWRAP_BIN _ENV_BIN + local _BWRAP_BIN _ENV_BIN _BASH_BIN _n _BWRAP_BIN="$(resolve_bwrap_bin)" _ENV_BIN="$(resolve_env_bin)" + _BASH_BIN="$(resolve_bash_bin)" + + local -a _data_env=() + _bwrap_fd_data_numbers + for _n in ${_BWRAP_FD_DATA_NUMS[@]+"${_BWRAP_FD_DATA_NUMS[@]}"}; do + _data_env+=("_BWRAP_FD_DATA_${_n}=${_BWRAP_FD_DATA["${_n}"]}") + done exec "${_ENV_BIN}" -i \ HOME="${_HOME}" \ @@ -3646,5 +3803,8 @@ launch_sandbox() { LOGNAME="${_USER}" \ PATH="/usr/bin:/bin" \ ${SANDBOX_ENV[@]+"${SANDBOX_ENV[@]}"} \ + ${_data_env[@]+"${_data_env[@]}"} \ + "${_BASH_BIN}" -c "${_BWRAP_FD_RELAY_SHIM}" bwrap-fd-relay \ + ${_BWRAP_FD_RELAY[@]+"${_BWRAP_FD_RELAY[@]}"} -- \ "${_BWRAP_BIN}" "${BWRAP_ARGS[@]}" -- "${_TOOL_CMD[@]}" ${TOOL_ARGS[@]+"${TOOL_ARGS[@]}"} } From 46c1adf0aa7fef6d7626584bb95f57c8d2de0432 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 29 Oct 2026 23:02:31 -0400 Subject: [PATCH 04/37] feat(bwrap): run every sandbox in a private network namespace pasta (package passt) now starts each sandbox in its own network namespace. Outbound connections still leave from the host's address, but the host's loopback services are unreachable and the sandbox's listeners are invisible to the host. There is no fallback to the host network: without a working pasta the launchers exit 3. Crossing the boundary is a per-port, per-launch opt-in: - --publish PORT[:SANDBOXPORT] binds a per-user, per-session host address 127.H1.H2.S (--publish-addr overrides it, e.g. for OAuth); - --host-port PORT[:SANDBOXPORT] makes the host's 127.0.0.1:PORT reachable as host.internal, with a deny list (CUPS, Docker, X11, Tailscale, the OpenCode 2 service); - --net-log FILE records every flow. pasta maps the caller to uid 0 in its user namespace, so bwrap now runs the agent as the real uid in a nested one (--unshare-user --uid --gid); without it the agent would be root with a full capability set there. bwrap also restores the real hostname that pasta replaces. /etc/resolv.conf and /etc/hosts are generated, the context file gains a Network section, and BWRAP_NET_* describe the forwards. This breaks --serve, will fix later. Assisted-by: opencode:claude-opus-5-5 --- .codespell.ignore | 1 + bin/bwclaude | 51 +++ bin/bwcodex | 51 +++ bin/bwcopilot | 51 +++ bin/bwopencode | 56 +++- bin/bwopencode2 | 65 +++- lib/bwrap_sandbox_lib.sh | 666 ++++++++++++++++++++++++++++++++++++++- 7 files changed, 927 insertions(+), 14 deletions(-) diff --git a/.codespell.ignore b/.codespell.ignore index e69de29..c25ef20 100644 --- a/.codespell.ignore +++ b/.codespell.ignore @@ -0,0 +1 @@ +passt diff --git a/bin/bwclaude b/bin/bwclaude index 64d9d38..2a38265 100755 --- a/bin/bwclaude +++ b/bin/bwclaude @@ -85,6 +85,22 @@ # so `--rw-path DIR --ro-path DIR/sub` protects DIR/sub. # --no-sandbox-context # Do not generate the sandbox context file (see below). +# --publish PORT[:SANDBOXPORT] +# Make a server in the sandbox reachable on the host at a +# per-user, per-session loopback address (127.H1.H2.S:PORT). +# May be repeated. +# --publish-addr ADDR +# Use ADDR (in 127.0.0.0/8) for --publish instead. +# --host-port PORT[:SANDBOXPORT] +# Make the host's 127.0.0.1:PORT reachable in the sandbox at +# host.internal:SANDBOXPORT. May be repeated. +# --net-log FILE Log every network flow to FILE on the host. +# +# Network: the sandbox has its own network namespace from pasta (package +# passt; required, there is no fallback to the host network). Outbound +# connections work; services on the host's loopback are unreachable and the +# sandbox's listeners are invisible to the host, except for the ports opened +# with --publish and --host-port. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are @@ -164,6 +180,38 @@ parse_wrapper_args() { SANDBOX_CONTEXT_ENABLED=0 shift ;; + --publish) + if [[ $# -lt 2 ]]; then + echo "Error: --publish requires PORT[:SANDBOXPORT]." >&2 + exit 1 + fi + net_add_publish "$2" + shift 2 + ;; + --publish-addr) + if [[ $# -lt 2 ]]; then + echo "Error: --publish-addr requires an address in 127.0.0.0/8." >&2 + exit 1 + fi + net_set_publish_addr "$2" + shift 2 + ;; + --host-port) + if [[ $# -lt 2 ]]; then + echo "Error: --host-port requires PORT[:SANDBOXPORT]." >&2 + exit 1 + fi + net_add_host_port "$2" + shift 2 + ;; + --net-log) + if [[ $# -lt 2 ]]; then + echo "Error: --net-log requires a file name." >&2 + exit 1 + fi + net_set_log_file "$2" + shift 2 + ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 @@ -263,6 +311,7 @@ Authentication: bwclaude --init-auth HELP_EOF + net_print_help # Delegate to claude --help inside the sandbox so the user sees the full # claude option set in the same invocation. bwrap_show_tool_help claude @@ -582,6 +631,7 @@ main() { bwrap_resolve_tool claude detect_kernel_capabilities + detect_net_capabilities resolve_common_paths resolve_claude_paths detect_shell @@ -597,6 +647,7 @@ main() { build_proc_dev_tmp build_sys_mounts build_etc_mounts + build_net_isolation build_managed_claude_paths build_home_tmpfs build_workdir_mount diff --git a/bin/bwcodex b/bin/bwcodex index 01009f1..79770a4 100755 --- a/bin/bwcodex +++ b/bin/bwcodex @@ -102,6 +102,22 @@ # so `--rw-path DIR --ro-path DIR/sub` protects DIR/sub. # --no-sandbox-context # Do not generate the sandbox context file (see below). +# --publish PORT[:SANDBOXPORT] +# Make a server in the sandbox reachable on the host at a +# per-user, per-session loopback address (127.H1.H2.S:PORT). +# May be repeated. +# --publish-addr ADDR +# Use ADDR (in 127.0.0.0/8) for --publish instead. +# --host-port PORT[:SANDBOXPORT] +# Make the host's 127.0.0.1:PORT reachable in the sandbox at +# host.internal:SANDBOXPORT. May be repeated. +# --net-log FILE Log every network flow to FILE on the host. +# +# Network: the sandbox has its own network namespace from pasta (package +# passt; required, there is no fallback to the host network). Outbound +# connections work; services on the host's loopback are unreachable and the +# sandbox's listeners are invisible to the host, except for the ports opened +# with --publish and --host-port. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are @@ -186,6 +202,38 @@ parse_wrapper_args() { SANDBOX_CONTEXT_ENABLED=0 shift ;; + --publish) + if [[ $# -lt 2 ]]; then + echo "Error: --publish requires PORT[:SANDBOXPORT]." >&2 + exit 1 + fi + net_add_publish "$2" + shift 2 + ;; + --publish-addr) + if [[ $# -lt 2 ]]; then + echo "Error: --publish-addr requires an address in 127.0.0.0/8." >&2 + exit 1 + fi + net_set_publish_addr "$2" + shift 2 + ;; + --host-port) + if [[ $# -lt 2 ]]; then + echo "Error: --host-port requires PORT[:SANDBOXPORT]." >&2 + exit 1 + fi + net_add_host_port "$2" + shift 2 + ;; + --net-log) + if [[ $# -lt 2 ]]; then + echo "Error: --net-log requires a file name." >&2 + exit 1 + fi + net_set_log_file "$2" + shift 2 + ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 @@ -293,6 +341,7 @@ Authentication: `codex login` to populate ~/.codex/auth.json. HELP_EOF + net_print_help # Delegate to codex --help inside the sandbox so the user sees the full # codex option set in the same invocation. bwrap_show_tool_help codex @@ -596,6 +645,7 @@ main() { bwrap_resolve_tool codex detect_kernel_capabilities + detect_net_capabilities resolve_common_paths resolve_codex_paths detect_shell @@ -612,6 +662,7 @@ main() { build_proc_dev_tmp build_sys_mounts build_etc_mounts + build_net_isolation build_managed_skills build_home_tmpfs build_workdir_mount diff --git a/bin/bwcopilot b/bin/bwcopilot index 696c2d9..221b021 100755 --- a/bin/bwcopilot +++ b/bin/bwcopilot @@ -70,6 +70,22 @@ # so `--rw-path DIR --ro-path DIR/sub` protects DIR/sub. # --no-sandbox-context # Do not generate the sandbox context file (see below). +# --publish PORT[:SANDBOXPORT] +# Make a server in the sandbox reachable on the host at a +# per-user, per-session loopback address (127.H1.H2.S:PORT). +# May be repeated. +# --publish-addr ADDR +# Use ADDR (in 127.0.0.0/8) for --publish instead. +# --host-port PORT[:SANDBOXPORT] +# Make the host's 127.0.0.1:PORT reachable in the sandbox at +# host.internal:SANDBOXPORT. May be repeated. +# --net-log FILE Log every network flow to FILE on the host. +# +# Network: the sandbox has its own network namespace from pasta (package +# passt; required, there is no fallback to the host network). Outbound +# connections work; services on the host's loopback are unreachable and the +# sandbox's listeners are invisible to the host, except for the ports opened +# with --publish and --host-port. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are @@ -144,6 +160,38 @@ parse_wrapper_args() { SANDBOX_CONTEXT_ENABLED=0 shift ;; + --publish) + if [[ $# -lt 2 ]]; then + echo "Error: --publish requires PORT[:SANDBOXPORT]." >&2 + exit 1 + fi + net_add_publish "$2" + shift 2 + ;; + --publish-addr) + if [[ $# -lt 2 ]]; then + echo "Error: --publish-addr requires an address in 127.0.0.0/8." >&2 + exit 1 + fi + net_set_publish_addr "$2" + shift 2 + ;; + --host-port) + if [[ $# -lt 2 ]]; then + echo "Error: --host-port requires PORT[:SANDBOXPORT]." >&2 + exit 1 + fi + net_add_host_port "$2" + shift 2 + ;; + --net-log) + if [[ $# -lt 2 ]]; then + echo "Error: --net-log requires a file name." >&2 + exit 1 + fi + net_set_log_file "$2" + shift 2 + ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 @@ -247,6 +295,7 @@ Known limitation: Use the COPILOT_HOME env var instead. HELP_EOF + net_print_help # Delegate to copilot --help inside the sandbox so the user sees the full # copilot option set in the same invocation. bwrap_show_tool_help copilot @@ -470,6 +519,7 @@ main() { bwrap_resolve_tool copilot "Install the standalone GitHub Copilot CLI from https://github.com/github/copilot-cli" detect_kernel_capabilities + detect_net_capabilities resolve_common_paths resolve_copilot_paths detect_shell @@ -485,6 +535,7 @@ main() { build_proc_dev_tmp build_sys_mounts build_etc_mounts + build_net_isolation build_home_tmpfs build_workdir_mount build_copilot_home_mount diff --git a/bin/bwopencode b/bin/bwopencode index 05c0123..e8e990a 100755 --- a/bin/bwopencode +++ b/bin/bwopencode @@ -82,6 +82,22 @@ # so `--rw-path DIR --ro-path DIR/sub` protects DIR/sub. # --no-sandbox-context # Do not generate the sandbox context file (see below). +# --publish PORT[:SANDBOXPORT] +# Make a server in the sandbox reachable on the host at a +# per-user, per-session loopback address (127.H1.H2.S:PORT). +# May be repeated. +# --publish-addr ADDR +# Use ADDR (in 127.0.0.0/8) for --publish instead. +# --host-port PORT[:SANDBOXPORT] +# Make the host's 127.0.0.1:PORT reachable in the sandbox at +# host.internal:SANDBOXPORT. May be repeated. +# --net-log FILE Log every network flow to FILE on the host. +# +# Network: the sandbox has its own network namespace from pasta (package +# passt; required, there is no fallback to the host network). Outbound +# connections work; services on the host's loopback are unreachable and the +# sandbox's listeners are invisible to the host, except for the ports opened +# with --publish and --host-port. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are @@ -156,6 +172,38 @@ parse_wrapper_args() { SANDBOX_CONTEXT_ENABLED=0 shift ;; + --publish) + if [[ $# -lt 2 ]]; then + echo "Error: --publish requires PORT[:SANDBOXPORT]." >&2 + exit 1 + fi + net_add_publish "$2" + shift 2 + ;; + --publish-addr) + if [[ $# -lt 2 ]]; then + echo "Error: --publish-addr requires an address in 127.0.0.0/8." >&2 + exit 1 + fi + net_set_publish_addr "$2" + shift 2 + ;; + --host-port) + if [[ $# -lt 2 ]]; then + echo "Error: --host-port requires PORT[:SANDBOXPORT]." >&2 + exit 1 + fi + net_add_host_port "$2" + shift 2 + ;; + --net-log) + if [[ $# -lt 2 ]]; then + echo "Error: --net-log requires a file name." >&2 + exit 1 + fi + net_set_log_file "$2" + shift 2 + ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 @@ -242,6 +290,7 @@ Wrapper options (consumed before opencode sees the command line): All other arguments are passed through to opencode unchanged. HELP_EOF + net_print_help # Delegate to opencode --help inside the sandbox so the user sees the full # opencode option set in the same invocation. We re-exec this script with # --exec so the sandbox is fully constructed; opencode --help is passed @@ -472,8 +521,9 @@ build_opencode_cache_mount() { # Pass through an ALLOWLIST of harmless OPENCODE_* behaviour toggles, and # AIFAPIM_* env vars for the NSLS-II Hermes gateway. Other host OPENCODE_* # variables are dropped: some are secrets (OPENCODE_SERVER_PASSWORD would -# let the agent, which shares the host network, drive an unsandboxed -# opencode server) and others redirect config/data/DB/model/server locations +# let the agent drive an unsandboxed opencode server; the private network +# namespace keeps the host's loopback out of reach, but the password stays +# out too, in case a server is reachable another way) and others redirect config/data/DB/model/server locations # or inject config (OPENCODE_CONFIG*, OPENCODE_PERMISSION) and so would # bypass the managed config. In --dry-run the NAMES (never values) of # dropped variables are printed to stderr. @@ -608,6 +658,7 @@ main() { bwrap_resolve_tool opencode detect_kernel_capabilities + detect_net_capabilities resolve_common_paths resolve_opencode_paths # Fail before any host-state change (ensure_host_dirs) if nothing is configured. @@ -626,6 +677,7 @@ main() { build_proc_dev_tmp build_sys_mounts build_etc_mounts + build_net_isolation build_managed_config build_managed_skills build_home_tmpfs diff --git a/bin/bwopencode2 b/bin/bwopencode2 index ad7a5be..1b4edac 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -123,6 +123,22 @@ # --github-tokens Forward any GH_TOKEN_* environment variables into the sandbox. # --no-sandbox-context # Do not generate the sandbox context file (see below). +# --publish PORT[:SANDBOXPORT] +# Make a server in the sandbox reachable on the host at a +# per-user, per-session loopback address (127.H1.H2.S:PORT). +# May be repeated. +# --publish-addr ADDR +# Use ADDR (in 127.0.0.0/8) for --publish instead. +# --host-port PORT[:SANDBOXPORT] +# Make the host's 127.0.0.1:PORT reachable in the sandbox at +# host.internal:SANDBOXPORT. May be repeated. +# --net-log FILE Log every network flow to FILE on the host. +# +# Network: the sandbox has its own network namespace from pasta (package +# passt; required, there is no fallback to the host network). Outbound +# connections work; services on the host's loopback are unreachable and the +# sandbox's listeners are invisible to the host, except for the ports opened +# with --publish and --host-port. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are @@ -237,6 +253,38 @@ parse_wrapper_args() { SERVE_SSH_CONFIG=1 shift ;; + --publish) + if [[ $# -lt 2 ]]; then + echo "Error: --publish requires PORT[:SANDBOXPORT]." >&2 + exit 1 + fi + net_add_publish "$2" + shift 2 + ;; + --publish-addr) + if [[ $# -lt 2 ]]; then + echo "Error: --publish-addr requires an address in 127.0.0.0/8." >&2 + exit 1 + fi + net_set_publish_addr "$2" + shift 2 + ;; + --host-port) + if [[ $# -lt 2 ]]; then + echo "Error: --host-port requires PORT[:SANDBOXPORT]." >&2 + exit 1 + fi + net_add_host_port "$2" + shift 2 + ;; + --net-log) + if [[ $# -lt 2 ]]; then + echo "Error: --net-log requires a file name." >&2 + exit 1 + fi + net_set_log_file "$2" + shift 2 + ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 @@ -369,6 +417,7 @@ git credentials file (only a few files of the XDG git directory are mounted). All other arguments are passed through to opencode2 unchanged. HELP_EOF + net_print_help # Delegate to opencode2 --help inside the sandbox so the user sees the full # opencode2 option set in the same invocation. We re-exec this script with # --exec so the sandbox is fully constructed; opencode2 --help is passed @@ -680,8 +729,11 @@ serve_print_info() { # ── Sandbox-escape guard (arguments) ───────────────────────────── # The OpenCode 2 background service listens on a fixed, shared port and runs -# tools outside any sandbox. Because the sandbox shares the host network, a -# CLI started without --standalone would delegate tool execution to it. +# tools outside any sandbox. A CLI started without --standalone would +# delegate tool execution to it. The sandbox's private network namespace +# already keeps the host's loopback (and so the service) out of reach, and +# --host-port refuses the port; this guard is kept as a second line of +# defence, and so that the CLI does not hang looking for the service. # # guard_tool_args scans TOOL_ARGS and either refuses the invocation or # injects --standalone: @@ -1065,9 +1117,10 @@ build_opencode2_state_dir() { # Pass through ONLY the allowlisted OPENCODE_* env vars (harmless behaviour # switches). Every other OPENCODE_* variable is dropped, because the # OpenCode 2 binary reads several sensitive ones, e.g.: -# OPENCODE_PASSWORD / OPENCODE_SERVER_PASSWORD password of a v2 server; the -# sandbox shares the host network, so holding it would let the agent -# connect to an UNSANDBOXED server (port 49374) and run tools outside +# OPENCODE_PASSWORD / OPENCODE_SERVER_PASSWORD password of a v2 server; +# with a route to it (the private network namespace normally gives +# none) it would let the agent drive an UNSANDBOXED server (port +# 49374) and run tools outside # OPENCODE_SSH_ASKPASS_TOKEN / _PORT, OPENCODE_EDITOR_SSE_PORT, # OPENCODE_PTY_HANDOFF host-side helper channels # OPENCODE_DB, OPENCODE_TEST_HOME, OPENCODE_CONFIG*, OPENCODE_MODELS_PATH/URL @@ -1226,6 +1279,7 @@ main() { fi detect_kernel_capabilities + detect_net_capabilities resolve_common_paths resolve_opencode2_paths if [[ "${SERVE}" -eq 1 ]]; then @@ -1248,6 +1302,7 @@ main() { build_proc_dev_tmp build_sys_mounts build_etc_mounts + build_net_isolation build_managed_config build_managed_skills build_managed_extras diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index 931fab7..bf5da95 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -12,7 +12,8 @@ # working directory, git config, pixi, ccache, npm, user bin, NSLS-II, # dynamic tool mounts, user-supplied extra paths (--ro-path / --rw-path) # - Environment variable framework (clean env + passthrough) -# - Launch logic (dry-run printing and bwrap exec) +# - Private network namespace via pasta, with opt-in port forwards +# - Launch logic (dry-run printing and pasta + fd relay + bwrap exec) # # Tool-specific wrapper scripts source this file and provide: # - Argument parsing (parse_wrapper_args) — must handle --ro-path/--rw-path @@ -21,7 +22,11 @@ # - Help text (show_help) # - Tool-specific path resolution, host directory creation, mounts, and env vars # - main() orchestrating the build sequence — must call build_extra_path_mounts -# after build_dynamic_tool_mounts +# after build_dynamic_tool_mounts, detect_net_capabilities after +# detect_kernel_capabilities, and build_net_isolation after build_etc_mounts +# - --publish / --publish-addr / --host-port / --net-log in parse_wrapper_args, +# through net_add_publish, net_set_publish_addr, net_add_host_port and +# net_set_log_file; net_print_help in show_help # # Usage (in tool wrapper scripts): # SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" @@ -29,6 +34,8 @@ # # Requires: bwrap >= 0.4.0 (RHEL 8) # Binary masking is verified on RHEL 8.10 (bubblewrap-0.4.0-3.el8_10) and EL9 (0.6.3). +# Requires: pasta (package passt), and /dev/net/tun and unprivileged user +# namespaces on the host. There is no fallback to the host network. # Guard against double-sourcing. [[ -n "${_BWRAP_SANDBOX_LIB_SOURCED:-}" ]] && return 0 @@ -58,6 +65,17 @@ EXTRA_RO_PATHS=() # shellcheck disable=SC2034 # used by tool wrappers that source this library EXTRA_RW_PATHS=() +# Private network namespace (see "Network isolation"). Filled by the +# launchers' parse_wrapper_args through net_add_publish, net_add_host_port, +# net_set_publish_addr and net_set_log_file; turned into pasta arguments by +# build_net_isolation. NET_PUBLISH and NET_HOST_PORTS hold validated specs +# "PORTS[:SANDBOXPORTS]" (PORTS is N or LOW-HIGH). +NET_PUBLISH=() +NET_PUBLISH_ADDR="" +NET_HOST_PORTS=() +NET_LOG_FILE="" +PASTA_ARGS=() + # N2C access tokens requested with --n2c-token (names, or "all"). # Populated by add_n2c_token; mounted by build_n2c_token_mounts. N2C_TOKENS=() @@ -2192,10 +2210,9 @@ build_sys_mounts() { # ── Selective /etc (read-only) ─────────────────────────────────── # Only expose what is needed for DNS, TLS, user identity, and NSS. +# /etc/resolv.conf and /etc/hosts are generated by build_net_isolation. build_etc_mounts() { BWRAP_ARGS+=( - --ro-bind-try /etc/resolv.conf /etc/resolv.conf - --ro-bind-try /etc/hosts /etc/hosts --ro-bind-try /etc/nsswitch.conf /etc/nsswitch.conf --ro-bind /etc/passwd /etc/passwd --ro-bind /etc/group /etc/group @@ -2212,6 +2229,568 @@ build_etc_mounts() { ) } +# ═══════════════════════════════════════════════════════════════════ +# Network isolation +# ═══════════════════════════════════════════════════════════════════ +# The sandbox gets its own network namespace from pasta (package passt): +# +# env -i ... pasta [PASTA_ARGS] -- bash -- bwrap [BWRAP_ARGS] -- tool +# +# pasta creates a user and a network namespace (plus mount, UTS and IPC, and +# a PID namespace unless --no-pidns), gives it one tap interface with the +# host's address and routes, and carries its traffic through ordinary host +# sockets. Outbound connections therefore work and leave from the host's +# address, while the host's loopback services (127.0.0.1: opencode's server, +# CUPS, databases, other users' sessions) are out of reach, and the sandbox +# has its own port space. Any crossing of that boundary is a per-port, +# per-launch opt-in: +# --publish PORT[:SANDBOXPORT] sandbox server -> host 127.H1.H2.S:PORT +# --host-port PORT[:SANDBOXPORT] host 127.0.0.1:PORT -> sandbox +# host.internal:SANDBOXPORT (127.0.42.1) +# There is no fallback to the host network: without a working pasta the +# launcher stops (exit 3). +# +# Points that are easy to get wrong: +# - --no-map-gw: pasta's default maps the gateway address to the host's +# loopback, which would re-expose every loopback service. +# - -T none / -U none: pasta's default ("auto") forwards ports bound on the +# host loopback INTO the namespace, a second path to the same services. +# - pasta maps the caller to uid 0 in its user namespace. bwrap therefore +# gets --unshare-user --uid/--gid with the real ids; without them the +# agent would run as root with a full capability set there. +# - pasta renames the host (pasta-) in its UTS namespace; bwrap's +# --unshare-uts --hostname restores the real name. +# - pasta closes inherited descriptors; see "File descriptor relay". +# - The host resolver is usually on loopback (127.0.0.53), so DNS goes to +# _NET_DNS_FORWARD_ADDR, which pasta forwards to the host's first +# nameserver; resolv.conf is generated to match. + +_PASTA_BIN="" +_PASTA_HAS_NO_PIDNS=0 +_NET_CONFIGURED=0 +_NET_PUBLISH_ADDR_EFFECTIVE="" +_NET_HOST_INTERNAL_ADDR="127.0.42.1" +_NET_HOST_INTERNAL_NAME="host.internal" +_NET_DNS_FORWARD_ADDR="169.254.1.1" +# Host loopback ports that --host-port refuses: services that trust loopback +# clients completely. CUPS, Docker (plain and TLS), X11, the Tailscale local +# API, and the OpenCode 2 background service (runs tools outside any sandbox). +_NET_HOST_PORT_DENY=(631 2375 2376 6000-6063 41112 49374) +# Human-readable forward descriptions for the launch report and context file. +_NET_PUBLISH_DESC=() +_NET_HOST_DESC=() +# Low and high ends of the listening side of every accepted spec, per +# direction, for the overlap check. +_NET_PUB_LO=() +_NET_PUB_HI=() +_NET_HP_LO=() +_NET_HP_HI=() + +# net_print_help +# Print the network options for a launcher's --help. +net_print_help() { + cat << 'NET_HELP_EOF' +Network: the sandbox has its own network namespace (pasta, package passt; +required, there is no fallback). Outbound connections work as on the host; +services on the host's loopback (127.0.0.1) are NOT reachable, and nothing the +agent listens on is reachable from the host, unless opened below. Ports must +be in 1024-65535; N or LOW-HIGH ranges are accepted. + --publish PORT[:SANDBOXPORT] + Make a server inside the sandbox (port SANDBOXPORT, default + PORT) reachable on the host at http://127.H1.H2.S:PORT/, an + address derived from your uid and a free session index S + (printed at launch). The server must bind 0.0.0.0, not + 127.0.0.1. May be repeated. Any local user can connect to + it: this is not access control. + --publish-addr ADDR + Use ADDR (in 127.0.0.0/8) instead of the derived address, for + example 127.0.0.1 for OAuth redirect URIs that require it. + --host-port PORT[:SANDBOXPORT] + Make the host's 127.0.0.1:PORT reachable in the sandbox at + host.internal:SANDBOXPORT (default PORT). The host service + sees a loopback client and may trust it. May be repeated. + Refused: 631 (CUPS), 2375-2376 (Docker), 6000-6063 (X11), + 41112 (Tailscale), 49374 (OpenCode 2 service). + --net-log FILE Log every network flow (pasta --debug) to FILE on the host. + +NET_HELP_EOF +} + +# _net_baseline_args +# Set PASTA_ARGS to the flags used with no forwards. +_net_baseline_args() { + PASTA_ARGS=( + --config-net + --no-map-gw + --dns-forward "${_NET_DNS_FORWARD_ADDR}" + --search none + -t none + -u none + -T none + -U none + --quiet + ) + if [[ "${_PASTA_HAS_NO_PIDNS}" -eq 1 ]]; then + PASTA_ARGS+=(--no-pidns) + fi +} + +# detect_net_capabilities +# Find pasta and check that it can create a namespace here (one probe run, +# about 50 ms). Exit 3 naming the missing piece otherwise. Under --dry-run +# nothing is run and a missing pasta is allowed, so the command line can +# still be inspected on a host without passt. +detect_net_capabilities() { + local _reason="" _out="" + _PASTA_BIN="$(command -v pasta 2> /dev/null || true)" + if [[ -z "${_PASTA_BIN}" || ! -x "${_PASTA_BIN}" ]]; then + if [[ "${DRY_RUN}" -eq 1 ]]; then + echo "${0##*/}: dry-run: pasta not found on PATH (install package: passt); a real run would stop here." >&2 + _PASTA_BIN="pasta" + _net_baseline_args + return 0 + fi + _reason="pasta not found on PATH (install package: passt)" + else + # --no-pidns (2026-09) is not in RHEL 9 / EPEL builds; without it + # pasta adds a PID namespace, inside which bwrap nests its own. + if "${_PASTA_BIN}" --help 2>&1 | grep -q -e '--no-pidns'; then + _PASTA_HAS_NO_PIDNS=1 + fi + _net_baseline_args + if [[ "${DRY_RUN}" -eq 1 ]]; then + return 0 + fi + if [[ ! -c /dev/net/tun ]]; then + _reason="/dev/net/tun is missing on the host" + elif ! _out="$("${_PASTA_BIN}" "${PASTA_ARGS[@]}" -- "$(command -v true)" 2>&1)"; then + _out="${_out%%$'\n'*}" + _reason="a test run of pasta failed${_out:+: ${_out}} (are unprivileged user namespaces disabled?)" + fi + fi + if [[ -n "${_reason}" ]]; then + printf 'Error: %s requires private networking via pasta: %s\n' "${0##*/}" "${_reason}" >&2 + exit 3 + fi +} + +# _net_parse_range OPTION TEXT +# Parse N or LOW-HIGH (ports 1024-65535) into _NET_LO and _NET_HI. Exit 1 +# with a message naming OPTION otherwise. +_NET_LO=0 +_NET_HI=0 +_net_parse_range() { + local _opt="$1" _t="$2" + if [[ ! "${_t}" =~ ^([0-9]{1,5})(-([0-9]{1,5}))?$ ]]; then + echo "Error: ${_opt}: '${_t}' is not a port or a LOW-HIGH port range." >&2 + exit 1 + fi + _NET_LO=$((10#${BASH_REMATCH[1]})) + _NET_HI="${_NET_LO}" + [[ -z "${BASH_REMATCH[3]}" ]] || _NET_HI=$((10#${BASH_REMATCH[3]})) + if [[ ${_NET_LO} -lt 1024 ]]; then + echo "Error: ${_opt}: port ${_t} is below 1024 (those ports need privileges); use 1024-65535." >&2 + exit 1 + fi + if [[ ${_NET_HI} -gt 65535 ]]; then + echo "Error: ${_opt}: port ${_t} is above 65535." >&2 + exit 1 + fi + if [[ ${_NET_HI} -lt ${_NET_LO} ]]; then + echo "Error: ${_opt}: port range ${_t} is empty (LOW must not exceed HIGH)." >&2 + exit 1 + fi +} + +# _net_check_port_spec OPTION SPEC +# Validate PORTS[:SANDBOXPORTS]. Set _NET_SPEC to the normalised spec, +# _NET_A_LO/_NET_A_HI to the first part and _NET_B_LO/_NET_B_HI to the +# second (the same as the first when it is omitted). Ranges on both sides +# must have the same length. +_NET_SPEC="" +_net_check_port_spec() { + local _opt="$1" _spec="$2" _a _b _na _nb + if [[ "${_spec}" == *:*:* ]]; then + echo "Error: ${_opt}: '${_spec}' has more than one ':' (expected PORT[:SANDBOXPORT])." >&2 + exit 1 + fi + _a="${_spec%%:*}" + _b="${_a}" + [[ "${_spec}" != *:* ]] || _b="${_spec#*:}" + _net_parse_range "${_opt}" "${_a}" + _NET_A_LO=${_NET_LO} + _NET_A_HI=${_NET_HI} + _net_parse_range "${_opt}" "${_b}" + _NET_B_LO=${_NET_LO} + _NET_B_HI=${_NET_HI} + if [[ $((_NET_A_HI - _NET_A_LO)) -ne $((_NET_B_HI - _NET_B_LO)) ]]; then + echo "Error: ${_opt}: the ranges in '${_spec}' have different lengths." >&2 + exit 1 + fi + _na="${_NET_A_LO}" + [[ ${_NET_A_HI} -eq ${_NET_A_LO} ]] || _na+="-${_NET_A_HI}" + _nb="${_NET_B_LO}" + [[ ${_NET_B_HI} -eq ${_NET_B_LO} ]] || _nb+="-${_NET_B_HI}" + _NET_SPEC="${_na}" + [[ "${_na}" == "${_nb}" ]] || _NET_SPEC+=":${_nb}" +} + +# _net_check_overlap OPTION WHAT LO HI LOS_ARRAY HIS_ARRAY +# Exit 1 when LO-HI overlaps a range already accepted for OPTION. WHAT +# names the side checked ("host port", "sandbox port"). +_net_check_overlap() { + local _opt="$1" _what="$2" _lo="$3" _hi="$4" _i _r="$3" + local -n _los="$5" _his="$6" + [[ ${_hi} -eq ${_lo} ]] || _r+="-${_hi}" + for _i in "${!_los[@]}"; do + if [[ ${_lo} -le ${_his[${_i}]} && ${_hi} -ge ${_los[${_i}]} ]]; then + echo "Error: ${_opt}: ${_what} ${_r} is given more than once." >&2 + exit 1 + fi + done +} + +# net_add_publish SPEC +# --publish PORT[:SANDBOXPORT]: make sandbox port SANDBOXPORT (default PORT) +# reachable on the host at the session's publish address, port PORT. +net_add_publish() { + _net_check_port_spec --publish "$1" + _net_check_overlap --publish "host port" "${_NET_A_LO}" "${_NET_A_HI}" _NET_PUB_LO _NET_PUB_HI + _NET_PUB_LO+=("${_NET_A_LO}") + _NET_PUB_HI+=("${_NET_A_HI}") + NET_PUBLISH+=("${_NET_SPEC}") +} + +# net_add_host_port SPEC +# --host-port PORT[:SANDBOXPORT]: make the host's 127.0.0.1:PORT reachable in +# the sandbox at host.internal:SANDBOXPORT (default PORT). Ports on the +# deny list (_NET_HOST_PORT_DENY) are refused. +net_add_host_port() { + local _d _dlo _dhi + _net_check_port_spec --host-port "$1" + for _d in "${_NET_HOST_PORT_DENY[@]}"; do + _dlo="${_d%-*}" + _dhi="${_d#*-}" + if [[ ${_NET_A_LO} -le ${_dhi} && ${_NET_A_HI} -ge ${_dlo} ]]; then + echo "Error: --host-port: host port ${_d} is refused: the service there trusts every local client." >&2 + exit 1 + fi + done + _net_check_overlap --host-port "sandbox port" "${_NET_B_LO}" "${_NET_B_HI}" _NET_HP_LO _NET_HP_HI + _NET_HP_LO+=("${_NET_B_LO}") + _NET_HP_HI+=("${_NET_B_HI}") + NET_HOST_PORTS+=("${_NET_SPEC}") +} + +# _net_check_lo_addr OPTION ADDR +# Exit 1 unless ADDR is a dotted-quad address in 127.0.0.0/8 (not the +# network or broadcast address), written without leading zeros. +_net_check_lo_addr() { + local _opt="$1" _addr="$2" _o='(0|[1-9][0-9]{0,2})' _i + if [[ ! "${_addr}" =~ ^127\.${_o}\.${_o}\.${_o}$ ]]; then + echo "Error: ${_opt}: '${_addr}' is not an IPv4 loopback address (127.x.y.z)." >&2 + exit 1 + fi + for _i in 1 2 3; do + if [[ ${BASH_REMATCH[${_i}]} -gt 255 ]]; then + echo "Error: ${_opt}: '${_addr}' is not a valid address." >&2 + exit 1 + fi + done + if [[ "${_addr}" == 127.0.0.0 || "${_addr}" == 127.255.255.255 ]]; then + echo "Error: ${_opt}: '${_addr}' cannot be used." >&2 + exit 1 + fi +} + +# net_set_publish_addr ADDR +# --publish-addr: use ADDR (in 127.0.0.0/8) on the host instead of the +# derived per-user, per-session address, for example 127.0.0.1 for OAuth +# redirect URIs that must name it. +net_set_publish_addr() { + _net_check_lo_addr --publish-addr "$1" + NET_PUBLISH_ADDR="$1" +} + +# net_set_log_file PATH +# --net-log: have pasta log every flow (debug level) to PATH on the host. +net_set_log_file() { + local _dir + if [[ -z "$1" ]]; then + echo "Error: --net-log requires a file name." >&2 + exit 1 + fi + _dir="$(dirname -- "$1")" + if [[ ! -d "${_dir}" || ! -w "${_dir}" ]]; then + echo "Error: --net-log: directory '${_dir}' does not exist or is not writable." >&2 + exit 1 + fi + NET_LOG_FILE="$(readlink -f -- "${_dir}")/${1##*/}" +} + +# _net_listeners +# Fill _NET_LISTEN ("ADDR PORT" lines) from the host's listening TCP sockets +# (ss -Hltn). Wildcard addresses (0.0.0.0, *, ::) are written as `*`, and +# IPv4-mapped IPv6 addresses as plain IPv4. Empty, with a warning, when ss +# is missing: pasta's own bind then reports a busy port. +_NET_LISTEN=() +_net_listeners() { + local _state _rq _sq _local _rest _addr _port + _NET_LISTEN=() + if ! command -v ss > /dev/null 2>&1; then + echo "Warning: ss (iproute) not found: busy publish ports are not detected in advance." >&2 + return 0 + fi + while read -r _state _rq _sq _local _rest; do + [[ -n "${_local}" && "${_local}" == *:* ]] || continue + _port="${_local##*:}" + _addr="${_local%:*}" + _addr="${_addr#[}" + _addr="${_addr%]}" + _addr="${_addr%%\%*}" + _addr="${_addr#::ffff:}" + case "${_addr}" in + 0.0.0.0 | '*' | ::) _addr='*' ;; + esac + _NET_LISTEN+=("${_addr} ${_port}") + done < <(ss -Hltn 2> /dev/null || true) +} + +# _net_busy_port ADDR +# Set _NET_BUSY to the first published host port that a listener already +# holds on ADDR (or on a wildcard address), empty when none. _NET_BUSY_WILD +# is 1 when that listener is a wildcard one. +_NET_BUSY="" +_NET_BUSY_WILD=0 +_net_busy_port() { + local _want="$1" _l _a _p _i + _NET_BUSY="" + _NET_BUSY_WILD=0 + for _l in ${_NET_LISTEN[@]+"${_NET_LISTEN[@]}"}; do + _a="${_l% *}" + _p="${_l##* }" + [[ "${_a}" == "${_want}" || "${_a}" == '*' ]] || continue + for _i in "${!_NET_PUB_LO[@]}"; do + if [[ ${_p} -ge ${_NET_PUB_LO[${_i}]} && ${_p} -le ${_NET_PUB_HI[${_i}]} ]]; then + _NET_BUSY="${_p}" + [[ "${_a}" != '*' ]] || _NET_BUSY_WILD=1 + return 0 + fi + done + done +} + +# _net_derive_publish_addr +# Set _NET_PUBLISH_ADDR_EFFECTIVE for --publish. Without --publish-addr the +# address is 127.H1.H2.S: H1.H2 from a hash of the uid (H1 in 1-254, so +# 127.0.x.x, 127.0.0.53 and 127.0.1.1 are never used), S the lowest session +# index in 1-254 whose address has none of the requested ports in use. +# With --publish-addr, that address must have them all free. Exit 1 when +# no address can be used. --dry-run checks nothing and shows S=1. +_net_derive_publish_addr() { + local _hex _v _h1 _h2 _s _cand + if [[ "${DRY_RUN}" -eq 0 ]]; then + _net_listeners + fi + if [[ -n "${NET_PUBLISH_ADDR}" ]]; then + _NET_PUBLISH_ADDR_EFFECTIVE="${NET_PUBLISH_ADDR}" + [[ "${DRY_RUN}" -eq 0 ]] || return 0 + _net_busy_port "${NET_PUBLISH_ADDR}" + if [[ -n "${_NET_BUSY}" ]]; then + echo "Error: --publish: port ${_NET_BUSY} is already in use on ${NET_PUBLISH_ADDR} (see: ss -ltnp)." >&2 + exit 1 + fi + return 0 + fi + + _hex="$(printf '%s' "$(id -u)" | sha256sum)" + _v=$((16#${_hex:0:4})) + _h1=$((1 + (_v >> 8) % 254)) + _h2=$((_v & 255)) + if [[ "${DRY_RUN}" -eq 1 ]]; then + _NET_PUBLISH_ADDR_EFFECTIVE="127.${_h1}.${_h2}.1" + echo "${0##*/}: dry-run: session index 1 shown for the publish address; a real run takes the first free one." >&2 + return 0 + fi + for ((_s = 1; _s <= 254; _s++)); do + _cand="127.${_h1}.${_h2}.${_s}" + _net_busy_port "${_cand}" + if [[ -z "${_NET_BUSY}" ]]; then + _NET_PUBLISH_ADDR_EFFECTIVE="${_cand}" + return 0 + fi + if [[ "${_NET_BUSY_WILD}" -eq 1 ]]; then + echo "Error: --publish: port ${_NET_BUSY} is already in use on all host addresses (see: ss -ltnp); choose another port." >&2 + exit 1 + fi + done + echo "Error: --publish: all 254 publish addresses 127.${_h1}.${_h2}.1-254 have a requested port in use." >&2 + echo " This usually means leftover sandbox (pasta) processes; see: ss -ltnp 'src 127.${_h1}.${_h2}.0/24'" >&2 + exit 1 +} + +# _net_join SPEC... +# Print the specs joined with commas. +_net_join() { + local IFS=, + printf '%s' "$*" +} + +# build_net_isolation +# Finish PASTA_ARGS from the --publish / --host-port / --net-log options, +# add the bwrap namespace options pasta needs, generate /etc/resolv.conf and +# /etc/hosts, and export BWRAP_NET_*. Call after +# detect_net_capabilities and build_etc_mounts, and before +# build_sandbox_context (the context file describes the forwards). +build_net_isolation() { + local _spec _host _sbx _i _hosts _resolv + _NET_CONFIGURED=1 + _NET_PUBLISH_DESC=() + _NET_HOST_DESC=() + _NET_PUBLISH_ADDR_EFFECTIVE="" + [[ ${#PASTA_ARGS[@]} -gt 0 ]] || _net_baseline_args + + # pasta maps the caller to uid 0 in its user namespace: run the sandbox as + # the real user in a nested one, and undo pasta's hostname change. + BWRAP_ARGS+=( + --unshare-user + --uid "$(id -u)" + --gid "$(id -g)" + --unshare-uts + --hostname "$(uname -n)" + ) + + if [[ -n "${NET_PUBLISH_ADDR}" && ${#NET_PUBLISH[@]} -eq 0 ]]; then + echo "Warning: --publish-addr has no effect without --publish." >&2 + fi + if [[ ${#NET_PUBLISH[@]} -gt 0 ]]; then + _net_derive_publish_addr + for _i in "${!PASTA_ARGS[@]}"; do + if [[ "${PASTA_ARGS[${_i}]}" == -t ]]; then + PASTA_ARGS[_i + 1]="${_NET_PUBLISH_ADDR_EFFECTIVE}/$(_net_join "${NET_PUBLISH[@]}")" + fi + done + for _spec in "${NET_PUBLISH[@]}"; do + _host="${_spec%%:*}" + _sbx="${_spec#*:}" + if [[ "${_host}" == *-* ]]; then + _NET_PUBLISH_DESC+=("${_NET_PUBLISH_ADDR_EFFECTIVE}:${_host}|${_sbx}") + else + _NET_PUBLISH_DESC+=("http://${_NET_PUBLISH_ADDR_EFFECTIVE}:${_host}/|${_sbx}") + fi + done + fi + + if [[ ${#NET_HOST_PORTS[@]} -gt 0 ]]; then + # pasta's -T takes NAMESPACE_PORT:HOST_PORT, the reverse of ours. + local -a _tspec=() + for _spec in "${NET_HOST_PORTS[@]}"; do + _host="${_spec%%:*}" + _sbx="${_spec#*:}" + if [[ "${_host}" == "${_sbx}" ]]; then + _tspec+=("${_host}") + else + _tspec+=("${_sbx}:${_host}") + fi + _NET_HOST_DESC+=("127.0.0.1:${_host}|${_NET_HOST_INTERNAL_NAME}:${_sbx}") + done + for _i in "${!PASTA_ARGS[@]}"; do + if [[ "${PASTA_ARGS[${_i}]}" == -T ]]; then + PASTA_ARGS[_i + 1]="${_NET_HOST_INTERNAL_ADDR}/$(_net_join "${_tspec[@]}")" + fi + done + fi + + if [[ -n "${NET_LOG_FILE}" ]]; then + PASTA_ARGS+=(--log-file "${NET_LOG_FILE}" --debug) + fi + + # DNS: pasta forwards queries for _NET_DNS_FORWARD_ADDR to the host's first + # nameserver. Only IPv4 is forwarded. + if [[ -r /etc/resolv.conf ]] && + ! grep -Eq '^[[:space:]]*nameserver[[:space:]]+[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' /etc/resolv.conf; then + echo "Warning: /etc/resolv.conf has no IPv4 nameserver: name resolution will not work in the sandbox." >&2 + fi + _resolv="nameserver ${_NET_DNS_FORWARD_ADDR}"$'\n'"options edns0" + bwrap_fd_from_data "generated /etc/resolv.conf" "${_resolv}" + BWRAP_ARGS+=(--ro-bind-data "${_BWRAP_FD}" /etc/resolv.conf) + + if [[ -r /etc/hosts ]]; then + _hosts="$(< /etc/hosts)" + else + _hosts="127.0.0.1 localhost"$'\n'"::1 localhost" + fi + if [[ ${#NET_HOST_PORTS[@]} -gt 0 ]]; then + _hosts+=$'\n'"${_NET_HOST_INTERNAL_ADDR} ${_NET_HOST_INTERNAL_NAME}" + fi + bwrap_fd_from_data "generated /etc/hosts" "${_hosts}" + BWRAP_ARGS+=(--ro-bind-data "${_BWRAP_FD}" /etc/hosts) + + SANDBOX_ENV+=( + "BWRAP_NET_PUBLISH=$(_net_join ${NET_PUBLISH[@]+"${NET_PUBLISH[@]}"})" + "BWRAP_NET_PUBLISH_ADDR=${_NET_PUBLISH_ADDR_EFFECTIVE}" + "BWRAP_NET_HOST_PORTS=$(_net_join ${NET_HOST_PORTS[@]+"${NET_HOST_PORTS[@]}"})" + "BWRAP_NET_HOST_ADDR=$([[ ${#NET_HOST_PORTS[@]} -eq 0 ]] || printf '%s' "${_NET_HOST_INTERNAL_ADDR}")" + ) +} + +# _net_print_report +# Print the network setup and every forward to stderr, so it stays in the +# scrollback above the TUI. With no forwards only the first line, and only +# when stderr is a terminal. Called by launch_sandbox. +_net_print_report() { + local _launcher="${0##*/}" _spec _line + local _n_fwd=$((${#_NET_PUBLISH_DESC[@]} + ${#_NET_HOST_DESC[@]})) + if [[ ${_n_fwd} -gt 0 || -t 2 ]]; then + _line="${_launcher}: network: private namespace (pasta)" + [[ -z "${_NET_PUBLISH_ADDR_EFFECTIVE}" ]] || _line+="; publish address ${_NET_PUBLISH_ADDR_EFFECTIVE}" + printf '%s\n' "${_line}" >&2 + fi + for _spec in ${_NET_PUBLISH_DESC[@]+"${_NET_PUBLISH_DESC[@]}"}; do + printf '%s: publish %s -> sandbox :%s (bind 0.0.0.0 inside)\n' \ + "${_launcher}" "${_spec%%|*}" "${_spec#*|}" >&2 + done + for _spec in ${_NET_HOST_DESC[@]+"${_NET_HOST_DESC[@]}"}; do + printf '%s: host-port host %s -> sandbox %s (host service sees a loopback client)\n' \ + "${_launcher}" "${_spec%%|*}" "${_spec#*|}" >&2 + done + if [[ -n "${NET_LOG_FILE}" ]]; then + printf '%s: network log %s\n' "${_launcher}" "${NET_LOG_FILE}" >&2 + fi +} + +# _net_require_configured +# Exit 1 unless build_net_isolation has run: there is no launch without the +# private network namespace. +_net_require_configured() { + if [[ "${_NET_CONFIGURED}" -ne 1 || -z "${_PASTA_BIN}" ]]; then + echo "Error: ${0##*/}: network isolation was not set up (detect_net_capabilities / build_net_isolation missing)." >&2 + exit 1 + fi +} + +# _net_warn_log_visible +# Warn when the --net-log file lies under a read-write mount, where the +# agent could read or rewrite it. Called by print_dry_run and +# launch_sandbox, once every mount is known. +_net_warn_log_visible() { + [[ -n "${NET_LOG_FILE}" ]] || return 0 + local _i=0 _flag + while [[ ${_i} -lt ${#BWRAP_ARGS[@]} ]]; do + _flag="${BWRAP_ARGS[${_i}]}" + _bwrap_flag_arity "${_flag}" + case "${_flag}" in + --bind | --bind-try) + if _is_prefix_of "${BWRAP_ARGS[$((_i + 1))]}" "${NET_LOG_FILE}"; then + echo "Warning: --net-log: ${NET_LOG_FILE} is under a read-write mount (${BWRAP_ARGS[$((_i + 1))]}); the agent can read and change it." >&2 + return 0 + fi + ;; + esac + _i=$((_i + 1 + _BWRAP_FLAG_ARITY)) + done +} + # ── OpenCode config requirement ─────────────────────────────────── # require_opencode_config WRAPPER USER_CONFIG_DIR # Exit 2 unless at least a minimal OpenCode config exists: a system-wide @@ -3183,6 +3762,47 @@ _bwrap_context_under_writable() { return 1 } +# _bwrap_context_network +# Set _CTX_NET to the "Network" section of the context file, from the +# forwards set up by build_net_isolation. +_CTX_NET="" +_bwrap_context_network() { + local _nl=$'\n' _d _t="" + _t+="## Network${_nl}${_nl}" + _t+="This sandbox has its own network namespace (pasta)." + _t+=" Outbound connections work and leave from the host's address;" + _t+=" DNS goes to \`${_NET_DNS_FORWARD_ADDR}\`, which is forwarded to the host's resolver." + _t+=" Services listening on the host's loopback (\`127.0.0.1\`, \`localhost\`) are not reachable from here," + _t+=" and nothing you listen on here is reachable from the host, except as listed below." + _t+=" \`127.0.0.1\` here is the sandbox's own loopback." + _t+=" ICMP (\`ping\`) may not work.${_nl}${_nl}" + + _t+="Published ports (a server you run here, reachable from the host):" + if [[ ${#_NET_PUBLISH_DESC[@]} -eq 0 ]]; then + _t+=" none.${_nl}${_nl}" + else + _t+="${_nl}${_nl}" + for _d in "${_NET_PUBLISH_DESC[@]}"; do + _t+="- \`${_d%%|*}\` on the host reaches port \`${_d#*|}\` here.${_nl}" + done + _t+="${_nl}**Bind \`0.0.0.0\`, not \`127.0.0.1\`, or the forward will not reach your server.**" + _t+=" Tell the user the host address above, not \`localhost\`.${_nl}${_nl}" + fi + + _t+="Host services (reachable from here):" + if [[ ${#_NET_HOST_DESC[@]} -eq 0 ]]; then + _t+=" none.${_nl}${_nl}" + else + _t+="${_nl}${_nl}" + for _d in "${_NET_HOST_DESC[@]}"; do + _t+="- \`${_d#*|}\` (\`${_NET_HOST_INTERNAL_ADDR}\`) reaches the host's \`${_d%%|*}\`.${_nl}" + done + _t+="${_nl}Your own \`127.0.0.1\` is unaffected: a server you start on the same port is separate.${_nl}${_nl}" + fi + _t+="\`BWRAP_NET_PUBLISH\`, \`BWRAP_NET_PUBLISH_ADDR\`, \`BWRAP_NET_HOST_PORTS\` and \`BWRAP_NET_HOST_ADDR\` hold the same information.${_nl}${_nl}" + _CTX_NET="${_t}" +} + # _bwrap_context_render TOOL DEST # Build the context file text for launcher TOOL (to be mounted at DEST) in # _SANDBOX_CONTEXT_TEXT, from BWRAP_ARGS, SANDBOX_ENV and the launcher's @@ -3270,7 +3890,12 @@ _bwrap_context_render() { _t+=" and only if the source is mounted read-write below." _t+=" \"Read-only file system\" on a mount point is expected.${_nl}" _t+="- \`ln -s\` creates ordinary symlinks, in writable locations only.${_nl}" - _t+="- The network is shared with the host. Host processes are not visible (private PID namespace)." + if [[ "${_NET_CONFIGURED}" -eq 1 ]]; then + _t+="- The network is private to this sandbox (see Network below)." + else + _t+="- The network is shared with the host." + fi + _t+=" Host processes are not visible (private PID namespace)." _t+=" There are no SSH or GPG agents and no sudo.${_nl}${_nl}" _bwrap_context_mode_at "${_PWD}" @@ -3295,6 +3920,10 @@ _bwrap_context_render() { fi _t+="${_nl}${_nl}" + _CTX_NET="" + [[ "${_NET_CONFIGURED}" -ne 1 ]] || _bwrap_context_network + _t+="${_CTX_NET}" + _t+="## Mounts${_nl}${_nl}" _t+="\`rw\` is the real host files (often NFS): changes persist and show on the host at once." _t+=" \`ro\` is read-only. \`copy\` is a writable copy of a host file: changes are lost at exit." @@ -3685,6 +4314,8 @@ _is_secret_env_name() { print_dry_run() { verify_n2c_state_not_exposed + _net_require_configured + _net_warn_log_visible local _BWRAP_BIN _ENV_BIN _BASH_BIN _BWRAP_BIN="$(resolve_bwrap_bin)" _ENV_BIN="$(resolve_env_bin)" @@ -3692,6 +4323,7 @@ print_dry_run() { # Mirror the exact invocation used by launch_sandbox: # exec -i + # -- # -c bwrap-fd-relay -- # -- <_TOOL_CMD> # Values of secret-looking variables (_is_secret_env_name) are printed as @@ -3717,6 +4349,21 @@ print_dry_run() { for _n in ${_BWRAP_FD_DATA_NUMS[@]+"${_BWRAP_FD_DATA_NUMS[@]}"}; do printf " _BWRAP_FD_DATA_%s='<%s, shown below>' \\\\\n" "${_n}" "${_BWRAP_FD_LABEL["${_n}"]}" done + printf " %s \\\\\n" "${_PASTA_BIN}" + local _p=0 + while [[ ${_p} -lt ${#PASTA_ARGS[@]} ]]; do + case "${PASTA_ARGS[${_p}]}" in + -t | -u | -T | -U | --dns-forward | --search | --log-file) + printf " %s %q \\\\\n" "${PASTA_ARGS[${_p}]}" "${PASTA_ARGS[$((_p + 1))]}" + _p=$((_p + 2)) + ;; + *) + printf " %s \\\\\n" "${PASTA_ARGS[${_p}]}" + _p=$((_p + 1)) + ;; + esac + done + printf " -- \\\\\n" printf " %s -c '<_BWRAP_FD_RELAY_SHIM from bwrap_sandbox_lib.sh>' bwrap-fd-relay \\\\\n" "${_BASH_BIN}" local _r=0 while [[ ${_r} -lt ${#_BWRAP_FD_RELAY[@]} ]]; do @@ -3782,10 +4429,13 @@ print_dry_run() { # via PATH lookup at exec time — this prevents a user shell script named # "env" earlier on PATH from hijacking the launch. See resolve_env_bin. # -# bwrap is started through the descriptor relay (see "File descriptor -# relay"), which opens the descriptors that BWRAP_ARGS refers to. +# pasta starts the descriptor relay in the sandbox's network namespace (see +# "Network isolation"); the relay opens the descriptors that BWRAP_ARGS refers +# to (see "File descriptor relay") and execs bwrap. launch_sandbox() { verify_n2c_state_not_exposed + _net_require_configured + _net_warn_log_visible local _BWRAP_BIN _ENV_BIN _BASH_BIN _n _BWRAP_BIN="$(resolve_bwrap_bin)" _ENV_BIN="$(resolve_env_bin)" @@ -3797,6 +4447,7 @@ launch_sandbox() { _data_env+=("_BWRAP_FD_DATA_${_n}=${_BWRAP_FD_DATA["${_n}"]}") done + _net_print_report exec "${_ENV_BIN}" -i \ HOME="${_HOME}" \ USER="${_USER}" \ @@ -3804,6 +4455,7 @@ launch_sandbox() { PATH="/usr/bin:/bin" \ ${SANDBOX_ENV[@]+"${SANDBOX_ENV[@]}"} \ ${_data_env[@]+"${_data_env[@]}"} \ + "${_PASTA_BIN}" "${PASTA_ARGS[@]}" -- \ "${_BASH_BIN}" -c "${_BWRAP_FD_RELAY_SHIM}" bwrap-fd-relay \ ${_BWRAP_FD_RELAY[@]+"${_BWRAP_FD_RELAY[@]}"} -- \ "${_BWRAP_BIN}" "${BWRAP_ARGS[@]}" -- "${_TOOL_CMD[@]}" ${TOOL_ARGS[@]+"${TOOL_ARGS[@]}"} From 6cacc54b5602e756a23f2e09ca892f9db31dfe17 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 29 Oct 2026 23:03:35 -0400 Subject: [PATCH 05/37] docs(bwrap): describe the private network namespace Document the pasta requirement, --publish / --publish-addr / --host-port / --net-log, the per-user publish addresses and what they do not guarantee, host.internal and the loopback-trust caveat, and the descriptor relay. Assisted-by: opencode:claude-opus-5-5 --- README.md | 9 ++- docs/bw-wrappers.md | 179 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 171 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f3f4d5c..f7f171e 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,9 @@ Add those three exports to your `~/.bashrc` or `~/.zshrc` for persistence. - `bash` >= 4.x (the `bw*` wrappers use `[[ ]]` and arrays) - `bubblewrap` (`bwrap`) >= 0.4.0 for the `bw*` wrappers - binary masking is verified on RHEL 8.10 (`bubblewrap-0.4.0-3.el8_10`) and EL9 (0.6.3) +- `pasta` (package `passt`), `/dev/net/tun` and unprivileged user namespaces + for the `bw*` wrappers, which run every sandbox in a private network + namespace (no fallback: they exit 3 without it) - `gpg(1)` for `gpg-passwd.sh` - `openssl(1)` for `pemdecompose` - `python3 >= 3.9` (stdlib only, no pip dependencies) for `azoidcapp` @@ -96,13 +99,17 @@ Common wrapper options: | Option | Effect | | --- | --- | | `--help`, `-h` | Show wrapper help, then the underlying tool's help when the tool is installed | -| `--dry-run` | Print the `bwrap` command without executing it | +| `--dry-run` | Print the command (`pasta`, descriptor relay, `bwrap`) without executing it | | `--exec CMD [ARGS...]` | Run `CMD` inside the sandbox instead of the tool. `CMD` is split on whitespace; following arguments are passed unchanged, e.g. `--exec bash -c 'env \| sort'` | | `--init-auth` | Persist auth credentials to the host (first-time setup) | | `--new-session` | Force `bwrap --new-session` (stricter isolation; breaks SIGWINCH) | | `--ro-path PATH`, `--rw-path PATH` | Mount an existing path read-only / read-write (may be repeated and nested) | | `--github-tokens` | Forward `GH_TOKEN_*` variables so `gh` can authenticate inside the sandbox (off by default) | | `--no-sandbox-context` | Do not tell the agent about the sandbox (by default a short description of the mounts is added to its instructions) | +| `--publish PORT[:SANDBOXPORT]` | Make a server in the sandbox reachable on the host at a per-user, per-session `127.x.y.z` address (printed at launch) | +| `--publish-addr ADDR` | Use `ADDR` (in `127.0.0.0/8`, e.g. `127.0.0.1` for OAuth) for `--publish` | +| `--host-port PORT[:SANDBOXPORT]` | Make the host's `127.0.0.1:PORT` reachable in the sandbox at `host.internal` | +| `--net-log FILE` | Log every network flow to `FILE` on the host | `bwclaude` also supports `--debug`. `bwopencode2` has no `--init-auth` (sign-in persists in its data directory), always runs OpenCode 2 with `--standalone` diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 5159890..4248df7 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -17,6 +17,10 @@ option list; the tables below summarise the common surface. - `bash` >= 4.x (the wrappers use `[[ ]]` and arrays) - `bubblewrap` (`bwrap`) >= 0.4.0 - binary masking is verified on RHEL 8.10 (`bubblewrap-0.4.0-3.el8_10`) and EL9 (0.6.3) +- `pasta` (package `passt`), `/dev/net/tun`, and unprivileged user namespaces + on the host. There is no fallback: without them every `bw*` wrapper stops + with exit status 3 (see [Network](#network)). `ss` (iproute) is used to + find busy ports for `--publish`. ## Managed system paths @@ -86,7 +90,7 @@ Every `bw*` wrapper accepts these options: | Option | Effect | | --- | --- | | `--help`, `-h` | Show wrapper help, then the underlying tool's help when the tool is installed | -| `--dry-run` | Print the `bwrap` command without executing it | +| `--dry-run` | Print the command (`pasta`, the descriptor relay and `bwrap`) without executing it | | `--exec CMD [ARGS...]` | Run `CMD` inside the sandbox instead of the tool. `CMD` is split on whitespace; following arguments are passed unchanged, e.g. `--exec bash -c 'env \| sort'` | | `--init-auth` | Persist auth credentials to the host (first-time setup; not available in `bwopencode2`) | | `--new-session` | Force `bwrap --new-session` (stricter isolation; breaks SIGWINCH) | @@ -95,6 +99,10 @@ Every `bw*` wrapper accepts these options: | `--n2c-token NAME` | Mount the N2C access token `NAME` read-only so skills in the sandbox can call NSLS-II APIs. May be repeated; `--n2c-token all` mounts every token present. Off by default. See [N2C access tokens](#n2c-access-tokens) | | `--github-tokens` | Forward every `GH_TOKEN_*` environment variable into the sandbox so the agent can authenticate `gh`. Off by default. In `--dry-run` mode, token values are printed as `REDACTED`. Use per-command token selection inside the sandbox: `GH_TOKEN="$GH_TOKEN_NSLS2" gh pr list -R NSLS2/repo` | | `--no-sandbox-context` | Do not generate the [sandbox context file](#sandbox-context) that tells the agent what is mounted | +| `--publish PORT[:SANDBOXPORT]` | Make a server in the sandbox reachable on the host at a per-user, per-session loopback address. May be repeated. See [Network](#network) | +| `--publish-addr ADDR` | Use `ADDR` (in `127.0.0.0/8`) for `--publish` instead of the derived address, for example `127.0.0.1` for OAuth redirects | +| `--host-port PORT[:SANDBOXPORT]` | Make the host's `127.0.0.1:PORT` reachable in the sandbox at `host.internal:SANDBOXPORT`. May be repeated | +| `--net-log FILE` | Log every network flow to `FILE` on the host (`pasta --debug`) | `--ro-path` and `--rw-path` may be nested: a path nested under another overrides it, so `--rw-path DIR --ro-path DIR/sub` mounts `DIR` read-write but @@ -224,12 +232,16 @@ and makes the tool load it. `bwrap` arguments, so the text always matches the session, including `--ro-path` / `--rw-path` mounts and a working directory outside `$HOME`. - **Mounted at `~/.sandbox/AGENTS.md`**, read-only, with `--ro-bind-data` on a - file descriptor. Nothing is written to the host, and no user file is + file descriptor (see [Descriptor relay](#descriptor-relay)). Nothing is + written to the host, and no user file is overwritten. `BWRAP_SANDBOX` (launcher name) and `BWRAP_SANDBOX_CONTEXT` (the path) are set in the sandbox for scripts and skills. - **Contents** (about 50 lines): `$HOME` is a tmpfs; same-inode files are bind mounts, not hard links, and are edited at their source; the working directory (its access taken from the effective mounts) and whether it is a git root; a + Network section (private namespace, DNS, the published ports with their host + URLs and the "bind `0.0.0.0`" rule, the host services under + `host.internal`); a table of every mount under `$HOME` marked `rw` / `ro` / `copy` (a writable copy of a host file, lost at exit) / `tmpfs`; the system mounts collapsed into one line each, with writable exceptions such as `--rw-path /usr/local` @@ -257,14 +269,144 @@ files: `--exec` mounts the file and sets the two variables but adds no loader, since the command is not the agent. -`--dry-run` prints the generated file after the command (the -`--ro-bind-data` descriptor number is only meaningful in a real run). +`--dry-run` prints the generated file after the command, with the other +generated files (`/etc/resolv.conf`, `/etc/hosts`). `--no-sandbox-context` removes the file, the variables and the loader. To check by hand, ask the agent a question only the file can answer ("is a read-only file under `$HOME` with the same inode as a dotfiles file a hard link?"), or for Copilot run `bwcopilot instruction list`. +## Network + +Every sandbox runs in its own network namespace, created by +[pasta](https://passt.top) (package `passt`): + +```text +env -i ... pasta [options] -- bash -- bwrap [options] -- +``` + +- **Outbound connections work** (model gateway, package installs, docs) and + leave from the host's address. Egress is not filtered. +- **The host's loopback is unreachable.** Services bound to `127.0.0.1` on the + host (OpenCode's own server, language servers, CUPS, local databases, Tiled + or queueserver dev instances, other users' sessions) cannot be reached from + the sandbox, and `ss -ltn` inside shows only the sandbox's own sockets. + `127.0.0.1` inside is the sandbox's private loopback. +- **Nothing the agent listens on is reachable from the host**, unless + published (below). +- **Not hidden:** host services bound to a non-loopback address (sshd on + `0.0.0.0:22`, the Tailscale address) stay reachable, as from any other + machine. +- **No fallback.** If `pasta` is missing, `/dev/net/tun` is missing, or a test + run of `pasta` fails (for example unprivileged user namespaces are disabled), + the wrapper stops with exit status 3 and names the problem. `--dry-run` works + without `pasta`. Deploy `passt` before this version of the wrappers. + +DNS: the host's resolver is usually on loopback (`127.0.0.53`), so the sandbox +gets a generated `/etc/resolv.conf` naming `169.254.1.1`, which pasta forwards +to the first nameserver in the host's `/etc/resolv.conf`. Only IPv4 +nameservers are supported; the wrapper warns when the host has none. The host's +search domains are not passed on, so use fully qualified names. `/etc/hosts` is +the host's file, plus `host.internal` when `--host-port` is used. + +### Opening ports + +Both directions are off by default, opt-in per port and per launch, and +limited to loopback on both ends. Ports are `1024-65535`; a `LOW-HIGH` range +is accepted, and a mapped range must have the same length. + +| Option | Direction | Host side | Sandbox side | +| --- | --- | --- | --- | +| `--publish PORT[:SANDBOXPORT]` | sandbox server → host | `127.H1.H2.S:PORT` (or `--publish-addr`) | the server on `SANDBOXPORT` (default `PORT`), which must listen on `0.0.0.0` | +| `--host-port PORT[:SANDBOXPORT]` | host service → sandbox | `127.0.0.1:PORT` | `host.internal:SANDBOXPORT` (`127.0.42.1`, default `PORT`) | + +Forwards are printed to stderr at launch, so they stay in the scrollback above +the TUI, and listed in the [sandbox context file](#sandbox-context). Inside the +sandbox `BWRAP_NET_PUBLISH`, `BWRAP_NET_PUBLISH_ADDR`, `BWRAP_NET_HOST_PORTS` +and `BWRAP_NET_HOST_ADDR` hold the same information. + +```text +bwopencode2: network: private namespace (pasta); publish address 127.65.81.1 +bwopencode2: publish http://127.65.81.1:8000/ -> sandbox :8000 (bind 0.0.0.0 inside) +bwopencode2: host-port host 127.0.0.1:5432 -> sandbox host.internal:5432 (host service sees a loopback client) +``` + +**`--publish`.** The host address is `127.H1.H2.S`: `H1.H2` comes from a hash +of your uid (`H1` is 1-254, so `127.0.0.1`, `127.0.0.53` and `127.0.1.1` are +never used), and `S` is the lowest session index (1-254) whose address has +none of the requested ports in use. Two users publishing port 8000 get +different addresses, a second sandbox of the same user gets the next index, +and your own dev server on `127.0.0.1:8000` does not collide. The address is +printed as a URL; there is no host name for it. Every `127/8` address is a +secure context in browsers. The host port is bound for the whole session, +whether or not a server runs. The server inside must bind `0.0.0.0` (or the +interface address): pasta delivers forwarded connections to the namespace's +interface, not its loopback; the context file tells the agent so. A port that +something on the host already holds on every address (`0.0.0.0:PORT`) is +refused at launch. + +**`--publish-addr ADDR`** replaces the derived address with `ADDR` (in +`127.0.0.0/8`). Use `127.0.0.1` for OAuth flows whose registered redirect URI +names it (some agent logins and MCP servers open a browser on the host that +calls back into the sandbox). The launch then fails if a requested port is +already in use on `ADDR`. + +**`--host-port`** makes one host loopback service visible in the sandbox as +`host.internal`, so the agent's own `127.0.0.1:PORT` stays free (it can run its +own Postgres on 5432 next to the host's). The host side is always +`127.0.0.1`. Nothing checks that the host port is open or who owns it. +**The host service sees a loopback client and may trust it** (no password for +local connections, admin APIs): only forward services you would let the agent +use. These host ports are refused outright: `631` (CUPS), `2375`-`2376` +(Docker), `6000`-`6063` (X11), `41112` (Tailscale local API) and `49374` +(OpenCode 2 service, which runs tools outside any sandbox); the list is +`_NET_HOST_PORT_DENY` in the library. + +**Not access control.** Any local user can connect to any `127/8` address, so +another user can reach your published ports; the per-user addresses prevent +accidental collisions, not access. Restricting that by uid would need +root-managed `nftables` rules. + +To list the sandbox publications on a host: +`ss -ltn 'src 127.0.0.0/8' | grep -v ' 127\.0\.'`. + +### Network log + +`--net-log FILE` passes `--log-file FILE --debug` to pasta: every flow is +logged on the host, which is the data needed to write an egress allowlist. +pasta keeps the newest 1 MiB. The wrapper warns when `FILE` is inside a +read-write mount of the sandbox, where the agent could change it. + +### Implementation notes + +- pasta runs outside bwrap, as you, and starts the sandbox. Default pasta + behaviour is overridden where it would re-expose the host's loopback: + `--no-map-gw` (the gateway address would otherwise map to the host's + loopback) and `-T none` / `-U none` (`auto` would forward every port bound + on the host's loopback into the namespace). `-t none` / `-u none` keep + inbound ports closed unless `--publish` is given. +- pasta maps you to uid 0 in its user namespace, so bwrap runs the agent in a + nested user namespace as your real uid and gid (`--unshare-user --uid + --gid`); file ownership looks as before and the agent has no capabilities. +- pasta renames the host to `pasta-`; bwrap's `--unshare-uts + --hostname` restores the real name. +- `--no-pidns` is passed when the installed pasta supports it (2026-09 and + later); older pasta adds a PID namespace, inside which bwrap nests its own. +- `ping` may not work (ICMP depends on the host's `ping_group_range`). +- Nested namespaces in the sandbox (rootless podman) still do not work. + +### Descriptor relay + +bwrap reads some files from descriptors: `--file` for writable copies of +credentials and config (`bwclaude`, `bwcopilot`, `bwcodex`), `--ro-bind-data` +for generated files (the context file, `/etc/resolv.conf`, `/etc/hosts`). +pasta closes every inherited descriptor above 2, so the launcher does not open +them itself. It reserves their numbers; a short fixed bash script (the relay, +`_BWRAP_FD_RELAY_SHIM`) runs as pasta's command, opens each descriptor at its +number, from the host file or from generated text carried in an environment +variable that it then unsets, and execs bwrap. Nothing is staged on the host. + ## Account check Every `bw*` launcher runs `/opt/nsls2/libexec/agent-account-check` first @@ -437,9 +579,9 @@ Only an allowlist of harmless `OPENCODE_*` behaviour toggles (46 names) is forwarded from the host environment into the sandbox. **Behaviour change:** any other host `OPENCODE_*` variable is now dropped (previously all were passed). Sensitive or location-changing variables are deliberately excluded: -`OPENCODE_SERVER_PASSWORD` / `OPENCODE_SERVER_USERNAME` (the sandbox shares -the host network, so an agent holding the password could drive an -unsandboxed opencode server), `OPENCODE_API_KEY`, `OPENCODE_CONSOLE_TOKEN`, +`OPENCODE_SERVER_PASSWORD` / `OPENCODE_SERVER_USERNAME` (an agent holding the +password and a route to an unsandboxed opencode server could drive it; the +private network namespace normally gives no such route), `OPENCODE_API_KEY`, `OPENCODE_CONSOLE_TOKEN`, `OPENCODE_CONFIG*` / `OPENCODE_TUI_CONFIG` / `OPENCODE_PERMISSION` (inject config or permissions and bypass the managed config), `OPENCODE_AUTH_CONTENT`, `OPENCODE_DB` / `OPENCODE_*_DB`, `OPENCODE_MODELS_URL` / `OPENCODE_MODELS_PATH`, @@ -583,9 +725,10 @@ in the sandbox. The rules, the recovery steps (`opencode2-db restore` and `OPENCODE_DISABLE_FFF`, `OPENCODE_DISABLE_PROJECT_CONFIG`, `OPENCODE_LOG_LEVEL`, `OPENCODE_PRINT_LOGS`. The variables that matter most are the server passwords (`OPENCODE_PASSWORD`, - `OPENCODE_SERVER_PASSWORD`): the sandbox shares the host network, so an agent - holding one could connect to an unsandboxed server on port 49374 and run - tools outside the sandbox. Others dropped include `OPENCODE_SSH_ASKPASS_*`, + `OPENCODE_SERVER_PASSWORD`): an agent holding one, with a route to an + unsandboxed server on port 49374, could run tools outside the sandbox. The + private network namespace normally gives no such route, and `--host-port` + refuses 49374. Others dropped include `OPENCODE_SSH_ASKPASS_*`, `OPENCODE_EDITOR_SSE_PORT`, `OPENCODE_PTY_HANDOFF`, `OPENCODE_DB`, `OPENCODE_TEST_HOME`, `OPENCODE_CONFIG*`, `OPENCODE_MODELS_PATH`/`_URL`, and `OPENCODE_API_KEY`. With `--dry-run`, the names (never the values) of the @@ -594,9 +737,10 @@ in the sandbox. The rules, the recovery steps (`opencode2-db restore` and ### `--standalone` is enforced By default OpenCode 2 connects to a shared background service (fixed port -49374) that survives the CLI and runs tools **outside** any sandbox. The -sandbox shares the host network, so a CLI started normally would hand tool -execution to that unsandboxed server. `bwopencode2` therefore: +49374) that survives the CLI and runs tools **outside** any sandbox. A CLI +started normally would hand tool execution to that unsandboxed server. The +sandbox's private network namespace already keeps the host's loopback, and so +the service, out of reach; as a second line of defence `bwopencode2`: - adds `--standalone` (a private in-process server inside the sandbox) when `opencode2` is started with no subcommand or a directory argument, or with @@ -812,7 +956,7 @@ Additional options: | Option | Effect | | --- | --- | | `--debug` | Enable verbose Codex logging (sets `RUST_LOG=debug`) | -| `--persist-config` | Bind-mount `~/.codex/config.toml` read-write into the sandbox so changes (e.g. project-trust grants) persist across sessions. Without this flag, `config.toml` is staged read-write into a per-session copy that is discarded on exit. Concurrent `--persist-config` sessions may race on writes | +| `--persist-config` | Bind-mount `~/.codex/config.toml` read-write into the sandbox so changes (e.g. project-trust grants) persist across sessions. Without this flag, `config.toml` is copied read-write into the sandbox's tmpfs for the session and the copy is discarded on exit. Concurrent `--persist-config` sessions may race on writes | Note: Codex CLI also enforces its own inner Landlock-based sandbox for agent tool calls. The outer `bwrap` here is complementary — it scopes @@ -823,5 +967,8 @@ the entire Codex process tree, not just agent-issued commands. Shared sandbox-building primitives sourced by all `bw*` wrappers. Not intended to be executed directly. The library header documents the public function surface (sandbox construction, dynamic mount discovery, -binary masking, environment scrubbing, and `build_sandbox_context` with its -`SANDBOX_CONTEXT_*` variables; see [Sandbox context](#sandbox-context)). +binary masking, environment scrubbing, `build_sandbox_context` with its +`SANDBOX_CONTEXT_*` variables (see [Sandbox context](#sandbox-context)), and +the network functions `detect_net_capabilities`, `net_add_publish`, +`net_add_host_port`, `net_set_publish_addr`, `net_set_log_file`, +`net_print_help` and `build_net_isolation` (see [Network](#network)). From 55683707aa0991b6ee102f44f1238c168168a344 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 30 Oct 2026 09:04:11 -0400 Subject: [PATCH 06/37] feat(bwrap): fall back to the host network without pasta passt is in RHEL 9 but not RHEL 8. Without a working pasta the sandbox now shares the host network as before, with a warning, instead of refusing to start. --local-net chooses the host network explicitly and silences the warning. The forwarding options still need pasta: with --local-net they are an error, and without pasta they stop the launch (exit 3). BWRAP_NET_MODE tells the sandbox which mode it is in, and the context file describes both. Assisted-by: opencode:claude-opus-5-5 --- README.md | 8 +- bin/bwclaude | 14 +++- bin/bwcodex | 14 +++- bin/bwcopilot | 14 +++- bin/bwopencode | 14 +++- bin/bwopencode2 | 14 +++- docs/bw-wrappers.md | 31 +++++-- lib/bwrap_sandbox_lib.sh | 175 ++++++++++++++++++++++++++++++--------- 8 files changed, 213 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index f7f171e..db74281 100644 --- a/README.md +++ b/README.md @@ -71,9 +71,10 @@ Add those three exports to your `~/.bashrc` or `~/.zshrc` for persistence. - `bash` >= 4.x (the `bw*` wrappers use `[[ ]]` and arrays) - `bubblewrap` (`bwrap`) >= 0.4.0 for the `bw*` wrappers - binary masking is verified on RHEL 8.10 (`bubblewrap-0.4.0-3.el8_10`) and EL9 (0.6.3) -- `pasta` (package `passt`), `/dev/net/tun` and unprivileged user namespaces - for the `bw*` wrappers, which run every sandbox in a private network - namespace (no fallback: they exit 3 without it) +- optional: `pasta` (package `passt`, RHEL 9+), `/dev/net/tun` and + unprivileged user namespaces, with which the `bw*` wrappers run every sandbox + in a private network namespace; without them (RHEL 8) the sandbox shares the + host network, with a warning - `gpg(1)` for `gpg-passwd.sh` - `openssl(1)` for `pemdecompose` - `python3 >= 3.9` (stdlib only, no pip dependencies) for `azoidcapp` @@ -110,6 +111,7 @@ Common wrapper options: | `--publish-addr ADDR` | Use `ADDR` (in `127.0.0.0/8`, e.g. `127.0.0.1` for OAuth) for `--publish` | | `--host-port PORT[:SANDBOXPORT]` | Make the host's `127.0.0.1:PORT` reachable in the sandbox at `host.internal` | | `--net-log FILE` | Log every network flow to `FILE` on the host | +| `--local-net` | Share the host network instead of a private namespace (the behaviour without `pasta`) | `bwclaude` also supports `--debug`. `bwopencode2` has no `--init-auth` (sign-in persists in its data directory), always runs OpenCode 2 with `--standalone` diff --git a/bin/bwclaude b/bin/bwclaude index 2a38265..50c3643 100755 --- a/bin/bwclaude +++ b/bin/bwclaude @@ -95,12 +95,14 @@ # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. # --net-log FILE Log every network flow to FILE on the host. +# --local-net Share the host network instead (the behaviour without +# pasta); not with the three options above. # # Network: the sandbox has its own network namespace from pasta (package -# passt; required, there is no fallback to the host network). Outbound -# connections work; services on the host's loopback are unreachable and the -# sandbox's listeners are invisible to the host, except for the ports opened -# with --publish and --host-port. +# passt). Outbound connections work; services on the host's loopback are +# unreachable and the sandbox's listeners are invisible to the host, except +# for the ports opened with --publish and --host-port. Without a working +# pasta (e.g. RHEL 8) the sandbox shares the host network, with a warning. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are @@ -204,6 +206,10 @@ parse_wrapper_args() { net_add_host_port "$2" shift 2 ;; + --local-net) + net_set_local + shift + ;; --net-log) if [[ $# -lt 2 ]]; then echo "Error: --net-log requires a file name." >&2 diff --git a/bin/bwcodex b/bin/bwcodex index 79770a4..5604129 100755 --- a/bin/bwcodex +++ b/bin/bwcodex @@ -112,12 +112,14 @@ # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. # --net-log FILE Log every network flow to FILE on the host. +# --local-net Share the host network instead (the behaviour without +# pasta); not with the three options above. # # Network: the sandbox has its own network namespace from pasta (package -# passt; required, there is no fallback to the host network). Outbound -# connections work; services on the host's loopback are unreachable and the -# sandbox's listeners are invisible to the host, except for the ports opened -# with --publish and --host-port. +# passt). Outbound connections work; services on the host's loopback are +# unreachable and the sandbox's listeners are invisible to the host, except +# for the ports opened with --publish and --host-port. Without a working +# pasta (e.g. RHEL 8) the sandbox shares the host network, with a warning. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are @@ -226,6 +228,10 @@ parse_wrapper_args() { net_add_host_port "$2" shift 2 ;; + --local-net) + net_set_local + shift + ;; --net-log) if [[ $# -lt 2 ]]; then echo "Error: --net-log requires a file name." >&2 diff --git a/bin/bwcopilot b/bin/bwcopilot index 221b021..5638557 100755 --- a/bin/bwcopilot +++ b/bin/bwcopilot @@ -80,12 +80,14 @@ # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. # --net-log FILE Log every network flow to FILE on the host. +# --local-net Share the host network instead (the behaviour without +# pasta); not with the three options above. # # Network: the sandbox has its own network namespace from pasta (package -# passt; required, there is no fallback to the host network). Outbound -# connections work; services on the host's loopback are unreachable and the -# sandbox's listeners are invisible to the host, except for the ports opened -# with --publish and --host-port. +# passt). Outbound connections work; services on the host's loopback are +# unreachable and the sandbox's listeners are invisible to the host, except +# for the ports opened with --publish and --host-port. Without a working +# pasta (e.g. RHEL 8) the sandbox shares the host network, with a warning. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are @@ -184,6 +186,10 @@ parse_wrapper_args() { net_add_host_port "$2" shift 2 ;; + --local-net) + net_set_local + shift + ;; --net-log) if [[ $# -lt 2 ]]; then echo "Error: --net-log requires a file name." >&2 diff --git a/bin/bwopencode b/bin/bwopencode index e8e990a..f768534 100755 --- a/bin/bwopencode +++ b/bin/bwopencode @@ -92,12 +92,14 @@ # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. # --net-log FILE Log every network flow to FILE on the host. +# --local-net Share the host network instead (the behaviour without +# pasta); not with the three options above. # # Network: the sandbox has its own network namespace from pasta (package -# passt; required, there is no fallback to the host network). Outbound -# connections work; services on the host's loopback are unreachable and the -# sandbox's listeners are invisible to the host, except for the ports opened -# with --publish and --host-port. +# passt). Outbound connections work; services on the host's loopback are +# unreachable and the sandbox's listeners are invisible to the host, except +# for the ports opened with --publish and --host-port. Without a working +# pasta (e.g. RHEL 8) the sandbox shares the host network, with a warning. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are @@ -196,6 +198,10 @@ parse_wrapper_args() { net_add_host_port "$2" shift 2 ;; + --local-net) + net_set_local + shift + ;; --net-log) if [[ $# -lt 2 ]]; then echo "Error: --net-log requires a file name." >&2 diff --git a/bin/bwopencode2 b/bin/bwopencode2 index 1b4edac..c2ef3d8 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -133,12 +133,14 @@ # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. # --net-log FILE Log every network flow to FILE on the host. +# --local-net Share the host network instead (the behaviour without +# pasta); not with the three options above. # # Network: the sandbox has its own network namespace from pasta (package -# passt; required, there is no fallback to the host network). Outbound -# connections work; services on the host's loopback are unreachable and the -# sandbox's listeners are invisible to the host, except for the ports opened -# with --publish and --host-port. +# passt). Outbound connections work; services on the host's loopback are +# unreachable and the sandbox's listeners are invisible to the host, except +# for the ports opened with --publish and --host-port. Without a working +# pasta (e.g. RHEL 8) the sandbox shares the host network, with a warning. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are @@ -277,6 +279,10 @@ parse_wrapper_args() { net_add_host_port "$2" shift 2 ;; + --local-net) + net_set_local + shift + ;; --net-log) if [[ $# -lt 2 ]]; then echo "Error: --net-log requires a file name." >&2 diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 4248df7..10b7833 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -17,10 +17,11 @@ option list; the tables below summarise the common surface. - `bash` >= 4.x (the wrappers use `[[ ]]` and arrays) - `bubblewrap` (`bwrap`) >= 0.4.0 - binary masking is verified on RHEL 8.10 (`bubblewrap-0.4.0-3.el8_10`) and EL9 (0.6.3) -- `pasta` (package `passt`), `/dev/net/tun`, and unprivileged user namespaces - on the host. There is no fallback: without them every `bw*` wrapper stops - with exit status 3 (see [Network](#network)). `ss` (iproute) is used to - find busy ports for `--publish`. +- Optional, for the private network namespace: `pasta` (package `passt`, + in RHEL 9 AppStream; not available for RHEL 8), `/dev/net/tun`, and + unprivileged user namespaces on the host. Without them the sandbox shares + the host network, with a warning (see [Network](#network)). `ss` (iproute) + is used to find busy ports for `--publish`. ## Managed system paths @@ -103,6 +104,7 @@ Every `bw*` wrapper accepts these options: | `--publish-addr ADDR` | Use `ADDR` (in `127.0.0.0/8`) for `--publish` instead of the derived address, for example `127.0.0.1` for OAuth redirects | | `--host-port PORT[:SANDBOXPORT]` | Make the host's `127.0.0.1:PORT` reachable in the sandbox at `host.internal:SANDBOXPORT`. May be repeated | | `--net-log FILE` | Log every network flow to `FILE` on the host (`pasta --debug`) | +| `--local-net` | Share the host network instead of a private namespace (the behaviour without `pasta`); not with the forwarding options | `--ro-path` and `--rw-path` may be nested: a path nested under another overrides it, so `--rw-path DIR --ro-path DIR/sub` mounts `DIR` read-write but @@ -298,10 +300,23 @@ env -i ... pasta [options] -- bash -- bwrap [options] -- = 0.4.0 (RHEL 8) # Binary masking is verified on RHEL 8.10 (bubblewrap-0.4.0-3.el8_10) and EL9 (0.6.3). -# Requires: pasta (package passt), and /dev/net/tun and unprivileged user -# namespaces on the host. There is no fallback to the host network. +# Optional: pasta (package passt), with /dev/net/tun and unprivileged user +# namespaces on the host, for the private network namespace. Without it +# (RHEL 8) the sandbox shares the host network, with a warning. # Guard against double-sourcing. [[ -n "${_BWRAP_SANDBOX_LIB_SOURCED:-}" ]] && return 0 @@ -67,13 +68,15 @@ EXTRA_RW_PATHS=() # Private network namespace (see "Network isolation"). Filled by the # launchers' parse_wrapper_args through net_add_publish, net_add_host_port, -# net_set_publish_addr and net_set_log_file; turned into pasta arguments by -# build_net_isolation. NET_PUBLISH and NET_HOST_PORTS hold validated specs +# net_set_publish_addr, net_set_log_file and net_set_local; turned into pasta +# arguments by build_net_isolation. NET_LOCAL=1 (--local-net) keeps the +# sandbox on the host network. NET_PUBLISH and NET_HOST_PORTS hold validated specs # "PORTS[:SANDBOXPORTS]" (PORTS is N or LOW-HIGH). NET_PUBLISH=() NET_PUBLISH_ADDR="" NET_HOST_PORTS=() NET_LOG_FILE="" +NET_LOCAL=0 PASTA_ARGS=() # N2C access tokens requested with --n2c-token (names, or "all"). @@ -2247,8 +2250,12 @@ build_etc_mounts() { # --publish PORT[:SANDBOXPORT] sandbox server -> host 127.H1.H2.S:PORT # --host-port PORT[:SANDBOXPORT] host 127.0.0.1:PORT -> sandbox # host.internal:SANDBOXPORT (127.0.42.1) -# There is no fallback to the host network: without a working pasta the -# launcher stops (exit 3). +# Without a working pasta (not installed, as on RHEL 8, no /dev/net/tun, or +# no unprivileged user namespaces), or with --local-net, the sandbox shares +# the host network as before: pasta is not used, the host's resolv.conf and +# hosts are mounted, and a warning is printed unless --local-net asked for +# it. --publish, --host-port and --net-log need pasta and then stop the +# launch (exit 3). # # Points that are easy to get wrong: # - --no-map-gw: pasta's default maps the gateway address to the host's @@ -2268,6 +2275,10 @@ build_etc_mounts() { _PASTA_BIN="" _PASTA_HAS_NO_PIDNS=0 _NET_CONFIGURED=0 +# 1 when the sandbox gets a private network namespace; otherwise +# _NET_SHARED_REASON says why it shares the host network. +_NET_PRIVATE=0 +_NET_SHARED_REASON="" _NET_PUBLISH_ADDR_EFFECTIVE="" _NET_HOST_INTERNAL_ADDR="127.0.42.1" _NET_HOST_INTERNAL_NAME="host.internal" @@ -2290,11 +2301,16 @@ _NET_HP_HI=() # Print the network options for a launcher's --help. net_print_help() { cat << 'NET_HELP_EOF' -Network: the sandbox has its own network namespace (pasta, package passt; -required, there is no fallback). Outbound connections work as on the host; -services on the host's loopback (127.0.0.1) are NOT reachable, and nothing the -agent listens on is reachable from the host, unless opened below. Ports must -be in 1024-65535; N or LOW-HIGH ranges are accepted. +Network: the sandbox has its own network namespace (pasta, package passt). +Outbound connections work as on the host; services on the host's loopback +(127.0.0.1) are NOT reachable, and nothing the agent listens on is reachable +from the host, unless opened below. Without pasta (for example on RHEL 8) the +sandbox shares the host network, with a warning. Ports must be in +1024-65535; N or LOW-HIGH ranges are accepted. + --local-net Share the host network (the behaviour without pasta): host + loopback services are reachable from the sandbox and its + listeners are reachable on the host. Cannot be combined + with the options below. --publish PORT[:SANDBOXPORT] Make a server inside the sandbox (port SANDBOXPORT, default PORT) reachable on the host at http://127.H1.H2.S:PORT/, an @@ -2335,21 +2351,48 @@ _net_baseline_args() { fi } +# net_set_local +# --local-net: share the host network, without pasta. +net_set_local() { + NET_LOCAL=1 +} + +# _net_forward_options +# Print the forward options given (--publish, --host-port, --net-log, as a +# space-separated list), nothing when none. +_net_forward_options() { + local -a _opts=() + [[ ${#NET_PUBLISH[@]} -eq 0 ]] || _opts+=(--publish) + [[ -z "${NET_PUBLISH_ADDR}" ]] || _opts+=(--publish-addr) + [[ ${#NET_HOST_PORTS[@]} -eq 0 ]] || _opts+=(--host-port) + [[ -z "${NET_LOG_FILE}" ]] || _opts+=(--net-log) + printf '%s' "${_opts[*]}" +} + # detect_net_capabilities -# Find pasta and check that it can create a namespace here (one probe run, -# about 50 ms). Exit 3 naming the missing piece otherwise. Under --dry-run -# nothing is run and a missing pasta is allowed, so the command line can -# still be inspected on a host without passt. +# Decide between a private network namespace and the host network. With +# --local-net, the host network. Otherwise find pasta and check that it can +# create a namespace here (one probe run, about 50 ms); when it cannot, warn +# and fall back to the host network. The forward options need pasta: with +# them, --local-net is an error (exit 1) and a missing pasta stops the launch +# (exit 3). Under --dry-run nothing is run: an installed pasta is assumed +# to work. detect_net_capabilities() { - local _reason="" _out="" + local _reason="" _out="" _fwd + _NET_PRIVATE=0 + _NET_SHARED_REASON="" + _fwd="$(_net_forward_options)" + if [[ "${NET_LOCAL}" -eq 1 ]]; then + if [[ -n "${_fwd}" ]]; then + echo "Error: --local-net cannot be combined with the forwarding options (${_fwd}): they need the private network namespace." >&2 + exit 1 + fi + _NET_SHARED_REASON="--local-net was given" + return 0 + fi _PASTA_BIN="$(command -v pasta 2> /dev/null || true)" if [[ -z "${_PASTA_BIN}" || ! -x "${_PASTA_BIN}" ]]; then - if [[ "${DRY_RUN}" -eq 1 ]]; then - echo "${0##*/}: dry-run: pasta not found on PATH (install package: passt); a real run would stop here." >&2 - _PASTA_BIN="pasta" - _net_baseline_args - return 0 - fi + _PASTA_BIN="" _reason="pasta not found on PATH (install package: passt)" else # --no-pidns (2026-09) is not in RHEL 9 / EPEL builds; without it @@ -2359,9 +2402,8 @@ detect_net_capabilities() { fi _net_baseline_args if [[ "${DRY_RUN}" -eq 1 ]]; then - return 0 - fi - if [[ ! -c /dev/net/tun ]]; then + : + elif [[ ! -c /dev/net/tun ]]; then _reason="/dev/net/tun is missing on the host" elif ! _out="$("${_PASTA_BIN}" "${PASTA_ARGS[@]}" -- "$(command -v true)" 2>&1)"; then _out="${_out%%$'\n'*}" @@ -2369,9 +2411,19 @@ detect_net_capabilities() { fi fi if [[ -n "${_reason}" ]]; then - printf 'Error: %s requires private networking via pasta: %s\n' "${0##*/}" "${_reason}" >&2 - exit 3 + if [[ -n "${_fwd}" ]]; then + printf 'Error: %s: the forwarding options (%s) need a private network namespace, which is not available: %s\n' "${0##*/}" "${_fwd}" "${_reason}" >&2 + exit 3 + fi + _PASTA_BIN="" + PASTA_ARGS=() + _NET_SHARED_REASON="${_reason}" + printf 'Warning: %s: no private network namespace: %s.\n' "${0##*/}" "${_reason}" >&2 + printf ' The sandbox shares the host network: services on the host loopback are reachable from it.\n' >&2 + printf ' Use --local-net to choose this explicitly and silence the warning.\n' >&2 + return 0 fi + _NET_PRIVATE=1 } # _net_parse_range OPTION TEXT @@ -2639,7 +2691,9 @@ _net_join() { # build_net_isolation # Finish PASTA_ARGS from the --publish / --host-port / --net-log options, # add the bwrap namespace options pasta needs, generate /etc/resolv.conf and -# /etc/hosts, and export BWRAP_NET_*. Call after +# /etc/hosts, and export BWRAP_NET_*. On the host network (see +# detect_net_capabilities) only mount the host's resolv.conf and hosts and +# set BWRAP_NET_MODE=host. Call after # detect_net_capabilities and build_etc_mounts, and before # build_sandbox_context (the context file describes the forwards). build_net_isolation() { @@ -2648,6 +2702,22 @@ build_net_isolation() { _NET_PUBLISH_DESC=() _NET_HOST_DESC=() _NET_PUBLISH_ADDR_EFFECTIVE="" + + # Host network (no pasta, or --local-net): the previous behaviour. + if [[ "${_NET_PRIVATE}" -ne 1 ]]; then + BWRAP_ARGS+=( + --ro-bind-try /etc/resolv.conf /etc/resolv.conf + --ro-bind-try /etc/hosts /etc/hosts + ) + SANDBOX_ENV+=( + "BWRAP_NET_MODE=host" + "BWRAP_NET_PUBLISH=" + "BWRAP_NET_PUBLISH_ADDR=" + "BWRAP_NET_HOST_PORTS=" + "BWRAP_NET_HOST_ADDR=" + ) + return 0 + fi [[ ${#PASTA_ARGS[@]} -gt 0 ]] || _net_baseline_args # pasta maps the caller to uid 0 in its user namespace: run the sandbox as @@ -2727,6 +2797,7 @@ build_net_isolation() { BWRAP_ARGS+=(--ro-bind-data "${_BWRAP_FD}" /etc/hosts) SANDBOX_ENV+=( + "BWRAP_NET_MODE=private" "BWRAP_NET_PUBLISH=$(_net_join ${NET_PUBLISH[@]+"${NET_PUBLISH[@]}"})" "BWRAP_NET_PUBLISH_ADDR=${_NET_PUBLISH_ADDR_EFFECTIVE}" "BWRAP_NET_HOST_PORTS=$(_net_join ${NET_HOST_PORTS[@]+"${NET_HOST_PORTS[@]}"})" @@ -2741,6 +2812,12 @@ build_net_isolation() { _net_print_report() { local _launcher="${0##*/}" _spec _line local _n_fwd=$((${#_NET_PUBLISH_DESC[@]} + ${#_NET_HOST_DESC[@]})) + if [[ "${_NET_PRIVATE}" -ne 1 ]]; then + if [[ "${NET_LOCAL}" -eq 1 && -t 2 ]]; then + printf '%s: network: shared with the host (--local-net)\n' "${_launcher}" >&2 + fi + return 0 + fi if [[ ${_n_fwd} -gt 0 || -t 2 ]]; then _line="${_launcher}: network: private namespace (pasta)" [[ -z "${_NET_PUBLISH_ADDR_EFFECTIVE}" ]] || _line+="; publish address ${_NET_PUBLISH_ADDR_EFFECTIVE}" @@ -2760,10 +2837,11 @@ _net_print_report() { } # _net_require_configured -# Exit 1 unless build_net_isolation has run: there is no launch without the -# private network namespace. +# Exit 1 unless detect_net_capabilities and build_net_isolation have run, so +# the network mode is always a decision, never an accident. _net_require_configured() { - if [[ "${_NET_CONFIGURED}" -ne 1 || -z "${_PASTA_BIN}" ]]; then + if [[ "${_NET_CONFIGURED}" -ne 1 ]] || + [[ "${_NET_PRIVATE}" -eq 1 && -z "${_PASTA_BIN}" ]]; then echo "Error: ${0##*/}: network isolation was not set up (detect_net_capabilities / build_net_isolation missing)." >&2 exit 1 fi @@ -3769,6 +3847,19 @@ _CTX_NET="" _bwrap_context_network() { local _nl=$'\n' _d _t="" _t+="## Network${_nl}${_nl}" + if [[ "${_NET_PRIVATE}" -ne 1 ]]; then + if [[ "${NET_LOCAL}" -eq 1 ]]; then + _t+="This sandbox shares the host's network (the user chose \`--local-net\`)." + else + _t+="This sandbox shares the host's network (no private network namespace is available on this host)." + fi + _t+=" Services listening on the host's loopback (\`127.0.0.1\`, \`localhost\`) are reachable from here," + _t+=" and they are not yours to use unless the user asks: they may belong to other users." + _t+=" Anything you listen on is reachable from the host, and on \`0.0.0.0\` from the network:" + _t+=" bind \`127.0.0.1\` unless told otherwise.${_nl}${_nl}" + _CTX_NET="${_t}" + return 0 + fi _t+="This sandbox has its own network namespace (pasta)." _t+=" Outbound connections work and leave from the host's address;" _t+=" DNS goes to \`${_NET_DNS_FORWARD_ADDR}\`, which is forwarded to the host's resolver." @@ -3799,7 +3890,7 @@ _bwrap_context_network() { done _t+="${_nl}Your own \`127.0.0.1\` is unaffected: a server you start on the same port is separate.${_nl}${_nl}" fi - _t+="\`BWRAP_NET_PUBLISH\`, \`BWRAP_NET_PUBLISH_ADDR\`, \`BWRAP_NET_HOST_PORTS\` and \`BWRAP_NET_HOST_ADDR\` hold the same information.${_nl}${_nl}" + _t+="\`BWRAP_NET_MODE\` is \`private\`; \`BWRAP_NET_PUBLISH\`, \`BWRAP_NET_PUBLISH_ADDR\`, \`BWRAP_NET_HOST_PORTS\` and \`BWRAP_NET_HOST_ADDR\` hold the same information.${_nl}${_nl}" _CTX_NET="${_t}" } @@ -3890,7 +3981,7 @@ _bwrap_context_render() { _t+=" and only if the source is mounted read-write below." _t+=" \"Read-only file system\" on a mount point is expected.${_nl}" _t+="- \`ln -s\` creates ordinary symlinks, in writable locations only.${_nl}" - if [[ "${_NET_CONFIGURED}" -eq 1 ]]; then + if [[ "${_NET_CONFIGURED}" -eq 1 && "${_NET_PRIVATE}" -eq 1 ]]; then _t+="- The network is private to this sandbox (see Network below)." else _t+="- The network is shared with the host." @@ -4323,7 +4414,7 @@ print_dry_run() { # Mirror the exact invocation used by launch_sandbox: # exec -i - # -- + # [ --] (private network only) # -c bwrap-fd-relay -- # -- <_TOOL_CMD> # Values of secret-looking variables (_is_secret_env_name) are printed as @@ -4349,9 +4440,9 @@ print_dry_run() { for _n in ${_BWRAP_FD_DATA_NUMS[@]+"${_BWRAP_FD_DATA_NUMS[@]}"}; do printf " _BWRAP_FD_DATA_%s='<%s, shown below>' \\\\\n" "${_n}" "${_BWRAP_FD_LABEL["${_n}"]}" done - printf " %s \\\\\n" "${_PASTA_BIN}" local _p=0 - while [[ ${_p} -lt ${#PASTA_ARGS[@]} ]]; do + [[ "${_NET_PRIVATE}" -ne 1 ]] || printf " %s \\\\\n" "${_PASTA_BIN}" + while [[ "${_NET_PRIVATE}" -eq 1 && ${_p} -lt ${#PASTA_ARGS[@]} ]]; do case "${PASTA_ARGS[${_p}]}" in -t | -u | -T | -U | --dns-forward | --search | --log-file) printf " %s %q \\\\\n" "${PASTA_ARGS[${_p}]}" "${PASTA_ARGS[$((_p + 1))]}" @@ -4363,7 +4454,7 @@ print_dry_run() { ;; esac done - printf " -- \\\\\n" + [[ "${_NET_PRIVATE}" -ne 1 ]] || printf " -- \\\\\n" printf " %s -c '<_BWRAP_FD_RELAY_SHIM from bwrap_sandbox_lib.sh>' bwrap-fd-relay \\\\\n" "${_BASH_BIN}" local _r=0 while [[ ${_r} -lt ${#_BWRAP_FD_RELAY[@]} ]]; do @@ -4430,7 +4521,8 @@ print_dry_run() { # "env" earlier on PATH from hijacking the launch. See resolve_env_bin. # # pasta starts the descriptor relay in the sandbox's network namespace (see -# "Network isolation"); the relay opens the descriptors that BWRAP_ARGS refers +# "Network isolation"; on the host network the relay is exec'd directly); the +# relay opens the descriptors that BWRAP_ARGS refers # to (see "File descriptor relay") and execs bwrap. launch_sandbox() { verify_n2c_state_not_exposed @@ -4441,7 +4533,10 @@ launch_sandbox() { _ENV_BIN="$(resolve_env_bin)" _BASH_BIN="$(resolve_bash_bin)" - local -a _data_env=() + local -a _data_env=() _net_prefix=() + if [[ "${_NET_PRIVATE}" -eq 1 ]]; then + _net_prefix=("${_PASTA_BIN}" "${PASTA_ARGS[@]}" --) + fi _bwrap_fd_data_numbers for _n in ${_BWRAP_FD_DATA_NUMS[@]+"${_BWRAP_FD_DATA_NUMS[@]}"}; do _data_env+=("_BWRAP_FD_DATA_${_n}=${_BWRAP_FD_DATA["${_n}"]}") @@ -4455,7 +4550,7 @@ launch_sandbox() { PATH="/usr/bin:/bin" \ ${SANDBOX_ENV[@]+"${SANDBOX_ENV[@]}"} \ ${_data_env[@]+"${_data_env[@]}"} \ - "${_PASTA_BIN}" "${PASTA_ARGS[@]}" -- \ + ${_net_prefix[@]+"${_net_prefix[@]}"} \ "${_BASH_BIN}" -c "${_BWRAP_FD_RELAY_SHIM}" bwrap-fd-relay \ ${_BWRAP_FD_RELAY[@]+"${_BWRAP_FD_RELAY[@]}"} -- \ "${_BWRAP_BIN}" "${BWRAP_ARGS[@]}" -- "${_TOOL_CMD[@]}" ${TOOL_ARGS[@]+"${TOOL_ARGS[@]}"} From 3471dcb6239ff1c0a3770762667530714b4c8311 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 30 Oct 2026 09:17:50 -0400 Subject: [PATCH 07/37] refactor(bwrap): rename --local-net to --share-net Use bwrap's own term for sharing the host network namespace. Assisted-by: opencode:claude-opus-5-5 --- README.md | 2 +- bin/bwclaude | 6 +++--- bin/bwcodex | 6 +++--- bin/bwcopilot | 6 +++--- bin/bwopencode | 6 +++--- bin/bwopencode2 | 6 +++--- docs/bw-wrappers.md | 4 ++-- lib/bwrap_sandbox_lib.sh | 42 ++++++++++++++++++++-------------------- 8 files changed, 39 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index db74281..2e917f8 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ Common wrapper options: | `--publish-addr ADDR` | Use `ADDR` (in `127.0.0.0/8`, e.g. `127.0.0.1` for OAuth) for `--publish` | | `--host-port PORT[:SANDBOXPORT]` | Make the host's `127.0.0.1:PORT` reachable in the sandbox at `host.internal` | | `--net-log FILE` | Log every network flow to `FILE` on the host | -| `--local-net` | Share the host network instead of a private namespace (the behaviour without `pasta`) | +| `--share-net` | Share the host network instead of a private namespace (the behaviour without `pasta`) | `bwclaude` also supports `--debug`. `bwopencode2` has no `--init-auth` (sign-in persists in its data directory), always runs OpenCode 2 with `--standalone` diff --git a/bin/bwclaude b/bin/bwclaude index 50c3643..93900f2 100755 --- a/bin/bwclaude +++ b/bin/bwclaude @@ -95,7 +95,7 @@ # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. # --net-log FILE Log every network flow to FILE on the host. -# --local-net Share the host network instead (the behaviour without +# --share-net Share the host network instead (the behaviour without # pasta); not with the three options above. # # Network: the sandbox has its own network namespace from pasta (package @@ -206,8 +206,8 @@ parse_wrapper_args() { net_add_host_port "$2" shift 2 ;; - --local-net) - net_set_local + --share-net) + net_set_share shift ;; --net-log) diff --git a/bin/bwcodex b/bin/bwcodex index 5604129..be8254b 100755 --- a/bin/bwcodex +++ b/bin/bwcodex @@ -112,7 +112,7 @@ # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. # --net-log FILE Log every network flow to FILE on the host. -# --local-net Share the host network instead (the behaviour without +# --share-net Share the host network instead (the behaviour without # pasta); not with the three options above. # # Network: the sandbox has its own network namespace from pasta (package @@ -228,8 +228,8 @@ parse_wrapper_args() { net_add_host_port "$2" shift 2 ;; - --local-net) - net_set_local + --share-net) + net_set_share shift ;; --net-log) diff --git a/bin/bwcopilot b/bin/bwcopilot index 5638557..4d74ce5 100755 --- a/bin/bwcopilot +++ b/bin/bwcopilot @@ -80,7 +80,7 @@ # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. # --net-log FILE Log every network flow to FILE on the host. -# --local-net Share the host network instead (the behaviour without +# --share-net Share the host network instead (the behaviour without # pasta); not with the three options above. # # Network: the sandbox has its own network namespace from pasta (package @@ -186,8 +186,8 @@ parse_wrapper_args() { net_add_host_port "$2" shift 2 ;; - --local-net) - net_set_local + --share-net) + net_set_share shift ;; --net-log) diff --git a/bin/bwopencode b/bin/bwopencode index f768534..72f06ef 100755 --- a/bin/bwopencode +++ b/bin/bwopencode @@ -92,7 +92,7 @@ # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. # --net-log FILE Log every network flow to FILE on the host. -# --local-net Share the host network instead (the behaviour without +# --share-net Share the host network instead (the behaviour without # pasta); not with the three options above. # # Network: the sandbox has its own network namespace from pasta (package @@ -198,8 +198,8 @@ parse_wrapper_args() { net_add_host_port "$2" shift 2 ;; - --local-net) - net_set_local + --share-net) + net_set_share shift ;; --net-log) diff --git a/bin/bwopencode2 b/bin/bwopencode2 index c2ef3d8..149d528 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -133,7 +133,7 @@ # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. # --net-log FILE Log every network flow to FILE on the host. -# --local-net Share the host network instead (the behaviour without +# --share-net Share the host network instead (the behaviour without # pasta); not with the three options above. # # Network: the sandbox has its own network namespace from pasta (package @@ -279,8 +279,8 @@ parse_wrapper_args() { net_add_host_port "$2" shift 2 ;; - --local-net) - net_set_local + --share-net) + net_set_share shift ;; --net-log) diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 10b7833..6d95fba 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -104,7 +104,7 @@ Every `bw*` wrapper accepts these options: | `--publish-addr ADDR` | Use `ADDR` (in `127.0.0.0/8`) for `--publish` instead of the derived address, for example `127.0.0.1` for OAuth redirects | | `--host-port PORT[:SANDBOXPORT]` | Make the host's `127.0.0.1:PORT` reachable in the sandbox at `host.internal:SANDBOXPORT`. May be repeated | | `--net-log FILE` | Log every network flow to `FILE` on the host (`pasta --debug`) | -| `--local-net` | Share the host network instead of a private namespace (the behaviour without `pasta`); not with the forwarding options | +| `--share-net` | Share the host network instead of a private namespace (the behaviour without `pasta`); not with the forwarding options | `--ro-path` and `--rw-path` may be nested: a path nested under another overrides it, so `--rw-path DIR --ro-path DIR/sub` mounts `DIR` read-write but @@ -307,7 +307,7 @@ env -i ... pasta [options] -- bash -- bwrap [options] -- sandbox # host.internal:SANDBOXPORT (127.0.42.1) # Without a working pasta (not installed, as on RHEL 8, no /dev/net/tun, or -# no unprivileged user namespaces), or with --local-net, the sandbox shares +# no unprivileged user namespaces), or with --share-net, the sandbox shares # the host network as before: pasta is not used, the host's resolv.conf and -# hosts are mounted, and a warning is printed unless --local-net asked for +# hosts are mounted, and a warning is printed unless --share-net asked for # it. --publish, --host-port and --net-log need pasta and then stop the # launch (exit 3). # @@ -2307,7 +2307,7 @@ Outbound connections work as on the host; services on the host's loopback from the host, unless opened below. Without pasta (for example on RHEL 8) the sandbox shares the host network, with a warning. Ports must be in 1024-65535; N or LOW-HIGH ranges are accepted. - --local-net Share the host network (the behaviour without pasta): host + --share-net Share the host network (the behaviour without pasta): host loopback services are reachable from the sandbox and its listeners are reachable on the host. Cannot be combined with the options below. @@ -2351,10 +2351,10 @@ _net_baseline_args() { fi } -# net_set_local -# --local-net: share the host network, without pasta. -net_set_local() { - NET_LOCAL=1 +# net_set_share +# --share-net: share the host network, without pasta. +net_set_share() { + NET_SHARE=1 } # _net_forward_options @@ -2371,10 +2371,10 @@ _net_forward_options() { # detect_net_capabilities # Decide between a private network namespace and the host network. With -# --local-net, the host network. Otherwise find pasta and check that it can +# --share-net, the host network. Otherwise find pasta and check that it can # create a namespace here (one probe run, about 50 ms); when it cannot, warn # and fall back to the host network. The forward options need pasta: with -# them, --local-net is an error (exit 1) and a missing pasta stops the launch +# them, --share-net is an error (exit 1) and a missing pasta stops the launch # (exit 3). Under --dry-run nothing is run: an installed pasta is assumed # to work. detect_net_capabilities() { @@ -2382,12 +2382,12 @@ detect_net_capabilities() { _NET_PRIVATE=0 _NET_SHARED_REASON="" _fwd="$(_net_forward_options)" - if [[ "${NET_LOCAL}" -eq 1 ]]; then + if [[ "${NET_SHARE}" -eq 1 ]]; then if [[ -n "${_fwd}" ]]; then - echo "Error: --local-net cannot be combined with the forwarding options (${_fwd}): they need the private network namespace." >&2 + echo "Error: --share-net cannot be combined with the forwarding options (${_fwd}): they need the private network namespace." >&2 exit 1 fi - _NET_SHARED_REASON="--local-net was given" + _NET_SHARED_REASON="--share-net was given" return 0 fi _PASTA_BIN="$(command -v pasta 2> /dev/null || true)" @@ -2420,7 +2420,7 @@ detect_net_capabilities() { _NET_SHARED_REASON="${_reason}" printf 'Warning: %s: no private network namespace: %s.\n' "${0##*/}" "${_reason}" >&2 printf ' The sandbox shares the host network: services on the host loopback are reachable from it.\n' >&2 - printf ' Use --local-net to choose this explicitly and silence the warning.\n' >&2 + printf ' Use --share-net to choose this explicitly and silence the warning.\n' >&2 return 0 fi _NET_PRIVATE=1 @@ -2703,7 +2703,7 @@ build_net_isolation() { _NET_HOST_DESC=() _NET_PUBLISH_ADDR_EFFECTIVE="" - # Host network (no pasta, or --local-net): the previous behaviour. + # Host network (no pasta, or --share-net): the previous behaviour. if [[ "${_NET_PRIVATE}" -ne 1 ]]; then BWRAP_ARGS+=( --ro-bind-try /etc/resolv.conf /etc/resolv.conf @@ -2813,8 +2813,8 @@ _net_print_report() { local _launcher="${0##*/}" _spec _line local _n_fwd=$((${#_NET_PUBLISH_DESC[@]} + ${#_NET_HOST_DESC[@]})) if [[ "${_NET_PRIVATE}" -ne 1 ]]; then - if [[ "${NET_LOCAL}" -eq 1 && -t 2 ]]; then - printf '%s: network: shared with the host (--local-net)\n' "${_launcher}" >&2 + if [[ "${NET_SHARE}" -eq 1 && -t 2 ]]; then + printf '%s: network: shared with the host (--share-net)\n' "${_launcher}" >&2 fi return 0 fi @@ -3848,8 +3848,8 @@ _bwrap_context_network() { local _nl=$'\n' _d _t="" _t+="## Network${_nl}${_nl}" if [[ "${_NET_PRIVATE}" -ne 1 ]]; then - if [[ "${NET_LOCAL}" -eq 1 ]]; then - _t+="This sandbox shares the host's network (the user chose \`--local-net\`)." + if [[ "${NET_SHARE}" -eq 1 ]]; then + _t+="This sandbox shares the host's network (the user chose \`--share-net\`)." else _t+="This sandbox shares the host's network (no private network namespace is available on this host)." fi From 8d9222d81e78da30516cff4569ae78cd9e3288db Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 30 Oct 2026 10:50:16 -0400 Subject: [PATCH 08/37] feat(bwrap): stop instead of sharing the host network Without a usable private network namespace the launchers now exit 3 naming the reason, instead of warning and sharing the host network. Only --share-net shares it. BWRAP_NET_BACKEND (pasta or none) replaces BWRAP_NET_MODE, ready for a second backend. Assisted-by: opencode:claude-opus-5-5 --- bin/bwclaude | 8 +-- bin/bwcodex | 8 +-- bin/bwcopilot | 8 +-- bin/bwopencode | 8 +-- bin/bwopencode2 | 8 +-- lib/bwrap_sandbox_lib.sh | 136 ++++++++++++++++++++------------------- 6 files changed, 91 insertions(+), 85 deletions(-) diff --git a/bin/bwclaude b/bin/bwclaude index 93900f2..75fbc97 100755 --- a/bin/bwclaude +++ b/bin/bwclaude @@ -95,14 +95,14 @@ # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. # --net-log FILE Log every network flow to FILE on the host. -# --share-net Share the host network instead (the behaviour without -# pasta); not with the three options above. +# --share-net Share the host network instead (the behaviour before +# private namespaces); not with the options above. # # Network: the sandbox has its own network namespace from pasta (package # passt). Outbound connections work; services on the host's loopback are # unreachable and the sandbox's listeners are invisible to the host, except -# for the ports opened with --publish and --host-port. Without a working -# pasta (e.g. RHEL 8) the sandbox shares the host network, with a warning. +# for the ports opened with --publish and --host-port. Without a usable +# private namespace the launch stops (exit 3) unless --share-net is given. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are diff --git a/bin/bwcodex b/bin/bwcodex index be8254b..501655a 100755 --- a/bin/bwcodex +++ b/bin/bwcodex @@ -112,14 +112,14 @@ # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. # --net-log FILE Log every network flow to FILE on the host. -# --share-net Share the host network instead (the behaviour without -# pasta); not with the three options above. +# --share-net Share the host network instead (the behaviour before +# private namespaces); not with the options above. # # Network: the sandbox has its own network namespace from pasta (package # passt). Outbound connections work; services on the host's loopback are # unreachable and the sandbox's listeners are invisible to the host, except -# for the ports opened with --publish and --host-port. Without a working -# pasta (e.g. RHEL 8) the sandbox shares the host network, with a warning. +# for the ports opened with --publish and --host-port. Without a usable +# private namespace the launch stops (exit 3) unless --share-net is given. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are diff --git a/bin/bwcopilot b/bin/bwcopilot index 4d74ce5..047b168 100755 --- a/bin/bwcopilot +++ b/bin/bwcopilot @@ -80,14 +80,14 @@ # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. # --net-log FILE Log every network flow to FILE on the host. -# --share-net Share the host network instead (the behaviour without -# pasta); not with the three options above. +# --share-net Share the host network instead (the behaviour before +# private namespaces); not with the options above. # # Network: the sandbox has its own network namespace from pasta (package # passt). Outbound connections work; services on the host's loopback are # unreachable and the sandbox's listeners are invisible to the host, except -# for the ports opened with --publish and --host-port. Without a working -# pasta (e.g. RHEL 8) the sandbox shares the host network, with a warning. +# for the ports opened with --publish and --host-port. Without a usable +# private namespace the launch stops (exit 3) unless --share-net is given. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are diff --git a/bin/bwopencode b/bin/bwopencode index 72f06ef..78ad1e8 100755 --- a/bin/bwopencode +++ b/bin/bwopencode @@ -92,14 +92,14 @@ # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. # --net-log FILE Log every network flow to FILE on the host. -# --share-net Share the host network instead (the behaviour without -# pasta); not with the three options above. +# --share-net Share the host network instead (the behaviour before +# private namespaces); not with the options above. # # Network: the sandbox has its own network namespace from pasta (package # passt). Outbound connections work; services on the host's loopback are # unreachable and the sandbox's listeners are invisible to the host, except -# for the ports opened with --publish and --host-port. Without a working -# pasta (e.g. RHEL 8) the sandbox shares the host network, with a warning. +# for the ports opened with --publish and --host-port. Without a usable +# private namespace the launch stops (exit 3) unless --share-net is given. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are diff --git a/bin/bwopencode2 b/bin/bwopencode2 index 149d528..4e5e655 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -133,14 +133,14 @@ # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. # --net-log FILE Log every network flow to FILE on the host. -# --share-net Share the host network instead (the behaviour without -# pasta); not with the three options above. +# --share-net Share the host network instead (the behaviour before +# private namespaces); not with the options above. # # Network: the sandbox has its own network namespace from pasta (package # passt). Outbound connections work; services on the host's loopback are # unreachable and the sandbox's listeners are invisible to the host, except -# for the ports opened with --publish and --host-port. Without a working -# pasta (e.g. RHEL 8) the sandbox shares the host network, with a warning. +# for the ports opened with --publish and --host-port. Without a usable +# private namespace the launch stops (exit 3) unless --share-net is given. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index 714d0f7..612f1da 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -2250,12 +2250,11 @@ build_etc_mounts() { # --publish PORT[:SANDBOXPORT] sandbox server -> host 127.H1.H2.S:PORT # --host-port PORT[:SANDBOXPORT] host 127.0.0.1:PORT -> sandbox # host.internal:SANDBOXPORT (127.0.42.1) -# Without a working pasta (not installed, as on RHEL 8, no /dev/net/tun, or -# no unprivileged user namespaces), or with --share-net, the sandbox shares -# the host network as before: pasta is not used, the host's resolv.conf and -# hosts are mounted, and a warning is printed unless --share-net asked for -# it. --publish, --host-port and --net-log need pasta and then stop the -# launch (exit 3). +# Without a usable backend (pasta not installed, as on RHEL 8, no +# /dev/net/tun, or no unprivileged user namespaces) the launch stops (exit 3). +# Only --share-net makes the sandbox share the host network as before: no +# backend, the host's resolv.conf and hosts mounted, and no forwarding +# options (exit 1 with them). # # Points that are easy to get wrong: # - --no-map-gw: pasta's default maps the gateway address to the host's @@ -2275,9 +2274,10 @@ build_etc_mounts() { _PASTA_BIN="" _PASTA_HAS_NO_PIDNS=0 _NET_CONFIGURED=0 -# 1 when the sandbox gets a private network namespace; otherwise -# _NET_SHARED_REASON says why it shares the host network. +# _NET_BACKEND is the backend that provides the private network namespace, +# or none with --share-net; _NET_PRIVATE is 1 unless it is none. _NET_PRIVATE=0 +_NET_BACKEND=none _NET_SHARED_REASON="" _NET_PUBLISH_ADDR_EFFECTIVE="" _NET_HOST_INTERNAL_ADDR="127.0.42.1" @@ -2304,13 +2304,13 @@ net_print_help() { Network: the sandbox has its own network namespace (pasta, package passt). Outbound connections work as on the host; services on the host's loopback (127.0.0.1) are NOT reachable, and nothing the agent listens on is reachable -from the host, unless opened below. Without pasta (for example on RHEL 8) the -sandbox shares the host network, with a warning. Ports must be in -1024-65535; N or LOW-HIGH ranges are accepted. - --share-net Share the host network (the behaviour without pasta): host - loopback services are reachable from the sandbox and its - listeners are reachable on the host. Cannot be combined - with the options below. +from the host, unless opened below. If no private network namespace can be +set up, the launch stops (exit 3) unless --share-net is given. Ports must be +in 1024-65535; N or LOW-HIGH ranges are accepted. + --share-net Share the host network instead (the behaviour before + private namespaces): host loopback services are reachable + from the sandbox and its listeners are reachable on the + host. Cannot be combined with the options below. --publish PORT[:SANDBOXPORT] Make a server inside the sandbox (port SANDBOXPORT, default PORT) reachable on the host at http://127.H1.H2.S:PORT/, an @@ -2352,7 +2352,7 @@ _net_baseline_args() { } # net_set_share -# --share-net: share the host network, without pasta. +# --share-net: share the host network, without a private namespace. net_set_share() { NET_SHARE=1 } @@ -2370,16 +2370,14 @@ _net_forward_options() { } # detect_net_capabilities -# Decide between a private network namespace and the host network. With -# --share-net, the host network. Otherwise find pasta and check that it can -# create a namespace here (one probe run, about 50 ms); when it cannot, warn -# and fall back to the host network. The forward options need pasta: with -# them, --share-net is an error (exit 1) and a missing pasta stops the launch -# (exit 3). Under --dry-run nothing is run: an installed pasta is assumed -# to work. +# Choose the network for the sandbox and set _NET_BACKEND: none with +# --share-net (the forwarding options are then an error, exit 1); otherwise +# the first usable backend. With none usable, stop (exit 3): the sandbox +# never shares the host network unless --share-net asks for it. detect_net_capabilities() { - local _reason="" _out="" _fwd + local _fwd _pasta_reason="" _NET_PRIVATE=0 + _NET_BACKEND=none _NET_SHARED_REASON="" _fwd="$(_net_forward_options)" if [[ "${NET_SHARE}" -eq 1 ]]; then @@ -2390,40 +2388,52 @@ detect_net_capabilities() { _NET_SHARED_REASON="--share-net was given" return 0 fi + if _net_try_pasta; then + _NET_BACKEND=pasta + _NET_PRIVATE=1 + return 0 + fi + _pasta_reason="${_NET_TRY_REASON}" + _PASTA_BIN="" + PASTA_ARGS=() + printf 'Error: %s: no private network namespace is available, and the sandbox does not share\n' "${0##*/}" >&2 + printf ' the host network unless asked to.\n' >&2 + printf ' pasta: %s\n' "${_pasta_reason}" >&2 + printf ' Install passt (RHEL 9 and later), or run with --share-net to share the host\n' >&2 + printf ' network (services on the host loopback are then reachable from the sandbox).\n' >&2 + exit 3 +} + +# _net_try_pasta +# Return 0 when pasta can be used (set _PASTA_BIN and PASTA_ARGS), else 1 +# with the reason in _NET_TRY_REASON. Under --dry-run an installed pasta is +# assumed to work. +_NET_TRY_REASON="" +_net_try_pasta() { + local _out + _NET_TRY_REASON="" _PASTA_BIN="$(command -v pasta 2> /dev/null || true)" if [[ -z "${_PASTA_BIN}" || ! -x "${_PASTA_BIN}" ]]; then - _PASTA_BIN="" - _reason="pasta not found on PATH (install package: passt)" - else - # --no-pidns (2026-09) is not in RHEL 9 / EPEL builds; without it - # pasta adds a PID namespace, inside which bwrap nests its own. - if "${_PASTA_BIN}" --help 2>&1 | grep -q -e '--no-pidns'; then - _PASTA_HAS_NO_PIDNS=1 - fi - _net_baseline_args - if [[ "${DRY_RUN}" -eq 1 ]]; then - : - elif [[ ! -c /dev/net/tun ]]; then - _reason="/dev/net/tun is missing on the host" - elif ! _out="$("${_PASTA_BIN}" "${PASTA_ARGS[@]}" -- "$(command -v true)" 2>&1)"; then - _out="${_out%%$'\n'*}" - _reason="a test run of pasta failed${_out:+: ${_out}} (are unprivileged user namespaces disabled?)" - fi + _NET_TRY_REASON="not found on PATH (package passt)" + return 1 fi - if [[ -n "${_reason}" ]]; then - if [[ -n "${_fwd}" ]]; then - printf 'Error: %s: the forwarding options (%s) need a private network namespace, which is not available: %s\n' "${0##*/}" "${_fwd}" "${_reason}" >&2 - exit 3 - fi - _PASTA_BIN="" - PASTA_ARGS=() - _NET_SHARED_REASON="${_reason}" - printf 'Warning: %s: no private network namespace: %s.\n' "${0##*/}" "${_reason}" >&2 - printf ' The sandbox shares the host network: services on the host loopback are reachable from it.\n' >&2 - printf ' Use --share-net to choose this explicitly and silence the warning.\n' >&2 - return 0 + # --no-pidns (2026-09) is not in RHEL 9 / EPEL builds; without it pasta + # adds a PID namespace, inside which bwrap nests its own. + if "${_PASTA_BIN}" --help 2>&1 | grep -q -e '--no-pidns'; then + _PASTA_HAS_NO_PIDNS=1 fi - _NET_PRIVATE=1 + _net_baseline_args + [[ "${DRY_RUN}" -eq 0 ]] || return 0 + if [[ ! -c /dev/net/tun ]]; then + _NET_TRY_REASON="/dev/net/tun is missing on the host" + return 1 + fi + if ! _out="$("${_PASTA_BIN}" "${PASTA_ARGS[@]}" -- "$(command -v true)" 2>&1)"; then + _out="${_out%%$'\n'*}" + _NET_TRY_REASON="a test run failed${_out:+: ${_out}} (are unprivileged user namespaces disabled?)" + return 1 + fi + return 0 } # _net_parse_range OPTION TEXT @@ -2693,7 +2703,7 @@ _net_join() { # add the bwrap namespace options pasta needs, generate /etc/resolv.conf and # /etc/hosts, and export BWRAP_NET_*. On the host network (see # detect_net_capabilities) only mount the host's resolv.conf and hosts and -# set BWRAP_NET_MODE=host. Call after +# set BWRAP_NET_BACKEND=none. Call after # detect_net_capabilities and build_etc_mounts, and before # build_sandbox_context (the context file describes the forwards). build_net_isolation() { @@ -2710,7 +2720,7 @@ build_net_isolation() { --ro-bind-try /etc/hosts /etc/hosts ) SANDBOX_ENV+=( - "BWRAP_NET_MODE=host" + "BWRAP_NET_BACKEND=none" "BWRAP_NET_PUBLISH=" "BWRAP_NET_PUBLISH_ADDR=" "BWRAP_NET_HOST_PORTS=" @@ -2797,7 +2807,7 @@ build_net_isolation() { BWRAP_ARGS+=(--ro-bind-data "${_BWRAP_FD}" /etc/hosts) SANDBOX_ENV+=( - "BWRAP_NET_MODE=private" + "BWRAP_NET_BACKEND=${_NET_BACKEND}" "BWRAP_NET_PUBLISH=$(_net_join ${NET_PUBLISH[@]+"${NET_PUBLISH[@]}"})" "BWRAP_NET_PUBLISH_ADDR=${_NET_PUBLISH_ADDR_EFFECTIVE}" "BWRAP_NET_HOST_PORTS=$(_net_join ${NET_HOST_PORTS[@]+"${NET_HOST_PORTS[@]}"})" @@ -2819,7 +2829,7 @@ _net_print_report() { return 0 fi if [[ ${_n_fwd} -gt 0 || -t 2 ]]; then - _line="${_launcher}: network: private namespace (pasta)" + _line="${_launcher}: network: private namespace (${_NET_BACKEND})" [[ -z "${_NET_PUBLISH_ADDR_EFFECTIVE}" ]] || _line+="; publish address ${_NET_PUBLISH_ADDR_EFFECTIVE}" printf '%s\n' "${_line}" >&2 fi @@ -3848,11 +3858,7 @@ _bwrap_context_network() { local _nl=$'\n' _d _t="" _t+="## Network${_nl}${_nl}" if [[ "${_NET_PRIVATE}" -ne 1 ]]; then - if [[ "${NET_SHARE}" -eq 1 ]]; then - _t+="This sandbox shares the host's network (the user chose \`--share-net\`)." - else - _t+="This sandbox shares the host's network (no private network namespace is available on this host)." - fi + _t+="This sandbox shares the host's network (the user chose \`--share-net\`)." _t+=" Services listening on the host's loopback (\`127.0.0.1\`, \`localhost\`) are reachable from here," _t+=" and they are not yours to use unless the user asks: they may belong to other users." _t+=" Anything you listen on is reachable from the host, and on \`0.0.0.0\` from the network:" @@ -3860,7 +3866,7 @@ _bwrap_context_network() { _CTX_NET="${_t}" return 0 fi - _t+="This sandbox has its own network namespace (pasta)." + _t+="This sandbox has its own network namespace (${_NET_BACKEND})." _t+=" Outbound connections work and leave from the host's address;" _t+=" DNS goes to \`${_NET_DNS_FORWARD_ADDR}\`, which is forwarded to the host's resolver." _t+=" Services listening on the host's loopback (\`127.0.0.1\`, \`localhost\`) are not reachable from here," @@ -3890,7 +3896,7 @@ _bwrap_context_network() { done _t+="${_nl}Your own \`127.0.0.1\` is unaffected: a server you start on the same port is separate.${_nl}${_nl}" fi - _t+="\`BWRAP_NET_MODE\` is \`private\`; \`BWRAP_NET_PUBLISH\`, \`BWRAP_NET_PUBLISH_ADDR\`, \`BWRAP_NET_HOST_PORTS\` and \`BWRAP_NET_HOST_ADDR\` hold the same information.${_nl}${_nl}" + _t+="\`BWRAP_NET_BACKEND\` is \`${_NET_BACKEND}\`; \`BWRAP_NET_PUBLISH\`, \`BWRAP_NET_PUBLISH_ADDR\`, \`BWRAP_NET_HOST_PORTS\` and \`BWRAP_NET_HOST_ADDR\` hold the same information.${_nl}${_nl}" _CTX_NET="${_t}" } From 75fd8472194f1a9f41a72ed4f26579ed4100fcb1 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 30 Oct 2026 10:59:56 -0400 Subject: [PATCH 09/37] feat(bwrap): add slirp4netns as a second network backend RHEL 8 has slirp4netns but no passt. When pasta is not usable, the launchers now use slirp4netns: util-linux unshare gives the launcher a user namespace that owns a new network namespace, bwrap runs the sandbox in it as the real user, and a background helper attaches slirp4netns (--disable-host-loopback, 192.168.254.0/24, built-in DNS) before releasing the sandbox through --block-fd. The launcher still execs in the foreground; slirp4netns exits with bwrap (--sync-fd). On a setup failure the helper kills the blocked sandbox before the tool starts. unshare, not bwrap --unshare-net, makes the namespaces: bwrap's devpts setup nests the sandbox in a second user namespace, from which slirp4netns would have no rights over the network namespace. --publish works through the slirp4netns API socket; --host-port and --net-log need pasta. --net-backend restricts the choice to one backend. Assisted-by: opencode:claude-opus-5-5 --- bin/bwclaude | 19 +- bin/bwcodex | 19 +- bin/bwcopilot | 19 +- bin/bwopencode | 19 +- bin/bwopencode2 | 19 +- lib/bwrap_sandbox_lib.sh | 506 +++++++++++++++++++++++++++++++++++---- 6 files changed, 538 insertions(+), 63 deletions(-) diff --git a/bin/bwclaude b/bin/bwclaude index 75fbc97..fd079b6 100755 --- a/bin/bwclaude +++ b/bin/bwclaude @@ -97,12 +97,15 @@ # --net-log FILE Log every network flow to FILE on the host. # --share-net Share the host network instead (the behaviour before # private namespaces); not with the options above. +# --net-backend pasta|slirp4netns +# Use only this backend for the private namespace. # # Network: the sandbox has its own network namespace from pasta (package -# passt). Outbound connections work; services on the host's loopback are -# unreachable and the sandbox's listeners are invisible to the host, except -# for the ports opened with --publish and --host-port. Without a usable -# private namespace the launch stops (exit 3) unless --share-net is given. +# passt), or else slirp4netns (--host-port and --net-log need pasta). +# Outbound connections work; services on the host's loopback are unreachable +# and the sandbox's listeners are invisible to the host, except for the ports +# opened with --publish and --host-port. Without a usable private namespace +# the launch stops (exit 3) unless --share-net is given. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are @@ -210,6 +213,14 @@ parse_wrapper_args() { net_set_share shift ;; + --net-backend) + if [[ $# -lt 2 ]]; then + echo "Error: --net-backend requires pasta or slirp4netns." >&2 + exit 1 + fi + net_set_backend "$2" + shift 2 + ;; --net-log) if [[ $# -lt 2 ]]; then echo "Error: --net-log requires a file name." >&2 diff --git a/bin/bwcodex b/bin/bwcodex index 501655a..31cb805 100755 --- a/bin/bwcodex +++ b/bin/bwcodex @@ -114,12 +114,15 @@ # --net-log FILE Log every network flow to FILE on the host. # --share-net Share the host network instead (the behaviour before # private namespaces); not with the options above. +# --net-backend pasta|slirp4netns +# Use only this backend for the private namespace. # # Network: the sandbox has its own network namespace from pasta (package -# passt). Outbound connections work; services on the host's loopback are -# unreachable and the sandbox's listeners are invisible to the host, except -# for the ports opened with --publish and --host-port. Without a usable -# private namespace the launch stops (exit 3) unless --share-net is given. +# passt), or else slirp4netns (--host-port and --net-log need pasta). +# Outbound connections work; services on the host's loopback are unreachable +# and the sandbox's listeners are invisible to the host, except for the ports +# opened with --publish and --host-port. Without a usable private namespace +# the launch stops (exit 3) unless --share-net is given. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are @@ -232,6 +235,14 @@ parse_wrapper_args() { net_set_share shift ;; + --net-backend) + if [[ $# -lt 2 ]]; then + echo "Error: --net-backend requires pasta or slirp4netns." >&2 + exit 1 + fi + net_set_backend "$2" + shift 2 + ;; --net-log) if [[ $# -lt 2 ]]; then echo "Error: --net-log requires a file name." >&2 diff --git a/bin/bwcopilot b/bin/bwcopilot index 047b168..2fc5554 100755 --- a/bin/bwcopilot +++ b/bin/bwcopilot @@ -82,12 +82,15 @@ # --net-log FILE Log every network flow to FILE on the host. # --share-net Share the host network instead (the behaviour before # private namespaces); not with the options above. +# --net-backend pasta|slirp4netns +# Use only this backend for the private namespace. # # Network: the sandbox has its own network namespace from pasta (package -# passt). Outbound connections work; services on the host's loopback are -# unreachable and the sandbox's listeners are invisible to the host, except -# for the ports opened with --publish and --host-port. Without a usable -# private namespace the launch stops (exit 3) unless --share-net is given. +# passt), or else slirp4netns (--host-port and --net-log need pasta). +# Outbound connections work; services on the host's loopback are unreachable +# and the sandbox's listeners are invisible to the host, except for the ports +# opened with --publish and --host-port. Without a usable private namespace +# the launch stops (exit 3) unless --share-net is given. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are @@ -190,6 +193,14 @@ parse_wrapper_args() { net_set_share shift ;; + --net-backend) + if [[ $# -lt 2 ]]; then + echo "Error: --net-backend requires pasta or slirp4netns." >&2 + exit 1 + fi + net_set_backend "$2" + shift 2 + ;; --net-log) if [[ $# -lt 2 ]]; then echo "Error: --net-log requires a file name." >&2 diff --git a/bin/bwopencode b/bin/bwopencode index 78ad1e8..b5dfe61 100755 --- a/bin/bwopencode +++ b/bin/bwopencode @@ -94,12 +94,15 @@ # --net-log FILE Log every network flow to FILE on the host. # --share-net Share the host network instead (the behaviour before # private namespaces); not with the options above. +# --net-backend pasta|slirp4netns +# Use only this backend for the private namespace. # # Network: the sandbox has its own network namespace from pasta (package -# passt). Outbound connections work; services on the host's loopback are -# unreachable and the sandbox's listeners are invisible to the host, except -# for the ports opened with --publish and --host-port. Without a usable -# private namespace the launch stops (exit 3) unless --share-net is given. +# passt), or else slirp4netns (--host-port and --net-log need pasta). +# Outbound connections work; services on the host's loopback are unreachable +# and the sandbox's listeners are invisible to the host, except for the ports +# opened with --publish and --host-port. Without a usable private namespace +# the launch stops (exit 3) unless --share-net is given. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are @@ -202,6 +205,14 @@ parse_wrapper_args() { net_set_share shift ;; + --net-backend) + if [[ $# -lt 2 ]]; then + echo "Error: --net-backend requires pasta or slirp4netns." >&2 + exit 1 + fi + net_set_backend "$2" + shift 2 + ;; --net-log) if [[ $# -lt 2 ]]; then echo "Error: --net-log requires a file name." >&2 diff --git a/bin/bwopencode2 b/bin/bwopencode2 index 4e5e655..4134332 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -135,12 +135,15 @@ # --net-log FILE Log every network flow to FILE on the host. # --share-net Share the host network instead (the behaviour before # private namespaces); not with the options above. +# --net-backend pasta|slirp4netns +# Use only this backend for the private namespace. # # Network: the sandbox has its own network namespace from pasta (package -# passt). Outbound connections work; services on the host's loopback are -# unreachable and the sandbox's listeners are invisible to the host, except -# for the ports opened with --publish and --host-port. Without a usable -# private namespace the launch stops (exit 3) unless --share-net is given. +# passt), or else slirp4netns (--host-port and --net-log need pasta). +# Outbound connections work; services on the host's loopback are unreachable +# and the sandbox's listeners are invisible to the host, except for the ports +# opened with --publish and --host-port. Without a usable private namespace +# the launch stops (exit 3) unless --share-net is given. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are @@ -283,6 +286,14 @@ parse_wrapper_args() { net_set_share shift ;; + --net-backend) + if [[ $# -lt 2 ]]; then + echo "Error: --net-backend requires pasta or slirp4netns." >&2 + exit 1 + fi + net_set_backend "$2" + shift 2 + ;; --net-log) if [[ $# -lt 2 ]]; then echo "Error: --net-log requires a file name." >&2 diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index 612f1da..4f550a0 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -70,13 +70,15 @@ EXTRA_RW_PATHS=() # launchers' parse_wrapper_args through net_add_publish, net_add_host_port, # net_set_publish_addr, net_set_log_file and net_set_share; turned into pasta # arguments by build_net_isolation. NET_SHARE=1 (--share-net) keeps the -# sandbox on the host network. NET_PUBLISH and NET_HOST_PORTS hold validated specs +# sandbox on the host network; NET_BACKEND_FORCE (--net-backend) restricts the +# choice to one backend. NET_PUBLISH and NET_HOST_PORTS hold validated specs # "PORTS[:SANDBOXPORTS]" (PORTS is N or LOW-HIGH). NET_PUBLISH=() NET_PUBLISH_ADDR="" NET_HOST_PORTS=() NET_LOG_FILE="" NET_SHARE=0 +NET_BACKEND_FORCE="" PASTA_ARGS=() # N2C access tokens requested with --n2c-token (names, or "all"). @@ -2235,7 +2237,8 @@ build_etc_mounts() { # ═══════════════════════════════════════════════════════════════════ # Network isolation # ═══════════════════════════════════════════════════════════════════ -# The sandbox gets its own network namespace from pasta (package passt): +# The sandbox gets its own network namespace from the first usable backend: +# pasta (package passt), else slirp4netns (see "slirp4netns backend" below). # # env -i ... pasta [PASTA_ARGS] -- bash -- bwrap [BWRAP_ARGS] -- tool # @@ -2250,8 +2253,9 @@ build_etc_mounts() { # --publish PORT[:SANDBOXPORT] sandbox server -> host 127.H1.H2.S:PORT # --host-port PORT[:SANDBOXPORT] host 127.0.0.1:PORT -> sandbox # host.internal:SANDBOXPORT (127.0.42.1) -# Without a usable backend (pasta not installed, as on RHEL 8, no +# Without a usable backend (neither pasta nor slirp4netns installed, no # /dev/net/tun, or no unprivileged user namespaces) the launch stops (exit 3). +# --net-backend NAME restricts the choice to one backend (for testing). # Only --share-net makes the sandbox share the host network as before: no # backend, the host's resolv.conf and hosts mounted, and no forwarding # options (exit 1 with them). @@ -2283,6 +2287,8 @@ _NET_PUBLISH_ADDR_EFFECTIVE="" _NET_HOST_INTERNAL_ADDR="127.0.42.1" _NET_HOST_INTERNAL_NAME="host.internal" _NET_DNS_FORWARD_ADDR="169.254.1.1" +# The sandbox's nameserver address with the chosen backend. +_NET_DNS_EFFECTIVE="" # Host loopback ports that --host-port refuses: services that trust loopback # clients completely. CUPS, Docker (plain and TLS), X11, the Tailscale local # API, and the OpenCode 2 background service (runs tools outside any sandbox). @@ -2301,7 +2307,8 @@ _NET_HP_HI=() # Print the network options for a launcher's --help. net_print_help() { cat << 'NET_HELP_EOF' -Network: the sandbox has its own network namespace (pasta, package passt). +Network: the sandbox has its own network namespace (pasta, package passt, +or else slirp4netns). Outbound connections work as on the host; services on the host's loopback (127.0.0.1) are NOT reachable, and nothing the agent listens on is reachable from the host, unless opened below. If no private network namespace can be @@ -2311,6 +2318,10 @@ in 1024-65535; N or LOW-HIGH ranges are accepted. private namespaces): host loopback services are reachable from the sandbox and its listeners are reachable on the host. Cannot be combined with the options below. + --net-backend pasta|slirp4netns + Use only this backend for the private namespace (normally + pasta, else slirp4netns). slirp4netns supports --publish + but not --host-port or --net-log. --publish PORT[:SANDBOXPORT] Make a server inside the sandbox (port SANDBOXPORT, default PORT) reachable on the host at http://127.H1.H2.S:PORT/, an @@ -2351,6 +2362,18 @@ _net_baseline_args() { fi } +# net_set_backend NAME +# --net-backend: use only backend NAME (pasta or slirp4netns). +net_set_backend() { + case "$1" in + pasta | slirp4netns) NET_BACKEND_FORCE="$1" ;; + *) + echo "Error: --net-backend: '$1' is not a backend (pasta or slirp4netns)." >&2 + exit 1 + ;; + esac +} + # net_set_share # --share-net: share the host network, without a private namespace. net_set_share() { @@ -2375,7 +2398,7 @@ _net_forward_options() { # the first usable backend. With none usable, stop (exit 3): the sandbox # never shares the host network unless --share-net asks for it. detect_net_capabilities() { - local _fwd _pasta_reason="" + local _fwd _want="${NET_BACKEND_FORCE}" _pasta_reason _slirp_reason _NET_PRIVATE=0 _NET_BACKEND=none _NET_SHARED_REASON="" @@ -2385,22 +2408,45 @@ detect_net_capabilities() { echo "Error: --share-net cannot be combined with the forwarding options (${_fwd}): they need the private network namespace." >&2 exit 1 fi + if [[ -n "${_want}" ]]; then + echo "Error: --share-net cannot be combined with --net-backend." >&2 + exit 1 + fi _NET_SHARED_REASON="--share-net was given" return 0 fi - if _net_try_pasta; then - _NET_BACKEND=pasta - _NET_PRIVATE=1 - return 0 + + _pasta_reason="not tried (--net-backend ${_want})" + if [[ -z "${_want}" || "${_want}" == pasta ]]; then + if _net_try_pasta; then + _NET_BACKEND=pasta + _NET_PRIVATE=1 + return 0 + fi + _pasta_reason="${_NET_TRY_REASON}" + _PASTA_BIN="" + PASTA_ARGS=() + fi + + _slirp_reason="not tried (--net-backend ${_want})" + if [[ -z "${_want}" || "${_want}" == slirp4netns ]]; then + if [[ ${#NET_HOST_PORTS[@]} -gt 0 || -n "${NET_LOG_FILE}" ]]; then + _slirp_reason="does not support --host-port or --net-log (pasta only)" + elif _net_try_slirp; then + _NET_BACKEND=slirp4netns + _NET_PRIVATE=1 + return 0 + else + _slirp_reason="${_NET_TRY_REASON}" + fi fi - _pasta_reason="${_NET_TRY_REASON}" - _PASTA_BIN="" - PASTA_ARGS=() + printf 'Error: %s: no private network namespace is available, and the sandbox does not share\n' "${0##*/}" >&2 printf ' the host network unless asked to.\n' >&2 - printf ' pasta: %s\n' "${_pasta_reason}" >&2 - printf ' Install passt (RHEL 9 and later), or run with --share-net to share the host\n' >&2 - printf ' network (services on the host loopback are then reachable from the sandbox).\n' >&2 + printf ' pasta: %s\n' "${_pasta_reason}" >&2 + printf ' slirp4netns: %s\n' "${_slirp_reason}" >&2 + printf ' Install passt (RHEL 9 and later) or slirp4netns (RHEL 8), or run with --share-net\n' >&2 + printf ' to share the host network (services on the host loopback are then reachable).\n' >&2 exit 3 } @@ -2436,6 +2482,334 @@ _net_try_pasta() { return 0 } +# ── slirp4netns backend ────────────────────────────────────────── +# Used when pasta is not usable (RHEL 8 has slirp4netns but no passt). +# slirp4netns cannot start the command itself; it attaches to an existing +# network namespace. The launch therefore has two steps, while the launcher +# still execs in the foreground (so the tty, job control, exit status and +# --die-with-parent are unchanged): +# +# launcher ─┬─ helper (background subshell, _net_slirp_helper) +# │ waits until the launcher's pid ($$) is in its new network +# │ namespace, starts `setsid slirp4netns ... $$ tap0`, waits +# │ for it to be ready, adds the --publish forwards through its +# │ API socket, then releases the sandbox (--block-fd) and exits +# └─ exec env -i ... unshare --user --map-root-user --net -- +# bash -- bwrap --unshare-user --uid U --gid G +# --info-fd 90 --block-fd 91 --sync-fd 92 ... -- tool +# +# On a failure the helper kills the still-blocked sandbox (its pid comes +# from --info-fd) and then bwrap: bwrap's --die-with-parent refers to the +# user's shell, so killing bwrap alone would leave the sandbox, which the +# closed --block-fd would then release. +# +# The namespaces come from unshare (util-linux), not from bwrap's +# --unshare-net: bwrap mounting /dev (devpts) puts the sandbox in a second +# user namespace nested in the one that owns the network namespace, and +# slirp4netns, which joins the target process's user namespace, would then +# have no rights over the network. With unshare the launcher's own process +# holds a single-level user namespace that owns the network namespace. It is +# mapped to uid 0 (as with pasta), so bwrap runs the sandbox as the real +# user with --unshare-user --uid/--gid. +# +# The tool does not start before the network is ready. slirp4netns runs in +# its own session (Ctrl-C does not reach it) with a scrubbed environment, and +# its --exit-fd is the read end of bwrap's --sync-fd pipe, so it exits with +# the sandbox. Settings: +# --disable-host-loopback mandatory: otherwise the gateway (.2) reaches the +# host's 127.0.0.1 on every port. +# built-in DNS (.3) forwarded to the host's first nameserver, port 53 +# only (libslirp >= 4.3.0, checked), so a loopback +# resolver such as systemd-resolved keeps working. +# --cidr 192.168.254.0/24 (BNL uses 10/8, slirp's default +# range); sandbox .100, gateway .2, DNS .3. +# --enable-sandbox/--enable-seccomp as podman does, when /etc/resolv.conf +# resolves inside /etc or /run (required by it). +# --publish works through the API socket (add_hostfwd, IPv4 TCP, one port per +# request); --host-port and --net-log have no slirp4netns equivalent. + +_SLIRP_BIN="" +_SLIRP_ARGS=() +_UNSHARE_BIN="" +_NET_SLIRP_NET="192.168.254" +_NET_SLIRP_CIDR="${_NET_SLIRP_NET}.0/24" +_NET_SLIRP_DNS="${_NET_SLIRP_NET}.3" +# "HOSTADDR,HOSTPORT,SANDBOXPORT" for each published port; at most +# _NET_SLIRP_MAX_FWDS (one API request each). +_NET_SLIRP_FWDS=() +_NET_SLIRP_MAX_FWDS=64 +_NET_SLIRP_DIR="" +_NET_PY_BIN="" +# Descriptors of the two-step launch. bwrap gets 90-92; the helper keeps +# 93-99. The descriptor relay's numbers stay below 90. +_NET_FD_INFO_W=90 +_NET_FD_BLOCK_R=91 +_NET_FD_SYNC_W=92 +_NET_FD_INFO_R=93 +_NET_FD_BLOCK_W=94 +_NET_FD_SYNC_R=95 +_NET_FD_READY_W=96 +_NET_FD_READY_R=97 +_NET_FD_LOG_W=98 +_NET_FD_LOG_R=99 + +# The API client: one add_hostfwd request per argument "ADDR,HOSTPORT,PORT". +_NET_SLIRP_API_CLIENT=' +import json, socket, sys +path = sys.argv[1] +for spec in sys.argv[2:]: + addr, hport, gport = spec.split(",") + req = {"execute": "add_hostfwd", "arguments": {"proto": "tcp", + "host_addr": addr, "host_port": int(hport), "guest_port": int(gport)}} + s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + s.connect(path) + s.sendall(json.dumps(req).encode()) + s.shutdown(socket.SHUT_WR) + data = b"" + while True: + chunk = s.recv(4096) + if not chunk: + break + data += chunk + s.close() + try: + reply = json.loads(data.decode() or "{}") + except ValueError: + reply = {"error": data.decode(errors="replace")} + if "return" not in reply: + sys.exit("%s:%s -> sandbox port %s: %s" % (addr, hport, gport, reply.get("error", reply))) +' + +# _net_try_slirp +# Return 0 when slirp4netns can be used (set _SLIRP_BIN, _SLIRP_ARGS and, +# for --publish, _NET_PY_BIN), else 1 with the reason in _NET_TRY_REASON. +# There is no test run (it would need a second sandbox); a failure at +# launch stops the sandbox before the tool starts (see _net_slirp_helper). +_net_try_slirp() { + local _out _sver="" _lver="" _resolv _n + _NET_TRY_REASON="" + _SLIRP_BIN="$(command -v slirp4netns 2> /dev/null || true)" + if [[ -z "${_SLIRP_BIN}" || ! -x "${_SLIRP_BIN}" ]]; then + _NET_TRY_REASON="not found on PATH (package slirp4netns)" + return 1 + fi + _UNSHARE_BIN="$(command -v unshare 2> /dev/null || true)" + if [[ -z "${_UNSHARE_BIN}" || ! -x "${_UNSHARE_BIN}" ]] || + ! "${_UNSHARE_BIN}" --help 2>&1 | grep -q -e '--map-root-user'; then + _NET_TRY_REASON="needs unshare (util-linux) with --map-root-user" + return 1 + fi + if ! _out="$("${_SLIRP_BIN}" --version 2>&1)"; then + _NET_TRY_REASON="'slirp4netns --version' failed" + return 1 + fi + [[ ! "${_out}" =~ slirp4netns\ version\ ([0-9][0-9.]*) ]] || _sver="${BASH_REMATCH[1]}" + [[ ! "${_out}" =~ libslirp:\ ([0-9][0-9.]*) ]] || _lver="${BASH_REMATCH[1]}" + if [[ -z "${_sver}" ]] || ! _version_ge "${_sver}" 0.4.0; then + _NET_TRY_REASON="version ${_sver:-unknown} is too old (0.4.0 or later is needed)" + return 1 + fi + # Before libslirp 4.3.0 the DNS address forwarded every port to the host's + # resolver address, which is often on loopback. + if [[ -z "${_lver}" ]] || ! _version_ge "${_lver}" 4.3.0; then + _NET_TRY_REASON="libslirp ${_lver:-version unknown} is too old (4.3.0 or later is needed)" + return 1 + fi + if [[ "${DRY_RUN}" -eq 0 ]]; then + if [[ ! -c /dev/net/tun ]]; then + _NET_TRY_REASON="/dev/net/tun is missing on the host" + return 1 + fi + _n="$(cat /proc/sys/user/max_user_namespaces 2> /dev/null || echo 1)" + if [[ "${_n}" == 0 ]]; then + _NET_TRY_REASON="unprivileged user namespaces are disabled (user.max_user_namespaces = 0)" + return 1 + fi + fi + _NET_PY_BIN="" + if [[ ${#NET_PUBLISH[@]} -gt 0 ]]; then + for _n in /usr/libexec/platform-python "$(command -v python3 2> /dev/null || true)"; do + if [[ -n "${_n}" && -x "${_n}" ]]; then + _NET_PY_BIN="${_n}" + break + fi + done + if [[ -z "${_NET_PY_BIN}" ]]; then + _NET_TRY_REASON="--publish needs python3 (or /usr/libexec/platform-python) to configure it" + return 1 + fi + fi + _SLIRP_ARGS=( + --configure + --mtu=65520 + --disable-host-loopback + --cidr="${_NET_SLIRP_CIDR}" + ) + _resolv="$(readlink -f /etc/resolv.conf 2> /dev/null || true)" + if [[ "${_resolv}" == /etc/* || "${_resolv}" == /run/* ]]; then + _SLIRP_ARGS+=(--enable-sandbox --enable-seccomp) + fi + return 0 +} + +# _net_slirp_publish_fwds +# Fill _NET_SLIRP_FWDS from NET_PUBLISH and the publish address, one entry +# per port. Exit 1 above _NET_SLIRP_MAX_FWDS ports. +_net_slirp_publish_fwds() { + local _spec _h _g _hlo _hhi _glo _p + _NET_SLIRP_FWDS=() + for _spec in ${NET_PUBLISH[@]+"${NET_PUBLISH[@]}"}; do + _h="${_spec%%:*}" + _g="${_spec#*:}" + _hlo="${_h%-*}" + _hhi="${_h#*-}" + _glo="${_g%-*}" + for ((_p = _hlo; _p <= _hhi; _p++)); do + if [[ ${#_NET_SLIRP_FWDS[@]} -ge ${_NET_SLIRP_MAX_FWDS} ]]; then + echo "Error: --publish: at most ${_NET_SLIRP_MAX_FWDS} ports can be published with slirp4netns." >&2 + exit 1 + fi + _NET_SLIRP_FWDS+=("${_NET_PUBLISH_ADDR_EFFECTIVE},${_p},$((_glo + _p - _hlo))") + done + done +} + +# _net_slirp_start +# Called by launch_sandbox just before it execs bwrap: create the pipes +# (FIFOs in a private directory, removed once open), start the helper in +# the background and close the helper's descriptors in the launcher. +_net_slirp_start() { + local _rt="${XDG_RUNTIME_DIR:-}" _f + if [[ -z "${_rt}" || ! -d "${_rt}" || ! -w "${_rt}" || ! -O "${_rt}" ]]; then + _rt="${TMPDIR:-/tmp}" + fi + if ! _NET_SLIRP_DIR="$(mktemp -d "${_rt}/bwrap-net.XXXXXXXX")"; then + echo "Error: cannot create a private directory in ${_rt} for slirp4netns." >&2 + exit 3 + fi + for _f in info block sync ready; do + mkfifo -m 0600 "${_NET_SLIRP_DIR}/${_f}" + done + # Each FIFO is opened read-write first, so the read-only open after it + # does not block. + exec 90<> "${_NET_SLIRP_DIR}/info" + exec 93< "${_NET_SLIRP_DIR}/info" + exec 94<> "${_NET_SLIRP_DIR}/block" + exec 91< "${_NET_SLIRP_DIR}/block" + exec 92<> "${_NET_SLIRP_DIR}/sync" + exec 95< "${_NET_SLIRP_DIR}/sync" + exec 96<> "${_NET_SLIRP_DIR}/ready" + exec 97< "${_NET_SLIRP_DIR}/ready" + exec 98> "${_NET_SLIRP_DIR}/log" + exec 99< "${_NET_SLIRP_DIR}/log" + rm -f "${_NET_SLIRP_DIR}/info" "${_NET_SLIRP_DIR}/block" "${_NET_SLIRP_DIR}/sync" \ + "${_NET_SLIRP_DIR}/ready" "${_NET_SLIRP_DIR}/log" + if [[ ${#_NET_SLIRP_FWDS[@]} -eq 0 ]]; then + rmdir "${_NET_SLIRP_DIR}" + fi + + _net_slirp_helper & + exec 93<&- 94>&- 95<&- 96>&- 97<&- 98>&- 99<&- +} + +# _net_slirp_helper +# The background half of the two-step launch (see "slirp4netns backend"). +# Runs in a subshell; $$ is still the launcher, which is bwrap by then. +# _ENV_BIN is launch_sandbox's local (bash scoping is dynamic). +_net_slirp_helper() { + local _own _ns="" _map="" _pid="$$" _r _out _i + local -a _api=() + set +e + exec 90>&- 91<&- 92>&- + trap '' INT QUIT TSTP + + # Wait (up to 10 s) until the launcher process has run unshare: a network + # namespace of its own, with the uid map written. + _own="$(readlink /proc/self/ns/net)" + for ((_i = 0; _i < 500; _i++)); do + _ns="$(readlink "/proc/${_pid}/ns/net" 2> /dev/null)" + _map="$(cat "/proc/${_pid}/uid_map" 2> /dev/null)" + [[ -z "${_ns}" || "${_ns}" == "${_own}" || -z "${_map}" ]] || break + sleep 0.02 + done + if [[ -z "${_ns}" || "${_ns}" == "${_own}" || -z "${_map}" ]]; then + _net_slirp_fail "the sandbox did not get its network namespace" + fi + + [[ ${#_NET_SLIRP_FWDS[@]} -eq 0 ]] || _api=(--api-socket="${_NET_SLIRP_DIR}/api.sock") + _net_slirp_run "${_pid}" ${_api[@]+"${_api[@]}"} > /dev/null 2>&98 93<&- 94>&- 97<&- 99<&- & + exec 95<&- 96>&- 98>&- + + if ! IFS= read -r -t 30 -n 1 _r <&97 || [[ "${_r}" != 1 ]]; then + _net_slirp_fail "slirp4netns did not become ready" + fi + if [[ ${#_NET_SLIRP_FWDS[@]} -gt 0 ]]; then + if ! _out="$("${_NET_PY_BIN}" -I -c "${_NET_SLIRP_API_CLIENT}" \ + "${_NET_SLIRP_DIR}/api.sock" "${_NET_SLIRP_FWDS[@]}" 2>&1)"; then + _net_slirp_fail "publishing a port failed: ${_out##*$'\n'}" + fi + rm -rf "${_NET_SLIRP_DIR}" + fi + printf 1 >&94 + exit 0 +} + +# _net_slirp_run PID [--api-socket=PATH] +# Exec slirp4netns for the network namespace of PID, in its own session, +# with a scrubbed environment and default signal handling. Run by +# _net_slirp_helper in the background. +_net_slirp_run() { + local _pid="$1" + shift + trap - INT QUIT TSTP + exec setsid "${_ENV_BIN}" -i PATH=/usr/bin:/bin \ + "${_SLIRP_BIN}" "${_SLIRP_ARGS[@]}" \ + --ready-fd=96 --exit-fd=95 "$@" "${_pid}" tap0 +} + +# _net_slirp_fail MESSAGE +# Report a failed network setup, kill the still-blocked sandbox (the tool +# has not started; its pid is read from bwrap's --info-fd) and then the +# launcher process ($$, by now bwrap), and exit the helper while it still +# holds the --block-fd pipe. slirp4netns, if started, exits with bwrap +# (--exit-fd). +_net_slirp_fail() { + local _msg="$1" _info="" _line + printf '%s: network setup failed (slirp4netns): %s\n' "${0##*/}" "${_msg}" >&2 + tail -n 5 <&99 2> /dev/null | sed 's/^/ /' >&2 + printf ' The sandbox was stopped. Run with --share-net to start without network isolation.\n' >&2 + while IFS= read -r -t 5 _line <&93; do + _info+="${_line}" + [[ "${_line}" != *"}"* ]] || break + done + if [[ "${_info}" =~ \"child-pid\":\ *([0-9]+) ]]; then + kill -KILL "${BASH_REMATCH[1]}" 2> /dev/null + fi + kill -KILL "$$" 2> /dev/null + [[ -z "${_NET_SLIRP_DIR}" ]] || rm -rf "${_NET_SLIRP_DIR}" + exit 1 +} + +# _net_slirp_print_dry_run +# Describe the helper's part of the launch as comments for --dry-run. +_net_slirp_print_dry_run() { + local _a _b _c _f + printf '\n# Network (slirp4netns): before exec, the launcher starts a helper that waits\n' + printf '# until the launcher pid is in the network namespace made by unshare, then runs\n' + printf '# setsid env -i PATH=/usr/bin:/bin %s' "${_SLIRP_BIN}" + for _a in "${_SLIRP_ARGS[@]}"; do printf ' %s' "${_a}"; done + printf ' --ready-fd=%s --exit-fd=%s' "${_NET_FD_READY_W}" "${_NET_FD_SYNC_R}" + [[ ${#_NET_SLIRP_FWDS[@]} -eq 0 ]] || printf ' --api-socket=/api.sock' + printf ' tap0\n' + for _f in ${_NET_SLIRP_FWDS[@]+"${_NET_SLIRP_FWDS[@]}"}; do + IFS=, read -r _a _b _c <<< "${_f}" + printf '# add_hostfwd tcp %s:%s -> sandbox port %s\n' "${_a}" "${_b}" "${_c}" + done + printf '# and writes to descriptor %s (bwrap --block-fd %s) to start the sandbox.\n' "${_NET_FD_BLOCK_W}" "${_NET_FD_BLOCK_R}" + printf '# On a failure it kills the sandbox (pid from bwrap --info-fd %s) before it starts.\n' "${_NET_FD_INFO_W}" + printf '# slirp4netns exits when bwrap closes descriptor %s (bwrap --sync-fd).\n' "${_NET_FD_SYNC_W}" +} + # _net_parse_range OPTION TEXT # Parse N or LOW-HIGH (ports 1024-65535) into _NET_LO and _NET_HI. Exit 1 # with a message naming OPTION otherwise. @@ -2713,7 +3087,7 @@ build_net_isolation() { _NET_HOST_DESC=() _NET_PUBLISH_ADDR_EFFECTIVE="" - # Host network (no pasta, or --share-net): the previous behaviour. + # Host network (--share-net): the previous behaviour. if [[ "${_NET_PRIVATE}" -ne 1 ]]; then BWRAP_ARGS+=( --ro-bind-try /etc/resolv.conf /etc/resolv.conf @@ -2728,28 +3102,48 @@ build_net_isolation() { ) return 0 fi - [[ ${#PASTA_ARGS[@]} -gt 0 ]] || _net_baseline_args - - # pasta maps the caller to uid 0 in its user namespace: run the sandbox as - # the real user in a nested one, and undo pasta's hostname change. - BWRAP_ARGS+=( - --unshare-user - --uid "$(id -u)" - --gid "$(id -g)" - --unshare-uts - --hostname "$(uname -n)" - ) + local _dns="${_NET_DNS_FORWARD_ADDR}" + if [[ "${_NET_BACKEND}" == pasta ]]; then + [[ ${#PASTA_ARGS[@]} -gt 0 ]] || _net_baseline_args + # pasta maps the caller to uid 0 in its user namespace: run the + # sandbox as the real user in a nested one, and undo pasta's hostname + # change. + BWRAP_ARGS+=( + --unshare-user + --uid "$(id -u)" + --gid "$(id -g)" + --unshare-uts + --hostname "$(uname -n)" + ) + else + # unshare creates the namespaces (mapping the caller to uid 0) and + # slirp4netns attaches to them; the launch descriptors are opened by + # _net_slirp_start. + _dns="${_NET_SLIRP_DNS}" + BWRAP_ARGS+=( + --unshare-user + --uid "$(id -u)" + --gid "$(id -g)" + --info-fd "${_NET_FD_INFO_W}" + --block-fd "${_NET_FD_BLOCK_R}" + --sync-fd "${_NET_FD_SYNC_W}" + ) + fi if [[ -n "${NET_PUBLISH_ADDR}" && ${#NET_PUBLISH[@]} -eq 0 ]]; then echo "Warning: --publish-addr has no effect without --publish." >&2 fi if [[ ${#NET_PUBLISH[@]} -gt 0 ]]; then _net_derive_publish_addr - for _i in "${!PASTA_ARGS[@]}"; do - if [[ "${PASTA_ARGS[${_i}]}" == -t ]]; then - PASTA_ARGS[_i + 1]="${_NET_PUBLISH_ADDR_EFFECTIVE}/$(_net_join "${NET_PUBLISH[@]}")" - fi - done + if [[ "${_NET_BACKEND}" == pasta ]]; then + for _i in "${!PASTA_ARGS[@]}"; do + if [[ "${PASTA_ARGS[${_i}]}" == -t ]]; then + PASTA_ARGS[_i + 1]="${_NET_PUBLISH_ADDR_EFFECTIVE}/$(_net_join "${NET_PUBLISH[@]}")" + fi + done + else + _net_slirp_publish_fwds + fi for _spec in "${NET_PUBLISH[@]}"; do _host="${_spec%%:*}" _sbx="${_spec#*:}" @@ -2785,13 +3179,14 @@ build_net_isolation() { PASTA_ARGS+=(--log-file "${NET_LOG_FILE}" --debug) fi - # DNS: pasta forwards queries for _NET_DNS_FORWARD_ADDR to the host's first - # nameserver. Only IPv4 is forwarded. + # DNS: pasta forwards queries for _NET_DNS_FORWARD_ADDR, slirp4netns for + # _NET_SLIRP_DNS, to the host's first nameserver. Only IPv4. if [[ -r /etc/resolv.conf ]] && ! grep -Eq '^[[:space:]]*nameserver[[:space:]]+[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' /etc/resolv.conf; then echo "Warning: /etc/resolv.conf has no IPv4 nameserver: name resolution will not work in the sandbox." >&2 fi - _resolv="nameserver ${_NET_DNS_FORWARD_ADDR}"$'\n'"options edns0" + _NET_DNS_EFFECTIVE="${_dns}" + _resolv="nameserver ${_dns}"$'\n'"options edns0" bwrap_fd_from_data "generated /etc/resolv.conf" "${_resolv}" BWRAP_ARGS+=(--ro-bind-data "${_BWRAP_FD}" /etc/resolv.conf) @@ -2851,7 +3246,8 @@ _net_print_report() { # the network mode is always a decision, never an accident. _net_require_configured() { if [[ "${_NET_CONFIGURED}" -ne 1 ]] || - [[ "${_NET_PRIVATE}" -eq 1 && -z "${_PASTA_BIN}" ]]; then + [[ "${_NET_BACKEND}" == pasta && -z "${_PASTA_BIN}" ]] || + [[ "${_NET_BACKEND}" == slirp4netns && -z "${_SLIRP_BIN}" ]]; then echo "Error: ${0##*/}: network isolation was not set up (detect_net_capabilities / build_net_isolation missing)." >&2 exit 1 fi @@ -3868,11 +4264,15 @@ _bwrap_context_network() { fi _t+="This sandbox has its own network namespace (${_NET_BACKEND})." _t+=" Outbound connections work and leave from the host's address;" - _t+=" DNS goes to \`${_NET_DNS_FORWARD_ADDR}\`, which is forwarded to the host's resolver." + _t+=" DNS goes to \`${_NET_DNS_EFFECTIVE}\`, which is forwarded to the host's resolver." _t+=" Services listening on the host's loopback (\`127.0.0.1\`, \`localhost\`) are not reachable from here," _t+=" and nothing you listen on here is reachable from the host, except as listed below." _t+=" \`127.0.0.1\` here is the sandbox's own loopback." - _t+=" ICMP (\`ping\`) may not work.${_nl}${_nl}" + _t+=" ICMP (\`ping\`) may not work." + if [[ "${_NET_BACKEND}" == slirp4netns ]]; then + _t+=" IPv4 only. Host services cannot be made reachable with this backend (no \`--host-port\`)." + fi + _t+="${_nl}${_nl}" _t+="Published ports (a server you run here, reachable from the host):" if [[ ${#_NET_PUBLISH_DESC[@]} -eq 0 ]]; then @@ -4188,6 +4588,16 @@ _bwrap_toml_string() { # exec'ing bwrap. Not for secrets: the variable stays in # pasta's environment for the whole session. +# _bwrap_fd_check_room +# Exit 1 when the reserved numbers would reach 90, where the slirp4netns +# launch keeps its own descriptors (_NET_FD_*). +_bwrap_fd_check_room() { + if [[ ${_BWRAP_FD_NEXT} -ge 90 ]]; then + echo "Error: too many files to pass to the sandbox by descriptor." >&2 + exit 1 + fi +} + # bwrap_fd_from_file PATH # Reserve a descriptor that the relay opens on PATH and set _BWRAP_FD to its # number. Exits 1 when PATH cannot be read now. @@ -4197,6 +4607,7 @@ bwrap_fd_from_file() { echo "Error: cannot read '${_path}'." >&2 exit 1 fi + _bwrap_fd_check_room _BWRAP_FD="${_BWRAP_FD_NEXT}" _BWRAP_FD_NEXT=$((_BWRAP_FD_NEXT + 1)) _BWRAP_FD_RELAY+=("${_BWRAP_FD}" file "${_path}") @@ -4213,6 +4624,7 @@ bwrap_fd_from_data() { echo "Error: the ${_label} is too large to pass to the sandbox (${#_text} characters)." >&2 exit 1 fi + _bwrap_fd_check_room _BWRAP_FD="${_BWRAP_FD_NEXT}" _BWRAP_FD_NEXT=$((_BWRAP_FD_NEXT + 1)) _BWRAP_FD_RELAY+=("${_BWRAP_FD}" data) @@ -4420,7 +4832,8 @@ print_dry_run() { # Mirror the exact invocation used by launch_sandbox: # exec -i - # [ --] (private network only) + # [ --] (pasta backend only) + # [ --user --map-root-user --net --] (slirp4netns only) # -c bwrap-fd-relay -- # -- <_TOOL_CMD> # Values of secret-looking variables (_is_secret_env_name) are printed as @@ -4447,8 +4860,8 @@ print_dry_run() { printf " _BWRAP_FD_DATA_%s='<%s, shown below>' \\\\\n" "${_n}" "${_BWRAP_FD_LABEL["${_n}"]}" done local _p=0 - [[ "${_NET_PRIVATE}" -ne 1 ]] || printf " %s \\\\\n" "${_PASTA_BIN}" - while [[ "${_NET_PRIVATE}" -eq 1 && ${_p} -lt ${#PASTA_ARGS[@]} ]]; do + [[ "${_NET_BACKEND}" != pasta ]] || printf " %s \\\\\n" "${_PASTA_BIN}" + while [[ "${_NET_BACKEND}" == pasta && ${_p} -lt ${#PASTA_ARGS[@]} ]]; do case "${PASTA_ARGS[${_p}]}" in -t | -u | -T | -U | --dns-forward | --search | --log-file) printf " %s %q \\\\\n" "${PASTA_ARGS[${_p}]}" "${PASTA_ARGS[$((_p + 1))]}" @@ -4460,7 +4873,10 @@ print_dry_run() { ;; esac done - [[ "${_NET_PRIVATE}" -ne 1 ]] || printf " -- \\\\\n" + [[ "${_NET_BACKEND}" != pasta ]] || printf " -- \\\\\n" + if [[ "${_NET_BACKEND}" == slirp4netns ]]; then + printf " %s --user --map-root-user --net -- \\\\\n" "${_UNSHARE_BIN}" + fi printf " %s -c '<_BWRAP_FD_RELAY_SHIM from bwrap_sandbox_lib.sh>' bwrap-fd-relay \\\\\n" "${_BASH_BIN}" local _r=0 while [[ ${_r} -lt ${#_BWRAP_FD_RELAY[@]} ]]; do @@ -4506,6 +4922,7 @@ print_dry_run() { local _arg for _arg in ${TOOL_ARGS[@]+"${TOOL_ARGS[@]}"}; do printf " %q" "${_arg}"; done printf "\n" + [[ "${_NET_BACKEND}" != slirp4netns ]] || _net_slirp_print_dry_run for _n in ${_BWRAP_FD_DATA_NUMS[@]+"${_BWRAP_FD_DATA_NUMS[@]}"}; do printf '\n# Descriptor %s, filled by the relay with the %s:\n#\n' "${_n}" "${_BWRAP_FD_LABEL["${_n}"]}" printf '%s\n' "${_BWRAP_FD_DATA["${_n}"]}" | sed 's/^/# /; s/ *$//' @@ -4540,8 +4957,10 @@ launch_sandbox() { _BASH_BIN="$(resolve_bash_bin)" local -a _data_env=() _net_prefix=() - if [[ "${_NET_PRIVATE}" -eq 1 ]]; then + if [[ "${_NET_BACKEND}" == pasta ]]; then _net_prefix=("${_PASTA_BIN}" "${PASTA_ARGS[@]}" --) + elif [[ "${_NET_BACKEND}" == slirp4netns ]]; then + _net_prefix=("${_UNSHARE_BIN}" --user --map-root-user --net --) fi _bwrap_fd_data_numbers for _n in ${_BWRAP_FD_DATA_NUMS[@]+"${_BWRAP_FD_DATA_NUMS[@]}"}; do @@ -4549,6 +4968,7 @@ launch_sandbox() { done _net_print_report + [[ "${_NET_BACKEND}" != slirp4netns ]] || _net_slirp_start exec "${_ENV_BIN}" -i \ HOME="${_HOME}" \ USER="${_USER}" \ From 8f828bcaa9ea9af0f4e749391f655b32763bad8e Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 30 Oct 2026 11:00:57 -0400 Subject: [PATCH 10/37] docs(bwrap): describe the backend choice and slirp4netns Document the selection order (pasta, slirp4netns, else stop unless --share-net), --net-backend, what each backend supports, and how the slirp4netns launch works. Assisted-by: opencode:claude-opus-5-5 --- README.md | 13 +++-- docs/bw-wrappers.md | 138 ++++++++++++++++++++++++++++++++------------ 2 files changed, 107 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 2e917f8..b90d255 100644 --- a/README.md +++ b/README.md @@ -71,10 +71,10 @@ Add those three exports to your `~/.bashrc` or `~/.zshrc` for persistence. - `bash` >= 4.x (the `bw*` wrappers use `[[ ]]` and arrays) - `bubblewrap` (`bwrap`) >= 0.4.0 for the `bw*` wrappers - binary masking is verified on RHEL 8.10 (`bubblewrap-0.4.0-3.el8_10`) and EL9 (0.6.3) -- optional: `pasta` (package `passt`, RHEL 9+), `/dev/net/tun` and - unprivileged user namespaces, with which the `bw*` wrappers run every sandbox - in a private network namespace; without them (RHEL 8) the sandbox shares the - host network, with a warning +- `pasta` (package `passt`, RHEL 9+) or `slirp4netns` (RHEL 8 AppStream), + with `/dev/net/tun` and unprivileged user namespaces, for the private network + namespace of the `bw*` wrappers; without either they stop unless run with + `--share-net` - `gpg(1)` for `gpg-passwd.sh` - `openssl(1)` for `pemdecompose` - `python3 >= 3.9` (stdlib only, no pip dependencies) for `azoidcapp` @@ -100,7 +100,7 @@ Common wrapper options: | Option | Effect | | --- | --- | | `--help`, `-h` | Show wrapper help, then the underlying tool's help when the tool is installed | -| `--dry-run` | Print the command (`pasta`, descriptor relay, `bwrap`) without executing it | +| `--dry-run` | Print the command (network backend, descriptor relay, `bwrap`) without executing it | | `--exec CMD [ARGS...]` | Run `CMD` inside the sandbox instead of the tool. `CMD` is split on whitespace; following arguments are passed unchanged, e.g. `--exec bash -c 'env \| sort'` | | `--init-auth` | Persist auth credentials to the host (first-time setup) | | `--new-session` | Force `bwrap --new-session` (stricter isolation; breaks SIGWINCH) | @@ -111,7 +111,8 @@ Common wrapper options: | `--publish-addr ADDR` | Use `ADDR` (in `127.0.0.0/8`, e.g. `127.0.0.1` for OAuth) for `--publish` | | `--host-port PORT[:SANDBOXPORT]` | Make the host's `127.0.0.1:PORT` reachable in the sandbox at `host.internal` | | `--net-log FILE` | Log every network flow to `FILE` on the host | -| `--share-net` | Share the host network instead of a private namespace (the behaviour without `pasta`) | +| `--share-net` | Share the host network instead of a private namespace (the behaviour before private namespaces) | +| `--net-backend pasta\|slirp4netns` | Use only this network backend | `bwclaude` also supports `--debug`. `bwopencode2` has no `--init-auth` (sign-in persists in its data directory), always runs OpenCode 2 with `--standalone` diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 6d95fba..966f356 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -17,11 +17,14 @@ option list; the tables below summarise the common surface. - `bash` >= 4.x (the wrappers use `[[ ]]` and arrays) - `bubblewrap` (`bwrap`) >= 0.4.0 - binary masking is verified on RHEL 8.10 (`bubblewrap-0.4.0-3.el8_10`) and EL9 (0.6.3) -- Optional, for the private network namespace: `pasta` (package `passt`, - in RHEL 9 AppStream; not available for RHEL 8), `/dev/net/tun`, and - unprivileged user namespaces on the host. Without them the sandbox shares - the host network, with a warning (see [Network](#network)). `ss` (iproute) - is used to find busy ports for `--publish`. +- For the private network namespace, one of: `pasta` (package `passt`, + RHEL 9 and later), or `slirp4netns` >= 0.4.0 with libslirp >= 4.3.0 + (package `slirp4netns`, RHEL 8 AppStream) and util-linux `unshare`; plus + `/dev/net/tun` and unprivileged user namespaces on the host. Without them + the wrappers stop (exit status 3) unless run with `--share-net` (see + [Network](#network)). `ss` (iproute) is used to find busy ports for + `--publish`; with slirp4netns, `--publish` also needs `python3` (or RHEL 8's + `/usr/libexec/platform-python`). ## Managed system paths @@ -91,7 +94,7 @@ Every `bw*` wrapper accepts these options: | Option | Effect | | --- | --- | | `--help`, `-h` | Show wrapper help, then the underlying tool's help when the tool is installed | -| `--dry-run` | Print the command (`pasta`, the descriptor relay and `bwrap`) without executing it | +| `--dry-run` | Print the command (the network backend, the descriptor relay and `bwrap`) without executing it | | `--exec CMD [ARGS...]` | Run `CMD` inside the sandbox instead of the tool. `CMD` is split on whitespace; following arguments are passed unchanged, e.g. `--exec bash -c 'env \| sort'` | | `--init-auth` | Persist auth credentials to the host (first-time setup; not available in `bwopencode2`) | | `--new-session` | Force `bwrap --new-session` (stricter isolation; breaks SIGWINCH) | @@ -102,9 +105,10 @@ Every `bw*` wrapper accepts these options: | `--no-sandbox-context` | Do not generate the [sandbox context file](#sandbox-context) that tells the agent what is mounted | | `--publish PORT[:SANDBOXPORT]` | Make a server in the sandbox reachable on the host at a per-user, per-session loopback address. May be repeated. See [Network](#network) | | `--publish-addr ADDR` | Use `ADDR` (in `127.0.0.0/8`) for `--publish` instead of the derived address, for example `127.0.0.1` for OAuth redirects | -| `--host-port PORT[:SANDBOXPORT]` | Make the host's `127.0.0.1:PORT` reachable in the sandbox at `host.internal:SANDBOXPORT`. May be repeated | -| `--net-log FILE` | Log every network flow to `FILE` on the host (`pasta --debug`) | -| `--share-net` | Share the host network instead of a private namespace (the behaviour without `pasta`); not with the forwarding options | +| `--host-port PORT[:SANDBOXPORT]` | Make the host's `127.0.0.1:PORT` reachable in the sandbox at `host.internal:SANDBOXPORT`. May be repeated (pasta only) | +| `--net-log FILE` | Log every network flow to `FILE` on the host (`pasta --debug`; pasta only) | +| `--share-net` | Share the host network instead of a private namespace (the behaviour before private namespaces); not with the forwarding options | +| `--net-backend pasta\|slirp4netns` | Use only this backend for the private namespace (normally pasta, else slirp4netns) | `--ro-path` and `--rw-path` may be nested: a path nested under another overrides it, so `--rw-path DIR --ro-path DIR/sub` mounts `DIR` read-write but @@ -281,15 +285,19 @@ link?"), or for Copilot run `bwcopilot instruction list`. ## Network -Every sandbox runs in its own network namespace, created by -[pasta](https://passt.top) (package `passt`): +Every sandbox runs in its own network namespace, provided by the first usable +backend: -```text -env -i ... pasta [options] -- bash -- bwrap [options] -- -``` +1. [pasta](https://passt.top) (package `passt`, RHEL 9 and later, Fedora); +2. else [slirp4netns](https://github.com/rootless-containers/slirp4netns) + (package `slirp4netns`, RHEL 8 AppStream); +3. else the launch **stops** (exit status 3), naming what is missing for each + backend, unless `--share-net` is given. + +So sandboxes are isolated by default, with one explicit escape hatch. -- **Outbound connections work** (model gateway, package installs, docs) and - leave from the host's address. Egress is not filtered. +- **Outbound connections work** (model gateway, package installs, docs). Egress + is not filtered. - **The host's loopback is unreachable.** Services bound to `127.0.0.1` on the host (OpenCode's own server, language servers, CUPS, local databases, Tiled or queueserver dev instances, other users' sessions) cannot be reached from @@ -300,27 +308,37 @@ env -i ... pasta [options] -- bash -- bwrap [options] -- -- bwrap --unshare-user --uid --gid + --info-fd --block-fd --sync-fd ... -- +``` + +- **The tool does not start before the network is ready**: bwrap waits on + `--block-fd` until the helper writes to it. +- **`unshare`, not bwrap's `--unshare-net`, creates the namespaces.** Because + bwrap mounts `/dev`, it nests the sandbox in a second user namespace inside + the one that owns the network namespace. slirp4netns joins the user + namespace of the process it is given, so it would have no rights over the + network. With `unshare` the launcher process itself holds a single user + namespace that owns the network namespace, and slirp4netns attaches to it. + `unshare` maps you to uid 0 there, so bwrap runs the agent as your real uid + and gid, as with pasta. +- slirp4netns options: `--disable-host-loopback` (otherwise the gateway + address `192.168.254.2` reaches the host's `127.0.0.1` on every port), + `--cidr=192.168.254.0/24` (slirp's default, `10.0.2.0/24`, lies inside + BNL's 10/8), the built-in DNS at `.3` (forwards port 53 only, with libslirp + 4.3.0 and later, which the launcher checks), `--mtu=65520`, and + `--enable-sandbox --enable-seccomp` when `/etc/resolv.conf` resolves inside + `/etc` or `/run`. +- slirp4netns runs in its own session (Ctrl-C does not reach it) with a + scrubbed environment (no API keys). It exits when bwrap does: its + `--exit-fd` is the other end of bwrap's `--sync-fd`. +- `--publish` uses slirp4netns's API socket (`add_hostfwd`, one request per + port). The socket is in a private `mktemp -d` directory under + `$XDG_RUNTIME_DIR`, removed once the forwards are added, so nothing else can + add forwards later. +- **On a setup failure** (slirp4netns does not start, a port cannot be + published) the helper prints the reason and slirp4netns's last messages, + kills the still-waiting sandbox (its pid comes from bwrap's `--info-fd`), so + the tool never runs, and then bwrap. The exit status is then 137, not 3. + ### Descriptor relay bwrap reads some files from descriptors: `--file` for writable copies of @@ -418,7 +480,7 @@ credentials and config (`bwclaude`, `bwcopilot`, `bwcodex`), `--ro-bind-data` for generated files (the context file, `/etc/resolv.conf`, `/etc/hosts`). pasta closes every inherited descriptor above 2, so the launcher does not open them itself. It reserves their numbers; a short fixed bash script (the relay, -`_BWRAP_FD_RELAY_SHIM`) runs as pasta's command, opens each descriptor at its +`_BWRAP_FD_RELAY_SHIM`) runs as the command of pasta or `unshare`, opens each descriptor at its number, from the host file or from generated text carried in an environment variable that it then unsets, and execs bwrap. Nothing is staged on the host. From d4acce70d8e5b36262e97ba37b158a30ee6c891d Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 9 Nov 2026 22:32:43 -0500 Subject: [PATCH 11/37] feat(bwopencode2): publish the --serve port in the private network --serve now adds its port to --publish: the server binds 0.0.0.0 inside the private namespace and is reached on the host at the user's publish address 127.H1.H2.S (normally .1), with both backends. With --share-net it binds 127.H1.H2.1 itself. The port is chosen free on that address, the printed ssh -L command and pairing links name it, and --serve-ssh-config forwards to it. net_publish_prefix exposes the per-user prefix. Assisted-by: opencode:claude-opus-5-5 --- bin/bwopencode2 | 132 ++++++++++++++++++++++++++------------- docs/bw-wrappers.md | 40 ++++++++---- lib/bwrap_sandbox_lib.sh | 29 +++++---- 3 files changed, 134 insertions(+), 67 deletions(-) diff --git a/bin/bwopencode2 b/bin/bwopencode2 index 4134332..73c254e 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -80,10 +80,11 @@ # commands (service, serve, api, pair, reload). See --help. # # --serve is the one deliberate exception: it runs `opencode2 serve` INSIDE this -# sandbox (tools stay sandboxed), bound to 127.0.0.1 on a free port from a fixed -# range and protected by a password, so the OpenCode web UI can be opened from a -# browser (over an ssh -L tunnel). The launcher builds those arguments itself; -# nothing from the command line reaches `serve`. See docs/bw-wrappers.md. +# sandbox (tools stay sandboxed) on a free port from a fixed range, protected by +# a password, and publishes that port (--publish) on this user's loopback +# address 127.H1.H2.S, so the OpenCode web UI can be opened from a browser (over +# an ssh -L tunnel). The launcher builds those arguments itself; nothing from +# the command line reaches `serve`. See docs/bw-wrappers.md. # # Usage: # bwopencode2 [bwopencode2-options] [opencode2 arguments...] @@ -100,7 +101,7 @@ # guard. Detaches the controlling tty for stricter isolation at the # cost of SIGWINCH (terminal resize) propagation. # --serve Run the OpenCode web UI server inside the sandbox instead of the -# TUI (see above). One server per invocation. +# TUI and publish its port (see above). One server per invocation. # --serve-port N With --serve: use port N (must be in the range) instead of the # first free one. # --serve-ssh-config @@ -190,6 +191,12 @@ SERVE_PASSWORD_RANDOM=0 SERVE_PORT_LO="" SERVE_PORT_HI="" SERVE_STATE_DIR="" +# Address the server is reached at on the host: this user's publish address +# (127.H1.H2.S, see net_publish_prefix) or --publish-addr. With a private +# network the port is published there and the server binds 0.0.0.0 inside the +# sandbox; with --share-net the server binds this address itself. +SERVE_ADDR="" +SERVE_BIND="" # Default range: below the kernel's ephemeral range (32768-60999 on Linux, so no # outgoing connection takes a server port), clear of the shared OpenCode service # (49374) and of the ports in use on the workstations. Keep ssh_config in step @@ -350,10 +357,12 @@ Wrapper options (consumed before opencode2 sees the command line): resize) propagates from tmux/screen. Use this flag to opt back into stricter terminal isolation when SIGWINCH is not important. --serve Run the OpenCode web UI server inside the sandbox instead of the - TUI: `opencode2 serve` on 127.0.0.1 (never another address), on - the first free port of the range 24700-24709 (override the range - with BWOPENCODE2_SERVE_PORTS=LO-HI), tools still sandboxed. Open - it from a browser through an ssh tunnel; the command, a one-time + TUI, tools still sandboxed: `opencode2 serve` on the first free + port of the range 24700-24709 (override the range with + BWOPENCODE2_SERVE_PORTS=LO-HI), published like --publish on your + loopback address 127.H1.H2.S (printed; with --share-net the + server binds that address itself, never 0.0.0.0). Open it + from a browser through an ssh tunnel; the command, a one-time pairing link (valid 5 minutes) and the password are printed at start; the web UI's "Use password" button also works. The password is random for each @@ -365,7 +374,8 @@ Wrapper options (consumed before opencode2 sees the command line): free one; fails if it is taken. --serve-ssh-config Print the ssh_config block (one alias, one LocalForward per port - of the range) for the machine your browser runs on, then exit. + of the range, to your address 127.H1.H2.1) for the machine your + browser runs on, then exit. --n2c-token NAME Mount the N2C access token NAME (read-only) so skills inside the sandbox can call NSLS-II APIs, for example observability, @@ -446,7 +456,11 @@ HELP_EOF # `opencode2 serve` is refused as a command-line argument (see the guard below) # because the shared service it could reach runs tools outside any sandbox. # --serve runs the server inside THIS sandbox instead, so tools stay sandboxed: -# - bound to 127.0.0.1 only; reach it from a browser with an ssh -L tunnel; +# - reached on the host at this user's publish address 127.H1.H2.S (normally +# .1; see net_publish_prefix): with a private network the port is added to +# --publish and the server binds 0.0.0.0 inside the namespace (forwarded +# connections arrive at its interface); with --share-net the server binds +# the address itself. From a browser elsewhere, use an ssh -L tunnel; # - one server per invocation, on the first free port of a fixed range, so one # ssh_config block covers every session (--serve-ssh-config); # - protected by a password that reaches the server only as OPENCODE_PASSWORD. @@ -456,7 +470,8 @@ HELP_EOF # can drive every one of them. # The launcher execs bwrap, so it cannot clean up or retry after the server # starts. A port is claimed with a file in the host-side state directory (not -# mounted in the sandbox), which stops two launchers picking one port; a claim +# mounted in the sandbox), which stops two launchers picking one port, and it +# must be free on the address checked (127.H1.H2.1 or --publish-addr); a claim # whose process is gone is stale and is removed. The remaining race is another # program taking the port in the moment before the server binds it. @@ -502,18 +517,20 @@ serve_parse_range() { # Print the ssh_config block for the port range (--serve-ssh-config). serve_print_ssh_config() { - local _p + local _p _addr + _addr="$(net_publish_prefix).1" printf '# Add to ~/.ssh/config on the machine your browser runs on, then: ssh %s\n' "$(serve_ssh_alias)" - printf '# One forward per port of the bwopencode2 --serve range %s-%s. A port with no\n' "${SERVE_PORT_LO}" "${SERVE_PORT_HI}" - printf '# session behind it just refuses connections. Add User / ProxyJump as needed.\n' + printf '# One forward per port of the bwopencode2 --serve range %s-%s, to %s, your\n' "${SERVE_PORT_LO}" "${SERVE_PORT_HI}" "${_addr}" + printf '# own publish address on %s (it depends on your uid: run this as the user who\n' "$(serve_host_name)" + printf '# runs the sessions). A port with no session behind it just refuses\n' + printf '# connections. Add User / ProxyJump as needed.\n' printf 'Host %s\n' "$(serve_ssh_alias)" printf ' HostName %s\n' "$(serve_host_name)" printf ' ExitOnForwardFailure no\n' for ((_p = SERVE_PORT_LO; _p <= SERVE_PORT_HI; _p++)); do - printf ' LocalForward %s 127.0.0.1:%s\n' "${_p}" "${_p}" + printf ' LocalForward %s %s:%s\n' "${_p}" "${_addr}" "${_p}" done } - # Refuse combinations that make no sense; validate --serve-port. serve_check_args() { if [[ "${SERVE}" -eq 0 ]]; then @@ -541,20 +558,21 @@ serve_check_args() { fi } -# True when nothing listens on 127.0.0.1:PORT (the bind succeeds, as the server's -# would; SO_REUSEADDR as a server sets it, so a lingering TIME_WAIT does not count). +# True when nothing listens on ADDR:PORT (the bind succeeds, as the server's or +# the network backend's would; SO_REUSEADDR as a server sets it, so a lingering +# TIME_WAIT does not count). serve_port_is_free() { - local _port="$1" + local _addr="$1" _port="$2" if command -v python3 > /dev/null 2>&1; then python3 -c ' import socket, sys s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) -s.bind(("127.0.0.1", int(sys.argv[1]))) -' "${_port}" 2> /dev/null +s.bind((sys.argv[1], int(sys.argv[2]))) +' "${_addr}" "${_port}" 2> /dev/null else # No python3: a successful connection means something listens. - if (exec 3<> "/dev/tcp/127.0.0.1/${_port}") 2> /dev/null; then + if (exec 3<> "/dev/tcp/${_addr}/${_port}") 2> /dev/null; then return 1 fi fi @@ -620,10 +638,10 @@ serve_choose_port() { _f="${SERVE_STATE_DIR}/${_p}.session" if [[ "${DRY_RUN}" -eq 1 ]]; then serve_claim_is_live "${_f}" && continue - serve_port_is_free "${_p}" || continue + serve_port_is_free "${SERVE_ADDR}" "${_p}" || continue else serve_claim_port "${_p}" || continue - if ! serve_port_is_free "${_p}"; then + if ! serve_port_is_free "${SERVE_ADDR}" "${_p}"; then rm -f "${_f}" continue fi @@ -633,7 +651,7 @@ serve_choose_port() { done if [[ -n "${SERVE_PORT_REQUEST}" ]]; then - echo "Error: port ${SERVE_PORT_REQUEST} is in use." >&2 + echo "Error: port ${SERVE_PORT_REQUEST} is in use on ${SERVE_ADDR}." >&2 else echo "Error: no free port in ${SERVE_PORT_LO}-${SERVE_PORT_HI}; stop a session or see ${SERVE_STATE_DIR}/." >&2 fi @@ -666,16 +684,21 @@ serve_make_password() { # The command run INSIDE the sandbox for --serve: start the server, then print a # one-time pairing link so the browser can sign in without typing the password # (the web UI's "Use password" button still works). It runs as -# `bash -c SERVE_WRAPPER bwopencode2-serve BINARY PORT`. OPENCODE_PASSWORD is in +# `bash -c SERVE_WRAPPER bwopencode2-serve BINARY PORT BIND`. OPENCODE_PASSWORD is in # its environment. The password reaches curl on stdin (-K -), never on a command # line: a process in the sandbox is visible to `ps` on the host. printf is a # builtin, so it does not show either. Needs only bash and curl. read -r -d '' SERVE_WRAPPER << 'SERVE_WRAPPER_EOF' || true set -u -bin=$1 port=$2 -base="http://127.0.0.1:${port}" - -"$bin" serve --hostname 127.0.0.1 --port "$port" & +bin=$1 port=$2 bind=$3 +# The server's own address for the requests below, and the host-side address +# for the printed link (the publish address, or the bound one with --share-net). +local_addr=$bind +[[ $bind != 0.0.0.0 ]] || local_addr=127.0.0.1 +base="http://${local_addr}:${port}" +host_addr=${BWRAP_NET_PUBLISH_ADDR:-$bind} + +"$bin" serve --hostname "$bind" --port "$port" & srv=$! trap 'kill "$srv" 2>/dev/null' INT TERM HUP @@ -698,13 +721,14 @@ done curl -s -K - -X POST "${base}/api/pair") || resp="" if [[ $resp =~ \"code\":\"([A-Za-z0-9_-]+)\" ]]; then printf '\nbwopencode2: pairing link (works once, expires in 5 minutes):\n' - printf ' %s/auth/connect/%s\n' "$base" "${BASH_REMATCH[1]}" + printf ' through the ssh tunnel: http://127.0.0.1:%s/auth/connect/%s\n' "$port" "${BASH_REMATCH[1]}" + printf ' on this host: http://%s:%s/auth/connect/%s\n' "$host_addr" "$port" "${BASH_REMATCH[1]}" printf ' After it expires, click "Use password" in the web UI instead.\n\n' else printf 'bwopencode2: could not get a pairing link; click "Use password" in the web UI.\n' fi else - printf 'bwopencode2: the server did not start listening on %s.\n' "$base" + printf 'bwopencode2: the server did not start listening on %s.\n' "$bind:$port" fi } >&2 @@ -713,25 +737,44 @@ SERVE_WRAPPER_EOF # Choose port and password, and give the sandbox its command. serve_prepare() { + if [[ "${NET_SHARE}" -eq 1 ]]; then + SERVE_ADDR="$(net_publish_prefix).1" + else + SERVE_ADDR="${NET_PUBLISH_ADDR:-$(net_publish_prefix).1}" + fi serve_choose_port serve_make_password - _TOOL_CMD=(/usr/bin/bash -c "${SERVE_WRAPPER}" bwopencode2-serve "${_TOOL_BIN}" "${SERVE_PORT}") + if [[ "${NET_SHARE}" -eq 1 ]]; then + SERVE_BIND="${SERVE_ADDR}" + else + # Forwarded connections arrive at the namespace's interface, not its + # loopback, so the server binds every address of the private + # namespace; only the published port reaches it. + SERVE_BIND="0.0.0.0" + net_add_publish "${SERVE_PORT}" + fi + _TOOL_CMD=(/usr/bin/bash -c "${SERVE_WRAPPER}" bwopencode2-serve "${_TOOL_BIN}" "${SERVE_PORT}" "${SERVE_BIND}") TOOL_ARGS=() } - # Tell the user how to reach the server (stderr, before the exec). serve_print_info() { - local _p="${SERVE_PORT}" _host _alias + local _p="${SERVE_PORT}" _host _alias _addr="${SERVE_ADDR}" + [[ "${NET_SHARE}" -eq 1 ]] || _addr="${_NET_PUBLISH_ADDR_EFFECTIVE}" _host="$(serve_host_name)" _alias="$(serve_ssh_alias)" { - printf '\nbwopencode2: sandboxed OpenCode 2 web UI on 127.0.0.1:%s (this host only)\n' "${_p}" + printf '\nbwopencode2: sandboxed OpenCode 2 web UI on %s:%s (this host only)\n' "${_addr}" "${_p}" printf ' 1. On the machine with your browser, forward the port:\n' - printf ' ssh -L %s:127.0.0.1:%s %s\n' "${_p}" "${_p}" "${_host}" - printf ' or use the ssh_config alias (bwopencode2 --serve-ssh-config): ssh %s\n' "${_alias}" - printf ' (on this host itself, skip this step)\n' + printf ' ssh -L %s:%s:%s %s\n' "${_p}" "${_addr}" "${_p}" "${_host}" + if [[ "${_addr}" == "$(net_publish_prefix).1" ]]; then + printf ' or use the ssh_config alias (bwopencode2 --serve-ssh-config): ssh %s\n' "${_alias}" + else + printf ' (the ssh_config alias from --serve-ssh-config forwards to %s.1, so it does\n' "$(net_publish_prefix)" + printf ' not reach this session: use the command above)\n' + fi + printf ' then open http://127.0.0.1:%s/ there. On this host itself, open http://%s:%s/.\n' "${_p}" "${_addr}" "${_p}" printf ' 2. Open the pairing link printed below once the server is up (it works once and\n' - printf ' expires in 5 minutes), or open http://127.0.0.1:%s/, click "Use password"\n' "${_p}" + printf ' expires in 5 minutes), or open the page above, click "Use password"\n' printf ' and enter the server password.\n' if [[ "${DRY_RUN}" -eq 1 ]]; then printf ' Password: (not shown on --dry-run)\n' @@ -743,7 +786,6 @@ serve_print_info() { printf ' Keep the same port number at both ends of the tunnel: the browser cookie is named by it.\n\n' } >&2 } - # ── Sandbox-escape guard (arguments) ───────────────────────────── # The OpenCode 2 background service listens on a fixed, shared port and runs # tools outside any sandbox. A CLI started without --standalone would @@ -1251,7 +1293,7 @@ build_opencode2_context() { ) if [[ "${SERVE}" -eq 1 ]]; then SANDBOX_CONTEXT_NOTES+=( - "This session is a sandboxed \`opencode2 serve\` (the OpenCode web UI), started by \`bwopencode2 --serve\`. Do not start or connect to any other OpenCode server." + "This session is a sandboxed \`opencode2 serve\` (the OpenCode web UI) on port ${SERVE_PORT}, started by \`bwopencode2 --serve\`; the user reaches it through the published port. Do not start or connect to any other OpenCode server." ) fi build_sandbox_context bwopencode2 @@ -1296,12 +1338,14 @@ main() { fi detect_kernel_capabilities - detect_net_capabilities resolve_common_paths resolve_opencode2_paths + # --serve adds its port to --publish, so it comes before the network + # backend is chosen. if [[ "${SERVE}" -eq 1 ]]; then serve_prepare fi + detect_net_capabilities # Fail before any host-state change (ensure_host_dirs) if nothing is configured. require_opencode_config bwopencode2 "${OC2_CONFIG_SRC}" detect_shell diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 966f356..4ed1acf 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -853,8 +853,15 @@ bwopencode2 --serve-ssh-config - **One server per invocation.** Each `--serve` is its own sandbox with its own server; stop it with Ctrl-C in its terminal. -- **Bound to `127.0.0.1` only**, never another address. Reach it from another - machine through an ssh tunnel. +- **Published like `--publish`.** The server's port is added to + [`--publish`](#opening-ports), so it is reachable on + the host only at your publish address `127.H1.H2.S:PORT` (normally `.1`; the + launcher prints it), never on another address. Inside the private network + namespace the server listens on `0.0.0.0`, because forwarded connections + arrive at the namespace's interface; nothing else reaches it. With + `--share-net` the server listens on `127.H1.H2.1` itself. `--publish-addr + 127.0.0.1` gives the old address. Reach it from another machine through an + ssh tunnel. Works with both network backends. - **Free port from a fixed range**, `24700-24709` by default (ten sessions at once). The range sits below the kernel's ephemeral range (32768-60999 on Linux), so no outgoing connection takes a server port, and clear of 49374 @@ -863,21 +870,28 @@ bwopencode2 --serve-ssh-config if it overlaps the ephemeral range). `--serve-port N` asks for one port in the range and fails if it is taken, so a saved tunnel always matches. - **The ssh tunnel.** At start the launcher prints - `ssh -L P:127.0.0.1:P ` with the host from `hostname -f`. For all - ports at once, `bwopencode2 --serve-ssh-config` prints a block for + `ssh -L P:127.H1.H2.S:P ` with the host from `hostname -f`; then open + `http://127.0.0.1:P/` on the machine with the browser. For all ports at + once, `bwopencode2 --serve-ssh-config` (run on the host, as the user who runs + the sessions: the address depends on the uid) prints a block for `~/.ssh/config` on the machine with the browser (alias `-oc`, one - `LocalForward` per port of the range, `ExitOnForwardFailure no` so a busy local - port does not stop the others). Keep the same port number at both ends: the - browser cookie is named by it (see below). Add `User` and `ProxyJump` to the - alias as needed. + `LocalForward` per port of the range to `127.H1.H2.1`, + `ExitOnForwardFailure no` so a busy local port does not stop the others). + The port is chosen free on `127.H1.H2.1`, so a session normally gets that + address; if it does not (another of your `--publish` ports is busy there), + the launcher says that the alias does not reach it. Keep the same port number + at both ends: the browser cookie is named by it (see below). Add `User` and + `ProxyJump` to the alias as needed. Each user has their own address, so + sessions of different users on one host no longer compete for + `127.0.0.1:24700-24709`. - **Signing in.** The web UI opens on a "Connect to a server" page that asks for a pairing link and offers **Use password**. Do not run `opencode pair` (the wrapper refuses it, and outside the sandbox it points at the shared service). Instead the sandbox runs a small wrapper, not `opencode2 serve` directly: it starts the server, waits until it answers, asks it for a - one-time pairing code and prints - `http://127.0.0.1:/auth/connect/`. Open that link (through the - tunnel) to be signed in, with the 30-day cookie, without typing the password. + one-time pairing code and prints the link twice, for the tunnel + (`http://127.0.0.1:/auth/connect/`) and for this host + (`http://127.H1.H2.S:/...`). Open one to be signed in, with the 30-day cookie, without typing the password. The link works once and expires after 5 minutes; after that, or if the printed link failed, click **Use password** and enter the password. The password reaches `curl` on its standard input (`-K -`), never on a command @@ -896,7 +910,9 @@ bwopencode2 --serve-ssh-config the host could drive them all. Prefer the random default. - **Port claims.** A file `~/.local/state/bwopencode2/serve/.session` (host side, not mounted in the sandbox; the port, launcher pid, host, directory and - start time, no secret) stops two launchers picking the same port. A claim + start time, no secret) stops two launchers picking the same port; the port + must also be free on the address checked (`127.H1.H2.1`, or `--publish-addr`). + A claim whose process is gone is stale and is removed on the next `--serve`. The launcher ends in `exec`, so it cannot retry or clean up afterwards; the remaining race is another program taking the port in the moment before the diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index 4f550a0..58b8e4e 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -3016,15 +3016,25 @@ _net_busy_port() { done } +# net_publish_prefix +# Print this user's publish prefix 127.H1.H2: H1.H2 from a hash of the uid +# (H1 in 1-254, so 127.0.x.x, 127.0.0.53 and 127.0.1.1 are never used). +# Session S of the user publishes on 127.H1.H2.S; S=1 is the usual one. +net_publish_prefix() { + local _hex _v + _hex="$(printf '%s' "$(id -u)" | sha256sum)" + _v=$((16#${_hex:0:4})) + printf '127.%s.%s' "$((1 + (_v >> 8) % 254))" "$((_v & 255))" +} + # _net_derive_publish_addr # Set _NET_PUBLISH_ADDR_EFFECTIVE for --publish. Without --publish-addr the -# address is 127.H1.H2.S: H1.H2 from a hash of the uid (H1 in 1-254, so -# 127.0.x.x, 127.0.0.53 and 127.0.1.1 are never used), S the lowest session +# address is 127.H1.H2.S (see net_publish_prefix), S the lowest session # index in 1-254 whose address has none of the requested ports in use. # With --publish-addr, that address must have them all free. Exit 1 when # no address can be used. --dry-run checks nothing and shows S=1. _net_derive_publish_addr() { - local _hex _v _h1 _h2 _s _cand + local _prefix _s _cand if [[ "${DRY_RUN}" -eq 0 ]]; then _net_listeners fi @@ -3039,17 +3049,14 @@ _net_derive_publish_addr() { return 0 fi - _hex="$(printf '%s' "$(id -u)" | sha256sum)" - _v=$((16#${_hex:0:4})) - _h1=$((1 + (_v >> 8) % 254)) - _h2=$((_v & 255)) + _prefix="$(net_publish_prefix)" if [[ "${DRY_RUN}" -eq 1 ]]; then - _NET_PUBLISH_ADDR_EFFECTIVE="127.${_h1}.${_h2}.1" + _NET_PUBLISH_ADDR_EFFECTIVE="${_prefix}.1" echo "${0##*/}: dry-run: session index 1 shown for the publish address; a real run takes the first free one." >&2 return 0 fi for ((_s = 1; _s <= 254; _s++)); do - _cand="127.${_h1}.${_h2}.${_s}" + _cand="${_prefix}.${_s}" _net_busy_port "${_cand}" if [[ -z "${_NET_BUSY}" ]]; then _NET_PUBLISH_ADDR_EFFECTIVE="${_cand}" @@ -3060,8 +3067,8 @@ _net_derive_publish_addr() { exit 1 fi done - echo "Error: --publish: all 254 publish addresses 127.${_h1}.${_h2}.1-254 have a requested port in use." >&2 - echo " This usually means leftover sandbox (pasta) processes; see: ss -ltnp 'src 127.${_h1}.${_h2}.0/24'" >&2 + echo "Error: --publish: all 254 publish addresses ${_prefix}.1-254 have a requested port in use." >&2 + echo " This usually means leftover sandbox (pasta) processes; see: ss -ltnp 'src ${_prefix}.0/24'" >&2 exit 1 } From ee7dc8cfb6b88b1cab63d776f04511d04bb22042 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 9 Nov 2026 22:34:57 -0500 Subject: [PATCH 12/37] fix(bwrap): keep network launch files in a checked runtime directory The slirp4netns pipes and API socket were in a mktemp directory under XDG_RUNTIME_DIR or TMPDIR, whose parent was not checked: another user able to write it could swap the directory during the launch. They now go in a per-launch session directory under $XDG_RUNTIME_DIR/bwrap-net, else /tmp/bwrap-net-UID, used only when no other user can change its parent and it is the user's own 0700 directory. TMPDIR is never used. Both locations are blocked paths, so no sandbox of the user can mount them and reach another sandbox's API socket. Assisted-by: opencode:claude-opus-5-5 --- docs/bw-wrappers.md | 27 +++++++++-- lib/bwrap_sandbox_lib.sh | 100 +++++++++++++++++++++++++++++++++++---- 2 files changed, 114 insertions(+), 13 deletions(-) diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 4ed1acf..4d30216 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -465,14 +465,35 @@ launcher ─┬─ helper (background): waits for the namespace, starts slirp4ne scrubbed environment (no API keys). It exits when bwrap does: its `--exit-fd` is the other end of bwrap's `--sync-fd`. - `--publish` uses slirp4netns's API socket (`add_hostfwd`, one request per - port). The socket is in a private `mktemp -d` directory under - `$XDG_RUNTIME_DIR`, removed once the forwards are added, so nothing else can - add forwards later. + port). The socket is in the launch's session directory (see + [Runtime directory](#runtime-directory)), removed once the forwards are + added, so nothing else can add forwards later. - **On a setup failure** (slirp4netns does not start, a port cannot be published) the helper prints the reason and slirp4netns's last messages, kills the still-waiting sandbox (its pid comes from bwrap's `--info-fd`), so the tool never runs, and then bwrap. The exit status is then 137, not 3. +### Runtime directory + +The launcher keeps its network files (the slirp4netns pipes and API socket, +and the claims and logs described elsewhere in this section) in a directory of +its own, outside every sandbox: + +- `$XDG_RUNTIME_DIR/bwrap-net`, else `/tmp/bwrap-net-`, mode 0700. A + candidate is used only if its parent cannot be changed by other users (owned + by you or root, and not writable by group or others, or sticky like `/tmp`) + and it is then a real directory of yours with mode 0700; otherwise the next + one is tried, and with none the launch stops (exit status 3). `TMPDIR` is + never used: in a directory other users can write, they could swap the + directory during the launch. +- Each launch works in its own `session.XXXXXXXX` subdirectory (`mktemp -d`), so + several sandboxes of one user never share files there, and removes it when + it no longer needs it. +- Both candidates are blocked paths: no sandbox can mount them, or a directory + that contains them, as its working directory or with `--ro-path` / + `--rw-path`. One sandbox therefore cannot reach the files of another, such as + the slirp4netns API socket during its setup. + ### Descriptor relay bwrap reads some files from descriptors: `--file` for writable copies of diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index 58b8e4e..0385718 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -1090,6 +1090,9 @@ _bwrap_load_blocked_prefixes() { "/etc/sudoers" "/etc/sudoers.d" ) + # The launcher's network runtime directories (slirp4netns API sockets, + # publish claims, network log pipes of every sandbox of this user). + mapfile -t -O "${#_literal[@]}" _literal < <(_net_runtime_candidates) local _e _r _BWRAP_BLOCKED_LIST=() for _e in "${_literal[@]}"; do @@ -2482,6 +2485,84 @@ _net_try_pasta() { return 0 } +# ── Per-user runtime directory ─────────────────────────────────── +# The launcher keeps its network files (slirp4netns pipes and API socket, the +# claims of publish addresses, the network log pipe) in one directory per +# user, outside every sandbox: +# +# $XDG_RUNTIME_DIR/bwrap-net/ (else /tmp/bwrap-net-UID) 0700 +# ├── session.XXXXXXXX/ one per launch (mktemp -d) +# └── ... +# +# Each launch works in its own session directory, so concurrent launches of +# one user do not meet there. Only the launcher (outside the sandbox) uses +# the directory, and it is in _BWRAP_BLOCKED_LIST, so no sandbox can mount it +# (by working directory, --ro-path or --rw-path) and reach another sandbox's +# files. TMPDIR is never used: its parent may be writable by other users, who +# could then swap the directory during the launch. + +_NET_RUNTIME_BASE="" +_NET_SESSION_DIR="" + +# _net_runtime_candidates +# Print the base directory candidates, best first, one per line: +# $XDG_RUNTIME_DIR/bwrap-net (when XDG_RUNTIME_DIR is set) and +# /tmp/bwrap-net-UID. Used by _net_runtime_base and the blocked list. +_net_runtime_candidates() { + [[ -z "${XDG_RUNTIME_DIR:-}" ]] || printf '%s/bwrap-net\n' "${XDG_RUNTIME_DIR%/}" + printf '/tmp/bwrap-net-%s\n' "$(id -u)" +} + +# _net_dir_is_trusted_parent DIR +# True when no other user can rename or replace entries of DIR: a real +# directory (not a symlink) owned by this user or root, and either not +# writable by group or others, or sticky (as /tmp is). +_net_dir_is_trusted_parent() { + local _d="$1" _st _owner _mode + [[ -d "${_d}" && ! -L "${_d}" ]] || return 1 + _st="$(stat -c '%u %a' -- "${_d}" 2> /dev/null)" || return 1 + _owner="${_st% *}" + _mode=$((8#${_st#* })) + [[ "${_owner}" == "$(id -u)" || "${_owner}" == 0 ]] || return 1 + ((!(_mode & 8#022) || (_mode & 8#1000))) +} + +# _net_runtime_base +# Set _NET_RUNTIME_BASE to this user's runtime directory, creating it +# (mode 0700). The first candidate whose parent is trusted is used, and +# it must then be a real directory owned by this user with mode 0700. +# Exit 3 when there is none. +_net_runtime_base() { + [[ -z "${_NET_RUNTIME_BASE}" ]] || return 0 + local _c _st _why="" + while IFS= read -r _c; do + if ! _net_dir_is_trusted_parent "${_c%/*}"; then + _why+="${_why:+; }${_c%/*} is not a private or sticky directory of this user or root" + continue + fi + mkdir -m 0700 -- "${_c}" 2> /dev/null || true + _st="$(stat -c '%u %a' -- "${_c}" 2> /dev/null || true)" + if [[ -d "${_c}" && ! -L "${_c}" && "${_st}" == "$(id -u) 700" ]]; then + _NET_RUNTIME_BASE="${_c}" + return 0 + fi + _why+="${_why:+; }${_c} exists but is not a directory of this user with mode 0700" + done < <(_net_runtime_candidates) + echo "Error: no private runtime directory for the network setup: ${_why}." >&2 + exit 3 +} + +# _net_session_dir +# Set _NET_SESSION_DIR to a new private directory for this launch. +_net_session_dir() { + [[ -z "${_NET_SESSION_DIR}" ]] || return 0 + _net_runtime_base + if ! _NET_SESSION_DIR="$(mktemp -d "${_NET_RUNTIME_BASE}/session.XXXXXXXX")"; then + echo "Error: cannot create a session directory in ${_NET_RUNTIME_BASE}." >&2 + exit 3 + fi +} + # ── slirp4netns backend ────────────────────────────────────────── # Used when pasta is not usable (RHEL 8 has slirp4netns but no passt). # slirp4netns cannot start the command itself; it attaches to an existing @@ -2676,17 +2757,13 @@ _net_slirp_publish_fwds() { # _net_slirp_start # Called by launch_sandbox just before it execs bwrap: create the pipes -# (FIFOs in a private directory, removed once open), start the helper in -# the background and close the helper's descriptors in the launcher. +# (FIFOs in a private session directory, removed once open), start the +# helper in the background and close the helper's descriptors in the +# launcher. _net_slirp_start() { - local _rt="${XDG_RUNTIME_DIR:-}" _f - if [[ -z "${_rt}" || ! -d "${_rt}" || ! -w "${_rt}" || ! -O "${_rt}" ]]; then - _rt="${TMPDIR:-/tmp}" - fi - if ! _NET_SLIRP_DIR="$(mktemp -d "${_rt}/bwrap-net.XXXXXXXX")"; then - echo "Error: cannot create a private directory in ${_rt} for slirp4netns." >&2 - exit 3 - fi + local _f + _net_session_dir + _NET_SLIRP_DIR="${_NET_SESSION_DIR}" for _f in info block sync ready; do mkfifo -m 0600 "${_NET_SLIRP_DIR}/${_f}" done @@ -2700,7 +2777,10 @@ _net_slirp_start() { exec 95< "${_NET_SLIRP_DIR}/sync" exec 96<> "${_NET_SLIRP_DIR}/ready" exec 97< "${_NET_SLIRP_DIR}/ready" + # noclobber: the log is a new file, never an existing one. + set -o noclobber exec 98> "${_NET_SLIRP_DIR}/log" + set +o noclobber exec 99< "${_NET_SLIRP_DIR}/log" rm -f "${_NET_SLIRP_DIR}/info" "${_NET_SLIRP_DIR}/block" "${_NET_SLIRP_DIR}/sync" \ "${_NET_SLIRP_DIR}/ready" "${_NET_SLIRP_DIR}/log" From 5e512253173774911b64ced86d7d121f389df024 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 9 Nov 2026 22:36:10 -0500 Subject: [PATCH 13/37] fix(bwrap): signal only the launcher's own processes on slirp failure The slirp4netns helper kept polling after the launcher had gone (for example when unshare failed) and then sent SIGKILL to the saved pid, which by then could belong to another process. It now exits quietly as soon as its parent is no longer the launcher, and before killing it checks that the launcher is still its parent and that the sandbox pid is the launcher's child. Assisted-by: opencode:claude-opus-5-5 --- docs/bw-wrappers.md | 4 +++ lib/bwrap_sandbox_lib.sh | 57 +++++++++++++++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 4d30216..decc7b5 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -472,6 +472,10 @@ launcher ─┬─ helper (background): waits for the namespace, starts slirp4ne published) the helper prints the reason and slirp4netns's last messages, kills the still-waiting sandbox (its pid comes from bwrap's `--info-fd`), so the tool never runs, and then bwrap. The exit status is then 137, not 3. + The helper signals a process only after checking that it is the launcher + (the helper's own parent) or a child of the launcher. If the launcher is + already gone (`unshare` or the descriptor relay failed and printed why), the + helper notices within a poll interval, signals nothing, and exits. ### Runtime directory diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index 0385718..022fe13 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -2797,7 +2797,7 @@ _net_slirp_start() { # Runs in a subshell; $$ is still the launcher, which is bwrap by then. # _ENV_BIN is launch_sandbox's local (bash scoping is dynamic). _net_slirp_helper() { - local _own _ns="" _map="" _pid="$$" _r _out _i + local _own _ns="" _map="" _pid="$$" _r="" _rc _out _i local -a _api=() set +e exec 90>&- 91<&- 92>&- @@ -2807,6 +2807,7 @@ _net_slirp_helper() { # namespace of its own, with the uid map written. _own="$(readlink /proc/self/ns/net)" for ((_i = 0; _i < 500; _i++)); do + _net_launcher_alive || _net_slirp_quit _ns="$(readlink "/proc/${_pid}/ns/net" 2> /dev/null)" _map="$(cat "/proc/${_pid}/uid_map" 2> /dev/null)" [[ -z "${_ns}" || "${_ns}" == "${_own}" || -z "${_map}" ]] || break @@ -2820,7 +2821,14 @@ _net_slirp_helper() { _net_slirp_run "${_pid}" ${_api[@]+"${_api[@]}"} > /dev/null 2>&98 93<&- 94>&- 97<&- 99<&- & exec 95<&- 96>&- 98>&- - if ! IFS= read -r -t 30 -n 1 _r <&97 || [[ "${_r}" != 1 ]]; then + # Wait (up to 30 s) for slirp4netns; stop waiting if the launcher is gone. + for ((_i = 0; _i < 30; _i++)); do + IFS= read -r -t 1 -n 1 _r <&97 + _rc=$? + ((_rc > 128)) || break + _net_launcher_alive || _net_slirp_quit + done + if [[ "${_r}" != 1 ]]; then _net_slirp_fail "slirp4netns did not become ready" fi if [[ ${#_NET_SLIRP_FWDS[@]} -gt 0 ]]; then @@ -2847,14 +2855,46 @@ _net_slirp_run() { --ready-fd=96 --exit-fd=95 "$@" "${_pid}" tap0 } +# _net_ppid PID +# Print the parent pid of PID (from /proc/PID/stat; empty when PID is gone). +_net_ppid() { + local _st _f + IFS= read -r _st < "/proc/$1/stat" 2> /dev/null || return 0 + # Fields after the command name, which may contain spaces: state ppid ... + read -r -a _f <<< "${_st##*) }" + printf '%s' "${_f[1]}" +} + +# _net_launcher_alive +# True while the calling helper's parent is still the launcher process +# ($$; it keeps that pid through exec). While it is our parent the pid +# cannot have been reused, so it is safe to signal. Once it has gone, we +# are re-parented and this is false. +_net_launcher_alive() { + # BASHPID is read here: inside $(...) it would be the substitution's pid. + local _me="${BASHPID}" + [[ "$(_net_ppid "${_me}")" == "$$" ]] +} + +# _net_slirp_quit +# The launcher is gone (unshare or the relay failed and has said why): +# clean up and exit without signalling anything. +_net_slirp_quit() { + [[ -z "${_NET_SLIRP_DIR}" ]] || rm -rf "${_NET_SLIRP_DIR}" + exit 0 +} + # _net_slirp_fail MESSAGE # Report a failed network setup, kill the still-blocked sandbox (the tool # has not started; its pid is read from bwrap's --info-fd) and then the # launcher process ($$, by now bwrap), and exit the helper while it still # holds the --block-fd pipe. slirp4netns, if started, exits with bwrap -# (--exit-fd). +# (--exit-fd). Only processes whose parent is checked are signalled: the +# sandbox must be a child of the launcher, the launcher still ours; +# otherwise (the launcher already gone) nothing is signalled. _net_slirp_fail() { - local _msg="$1" _info="" _line + local _msg="$1" _info="" _line _child + _net_launcher_alive || _net_slirp_quit printf '%s: network setup failed (slirp4netns): %s\n' "${0##*/}" "${_msg}" >&2 tail -n 5 <&99 2> /dev/null | sed 's/^/ /' >&2 printf ' The sandbox was stopped. Run with --share-net to start without network isolation.\n' >&2 @@ -2863,9 +2903,14 @@ _net_slirp_fail() { [[ "${_line}" != *"}"* ]] || break done if [[ "${_info}" =~ \"child-pid\":\ *([0-9]+) ]]; then - kill -KILL "${BASH_REMATCH[1]}" 2> /dev/null + _child="${BASH_REMATCH[1]}" + if [[ "$(_net_ppid "${_child}")" == "$$" ]]; then + kill -KILL "${_child}" 2> /dev/null + fi + fi + if _net_launcher_alive; then + kill -KILL "$$" 2> /dev/null fi - kill -KILL "$$" 2> /dev/null [[ -z "${_NET_SLIRP_DIR}" ]] || rm -rf "${_NET_SLIRP_DIR}" exit 1 } From 7d24a2fa54b7f3c8a752115bd6f10c77b53ce290 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 9 Nov 2026 22:37:41 -0500 Subject: [PATCH 14/37] fix(bwrap): claim publish addresses under a lock The publish address came from one ss snapshot, and the backend binds it only after exec, so two launches at the same moment could pick the same address and port. The choice is now made under flock in the runtime directory, and each launch leaves a claim (launcher pid and start time, host ports) that later launches respect; claims whose process is gone are removed. --serve port claims also record the start time, so a reused pid does not keep them alive. Assisted-by: opencode:claude-opus-5-5 --- bin/bwopencode2 | 4 +- docs/bw-wrappers.md | 12 +++- lib/bwrap_sandbox_lib.sh | 140 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 149 insertions(+), 7 deletions(-) diff --git a/bin/bwopencode2 b/bin/bwopencode2 index 73c254e..3c9e52f 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -585,7 +585,8 @@ serve_claim_is_live() { [[ -e "${_f}" ]] || return 1 _pid="$(sed -n 's/^pid=//p' "${_f}" 2> /dev/null | head -n 1)" if [[ "${_pid}" =~ ^[0-9]+$ ]]; then - kill -0 "${_pid}" 2> /dev/null + # The start time guards against a reused pid (see bwrap_proc_is). + bwrap_proc_is "${_pid}" "$(sed -n 's/^start=//p' "${_f}" 2> /dev/null | head -n 1)" return fi _mtime="$(stat -c %Y "${_f}" 2> /dev/null || echo 0)" @@ -604,6 +605,7 @@ serve_claim_port() { (set -o noclobber && : > "${_f}") 2> /dev/null || return 1 { printf 'pid=%s\n' "$$" + printf 'start=%s\n' "$(bwrap_proc_start_time "$$")" printf 'port=%s\n' "${_port}" printf 'host=%s\n' "$(serve_host_name)" printf 'dir=%s\n' "${_PWD}" diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index decc7b5..6f1c54b 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -379,11 +379,21 @@ interface, not its loopback; the context file tells the agent so. A port that something on the host already holds on every address (`0.0.0.0:PORT`) is refused at launch. +The backend binds the address only after the launcher has started it, so the +choice is made under a lock (`flock` on `claims.lock` in the +[runtime directory](#runtime-directory)), and each launch leaves a claim +(`claims/ADDR.PID`: launcher pid and its start time, host ports, launcher, +directory). Sandboxes of one user started at the same moment therefore get +different addresses. A claim whose process is gone (checked with the start +time, so a reused pid does not count) is removed by the next launch. A program +outside the launchers can still take a port first; pasta and slirp4netns then +fail to bind it and the launch stops. + **`--publish-addr ADDR`** replaces the derived address with `ADDR` (in `127.0.0.0/8`). Use `127.0.0.1` for OAuth flows whose registered redirect URI names it (some agent logins and MCP servers open a browser on the host that calls back into the sandbox). The launch then fails if a requested port is -already in use on `ADDR`. +already in use or claimed by another of your sandboxes on `ADDR`. **`--host-port`** makes one host loopback service visible in the sandbox as `host.internal`, so the agent's own `127.0.0.1:PORT` stays free (it can run its diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index 022fe13..fd0a325 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -3117,16 +3117,130 @@ _net_listeners() { done < <(ss -Hltn 2> /dev/null || true) } +# ── Publish claims ─────────────────────────────────────────────── +# Choosing a publish address reads the listeners once (ss), and the backend +# binds the address only later, after the launcher has exec'd. Two launches +# of one user at the same moment could therefore pick the same address and +# port. So the choice is made under a lock, and each launch records what it +# chose in a claim file that later launches respect: +# +# /claims.lock flock, held only while choosing +# /claims/ADDR.PID pid=, start=, ports= (host ports) +# +# PID is the launcher's pid, which stays the pid of pasta (or of bwrap with +# slirp4netns) for the whole session; start= is its start time, so a reused +# pid does not keep a claim alive. A claim whose process is gone is stale and +# is removed by the next launch. Nothing is removed at exit (the launcher has +# exec'd). Not covered: a program outside the launchers binding the address +# first; the backend then fails to bind and the launch stops. + +_NET_CLAIMS_DIR="" +_NET_CLAIMS_LOCK_FD="" + +# bwrap_proc_start_time PID +# Print the start time of PID (field 22 of /proc/PID/stat, in clock ticks +# since boot); empty when PID is gone. +bwrap_proc_start_time() { + local _st _f + IFS= read -r _st < "/proc/$1/stat" 2> /dev/null || return 0 + read -r -a _f <<< "${_st##*) }" + printf '%s' "${_f[19]}" +} + +# bwrap_proc_is PID START +# True when PID runs and started at START (see bwrap_proc_start_time). +bwrap_proc_is() { + [[ "$1" =~ ^[0-9]+$ && -n "$2" && "$(bwrap_proc_start_time "$1")" == "$2" ]] +} + +# _net_claims_begin +# Take the claims lock (waiting up to 10 s) and remove stale claims. +_net_claims_begin() { + local _f _pid _start + _net_runtime_base + _NET_CLAIMS_DIR="${_NET_RUNTIME_BASE}/claims" + [[ -d "${_NET_CLAIMS_DIR}" ]] || mkdir -m 0700 -- "${_NET_CLAIMS_DIR}" + if command -v flock > /dev/null 2>&1; then + exec {_NET_CLAIMS_LOCK_FD}>> "${_NET_RUNTIME_BASE}/claims.lock" + if ! flock -w 10 "${_NET_CLAIMS_LOCK_FD}"; then + echo "Error: --publish: another launch holds ${_NET_RUNTIME_BASE}/claims.lock." >&2 + exit 1 + fi + else + echo "Warning: flock (util-linux) not found: two launches at the same moment may pick the same publish address." >&2 + fi + for _f in "${_NET_CLAIMS_DIR}"/*; do + [[ -f "${_f}" ]] || continue + _pid="$(sed -n 's/^pid=//p' "${_f}" 2> /dev/null)" + _start="$(sed -n 's/^start=//p' "${_f}" 2> /dev/null)" + bwrap_proc_is "${_pid}" "${_start}" || rm -f -- "${_f}" + done +} + +# _net_claimed_port ADDR +# Set _NET_BUSY / _NET_BUSY_PID to the first published host port that a +# live claim of another launch holds on ADDR (claims are pruned by +# _net_claims_begin). Does nothing outside _net_claims_begin/_end. +_net_claimed_port() { + local _want="$1" _f _pid _ports _r _lo _hi _i + [[ -n "${_NET_CLAIMS_DIR}" ]] || return 0 + for _f in "${_NET_CLAIMS_DIR}/${_want}".*; do + [[ -f "${_f}" ]] || continue + _pid="$(sed -n 's/^pid=//p' "${_f}" 2> /dev/null)" + [[ "${_pid}" != "$$" ]] || continue + _ports="$(sed -n 's/^ports=//p' "${_f}" 2> /dev/null)" + for _r in ${_ports}; do + _lo="${_r%-*}" + _hi="${_r#*-}" + for _i in "${!_NET_PUB_LO[@]}"; do + if ((_lo <= _NET_PUB_HI[_i] && _hi >= _NET_PUB_LO[_i])); then + _NET_BUSY="$((_lo > _NET_PUB_LO[_i] ? _lo : _NET_PUB_LO[_i]))" + _NET_BUSY_PID="${_pid}" + return 0 + fi + done + done + done +} + +# _net_claims_write +# Record this launch's claim on _NET_PUBLISH_ADDR_EFFECTIVE. +_net_claims_write() { + local _i _ports="" + for _i in "${!_NET_PUB_LO[@]}"; do + _ports+="${_ports:+ }${_NET_PUB_LO[${_i}]}-${_NET_PUB_HI[${_i}]}" + done + printf 'pid=%s\nstart=%s\nports=%s\nlauncher=%s\ndir=%s\n' \ + "$$" "$(bwrap_proc_start_time "$$")" "${_ports}" "${0##*/}" "${PWD}" \ + > "${_NET_CLAIMS_DIR}/${_NET_PUBLISH_ADDR_EFFECTIVE}.$$" +} + +# _net_claims_end +# Release the claims lock. +_net_claims_end() { + if [[ -n "${_NET_CLAIMS_LOCK_FD}" ]]; then + exec {_NET_CLAIMS_LOCK_FD}>&- + _NET_CLAIMS_LOCK_FD="" + fi + _NET_CLAIMS_DIR="" +} + # _net_busy_port ADDR # Set _NET_BUSY to the first published host port that a listener already -# holds on ADDR (or on a wildcard address), empty when none. _NET_BUSY_WILD -# is 1 when that listener is a wildcard one. +# holds on ADDR (or on a wildcard address), or that a live claim of another +# launch holds on ADDR; empty when none. _NET_BUSY_WILD is 1 when that +# listener is a wildcard one; _NET_BUSY_PID is the claiming launcher's pid +# when it is a claim. _NET_BUSY="" _NET_BUSY_WILD=0 +_NET_BUSY_PID="" _net_busy_port() { local _want="$1" _l _a _p _i _NET_BUSY="" _NET_BUSY_WILD=0 + _NET_BUSY_PID="" + _net_claimed_port "${_want}" + [[ -z "${_NET_BUSY}" ]] || return 0 for _l in ${_NET_LISTEN[@]+"${_NET_LISTEN[@]}"}; do _a="${_l% *}" _p="${_l##* }" @@ -3159,14 +3273,30 @@ net_publish_prefix() { # With --publish-addr, that address must have them all free. Exit 1 when # no address can be used. --dry-run checks nothing and shows S=1. _net_derive_publish_addr() { - local _prefix _s _cand - if [[ "${DRY_RUN}" -eq 0 ]]; then - _net_listeners + if [[ "${DRY_RUN}" -eq 1 ]]; then + _net_choose_publish_addr + return 0 fi + _net_claims_begin + _net_listeners + _net_choose_publish_addr + _net_claims_write + _net_claims_end +} + +# _net_choose_publish_addr +# The choice for _net_derive_publish_addr, against the listeners and (on a +# real run) the claims of the user's other launches. +_net_choose_publish_addr() { + local _prefix _s _cand if [[ -n "${NET_PUBLISH_ADDR}" ]]; then _NET_PUBLISH_ADDR_EFFECTIVE="${NET_PUBLISH_ADDR}" [[ "${DRY_RUN}" -eq 0 ]] || return 0 _net_busy_port "${NET_PUBLISH_ADDR}" + if [[ -n "${_NET_BUSY_PID}" ]]; then + echo "Error: --publish: port ${_NET_BUSY} on ${NET_PUBLISH_ADDR} is claimed by another of your sandboxes (launcher pid ${_NET_BUSY_PID})." >&2 + exit 1 + fi if [[ -n "${_NET_BUSY}" ]]; then echo "Error: --publish: port ${_NET_BUSY} is already in use on ${NET_PUBLISH_ADDR} (see: ss -ltnp)." >&2 exit 1 From c6128350933c7283d59d3a17a7693ecbbc39f818 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 9 Nov 2026 22:38:01 -0500 Subject: [PATCH 15/37] docs(bwrap): describe the backend choice in the library header The header still said that without pasta the sandbox shares the host network with a warning; the launch tries slirp4netns and otherwise stops unless --share-net is given. Assisted-by: opencode:claude-opus-5-5 --- docs/bw-wrappers.md | 8 +++++--- lib/bwrap_sandbox_lib.sh | 7 ++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 6f1c54b..63d8927 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -944,9 +944,11 @@ bwopencode2 --serve-ssh-config fixed one it reaches every session that shares it, and any sandboxed agent on the host could drive them all. Prefer the random default. - **Port claims.** A file `~/.local/state/bwopencode2/serve/.session` (host - side, not mounted in the sandbox; the port, launcher pid, host, directory and - start time, no secret) stops two launchers picking the same port; the port - must also be free on the address checked (`127.H1.H2.1`, or `--publish-addr`). + side, not mounted in the sandbox; the port, launcher pid and its process + start time, host, directory and start date, no secret) stops two launchers + picking the same port; the port must also be free on the address checked + (`127.H1.H2.1`, or `--publish-addr`), and the address is claimed as for any + `--publish`. A claim whose process is gone is stale and is removed on the next `--serve`. The launcher ends in `exec`, so it cannot retry or clean up afterwards; the diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index fd0a325..cdc2d81 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -34,9 +34,10 @@ # # Requires: bwrap >= 0.4.0 (RHEL 8) # Binary masking is verified on RHEL 8.10 (bubblewrap-0.4.0-3.el8_10) and EL9 (0.6.3). -# Optional: pasta (package passt), with /dev/net/tun and unprivileged user -# namespaces on the host, for the private network namespace. Without it -# (RHEL 8) the sandbox shares the host network, with a warning. +# Network: a private network namespace from pasta (package passt, RHEL 9+), +# else slirp4netns (RHEL 8) with util-linux unshare; both need /dev/net/tun +# and unprivileged user namespaces. Without either the launch stops +# (exit 3) unless --share-net is given (see "Network isolation"). # Guard against double-sourcing. [[ -n "${_BWRAP_SANDBOX_LIB_SOURCED:-}" ]] && return 0 From 580d8aa81dc550ab0c3a63bfc3e7a519ded1e07d Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 9 Nov 2026 22:40:45 -0500 Subject: [PATCH 16/37] refactor(bwrap): remove --net-log FILE pasta writes one log file, and the network log becomes an always-on journal record in the next commit; a file the user names is easier to edit, lose, or leave where the agent can write it. Assisted-by: opencode:claude-opus-5-5 --- README.md | 1 - bin/bwclaude | 11 +----- bin/bwcodex | 11 +----- bin/bwcopilot | 11 +----- bin/bwopencode | 11 +----- bin/bwopencode2 | 11 +----- docs/bw-wrappers.md | 16 +++------ lib/bwrap_sandbox_lib.sh | 73 +++++++--------------------------------- 8 files changed, 21 insertions(+), 124 deletions(-) diff --git a/README.md b/README.md index b90d255..53830cd 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,6 @@ Common wrapper options: | `--publish PORT[:SANDBOXPORT]` | Make a server in the sandbox reachable on the host at a per-user, per-session `127.x.y.z` address (printed at launch) | | `--publish-addr ADDR` | Use `ADDR` (in `127.0.0.0/8`, e.g. `127.0.0.1` for OAuth) for `--publish` | | `--host-port PORT[:SANDBOXPORT]` | Make the host's `127.0.0.1:PORT` reachable in the sandbox at `host.internal` | -| `--net-log FILE` | Log every network flow to `FILE` on the host | | `--share-net` | Share the host network instead of a private namespace (the behaviour before private namespaces) | | `--net-backend pasta\|slirp4netns` | Use only this network backend | diff --git a/bin/bwclaude b/bin/bwclaude index fd079b6..41304cd 100755 --- a/bin/bwclaude +++ b/bin/bwclaude @@ -94,14 +94,13 @@ # --host-port PORT[:SANDBOXPORT] # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. -# --net-log FILE Log every network flow to FILE on the host. # --share-net Share the host network instead (the behaviour before # private namespaces); not with the options above. # --net-backend pasta|slirp4netns # Use only this backend for the private namespace. # # Network: the sandbox has its own network namespace from pasta (package -# passt), or else slirp4netns (--host-port and --net-log need pasta). +# passt), or else slirp4netns (--host-port needs pasta). # Outbound connections work; services on the host's loopback are unreachable # and the sandbox's listeners are invisible to the host, except for the ports # opened with --publish and --host-port. Without a usable private namespace @@ -221,14 +220,6 @@ parse_wrapper_args() { net_set_backend "$2" shift 2 ;; - --net-log) - if [[ $# -lt 2 ]]; then - echo "Error: --net-log requires a file name." >&2 - exit 1 - fi - net_set_log_file "$2" - shift 2 - ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 diff --git a/bin/bwcodex b/bin/bwcodex index 31cb805..24cfb0a 100755 --- a/bin/bwcodex +++ b/bin/bwcodex @@ -111,14 +111,13 @@ # --host-port PORT[:SANDBOXPORT] # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. -# --net-log FILE Log every network flow to FILE on the host. # --share-net Share the host network instead (the behaviour before # private namespaces); not with the options above. # --net-backend pasta|slirp4netns # Use only this backend for the private namespace. # # Network: the sandbox has its own network namespace from pasta (package -# passt), or else slirp4netns (--host-port and --net-log need pasta). +# passt), or else slirp4netns (--host-port needs pasta). # Outbound connections work; services on the host's loopback are unreachable # and the sandbox's listeners are invisible to the host, except for the ports # opened with --publish and --host-port. Without a usable private namespace @@ -243,14 +242,6 @@ parse_wrapper_args() { net_set_backend "$2" shift 2 ;; - --net-log) - if [[ $# -lt 2 ]]; then - echo "Error: --net-log requires a file name." >&2 - exit 1 - fi - net_set_log_file "$2" - shift 2 - ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 diff --git a/bin/bwcopilot b/bin/bwcopilot index 2fc5554..49273a2 100755 --- a/bin/bwcopilot +++ b/bin/bwcopilot @@ -79,14 +79,13 @@ # --host-port PORT[:SANDBOXPORT] # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. -# --net-log FILE Log every network flow to FILE on the host. # --share-net Share the host network instead (the behaviour before # private namespaces); not with the options above. # --net-backend pasta|slirp4netns # Use only this backend for the private namespace. # # Network: the sandbox has its own network namespace from pasta (package -# passt), or else slirp4netns (--host-port and --net-log need pasta). +# passt), or else slirp4netns (--host-port needs pasta). # Outbound connections work; services on the host's loopback are unreachable # and the sandbox's listeners are invisible to the host, except for the ports # opened with --publish and --host-port. Without a usable private namespace @@ -201,14 +200,6 @@ parse_wrapper_args() { net_set_backend "$2" shift 2 ;; - --net-log) - if [[ $# -lt 2 ]]; then - echo "Error: --net-log requires a file name." >&2 - exit 1 - fi - net_set_log_file "$2" - shift 2 - ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 diff --git a/bin/bwopencode b/bin/bwopencode index b5dfe61..ebcbb27 100755 --- a/bin/bwopencode +++ b/bin/bwopencode @@ -91,14 +91,13 @@ # --host-port PORT[:SANDBOXPORT] # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. -# --net-log FILE Log every network flow to FILE on the host. # --share-net Share the host network instead (the behaviour before # private namespaces); not with the options above. # --net-backend pasta|slirp4netns # Use only this backend for the private namespace. # # Network: the sandbox has its own network namespace from pasta (package -# passt), or else slirp4netns (--host-port and --net-log need pasta). +# passt), or else slirp4netns (--host-port needs pasta). # Outbound connections work; services on the host's loopback are unreachable # and the sandbox's listeners are invisible to the host, except for the ports # opened with --publish and --host-port. Without a usable private namespace @@ -213,14 +212,6 @@ parse_wrapper_args() { net_set_backend "$2" shift 2 ;; - --net-log) - if [[ $# -lt 2 ]]; then - echo "Error: --net-log requires a file name." >&2 - exit 1 - fi - net_set_log_file "$2" - shift 2 - ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 diff --git a/bin/bwopencode2 b/bin/bwopencode2 index 3c9e52f..8c83b05 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -133,14 +133,13 @@ # --host-port PORT[:SANDBOXPORT] # Make the host's 127.0.0.1:PORT reachable in the sandbox at # host.internal:SANDBOXPORT. May be repeated. -# --net-log FILE Log every network flow to FILE on the host. # --share-net Share the host network instead (the behaviour before # private namespaces); not with the options above. # --net-backend pasta|slirp4netns # Use only this backend for the private namespace. # # Network: the sandbox has its own network namespace from pasta (package -# passt), or else slirp4netns (--host-port and --net-log need pasta). +# passt), or else slirp4netns (--host-port needs pasta). # Outbound connections work; services on the host's loopback are unreachable # and the sandbox's listeners are invisible to the host, except for the ports # opened with --publish and --host-port. Without a usable private namespace @@ -301,14 +300,6 @@ parse_wrapper_args() { net_set_backend "$2" shift 2 ;; - --net-log) - if [[ $# -lt 2 ]]; then - echo "Error: --net-log requires a file name." >&2 - exit 1 - fi - net_set_log_file "$2" - shift 2 - ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 63d8927..ec4af58 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -106,7 +106,6 @@ Every `bw*` wrapper accepts these options: | `--publish PORT[:SANDBOXPORT]` | Make a server in the sandbox reachable on the host at a per-user, per-session loopback address. May be repeated. See [Network](#network) | | `--publish-addr ADDR` | Use `ADDR` (in `127.0.0.0/8`) for `--publish` instead of the derived address, for example `127.0.0.1` for OAuth redirects | | `--host-port PORT[:SANDBOXPORT]` | Make the host's `127.0.0.1:PORT` reachable in the sandbox at `host.internal:SANDBOXPORT`. May be repeated (pasta only) | -| `--net-log FILE` | Log every network flow to `FILE` on the host (`pasta --debug`; pasta only) | | `--share-net` | Share the host network instead of a private namespace (the behaviour before private namespaces); not with the forwarding options | | `--net-backend pasta\|slirp4netns` | Use only this backend for the private namespace (normally pasta, else slirp4netns) | @@ -329,11 +328,11 @@ So sandboxes are isolated by default, with one explicit escape hatch. | Outbound traffic leaves from | the host's address | the host's address | the host | | DNS | `169.254.1.1`, forwarded to the host's resolver | `192.168.254.3`, forwarded to the host's resolver | the host's `resolv.conf` | | `--publish`, `--publish-addr` | yes | yes (at most 64 ports) | no | -| `--host-port`, `--net-log` | yes | no: the launch stops (exit 3) rather than drop them | no | +| `--host-port` | yes | no: the launch stops (exit 3) rather than drop it | no | | Speed | close to native | slower (user-mode TCP/IP); fine for API calls | native | -With `--host-port` or `--net-log`, a host without a working pasta stops even -if slirp4netns would work. +With `--host-port`, a host without a working pasta stops even if slirp4netns +would work. DNS: the host's resolver is usually on loopback (`127.0.0.53`), so the sandbox gets a generated `/etc/resolv.conf` naming the backend's DNS address, which the @@ -414,13 +413,6 @@ root-managed `nftables` rules. To list the sandbox publications on a host: `ss -ltn 'src 127.0.0.0/8' | grep -v ' 127\.0\.'`. -### Network log - -`--net-log FILE` passes `--log-file FILE --debug` to pasta: every flow is -logged on the host, which is the data needed to write an egress allowlist. -pasta keeps the newest 1 MiB. The wrapper warns when `FILE` is inside a -read-write mount of the sandbox, where the agent could change it. - ### Implementation notes: pasta - pasta runs outside bwrap, as you, and starts the sandbox. Default pasta @@ -1100,5 +1092,5 @@ public function surface (sandbox construction, dynamic mount discovery, binary masking, environment scrubbing, `build_sandbox_context` with its `SANDBOX_CONTEXT_*` variables (see [Sandbox context](#sandbox-context)), and the network functions `detect_net_capabilities`, `net_add_publish`, -`net_add_host_port`, `net_set_publish_addr`, `net_set_log_file`, +`net_add_host_port`, `net_set_publish_addr`, `net_set_share`, `net_set_backend`, `net_print_help` and `build_net_isolation` (see [Network](#network)). diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index cdc2d81..a677cfb 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -24,9 +24,10 @@ # - main() orchestrating the build sequence — must call build_extra_path_mounts # after build_dynamic_tool_mounts, detect_net_capabilities after # detect_kernel_capabilities, and build_net_isolation after build_etc_mounts -# - --publish / --publish-addr / --host-port / --net-log in parse_wrapper_args, -# through net_add_publish, net_set_publish_addr, net_add_host_port and -# net_set_log_file; net_print_help in show_help +# - --publish / --publish-addr / --host-port / --share-net / --net-backend in +# parse_wrapper_args, through net_add_publish, net_set_publish_addr, +# net_add_host_port, net_set_share and net_set_backend; net_print_help in +# show_help # # Usage (in tool wrapper scripts): # SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" @@ -69,7 +70,7 @@ EXTRA_RW_PATHS=() # Private network namespace (see "Network isolation"). Filled by the # launchers' parse_wrapper_args through net_add_publish, net_add_host_port, -# net_set_publish_addr, net_set_log_file and net_set_share; turned into pasta +# net_set_publish_addr and net_set_share; turned into pasta # arguments by build_net_isolation. NET_SHARE=1 (--share-net) keeps the # sandbox on the host network; NET_BACKEND_FORCE (--net-backend) restricts the # choice to one backend. NET_PUBLISH and NET_HOST_PORTS hold validated specs @@ -77,7 +78,6 @@ EXTRA_RW_PATHS=() NET_PUBLISH=() NET_PUBLISH_ADDR="" NET_HOST_PORTS=() -NET_LOG_FILE="" NET_SHARE=0 NET_BACKEND_FORCE="" PASTA_ARGS=() @@ -2325,7 +2325,7 @@ in 1024-65535; N or LOW-HIGH ranges are accepted. --net-backend pasta|slirp4netns Use only this backend for the private namespace (normally pasta, else slirp4netns). slirp4netns supports --publish - but not --host-port or --net-log. + but not --host-port. --publish PORT[:SANDBOXPORT] Make a server inside the sandbox (port SANDBOXPORT, default PORT) reachable on the host at http://127.H1.H2.S:PORT/, an @@ -2342,7 +2342,6 @@ in 1024-65535; N or LOW-HIGH ranges are accepted. sees a loopback client and may trust it. May be repeated. Refused: 631 (CUPS), 2375-2376 (Docker), 6000-6063 (X11), 41112 (Tailscale), 49374 (OpenCode 2 service). - --net-log FILE Log every network flow (pasta --debug) to FILE on the host. NET_HELP_EOF } @@ -2385,14 +2384,13 @@ net_set_share() { } # _net_forward_options -# Print the forward options given (--publish, --host-port, --net-log, as a -# space-separated list), nothing when none. +# Print the forward options given (--publish, --publish-addr, --host-port, +# as a space-separated list), nothing when none. _net_forward_options() { local -a _opts=() [[ ${#NET_PUBLISH[@]} -eq 0 ]] || _opts+=(--publish) [[ -z "${NET_PUBLISH_ADDR}" ]] || _opts+=(--publish-addr) [[ ${#NET_HOST_PORTS[@]} -eq 0 ]] || _opts+=(--host-port) - [[ -z "${NET_LOG_FILE}" ]] || _opts+=(--net-log) printf '%s' "${_opts[*]}" } @@ -2434,8 +2432,8 @@ detect_net_capabilities() { _slirp_reason="not tried (--net-backend ${_want})" if [[ -z "${_want}" || "${_want}" == slirp4netns ]]; then - if [[ ${#NET_HOST_PORTS[@]} -gt 0 || -n "${NET_LOG_FILE}" ]]; then - _slirp_reason="does not support --host-port or --net-log (pasta only)" + if [[ ${#NET_HOST_PORTS[@]} -gt 0 ]]; then + _slirp_reason="does not support --host-port (pasta only)" elif _net_try_slirp; then _NET_BACKEND=slirp4netns _NET_PRIVATE=1 @@ -2608,7 +2606,7 @@ _net_session_dir() { # --enable-sandbox/--enable-seccomp as podman does, when /etc/resolv.conf # resolves inside /etc or /run (required by it). # --publish works through the API socket (add_hostfwd, IPv4 TCP, one port per -# request); --host-port and --net-log have no slirp4netns equivalent. +# request); --host-port has no slirp4netns equivalent. _SLIRP_BIN="" _SLIRP_ARGS=() @@ -3074,22 +3072,6 @@ net_set_publish_addr() { NET_PUBLISH_ADDR="$1" } -# net_set_log_file PATH -# --net-log: have pasta log every flow (debug level) to PATH on the host. -net_set_log_file() { - local _dir - if [[ -z "$1" ]]; then - echo "Error: --net-log requires a file name." >&2 - exit 1 - fi - _dir="$(dirname -- "$1")" - if [[ ! -d "${_dir}" || ! -w "${_dir}" ]]; then - echo "Error: --net-log: directory '${_dir}' does not exist or is not writable." >&2 - exit 1 - fi - NET_LOG_FILE="$(readlink -f -- "${_dir}")/${1##*/}" -} - # _net_listeners # Fill _NET_LISTEN ("ADDR PORT" lines) from the host's listening TCP sockets # (ss -Hltn). Wildcard addresses (0.0.0.0, *, ::) are written as `*`, and @@ -3336,7 +3318,7 @@ _net_join() { } # build_net_isolation -# Finish PASTA_ARGS from the --publish / --host-port / --net-log options, +# Finish PASTA_ARGS from the --publish / --host-port options, # add the bwrap namespace options pasta needs, generate /etc/resolv.conf and # /etc/hosts, and export BWRAP_NET_*. On the host network (see # detect_net_capabilities) only mount the host's resolv.conf and hosts and @@ -3438,10 +3420,6 @@ build_net_isolation() { done fi - if [[ -n "${NET_LOG_FILE}" ]]; then - PASTA_ARGS+=(--log-file "${NET_LOG_FILE}" --debug) - fi - # DNS: pasta forwards queries for _NET_DNS_FORWARD_ADDR, slirp4netns for # _NET_SLIRP_DNS, to the host's first nameserver. Only IPv4. if [[ -r /etc/resolv.conf ]] && @@ -3499,9 +3477,6 @@ _net_print_report() { printf '%s: host-port host %s -> sandbox %s (host service sees a loopback client)\n' \ "${_launcher}" "${_spec%%|*}" "${_spec#*|}" >&2 done - if [[ -n "${NET_LOG_FILE}" ]]; then - printf '%s: network log %s\n' "${_launcher}" "${NET_LOG_FILE}" >&2 - fi } # _net_require_configured @@ -3516,28 +3491,6 @@ _net_require_configured() { fi } -# _net_warn_log_visible -# Warn when the --net-log file lies under a read-write mount, where the -# agent could read or rewrite it. Called by print_dry_run and -# launch_sandbox, once every mount is known. -_net_warn_log_visible() { - [[ -n "${NET_LOG_FILE}" ]] || return 0 - local _i=0 _flag - while [[ ${_i} -lt ${#BWRAP_ARGS[@]} ]]; do - _flag="${BWRAP_ARGS[${_i}]}" - _bwrap_flag_arity "${_flag}" - case "${_flag}" in - --bind | --bind-try) - if _is_prefix_of "${BWRAP_ARGS[$((_i + 1))]}" "${NET_LOG_FILE}"; then - echo "Warning: --net-log: ${NET_LOG_FILE} is under a read-write mount (${BWRAP_ARGS[$((_i + 1))]}); the agent can read and change it." >&2 - return 0 - fi - ;; - esac - _i=$((_i + 1 + _BWRAP_FLAG_ARITY)) - done -} - # ── OpenCode config requirement ─────────────────────────────────── # require_opencode_config WRAPPER USER_CONFIG_DIR # Exit 2 unless at least a minimal OpenCode config exists: a system-wide @@ -5087,7 +5040,6 @@ _is_secret_env_name() { print_dry_run() { verify_n2c_state_not_exposed _net_require_configured - _net_warn_log_visible local _BWRAP_BIN _ENV_BIN _BASH_BIN _BWRAP_BIN="$(resolve_bwrap_bin)" _ENV_BIN="$(resolve_env_bin)" @@ -5213,7 +5165,6 @@ print_dry_run() { launch_sandbox() { verify_n2c_state_not_exposed _net_require_configured - _net_warn_log_visible local _BWRAP_BIN _ENV_BIN _BASH_BIN _n _BWRAP_BIN="$(resolve_bwrap_bin)" _ENV_BIN="$(resolve_env_bin)" From b071672f2fa2dc65fa88de8f02c050548da740f7 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 9 Nov 2026 22:47:32 -0500 Subject: [PATCH 17/37] feat(bwrap): always log network sessions and pasta flows to the system log Every launch writes start and end records (tag bwrap-net, a session id also given to the sandbox as BWRAP_SESSION) with systemd-cat, else logger; with neither it warns and runs without the log. There is no option to turn it off. With pasta every flow is logged too: pasta runs with --debug and a FIFO as --log-file (huge --log-size, as its rotation fails on a FIFO) and as its stderr, and a supervisor outside the sandbox copies each line to the log, holding the FIFO open so pasta never blocks. The descriptor relay prints a marker and reopens the user's stderr, so pasta's setup lines go to the log, and if pasta fails before the marker its lines are shown on the terminal. Assisted-by: opencode:claude-opus-5-5 --- README.md | 3 + bin/bwclaude | 4 +- bin/bwcodex | 4 +- bin/bwcopilot | 4 +- bin/bwopencode | 4 +- bin/bwopencode2 | 4 +- docs/bw-wrappers.md | 51 +++++++ lib/bwrap_sandbox_lib.sh | 319 ++++++++++++++++++++++++++++++++++++++- 8 files changed, 381 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 53830cd..7f86ec5 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,9 @@ Add those three exports to your `~/.bashrc` or `~/.zshrc` for persistence. with `/dev/net/tun` and unprivileged user namespaces, for the private network namespace of the `bw*` wrappers; without either they stop unless run with `--share-net` +- `systemd-cat` (with a running journal) or `logger`, for the `bw*` wrappers' + always-on network log (tag `bwrap-net`); without either they warn and run + without it - `gpg(1)` for `gpg-passwd.sh` - `openssl(1)` for `pemdecompose` - `python3 >= 3.9` (stdlib only, no pip dependencies) for `azoidcapp` diff --git a/bin/bwclaude b/bin/bwclaude index 41304cd..e59679f 100755 --- a/bin/bwclaude +++ b/bin/bwclaude @@ -104,7 +104,9 @@ # Outbound connections work; services on the host's loopback are unreachable # and the sandbox's listeners are invisible to the host, except for the ports # opened with --publish and --host-port. Without a usable private namespace -# the launch stops (exit 3) unless --share-net is given. +# the launch stops (exit 3) unless --share-net is given. Every session (with +# pasta every connection too) is recorded in the host's system log, tag +# bwrap-net; there is no option to turn this off. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are diff --git a/bin/bwcodex b/bin/bwcodex index 24cfb0a..9ed774d 100755 --- a/bin/bwcodex +++ b/bin/bwcodex @@ -121,7 +121,9 @@ # Outbound connections work; services on the host's loopback are unreachable # and the sandbox's listeners are invisible to the host, except for the ports # opened with --publish and --host-port. Without a usable private namespace -# the launch stops (exit 3) unless --share-net is given. +# the launch stops (exit 3) unless --share-net is given. Every session (with +# pasta every connection too) is recorded in the host's system log, tag +# bwrap-net; there is no option to turn this off. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are diff --git a/bin/bwcopilot b/bin/bwcopilot index 49273a2..3dc0c85 100755 --- a/bin/bwcopilot +++ b/bin/bwcopilot @@ -89,7 +89,9 @@ # Outbound connections work; services on the host's loopback are unreachable # and the sandbox's listeners are invisible to the host, except for the ports # opened with --publish and --host-port. Without a usable private namespace -# the launch stops (exit 3) unless --share-net is given. +# the launch stops (exit 3) unless --share-net is given. Every session (with +# pasta every connection too) is recorded in the host's system log, tag +# bwrap-net; there is no option to turn this off. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are diff --git a/bin/bwopencode b/bin/bwopencode index ebcbb27..91db335 100755 --- a/bin/bwopencode +++ b/bin/bwopencode @@ -101,7 +101,9 @@ # Outbound connections work; services on the host's loopback are unreachable # and the sandbox's listeners are invisible to the host, except for the ports # opened with --publish and --host-port. Without a usable private namespace -# the launch stops (exit 3) unless --share-net is given. +# the launch stops (exit 3) unless --share-net is given. Every session (with +# pasta every connection too) is recorded in the host's system log, tag +# bwrap-net; there is no option to turn this off. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are diff --git a/bin/bwopencode2 b/bin/bwopencode2 index 8c83b05..3e1b74f 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -143,7 +143,9 @@ # Outbound connections work; services on the host's loopback are unreachable # and the sandbox's listeners are invisible to the host, except for the ports # opened with --publish and --host-port. Without a usable private namespace -# the launch stops (exit 3) unless --share-net is given. +# the launch stops (exit 3) unless --share-net is given. Every session (with +# pasta every connection too) is recorded in the host's system log, tag +# bwrap-net; there is no option to turn this off. # # Sandbox context: a short markdown file describing this sandbox (what is # mounted read-only or read-write, what is lost at exit, which tools are diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index ec4af58..c42d7d6 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -25,6 +25,9 @@ option list; the tables below summarise the common surface. [Network](#network)). `ss` (iproute) is used to find busy ports for `--publish`; with slirp4netns, `--publish` also needs `python3` (or RHEL 8's `/usr/libexec/platform-python`). +- For the [network log](#network-log): `systemd-cat` (systemd) with a running + journal, or `logger` (util-linux) with `/dev/log`. Without either the + wrappers warn and run without the log. ## Managed system paths @@ -413,6 +416,54 @@ root-managed `nftables` rules. To list the sandbox publications on a host: `ss -ltn 'src 127.0.0.0/8' | grep -v ' 127\.0\.'`. +### Network log + +Every launch records its network session in the host's system log, and with +pasta every connection as well. It is **always on**: there is no option to turn +it off. The records are written outside the sandbox, which can neither read nor +change them, and they are the data needed for a later egress allowlist. + +| Record | Content | +| --- | --- | +| start | `event=start session=ID launcher=... user=... uid=... pid=... backend=... flows=logged\|unavailable publish=ADDR:PORTS host_ports=... dir=...` | +| each pasta line | `session=ID ` (setup, then a line for each flow and its state changes) | +| end | `event=end session=ID ...`, when the sandbox has exited | + +- **Tag** (`SYSLOG_IDENTIFIER`): `bwrap-net`. `journalctl -t bwrap-net` shows + all sessions; `journalctl -t bwrap-net -g ID` one. Inside the sandbox + `BWRAP_SESSION` is the same ID, and the context file names it. +- **Tool:** `systemd-cat` when the journal's stream socket exists and accepts a + connection, else `logger` (util-linux) on `/dev/log` (journald or rsyslog). + Both are tried at launch without logging anything; the launcher then writes + the start record itself. With neither, the launch warns and continues + without the log. +- **Flows only with pasta.** slirp4netns has no flow log, so with it (and with + `--share-net`, recorded as `backend=none`) only the start and end records + are written; the start record says `flows=unavailable`. +- When the launcher runs on a terminal it prints the tool and the session ID. +- **How (pasta).** pasta logs flows only at debug level, and at debug level it + never uses syslog. So pasta gets `--debug --log-file FIFO` (and not + `--quiet`), with the FIFO in the launch's + [session directory](#runtime-directory), and a supervisor started by the + launcher (outside the sandbox) copies each line to the log: + - It holds the FIFO open read-write. pasta closes inherited descriptors + before it opens its log file; a reader that saw end-of-file in that gap + would leave pasta blocked on a full pipe, which stalls the sandbox's + network. It stops when pasta has exited, after draining the pipe, then + writes the end record and removes the session directory. + - If the tool dies, it goes on reading and drops the lines, so pasta never + blocks on it. + - `--log-size` is set to 1 TiB: pasta rotates its log with `fallocate` or + `lseek`, which fail on a FIFO and would make it drop every later line. + - pasta also prints its setup to stderr until it starts the command. Its + stderr is therefore the FIFO too, so those lines go to the log instead of + your terminal. The descriptor relay (pasta's command) prints a marker there + and reopens your stderr: by path when it is a terminal or a file, else (a + pipe) through a second FIFO that the supervisor copies. If pasta stops + before the marker (a failed start), the supervisor prints pasta's lines on + your terminal, prefixed `: pasta:`, just after the launcher has + exited. + ### Implementation notes: pasta - pasta runs outside bwrap, as you, and starts the sandbox. Default pasta diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index a677cfb..22a825a 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -2316,7 +2316,9 @@ or else slirp4netns). Outbound connections work as on the host; services on the host's loopback (127.0.0.1) are NOT reachable, and nothing the agent listens on is reachable from the host, unless opened below. If no private network namespace can be -set up, the launch stops (exit 3) unless --share-net is given. Ports must be +set up, the launch stops (exit 3) unless --share-net is given. Every session +(and with pasta every connection) is recorded in the host's system log, tag +bwrap-net (journalctl -t bwrap-net); this cannot be turned off. Ports must be in 1024-65535; N or LOW-HIGH ranges are accepted. --share-net Share the host network instead (the behaviour before private namespaces): host loopback services are reachable @@ -2562,6 +2564,287 @@ _net_session_dir() { fi } +# ── Network log (system journal / syslog) ─────────────────────── +# Every launch records its network session in the host's system log, and +# with pasta every connection too. It is always on: there is no option to +# turn it off. When no logging tool works, the launch warns and continues +# without the log. +# +# tag (SYSLOG_IDENTIFIER) bwrap-net +# start record event=start session=ID launcher=... backend=... +# each pasta line session=ID +# end record event=end session=ID ... +# +# `journalctl -t bwrap-net` shows them; `journalctl -t bwrap-net -g ID` one +# session. BWRAP_SESSION in the sandbox is the same ID. The tool is +# systemd-cat when the journal's stream socket exists, else logger (util- +# linux; /dev/log, so journald or rsyslog). The launch writes the start +# record itself, synchronously, which also checks that the tool works. +# +# pasta at debug level never uses syslog: it writes to --log-file, and until +# it starts the command, to stderr too. So with pasta: +# +# launcher ─┬─ supervisor (_net_log_supervisor, background, outside the sandbox) +# │ reads the netlog FIFO line by line and writes each line, +# │ prefixed with the session, to the tool; at the end writes +# │ the end record and removes the session directory +# └─ exec env -i ... pasta --debug --log-file FIFO ... 2<>FIFO +# -- relay (prints a marker, reopens its stderr) -- bwrap ... +# +# - The supervisor holds the FIFO open read-write, so it never sees EOF +# early: pasta closes inherited descriptors before it opens the log file, +# and a reader that saw EOF in that gap would leave pasta blocked on a +# full pipe, which stalls the sandbox's network. It stops reading when +# the launcher process (pasta) is gone, after draining what is left. +# - It never blocks pasta: if the tool dies it goes on reading and drops the +# lines. +# - --log-size is huge: pasta rotates its log with fallocate/lseek, which +# fail on a FIFO and would make it drop every later line. +# - pasta's stderr goes to the FIFO as well (its startup lines would +# otherwise print on the terminal); the relay prints a marker there and +# then reopens the user's stderr by path (a terminal or a file), or +# through a second FIFO that the supervisor copies to it (a pipe). If +# pasta exits before the marker (a failed start), the supervisor prints +# its lines on the terminal, so its errors are not lost. +# With slirp4netns or --share-net only the start and end records are written +# (slirp4netns has no flow log); the supervisor just waits for the end. + +_NET_LOG_TAG="bwrap-net" +_NET_LOG_TOOL="" +_NET_LOG_TOOLS=() +_NET_LOG_SESSION="" +_NET_LOG_FLOWS=0 +_NET_LOG_FIFO="" +_NET_LOG_ERR_FIFO="" +_NET_LOG_MARKER="" +# pasta's --log-size: 1 TiB, never reached (see above). +_NET_LOG_PASTA_SIZE=1099511627776 + +# _net_log_detect +# List the usable logging tools, best first (_NET_LOG_TOOLS: systemd-cat +# when the journal's stream socket exists, logger when /dev/log does), set +# _NET_LOG_TOOL to the first (empty when none), choose +# the session id, and export BWRAP_SESSION. Called by build_net_isolation. +_net_log_detect() { + _NET_LOG_SESSION="$(od -An -N6 -tx1 /dev/urandom | tr -d ' \n')" + _NET_LOG_TOOLS=() + # A real launch also tries each tool without logging anything: + # systemd-cat connects to the journal before running `true`, and logger + # opens /dev/log before reading its (empty) input. + if command -v systemd-cat > /dev/null 2>&1 && [[ -S /run/systemd/journal/stdout ]] && + { [[ "${DRY_RUN}" -eq 1 ]] || systemd-cat -t "${_NET_LOG_TAG}" true > /dev/null 2>&1; }; then + _NET_LOG_TOOLS+=(systemd-cat) + fi + if command -v logger > /dev/null 2>&1 && [[ -S /dev/log ]] && + { [[ "${DRY_RUN}" -eq 1 ]] || logger --socket-errors=on -t "${_NET_LOG_TAG}" < /dev/null > /dev/null 2>&1; }; then + _NET_LOG_TOOLS+=(logger) + fi + _NET_LOG_TOOL="${_NET_LOG_TOOLS[0]:-}" + _NET_LOG_FLOWS=0 + [[ -z "${_NET_LOG_TOOL}" || "${_NET_BACKEND}" != pasta ]] || _NET_LOG_FLOWS=1 + SANDBOX_ENV+=("BWRAP_SESSION=${_NET_LOG_SESSION}") +} + +# _net_log_cmd +# Fill the array _NET_LOG_CMD with the command that writes its standard +# input to the system log, one record per line. +_NET_LOG_CMD=() +_net_log_cmd() { + case "${_NET_LOG_TOOL}" in + systemd-cat) _NET_LOG_CMD=(systemd-cat -t "${_NET_LOG_TAG}" -p info) ;; + logger) _NET_LOG_CMD=(logger --socket-errors=on -t "${_NET_LOG_TAG}" -p user.info --id="$$") ;; + *) _NET_LOG_CMD=() ;; + esac +} + +# _net_log_record EVENT +# Print the session record for EVENT (start or end). +_net_log_record() { + local _r _i _hp="" + _r="event=$1 session=${_NET_LOG_SESSION} launcher=${0##*/} user=${_USER:-$(id -un)} uid=$(id -u)" + _r+=" pid=$$ backend=${_NET_BACKEND}" + if [[ "$1" == start ]]; then + if [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then + _r+=" flows=logged" + else + _r+=" flows=unavailable" + fi + [[ -z "${_NET_PUBLISH_ADDR_EFFECTIVE}" ]] || _r+=" publish=${_NET_PUBLISH_ADDR_EFFECTIVE}:$(_net_join ${NET_PUBLISH[@]+"${NET_PUBLISH[@]}"})" + for _i in ${NET_HOST_PORTS[@]+"${NET_HOST_PORTS[@]}"}; do _hp+="${_hp:+,}${_i}"; done + [[ -z "${_hp}" ]] || _r+=" host_ports=${_hp}" + _r+=" dir=${PWD}" + fi + printf '%s\n' "${_r}" +} + +# _net_log_start +# Called by launch_sandbox just before it execs: write the start record +# (which checks the tool; without one, warn and log nothing), then with +# pasta add the log options and the stderr hand-over, and start the +# supervisor. +_net_log_start() { + local _err _launcher="${0##*/}" _t + _NET_LOG_TOOL="" + for _t in ${_NET_LOG_TOOLS[@]+"${_NET_LOG_TOOLS[@]}"}; do + _NET_LOG_TOOL="${_t}" + _net_log_cmd + if _net_log_record start | "${_NET_LOG_CMD[@]}" > /dev/null 2>&1; then + break + fi + _NET_LOG_TOOL="" + done + if [[ -z "${_NET_LOG_TOOL}" ]]; then + printf 'Warning: %s: neither systemd-cat (journal) nor logger (/dev/log) can write to the system log;\n' "${_launcher}" >&2 + printf ' this network session is not logged.\n' >&2 + _NET_LOG_TOOL="" + _NET_LOG_FLOWS=0 + return 0 + fi + if [[ -t 2 ]]; then + printf '%s: network log: %s, tag %s, session %s\n' "${_launcher}" \ + "$([[ "${_NET_LOG_TOOL}" == systemd-cat ]] && echo journal || echo syslog)" \ + "${_NET_LOG_TAG}" "${_NET_LOG_SESSION}" >&2 + fi + + if [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then + _net_session_dir + _NET_LOG_FIFO="${_NET_SESSION_DIR}/netlog" + mkfifo -m 0600 "${_NET_LOG_FIFO}" + _NET_LOG_MARKER="bwrap-net relay started ${_NET_LOG_SESSION}" + local _i + for _i in "${!PASTA_ARGS[@]}"; do + [[ "${PASTA_ARGS[${_i}]}" != --quiet ]] || unset 'PASTA_ARGS[_i]' + done + PASTA_ARGS=("${PASTA_ARGS[@]}" --debug --log-file "${_NET_LOG_FIFO}" --log-size "${_NET_LOG_PASTA_SIZE}") + # The relay restores the user's stderr: by path when it is a terminal + # or a file, else through a FIFO the supervisor copies. + _err="$(readlink "/proc/$$/fd/2" 2> /dev/null || true)" + if [[ "${_err}" != /* || "${_err}" == *" (deleted)" ]] || + { [[ ! -c "${_err}" && ! -f "${_err}" ]]; }; then + _NET_LOG_ERR_FIFO="${_NET_SESSION_DIR}/stderr" + mkfifo -m 0600 "${_NET_LOG_ERR_FIFO}" + _err="${_NET_LOG_ERR_FIFO}" + fi + _BWRAP_FD_RELAY=(2 note "${_NET_LOG_MARKER}" 2 append "${_err}" ${_BWRAP_FD_RELAY[@]+"${_BWRAP_FD_RELAY[@]}"}) + fi + _net_log_supervisor & +} + +# _net_log_supervisor +# The background half of the network log (see "Network log"). Runs in a +# subshell; $$ is still the launcher, which is pasta (or bwrap) by then. +_net_log_supervisor() { + local _sink _fifo _line="" _part="" _rc _started=0 _dead=0 _bridge="" _i + local -a _early=() + set +e + # Signals meant for the session (Ctrl-C, a hangup, `kill` of its process + # group) must not cost the end record: the supervisor ends by itself once + # the launcher has gone. + trap '' INT QUIT TSTP HUP TERM PIPE + exec < /dev/null > /dev/null + exec {_sink}> >(exec setsid "${_NET_LOG_CMD[@]}" > /dev/null 2>&1) + + if [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then + exec {_fifo}<> "${_NET_LOG_FIFO}" + if [[ -n "${_NET_LOG_ERR_FIFO}" ]]; then + cat < "${_NET_LOG_ERR_FIFO}" >&2 & + _bridge=$! + fi + while :; do + # Poll faster until the sandbox has started, so a failed pasta + # start is reported promptly. + IFS= read -r -t "$([[ "${_started}" -eq 1 ]] && echo 1 || echo 0.1)" _line <&"${_fifo}" + _rc=$? + if ((_rc > 128)); then + # Timeout; a partial line stays in _line. + _part+="${_line}" + _net_launcher_alive || break + continue + fi + _line="${_part}${_line}" + _part="" + if [[ "${_started}" -eq 0 ]]; then + if [[ "${_line}" == "${_NET_LOG_MARKER}" ]]; then + _started=1 + _early=() + continue + fi + [[ ${#_early[@]} -ge 200 ]] || _early+=("${_line}") + fi + if [[ "${_dead}" -eq 0 ]] && + ! printf 'session=%s %s\n' "${_NET_LOG_SESSION}" "${_line}" 1>&"${_sink}" 2> /dev/null; then + _dead=1 + fi + done + # pasta has exited: drain what it left in the pipe. + while IFS= read -r -t 0.2 _line <&"${_fifo}"; do + [[ "${_dead}" -eq 1 ]] || + printf 'session=%s %s\n' "${_NET_LOG_SESSION}" "${_part}${_line}" 1>&"${_sink}" 2> /dev/null || _dead=1 + _part="" + done + exec {_fifo}<&- + if [[ "${_started}" -eq 0 ]]; then + # pasta stopped before starting the sandbox: show what it said. + for _line in ${_early[@]+"${_early[@]}"}; do + printf '%s: pasta: %s\n' "${0##*/}" "${_line}" >&2 + done + fi + if [[ -n "${_bridge}" ]]; then + for ((_i = 0; _i < 50; _i++)); do + kill -0 "${_bridge}" 2> /dev/null || break + sleep 0.1 + done + kill "${_bridge}" 2> /dev/null + fi + else + while _net_launcher_alive; do + sleep 1 + done + fi + if [[ "${_dead}" -eq 0 ]]; then + _net_log_record end 1>&"${_sink}" 2> /dev/null + fi + exec {_sink}>&- + [[ -z "${_NET_SESSION_DIR}" ]] || rm -rf "${_NET_SESSION_DIR}" + exit 0 +} + +# _net_log_context_text +# Set _NET_LOG_CTX to the context file's paragraph about the network log, +# empty when no logging tool is available. +_NET_LOG_CTX="" +_net_log_context_text() { + _NET_LOG_CTX="" + [[ -n "${_NET_LOG_TOOL}" ]] || return 0 + _NET_LOG_CTX="The host records this network session in its system log (tag \`${_NET_LOG_TAG}\`, session \`${_NET_LOG_SESSION}\`, also in \`BWRAP_SESSION\`)" + if [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then + _NET_LOG_CTX+=", with every connection pasta makes for it" + fi + _NET_LOG_CTX+=". The sandbox cannot read or change that log."$'\n\n' +} + +# _net_log_print_dry_run +# Describe the network log as comments for --dry-run. +_net_log_print_dry_run() { + _net_log_cmd + printf '\n# Network log: ' + if [[ -z "${_NET_LOG_TOOL}" ]]; then + printf 'neither systemd-cat (journal) nor logger (/dev/log) is available;\n' + printf '# a real launch warns and logs nothing.\n' + return 0 + fi + printf 'the launcher writes the start record (session %s) with\n# %s\n' \ + "${_NET_LOG_SESSION}" "${_NET_LOG_CMD[*]}" + if [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then + printf '# and pasta gets --debug --log-file /netlog --log-size %s (not --quiet)\n' "${_NET_LOG_PASTA_SIZE}" + printf '# with its stderr on that FIFO; a supervisor copies each line to the log and the\n' + printf '# relay first reopens your stderr. The end record is written when pasta exits.\n' + else + printf '# and a supervisor writes the end record when the sandbox exits (no flow log\n' + printf '# with %s).\n' "${_NET_BACKEND}" + fi +} + # ── slirp4netns backend ────────────────────────────────────────── # Used when pasta is not usable (RHEL 8 has slirp4netns but no passt). # slirp4netns cannot start the command itself; it attaches to an existing @@ -2858,7 +3141,7 @@ _net_slirp_run() { # Print the parent pid of PID (from /proc/PID/stat; empty when PID is gone). _net_ppid() { local _st _f - IFS= read -r _st < "/proc/$1/stat" 2> /dev/null || return 0 + IFS= read -r _st 2> /dev/null < "/proc/$1/stat" || return 0 # Fields after the command name, which may contain spaces: state ppid ... read -r -a _f <<< "${_st##*) }" printf '%s' "${_f[1]}" @@ -3125,7 +3408,7 @@ _NET_CLAIMS_LOCK_FD="" # since boot); empty when PID is gone. bwrap_proc_start_time() { local _st _f - IFS= read -r _st < "/proc/$1/stat" 2> /dev/null || return 0 + IFS= read -r _st 2> /dev/null < "/proc/$1/stat" || return 0 read -r -a _f <<< "${_st##*) }" printf '%s' "${_f[19]}" } @@ -3331,6 +3614,7 @@ build_net_isolation() { _NET_PUBLISH_DESC=() _NET_HOST_DESC=() _NET_PUBLISH_ADDR_EFFECTIVE="" + _net_log_detect # Host network (--share-net): the previous behaviour. if [[ "${_NET_PRIVATE}" -ne 1 ]]; then @@ -4475,7 +4759,8 @@ _bwrap_context_network() { _t+=" and they are not yours to use unless the user asks: they may belong to other users." _t+=" Anything you listen on is reachable from the host, and on \`0.0.0.0\` from the network:" _t+=" bind \`127.0.0.1\` unless told otherwise.${_nl}${_nl}" - _CTX_NET="${_t}" + _net_log_context_text + _CTX_NET="${_t}${_NET_LOG_CTX}" return 0 fi _t+="This sandbox has its own network namespace (${_NET_BACKEND})." @@ -4513,7 +4798,8 @@ _bwrap_context_network() { _t+="${_nl}Your own \`127.0.0.1\` is unaffected: a server you start on the same port is separate.${_nl}${_nl}" fi _t+="\`BWRAP_NET_BACKEND\` is \`${_NET_BACKEND}\`; \`BWRAP_NET_PUBLISH\`, \`BWRAP_NET_PUBLISH_ADDR\`, \`BWRAP_NET_HOST_PORTS\` and \`BWRAP_NET_HOST_ADDR\` hold the same information.${_nl}${_nl}" - _CTX_NET="${_t}" + _net_log_context_text + _CTX_NET="${_t}${_NET_LOG_CTX}" } # _bwrap_context_render TOOL DEST @@ -4803,6 +5089,11 @@ _bwrap_toml_string() { # (which appends one newline) and unsets the variable before # exec'ing bwrap. Not for secrets: the variable stays in # pasta's environment for the whole session. +# Two more entries are used by the network log (see "Network log"), before +# the others: +# note TEXT the relay prints TEXT on the descriptor (a marker on stderr). +# append PATH the relay opens PATH for appending on the descriptor (the +# user's stderr again). # _bwrap_fd_check_room # Exit 1 when the reserved numbers would reach 90, where the slirp4netns @@ -4872,6 +5163,14 @@ while [[ $# -gt 0 && "$1" != -- ]]; do unset "${_v}" shift 2 ;; + note) + printf "%s\n" "$3" >&"$1" + shift 3 + ;; + append) + eval "exec $1>>\"\$3\"" || { echo "$0: cannot open $3" >&2; exit 126; } + shift 3 + ;; *) echo "$0: bad relay entry: $2" >&2 exit 126 @@ -4907,7 +5206,7 @@ _bwrap_fd_data_numbers() { local _r=0 _BWRAP_FD_DATA_NUMS=() while [[ ${_r} -lt ${#_BWRAP_FD_RELAY[@]} ]]; do - if [[ "${_BWRAP_FD_RELAY[$((_r + 1))]}" == file ]]; then + if [[ "${_BWRAP_FD_RELAY[$((_r + 1))]}" != data ]]; then _r=$((_r + 3)) else _BWRAP_FD_DATA_NUMS+=("${_BWRAP_FD_RELAY[${_r}]}") @@ -5138,6 +5437,7 @@ print_dry_run() { for _arg in ${TOOL_ARGS[@]+"${TOOL_ARGS[@]}"}; do printf " %q" "${_arg}"; done printf "\n" [[ "${_NET_BACKEND}" != slirp4netns ]] || _net_slirp_print_dry_run + _net_log_print_dry_run for _n in ${_BWRAP_FD_DATA_NUMS[@]+"${_BWRAP_FD_DATA_NUMS[@]}"}; do printf '\n# Descriptor %s, filled by the relay with the %s:\n#\n' "${_n}" "${_BWRAP_FD_LABEL["${_n}"]}" printf '%s\n' "${_BWRAP_FD_DATA["${_n}"]}" | sed 's/^/# /; s/ *$//' @@ -5171,6 +5471,8 @@ launch_sandbox() { _BASH_BIN="$(resolve_bash_bin)" local -a _data_env=() _net_prefix=() + _net_print_report + _net_log_start if [[ "${_NET_BACKEND}" == pasta ]]; then _net_prefix=("${_PASTA_BIN}" "${PASTA_ARGS[@]}" --) elif [[ "${_NET_BACKEND}" == slirp4netns ]]; then @@ -5181,8 +5483,11 @@ launch_sandbox() { _data_env+=("_BWRAP_FD_DATA_${_n}=${_BWRAP_FD_DATA["${_n}"]}") done - _net_print_report [[ "${_NET_BACKEND}" != slirp4netns ]] || _net_slirp_start + if [[ -n "${_NET_LOG_FIFO}" ]]; then + # pasta's stderr goes to the network log (see "Network log"). + exec 2<> "${_NET_LOG_FIFO}" + fi exec "${_ENV_BIN}" -i \ HOME="${_HOME}" \ USER="${_USER}" \ From 4aa3c8ca2847597f70607be9f63390de3db360c4 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 9 Nov 2026 22:47:44 -0500 Subject: [PATCH 18/37] test(bwrap): add fake network backends to run the launchers tests/fake-net-run runs a launcher against fake pasta, slirp4netns, unshare, systemd-cat and logger, where /dev/net/tun is missing (as in a development sandbox). The fakes really create or join the namespaces and forward TCP, so --publish, --serve and the network log can be tested end to end; failure modes are read from $FAKE_NET_DIR. Assisted-by: opencode:claude-opus-5-5 --- tests/fake-net-run | 59 ++++++++++++++ tests/fakes/_nsproxy.py | 133 ++++++++++++++++++++++++++++++++ tests/fakes/bin/logger | 21 +++++ tests/fakes/bin/slirp4netns | 133 ++++++++++++++++++++++++++++++++ tests/fakes/bin/systemd-cat | 26 +++++++ tests/fakes/bin/unshare | 35 +++++++++ tests/fakes/pasta-bin/pasta | 150 ++++++++++++++++++++++++++++++++++++ 7 files changed, 557 insertions(+) create mode 100755 tests/fake-net-run create mode 100644 tests/fakes/_nsproxy.py create mode 100755 tests/fakes/bin/logger create mode 100755 tests/fakes/bin/slirp4netns create mode 100755 tests/fakes/bin/systemd-cat create mode 100755 tests/fakes/bin/unshare create mode 100755 tests/fakes/pasta-bin/pasta diff --git a/tests/fake-net-run b/tests/fake-net-run new file mode 100755 index 0000000..3415042 --- /dev/null +++ b/tests/fake-net-run @@ -0,0 +1,59 @@ +#!/bin/bash +# Run a bw* launcher against the fake network backends in tests/fakes. +# +# tests/fake-net-run [--backend pasta|slirp4netns] LAUNCHER [ARGS...] +# +# An outer bwrap supplies what the development sandbox lacks: a character +# device at /dev/net/tun (/dev/null), and plain-file copies of /etc/resolv.conf +# and /etc/hosts (a file that is itself a bwrap data mount cannot be bound +# again). The real pasta is hidden; --backend slirp4netns leaves the fake +# pasta off PATH too. The fakes read failure modes from $FAKE_NET_DIR. +# FAKE_NET_LOG=journal (default), syslog, both or none puts a socket file at +# /run/systemd/journal/stdout, /dev/log, both or neither (so the launcher +# picks the fake systemd-cat or logger, which write to +# $FAKE_NET_DIR/journal.log). +set -euo pipefail +here="$(dirname "$(readlink -f "$0")")" +backend=pasta +if [[ "${1:-}" == --backend ]]; then + backend="$2" + shift 2 +fi +export FAKE_NET_DIR="${FAKE_NET_DIR:-/tmp/opencode/fakenet}" +mkdir -p "${FAKE_NET_DIR}/etc" +cat /etc/resolv.conf >"${FAKE_NET_DIR}/etc/resolv.conf" +cat /etc/hosts >"${FAKE_NET_DIR}/etc/hosts" +# A fresh /etc: the plain copies, plus every other entry bound as it is. +etc=(--tmpfs /etc) +for e in /etc/* /etc/.[!.]*; do + [[ -e "${e}" || -L "${e}" ]] || continue + case "${e}" in + /etc/resolv.conf | /etc/hosts) ;; + *) etc+=(--ro-bind "${e}" "${e}") ;; + esac +done +etc+=(--ro-bind "${FAKE_NET_DIR}/etc/resolv.conf" /etc/resolv.conf) +etc+=(--ro-bind "${FAKE_NET_DIR}/etc/hosts" /etc/hosts) +logsock=() +mksock() { + rm -f "$1" + python3 -c 'import socket, sys; socket.socket(socket.AF_UNIX).bind(sys.argv[1])' "$1" +} +case "${FAKE_NET_LOG:-journal}" in +journal | both) + mksock "${FAKE_NET_DIR}/journal.sock" + logsock+=(--dir /run/systemd/journal --bind "${FAKE_NET_DIR}/journal.sock" /run/systemd/journal/stdout) + ;;& +syslog | both) + mksock "${FAKE_NET_DIR}/devlog.sock" + logsock+=(--bind "${FAKE_NET_DIR}/devlog.sock" /dev/log) + ;; +esac +path="${here}/fakes/bin:${PATH}" +[[ "${backend}" != pasta ]] || path="${here}/fakes/pasta-bin:${path}" +exec bwrap --dev-bind / / --dev /dev --dev-bind /dev/null /dev/net/tun \ + --ro-bind /dev/null /usr/bin/passt \ + "${etc[@]}" \ + ${logsock[@]+"${logsock[@]}"} \ + --chdir "${PWD}" \ + -- env PATH="${path}" "$@" diff --git a/tests/fakes/_nsproxy.py b/tests/fakes/_nsproxy.py new file mode 100644 index 0000000..567b266 --- /dev/null +++ b/tests/fakes/_nsproxy.py @@ -0,0 +1,133 @@ +"""Shared helpers for the fake network backends (pasta, slirp4netns). + +The fakes stand in for the real backends where /dev/net/tun is missing (for +example inside a development sandbox). They really create or join the +namespaces and really forward TCP, so the launchers can be tested end to end: + +- NsProxy forks a process that joins the user and network namespace of a + target pid and creates sockets there on request (connect, listen), passing + them back with SCM_RIGHTS. The caller stays in the host namespace. +- forward() accepts on a listening socket and splices each connection to a + socket returned by a callback. +""" + +import ctypes +import json +import os +import socket +import subprocess +import threading + +PR_SET_PDEATHSIG = 1 + + +def _die_with_parent(): + libc = ctypes.CDLL(None, use_errno=True) + libc.prctl(PR_SET_PDEATHSIG, 9, 0, 0, 0) + + +class NsProxy: + def __init__(self, pid): + a, b = socket.socketpair(socket.AF_UNIX, socket.SOCK_SEQPACKET) + self.pid = os.fork() + if self.pid == 0: + a.close() + _die_with_parent() + for ns, flag in (("user", os.CLONE_NEWUSER), ("net", os.CLONE_NEWNET)): + fd = os.open("/proc/%d/ns/%s" % (pid, ns), os.O_RDONLY) + os.setns(fd, flag) + os.close(fd) + self._serve(b) + os._exit(0) + b.close() + self.sock = a + self.lock = threading.Lock() + + @staticmethod + def _serve(chan): + while True: + msg = chan.recv(65536) + if not msg: + return + req = json.loads(msg) + try: + if req["op"] == "connect": + s = socket.create_connection((req["host"], req["port"]), timeout=5) + s.settimeout(None) + elif req["op"] == "listen": + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + s.bind((req["host"], req["port"])) + s.listen(16) + elif req["op"] == "run": + r = subprocess.run(req["cmd"], capture_output=True, text=True) + chan.send(json.dumps({"rc": r.returncode, "err": r.stderr}).encode()) + continue + else: + raise OSError("unknown op") + socket.send_fds(chan, [b"ok"], [s.fileno()]) + s.close() + except OSError as e: + chan.send(json.dumps({"error": str(e)}).encode()) + + def _request(self, **req): + with self.lock: + self.sock.send(json.dumps(req).encode()) + msg, fds, _, _ = socket.recv_fds(self.sock, 65536, 1) + if not fds: + reply = json.loads(msg) + if "rc" in reply: + return reply + raise OSError(reply.get("error", "proxy error")) + return socket.socket(fileno=fds[0]) + + def connect(self, host, port): + return self._request(op="connect", host=host, port=port) + + def listen(self, host, port): + return self._request(op="listen", host=host, port=port) + + def run(self, cmd): + return self._request(op="run", cmd=cmd) + + +def _pipe(src, dst): + try: + while True: + data = src.recv(65536) + if not data: + break + dst.sendall(data) + except OSError: + pass + try: + dst.shutdown(socket.SHUT_WR) + except OSError: + pass + + +def forward(listener, connect, log=None, label=""): + def loop(): + while True: + try: + c, peer = listener.accept() + except OSError: + return + try: + t = connect() + except OSError as e: + if log: + log("Flow %s: %s:%s refused: %s" % (label, peer[0], peer[1], e)) + c.close() + continue + if log: + log("Flow %s: TAP_FWD %s:%s => %s" % (label, peer[0], peer[1], t.getpeername())) + for a, b in ((c, t), (t, c)): + threading.Thread(target=_pipe, args=(a, b), daemon=True).start() + + threading.Thread(target=loop, daemon=True).start() + + +def port_range(spec): + lo, _, hi = spec.partition("-") + return range(int(lo), int(hi or lo) + 1) diff --git a/tests/fakes/bin/logger b/tests/fakes/bin/logger new file mode 100755 index 0000000..f41d520 --- /dev/null +++ b/tests/fakes/bin/logger @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 +"""Fake util-linux logger reading stdin: append each line to +$FAKE_NET_DIR/journal.log as "IDENT[PRIORITY,id=ID] line". +$FAKE_NET_DIR/log.fail = logger makes it fail at once.""" + +import os +import sys + +d = os.environ.get("FAKE_NET_DIR", "/tmp/opencode/fakenet") +try: + if open(os.path.join(d, "log.fail")).read().strip() == "logger": + sys.exit("logger: socket /dev/log: No such file or directory (fake)") +except OSError: + pass +a = sys.argv[1:] +ident = a[a.index("-t") + 1] +prio = a[a.index("-p") + 1] if "-p" in a else "user.notice" +pid = [x.split("=", 1)[1] for x in a if x.startswith("--id=")] +with open(os.path.join(d, "journal.log"), "a", buffering=1) as f: + for line in sys.stdin: + f.write("%s[%s,id=%s] %s" % (ident, prio, pid[0] if pid else "", line)) diff --git a/tests/fakes/bin/slirp4netns b/tests/fakes/bin/slirp4netns new file mode 100755 index 0000000..603d373 --- /dev/null +++ b/tests/fakes/bin/slirp4netns @@ -0,0 +1,133 @@ +#!/usr/bin/env python3 +"""Fake slirp4netns for testing the bw* launchers without /dev/net/tun. + +Like the real one: joins /proc/PID/ns/{user,net}, creates the interface (a +dummy, not a tap) with the --cidr address .100, serves add_hostfwd on +--api-socket (really forwarding host_addr:host_port to .100:guest_port), +writes "1" to --ready-fd, and exits when --exit-fd hangs up. + +It runs under `env -i`, so it reads its settings from $FAKE_NET_DIR (default +/tmp/opencode/fakenet): slirp.fail (ready: fail before ready; api: refuse +add_hostfwd; delay: start 2 s late), slirp.libslirp (reported libslirp +version); it logs to slirp.log there. +""" + +import json +import os +import select +import socket +import sys +import threading +import time + +sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) +import _nsproxy # noqa: E402 + +FAKE_DIR = os.environ.get("FAKE_NET_DIR", "/tmp/opencode/fakenet") + + +def setting(name, default=""): + try: + return open(os.path.join(FAKE_DIR, name)).read().strip() + except OSError: + return default + + +def log(msg): + with open(os.path.join(FAKE_DIR, "slirp.log"), "a") as f: + f.write(msg + "\n") + + +args = sys.argv[1:] +if args in (["--version"], ["-v"]): + print("slirp4netns version 1.2.3\ncommit: fake") + print("libslirp: " + setting("slirp.libslirp", "4.4.0")) + print("SLIRP_CONFIG_VERSION_MAX: 3\nlibseccomp: 2.5.2") + sys.exit(0) + +FAIL = setting("slirp.fail") +opts, pos = {}, [] +for a in args: + if a.startswith("--"): + k, _, v = a.partition("=") + opts[k] = v + else: + pos.append(a) +log("argv " + repr(args)) +log("env " + repr(sorted(os.environ))) +log("fds " + repr(sorted(int(x) for x in os.listdir("/proc/self/fd")))) +log("sid==pid %s" % (os.getsid(0) == os.getpid())) +for need in ("--configure", "--disable-host-loopback", "--cidr", "--ready-fd", "--exit-fd"): + if need not in opts: + sys.exit("fake slirp4netns: missing " + need) +pid, tap = int(pos[0]), pos[1] +net = opts["--cidr"].rsplit(".", 1)[0] +guest = net + ".100" + +if FAIL == "delay": + time.sleep(2) +if FAIL == "ready": + sys.exit("WARNING: fake failure before ready") + +try: + proxy = _nsproxy.NsProxy(pid) + for cmd in ( + ["ip", "link", "set", "lo", "up"], + ["ip", "link", "add", tap, "type", "dummy"], + ["ip", "addr", "add", guest + "/24", "dev", tap], + ["ip", "link", "set", tap, "up"], + ): + r = proxy.run(cmd) + if r["rc"] != 0: + raise OSError(r["err"]) +except OSError as e: + sys.exit("fake slirp4netns: namespace setup failed: %s" % e) + +api = opts.get("--api-socket") +if api: + srv = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + srv.bind(api) + srv.listen() + + def serve(): + n = 0 + while True: + c, _ = srv.accept() + data = b"" + while True: + chunk = c.recv(4096) + if not chunk: + break + data += chunk + req = json.loads(data) + log("api " + json.dumps(req)) + a = req.get("arguments", {}) + try: + if FAIL == "api": + raise OSError("fake: bind failed") + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + s.bind((a.get("host_addr", "0.0.0.0"), a["host_port"])) + s.listen(16) + gp = a["guest_port"] + _nsproxy.forward(s, lambda gp=gp: proxy.connect(a.get("guest_addr", guest), gp)) + n += 1 + rep = {"return": {"id": n}} + except OSError as e: + rep = {"error": {"desc": str(e)}} + c.sendall(json.dumps(rep).encode()) + c.close() + + threading.Thread(target=serve, daemon=True).start() + +ready = int(opts["--ready-fd"]) +os.write(ready, b"1") +os.close(ready) +log("ready") + +p = select.poll() +p.register(int(opts["--exit-fd"]), select.POLLIN | select.POLLHUP) +while True: + if any(e & select.POLLHUP for _, e in p.poll()): + break +log("exit-fd hangup, exiting") diff --git a/tests/fakes/bin/systemd-cat b/tests/fakes/bin/systemd-cat new file mode 100755 index 0000000..67a4bfb --- /dev/null +++ b/tests/fakes/bin/systemd-cat @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +"""Fake systemd-cat: append each line of stdin to $FAKE_NET_DIR/journal.log as +"IDENT[PRIORITY] line". Only `-t IDENT -p PRIORITY` without a command is +supported. $FAKE_NET_DIR/log.fail = systemd-cat makes it fail at once.""" + +import os +import sys + +d = os.environ.get("FAKE_NET_DIR", "/tmp/opencode/fakenet") +try: + if open(os.path.join(d, "log.fail")).read().strip() == "systemd-cat": + sys.exit("Failed to create stream fd: No such file or directory (fake)") +except OSError: + pass +a = sys.argv[1:] +ident = a[a.index("-t") + 1] +rest = a[a.index("-t") + 2 :] +if rest[:1] == ["-p"]: + prio, rest = rest[1], rest[2:] +else: + prio = "info" +if rest: + os.execvp(rest[0], rest) +with open(os.path.join(d, "journal.log"), "a", buffering=1) as f: + for line in sys.stdin: + f.write("%s[%s] %s" % (ident, prio, line)) diff --git a/tests/fakes/bin/unshare b/tests/fakes/bin/unshare new file mode 100755 index 0000000..e59cb56 --- /dev/null +++ b/tests/fakes/bin/unshare @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +"""Fake util-linux `unshare --user --map-root-user --net -- CMD...`. + +Like util-linux it does not fork: it unshares, writes the maps and execs CMD +in the same process. The failure mode is read from $FAKE_NET_DIR/unshare.fail +(fail: exit 1 before execing). +""" + +import os +import sys + +FAKE_DIR = os.environ.get("FAKE_NET_DIR", "/tmp/opencode/fakenet") +args = sys.argv[1:] +if args[:1] in (["--help"], ["-h"]): + print(" -U, --user[=] unshare user namespace") + print(" -n, --net[=] unshare network namespace") + print(" -r, --map-root-user map current user to root (implies --user)") + sys.exit(0) +try: + if open(os.path.join(FAKE_DIR, "unshare.fail")).read().strip() == "fail": + sys.exit("unshare: unshare failed: Operation not permitted (fake)") +except OSError: + pass +i = args.index("--") +opts, cmd = args[:i], args[i + 1 :] +assert set(opts) == {"--user", "--map-root-user", "--net"}, opts +uid, gid = os.getuid(), os.getgid() +os.unshare(os.CLONE_NEWUSER | os.CLONE_NEWNET) +with open("/proc/self/setgroups", "w") as f: + f.write("deny") +with open("/proc/self/uid_map", "w") as f: + f.write("0 %d 1" % uid) +with open("/proc/self/gid_map", "w") as f: + f.write("0 %d 1" % gid) +os.execvp(cmd[0], cmd) diff --git a/tests/fakes/pasta-bin/pasta b/tests/fakes/pasta-bin/pasta new file mode 100755 index 0000000..384a9ca --- /dev/null +++ b/tests/fakes/pasta-bin/pasta @@ -0,0 +1,150 @@ +#!/usr/bin/env python3 +"""Fake pasta for testing the bw* launchers without /dev/net/tun. + +Like the real one it closes inherited descriptors above 2 before opening +--log-file, creates a user namespace (mapping the caller to uid 0) and a +network namespace, starts the command in it and stays its parent, and +forwards -t (host -> namespace interface address) and -T (namespace -> host +loopback) ports. The namespace gets a dummy interface with 192.168.77.2 +instead of a tap with the host's address. With --debug and --log-file it +writes header, startup and "Flow" lines to the log file (blocking writes, as +pasta's are). The failure mode is read from $FAKE_NET_DIR/pasta.fail +(bind: refuse every -t port). +""" + +import os +import sys +import threading + +sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) +import _nsproxy # noqa: E402 + +NS_ADDR = "192.168.77.2" +FAKE_DIR = os.environ.get("FAKE_NET_DIR", "/tmp/opencode/fakenet") + +args = sys.argv[1:] +if args[:1] in (["--help"], ["-h"]): + print("Usage: pasta [OPTION]... [COMMAND [ARG]...]") + print(" --no-pidns Don't spawn command in a new PID namespace") + sys.exit(0) +if args[:1] == ["--version"]: + print("pasta fake-2026_10_02") + sys.exit(0) + +VALUE_OPTS = {"-t", "-u", "-T", "-U", "--dns-forward", "--search", "--log-file", "--log-size"} +opts, fwd_t, fwd_T, cmd = {}, [], [], [] +i = 0 +while i < len(args): + a = args[i] + if a == "--": + cmd = args[i + 1 :] + break + if a in VALUE_OPTS: + v = args[i + 1] + if a == "-t" and v != "none": + fwd_t.append(v) + elif a == "-T" and v != "none": + fwd_T.append(v) + else: + opts[a] = v + i += 2 + continue + opts[a] = True + i += 1 +if not cmd: + sys.exit("fake pasta: no command") + +os.closerange(3, 4096) + +log_fd = -1 +if "--log-file" in opts: + log_fd = os.open(opts["--log-file"], os.O_CREAT | os.O_TRUNC | os.O_APPEND | os.O_RDWR, 0o600) + os.write(log_fd, ("pasta fake-2026_10_02: %s (%d)\n" % (os.path.realpath(__file__), os.getpid())).encode()) +log_lock = threading.Lock() + + +def log(msg): + if log_fd >= 0 and "--debug" in opts: + with log_lock: + os.write(log_fd, ("0.0001: %s\n" % msg).encode()) + + +try: + FAIL = open(os.path.join(FAKE_DIR, "pasta.fail")).read().strip() +except OSError: + FAIL = "" + +uid, gid = os.getuid(), os.getgid() +ready_r, ready_w = os.pipe() +go_r, go_w = os.pipe() +child = os.fork() +if child == 0: + os.close(ready_r) + os.close(go_w) + os.unshare(os.CLONE_NEWUSER | os.CLONE_NEWNET | os.CLONE_NEWUTS) + with open("/proc/self/setgroups", "w") as f: + f.write("deny") + with open("/proc/self/uid_map", "w") as f: + f.write("0 %d 1" % uid) + with open("/proc/self/gid_map", "w") as f: + f.write("0 %d 1" % gid) + import socket as _s + + _s.sethostname("pasta-" + os.uname().nodename) + for c in ( + ["ip", "link", "set", "lo", "up"], + ["ip", "link", "add", "eth0", "type", "dummy"], + ["ip", "addr", "add", NS_ADDR + "/24", "dev", "eth0"], + ["ip", "link", "set", "eth0", "up"], + ): + if os.spawnvp(os.P_WAIT, c[0], c) != 0: + os._exit(1) + os.write(ready_w, b"1") + os.close(ready_w) + os.read(go_r, 1) + os.close(go_r) + try: + os.execvp(cmd[0], cmd) + except OSError as e: + print("fake pasta: %s: %s" % (cmd[0], e), file=sys.stderr) + os._exit(127) +os.close(ready_w) +os.close(go_r) +if os.read(ready_r, 1) != b"1": + print("fake pasta: namespace setup failed", file=sys.stderr) + sys.exit(1) +proxy = _nsproxy.NsProxy(child) +log("Template interface: eth0 (IPv4)") + +import socket # noqa: E402 + +for spec in fwd_t: + addr, _, ports = spec.rpartition("/") + hports, _, gports = ports.partition(":") + hr = list(_nsproxy.port_range(hports)) + gr = list(_nsproxy.port_range(gports or hports)) + for hp, gp in zip(hr, gr): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + try: + if FAIL == "bind": + raise OSError("fake: bind refused") + s.bind((addr or "0.0.0.0", hp)) + except OSError as e: + print("Failed to bind port %d (%s) for option '-t %s', exiting" % (hp, e, spec), file=sys.stderr) + os.kill(child, 9) + sys.exit(1) + s.listen(16) + _nsproxy.forward(s, lambda gp=gp: proxy.connect(NS_ADDR, gp), log, "-t %d" % hp) +for spec in fwd_T: + addr, _, ports = spec.rpartition("/") + sports, _, hports = ports.partition(":") + for sp, hp in zip(_nsproxy.port_range(sports), _nsproxy.port_range(hports or sports)): + s = proxy.listen(addr or "127.0.0.1", sp) + _nsproxy.forward(s, lambda hp=hp: socket.create_connection(("127.0.0.1", hp)), log, "-T %d" % sp) + +os.write(go_w, b"1") +os.close(go_w) +_, status = os.waitpid(child, 0) +log("pasta: child exited") +sys.exit(os.waitstatus_to_exitcode(status) if os.WIFEXITED(status) else 128 + os.WTERMSIG(status)) From 4784f93c8e7bd651664017b4a19deaaaa8979dfd Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 10 Nov 2026 09:45:07 -0500 Subject: [PATCH 19/37] fix(bwopencode2): always serve on the user's first publish address A --serve session could move to another session index when one of its ports was busy on 127.H1.H2.1, and the --serve-ssh-config alias then did not reach it. --serve now fixes the session's publish address to .1: its port is chosen free and unclaimed there, a clash with another --publish port stops the launch with the reason, and --publish-addr is refused with --serve. The fake pasta now handles comma-separated port lists and never starts the command when its setup failed. Assisted-by: opencode:claude-opus-5-5 --- bin/bwopencode2 | 66 +++++++++++++++++++++---------------- docs/bw-wrappers.md | 22 +++++++------ lib/bwrap_sandbox_lib.sh | 32 ++++++++++++++++-- tests/fakes/_nsproxy.py | 8 +++-- tests/fakes/pasta-bin/pasta | 19 +++++++---- 5 files changed, 98 insertions(+), 49 deletions(-) diff --git a/bin/bwopencode2 b/bin/bwopencode2 index 3e1b74f..13a8581 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -82,7 +82,7 @@ # --serve is the one deliberate exception: it runs `opencode2 serve` INSIDE this # sandbox (tools stay sandboxed) on a free port from a fixed range, protected by # a password, and publishes that port (--publish) on this user's loopback -# address 127.H1.H2.S, so the OpenCode web UI can be opened from a browser (over +# address 127.H1.H2.1, so the OpenCode web UI can be opened from a browser (over # an ssh -L tunnel). The launcher builds those arguments itself; nothing from # the command line reaches `serve`. See docs/bw-wrappers.md. # @@ -192,10 +192,12 @@ SERVE_PASSWORD_RANDOM=0 SERVE_PORT_LO="" SERVE_PORT_HI="" SERVE_STATE_DIR="" -# Address the server is reached at on the host: this user's publish address -# (127.H1.H2.S, see net_publish_prefix) or --publish-addr. With a private -# network the port is published there and the server binds 0.0.0.0 inside the -# sandbox; with --share-net the server binds this address itself. +# Address the server is reached at on the host: always this user's first +# publish address 127.H1.H2.1 (see net_publish_prefix), the one the +# --serve-ssh-config alias forwards to. With a private network the port is +# published there (and the session's other --publish ports with it) and the +# server binds 0.0.0.0 inside the sandbox; with --share-net the server binds +# this address itself. SERVE_ADDR="" SERVE_BIND="" # Default range: below the kernel's ephemeral range (32768-60999 on Linux, so no @@ -352,9 +354,11 @@ Wrapper options (consumed before opencode2 sees the command line): --serve Run the OpenCode web UI server inside the sandbox instead of the TUI, tools still sandboxed: `opencode2 serve` on the first free port of the range 24700-24709 (override the range with - BWOPENCODE2_SERVE_PORTS=LO-HI), published like --publish on your - loopback address 127.H1.H2.S (printed; with --share-net the - server binds that address itself, never 0.0.0.0). Open it + BWOPENCODE2_SERVE_PORTS=LO-HI), published like --publish, + always on your loopback address 127.H1.H2.1 (printed; the + --serve-ssh-config alias forwards to it; not with + --publish-addr; with --share-net the server binds that + address itself, never 0.0.0.0). Open it from a browser through an ssh tunnel; the command, a one-time pairing link (valid 5 minutes) and the password are printed at start; the web UI's "Use password" button also works. The @@ -449,11 +453,14 @@ HELP_EOF # `opencode2 serve` is refused as a command-line argument (see the guard below) # because the shared service it could reach runs tools outside any sandbox. # --serve runs the server inside THIS sandbox instead, so tools stay sandboxed: -# - reached on the host at this user's publish address 127.H1.H2.S (normally -# .1; see net_publish_prefix): with a private network the port is added to -# --publish and the server binds 0.0.0.0 inside the namespace (forwarded -# connections arrive at its interface); with --share-net the server binds -# the address itself. From a browser elsewhere, use an ssh -L tunnel; +# - reached on the host at this user's publish address 127.H1.H2.1 (see +# net_publish_prefix), always, so the --serve-ssh-config alias always +# reaches it: with a private network the port is added to --publish with +# that address fixed (a session never moves to another index; a clash +# stops the launch) and the server binds 0.0.0.0 inside the namespace +# (forwarded connections arrive at its interface); with --share-net the +# server binds the address itself. From a browser elsewhere, use an ssh +# -L tunnel; # - one server per invocation, on the first free port of a fixed range, so one # ssh_config block covers every session (--serve-ssh-config); # - protected by a password that reaches the server only as OPENCODE_PASSWORD. @@ -464,7 +471,7 @@ HELP_EOF # The launcher execs bwrap, so it cannot clean up or retry after the server # starts. A port is claimed with a file in the host-side state directory (not # mounted in the sandbox), which stops two launchers picking one port, and it -# must be free on the address checked (127.H1.H2.1 or --publish-addr); a claim +# must be free on 127.H1.H2.1 and not claimed there by another launch; a claim # whose process is gone is stale and is removed. The remaining race is another # program taking the port in the moment before the server binds it. @@ -541,6 +548,10 @@ serve_check_args() { echo "Error: --serve takes no opencode2 arguments (got '${TOOL_ARGS[*]}')." >&2 exit 1 fi + if [[ -n "${NET_PUBLISH_ADDR}" ]]; then + echo "Error: --serve always publishes on your address $(net_publish_prefix).1 (the one the --serve-ssh-config alias forwards to); it cannot be combined with --publish-addr." >&2 + exit 1 + fi if [[ -n "${SERVE_PORT_REQUEST}" ]]; then if [[ ! "${SERVE_PORT_REQUEST}" =~ ^[0-9]+$ ]] || ((10#${SERVE_PORT_REQUEST} < SERVE_PORT_LO || 10#${SERVE_PORT_REQUEST} > SERVE_PORT_HI)); then @@ -636,7 +647,8 @@ serve_choose_port() { serve_port_is_free "${SERVE_ADDR}" "${_p}" || continue else serve_claim_port "${_p}" || continue - if ! serve_port_is_free "${SERVE_ADDR}" "${_p}"; then + if ! serve_port_is_free "${SERVE_ADDR}" "${_p}" || + { [[ "${NET_SHARE}" -eq 0 ]] && net_publish_port_claimed "${SERVE_ADDR}" "${_p}"; }; then rm -f "${_f}" continue fi @@ -646,7 +658,7 @@ serve_choose_port() { done if [[ -n "${SERVE_PORT_REQUEST}" ]]; then - echo "Error: port ${SERVE_PORT_REQUEST} is in use on ${SERVE_ADDR}." >&2 + echo "Error: port ${SERVE_PORT_REQUEST} is in use, or claimed by another of your sandboxes, on ${SERVE_ADDR}." >&2 else echo "Error: no free port in ${SERVE_PORT_LO}-${SERVE_PORT_HI}; stop a session or see ${SERVE_STATE_DIR}/." >&2 fi @@ -732,11 +744,7 @@ SERVE_WRAPPER_EOF # Choose port and password, and give the sandbox its command. serve_prepare() { - if [[ "${NET_SHARE}" -eq 1 ]]; then - SERVE_ADDR="$(net_publish_prefix).1" - else - SERVE_ADDR="${NET_PUBLISH_ADDR:-$(net_publish_prefix).1}" - fi + SERVE_ADDR="$(net_publish_prefix).1" serve_choose_port serve_make_password if [[ "${NET_SHARE}" -eq 1 ]]; then @@ -747,6 +755,10 @@ serve_prepare() { # namespace; only the published port reaches it. SERVE_BIND="0.0.0.0" net_add_publish "${SERVE_PORT}" + # Never another session index: the ssh_config alias must reach it. + NET_PUBLISH_ADDR="${SERVE_ADDR}" + # shellcheck disable=SC2034 # read by bwrap_sandbox_lib.sh + NET_PUBLISH_ADDR_FIXED_BY="--serve" fi _TOOL_CMD=(/usr/bin/bash -c "${SERVE_WRAPPER}" bwopencode2-serve "${_TOOL_BIN}" "${SERVE_PORT}" "${SERVE_BIND}") TOOL_ARGS=() @@ -754,19 +766,17 @@ serve_prepare() { # Tell the user how to reach the server (stderr, before the exec). serve_print_info() { local _p="${SERVE_PORT}" _host _alias _addr="${SERVE_ADDR}" - [[ "${NET_SHARE}" -eq 1 ]] || _addr="${_NET_PUBLISH_ADDR_EFFECTIVE}" + if [[ "${NET_SHARE}" -eq 0 && "${_NET_PUBLISH_ADDR_EFFECTIVE}" != "${_addr}" ]]; then + echo "Error: --serve: the port was published on ${_NET_PUBLISH_ADDR_EFFECTIVE}, not ${_addr}." >&2 + exit 1 + fi _host="$(serve_host_name)" _alias="$(serve_ssh_alias)" { printf '\nbwopencode2: sandboxed OpenCode 2 web UI on %s:%s (this host only)\n' "${_addr}" "${_p}" printf ' 1. On the machine with your browser, forward the port:\n' printf ' ssh -L %s:%s:%s %s\n' "${_p}" "${_addr}" "${_p}" "${_host}" - if [[ "${_addr}" == "$(net_publish_prefix).1" ]]; then - printf ' or use the ssh_config alias (bwopencode2 --serve-ssh-config): ssh %s\n' "${_alias}" - else - printf ' (the ssh_config alias from --serve-ssh-config forwards to %s.1, so it does\n' "$(net_publish_prefix)" - printf ' not reach this session: use the command above)\n' - fi + printf ' or use the ssh_config alias (bwopencode2 --serve-ssh-config): ssh %s\n' "${_alias}" printf ' then open http://127.0.0.1:%s/ there. On this host itself, open http://%s:%s/.\n' "${_p}" "${_addr}" "${_p}" printf ' 2. Open the pairing link printed below once the server is up (it works once and\n' printf ' expires in 5 minutes), or open the page above, click "Use password"\n' diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index c42d7d6..039378c 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -933,13 +933,16 @@ bwopencode2 --serve-ssh-config server; stop it with Ctrl-C in its terminal. - **Published like `--publish`.** The server's port is added to [`--publish`](#opening-ports), so it is reachable on - the host only at your publish address `127.H1.H2.S:PORT` (normally `.1`; the - launcher prints it), never on another address. Inside the private network + the host only at your first publish address `127.H1.H2.1:PORT` (the + launcher prints it), **always**: the session's publish address is fixed to + `.1` and never moves to another index, so the `--serve-ssh-config` alias + always reaches it. If one of the session's ports is taken there (another + of your `--publish` ports), the launch stops and says so instead. + `--publish-addr` is refused with `--serve`. Inside the private network namespace the server listens on `0.0.0.0`, because forwarded connections arrive at the namespace's interface; nothing else reaches it. With - `--share-net` the server listens on `127.H1.H2.1` itself. `--publish-addr - 127.0.0.1` gives the old address. Reach it from another machine through an - ssh tunnel. Works with both network backends. + `--share-net` the server listens on `127.H1.H2.1` itself. Reach it from + another machine through an ssh tunnel. Works with both network backends. - **Free port from a fixed range**, `24700-24709` by default (ten sessions at once). The range sits below the kernel's ephemeral range (32768-60999 on Linux), so no outgoing connection takes a server port, and clear of 49374 @@ -948,16 +951,15 @@ bwopencode2 --serve-ssh-config if it overlaps the ephemeral range). `--serve-port N` asks for one port in the range and fails if it is taken, so a saved tunnel always matches. - **The ssh tunnel.** At start the launcher prints - `ssh -L P:127.H1.H2.S:P ` with the host from `hostname -f`; then open + `ssh -L P:127.H1.H2.1:P ` with the host from `hostname -f`; then open `http://127.0.0.1:P/` on the machine with the browser. For all ports at once, `bwopencode2 --serve-ssh-config` (run on the host, as the user who runs the sessions: the address depends on the uid) prints a block for `~/.ssh/config` on the machine with the browser (alias `-oc`, one `LocalForward` per port of the range to `127.H1.H2.1`, `ExitOnForwardFailure no` so a busy local port does not stop the others). - The port is chosen free on `127.H1.H2.1`, so a session normally gets that - address; if it does not (another of your `--publish` ports is busy there), - the launcher says that the alias does not reach it. Keep the same port number + The port is chosen free on `127.H1.H2.1`, and not claimed there by another + of your sandboxes. Keep the same port number at both ends: the browser cookie is named by it (see below). Add `User` and `ProxyJump` to the alias as needed. Each user has their own address, so sessions of different users on one host no longer compete for @@ -969,7 +971,7 @@ bwopencode2 --serve-ssh-config directly: it starts the server, waits until it answers, asks it for a one-time pairing code and prints the link twice, for the tunnel (`http://127.0.0.1:/auth/connect/`) and for this host - (`http://127.H1.H2.S:/...`). Open one to be signed in, with the 30-day cookie, without typing the password. + (`http://127.H1.H2.1:/...`). Open one to be signed in, with the 30-day cookie, without typing the password. The link works once and expires after 5 minutes; after that, or if the printed link failed, click **Use password** and enter the password. The password reaches `curl` on its standard input (`-K -`), never on a command diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index 22a825a..260769e 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -77,6 +77,9 @@ EXTRA_RW_PATHS=() # "PORTS[:SANDBOXPORTS]" (PORTS is N or LOW-HIGH). NET_PUBLISH=() NET_PUBLISH_ADDR="" +# Set (to the option's name) when a launcher fixes NET_PUBLISH_ADDR itself +# (bwopencode2 --serve): the address must then not move, and errors say why. +NET_PUBLISH_ADDR_FIXED_BY="" NET_HOST_PORTS=() NET_SHARE=0 NET_BACKEND_FORCE="" @@ -3469,6 +3472,28 @@ _net_claimed_port() { done } +# net_publish_port_claimed ADDR PORT +# True when a live claim of another launch holds host PORT on ADDR. Read +# without the lock (for an early choice, such as the --serve port); the +# final check is made under the lock by _net_derive_publish_addr. +net_publish_port_claimed() { + local _f _pid _start _ports _r + _net_runtime_base + for _f in "${_NET_RUNTIME_BASE}/claims/$1".*; do + [[ -f "${_f}" ]] || continue + _pid="$(sed -n 's/^pid=//p' "${_f}" 2> /dev/null)" + _start="$(sed -n 's/^start=//p' "${_f}" 2> /dev/null)" + if [[ "${_pid}" == "$$" ]] || ! bwrap_proc_is "${_pid}" "${_start}"; then + continue + fi + _ports="$(sed -n 's/^ports=//p' "${_f}" 2> /dev/null)" + for _r in ${_ports}; do + ((${_r%-*} <= $2 && $2 <= ${_r#*-})) && return 0 + done + done + return 1 +} + # _net_claims_write # Record this launch's claim on _NET_PUBLISH_ADDR_EFFECTIVE. _net_claims_write() { @@ -3561,10 +3586,13 @@ _net_choose_publish_addr() { _net_busy_port "${NET_PUBLISH_ADDR}" if [[ -n "${_NET_BUSY_PID}" ]]; then echo "Error: --publish: port ${_NET_BUSY} on ${NET_PUBLISH_ADDR} is claimed by another of your sandboxes (launcher pid ${_NET_BUSY_PID})." >&2 - exit 1 + elif [[ -n "${_NET_BUSY}" ]]; then + echo "Error: --publish: port ${_NET_BUSY} is already in use on ${NET_PUBLISH_ADDR} (see: ss -ltnp)." >&2 fi if [[ -n "${_NET_BUSY}" ]]; then - echo "Error: --publish: port ${_NET_BUSY} is already in use on ${NET_PUBLISH_ADDR} (see: ss -ltnp)." >&2 + if [[ -n "${NET_PUBLISH_ADDR_FIXED_BY}" ]]; then + echo " ${NET_PUBLISH_ADDR_FIXED_BY} always publishes on ${NET_PUBLISH_ADDR}; choose other --publish ports, or run again." >&2 + fi exit 1 fi return 0 diff --git a/tests/fakes/_nsproxy.py b/tests/fakes/_nsproxy.py index 567b266..40a6277 100644 --- a/tests/fakes/_nsproxy.py +++ b/tests/fakes/_nsproxy.py @@ -129,5 +129,9 @@ def loop(): def port_range(spec): - lo, _, hi = spec.partition("-") - return range(int(lo), int(hi or lo) + 1) + """Ports of a pasta port list: N, LO-HI, comma-separated.""" + ports = [] + for part in spec.split(","): + lo, _, hi = part.partition("-") + ports.extend(range(int(lo), int(hi or lo) + 1)) + return ports diff --git a/tests/fakes/pasta-bin/pasta b/tests/fakes/pasta-bin/pasta index 384a9ca..5626ed4 100755 --- a/tests/fakes/pasta-bin/pasta +++ b/tests/fakes/pasta-bin/pasta @@ -101,7 +101,8 @@ if child == 0: os._exit(1) os.write(ready_w, b"1") os.close(ready_w) - os.read(go_r, 1) + if os.read(go_r, 1) != b"1": + os._exit(1) # pasta failed during setup: never start the command os.close(go_r) try: os.execvp(cmd[0], cmd) @@ -120,10 +121,11 @@ import socket # noqa: E402 for spec in fwd_t: addr, _, ports = spec.rpartition("/") - hports, _, gports = ports.partition(":") - hr = list(_nsproxy.port_range(hports)) - gr = list(_nsproxy.port_range(gports or hports)) - for hp, gp in zip(hr, gr): + pairs = [] + for part in ports.split(","): + hports, _, gports = part.partition(":") + pairs += zip(_nsproxy.port_range(hports), _nsproxy.port_range(gports or hports)) + for hp, gp in pairs: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: @@ -138,8 +140,11 @@ for spec in fwd_t: _nsproxy.forward(s, lambda gp=gp: proxy.connect(NS_ADDR, gp), log, "-t %d" % hp) for spec in fwd_T: addr, _, ports = spec.rpartition("/") - sports, _, hports = ports.partition(":") - for sp, hp in zip(_nsproxy.port_range(sports), _nsproxy.port_range(hports or sports)): + pairs = [] + for part in ports.split(","): + sports, _, hports = part.partition(":") + pairs += zip(_nsproxy.port_range(sports), _nsproxy.port_range(hports or sports)) + for sp, hp in pairs: s = proxy.listen(addr or "127.0.0.1", sp) _nsproxy.forward(s, lambda hp=hp: socket.create_connection(("127.0.0.1", hp)), log, "-T %d" % sp) From 7943d1d9fcf716f755b7fc0459e997b3be7d07e5 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 10 Nov 2026 10:08:53 -0500 Subject: [PATCH 20/37] fix(bwrap): forward --host-port to the sandbox's own loopback pasta's -T takes no listening address ("Listening address not allowed for -T"): it always listens on the namespace's loopback. So --host-port PORT[:SANDBOXPORT] now passes -T SANDBOXPORT:PORT and the agent reaches the host service at 127.0.0.1:SANDBOXPORT; host.internal, 127.0.42.1 and BWRAP_NET_HOST_ADDR are gone. That sandbox port is held by pasta, so a --publish to it is refused, and the context file says the agent cannot serve on it. The fake pasta now rejects an address on -T/-U and listens on the namespace's loopback, as the real one does; it had accepted the old form, which is why the tests passed. Assisted-by: opencode:claude-opus-5-5 --- README.md | 2 +- bin/bwclaude | 3 +- bin/bwcodex | 3 +- bin/bwcopilot | 3 +- bin/bwopencode | 3 +- bin/bwopencode2 | 3 +- docs/bw-wrappers.md | 27 ++++++++++------- lib/bwrap_sandbox_lib.sh | 60 +++++++++++++++++++++++++------------ tests/fakes/pasta-bin/pasta | 13 +++++--- 9 files changed, 77 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 7f86ec5..26841fb 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ Common wrapper options: | `--no-sandbox-context` | Do not tell the agent about the sandbox (by default a short description of the mounts is added to its instructions) | | `--publish PORT[:SANDBOXPORT]` | Make a server in the sandbox reachable on the host at a per-user, per-session `127.x.y.z` address (printed at launch) | | `--publish-addr ADDR` | Use `ADDR` (in `127.0.0.0/8`, e.g. `127.0.0.1` for OAuth) for `--publish` | -| `--host-port PORT[:SANDBOXPORT]` | Make the host's `127.0.0.1:PORT` reachable in the sandbox at `host.internal` | +| `--host-port PORT[:SANDBOXPORT]` | Make the host's `127.0.0.1:PORT` reachable in the sandbox at `127.0.0.1:SANDBOXPORT` (pasta only) | | `--share-net` | Share the host network instead of a private namespace (the behaviour before private namespaces) | | `--net-backend pasta\|slirp4netns` | Use only this network backend | diff --git a/bin/bwclaude b/bin/bwclaude index e59679f..0ead802 100755 --- a/bin/bwclaude +++ b/bin/bwclaude @@ -93,7 +93,8 @@ # Use ADDR (in 127.0.0.0/8) for --publish instead. # --host-port PORT[:SANDBOXPORT] # Make the host's 127.0.0.1:PORT reachable in the sandbox at -# host.internal:SANDBOXPORT. May be repeated. +# 127.0.0.1:SANDBOXPORT (pasta only; that sandbox port is +# then taken). May be repeated. # --share-net Share the host network instead (the behaviour before # private namespaces); not with the options above. # --net-backend pasta|slirp4netns diff --git a/bin/bwcodex b/bin/bwcodex index 9ed774d..304b4f2 100755 --- a/bin/bwcodex +++ b/bin/bwcodex @@ -110,7 +110,8 @@ # Use ADDR (in 127.0.0.0/8) for --publish instead. # --host-port PORT[:SANDBOXPORT] # Make the host's 127.0.0.1:PORT reachable in the sandbox at -# host.internal:SANDBOXPORT. May be repeated. +# 127.0.0.1:SANDBOXPORT (pasta only; that sandbox port is +# then taken). May be repeated. # --share-net Share the host network instead (the behaviour before # private namespaces); not with the options above. # --net-backend pasta|slirp4netns diff --git a/bin/bwcopilot b/bin/bwcopilot index 3dc0c85..c4bda22 100755 --- a/bin/bwcopilot +++ b/bin/bwcopilot @@ -78,7 +78,8 @@ # Use ADDR (in 127.0.0.0/8) for --publish instead. # --host-port PORT[:SANDBOXPORT] # Make the host's 127.0.0.1:PORT reachable in the sandbox at -# host.internal:SANDBOXPORT. May be repeated. +# 127.0.0.1:SANDBOXPORT (pasta only; that sandbox port is +# then taken). May be repeated. # --share-net Share the host network instead (the behaviour before # private namespaces); not with the options above. # --net-backend pasta|slirp4netns diff --git a/bin/bwopencode b/bin/bwopencode index 91db335..f776efe 100755 --- a/bin/bwopencode +++ b/bin/bwopencode @@ -90,7 +90,8 @@ # Use ADDR (in 127.0.0.0/8) for --publish instead. # --host-port PORT[:SANDBOXPORT] # Make the host's 127.0.0.1:PORT reachable in the sandbox at -# host.internal:SANDBOXPORT. May be repeated. +# 127.0.0.1:SANDBOXPORT (pasta only; that sandbox port is +# then taken). May be repeated. # --share-net Share the host network instead (the behaviour before # private namespaces); not with the options above. # --net-backend pasta|slirp4netns diff --git a/bin/bwopencode2 b/bin/bwopencode2 index 13a8581..0b6d63c 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -132,7 +132,8 @@ # Use ADDR (in 127.0.0.0/8) for --publish instead. # --host-port PORT[:SANDBOXPORT] # Make the host's 127.0.0.1:PORT reachable in the sandbox at -# host.internal:SANDBOXPORT. May be repeated. +# 127.0.0.1:SANDBOXPORT (pasta only; that sandbox port is +# then taken). May be repeated. # --share-net Share the host network instead (the behaviour before # private namespaces); not with the options above. # --net-backend pasta|slirp4netns diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 039378c..1f3793a 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -108,7 +108,7 @@ Every `bw*` wrapper accepts these options: | `--no-sandbox-context` | Do not generate the [sandbox context file](#sandbox-context) that tells the agent what is mounted | | `--publish PORT[:SANDBOXPORT]` | Make a server in the sandbox reachable on the host at a per-user, per-session loopback address. May be repeated. See [Network](#network) | | `--publish-addr ADDR` | Use `ADDR` (in `127.0.0.0/8`) for `--publish` instead of the derived address, for example `127.0.0.1` for OAuth redirects | -| `--host-port PORT[:SANDBOXPORT]` | Make the host's `127.0.0.1:PORT` reachable in the sandbox at `host.internal:SANDBOXPORT`. May be repeated (pasta only) | +| `--host-port PORT[:SANDBOXPORT]` | Make the host's `127.0.0.1:PORT` reachable in the sandbox at `127.0.0.1:SANDBOXPORT`, which it then takes. May be repeated (pasta only) | | `--share-net` | Share the host network instead of a private namespace (the behaviour before private namespaces); not with the forwarding options | | `--net-backend pasta\|slirp4netns` | Use only this backend for the private namespace (normally pasta, else slirp4netns) | @@ -248,8 +248,8 @@ and makes the tool load it. mounts, not hard links, and are edited at their source; the working directory (its access taken from the effective mounts) and whether it is a git root; a Network section (private namespace, DNS, the published ports with their host - URLs and the "bind `0.0.0.0`" rule, the host services under - `host.internal`); a + URLs and the "bind `0.0.0.0`" rule, the host services and the sandbox ports + they take); a table of every mount under `$HOME` marked `rw` / `ro` / `copy` (a writable copy of a host file, lost at exit) / `tmpfs`; the system mounts collapsed into one line each, with writable exceptions such as `--rw-path /usr/local` @@ -343,7 +343,7 @@ backend forwards to the first nameserver in the host's `/etc/resolv.conf` (port 53 only), so split DNS (VPN, tailnet) keeps working. Only IPv4 nameservers are supported; the wrapper warns when the host has none. The host's search domains are not passed on, so use fully qualified names. `/etc/hosts` is -the host's file, plus `host.internal` when `--host-port` is used. +the host's file. ### Opening ports @@ -354,17 +354,17 @@ is accepted, and a mapped range must have the same length. | Option | Direction | Host side | Sandbox side | | --- | --- | --- | --- | | `--publish PORT[:SANDBOXPORT]` | sandbox server → host | `127.H1.H2.S:PORT` (or `--publish-addr`) | the server on `SANDBOXPORT` (default `PORT`), which must listen on `0.0.0.0` | -| `--host-port PORT[:SANDBOXPORT]` | host service → sandbox | `127.0.0.1:PORT` | `host.internal:SANDBOXPORT` (`127.0.42.1`, default `PORT`) | +| `--host-port PORT[:SANDBOXPORT]` | host service → sandbox | `127.0.0.1:PORT` | `127.0.0.1:SANDBOXPORT` (default `PORT`), held by pasta; pasta only | Forwards are printed to stderr at launch, so they stay in the scrollback above the TUI, and listed in the [sandbox context file](#sandbox-context). Inside the -sandbox `BWRAP_NET_PUBLISH`, `BWRAP_NET_PUBLISH_ADDR`, `BWRAP_NET_HOST_PORTS` -and `BWRAP_NET_HOST_ADDR` hold the same information. +sandbox `BWRAP_NET_PUBLISH`, `BWRAP_NET_PUBLISH_ADDR` and +`BWRAP_NET_HOST_PORTS` hold the same information. ```text bwopencode2: network: private namespace (pasta); publish address 127.65.81.1 bwopencode2: publish http://127.65.81.1:8000/ -> sandbox :8000 (bind 0.0.0.0 inside) -bwopencode2: host-port host 127.0.0.1:5432 -> sandbox host.internal:5432 (host service sees a loopback client) +bwopencode2: host-port host 127.0.0.1:5432 -> sandbox 127.0.0.1:5432 (host service sees a loopback client) ``` **`--publish`.** The host address is `127.H1.H2.S`: `H1.H2` comes from a hash @@ -397,9 +397,14 @@ names it (some agent logins and MCP servers open a browser on the host that calls back into the sandbox). The launch then fails if a requested port is already in use or claimed by another of your sandboxes on `ADDR`. -**`--host-port`** makes one host loopback service visible in the sandbox as -`host.internal`, so the agent's own `127.0.0.1:PORT` stays free (it can run its -own Postgres on 5432 next to the host's). The host side is always +**`--host-port`** makes one host loopback service visible in the sandbox at +`127.0.0.1:SANDBOXPORT` (and `localhost`), as pasta's `-T` does: pasta listens +on the sandbox's loopback and connects to the host's `127.0.0.1:PORT`. pasta +takes no other listening address for `-T`, so there is no separate name for +the host. That sandbox port is then taken: the agent cannot run its own server +on it, and a `--publish` to the same sandbox port is refused. To keep `PORT` +free in the sandbox (to run its own Postgres on 5432 next to the host's), +map it elsewhere: `--host-port 5432:15432`. The host side is always `127.0.0.1`. Nothing checks that the host port is open or who owns it. **The host service sees a loopback client and may trust it** (no password for local connections, admin APIs): only forward services you would let the agent diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index 260769e..8374f1d 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -2259,7 +2259,9 @@ build_etc_mounts() { # per-launch opt-in: # --publish PORT[:SANDBOXPORT] sandbox server -> host 127.H1.H2.S:PORT # --host-port PORT[:SANDBOXPORT] host 127.0.0.1:PORT -> sandbox -# host.internal:SANDBOXPORT (127.0.42.1) +# 127.0.0.1:SANDBOXPORT (pasta -T, which +# always listens on the sandbox's loopback; +# it takes no listening address) # Without a usable backend (neither pasta nor slirp4netns installed, no # /dev/net/tun, or no unprivileged user namespaces) the launch stops (exit 3). # --net-backend NAME restricts the choice to one backend (for testing). @@ -2291,8 +2293,6 @@ _NET_PRIVATE=0 _NET_BACKEND=none _NET_SHARED_REASON="" _NET_PUBLISH_ADDR_EFFECTIVE="" -_NET_HOST_INTERNAL_ADDR="127.0.42.1" -_NET_HOST_INTERNAL_NAME="host.internal" _NET_DNS_FORWARD_ADDR="169.254.1.1" # The sandbox's nameserver address with the chosen backend. _NET_DNS_EFFECTIVE="" @@ -2304,9 +2304,12 @@ _NET_HOST_PORT_DENY=(631 2375 2376 6000-6063 41112 49374) _NET_PUBLISH_DESC=() _NET_HOST_DESC=() # Low and high ends of the listening side of every accepted spec, per -# direction, for the overlap check. +# direction, for the overlap check, and of the sandbox side of --publish (a +# --host-port sandbox port is held by pasta, so a server cannot use it). _NET_PUB_LO=() _NET_PUB_HI=() +_NET_PUBSBX_LO=() +_NET_PUBSBX_HI=() _NET_HP_LO=() _NET_HP_HI=() @@ -2343,8 +2346,10 @@ in 1024-65535; N or LOW-HIGH ranges are accepted. example 127.0.0.1 for OAuth redirect URIs that require it. --host-port PORT[:SANDBOXPORT] Make the host's 127.0.0.1:PORT reachable in the sandbox at - host.internal:SANDBOXPORT (default PORT). The host service - sees a loopback client and may trust it. May be repeated. + 127.0.0.1:SANDBOXPORT (default PORT; pasta only). That port + of the sandbox is then taken: use SANDBOXPORT to keep PORT + free for a server of the agent. The host service sees a + loopback client and may trust it. May be repeated. Refused: 631 (CUPS), 2375-2376 (Docker), 6000-6063 (X11), 41112 (Tailscale), 49374 (OpenCode 2 service). @@ -3302,15 +3307,34 @@ _net_check_overlap() { net_add_publish() { _net_check_port_spec --publish "$1" _net_check_overlap --publish "host port" "${_NET_A_LO}" "${_NET_A_HI}" _NET_PUB_LO _NET_PUB_HI + _net_check_sandbox_ports "${_NET_B_LO}" "${_NET_B_HI}" _NET_HP_LO _NET_HP_HI _NET_PUB_LO+=("${_NET_A_LO}") _NET_PUB_HI+=("${_NET_A_HI}") + _NET_PUBSBX_LO+=("${_NET_B_LO}") + _NET_PUBSBX_HI+=("${_NET_B_HI}") NET_PUBLISH+=("${_NET_SPEC}") } +# _net_check_sandbox_ports LO HI LOS HIS +# Exit 1 when sandbox ports LO-HI overlap a range in the arrays LOS/HIS: +# one sandbox port cannot both receive --publish and be held by pasta for +# --host-port. +_net_check_sandbox_ports() { + local _lo="$1" _hi="$2" _i _r="$1" + local -n _los="$3" _his="$4" + [[ ${_hi} -eq ${_lo} ]] || _r+="-${_hi}" + for _i in "${!_los[@]}"; do + if [[ ${_lo} -le ${_his[${_i}]} && ${_hi} -ge ${_los[${_i}]} ]]; then + echo "Error: sandbox port ${_r} is used by both --publish and --host-port; give --host-port another sandbox port (PORT:SANDBOXPORT)." >&2 + exit 1 + fi + done +} + # net_add_host_port SPEC # --host-port PORT[:SANDBOXPORT]: make the host's 127.0.0.1:PORT reachable in -# the sandbox at host.internal:SANDBOXPORT (default PORT). Ports on the -# deny list (_NET_HOST_PORT_DENY) are refused. +# the sandbox at 127.0.0.1:SANDBOXPORT (default PORT), where pasta then holds +# the port. Ports on the deny list (_NET_HOST_PORT_DENY) are refused. net_add_host_port() { local _d _dlo _dhi _net_check_port_spec --host-port "$1" @@ -3323,6 +3347,7 @@ net_add_host_port() { fi done _net_check_overlap --host-port "sandbox port" "${_NET_B_LO}" "${_NET_B_HI}" _NET_HP_LO _NET_HP_HI + _net_check_sandbox_ports "${_NET_B_LO}" "${_NET_B_HI}" _NET_PUBSBX_LO _NET_PUBSBX_HI _NET_HP_LO+=("${_NET_B_LO}") _NET_HP_HI+=("${_NET_B_HI}") NET_HOST_PORTS+=("${_NET_SPEC}") @@ -3655,7 +3680,6 @@ build_net_isolation() { "BWRAP_NET_PUBLISH=" "BWRAP_NET_PUBLISH_ADDR=" "BWRAP_NET_HOST_PORTS=" - "BWRAP_NET_HOST_ADDR=" ) return 0 fi @@ -3713,7 +3737,9 @@ build_net_isolation() { fi if [[ ${#NET_HOST_PORTS[@]} -gt 0 ]]; then - # pasta's -T takes NAMESPACE_PORT:HOST_PORT, the reverse of ours. + # pasta's -T takes NAMESPACE_PORT:HOST_PORT, the reverse of ours, and + # no listening address: it always listens on the namespace's + # loopback (127.0.0.1 and ::1). local -a _tspec=() for _spec in "${NET_HOST_PORTS[@]}"; do _host="${_spec%%:*}" @@ -3723,11 +3749,11 @@ build_net_isolation() { else _tspec+=("${_sbx}:${_host}") fi - _NET_HOST_DESC+=("127.0.0.1:${_host}|${_NET_HOST_INTERNAL_NAME}:${_sbx}") + _NET_HOST_DESC+=("127.0.0.1:${_host}|127.0.0.1:${_sbx}") done for _i in "${!PASTA_ARGS[@]}"; do if [[ "${PASTA_ARGS[${_i}]}" == -T ]]; then - PASTA_ARGS[_i + 1]="${_NET_HOST_INTERNAL_ADDR}/$(_net_join "${_tspec[@]}")" + PASTA_ARGS[_i + 1]="$(_net_join "${_tspec[@]}")" fi done fi @@ -3748,9 +3774,6 @@ build_net_isolation() { else _hosts="127.0.0.1 localhost"$'\n'"::1 localhost" fi - if [[ ${#NET_HOST_PORTS[@]} -gt 0 ]]; then - _hosts+=$'\n'"${_NET_HOST_INTERNAL_ADDR} ${_NET_HOST_INTERNAL_NAME}" - fi bwrap_fd_from_data "generated /etc/hosts" "${_hosts}" BWRAP_ARGS+=(--ro-bind-data "${_BWRAP_FD}" /etc/hosts) @@ -3759,7 +3782,6 @@ build_net_isolation() { "BWRAP_NET_PUBLISH=$(_net_join ${NET_PUBLISH[@]+"${NET_PUBLISH[@]}"})" "BWRAP_NET_PUBLISH_ADDR=${_NET_PUBLISH_ADDR_EFFECTIVE}" "BWRAP_NET_HOST_PORTS=$(_net_join ${NET_HOST_PORTS[@]+"${NET_HOST_PORTS[@]}"})" - "BWRAP_NET_HOST_ADDR=$([[ ${#NET_HOST_PORTS[@]} -eq 0 ]] || printf '%s' "${_NET_HOST_INTERNAL_ADDR}")" ) } @@ -4821,11 +4843,11 @@ _bwrap_context_network() { else _t+="${_nl}${_nl}" for _d in "${_NET_HOST_DESC[@]}"; do - _t+="- \`${_d#*|}\` (\`${_NET_HOST_INTERNAL_ADDR}\`) reaches the host's \`${_d%%|*}\`.${_nl}" + _t+="- \`${_d#*|}\` here reaches the host's \`${_d%%|*}\`.${_nl}" done - _t+="${_nl}Your own \`127.0.0.1\` is unaffected: a server you start on the same port is separate.${_nl}${_nl}" + _t+="${_nl}These ports of this sandbox are taken by the forward: you cannot run your own server on them.${_nl}${_nl}" fi - _t+="\`BWRAP_NET_BACKEND\` is \`${_NET_BACKEND}\`; \`BWRAP_NET_PUBLISH\`, \`BWRAP_NET_PUBLISH_ADDR\`, \`BWRAP_NET_HOST_PORTS\` and \`BWRAP_NET_HOST_ADDR\` hold the same information.${_nl}${_nl}" + _t+="\`BWRAP_NET_BACKEND\` is \`${_NET_BACKEND}\`; \`BWRAP_NET_PUBLISH\`, \`BWRAP_NET_PUBLISH_ADDR\` and \`BWRAP_NET_HOST_PORTS\` hold the same information.${_nl}${_nl}" _net_log_context_text _CTX_NET="${_t}${_NET_LOG_CTX}" } diff --git a/tests/fakes/pasta-bin/pasta b/tests/fakes/pasta-bin/pasta index 5626ed4..91b4b6e 100755 --- a/tests/fakes/pasta-bin/pasta +++ b/tests/fakes/pasta-bin/pasta @@ -4,8 +4,9 @@ Like the real one it closes inherited descriptors above 2 before opening --log-file, creates a user namespace (mapping the caller to uid 0) and a network namespace, starts the command in it and stays its parent, and -forwards -t (host -> namespace interface address) and -T (namespace -> host -loopback) ports. The namespace gets a dummy interface with 192.168.77.2 +forwards -t (host -> namespace interface address) and -T (namespace +loopback -> host loopback) ports. Like the real one (fwd_rule.c), -T and -U +take no listening address: "-T ADDR/..." stops it with pasta's error. The namespace gets a dummy interface with 192.168.77.2 instead of a tap with the host's address. With --debug and --log-file it writes header, startup and "Flow" lines to the log file (blocking writes, as pasta's are). The failure mode is read from $FAKE_NET_DIR/pasta.fail @@ -53,6 +54,10 @@ while i < len(args): i += 1 if not cmd: sys.exit("fake pasta: no command") +for opt, specs in (("T", fwd_T), ("U", [opts.get("-U", "none")])): + for spec in specs: + if spec != "none" and "/" in spec: + sys.exit("0.0012: ERROR: Listening address not allowed for -%s %s" % (opt, spec)) os.closerange(3, 4096) @@ -139,13 +144,13 @@ for spec in fwd_t: s.listen(16) _nsproxy.forward(s, lambda gp=gp: proxy.connect(NS_ADDR, gp), log, "-t %d" % hp) for spec in fwd_T: - addr, _, ports = spec.rpartition("/") + ports = spec pairs = [] for part in ports.split(","): sports, _, hports = part.partition(":") pairs += zip(_nsproxy.port_range(sports), _nsproxy.port_range(hports or sports)) for sp, hp in pairs: - s = proxy.listen(addr or "127.0.0.1", sp) + s = proxy.listen("127.0.0.1", sp) _nsproxy.forward(s, lambda hp=hp: socket.create_connection(("127.0.0.1", hp)), log, "-T %d" % sp) os.write(go_w, b"1") From 336115485713e007012bc33bf2b8d437eee7a664 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 10 Nov 2026 10:34:19 -0500 Subject: [PATCH 21/37] feat(bwrap): condense the network log and name destinations pasta's debug log has about 34 lines per connection (state changes, timers, ACK bookkeeping) plus a doubled setup dump, and only addresses. lib/bwrap_netlog.py condense now turns it into one logfmt line when a connection opens and one when it closes (with its duration), a fail line for connections that never became active, and pasta's warnings and errors; 5000 lines of a real session became 280. To name destinations, bwrap_netlog.py dns runs as a DNS relay on 127.53.0.1:53 in the sandbox's network namespace, started by the descriptor relay before bwrap (it waits until the relay listens, at most 0.25 s) and dropping its capabilities after binding. It logs one dns line per lookup and forwards to pasta's DNS address, which resolv.conf lists second so lookups go on working if the relay is down. The condenser adds name= to connections from the answers. Both need Python 3.6+ (platform-python on RHEL 8); without it the log is unfiltered as before. Assisted-by: opencode:claude-opus-5-5 --- docs/bw-wrappers.md | 52 ++++- lib/bwrap_netlog.py | 459 +++++++++++++++++++++++++++++++++++++++ lib/bwrap_sandbox_lib.sh | 111 ++++++++-- 3 files changed, 602 insertions(+), 20 deletions(-) create mode 100644 lib/bwrap_netlog.py diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 1f3793a..9082ae3 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -27,7 +27,8 @@ option list; the tables below summarise the common surface. `/usr/libexec/platform-python`). - For the [network log](#network-log): `systemd-cat` (systemd) with a running journal, or `logger` (util-linux) with `/dev/log`. Without either the - wrappers warn and run without the log. + wrappers warn and run without the log. Python 3.6+ (standard library) to + condense it and to name destinations (DNS relay). ## Managed system paths @@ -431,9 +432,31 @@ change them, and they are the data needed for a later egress allowlist. | Record | Content | | --- | --- | | start | `event=start session=ID launcher=... user=... uid=... pid=... backend=... flows=logged\|unavailable publish=ADDR:PORTS host_ports=... dir=...` | -| each pasta line | `session=ID ` (setup, then a line for each flow and its state changes) | +| pasta | `session=ID pasta version=... pid=...`, and pasta's warnings and errors (`pasta level=error msg=...`) | +| dns | `session=ID dns name=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21` | +| open | `session=ID flow=7 open proto=tcp dir=out src=ADDR:PORT dst=140.82.112.21:443 name=github.com t=21.359` | +| close | `session=ID flow=7 close proto=tcp dir=out dst=140.82.112.21:443 name=github.com t=33.770 secs=12.411` | +| fail | `session=ID flow=9 fail ...`: a connection that never became active (refused, unreachable) | | end | `event=end session=ID ...`, when the sandbox has exited | +Every line is `key=value` (logfmt), so Loki's `| logfmt` can filter on any +field, for example the names an agent reached: +`{syslog_identifier="bwrap-net"} | logfmt | dir="out"` grouped by `name`. + +- `dir` is `out` (the sandbox to the network), `publish` (a host client to a + `--publish` port) or `host-port` (the sandbox to a `--host-port` service). + `via=` is the address on the other side when it differs (the sandbox port + for `publish`). `t` is seconds since pasta started; `secs` is the + connection's duration. Flows still open when the session ends get a close + with `reason=session-end`. +- `flow` is pasta's flow slot, which it reuses: it identifies a connection + only between its open and its close. +- pasta logs no byte counts. +- `name` is the name of the last lookup, through the DNS relay, that returned + the destination address. It is a record, not proof: several names can share + one address (CDNs), and a lookup that bypasses the relay (a query sent to + `169.254.1.1` directly, DNS over HTTPS) leaves no name. + - **Tag** (`SYSLOG_IDENTIFIER`): `bwrap-net`. `journalctl -t bwrap-net` shows all sessions; `journalctl -t bwrap-net -g ID` one. Inside the sandbox `BWRAP_SESSION` is the same ID, and the context file names it. @@ -468,6 +491,31 @@ change them, and they are the data needed for a later egress allowlist. before the marker (a failed start), the supervisor prints pasta's lines on your terminal, prefixed `: pasta:`, just after the launcher has exited. + - **Condensed.** pasta's debug log has about 34 lines per connection (state + changes, timers, ACK and buffer bookkeeping) and repeats its setup. The + supervisor sends it through `lib/bwrap_netlog.py condense`, which keeps one + line when a connection opens and one when it closes, plus pasta's warnings + and errors, and drops the rest (in a real session, 5000 lines became + 280). It writes to `systemd-cat` / `logger` itself. +- **DNS relay (pasta).** To name destinations, `lib/bwrap_netlog.py dns` + listens on `127.53.0.1:53` (UDP and TCP) in the sandbox's network namespace, + forwards each lookup to `169.254.1.1` (pasta's DNS forward) and writes one + `dns` line per lookup to the log. The condenser keeps a map from the answers' + addresses to names. The sandbox's `/etc/resolv.conf` lists the relay first + and `169.254.1.1` second, so lookups go on working, at once, if the relay is + down (its port is then closed); pasta's own leg of a relayed lookup (UDP to + `169.254.1.1:53`) is not logged as a connection. + - The descriptor relay starts it before bwrap, as root of pasta's user + namespace, which may bind port 53 there; it then drops every capability. + It runs outside bwrap: the agent cannot see it (separate process + namespace), and cannot take its port (it has no capabilities). + - The descriptor relay waits until it listens (it says so on a pipe) before + starting bwrap, at most 0.25 s, so the first lookups are logged too. In + practice that is a Python start, some tens of milliseconds. + - It exits when bwrap does. +- Both need Python 3.6 or later, standard library only (RHEL 8's + `/usr/libexec/platform-python`, else `python3`). Without it pasta's lines go + to the log unfiltered, and DNS goes straight to `169.254.1.1`. ### Implementation notes: pasta diff --git a/lib/bwrap_netlog.py b/lib/bwrap_netlog.py new file mode 100644 index 0000000..d50459c --- /dev/null +++ b/lib/bwrap_netlog.py @@ -0,0 +1,459 @@ +#!/usr/bin/env python3 +"""Network log helpers for the bw* sandbox launchers (see bwrap_sandbox_lib.sh, +"Network log"). Standard library only; runs on Python 3.6 (RHEL 8 +platform-python) and later. + + bwrap_netlog.py condense --session ID [--dns-forward ADDR] -- CMD... + Read pasta's debug log (and the DNS relay's lines) on standard input + and write one line when a connection opens and one when it closes, + with the DNS name of the destination when the relay saw it, to CMD's + standard input (systemd-cat or logger). Everything else pasta writes + at debug level (timers, ACK and buffer bookkeeping, state changes, + its setup dump) is dropped; its warnings and errors are kept. + + bwrap_netlog.py dns LISTEN UPSTREAM PARENT_PID + A DNS relay for the sandbox's network namespace: listen on LISTEN + (ADDR or ADDR:PORT, UDP and TCP), forward every query to UPSTREAM + (pasta's DNS forward address) and print one line per query on + standard output. It exits when PARENT_PID (the descriptor relay, + which becomes bwrap) does. + +Output lines are logfmt (key=value), so Loki's `| logfmt` can filter them: + + session=ID flow=7 open proto=tcp dir=out src=... dst=140.82.113.22:443 name=github.com t=21.359 + session=ID flow=7 close proto=tcp dir=out dst=140.82.113.22:443 name=github.com t=33.770 secs=12.411 + session=ID flow=9 fail proto=tcp dir=out dst=192.0.2.1:443 src=... t=40.120 + session=ID dns name=github.com type=A rcode=NOERROR answers=140.82.113.22 +""" + +# Kept to what the DNS relay needs: it starts while the sandbox starts, and +# lookups made before it listens skip it (see bwrap_sandbox_lib.sh). +import ctypes +import os +import re +import signal +import socket +import struct +import sys +import threading + +# ── condense ──────────────────────────────────────────────────────────── + +_TS = re.compile(r"^(\d+\.\d+):\s+(.*)$") +_FLOW = re.compile(r"^Flow (\d+) \((.*?)\):\s+(.*)$") +_ENDPOINTS = re.compile( + r"^(\w+) \[([^\]]*)\]:(\d+) -> \[([^\]]*)\]:(\d+) => " + r"(?:(\w+) \[([^\]]*)\]:(\d+) -> \[([^\]]*)\]:(\d+)|\?)$" +) +_HEADER = re.compile(r"^pasta (\S+): (\S+) \((\d+)\)$") +_DIRS = { + ("TAP", "HOST"): "out", + ("HOST", "SPLICE"): "publish", + ("HOST", "TAP"): "publish", + ("SPLICE", "HOST"): "host-port", +} + + +def _hostport(addr, port): + if ":" in addr: + return "[%s]:%s" % (addr, port) + return "%s:%s" % (addr, port) + + +def _value(text): + """A logfmt value: quoted when it contains a space, = or a quote.""" + if text and not re.search(r'[\s="]', text): + return text + return '"%s"' % text.replace("\\", "\\\\").replace('"', '\\"') + + +class Condenser: + """Turn pasta debug lines into open and close lines, one pair per flow.""" + + def __init__(self, emit, dns_forward="169.254.1.1"): + self.emit = emit + self.dns_forward = dns_forward + self.flows = {} + self.names = {} + self.last_t = 0.0 + self.header_seen = False + + def line(self, raw): + raw = raw.rstrip("\n") + if raw.startswith("event="): + if raw.startswith("event=end"): + self.finish() + self.emit(raw, prefix=False) + return + if raw.startswith("bwrap-dns "): + self._dns(raw[len("bwrap-dns ") :]) + return + m = _HEADER.match(raw) + if m and not self.header_seen: + self.header_seen = True + self.emit("pasta version=%s pid=%s" % (m.group(1), m.group(3))) + return + m = _TS.match(raw) + if not m: + return # pasta's stderr copy of its setup, or noise + t = float(m.group(1)) + self.last_t = max(self.last_t, t) + rest = m.group(2) + if rest.startswith(("ERROR:", "WARNING:")): + level, _, msg = rest.partition(":") + self.emit("pasta level=%s msg=%s t=%.3f" % (level.lower(), _value(msg.strip()), t)) + return + m = _FLOW.match(rest) + if m: + self._flow(int(m.group(1)), m.group(2), m.group(3).strip(), t) + + def _dns(self, fields): + kv = dict(re.findall(r'(\w+)=("(?:[^"\\]|\\.)*"|\S*)', fields)) + name = kv.get("name", "").strip('"') + for a in kv.get("answers", "").split(","): + if a and (re.match(r"^\d+\.\d+\.\d+\.\d+$", a) or ":" in a): + self.names[a] = name + self.emit("dns " + fields) + + def _flow(self, idx, kind, rest, t): + f = self.flows.get(idx) + if kind == "NEW": + self.flows[idx] = {"t0": t} + return + if f is None: + return + if kind == "TGT": + m = _ENDPOINTS.match(rest) + if m and m.group(6): + f.update( + ini=m.group(1), + src=(m.group(2), m.group(3)), + dst=(m.group(4), m.group(5)), + tgt=m.group(6), + tdst=(m.group(9), m.group(10)), + ) + return + if kind == "FREE": + if rest.endswith("-> FREE"): + del self.flows[idx] + if f.get("open"): + self._close(idx, f, t) + elif "ini" in f and not self._is_dns_forward(f): + # Never became active: a refused or failed connection. + self.emit(" ".join(self._fields(idx, f, "fail") + ["src=%s" % _hostport(*f["src"]), "t=%.3f" % t])) + return + # A typed flow: "TCP connection", "TCP connection (spliced)", "UDP flow", ... + if rest == "TGT -> TYPED": + f["proto"] = kind.split()[0].lower() + elif rest == "TYPED -> ACTIVE" and "ini" in f: + self._open(idx, f, t) + + def _fields(self, idx, f, event): + direction = _DIRS.get((f["ini"], f["tgt"]), "%s-%s" % (f["ini"].lower(), f["tgt"].lower())) + parts = ["flow=%d" % idx, event, "proto=%s" % f.get("proto", "?"), "dir=%s" % direction] + if event == "open": + parts.append("src=%s" % _hostport(*f["src"])) + parts.append("dst=%s" % _hostport(*f["dst"])) + if f["tdst"][0] != f["dst"][0] and f["tdst"][0] not in ("0.0.0.0", "::"): + parts.append("via=%s" % _hostport(*f["tdst"])) + name = self.names.get(f["dst"][0]) + if name and direction == "out": + parts.append("name=%s" % _value(name)) + return parts + + def _is_dns_forward(self, f): + # pasta's own leg of a lookup the relay already logged as a dns line. + return f.get("proto") == "udp" and f["dst"] == (self.dns_forward, "53") + + def _open(self, idx, f, t): + f["open"] = True + if self._is_dns_forward(f): + return + self.emit(" ".join(self._fields(idx, f, "open") + ["t=%.3f" % t])) + + def _close(self, idx, f, t, reason=None): + if self._is_dns_forward(f): + return + parts = self._fields(idx, f, "close") + ["t=%.3f" % t, "secs=%.3f" % (t - f["t0"])] + if reason: + parts.append("reason=%s" % reason) + self.emit(" ".join(parts)) + + def finish(self): + """Close the flows still open when the session ends.""" + for idx in sorted(self.flows): + f = self.flows[idx] + if f.get("open"): + self._close(idx, f, self.last_t, reason="session-end") + self.flows.clear() + + +def condense(argv): + import subprocess + + session, dns_forward, cmd = "", "169.254.1.1", [] + i = 0 + while i < len(argv): + if argv[i] == "--session": + session = argv[i + 1] + i += 2 + elif argv[i] == "--dns-forward": + dns_forward = argv[i + 1] + i += 2 + elif argv[i] == "--": + cmd = argv[i + 1 :] + break + else: + sys.exit("bwrap_netlog condense: unknown argument %r" % argv[i]) + sink = None + if cmd: + sink = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + out = sink.stdin if sink else sys.stdout.buffer + state = {"dead": False} + + def emit(text, prefix=True): + if state["dead"]: + return + if prefix and session: + text = "session=%s %s" % (session, text) + try: + out.write((text + "\n").encode("utf-8", "replace")) + out.flush() + except (BrokenPipeError, OSError): + # The log tool is gone: keep reading, so pasta never blocks. + state["dead"] = True + + c = Condenser(emit, dns_forward) + for raw in sys.stdin.buffer: + try: + c.line(raw.decode("utf-8", "replace")) + except Exception as e: # never stop reading pasta's log + emit("condense level=error msg=%s" % _value(repr(e))) + c.finish() + if sink: + try: + sink.stdin.close() + except OSError: + pass + sink.wait() + + +# ── dns relay ─────────────────────────────────────────────────────────── + +_QTYPES = {1: "A", 2: "NS", 5: "CNAME", 6: "SOA", 12: "PTR", 15: "MX", 16: "TXT", 28: "AAAA", 33: "SRV", 65: "HTTPS"} +_RCODES = {0: "NOERROR", 1: "FORMERR", 2: "SERVFAIL", 3: "NXDOMAIN", 4: "NOTIMP", 5: "REFUSED"} + + +def _read_name(msg, off): + """Read a (possibly compressed) domain name at OFF; return (name, next offset).""" + labels, jumped, end, hops = [], False, off, 0 + while True: + if off >= len(msg) or hops > 64: + raise ValueError("bad name") + n = msg[off] + if n == 0: + off += 1 + break + if n & 0xC0 == 0xC0: + if off + 1 >= len(msg): + raise ValueError("bad pointer") + if not jumped: + end = off + 2 + off = ((n & 0x3F) << 8) | msg[off + 1] + jumped, hops = True, hops + 1 + continue + labels.append(msg[off + 1 : off + 1 + n].decode("ascii", "replace")) + off += 1 + n + return ".".join(labels) or ".", (end if jumped else off) + + +def parse_query(msg): + """Return (name, type) of the first question of a DNS message.""" + if len(msg) < 12 or struct.unpack("!H", msg[4:6])[0] < 1: + raise ValueError("no question") + name, off = _read_name(msg, 12) + qtype = struct.unpack("!H", msg[off : off + 2])[0] + return name.lower(), _QTYPES.get(qtype, str(qtype)) + + +def parse_answers(msg): + """Return (rcode, [answers]) of a DNS response: A, AAAA and CNAME data.""" + if len(msg) < 12: + raise ValueError("short response") + flags, qd, an = struct.unpack("!HHH", msg[2:8]) + off = 12 + for _ in range(qd): + _, off = _read_name(msg, off) + off += 4 + answers = [] + for _ in range(an): + _, off = _read_name(msg, off) + rtype, _, _, rdlen = struct.unpack("!HHIH", msg[off : off + 10]) + off += 10 + rdata = msg[off : off + rdlen] + if rtype == 1 and rdlen == 4: + answers.append(socket.inet_ntop(socket.AF_INET, rdata)) + elif rtype == 28 and rdlen == 16: + answers.append(socket.inet_ntop(socket.AF_INET6, rdata)) + elif rtype == 5: + answers.append(_read_name(msg, off)[0].lower()) + off += rdlen + return _RCODES.get(flags & 0xF, str(flags & 0xF)), answers + + +def _addr(spec, port=53): + host, sep, p = spec.rpartition(":") + if sep and p.isdigit() and host.count(":") == 0: + return host, int(p) + return spec, port + + +class DnsRelay: + def __init__(self, listen, upstream, out=None): + self.listen = _addr(listen) + self.upstream = _addr(upstream) + self.out = out or sys.stdout + self.lock = threading.Lock() + + def log(self, query, response, proto): + try: + name, qtype = parse_query(query) + except (ValueError, struct.error, IndexError): + name, qtype = "?", "?" + if response is None: + rcode, answers = "TIMEOUT", [] + else: + try: + rcode, answers = parse_answers(response) + except (ValueError, struct.error, IndexError): + rcode, answers = "UNPARSED", [] + line = "bwrap-dns name=%s type=%s proto=%s rcode=%s answers=%s\n" % ( + _value(name), + qtype, + proto, + rcode, + ",".join(answers) or "-", + ) + with self.lock: + try: + self.out.write(line) + self.out.flush() + except OSError: + pass + + def _udp_one(self, srv, query, client): + response = None + try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.settimeout(5) + s.connect(self.upstream) + s.send(query) + response = s.recv(65535) + s.close() + srv.sendto(response, client) + except OSError: + pass + self.log(query, response, "udp") + + def _recv_exact(self, s, n): + data = b"" + while len(data) < n: + chunk = s.recv(n - len(data)) + if not chunk: + raise OSError("closed") + data += chunk + return data + + def _tcp_one(self, conn): + try: + conn.settimeout(10) + up = socket.create_connection(self.upstream, timeout=5) + while True: + try: + hdr = self._recv_exact(conn, 2) + except OSError: + break + query = self._recv_exact(conn, struct.unpack("!H", hdr)[0]) + up.sendall(hdr + query) + rhdr = self._recv_exact(up, 2) + response = self._recv_exact(up, struct.unpack("!H", rhdr)[0]) + conn.sendall(rhdr + response) + self.log(query, response, "tcp") + up.close() + except OSError: + pass + finally: + conn.close() + + def bind(self): + self.udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.udp.bind(self.listen) + self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + self.tcp.bind(self.listen) + self.tcp.listen(16) + + def serve(self): + def tcp_loop(): + while True: + conn, _ = self.tcp.accept() + threading.Thread(target=self._tcp_one, args=(conn,), daemon=True).start() + + threading.Thread(target=tcp_loop, daemon=True).start() + while True: + query, client = self.udp.recvfrom(65535) + threading.Thread(target=self._udp_one, args=(self.udp, query, client), daemon=True).start() + + +def _drop_capabilities(): + """Drop every capability (the relay runs as root of pasta's user namespace; + it needs CAP_NET_BIND_SERVICE only to bind port 53, which is done).""" + try: + libc = ctypes.CDLL(None, use_errno=True) + header = struct.pack("II", 0x20080522, 0) # _LINUX_CAPABILITY_VERSION_3, this thread + data = ctypes.create_string_buffer(24) # 2 x {effective, permitted, inheritable} + libc.capset(ctypes.create_string_buffer(header), data) + libc.prctl(38, 1, 0, 0, 0) # PR_SET_NO_NEW_PRIVS + except (OSError, AttributeError): + pass + + +def dns(argv): + listen, upstream, parent = argv[0], argv[1], int(argv[2]) + try: + libc = ctypes.CDLL(None, use_errno=True) + libc.prctl(1, signal.SIGTERM, 0, 0, 0) # PR_SET_PDEATHSIG + except (OSError, AttributeError): + pass + if os.getppid() != parent: + return + relay = DnsRelay(listen, upstream) + try: + relay.bind() + except OSError as e: + sys.stdout.write("bwrap-dns name=- type=- proto=- rcode=RELAY-FAILED answers=%s\n" % _value(str(e))) + sys.stdout.flush() + return + _drop_capabilities() + try: # tell the descriptor relay, which waits for this, that it listens + os.write(3, b"ready\n") + os.close(3) + except OSError: + pass + relay.serve() + + +def main(): + if len(sys.argv) >= 2 and sys.argv[1] == "condense": + condense(sys.argv[2:]) + elif len(sys.argv) == 5 and sys.argv[1] == "dns": + try: + dns(sys.argv[2:]) + except KeyboardInterrupt: + pass + else: + sys.exit(__doc__) + + +if __name__ == "__main__": + main() diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index 8374f1d..ae2695d 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -2618,6 +2618,12 @@ _net_session_dir() { # (slirp4netns has no flow log); the supervisor just waits for the end. _NET_LOG_TAG="bwrap-net" +# Condenser and DNS relay (lib/bwrap_netlog.py), and the relay's address in +# the sandbox's network namespace. +_NET_LOG_HELPER="${BASH_SOURCE[0]%/*}/bwrap_netlog.py" +_NET_LOG_PY="" +_NET_DNS_RELAY=0 +_NET_DNS_RELAY_ADDR="127.53.0.1" _NET_LOG_TOOL="" _NET_LOG_TOOLS=() _NET_LOG_SESSION="" @@ -2650,6 +2656,15 @@ _net_log_detect() { _NET_LOG_TOOL="${_NET_LOG_TOOLS[0]:-}" _NET_LOG_FLOWS=0 [[ -z "${_NET_LOG_TOOL}" || "${_NET_BACKEND}" != pasta ]] || _NET_LOG_FLOWS=1 + # With Python the flow log is condensed (one line per open and close) + # and the DNS relay names the destinations; without it pasta's lines go + # to the log as they are. + _NET_LOG_PY="" + _NET_DNS_RELAY=0 + if [[ "${_NET_LOG_FLOWS}" -eq 1 && -r "${_NET_LOG_HELPER}" ]]; then + _NET_LOG_PY="$(_net_python)" + [[ -z "${_NET_LOG_PY}" ]] || _NET_DNS_RELAY=1 + fi SANDBOX_ENV+=("BWRAP_SESSION=${_NET_LOG_SESSION}") } @@ -2702,10 +2717,12 @@ _net_log_start() { _NET_LOG_TOOL="" done if [[ -z "${_NET_LOG_TOOL}" ]]; then + _NET_DNS_RELAY=0 printf 'Warning: %s: neither systemd-cat (journal) nor logger (/dev/log) can write to the system log;\n' "${_launcher}" >&2 printf ' this network session is not logged.\n' >&2 _NET_LOG_TOOL="" _NET_LOG_FLOWS=0 + _NET_LOG_PY="" return 0 fi if [[ -t 2 ]]; then @@ -2733,7 +2750,13 @@ _net_log_start() { mkfifo -m 0600 "${_NET_LOG_ERR_FIFO}" _err="${_NET_LOG_ERR_FIFO}" fi - _BWRAP_FD_RELAY=(2 note "${_NET_LOG_MARKER}" 2 append "${_err}" ${_BWRAP_FD_RELAY[@]+"${_BWRAP_FD_RELAY[@]}"}) + local -a _pre=(2 note "${_NET_LOG_MARKER}") + if [[ "${_NET_DNS_RELAY}" -eq 1 ]]; then + # The DNS relay, started by the descriptor relay in the sandbox's + # network namespace (outside bwrap), logs on the same FIFO. + _pre+=(2 spawn "${_NET_LOG_PY}" "${_NET_LOG_HELPER}" "${_NET_DNS_RELAY_ADDR}" "${_NET_DNS_FORWARD_ADDR}") + fi + _BWRAP_FD_RELAY=("${_pre[@]}" 2 append "${_err}" ${_BWRAP_FD_RELAY[@]+"${_BWRAP_FD_RELAY[@]}"}) fi _net_log_supervisor & } @@ -2742,7 +2765,7 @@ _net_log_start() { # The background half of the network log (see "Network log"). Runs in a # subshell; $$ is still the launcher, which is pasta (or bwrap) by then. _net_log_supervisor() { - local _sink _fifo _line="" _part="" _rc _started=0 _dead=0 _bridge="" _i + local _sink _prefix _fifo _line="" _part="" _rc _started=0 _dead=0 _bridge="" _i local -a _early=() set +e # Signals meant for the session (Ctrl-C, a hangup, `kill` of its process @@ -2750,7 +2773,16 @@ _net_log_supervisor() { # the launcher has gone. trap '' INT QUIT TSTP HUP TERM PIPE exec < /dev/null > /dev/null - exec {_sink}> >(exec setsid "${_NET_LOG_CMD[@]}" > /dev/null 2>&1) + if [[ -n "${_NET_LOG_PY}" ]]; then + # The condenser writes session=ID itself. + exec {_sink}> >(exec setsid "${_NET_LOG_PY}" -I -S "${_NET_LOG_HELPER}" condense \ + --session "${_NET_LOG_SESSION}" --dns-forward "${_NET_DNS_FORWARD_ADDR}" \ + -- "${_NET_LOG_CMD[@]}" > /dev/null 2>&1) + _prefix="" + else + exec {_sink}> >(exec setsid "${_NET_LOG_CMD[@]}" > /dev/null 2>&1) + _prefix="session=${_NET_LOG_SESSION} " + fi if [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then exec {_fifo}<> "${_NET_LOG_FIFO}" @@ -2780,14 +2812,14 @@ _net_log_supervisor() { [[ ${#_early[@]} -ge 200 ]] || _early+=("${_line}") fi if [[ "${_dead}" -eq 0 ]] && - ! printf 'session=%s %s\n' "${_NET_LOG_SESSION}" "${_line}" 1>&"${_sink}" 2> /dev/null; then + ! printf '%s%s\n' "${_prefix}" "${_line}" 1>&"${_sink}" 2> /dev/null; then _dead=1 fi done # pasta has exited: drain what it left in the pipe. while IFS= read -r -t 0.2 _line <&"${_fifo}"; do [[ "${_dead}" -eq 1 ]] || - printf 'session=%s %s\n' "${_NET_LOG_SESSION}" "${_part}${_line}" 1>&"${_sink}" 2> /dev/null || _dead=1 + printf '%s%s\n' "${_prefix}" "${_part}${_line}" 1>&"${_sink}" 2> /dev/null || _dead=1 _part="" done exec {_fifo}<&- @@ -2847,6 +2879,17 @@ _net_log_print_dry_run() { printf '# and pasta gets --debug --log-file /netlog --log-size %s (not --quiet)\n' "${_NET_LOG_PASTA_SIZE}" printf '# with its stderr on that FIFO; a supervisor copies each line to the log and the\n' printf '# relay first reopens your stderr. The end record is written when pasta exits.\n' + if [[ -n "${_NET_LOG_PY}" ]]; then + printf '# The lines are condensed (one per connection opened and closed) by +' + printf '# %s -I %s condense +' "${_NET_LOG_PY}" "${_NET_LOG_HELPER}" + printf '# and the relay first starts the DNS relay on %s:53 in the namespace: +' "${_NET_DNS_RELAY_ADDR}" + printf '# %s -I %s dns %s %s +' "${_NET_LOG_PY}" "${_NET_LOG_HELPER}" \ + "${_NET_DNS_RELAY_ADDR}" "${_NET_DNS_FORWARD_ADDR}" + fi else printf '# and a supervisor writes the end record when the sandbox exits (no flow log\n' printf '# with %s).\n' "${_NET_BACKEND}" @@ -2951,6 +2994,20 @@ for spec in sys.argv[2:]: sys.exit("%s:%s -> sandbox port %s: %s" % (addr, hport, gport, reply.get("error", reply))) ' +# _net_python +# Print the Python used by the network helpers (RHEL 8's platform-python, +# else python3), nothing when there is none. They need the standard +# library of Python 3.6 or later only. +_net_python() { + local _p + for _p in /usr/libexec/platform-python "$(command -v python3 2> /dev/null || true)"; do + if [[ -n "${_p}" && -x "${_p}" ]]; then + printf '%s' "${_p}" + return 0 + fi + done +} + # _net_try_slirp # Return 0 when slirp4netns can be used (set _SLIRP_BIN, _SLIRP_ARGS and, # for --publish, _NET_PY_BIN), else 1 with the reason in _NET_TRY_REASON. @@ -2999,12 +3056,7 @@ _net_try_slirp() { fi _NET_PY_BIN="" if [[ ${#NET_PUBLISH[@]} -gt 0 ]]; then - for _n in /usr/libexec/platform-python "$(command -v python3 2> /dev/null || true)"; do - if [[ -n "${_n}" && -x "${_n}" ]]; then - _NET_PY_BIN="${_n}" - break - fi - done + _NET_PY_BIN="$(_net_python)" if [[ -z "${_NET_PY_BIN}" ]]; then _NET_TRY_REASON="--publish needs python3 (or /usr/libexec/platform-python) to configure it" return 1 @@ -3766,6 +3818,11 @@ build_net_isolation() { fi _NET_DNS_EFFECTIVE="${_dns}" _resolv="nameserver ${_dns}"$'\n'"options edns0" + if [[ "${_NET_DNS_RELAY}" -eq 1 ]]; then + # The DNS relay first (logged); the backend's address second, which a + # resolver uses at once when the relay is not up (port closed). + _resolv="nameserver ${_NET_DNS_RELAY_ADDR}"$'\n'"${_resolv}" + fi bwrap_fd_from_data "generated /etc/resolv.conf" "${_resolv}" BWRAP_ARGS+=(--ro-bind-data "${_BWRAP_FD}" /etc/resolv.conf) @@ -4815,7 +4872,11 @@ _bwrap_context_network() { fi _t+="This sandbox has its own network namespace (${_NET_BACKEND})." _t+=" Outbound connections work and leave from the host's address;" - _t+=" DNS goes to \`${_NET_DNS_EFFECTIVE}\`, which is forwarded to the host's resolver." + if [[ "${_NET_DNS_RELAY}" -eq 1 ]]; then + _t+=" DNS goes to \`${_NET_DNS_RELAY_ADDR}\`, a relay that logs each lookup, and on to \`${_NET_DNS_EFFECTIVE}\`, which is forwarded to the host's resolver." + else + _t+=" DNS goes to \`${_NET_DNS_EFFECTIVE}\`, which is forwarded to the host's resolver." + fi _t+=" Services listening on the host's loopback (\`127.0.0.1\`, \`localhost\`) are not reachable from here," _t+=" and nothing you listen on here is reachable from the host, except as listed below." _t+=" \`127.0.0.1\` here is the sandbox's own loopback." @@ -5144,6 +5205,12 @@ _bwrap_toml_string() { # note TEXT the relay prints TEXT on the descriptor (a marker on stderr). # append PATH the relay opens PATH for appending on the descriptor (the # user's stderr again). +# spawn PY HELPER LISTEN UPSTREAM the relay starts the DNS relay +# (`PY -I -S HELPER dns LISTEN UPSTREAM `) in the +# background, its output on the descriptor, and waits until it +# listens (it says so on descriptor 3; a Python start, some tens +# of milliseconds), at most 0.25 s, so the first lookups are +# logged too. # _bwrap_fd_check_room # Exit 1 when the reserved numbers would reach 90, where the slirp4netns @@ -5221,6 +5288,12 @@ while [[ $# -gt 0 && "$1" != -- ]]; do eval "exec $1>>\"\$3\"" || { echo "$0: cannot open $3" >&2; exit 126; } shift 3 ;; + spawn) + coproc _bwrap_dns { exec "$3" -I -S "$4" dns "$5" "$6" "$$" 3>&1 1>&"$1" 2>&"$1" < /dev/null; } + read -r -t 0.25 _v <&"${_bwrap_dns[0]}" + eval "exec ${_bwrap_dns[0]}<&- ${_bwrap_dns[1]}>&-" + shift 6 + ;; *) echo "$0: bad relay entry: $2" >&2 exit 126 @@ -5256,12 +5329,14 @@ _bwrap_fd_data_numbers() { local _r=0 _BWRAP_FD_DATA_NUMS=() while [[ ${_r} -lt ${#_BWRAP_FD_RELAY[@]} ]]; do - if [[ "${_BWRAP_FD_RELAY[$((_r + 1))]}" != data ]]; then - _r=$((_r + 3)) - else - _BWRAP_FD_DATA_NUMS+=("${_BWRAP_FD_RELAY[${_r}]}") - _r=$((_r + 2)) - fi + case "${_BWRAP_FD_RELAY[$((_r + 1))]}" in + data) + _BWRAP_FD_DATA_NUMS+=("${_BWRAP_FD_RELAY[${_r}]}") + _r=$((_r + 2)) + ;; + spawn) _r=$((_r + 6)) ;; + *) _r=$((_r + 3)) ;; + esac done } From 67717c17a9bcd571f33bb15f6438af0456acc03d Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 10 Nov 2026 10:34:22 -0500 Subject: [PATCH 22/37] test(bwrap): test the network log condenser and DNS relay Unit tests (pixi run -e dev test) against part of a real pasta debug log with documentation addresses, and a DNS relay against a local fake upstream. The fake pasta now writes pasta's debug format for its forwards and answers DNS on --dns-forward, so the launchers' log can be checked end to end with tests/fake-net-run. Assisted-by: opencode:claude-opus-5-5 --- pixi.toml | 1 + tests/data/pasta-debug.log | 900 ++++++++++++++++++++++++++++++++++++ tests/fakes/_nsproxy.py | 32 +- tests/fakes/pasta-bin/pasta | 108 ++++- tests/test_netlog.py | 231 +++++++++ 5 files changed, 1256 insertions(+), 16 deletions(-) create mode 100644 tests/data/pasta-debug.log create mode 100644 tests/test_netlog.py diff --git a/pixi.toml b/pixi.toml index 70b8faf..28721c0 100644 --- a/pixi.toml +++ b/pixi.toml @@ -31,3 +31,4 @@ mypy = "*" [feature.dev.tasks] lint = "prek run --all-files" lint-files = "prek run --files" +test = "python -m unittest discover -s tests -p 'test_*.py'" diff --git a/tests/data/pasta-debug.log b/tests/data/pasta-debug.log new file mode 100644 index 0000000..825138d --- /dev/null +++ b/tests/data/pasta-debug.log @@ -0,0 +1,900 @@ +pasta 2026_10_02.cba3570: /usr/bin/passt (3962723) +0.0071: info: Template interface: enp7s0 (IPv4), enp7s0 (IPv6) +Template interface: enp7s0 (IPv4), enp7s0 (IPv6) +0.0071: info: Namespace interface: enp7s0 +Namespace interface: enp7s0 +0.0071: info: MAC: +MAC: +0.0071: info: host: 02:00:00:00:00:01 + host: 02:00:00:00:00:01 +0.0071: info: DHCP: +DHCP: +0.0071: info: assign: 198.51.100.7 + assign: 198.51.100.7 +0.0071: info: mask: 255.255.255.0 + mask: 255.255.255.0 +0.0071: info: router: 198.51.100.1 + router: 198.51.100.1 +0.0071: info: DNS: +DNS: +0.0072: info: 169.254.1.1 + 169.254.1.1 +0.0072: info: NDP/DHCPv6: +NDP/DHCPv6: +0.0072: info: assign: 2001:db8::7 + assign: 2001:db8::7 +0.0072: info: router: fe80::1 + router: fe80::1 +0.0072: info: our link-local: fe80::1 + our link-local: fe80::1 +0.0072: info: Inbound forwarding rules (HOST): +Inbound forwarding rules (HOST): +0.0072: info: Outbound forwarding rules (SPLICE): +Outbound forwarding rules (SPLICE): +0.0161: Sending initial ARP request for guest MAC address +Sending initial ARP request for guest MAC address +0.0162: Sending initial NDP NS request for guest MAC address +Sending initial NDP NS request for guest MAC address +0.0188: Using UDP timeout parameters, timeout: 30, stream_timeout: 120 +Using UDP timeout parameters, timeout: 30, stream_timeout: 120 +0.0188: Using TCP RTO parameters, syn_retries: 6, syn_linear_timeouts: 4, rto_max: 120 +Using TCP RTO parameters, syn_retries: 6, syn_linear_timeouts: 4, rto_max: 120 +0.0203: SO_PEEK_OFF supported +SO_PEEK_OFF supported +0.0203: TCP_INFO tcpi_snd_wnd field supported +TCP_INFO tcpi_snd_wnd field supported +0.0203: TCP_INFO tcpi_bytes_acked field supported +TCP_INFO tcpi_bytes_acked field supported +0.0203: TCP_INFO tcpi_min_rtt field supported +TCP_INFO tcpi_min_rtt field supported +0.0206: ARP announcement for 198.51.100.1 / 02:00:00:00:00:02 +ARP announcement for 198.51.100.1 / 02:00:00:00:00:02 +0.0227: info: NDP: received NS, sending NA +0.6782: Flow 0 (NEW): FREE -> NEW +0.6782: Flow 0 (INI): NEW -> INI +0.6782: Flow 0 (INI): TAP [198.51.100.7]:35358 -> [169.254.1.1]:53 => ? +0.6782: Flow 0 (TGT): INI -> TGT +0.6782: Flow 0 (TGT): TAP [198.51.100.7]:35358 -> [169.254.1.1]:53 => HOST [0.0.0.0]:35358 -> [127.0.0.53]:53 +0.6782: Flow 0 (UDP flow): TGT -> TYPED +0.6782: Flow 0 (UDP flow): TAP [198.51.100.7]:35358 -> [169.254.1.1]:53 => HOST [0.0.0.0]:35358 -> [127.0.0.53]:53 +0.6783: Flow 0 (UDP flow): Side 0 hash table insert: bucket: 260701 +0.6783: Flow 0 (UDP flow): Side 1 hash table insert: bucket: 346642 +0.6783: Flow 0 (UDP flow): TYPED -> ACTIVE +0.6783: Flow 0 (UDP flow): TAP [198.51.100.7]:35358 -> [169.254.1.1]:53 => HOST [127.0.0.1]:35358 -> [127.0.0.53]:53 +0.6842: Flow 1 (NEW): FREE -> NEW +0.6842: Flow 1 (INI): NEW -> INI +0.6842: Flow 1 (INI): TAP [2001:db8::7]:49344 -> [2606:4700:10::ac42:ad95]:443 => ? +0.6842: Flow 1 (TGT): INI -> TGT +0.6842: Flow 1 (TGT): TAP [2001:db8::7]:49344 -> [2606:4700:10::ac42:ad95]:443 => HOST [::]:0 -> [2606:4700:10::ac42:ad95]:443 +0.6842: Flow 1 (TCP connection): TGT -> TYPED +0.6842: Flow 1 (TCP connection): TAP [2001:db8::7]:49344 -> [2606:4700:10::ac42:ad95]:443 => HOST [::]:0 -> [2606:4700:10::ac42:ad95]:443 +0.6842: Flow 1 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +0.6842: Flow 1 (TCP connection): Side 0 hash table insert: bucket: 277245 +0.6842: Flow 1 (TCP connection): TYPED -> ACTIVE +0.6842: Flow 1 (TCP connection): TAP [2001:db8::7]:49344 -> [2606:4700:10::ac42:ad95]:443 => HOST [::]:0 -> [2606:4700:10::ac42:ad95]:443 +0.6842: Flow 2 (NEW): FREE -> NEW +0.6842: Flow 2 (INI): NEW -> INI +0.6842: Flow 2 (INI): TAP [198.51.100.7]:40178 -> [172.66.173.149]:443 => ? +0.6842: Flow 2 (TGT): INI -> TGT +0.6842: Flow 2 (TGT): TAP [198.51.100.7]:40178 -> [172.66.173.149]:443 => HOST [0.0.0.0]:0 -> [172.66.173.149]:443 +0.6842: Flow 2 (TCP connection): TGT -> TYPED +0.6842: Flow 2 (TCP connection): TAP [198.51.100.7]:40178 -> [172.66.173.149]:443 => HOST [0.0.0.0]:0 -> [172.66.173.149]:443 +0.6842: Flow 2 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +0.6842: Flow 2 (TCP connection): Side 0 hash table insert: bucket: 124438 +0.6843: Flow 2 (TCP connection): TYPED -> ACTIVE +0.6843: Flow 2 (TCP connection): TAP [198.51.100.7]:40178 -> [172.66.173.149]:443 => HOST [0.0.0.0]:0 -> [172.66.173.149]:443 +0.6843: Flow 3 (NEW): FREE -> NEW +0.6843: Flow 3 (INI): NEW -> INI +0.6843: Flow 3 (INI): TAP [198.51.100.7]:33998 -> [104.20.32.17]:443 => ? +0.6843: Flow 3 (TGT): INI -> TGT +0.6843: Flow 3 (TGT): TAP [198.51.100.7]:33998 -> [104.20.32.17]:443 => HOST [0.0.0.0]:0 -> [104.20.32.17]:443 +0.6843: Flow 3 (TCP connection): TGT -> TYPED +0.6843: Flow 3 (TCP connection): TAP [198.51.100.7]:33998 -> [104.20.32.17]:443 => HOST [0.0.0.0]:0 -> [104.20.32.17]:443 +0.6843: Flow 3 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +0.6843: Flow 3 (TCP connection): Side 0 hash table insert: bucket: 123425 +0.6843: Flow 3 (TCP connection): TYPED -> ACTIVE +0.6843: Flow 3 (TCP connection): TAP [198.51.100.7]:33998 -> [104.20.32.17]:443 => HOST [0.0.0.0]:0 -> [104.20.32.17]:443 +0.6843: Flow 4 (NEW): FREE -> NEW +0.6843: Flow 4 (INI): NEW -> INI +0.6843: Flow 4 (INI): TAP [2001:db8::7]:45592 -> [2606:4700:10::6814:2011]:443 => ? +0.6843: Flow 4 (TGT): INI -> TGT +0.6843: Flow 4 (TGT): TAP [2001:db8::7]:45592 -> [2606:4700:10::6814:2011]:443 => HOST [::]:0 -> [2606:4700:10::6814:2011]:443 +0.6843: Flow 4 (TCP connection): TGT -> TYPED +0.6843: Flow 4 (TCP connection): TAP [2001:db8::7]:45592 -> [2606:4700:10::6814:2011]:443 => HOST [::]:0 -> [2606:4700:10::6814:2011]:443 +0.6843: Flow 4 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +0.6843: Flow 4 (TCP connection): Side 0 hash table insert: bucket: 256789 +0.6843: Flow 4 (TCP connection): TYPED -> ACTIVE +0.6843: Flow 4 (TCP connection): TAP [2001:db8::7]:45592 -> [2606:4700:10::6814:2011]:443 => HOST [::]:0 -> [2606:4700:10::6814:2011]:443 +0.6885: Flow 3 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +0.6885: Flow 3 (TCP connection): ACK_FROM_TAP_DUE +0.6885: Flow 3 (TCP connection): timer expires in 1.000s +0.6886: Flow 3 (TCP connection): ACK_FROM_TAP_DUE dropped +0.6886: Flow 3 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED +0.6886: Flow 1 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +0.6886: Flow 1 (TCP connection): ACK_FROM_TAP_DUE +0.6886: Flow 1 (TCP connection): timer expires in 1.000s +0.6886: Flow 1 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +0.6886: Flow 1 (TCP connection): Side 0 hash table remove: bucket: 277245 +0.6887: Flow 1 (FREE): ACTIVE -> FREE +0.6887: Flow 1 (FREE): TAP [2001:db8::7]:49344 -> [2606:4700:10::ac42:ad95]:443 => HOST [::]:0 -> [2606:4700:10::ac42:ad95]:443 +0.6887: Flow 3 (TCP connection): ACK_TO_TAP_DUE +0.6887: Flow 2 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +0.6887: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +0.6887: Flow 2 (TCP connection): timer expires in 1.000s +0.6887: Flow 2 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +0.6887: Flow 2 (TCP connection): Side 0 hash table remove: bucket: 124438 +0.6887: Flow 2 (FREE): ACTIVE -> FREE +0.6887: Flow 2 (FREE): TAP [198.51.100.7]:40178 -> [172.66.173.149]:443 => HOST [0.0.0.0]:0 -> [172.66.173.149]:443 +0.6895: Flow 4 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +0.6895: Flow 4 (TCP connection): ACK_FROM_TAP_DUE +0.6895: Flow 4 (TCP connection): timer expires in 1.000s +0.6895: Flow 4 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +0.6895: Flow 4 (TCP connection): Side 0 hash table remove: bucket: 256789 +0.6895: Flow 4 (FREE): ACTIVE -> FREE +0.6895: Flow 4 (FREE): TAP [2001:db8::7]:45592 -> [2606:4700:10::6814:2011]:443 => HOST [::]:0 -> [2606:4700:10::6814:2011]:443 +0.6951: Flow 3 (TCP connection): ACK_TO_TAP_DUE dropped +0.6951: Flow 3 (TCP connection): timer expires in 0.000s +0.7048: Flow 3 (TCP connection): ACK_FROM_TAP_DUE +0.7048: Flow 3 (TCP connection): timer expires in 1.000s +0.7048: Flow 3 (TCP connection): STALLED +0.7048: Flow 3 (TCP connection): ACK_FROM_TAP_DUE dropped +0.7048: Flow 3 (TCP connection): STALLED dropped +0.7054: Flow 3 (TCP connection): ACK_TO_TAP_DUE +0.7504: Flow 3 (TCP connection): ACK_TO_TAP_DUE dropped +0.7504: Flow 3 (TCP connection): timer expires in 0.000s +0.7625: Flow 3 (TCP connection): ACK_FROM_TAP_DUE +0.7625: Flow 3 (TCP connection): timer expires in 1.000s +0.7625: Flow 3 (TCP connection): STALLED +0.8030: Flow 3 (TCP connection): ACK_FROM_TAP_DUE dropped +0.8030: Flow 3 (TCP connection): STALLED dropped +0.9127: Flow 1 (NEW): FREE -> NEW +0.9127: Flow 1 (INI): NEW -> INI +0.9127: Flow 1 (INI): TAP [198.51.100.7]:59425 -> [169.254.1.1]:53 => ? +0.9127: Flow 1 (TGT): INI -> TGT +0.9127: Flow 1 (TGT): TAP [198.51.100.7]:59425 -> [169.254.1.1]:53 => HOST [0.0.0.0]:59425 -> [127.0.0.53]:53 +0.9127: Flow 1 (UDP flow): TGT -> TYPED +0.9127: Flow 1 (UDP flow): TAP [198.51.100.7]:59425 -> [169.254.1.1]:53 => HOST [0.0.0.0]:59425 -> [127.0.0.53]:53 +0.9128: Flow 1 (UDP flow): Side 0 hash table insert: bucket: 263925 +0.9128: Flow 1 (UDP flow): Side 1 hash table insert: bucket: 136662 +0.9128: Flow 1 (UDP flow): TYPED -> ACTIVE +0.9128: Flow 1 (UDP flow): TAP [198.51.100.7]:59425 -> [169.254.1.1]:53 => HOST [127.0.0.1]:59425 -> [127.0.0.53]:53 +0.9170: Flow 2 (NEW): FREE -> NEW +0.9170: Flow 2 (INI): NEW -> INI +0.9170: Flow 2 (INI): TAP [198.51.100.7]:59092 -> [140.82.112.21]:443 => ? +0.9170: Flow 2 (TGT): INI -> TGT +0.9171: Flow 2 (TGT): TAP [198.51.100.7]:59092 -> [140.82.112.21]:443 => HOST [0.0.0.0]:0 -> [140.82.112.21]:443 +0.9171: Flow 2 (TCP connection): TGT -> TYPED +0.9171: Flow 2 (TCP connection): TAP [198.51.100.7]:59092 -> [140.82.112.21]:443 => HOST [0.0.0.0]:0 -> [140.82.112.21]:443 +0.9171: Flow 2 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +0.9171: Flow 2 (TCP connection): Side 0 hash table insert: bucket: 234639 +0.9171: Flow 2 (TCP connection): TYPED -> ACTIVE +0.9171: Flow 2 (TCP connection): TAP [198.51.100.7]:59092 -> [140.82.112.21]:443 => HOST [0.0.0.0]:0 -> [140.82.112.21]:443 +0.9270: Flow 2 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +0.9270: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +0.9270: Flow 2 (TCP connection): timer expires in 1.000s +0.9270: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped +0.9270: Flow 2 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED +0.9272: Flow 2 (TCP connection): ACK_TO_TAP_DUE +0.9401: Flow 2 (TCP connection): ACK_TO_TAP_DUE dropped +0.9401: Flow 2 (TCP connection): timer expires in 0.000s +0.9404: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +0.9404: Flow 2 (TCP connection): timer expires in 1.000s +0.9404: Flow 2 (TCP connection): timer expires in 1.000s +0.9404: Flow 2 (TCP connection): timer expires in 1.000s +0.9404: Flow 2 (TCP connection): STALLED +0.9404: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped +0.9404: Flow 2 (TCP connection): STALLED dropped +0.9480: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +0.9480: Flow 2 (TCP connection): timer expires in 1.000s +0.9480: Flow 2 (TCP connection): STALLED +0.9480: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped +0.9480: Flow 2 (TCP connection): STALLED dropped +0.9487: Flow 2 (TCP connection): ACK_TO_TAP_DUE +0.9584: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +0.9585: Flow 2 (TCP connection): STALLED +0.9617: Flow 2 (TCP connection): ACK_TO_TAP_DUE dropped +0.9617: Flow 2 (TCP connection): timer expires in 1.000s +0.9617: Flow 2 (TCP connection): timer expires in 1.000s +0.9989: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped +0.9990: Flow 2 (TCP connection): STALLED dropped +1.0251: TCP inactivity scan +1.1039: Flow 4 (NEW): FREE -> NEW +1.1039: Flow 4 (INI): NEW -> INI +1.1039: Flow 4 (INI): TAP [198.51.100.7]:42639 -> [169.254.1.1]:53 => ? +1.1039: Flow 4 (TGT): INI -> TGT +1.1039: Flow 4 (TGT): TAP [198.51.100.7]:42639 -> [169.254.1.1]:53 => HOST [0.0.0.0]:42639 -> [127.0.0.53]:53 +1.1039: Flow 4 (UDP flow): TGT -> TYPED +1.1039: Flow 4 (UDP flow): TAP [198.51.100.7]:42639 -> [169.254.1.1]:53 => HOST [0.0.0.0]:42639 -> [127.0.0.53]:53 +1.1039: Flow 4 (UDP flow): Side 0 hash table insert: bucket: 104330 +1.1039: Flow 4 (UDP flow): Side 1 hash table insert: bucket: 4393 +1.1039: Flow 4 (UDP flow): TYPED -> ACTIVE +1.1039: Flow 4 (UDP flow): TAP [198.51.100.7]:42639 -> [169.254.1.1]:53 => HOST [127.0.0.1]:42639 -> [127.0.0.53]:53 +1.1042: Flow 5 (NEW): FREE -> NEW +1.1042: Flow 5 (INI): NEW -> INI +1.1042: Flow 5 (INI): TAP [198.51.100.7]:49988 -> [140.82.112.3]:443 => ? +1.1042: Flow 5 (TGT): INI -> TGT +1.1042: Flow 5 (TGT): TAP [198.51.100.7]:49988 -> [140.82.112.3]:443 => HOST [0.0.0.0]:0 -> [140.82.112.3]:443 +1.1042: Flow 5 (TCP connection): TGT -> TYPED +1.1042: Flow 5 (TCP connection): TAP [198.51.100.7]:49988 -> [140.82.112.3]:443 => HOST [0.0.0.0]:0 -> [140.82.112.3]:443 +1.1042: Flow 5 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.1042: Flow 5 (TCP connection): Side 0 hash table insert: bucket: 66400 +1.1043: Flow 5 (TCP connection): TYPED -> ACTIVE +1.1043: Flow 5 (TCP connection): TAP [198.51.100.7]:49988 -> [140.82.112.3]:443 => HOST [0.0.0.0]:0 -> [140.82.112.3]:443 +1.1152: Flow 5 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.1152: Flow 5 (TCP connection): ACK_FROM_TAP_DUE +1.1152: Flow 5 (TCP connection): timer expires in 1.000s +1.1152: Flow 5 (TCP connection): ACK_FROM_TAP_DUE dropped +1.1152: Flow 5 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED +1.1160: Flow 5 (TCP connection): ACK_TO_TAP_DUE +1.1261: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +1.1261: Flow 2 (TCP connection): timer expires in 1.000s +1.1261: Flow 2 (TCP connection): STALLED +1.1262: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped +1.1262: Flow 2 (TCP connection): STALLED dropped +1.1288: Flow 5 (TCP connection): ACK_FROM_TAP_DUE +1.1288: Flow 5 (TCP connection): STALLED +1.1288: Flow 5 (TCP connection): ACK_FROM_TAP_DUE dropped +1.1288: Flow 5 (TCP connection): ACK_TO_TAP_DUE dropped +1.1288: Flow 5 (TCP connection): STALLED dropped +1.1290: Flow 5 (TCP connection): ACK_FROM_TAP_DUE +1.1290: Flow 5 (TCP connection): timer expires in 1.000s +1.1290: Flow 5 (TCP connection): STALLED +1.1290: Flow 5 (TCP connection): ACK_FROM_TAP_DUE dropped +1.1290: Flow 5 (TCP connection): STALLED dropped +1.1299: Flow 5 (TCP connection): ACK_TO_TAP_DUE +1.1358: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +1.1358: Flow 2 (TCP connection): timer expires in 1.000s +1.1358: Flow 2 (TCP connection): timer expires in 1.000s +1.1359: Flow 2 (TCP connection): timer expires in 1.000s +1.1359: Flow 2 (TCP connection): timer expires in 1.000s +1.1359: Flow 2 (TCP connection): timer expires in 1.000s +1.1359: Flow 2 (TCP connection): timer expires in 1.000s +1.1359: Flow 2 (TCP connection): timer expires in 1.000s +1.1359: Flow 2 (TCP connection): STALLED +1.1359: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped +1.1359: Flow 2 (TCP connection): STALLED dropped +1.1416: Flow 5 (TCP connection): ACK_FROM_TAP_DUE +1.1417: Flow 5 (TCP connection): STALLED +1.1448: Flow 5 (TCP connection): ACK_TO_TAP_DUE dropped +1.1449: Flow 5 (TCP connection): timer expires in 1.000s +1.1449: Flow 5 (TCP connection): timer expires in 1.000s +1.1504: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +1.1504: Flow 2 (TCP connection): timer expires in 1.000s +1.1505: Flow 2 (TCP connection): STALLED +1.1505: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped +1.1505: Flow 2 (TCP connection): STALLED dropped +1.1820: Flow 5 (TCP connection): ACK_FROM_TAP_DUE dropped +1.1820: Flow 5 (TCP connection): STALLED dropped +1.2174: Flow 5 (TCP connection): ACK_FROM_TAP_DUE +1.2174: Flow 5 (TCP connection): timer expires in 1.000s +1.2175: Flow 5 (TCP connection): STALLED +1.2176: Flow 5 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2176: Flow 5 (TCP connection): STALLED dropped +1.2179: Flow 6 (NEW): FREE -> NEW +1.2179: Flow 6 (INI): NEW -> INI +1.2179: Flow 6 (INI): TAP [198.51.100.7]:57247 -> [169.254.1.1]:53 => ? +1.2179: Flow 6 (TGT): INI -> TGT +1.2179: Flow 6 (TGT): TAP [198.51.100.7]:57247 -> [169.254.1.1]:53 => HOST [0.0.0.0]:57247 -> [127.0.0.53]:53 +1.2179: Flow 6 (UDP flow): TGT -> TYPED +1.2179: Flow 6 (UDP flow): TAP [198.51.100.7]:57247 -> [169.254.1.1]:53 => HOST [0.0.0.0]:57247 -> [127.0.0.53]:53 +1.2180: Flow 6 (UDP flow): Side 0 hash table insert: bucket: 56993 +1.2180: Flow 6 (UDP flow): Side 1 hash table insert: bucket: 194914 +1.2180: Flow 6 (UDP flow): TYPED -> ACTIVE +1.2180: Flow 6 (UDP flow): TAP [198.51.100.7]:57247 -> [169.254.1.1]:53 => HOST [127.0.0.1]:57247 -> [127.0.0.53]:53 +1.2225: Flow 7 (NEW): FREE -> NEW +1.2225: Flow 7 (INI): NEW -> INI +1.2225: Flow 7 (INI): TAP [198.51.100.7]:40696 -> [185.199.109.133]:443 => ? +1.2225: Flow 7 (TGT): INI -> TGT +1.2225: Flow 7 (TGT): TAP [198.51.100.7]:40696 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 +1.2226: Flow 7 (TCP connection): TGT -> TYPED +1.2226: Flow 7 (TCP connection): TAP [198.51.100.7]:40696 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 +1.2226: Flow 7 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.2226: Flow 7 (TCP connection): Side 0 hash table insert: bucket: 325936 +1.2226: Flow 7 (TCP connection): TYPED -> ACTIVE +1.2226: Flow 7 (TCP connection): TAP [198.51.100.7]:40696 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 +1.2226: Flow 8 (NEW): FREE -> NEW +1.2226: Flow 8 (INI): NEW -> INI +1.2226: Flow 8 (INI): TAP [198.51.100.7]:35412 -> [185.199.110.133]:443 => ? +1.2226: Flow 8 (TGT): INI -> TGT +1.2226: Flow 8 (TGT): TAP [198.51.100.7]:35412 -> [185.199.110.133]:443 => HOST [0.0.0.0]:0 -> [185.199.110.133]:443 +1.2226: Flow 8 (TCP connection): TGT -> TYPED +1.2226: Flow 8 (TCP connection): TAP [198.51.100.7]:35412 -> [185.199.110.133]:443 => HOST [0.0.0.0]:0 -> [185.199.110.133]:443 +1.2227: Flow 8 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.2227: Flow 8 (TCP connection): Side 0 hash table insert: bucket: 296582 +1.2227: Flow 8 (TCP connection): TYPED -> ACTIVE +1.2227: Flow 8 (TCP connection): TAP [198.51.100.7]:35412 -> [185.199.110.133]:443 => HOST [0.0.0.0]:0 -> [185.199.110.133]:443 +1.2227: Flow 9 (NEW): FREE -> NEW +1.2227: Flow 9 (INI): NEW -> INI +1.2227: Flow 9 (INI): TAP [198.51.100.7]:55588 -> [185.199.111.133]:443 => ? +1.2227: Flow 9 (TGT): INI -> TGT +1.2227: Flow 9 (TGT): TAP [198.51.100.7]:55588 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.2227: Flow 9 (TCP connection): TGT -> TYPED +1.2227: Flow 9 (TCP connection): TAP [198.51.100.7]:55588 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.2227: Flow 9 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.2227: Flow 9 (TCP connection): Side 0 hash table insert: bucket: 261510 +1.2227: Flow 9 (TCP connection): TYPED -> ACTIVE +1.2227: Flow 9 (TCP connection): TAP [198.51.100.7]:55588 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.2227: Flow 10 (NEW): FREE -> NEW +1.2227: Flow 10 (INI): NEW -> INI +1.2227: Flow 10 (INI): TAP [198.51.100.7]:52618 -> [185.199.108.133]:443 => ? +1.2227: Flow 10 (TGT): INI -> TGT +1.2227: Flow 10 (TGT): TAP [198.51.100.7]:52618 -> [185.199.108.133]:443 => HOST [0.0.0.0]:0 -> [185.199.108.133]:443 +1.2227: Flow 10 (TCP connection): TGT -> TYPED +1.2227: Flow 10 (TCP connection): TAP [198.51.100.7]:52618 -> [185.199.108.133]:443 => HOST [0.0.0.0]:0 -> [185.199.108.133]:443 +1.2227: Flow 10 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.2227: Flow 10 (TCP connection): Side 0 hash table insert: bucket: 325642 +1.2227: Flow 10 (TCP connection): TYPED -> ACTIVE +1.2227: Flow 10 (TCP connection): TAP [198.51.100.7]:52618 -> [185.199.108.133]:443 => HOST [0.0.0.0]:0 -> [185.199.108.133]:443 +1.2264: Flow 10 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.2264: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2265: Flow 10 (TCP connection): timer expires in 1.000s +1.2265: Flow 7 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.2265: Flow 7 (TCP connection): ACK_FROM_TAP_DUE +1.2265: Flow 7 (TCP connection): timer expires in 1.000s +1.2266: Flow 9 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.2266: Flow 9 (TCP connection): ACK_FROM_TAP_DUE +1.2266: Flow 9 (TCP connection): timer expires in 1.000s +1.2266: Flow 8 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.2266: Flow 8 (TCP connection): ACK_FROM_TAP_DUE +1.2266: Flow 8 (TCP connection): timer expires in 1.000s +1.2266: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2266: Flow 10 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED +1.2267: Flow 7 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +1.2267: Flow 7 (TCP connection): Side 0 hash table remove: bucket: 325936 +1.2267: Flow 9 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +1.2267: Flow 9 (TCP connection): Side 0 hash table remove: bucket: 261510 +1.2267: Flow 8 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +1.2267: Flow 8 (TCP connection): Side 0 hash table remove: bucket: 296582 +1.2268: Flow 7 (FREE): ACTIVE -> FREE +1.2268: Flow 7 (FREE): TAP [198.51.100.7]:40696 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 +1.2268: Flow 8 (FREE): ACTIVE -> FREE +1.2268: Flow 8 (FREE): TAP [198.51.100.7]:35412 -> [185.199.110.133]:443 => HOST [0.0.0.0]:0 -> [185.199.110.133]:443 +1.2268: Flow 9 (FREE): ACTIVE -> FREE +1.2268: Flow 9 (FREE): TAP [198.51.100.7]:55588 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.2270: Flow 10 (TCP connection): ACK_TO_TAP_DUE +1.2448: Flow 10 (TCP connection): ACK_TO_TAP_DUE dropped +1.2448: Flow 10 (TCP connection): timer expires in 0.000s +1.2592: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2593: Flow 10 (TCP connection): timer expires in 1.000s +1.2593: Flow 10 (TCP connection): timer expires in 1.000s +1.2594: Flow 10 (TCP connection): timer expires in 1.000s +1.2594: Flow 10 (TCP connection): STALLED +1.2594: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2594: Flow 10 (TCP connection): STALLED dropped +1.2609: Flow 10 (TCP connection): ACK_TO_TAP_DUE +1.2642: Flow 10 (TCP connection): ACK_TO_TAP_DUE dropped +1.2642: Flow 10 (TCP connection): timer expires in 0.000s +1.2734: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2734: Flow 10 (TCP connection): timer expires in 1.000s +1.2735: Flow 10 (TCP connection): STALLED +1.2741: Flow 10 (TCP connection): STALLED dropped +1.2741: Flow 10 (TCP connection): timer expires in 1.000s +1.2742: Flow 10 (TCP connection): STALLED +1.2742: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2742: Flow 10 (TCP connection): STALLED dropped +1.2751: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2751: Flow 10 (TCP connection): timer expires in 1.000s +1.2751: Flow 10 (TCP connection): STALLED +1.2761: Flow 10 (TCP connection): STALLED dropped +1.2761: Flow 10 (TCP connection): timer expires in 1.000s +1.2761: Flow 10 (TCP connection): STALLED +1.2761: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2761: Flow 10 (TCP connection): STALLED dropped +1.2771: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2771: Flow 10 (TCP connection): timer expires in 1.000s +1.2772: Flow 10 (TCP connection): STALLED +1.2780: Flow 10 (TCP connection): STALLED dropped +1.2780: Flow 10 (TCP connection): timer expires in 1.000s +1.2781: Flow 10 (TCP connection): STALLED +1.2781: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2781: Flow 10 (TCP connection): STALLED dropped +1.2791: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2792: Flow 10 (TCP connection): timer expires in 1.000s +1.2792: Flow 10 (TCP connection): STALLED +1.2798: Flow 10 (TCP connection): STALLED dropped +1.2798: Flow 10 (TCP connection): timer expires in 1.000s +1.2799: Flow 10 (TCP connection): STALLED +1.2799: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2799: Flow 10 (TCP connection): STALLED dropped +1.2808: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2808: Flow 10 (TCP connection): timer expires in 1.000s +1.2808: Flow 10 (TCP connection): STALLED +1.2820: Flow 10 (TCP connection): STALLED dropped +1.2821: Flow 10 (TCP connection): timer expires in 1.000s +1.2821: Flow 10 (TCP connection): STALLED +1.2821: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2821: Flow 10 (TCP connection): STALLED dropped +1.2831: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2832: Flow 10 (TCP connection): timer expires in 1.000s +1.2832: Flow 10 (TCP connection): STALLED +1.2832: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2832: Flow 10 (TCP connection): STALLED dropped +1.2841: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2841: Flow 10 (TCP connection): timer expires in 1.000s +1.2841: Flow 10 (TCP connection): STALLED +1.2847: Flow 10 (TCP connection): STALLED dropped +1.2847: Flow 10 (TCP connection): timer expires in 1.000s +1.2848: Flow 10 (TCP connection): STALLED +1.2849: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2849: Flow 10 (TCP connection): STALLED dropped +1.2853: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2853: Flow 10 (TCP connection): timer expires in 1.000s +1.2853: Flow 10 (TCP connection): STALLED +1.2862: Flow 10 (TCP connection): STALLED dropped +1.2862: Flow 10 (TCP connection): timer expires in 1.000s +1.2863: Flow 10 (TCP connection): STALLED +1.2863: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2863: Flow 10 (TCP connection): STALLED dropped +1.2871: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2871: Flow 10 (TCP connection): timer expires in 1.000s +1.2872: Flow 10 (TCP connection): STALLED +1.2877: Flow 10 (TCP connection): STALLED dropped +1.2877: Flow 10 (TCP connection): timer expires in 1.000s +1.2878: Flow 10 (TCP connection): STALLED +1.2878: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2878: Flow 10 (TCP connection): STALLED dropped +1.2886: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2886: Flow 10 (TCP connection): timer expires in 1.000s +1.2886: Flow 10 (TCP connection): STALLED +1.2890: Flow 10 (TCP connection): STALLED dropped +1.2890: Flow 10 (TCP connection): timer expires in 1.000s +1.2890: Flow 10 (TCP connection): STALLED +1.2890: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2890: Flow 10 (TCP connection): STALLED dropped +1.2893: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2893: Flow 10 (TCP connection): timer expires in 1.000s +1.2893: Flow 10 (TCP connection): STALLED +1.2894: Flow 10 (TCP connection): STALLED dropped +1.2894: Flow 10 (TCP connection): timer expires in 1.000s +1.2895: Flow 10 (TCP connection): STALLED +1.2895: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2895: Flow 10 (TCP connection): STALLED dropped +1.2899: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2899: Flow 10 (TCP connection): timer expires in 1.000s +1.2899: Flow 10 (TCP connection): STALLED +1.2901: Flow 10 (TCP connection): STALLED dropped +1.2901: Flow 10 (TCP connection): timer expires in 1.000s +1.2902: Flow 10 (TCP connection): timer expires in 1.000s +1.2902: Flow 10 (TCP connection): STALLED +1.2902: Flow 10 (TCP connection): timer expires in 1.000s +1.2902: Flow 10 (TCP connection): STALLED dropped +1.2902: Flow 10 (TCP connection): STALLED +1.2904: Flow 10 (TCP connection): STALLED dropped +1.2904: Flow 10 (TCP connection): timer expires in 1.000s +1.2905: Flow 10 (TCP connection): STALLED +1.2905: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2905: Flow 10 (TCP connection): STALLED dropped +1.2907: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2907: Flow 10 (TCP connection): timer expires in 1.000s +1.2907: Flow 10 (TCP connection): timer expires in 1.000s +1.2907: Flow 10 (TCP connection): STALLED +1.2907: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2907: Flow 10 (TCP connection): STALLED dropped +1.2936: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2936: Flow 10 (TCP connection): timer expires in 1.000s +1.2936: Flow 10 (TCP connection): STALLED +1.2938: Flow 10 (TCP connection): STALLED dropped +1.2938: Flow 10 (TCP connection): timer expires in 1.000s +1.2939: Flow 10 (TCP connection): timer expires in 1.000s +1.2939: Flow 10 (TCP connection): timer expires in 1.000s +1.2939: Flow 10 (TCP connection): STALLED +1.2941: Flow 10 (TCP connection): STALLED dropped +1.2942: Flow 10 (TCP connection): timer expires in 1.000s +1.2942: Flow 10 (TCP connection): STALLED +1.2942: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2942: Flow 10 (TCP connection): STALLED dropped +1.2944: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2944: Flow 10 (TCP connection): timer expires in 1.000s +1.2944: Flow 10 (TCP connection): timer expires in 1.000s +1.2944: Flow 10 (TCP connection): STALLED +1.2945: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2945: Flow 10 (TCP connection): STALLED dropped +1.2947: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2947: Flow 10 (TCP connection): timer expires in 1.000s +1.2948: Flow 10 (TCP connection): STALLED +1.2949: Flow 10 (TCP connection): STALLED dropped +1.2949: Flow 10 (TCP connection): timer expires in 1.000s +1.2950: Flow 10 (TCP connection): timer expires in 1.000s +1.2950: Flow 10 (TCP connection): timer expires in 1.000s +1.2950: Flow 10 (TCP connection): STALLED +1.2952: Flow 10 (TCP connection): STALLED dropped +1.2952: Flow 10 (TCP connection): timer expires in 1.000s +1.2953: Flow 10 (TCP connection): STALLED +1.2953: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2953: Flow 10 (TCP connection): STALLED dropped +1.2955: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2955: Flow 10 (TCP connection): timer expires in 1.000s +1.2955: Flow 10 (TCP connection): STALLED +1.2958: Flow 10 (TCP connection): STALLED dropped +1.2958: Flow 10 (TCP connection): timer expires in 1.000s +1.2958: Flow 10 (TCP connection): STALLED +1.2958: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2958: Flow 10 (TCP connection): STALLED dropped +1.2961: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2961: Flow 10 (TCP connection): timer expires in 1.000s +1.2961: Flow 10 (TCP connection): STALLED +1.2963: Flow 10 (TCP connection): STALLED dropped +1.2963: Flow 10 (TCP connection): timer expires in 1.000s +1.2963: Flow 10 (TCP connection): STALLED +1.2964: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2964: Flow 10 (TCP connection): STALLED dropped +1.2966: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2966: Flow 10 (TCP connection): timer expires in 1.000s +1.2967: Flow 10 (TCP connection): STALLED +1.2969: Flow 10 (TCP connection): STALLED dropped +1.2969: Flow 10 (TCP connection): timer expires in 1.000s +1.2969: Flow 10 (TCP connection): STALLED +1.2969: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2969: Flow 10 (TCP connection): STALLED dropped +1.2971: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2971: Flow 10 (TCP connection): timer expires in 1.000s +1.2972: Flow 10 (TCP connection): STALLED +1.2974: Flow 10 (TCP connection): STALLED dropped +1.2974: Flow 10 (TCP connection): timer expires in 1.000s +1.2974: Flow 10 (TCP connection): STALLED +1.2974: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2974: Flow 10 (TCP connection): STALLED dropped +1.2977: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2977: Flow 10 (TCP connection): timer expires in 1.000s +1.2977: Flow 10 (TCP connection): STALLED +1.2979: Flow 10 (TCP connection): STALLED dropped +1.2979: Flow 10 (TCP connection): timer expires in 1.000s +1.2980: Flow 10 (TCP connection): STALLED +1.2980: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2980: Flow 10 (TCP connection): STALLED dropped +1.2983: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2983: Flow 10 (TCP connection): timer expires in 1.000s +1.2983: Flow 10 (TCP connection): STALLED +1.2985: Flow 10 (TCP connection): STALLED dropped +1.2985: Flow 10 (TCP connection): timer expires in 1.000s +1.2985: Flow 10 (TCP connection): STALLED +1.2985: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2985: Flow 10 (TCP connection): STALLED dropped +1.2988: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2988: Flow 10 (TCP connection): timer expires in 1.000s +1.2988: Flow 10 (TCP connection): STALLED +1.2990: Flow 10 (TCP connection): STALLED dropped +1.2990: Flow 10 (TCP connection): timer expires in 1.000s +1.2991: Flow 10 (TCP connection): STALLED +1.2991: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2991: Flow 10 (TCP connection): STALLED dropped +1.2994: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2994: Flow 10 (TCP connection): timer expires in 1.000s +1.2994: Flow 10 (TCP connection): STALLED +1.2996: Flow 10 (TCP connection): STALLED dropped +1.2996: Flow 10 (TCP connection): timer expires in 1.000s +1.2996: Flow 10 (TCP connection): STALLED +1.2996: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2996: Flow 10 (TCP connection): STALLED dropped +1.2999: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2999: Flow 10 (TCP connection): timer expires in 1.000s +1.3000: Flow 10 (TCP connection): STALLED +1.3002: Flow 10 (TCP connection): STALLED dropped +1.3002: Flow 10 (TCP connection): timer expires in 1.000s +1.3004: Flow 10 (TCP connection): STALLED +1.3004: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.3004: Flow 10 (TCP connection): STALLED dropped +1.3005: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.3005: Flow 10 (TCP connection): timer expires in 1.000s +1.3006: Flow 10 (TCP connection): timer expires in 1.000s +1.3006: Flow 10 (TCP connection): STALLED +1.3007: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.3007: Flow 10 (TCP connection): STALLED dropped +1.3008: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.3008: Flow 10 (TCP connection): timer expires in 1.000s +1.3008: Flow 10 (TCP connection): STALLED +1.3011: Flow 10 (TCP connection): STALLED dropped +1.3011: Flow 10 (TCP connection): timer expires in 1.000s +1.3012: Flow 10 (TCP connection): timer expires in 1.000s +1.3012: Flow 10 (TCP connection): STALLED +1.3012: Flow 10 (TCP connection): timer expires in 1.000s +1.3012: Flow 10 (TCP connection): STALLED dropped +1.3012: Flow 10 (TCP connection): STALLED +1.3014: Flow 10 (TCP connection): STALLED dropped +1.3014: Flow 10 (TCP connection): timer expires in 1.000s +1.3014: Flow 10 (TCP connection): timer expires in 1.000s +1.3014: Flow 10 (TCP connection): STALLED +1.3014: Flow 10 (TCP connection): timer expires in 1.000s +1.3014: Flow 10 (TCP connection): STALLED dropped +1.3014: Flow 10 (TCP connection): STALLED +1.3017: Flow 10 (TCP connection): STALLED dropped +1.3017: Flow 10 (TCP connection): timer expires in 1.000s +1.3017: Flow 10 (TCP connection): STALLED +1.3017: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.3017: Flow 10 (TCP connection): STALLED dropped +1.3020: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.3020: Flow 10 (TCP connection): timer expires in 1.000s +1.3020: Flow 10 (TCP connection): STALLED +1.3219: Flow 7 (NEW): FREE -> NEW +1.3219: Flow 7 (INI): NEW -> INI +1.3219: Flow 7 (INI): TAP [198.51.100.7]:49856 -> [169.254.1.1]:53 => ? +1.3219: Flow 7 (TGT): INI -> TGT +1.3219: Flow 7 (TGT): TAP [198.51.100.7]:49856 -> [169.254.1.1]:53 => HOST [0.0.0.0]:49856 -> [127.0.0.53]:53 +1.3219: Flow 7 (UDP flow): TGT -> TYPED +1.3219: Flow 7 (UDP flow): TAP [198.51.100.7]:49856 -> [169.254.1.1]:53 => HOST [0.0.0.0]:49856 -> [127.0.0.53]:53 +1.3219: Flow 7 (UDP flow): Side 0 hash table insert: bucket: 277953 +1.3219: Flow 7 (UDP flow): Side 1 hash table insert: bucket: 193400 +1.3219: Flow 7 (UDP flow): TYPED -> ACTIVE +1.3219: Flow 7 (UDP flow): TAP [198.51.100.7]:49856 -> [169.254.1.1]:53 => HOST [127.0.0.1]:49856 -> [127.0.0.53]:53 +1.3274: Flow 8 (NEW): FREE -> NEW +1.3274: Flow 8 (INI): NEW -> INI +1.3274: Flow 8 (INI): TAP [2001:db8::7]:38294 -> [2606:50c0:8003::154]:443 => ? +1.3274: Flow 8 (TGT): INI -> TGT +1.3275: Flow 8 (TGT): TAP [2001:db8::7]:38294 -> [2606:50c0:8003::154]:443 => HOST [::]:0 -> [2606:50c0:8003::154]:443 +1.3275: Flow 8 (TCP connection): TGT -> TYPED +1.3275: Flow 8 (TCP connection): TAP [2001:db8::7]:38294 -> [2606:50c0:8003::154]:443 => HOST [::]:0 -> [2606:50c0:8003::154]:443 +1.3275: Flow 8 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.3275: Flow 8 (TCP connection): Side 0 hash table insert: bucket: 189691 +1.3275: Flow 8 (TCP connection): TYPED -> ACTIVE +1.3275: Flow 8 (TCP connection): TAP [2001:db8::7]:38294 -> [2606:50c0:8003::154]:443 => HOST [::]:0 -> [2606:50c0:8003::154]:443 +1.3275: Flow 9 (NEW): FREE -> NEW +1.3275: Flow 9 (INI): NEW -> INI +1.3275: Flow 9 (INI): TAP [198.51.100.7]:40716 -> [185.199.109.133]:443 => ? +1.3276: Flow 9 (TGT): INI -> TGT +1.3276: Flow 9 (TGT): TAP [198.51.100.7]:40716 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 +1.3276: Flow 9 (TCP connection): TGT -> TYPED +1.3276: Flow 9 (TCP connection): TAP [198.51.100.7]:40716 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 +1.3276: Flow 9 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.3276: Flow 9 (TCP connection): Side 0 hash table insert: bucket: 13717 +1.3276: Flow 9 (TCP connection): TYPED -> ACTIVE +1.3276: Flow 9 (TCP connection): TAP [198.51.100.7]:40716 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 +1.3276: Flow 11 (NEW): FREE -> NEW +1.3276: Flow 11 (INI): NEW -> INI +1.3276: Flow 11 (INI): TAP [198.51.100.7]:55600 -> [185.199.111.133]:443 => ? +1.3276: Flow 11 (TGT): INI -> TGT +1.3276: Flow 11 (TGT): TAP [198.51.100.7]:55600 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.3276: Flow 11 (TCP connection): TGT -> TYPED +1.3276: Flow 11 (TCP connection): TAP [198.51.100.7]:55600 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.3276: Flow 11 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.3276: Flow 11 (TCP connection): Side 0 hash table insert: bucket: 280556 +1.3276: Flow 11 (TCP connection): TYPED -> ACTIVE +1.3276: Flow 11 (TCP connection): TAP [198.51.100.7]:55600 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.3276: Flow 12 (NEW): FREE -> NEW +1.3276: Flow 12 (INI): NEW -> INI +1.3276: Flow 12 (INI): TAP [2001:db8::7]:36704 -> [2606:50c0:8001::154]:443 => ? +1.3276: Flow 12 (TGT): INI -> TGT +1.3276: Flow 12 (TGT): TAP [2001:db8::7]:36704 -> [2606:50c0:8001::154]:443 => HOST [::]:0 -> [2606:50c0:8001::154]:443 +1.3276: Flow 12 (TCP connection): TGT -> TYPED +1.3276: Flow 12 (TCP connection): TAP [2001:db8::7]:36704 -> [2606:50c0:8001::154]:443 => HOST [::]:0 -> [2606:50c0:8001::154]:443 +1.3276: Flow 12 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.3276: Flow 12 (TCP connection): Side 0 hash table insert: bucket: 206072 +1.3277: Flow 12 (TCP connection): TYPED -> ACTIVE +1.3277: Flow 12 (TCP connection): TAP [2001:db8::7]:36704 -> [2606:50c0:8001::154]:443 => HOST [::]:0 -> [2606:50c0:8001::154]:443 +1.3308: Flow 9 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.3308: Flow 9 (TCP connection): ACK_FROM_TAP_DUE +1.3308: Flow 9 (TCP connection): timer expires in 1.000s +1.3308: Flow 8 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.3308: Flow 8 (TCP connection): ACK_FROM_TAP_DUE +1.3309: Flow 8 (TCP connection): timer expires in 1.000s +1.3309: Flow 9 (TCP connection): ACK_FROM_TAP_DUE dropped +1.3309: Flow 9 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED +1.3310: Flow 8 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +1.3310: Flow 8 (TCP connection): Side 0 hash table remove: bucket: 189691 +1.3310: Flow 12 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.3310: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +1.3310: Flow 12 (TCP connection): timer expires in 1.000s +1.3311: Flow 11 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.3311: Flow 11 (TCP connection): ACK_FROM_TAP_DUE +1.3311: Flow 11 (TCP connection): timer expires in 1.000s +1.3313: Flow 8 (FREE): ACTIVE -> FREE +1.3313: Flow 8 (FREE): TAP [2001:db8::7]:38294 -> [2606:50c0:8003::154]:443 => HOST [::]:0 -> [2606:50c0:8003::154]:443 +1.3313: Flow 11 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +1.3313: Flow 11 (TCP connection): Side 0 hash table remove: bucket: 280556 +1.3314: Flow 9 (TCP connection): ACK_TO_TAP_DUE +1.3314: Flow 12 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +1.3314: Flow 12 (TCP connection): Side 0 hash table remove: bucket: 206072 +1.3315: Flow 11 (FREE): ACTIVE -> FREE +1.3315: Flow 11 (FREE): TAP [198.51.100.7]:55600 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.3315: Flow 12 (FREE): ACTIVE -> FREE +1.3315: Flow 12 (FREE): TAP [2001:db8::7]:36704 -> [2606:50c0:8001::154]:443 => HOST [::]:0 -> [2606:50c0:8001::154]:443 +1.3380: Flow 9 (TCP connection): ACK_TO_TAP_DUE dropped +1.3381: Flow 9 (TCP connection): timer expires in 0.000s +1.3398: Flow 9 (TCP connection): ACK_FROM_TAP_DUE +1.3399: Flow 9 (TCP connection): timer expires in 1.000s +1.3400: Flow 9 (TCP connection): STALLED +1.3400: Flow 9 (TCP connection): ACK_FROM_TAP_DUE dropped +1.3400: Flow 9 (TCP connection): STALLED dropped +1.3418: Flow 9 (TCP connection): ACK_TO_TAP_DUE +1.3430: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.3430: Flow 10 (TCP connection): STALLED dropped +1.3451: Flow 9 (TCP connection): ACK_TO_TAP_DUE dropped +1.3451: Flow 9 (TCP connection): timer expires in 0.000s +1.3670: Flow 9 (TCP connection): ACK_FROM_TAP_DUE +1.3670: Flow 9 (TCP connection): timer expires in 1.000s +1.3671: Flow 9 (TCP connection): STALLED +1.4080: Flow 9 (TCP connection): ACK_FROM_TAP_DUE dropped +1.4080: Flow 9 (TCP connection): STALLED dropped +740.9544: Flow 0 (TCP connection (spliced)): FIN_RCVD_0 +740.9545: Flow 0 (TCP connection (spliced)): FIN_SENT_1 +740.9545: Flow 0 (TCP connection (spliced)): RCVLOWAT_ACT_1 +740.9545: Flow 0 (TCP connection (spliced)): CLOSED +740.9546: Flow 0 (FREE): ACTIVE -> FREE +740.9546: Flow 0 (FREE): HOST [127.0.0.1]:51982 -> [127.65.81.1]:24700 => SPLICE [0.0.0.0]:0 -> [198.51.100.7]:24700 +740.9546: Flow 0 (NEW): FREE -> NEW +740.9547: Flow 0 (INI): NEW -> INI +740.9547: Flow 0 (INI): HOST [127.0.0.1]:38982 -> [0.0.0.0]:24700 => ? +740.9547: Flow 0 (TGT): INI -> TGT +740.9547: Flow 0 (TGT): HOST [127.0.0.1]:38982 -> [127.65.81.1]:24700 => SPLICE [0.0.0.0]:0 -> [198.51.100.7]:24700 +740.9547: Flow 0 (TCP connection (spliced)): TGT -> TYPED +740.9547: Flow 0 (TCP connection (spliced)): HOST [127.0.0.1]:38982 -> [127.65.81.1]:24700 => SPLICE [0.0.0.0]:0 -> [198.51.100.7]:24700 +740.9548: Flow 0 (TCP connection (spliced)): SPLICE_CONNECT +740.9548: Flow 0 (TCP connection (spliced)): TYPED -> ACTIVE +740.9549: Flow 0 (TCP connection (spliced)): HOST [127.0.0.1]:38982 -> [127.65.81.1]:24700 => SPLICE [0.0.0.0]:0 -> [198.51.100.7]:24700 +740.9549: Flow 0 (TCP connection (spliced)): SPLICE_ESTABLISHED +7.3984: Flow 8 (NEW): FREE -> NEW +7.3984: Flow 8 (INI): NEW -> INI +7.3984: Flow 8 (INI): TAP [198.51.100.7]:52061 -> [169.254.1.1]:53 => ? +7.3984: Flow 8 (TGT): INI -> TGT +7.3984: Flow 8 (TGT): TAP [198.51.100.7]:52061 -> [169.254.1.1]:53 => HOST [0.0.0.0]:52061 -> [127.0.0.53]:53 +7.3984: Flow 8 (UDP flow): TGT -> TYPED +7.3984: Flow 8 (UDP flow): TAP [198.51.100.7]:52061 -> [169.254.1.1]:53 => HOST [0.0.0.0]:52061 -> [127.0.0.53]:53 +7.3984: Flow 8 (UDP flow): Side 0 hash table insert: bucket: 191745 +7.3984: Flow 8 (UDP flow): Side 1 hash table insert: bucket: 265803 +7.3984: Flow 8 (UDP flow): TYPED -> ACTIVE +7.3984: Flow 8 (UDP flow): TAP [198.51.100.7]:52061 -> [169.254.1.1]:53 => HOST [127.0.0.1]:52061 -> [127.0.0.53]:53 +7.4192: Flow 11 (NEW): FREE -> NEW +7.4192: Flow 11 (INI): NEW -> INI +7.4192: Flow 11 (INI): TAP [198.51.100.7]:55606 -> [44.193.164.157]:443 => ? +7.4192: Flow 11 (TGT): INI -> TGT +7.4192: Flow 11 (TGT): TAP [198.51.100.7]:55606 -> [44.193.164.157]:443 => HOST [0.0.0.0]:0 -> [44.193.164.157]:443 +7.4192: Flow 11 (TCP connection): TGT -> TYPED +7.4192: Flow 11 (TCP connection): TAP [198.51.100.7]:55606 -> [44.193.164.157]:443 => HOST [0.0.0.0]:0 -> [44.193.164.157]:443 +7.4192: Flow 11 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +7.4193: Flow 11 (TCP connection): Side 0 hash table insert: bucket: 371710 +7.4193: Flow 11 (TCP connection): TYPED -> ACTIVE +7.4193: Flow 11 (TCP connection): TAP [198.51.100.7]:55606 -> [44.193.164.157]:443 => HOST [0.0.0.0]:0 -> [44.193.164.157]:443 +7.4193: Flow 12 (NEW): FREE -> NEW +7.4193: Flow 12 (INI): NEW -> INI +7.4193: Flow 12 (INI): TAP [198.51.100.7]:44110 -> [100.50.38.106]:443 => ? +7.4193: Flow 12 (TGT): INI -> TGT +7.4193: Flow 12 (TGT): TAP [198.51.100.7]:44110 -> [100.50.38.106]:443 => HOST [0.0.0.0]:0 -> [100.50.38.106]:443 +7.4193: Flow 12 (TCP connection): TGT -> TYPED +7.4193: Flow 12 (TCP connection): TAP [198.51.100.7]:44110 -> [100.50.38.106]:443 => HOST [0.0.0.0]:0 -> [100.50.38.106]:443 +7.4193: Flow 12 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +7.4193: Flow 12 (TCP connection): Side 0 hash table insert: bucket: 134729 +7.4193: Flow 12 (TCP connection): TYPED -> ACTIVE +7.4193: Flow 12 (TCP connection): TAP [198.51.100.7]:44110 -> [100.50.38.106]:443 => HOST [0.0.0.0]:0 -> [100.50.38.106]:443 +7.4193: Flow 13 (NEW): FREE -> NEW +7.4193: Flow 13 (INI): NEW -> INI +7.4193: Flow 13 (INI): TAP [198.51.100.7]:39966 -> [23.22.18.51]:443 => ? +7.4193: Flow 13 (TGT): INI -> TGT +7.4193: Flow 13 (TGT): TAP [198.51.100.7]:39966 -> [23.22.18.51]:443 => HOST [0.0.0.0]:0 -> [23.22.18.51]:443 +7.4193: Flow 13 (TCP connection): TGT -> TYPED +7.4193: Flow 13 (TCP connection): TAP [198.51.100.7]:39966 -> [23.22.18.51]:443 => HOST [0.0.0.0]:0 -> [23.22.18.51]:443 +7.4193: Flow 13 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +7.4193: Flow 13 (TCP connection): Side 0 hash table insert: bucket: 145688 +7.4193: Flow 13 (TCP connection): TYPED -> ACTIVE +7.4193: Flow 13 (TCP connection): TAP [198.51.100.7]:39966 -> [23.22.18.51]:443 => HOST [0.0.0.0]:0 -> [23.22.18.51]:443 +7.4193: Flow 14 (NEW): FREE -> NEW +7.4193: Flow 14 (INI): NEW -> INI +7.4193: Flow 14 (INI): TAP [198.51.100.7]:46830 -> [100.31.23.131]:443 => ? +7.4193: Flow 14 (TGT): INI -> TGT +7.4193: Flow 14 (TGT): TAP [198.51.100.7]:46830 -> [100.31.23.131]:443 => HOST [0.0.0.0]:0 -> [100.31.23.131]:443 +7.4193: Flow 14 (TCP connection): TGT -> TYPED +7.4193: Flow 14 (TCP connection): TAP [198.51.100.7]:46830 -> [100.31.23.131]:443 => HOST [0.0.0.0]:0 -> [100.31.23.131]:443 +7.4193: Flow 14 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +7.4193: Flow 14 (TCP connection): Side 0 hash table insert: bucket: 20416 +7.4193: Flow 14 (TCP connection): TYPED -> ACTIVE +7.4193: Flow 14 (TCP connection): TAP [198.51.100.7]:46830 -> [100.31.23.131]:443 => HOST [0.0.0.0]:0 -> [100.31.23.131]:443 +7.4292: Flow 12 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +7.4292: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +7.4292: Flow 12 (TCP connection): timer expires in 1.000s +7.4292: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +7.4292: Flow 12 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED +7.4293: Flow 11 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +7.4293: Flow 11 (TCP connection): ACK_FROM_TAP_DUE +7.4293: Flow 11 (TCP connection): timer expires in 1.000s +7.4294: Flow 11 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +7.4294: Flow 11 (TCP connection): Side 0 hash table remove: bucket: 371710 +7.4294: Flow 11 (FREE): ACTIVE -> FREE +7.4294: Flow 11 (FREE): TAP [198.51.100.7]:55606 -> [44.193.164.157]:443 => HOST [0.0.0.0]:0 -> [44.193.164.157]:443 +7.4294: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.4294: Flow 14 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +7.4294: Flow 14 (TCP connection): ACK_FROM_TAP_DUE +7.4295: Flow 14 (TCP connection): timer expires in 1.000s +7.4295: Flow 14 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +7.4295: Flow 14 (TCP connection): Side 0 hash table remove: bucket: 20416 +7.4295: Flow 14 (FREE): ACTIVE -> FREE +7.4295: Flow 14 (FREE): TAP [198.51.100.7]:46830 -> [100.31.23.131]:443 => HOST [0.0.0.0]:0 -> [100.31.23.131]:443 +7.4296: Flow 13 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +7.4296: Flow 13 (TCP connection): ACK_FROM_TAP_DUE +7.4296: Flow 13 (TCP connection): timer expires in 1.000s +7.4296: Flow 13 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +7.4296: Flow 13 (TCP connection): Side 0 hash table remove: bucket: 145688 +7.4297: Flow 13 (FREE): ACTIVE -> FREE +7.4297: Flow 13 (FREE): TAP [198.51.100.7]:39966 -> [23.22.18.51]:443 => HOST [0.0.0.0]:0 -> [23.22.18.51]:443 +7.4423: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.4423: Flow 12 (TCP connection): timer expires in 0.000s +7.4460: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +7.4460: Flow 12 (TCP connection): timer expires in 1.000s +7.4461: Flow 12 (TCP connection): STALLED +7.4461: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +7.4461: Flow 12 (TCP connection): STALLED dropped +7.4472: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +7.4472: Flow 12 (TCP connection): timer expires in 1.000s +7.4473: Flow 12 (TCP connection): STALLED +7.4473: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +7.4473: Flow 12 (TCP connection): STALLED dropped +7.4477: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.4573: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +7.4573: Flow 12 (TCP connection): STALLED +7.4605: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.4605: Flow 12 (TCP connection): timer expires in 1.000s +7.4605: Flow 12 (TCP connection): timer expires in 1.000s +7.4980: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +7.4980: Flow 12 (TCP connection): STALLED dropped +7.6732: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.6925: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.6926: Flow 12 (TCP connection): timer expires in 0.000s +7.6928: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.6960: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.6960: Flow 12 (TCP connection): timer expires in 0.000s +7.6962: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.7059: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.7059: Flow 12 (TCP connection): timer expires in 0.000s +7.7063: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.7127: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.7128: Flow 12 (TCP connection): timer expires in 0.000s +7.7130: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.7163: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.7163: Flow 12 (TCP connection): timer expires in 0.000s +7.7170: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.7234: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.7235: Flow 12 (TCP connection): timer expires in 0.000s +746.9581: Flow 0 (TCP connection (spliced)): FIN_RCVD_1 +746.9582: Flow 0 (TCP connection (spliced)): FIN_SENT_0 +9.9313: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +9.9313: Flow 12 (TCP connection): timer expires in 1.000s +9.9314: Flow 12 (TCP connection): STALLED +9.9314: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +9.9314: Flow 12 (TCP connection): STALLED dropped +10.1216: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +10.1216: Flow 12 (TCP connection): timer expires in 1.000s +10.1217: Flow 12 (TCP connection): STALLED +10.1217: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +10.1217: Flow 12 (TCP connection): STALLED dropped +11.2100: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +11.2100: Flow 12 (TCP connection): timer expires in 1.000s +11.2101: Flow 12 (TCP connection): STALLED +11.2101: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +11.2101: Flow 12 (TCP connection): STALLED dropped +11.2139: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +11.2139: Flow 12 (TCP connection): timer expires in 1.000s +11.2139: Flow 12 (TCP connection): STALLED +11.2140: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +11.2140: Flow 12 (TCP connection): STALLED dropped +11.2181: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +11.2181: Flow 12 (TCP connection): timer expires in 1.000s +11.2182: Flow 12 (TCP connection): STALLED +11.2182: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +11.2182: Flow 12 (TCP connection): STALLED dropped +11.2184: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +11.2184: Flow 12 (TCP connection): timer expires in 1.000s +11.2184: Flow 12 (TCP connection): STALLED +11.2184: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +11.2184: Flow 12 (TCP connection): STALLED dropped +11.2484: Flow 11 (NEW): FREE -> NEW +11.2484: Flow 11 (INI): NEW -> INI +11.2484: Flow 11 (INI): TAP [198.51.100.7]:56854 -> [169.254.1.1]:53 => ? +11.2484: Flow 11 (TGT): INI -> TGT +11.2484: Flow 11 (TGT): TAP [198.51.100.7]:56854 -> [169.254.1.1]:53 => HOST [0.0.0.0]:56854 -> [127.0.0.53]:53 +11.2484: Flow 11 (UDP flow): TGT -> TYPED +11.2484: Flow 11 (UDP flow): TAP [198.51.100.7]:56854 -> [169.254.1.1]:53 => HOST [0.0.0.0]:56854 -> [127.0.0.53]:53 +11.2484: Flow 11 (UDP flow): Side 0 hash table insert: bucket: 348561 +11.2484: Flow 11 (UDP flow): Side 1 hash table insert: bucket: 21593 +11.2484: Flow 11 (UDP flow): TYPED -> ACTIVE +11.2484: Flow 11 (UDP flow): TAP [198.51.100.7]:56854 -> [169.254.1.1]:53 => HOST [127.0.0.1]:56854 -> [127.0.0.53]:53 +11.2485: Flow 13 (NEW): FREE -> NEW +11.2485: Flow 13 (INI): NEW -> INI +11.2485: Flow 13 (INI): TAP [198.51.100.7]:49064 -> [169.254.1.1]:53 => ? +11.2485: Flow 13 (TGT): INI -> TGT +11.2485: Flow 13 (TGT): TAP [198.51.100.7]:49064 -> [169.254.1.1]:53 => HOST [0.0.0.0]:49064 -> [127.0.0.53]:53 +11.2485: Flow 13 (UDP flow): TGT -> TYPED +11.2485: Flow 13 (UDP flow): TAP [198.51.100.7]:49064 -> [169.254.1.1]:53 => HOST [0.0.0.0]:49064 -> [127.0.0.53]:53 +11.2485: Flow 13 (UDP flow): Side 0 hash table insert: bucket: 125764 +11.2485: Flow 13 (UDP flow): Side 1 hash table insert: bucket: 213015 +11.2485: Flow 13 (UDP flow): TYPED -> ACTIVE +11.2485: Flow 13 (UDP flow): TAP [198.51.100.7]:49064 -> [169.254.1.1]:53 => HOST [127.0.0.1]:49064 -> [127.0.0.53]:53 +11.2487: Flow 14 (NEW): FREE -> NEW +11.2487: Flow 14 (INI): NEW -> INI +0.0012: ERROR: Listening address not allowed for -T 127.0.42.1/3000 diff --git a/tests/fakes/_nsproxy.py b/tests/fakes/_nsproxy.py index 40a6277..fab8cad 100644 --- a/tests/fakes/_nsproxy.py +++ b/tests/fakes/_nsproxy.py @@ -59,6 +59,9 @@ def _serve(chan): s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((req["host"], req["port"])) s.listen(16) + elif req["op"] == "udp": + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.bind((req["host"], req["port"])) elif req["op"] == "run": r = subprocess.run(req["cmd"], capture_output=True, text=True) chan.send(json.dumps({"rc": r.returncode, "err": r.stderr}).encode()) @@ -87,6 +90,9 @@ def connect(self, host, port): def listen(self, host, port): return self._request(op="listen", host=host, port=port) + def udp(self, host, port): + return self._request(op="udp", host=host, port=port) + def run(self, cmd): return self._request(op="run", cmd=cmd) @@ -106,7 +112,21 @@ def _pipe(src, dst): pass -def forward(listener, connect, log=None, label=""): +def forward(listener, connect, on_open=None, on_close=None): + """Splice each connection accepted on LISTENER to connect(). on_open(peer) + returns a token given to on_close(token) once both directions are done.""" + + def conn(c, t, token): + threads = [threading.Thread(target=_pipe, args=(a, b), daemon=True) for a, b in ((c, t), (t, c))] + for th in threads: + th.start() + for th in threads: + th.join() + c.close() + t.close() + if on_close: + on_close(token) + def loop(): while True: try: @@ -115,15 +135,11 @@ def loop(): return try: t = connect() - except OSError as e: - if log: - log("Flow %s: %s:%s refused: %s" % (label, peer[0], peer[1], e)) + except OSError: c.close() continue - if log: - log("Flow %s: TAP_FWD %s:%s => %s" % (label, peer[0], peer[1], t.getpeername())) - for a, b in ((c, t), (t, c)): - threading.Thread(target=_pipe, args=(a, b), daemon=True).start() + token = on_open(peer) if on_open else None + threading.Thread(target=conn, args=(c, t, token), daemon=True).start() threading.Thread(target=loop, daemon=True).start() diff --git a/tests/fakes/pasta-bin/pasta b/tests/fakes/pasta-bin/pasta index 91b4b6e..c5ac830 100755 --- a/tests/fakes/pasta-bin/pasta +++ b/tests/fakes/pasta-bin/pasta @@ -6,7 +6,11 @@ Like the real one it closes inherited descriptors above 2 before opening network namespace, starts the command in it and stays its parent, and forwards -t (host -> namespace interface address) and -T (namespace loopback -> host loopback) ports. Like the real one (fwd_rule.c), -T and -U -take no listening address: "-T ADDR/..." stops it with pasta's error. The namespace gets a dummy interface with 192.168.77.2 +take no listening address: "-T ADDR/..." stops it with pasta's error. With +--dns-forward ADDR it answers DNS queries to ADDR:53 itself (every name is +192.0.2.10). Its debug lines (--debug --log-file) use pasta's format: a +timestamp, then "Flow N (KIND): ..." state changes and endpoints, plus timer +noise, so the launcher's condenser can be tested against them. The namespace gets a dummy interface with 192.168.77.2 instead of a tap with the host's address. With --debug and --log-file it writes header, startup and "Flow" lines to the log file (blocking writes, as pasta's are). The failure mode is read from $FAKE_NET_DIR/pasta.fail @@ -14,8 +18,10 @@ pasta's are). The failure mode is read from $FAKE_NET_DIR/pasta.fail """ import os +import struct import sys import threading +import time sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) import _nsproxy # noqa: E402 @@ -66,12 +72,49 @@ if "--log-file" in opts: log_fd = os.open(opts["--log-file"], os.O_CREAT | os.O_TRUNC | os.O_APPEND | os.O_RDWR, 0o600) os.write(log_fd, ("pasta fake-2026_10_02: %s (%d)\n" % (os.path.realpath(__file__), os.getpid())).encode()) log_lock = threading.Lock() +T0 = time.monotonic() +_flow_ids = [] -def log(msg): +def log(msg, level=" "): if log_fd >= 0 and "--debug" in opts: with log_lock: - os.write(log_fd, ("0.0001: %s\n" % msg).encode()) + os.write(log_fd, ("%.4f: %s %s\n" % (time.monotonic() - T0, level, msg)).encode()) + + +def ep(a, p): + return "[%s]:%s" % (a, p) + + +def flow_open(kind, ini, src, dst, tgt, tsrc, tdst): + with log_lock: + idx = next(i for i in range(len(_flow_ids) + 1) if i >= len(_flow_ids) or _flow_ids[i] is None) + if idx == len(_flow_ids): + _flow_ids.append(True) + _flow_ids[idx] = True + ends = "%s %s -> %s => %s %s -> %s" % (ini, ep(*src), ep(*dst), tgt, ep(*tsrc), ep(*tdst)) + for line in ( + "Flow %d (NEW): FREE -> NEW" % idx, + "Flow %d (INI): NEW -> INI" % idx, + "Flow %d (INI): %s %s -> %s => ?" % (idx, ini, ep(*src), ep(*dst)), + "Flow %d (TGT): INI -> TGT" % idx, + "Flow %d (TGT): %s" % (idx, ends), + "Flow %d (%s): TGT -> TYPED" % (idx, kind), + "Flow %d (%s): %s" % (idx, kind, ends), + "Flow %d (%s): TYPED -> ACTIVE" % (idx, kind), + "Flow %d (%s): timer expires in 2.000s" % (idx, kind), + ): + log(line) + return (idx, kind, ends) + + +def flow_close(token): + idx, kind, ends = token + log("Flow %d (%s): CLOSED" % (idx, kind)) + log("Flow %d (FREE): ACTIVE -> FREE" % idx) + log("Flow %d (FREE): %s" % (idx, ends)) + with log_lock: + _flow_ids[idx] = None try: @@ -96,12 +139,15 @@ if child == 0: import socket as _s _s.sethostname("pasta-" + os.uname().nodename) - for c in ( + setup = [ ["ip", "link", "set", "lo", "up"], ["ip", "link", "add", "eth0", "type", "dummy"], ["ip", "addr", "add", NS_ADDR + "/24", "dev", "eth0"], ["ip", "link", "set", "eth0", "up"], - ): + ] + if "--dns-forward" in opts: + setup.append(["ip", "addr", "add", opts["--dns-forward"] + "/32", "dev", "lo"]) + for c in setup: if os.spawnvp(os.P_WAIT, c[0], c) != 0: os._exit(1) os.write(ready_w, b"1") @@ -120,7 +166,7 @@ if os.read(ready_r, 1) != b"1": print("fake pasta: namespace setup failed", file=sys.stderr) sys.exit(1) proxy = _nsproxy.NsProxy(child) -log("Template interface: eth0 (IPv4)") +log("Template interface: eth0 (IPv4)", "info: ") import socket # noqa: E402 @@ -142,7 +188,14 @@ for spec in fwd_t: os.kill(child, 9) sys.exit(1) s.listen(16) - _nsproxy.forward(s, lambda gp=gp: proxy.connect(NS_ADDR, gp), log, "-t %d" % hp) + _nsproxy.forward( + s, + lambda gp=gp: proxy.connect(NS_ADDR, gp), + lambda peer, hp=hp, gp=gp, addr=addr: flow_open( + "TCP connection (spliced)", "HOST", peer, (addr, hp), "SPLICE", ("0.0.0.0", 0), (NS_ADDR, gp) + ), + flow_close, + ) for spec in fwd_T: ports = spec pairs = [] @@ -151,7 +204,46 @@ for spec in fwd_T: pairs += zip(_nsproxy.port_range(sports), _nsproxy.port_range(hports or sports)) for sp, hp in pairs: s = proxy.listen("127.0.0.1", sp) - _nsproxy.forward(s, lambda hp=hp: socket.create_connection(("127.0.0.1", hp)), log, "-T %d" % sp) + _nsproxy.forward( + s, + lambda hp=hp: socket.create_connection(("127.0.0.1", hp)), + lambda peer, sp=sp, hp=hp: flow_open( + "TCP connection (spliced)", "SPLICE", peer, ("127.0.0.1", sp), "HOST", ("0.0.0.0", 0), ("127.0.0.1", hp) + ), + flow_close, + ) + + +def dns_answer(query): + """Answer QUERY with A 192.0.2.10 (no records for other types).""" + tid, _, qd = struct.unpack("!HHH", query[:6]) + off = 12 + while query[off]: + off += 1 + query[off] + off += 5 + qtype = struct.unpack("!H", query[off - 4 : off - 2])[0] + question = query[12:off] + if qtype == 1: + rr = b"\xc0\x0c" + struct.pack("!HHIH", 1, 1, 60, 4) + socket.inet_aton("192.0.2.10") + return struct.pack("!HHHHHH", tid, 0x8180, 1, 1, 0, 0) + question + rr + return struct.pack("!HHHHHH", tid, 0x8180, 1, 0, 0, 0) + question + + +def dns_loop(sock, addr): + while True: + query, peer = sock.recvfrom(4096) + token = flow_open("UDP flow", "TAP", peer, (addr, 53), "HOST", ("0.0.0.0", peer[1]), ("127.0.0.53", 53)) + try: + sock.sendto(dns_answer(query), peer) + except (OSError, IndexError, struct.error): + pass + flow_close(token) + + +if "--dns-forward" in opts: + threading.Thread( + target=dns_loop, args=(proxy.udp(opts["--dns-forward"], 53), opts["--dns-forward"]), daemon=True + ).start() os.write(go_w, b"1") os.close(go_w) diff --git a/tests/test_netlog.py b/tests/test_netlog.py new file mode 100644 index 0000000..7519cd0 --- /dev/null +++ b/tests/test_netlog.py @@ -0,0 +1,231 @@ +"""Tests for lib/bwrap_netlog.py (the network log condenser and DNS relay). + + python3 -m unittest discover -s tests -p 'test_*.py' + +tests/data/pasta-debug.log is part of a real pasta 2026_10_02 debug log +(--debug --log-file, with its stderr copy of the setup), addresses of the +host replaced with documentation addresses. +""" + +import io +import os +import re +import socket +import struct +import sys +import threading +import time +import unittest + +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, os.path.join(HERE, "..", "lib")) +import bwrap_netlog as netlog # noqa: E402 + +FIXTURE = os.path.join(HERE, "data", "pasta-debug.log") + + +def condense(lines): + out = [] + c = netlog.Condenser(lambda text, prefix=True: out.append(text)) + for line in lines: + c.line(line) + c.finish() + return out + + +def query(name, qtype=1, tid=0x1234): + body = b"".join(bytes([len(p)]) + p.encode() for p in name.split(".")) + b"\0" + return struct.pack("!HHHHHH", tid, 0x0100, 1, 0, 0, 0) + body + struct.pack("!HH", qtype, 1) + + +def answer(q): + """A CNAME to example.org and A 192.0.2.10, with compressed names.""" + end = 12 + while q[end]: + end += 1 + q[end] + end += 5 + cname = bytes([7]) + b"example" + bytes([3]) + b"org" + b"\0" + rr1 = b"\xc0\x0c" + struct.pack("!HHIH", 5, 1, 60, len(cname)) + cname + rr2 = b"\xc0\x0c" + struct.pack("!HHIH", 1, 1, 60, 4) + socket.inet_aton("192.0.2.10") + return q[:2] + struct.pack("!HHHHH", 0x8180, 1, 2, 0, 0) + q[12:end] + rr1 + rr2 + + +class CondenserTest(unittest.TestCase): + def setUp(self): + with open(FIXTURE) as f: + self.raw = f.read().splitlines() + self.out = condense(self.raw) + + def flows(self, event): + return [line for line in self.out if " %s " % event in line and line.startswith("flow=")] + + def test_much_shorter(self): + self.assertLess(len(self.out), len(self.raw) / 5) + + def test_one_open_and_one_close_per_flow(self): + opens, closes = self.flows("open"), self.flows("close") + self.assertTrue(opens) + self.assertEqual(len(opens), len(closes)) + active = sum(1 for line in self.raw if line.endswith("TYPED -> ACTIVE")) + dns = sum(1 for line in self.raw if re.search(r"\(TGT\):\s+TAP \S+ -> \[169\.254\.1\.1\]:53 ", line)) + self.assertEqual(len(opens), active - dns) + + def test_close_has_duration(self): + for line in self.flows("close"): + self.assertRegex(line, r" t=\d+\.\d{3} secs=\d+\.\d{3}") + + def test_open_has_endpoints(self): + line = next(x for x in self.flows("open") if "dst=140.82.112.21:443" in x) + self.assertIn("proto=tcp dir=out src=198.51.100.7:", line) + + def test_ipv6_in_brackets(self): + self.assertTrue(any("dst=[2606:4700:10::ac42:ad95]:443" in x for x in self.out)) + + def test_setup_and_noise_dropped(self): + text = "\n".join(self.out) + for noise in ("Template interface", "timer expires", "STALLED", "ACK_FROM_TAP_DUE", "hash table"): + self.assertNotIn(noise, text) + + def test_dns_forward_flows_dropped(self): + self.assertFalse(any("169.254.1.1:53" in x for x in self.out)) + + def test_error_kept(self): + self.assertIn('pasta level=error msg="Listening address not allowed for -T 127.0.42.1/3000" t=0.001', self.out) + + def test_header(self): + self.assertEqual(self.out[0], "pasta version=2026_10_02.cba3570 pid=3962723") + + def test_names_from_dns(self): + out = condense(["bwrap-dns name=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21"] + self.raw) + self.assertEqual(out[0], "dns name=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21") + line = next(x for x in out if " open " in x and "dst=140.82.112.21:443" in x) + self.assertTrue(line.endswith("name=github.com t=0.917"), line) + + def test_publish_and_host_port(self): + t = "1.0000: Flow 3 " + out = condense( + [ + t + "(NEW): FREE -> NEW", + t + + "(TGT): HOST [127.0.0.1]:5000 -> [127.65.81.1]:8000 => SPLICE [0.0.0.0]:0 -> [198.51.100.7]:8000", + t + "(TCP connection (spliced)): TGT -> TYPED", + t + "(TCP connection (spliced)): TYPED -> ACTIVE", + "2.5000: Flow 3 (FREE): ACTIVE -> FREE", + ] + ) + self.assertEqual( + out, + [ + "flow=3 open proto=tcp dir=publish src=127.0.0.1:5000 dst=127.65.81.1:8000 via=198.51.100.7:8000 t=1.000", + "flow=3 close proto=tcp dir=publish dst=127.65.81.1:8000 via=198.51.100.7:8000 t=2.500 secs=1.500", + ], + ) + + def test_failed_connection(self): + t = "1.0000: Flow 2 " + out = condense( + [ + t + "(NEW): FREE -> NEW", + t + "(TGT): TAP [198.51.100.7]:5000 -> [192.0.2.1]:443 => HOST [0.0.0.0]:0 -> [192.0.2.1]:443", + t + "(TCP connection): TGT -> TYPED", + "1.5000: Flow 2 (FREE): TYPED -> FREE", + ] + ) + self.assertEqual(out, ["flow=2 fail proto=tcp dir=out dst=192.0.2.1:443 src=198.51.100.7:5000 t=1.500"]) + + def test_open_flows_closed_at_session_end(self): + t = "1.0000: Flow 0 " + out = condense( + [ + t + "(NEW): FREE -> NEW", + t + "(TGT): TAP [198.51.100.7]:5000 -> [192.0.2.1]:443 => HOST [0.0.0.0]:0 -> [192.0.2.1]:443", + t + "(TCP connection): TGT -> TYPED", + t + "(TCP connection): TYPED -> ACTIVE", + "9.0000: Flow 1 (NEW): FREE -> NEW", + "event=end session=x", + ] + ) + self.assertEqual( + out[-2], "flow=0 close proto=tcp dir=out dst=192.0.2.1:443 t=9.000 secs=8.000 reason=session-end" + ) + self.assertEqual(out[-1], "event=end session=x") + + +class DnsParseTest(unittest.TestCase): + def test_query(self): + self.assertEqual(netlog.parse_query(query("WWW.Example.org", 28)), ("www.example.org", "AAAA")) + + def test_answers_with_compression(self): + self.assertEqual( + netlog.parse_answers(answer(query("www.example.org"))), ("NOERROR", ["example.org", "192.0.2.10"]) + ) + + def test_garbage(self): + for bad in (b"", b"\x00" * 11, b"\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\xc0"): + with self.assertRaises((ValueError, struct.error, IndexError)): + netlog.parse_query(bad) + + +class DnsRelayTest(unittest.TestCase): + def setUp(self): + self.up_udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.up_udp.bind(("127.0.0.1", 0)) + port = self.up_udp.getsockname()[1] + self.up_tcp = socket.socket() + self.up_tcp.bind(("127.0.0.1", port)) + self.up_tcp.listen() + threading.Thread(target=self._serve_udp, daemon=True).start() + threading.Thread(target=self._serve_tcp, daemon=True).start() + self.out = io.StringIO() + self.relay = netlog.DnsRelay("127.0.0.1:0", "127.0.0.1:%d" % port, self.out) + self.relay.listen = ("127.0.0.1", 0) + self.relay.bind() + lport = self.relay.udp.getsockname()[1] + self.relay.tcp.close() + self.relay.tcp = socket.socket() + self.relay.tcp.bind(("127.0.0.1", lport)) + self.relay.tcp.listen() + self.addr = ("127.0.0.1", lport) + threading.Thread(target=self.relay.serve, daemon=True).start() + + def _serve_udp(self): + while True: + q, a = self.up_udp.recvfrom(4096) + self.up_udp.sendto(answer(q), a) + + def _serve_tcp(self): + while True: + c, _ = self.up_tcp.accept() + n = struct.unpack("!H", c.recv(2))[0] + r = answer(c.recv(n)) + c.sendall(struct.pack("!H", len(r)) + r) + c.close() + + def wait_lines(self, n): + for _ in range(50): + if len(self.out.getvalue().splitlines()) >= n: + break + time.sleep(0.05) + return self.out.getvalue().splitlines() + + def test_udp(self): + c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + c.settimeout(3) + c.sendto(query("www.example.org"), self.addr) + self.assertEqual(netlog.parse_answers(c.recv(4096))[1], ["example.org", "192.0.2.10"]) + self.assertEqual( + self.wait_lines(1), + ["bwrap-dns name=www.example.org type=A proto=udp rcode=NOERROR answers=example.org,192.0.2.10"], + ) + + def test_tcp(self): + c = socket.create_connection(self.addr, timeout=3) + q = query("www.example.org", 28) + c.sendall(struct.pack("!H", len(q)) + q) + n = struct.unpack("!H", c.recv(2))[0] + self.assertEqual(len(c.recv(n)), n) + self.assertIn("type=AAAA proto=tcp rcode=NOERROR", self.wait_lines(1)[0]) + + +if __name__ == "__main__": + unittest.main() From a73e6f3bbfe13db0edec59d7fd11d1cbcbdd9b51 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 10 Nov 2026 12:41:30 -0500 Subject: [PATCH 23/37] fix(bwrap): log DNS answers before relaying them, split out CNAMEs On a real session the first IPv6 connections were logged without a name: the client connected as soon as it had the answer, before the relay had written its dns line. The relay now logs first. CNAMEs go to cname= so answers= holds addresses only. Assisted-by: opencode:claude-opus-5-5 --- docs/bw-wrappers.md | 5 +++-- lib/bwrap_netlog.py | 18 ++++++++++++++---- tests/test_netlog.py | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 9082ae3..02f0bad 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -433,7 +433,7 @@ change them, and they are the data needed for a later egress allowlist. | --- | --- | | start | `event=start session=ID launcher=... user=... uid=... pid=... backend=... flows=logged\|unavailable publish=ADDR:PORTS host_ports=... dir=...` | | pasta | `session=ID pasta version=... pid=...`, and pasta's warnings and errors (`pasta level=error msg=...`) | -| dns | `session=ID dns name=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21` | +| dns | `session=ID dns name=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21`, plus `cname=...` when the answer went through aliases | | open | `session=ID flow=7 open proto=tcp dir=out src=ADDR:PORT dst=140.82.112.21:443 name=github.com t=21.359` | | close | `session=ID flow=7 close proto=tcp dir=out dst=140.82.112.21:443 name=github.com t=33.770 secs=12.411` | | fail | `session=ID flow=9 fail ...`: a connection that never became active (refused, unreachable) | @@ -500,7 +500,8 @@ field, for example the names an agent reached: - **DNS relay (pasta).** To name destinations, `lib/bwrap_netlog.py dns` listens on `127.53.0.1:53` (UDP and TCP) in the sandbox's network namespace, forwards each lookup to `169.254.1.1` (pasta's DNS forward) and writes one - `dns` line per lookup to the log. The condenser keeps a map from the answers' + `dns` line per lookup to the log, before it answers, so the name is known + when the connection that follows is logged. The condenser keeps a map from the answers' addresses to names. The sandbox's `/etc/resolv.conf` lists the relay first and `169.254.1.1` second, so lookups go on working, at once, if the relay is down (its port is then closed); pasta's own leg of a relayed lookup (UDP to diff --git a/lib/bwrap_netlog.py b/lib/bwrap_netlog.py index d50459c..3221633 100644 --- a/lib/bwrap_netlog.py +++ b/lib/bwrap_netlog.py @@ -24,6 +24,7 @@ session=ID flow=7 close proto=tcp dir=out dst=140.82.113.22:443 name=github.com t=33.770 secs=12.411 session=ID flow=9 fail proto=tcp dir=out dst=192.0.2.1:443 src=... t=40.120 session=ID dns name=github.com type=A rcode=NOERROR answers=140.82.113.22 + session=ID dns name=api.x.com type=A rcode=NOERROR answers=192.0.2.1 cname=lb.x.net """ # Kept to what the DNS relay needs: it starts while the sandbox starts, and @@ -327,12 +328,15 @@ def log(self, query, response, proto): rcode, answers = parse_answers(response) except (ValueError, struct.error, IndexError): rcode, answers = "UNPARSED", [] - line = "bwrap-dns name=%s type=%s proto=%s rcode=%s answers=%s\n" % ( + addrs = [a for a in answers if re.match(r"^[0-9.]+$", a) or ":" in a] + cnames = [a for a in answers if a not in addrs] + line = "bwrap-dns name=%s type=%s proto=%s rcode=%s answers=%s%s\n" % ( _value(name), qtype, proto, rcode, - ",".join(answers) or "-", + ",".join(addrs) or "-", + " cname=%s" % ",".join(cnames) if cnames else "", ) with self.lock: try: @@ -350,10 +354,16 @@ def _udp_one(self, srv, query, client): s.send(query) response = s.recv(65535) s.close() - srv.sendto(response, client) except OSError: pass + # Log before answering: the client connects as soon as it has the + # answer, and the condenser must know the name by then. self.log(query, response, "udp") + if response is not None: + try: + srv.sendto(response, client) + except OSError: + pass def _recv_exact(self, s, n): data = b"" @@ -377,8 +387,8 @@ def _tcp_one(self, conn): up.sendall(hdr + query) rhdr = self._recv_exact(up, 2) response = self._recv_exact(up, struct.unpack("!H", rhdr)[0]) - conn.sendall(rhdr + response) self.log(query, response, "tcp") + conn.sendall(rhdr + response) up.close() except OSError: pass diff --git a/tests/test_netlog.py b/tests/test_netlog.py index 7519cd0..6d707b3 100644 --- a/tests/test_netlog.py +++ b/tests/test_netlog.py @@ -215,7 +215,7 @@ def test_udp(self): self.assertEqual(netlog.parse_answers(c.recv(4096))[1], ["example.org", "192.0.2.10"]) self.assertEqual( self.wait_lines(1), - ["bwrap-dns name=www.example.org type=A proto=udp rcode=NOERROR answers=example.org,192.0.2.10"], + ["bwrap-dns name=www.example.org type=A proto=udp rcode=NOERROR answers=192.0.2.10 cname=example.org"], ) def test_tcp(self): From efb5fad518a6f611bda89b806e72f75b29d979ca Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 10 Nov 2026 12:44:12 -0500 Subject: [PATCH 24/37] refactor(bwrap): call the destination's DNS name domain= in the network log name= did not say what it was. Connection and dns lines now use domain= (the domain the relay looked up), which is also what an allow-list is written in. Assisted-by: opencode:claude-opus-5-5 --- docs/bw-wrappers.md | 20 ++++++++++---------- lib/bwrap_netlog.py | 18 +++++++++--------- tests/test_netlog.py | 8 ++++---- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 02f0bad..fb3a8f6 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -433,15 +433,15 @@ change them, and they are the data needed for a later egress allowlist. | --- | --- | | start | `event=start session=ID launcher=... user=... uid=... pid=... backend=... flows=logged\|unavailable publish=ADDR:PORTS host_ports=... dir=...` | | pasta | `session=ID pasta version=... pid=...`, and pasta's warnings and errors (`pasta level=error msg=...`) | -| dns | `session=ID dns name=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21`, plus `cname=...` when the answer went through aliases | -| open | `session=ID flow=7 open proto=tcp dir=out src=ADDR:PORT dst=140.82.112.21:443 name=github.com t=21.359` | -| close | `session=ID flow=7 close proto=tcp dir=out dst=140.82.112.21:443 name=github.com t=33.770 secs=12.411` | +| dns | `session=ID dns domain=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21`, plus `cname=...` when the answer went through aliases | +| open | `session=ID flow=7 open proto=tcp dir=out src=ADDR:PORT dst=140.82.112.21:443 domain=github.com t=21.359` | +| close | `session=ID flow=7 close proto=tcp dir=out dst=140.82.112.21:443 domain=github.com t=33.770 secs=12.411` | | fail | `session=ID flow=9 fail ...`: a connection that never became active (refused, unreachable) | | end | `event=end session=ID ...`, when the sandbox has exited | Every line is `key=value` (logfmt), so Loki's `| logfmt` can filter on any -field, for example the names an agent reached: -`{syslog_identifier="bwrap-net"} | logfmt | dir="out"` grouped by `name`. +field, for example the domains an agent reached: +`{syslog_identifier="bwrap-net"} | logfmt | dir="out"` grouped by `domain`. - `dir` is `out` (the sandbox to the network), `publish` (a host client to a `--publish` port) or `host-port` (the sandbox to a `--host-port` service). @@ -452,10 +452,10 @@ field, for example the names an agent reached: - `flow` is pasta's flow slot, which it reuses: it identifies a connection only between its open and its close. - pasta logs no byte counts. -- `name` is the name of the last lookup, through the DNS relay, that returned - the destination address. It is a record, not proof: several names can share - one address (CDNs), and a lookup that bypasses the relay (a query sent to - `169.254.1.1` directly, DNS over HTTPS) leaves no name. +- `domain` is the domain of the last lookup, through the DNS relay, that + returned the destination address. It is a record, not proof: several + domains can share one address (CDNs), and a lookup that bypasses the relay + (a query sent to `169.254.1.1` directly, DNS over HTTPS) leaves none. - **Tag** (`SYSLOG_IDENTIFIER`): `bwrap-net`. `journalctl -t bwrap-net` shows all sessions; `journalctl -t bwrap-net -g ID` one. Inside the sandbox @@ -502,7 +502,7 @@ field, for example the names an agent reached: forwards each lookup to `169.254.1.1` (pasta's DNS forward) and writes one `dns` line per lookup to the log, before it answers, so the name is known when the connection that follows is logged. The condenser keeps a map from the answers' - addresses to names. The sandbox's `/etc/resolv.conf` lists the relay first + addresses to domains. The sandbox's `/etc/resolv.conf` lists the relay first and `169.254.1.1` second, so lookups go on working, at once, if the relay is down (its port is then closed); pasta's own leg of a relayed lookup (UDP to `169.254.1.1:53`) is not logged as a connection. diff --git a/lib/bwrap_netlog.py b/lib/bwrap_netlog.py index 3221633..2deed2e 100644 --- a/lib/bwrap_netlog.py +++ b/lib/bwrap_netlog.py @@ -6,7 +6,7 @@ bwrap_netlog.py condense --session ID [--dns-forward ADDR] -- CMD... Read pasta's debug log (and the DNS relay's lines) on standard input and write one line when a connection opens and one when it closes, - with the DNS name of the destination when the relay saw it, to CMD's + with the domain of the destination when the relay saw it, to CMD's standard input (systemd-cat or logger). Everything else pasta writes at debug level (timers, ACK and buffer bookkeeping, state changes, its setup dump) is dropped; its warnings and errors are kept. @@ -20,11 +20,11 @@ Output lines are logfmt (key=value), so Loki's `| logfmt` can filter them: - session=ID flow=7 open proto=tcp dir=out src=... dst=140.82.113.22:443 name=github.com t=21.359 - session=ID flow=7 close proto=tcp dir=out dst=140.82.113.22:443 name=github.com t=33.770 secs=12.411 + session=ID flow=7 open proto=tcp dir=out src=... dst=140.82.113.22:443 domain=github.com t=21.359 + session=ID flow=7 close proto=tcp dir=out dst=140.82.113.22:443 domain=github.com t=33.770 secs=12.411 session=ID flow=9 fail proto=tcp dir=out dst=192.0.2.1:443 src=... t=40.120 - session=ID dns name=github.com type=A rcode=NOERROR answers=140.82.113.22 - session=ID dns name=api.x.com type=A rcode=NOERROR answers=192.0.2.1 cname=lb.x.net + session=ID dns domain=github.com type=A rcode=NOERROR answers=140.82.113.22 + session=ID dns domain=api.x.com type=A rcode=NOERROR answers=192.0.2.1 cname=lb.x.net """ # Kept to what the DNS relay needs: it starts while the sandbox starts, and @@ -110,7 +110,7 @@ def line(self, raw): def _dns(self, fields): kv = dict(re.findall(r'(\w+)=("(?:[^"\\]|\\.)*"|\S*)', fields)) - name = kv.get("name", "").strip('"') + name = kv.get("domain", "").strip('"') for a in kv.get("answers", "").split(","): if a and (re.match(r"^\d+\.\d+\.\d+\.\d+$", a) or ":" in a): self.names[a] = name @@ -159,7 +159,7 @@ def _fields(self, idx, f, event): parts.append("via=%s" % _hostport(*f["tdst"])) name = self.names.get(f["dst"][0]) if name and direction == "out": - parts.append("name=%s" % _value(name)) + parts.append("domain=%s" % _value(name)) return parts def _is_dns_forward(self, f): @@ -330,7 +330,7 @@ def log(self, query, response, proto): rcode, answers = "UNPARSED", [] addrs = [a for a in answers if re.match(r"^[0-9.]+$", a) or ":" in a] cnames = [a for a in answers if a not in addrs] - line = "bwrap-dns name=%s type=%s proto=%s rcode=%s answers=%s%s\n" % ( + line = "bwrap-dns domain=%s type=%s proto=%s rcode=%s answers=%s%s\n" % ( _value(name), qtype, proto, @@ -441,7 +441,7 @@ def dns(argv): try: relay.bind() except OSError as e: - sys.stdout.write("bwrap-dns name=- type=- proto=- rcode=RELAY-FAILED answers=%s\n" % _value(str(e))) + sys.stdout.write("bwrap-dns domain=- type=- proto=- rcode=RELAY-FAILED answers=%s\n" % _value(str(e))) sys.stdout.flush() return _drop_capabilities() diff --git a/tests/test_netlog.py b/tests/test_netlog.py index 6d707b3..99d8981 100644 --- a/tests/test_netlog.py +++ b/tests/test_netlog.py @@ -96,10 +96,10 @@ def test_header(self): self.assertEqual(self.out[0], "pasta version=2026_10_02.cba3570 pid=3962723") def test_names_from_dns(self): - out = condense(["bwrap-dns name=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21"] + self.raw) - self.assertEqual(out[0], "dns name=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21") + out = condense(["bwrap-dns domain=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21"] + self.raw) + self.assertEqual(out[0], "dns domain=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21") line = next(x for x in out if " open " in x and "dst=140.82.112.21:443" in x) - self.assertTrue(line.endswith("name=github.com t=0.917"), line) + self.assertTrue(line.endswith("domain=github.com t=0.917"), line) def test_publish_and_host_port(self): t = "1.0000: Flow 3 " @@ -215,7 +215,7 @@ def test_udp(self): self.assertEqual(netlog.parse_answers(c.recv(4096))[1], ["example.org", "192.0.2.10"]) self.assertEqual( self.wait_lines(1), - ["bwrap-dns name=www.example.org type=A proto=udp rcode=NOERROR answers=192.0.2.10 cname=example.org"], + ["bwrap-dns domain=www.example.org type=A proto=udp rcode=NOERROR answers=192.0.2.10 cname=example.org"], ) def test_tcp(self): From ff205446ca1e9bf1464933c14bb1bc6092fe7c58 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 10 Nov 2026 12:48:30 -0500 Subject: [PATCH 25/37] build(lint): run actionlint from pixi instead of building it from source The upstream actionlint hook is language: golang: prek downloads a Go toolchain and builds it in every fresh environment, which is where sandboxed sessions' connections to go.dev, dl.google.com and proxy.golang.org came from. conda-forge has actionlint 1.7.12, the pinned version, so it now runs from the pixi dev environment like the other linters. Assisted-by: opencode:claude-opus-5-5 --- .pre-commit-config.yaml | 11 +++++++-- pixi.lock | 50 +++++++++++++++++++++++++++++++++++++++++ pixi.toml | 1 + 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a3e75ff..57b11a6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -95,10 +95,17 @@ repos: entry: pixi run --frozen -e dev yamllint types: [yaml] - - repo: https://github.com/rhysd/actionlint - rev: v1.7.12 + # From pixi (conda-forge) rather than the upstream hook, which is + # `language: golang` and builds actionlint from source: a Go toolchain and + # its modules downloaded in every fresh environment. + - repo: local hooks: - id: actionlint + name: actionlint (pixi dev) + language: system + entry: pixi run --frozen -e dev actionlint + types: [yaml] + files: '^\.github/workflows/' - repo: local hooks: diff --git a/pixi.lock b/pixi.lock index 74fb48b..374d1cc 100644 --- a/pixi.lock +++ b/pixi.lock @@ -106,6 +106,7 @@ environments: packages: linux-64: - conda: https://conda.anaconda.org/conda-forge/linux-64/_openmp_mutex-4.5-20_gnu.conda + - conda: https://conda.anaconda.org/conda-forge/linux-64/actionlint-1.7.12-h8bc977c_0.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/ast-serialize-0.5.0-py310hd8a072f_1.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/bzip2-1.0.8-hda65f42_9.conda - conda: https://conda.anaconda.org/conda-forge/linux-64/icu-78.3-h33c6efd_0.conda @@ -158,6 +159,7 @@ environments: - conda: https://conda.anaconda.org/conda-forge/noarch/typing_extensions-4.15.0-pyhcf101f3_0.conda - conda: https://conda.anaconda.org/conda-forge/noarch/tzdata-2025c-hc9c84f9_1.conda - conda: https://conda.anaconda.org/conda-forge/noarch/yamllint-1.38.0-pyhcf101f3_1.conda + - conda: https://conda.anaconda.org/conda-forge/osx-64/actionlint-1.7.12-h5220d24_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-64/ast-serialize-0.5.0-py310hb9b2626_1.conda - conda: https://conda.anaconda.org/conda-forge/osx-64/bzip2-1.0.8-h500dc9f_9.conda - conda: https://conda.anaconda.org/conda-forge/osx-64/libexpat-2.8.1-hcc62823_1.conda @@ -191,6 +193,7 @@ environments: - conda: https://conda.anaconda.org/conda-forge/noarch/typing_extensions-4.15.0-pyhcf101f3_0.conda - conda: https://conda.anaconda.org/conda-forge/noarch/tzdata-2025c-hc9c84f9_1.conda - conda: https://conda.anaconda.org/conda-forge/noarch/yamllint-1.38.0-pyhcf101f3_1.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/actionlint-1.7.12-h7969508_0.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/ast-serialize-0.5.0-py310h3b8a9b8_1.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/bzip2-1.0.8-hd037594_9.conda - conda: https://conda.anaconda.org/conda-forge/osx-arm64/icu-78.3-hef89b57_0.conda @@ -224,6 +227,7 @@ environments: - conda: https://conda.anaconda.org/conda-forge/noarch/typing_extensions-4.15.0-pyhcf101f3_0.conda - conda: https://conda.anaconda.org/conda-forge/noarch/tzdata-2025c-hc9c84f9_1.conda - conda: https://conda.anaconda.org/conda-forge/noarch/yamllint-1.38.0-pyhcf101f3_1.conda + - conda: https://conda.anaconda.org/conda-forge/win-64/actionlint-1.7.12-h11b0a5a_0.conda - conda: https://conda.anaconda.org/conda-forge/win-64/ast-serialize-0.5.0-py310ha413424_1.conda - conda: https://conda.anaconda.org/conda-forge/win-64/bzip2-1.0.8-h0ad9c76_9.conda - conda: https://conda.anaconda.org/conda-forge/win-64/libexpat-2.8.1-hac47afa_1.conda @@ -261,6 +265,18 @@ packages: license_family: BSD size: 28948 timestamp: 1770939786096 +- conda: https://conda.anaconda.org/conda-forge/linux-64/actionlint-1.7.12-h8bc977c_0.conda + sha256: 1fff5ee0d83045ef90104ca83f52b4c44feb4ab2036fc7903fe9733f50721209 + md5: bab393750db08f50eebaf96f50d18734 + depends: + - libgcc >=14 + - __glibc >=2.17 + - __glibc >=2.17,<3.0.a0 + license: MIT + license_family: MIT + run_exports: {} + size: 2131192 + timestamp: 1774975766537 - conda: https://conda.anaconda.org/conda-forge/linux-64/ast-serialize-0.5.0-py310hd8a072f_1.conda noarch: python sha256: cf1cf3d0fa59fe0ab6bc3af722d820c1a85a9233c786f614f377c651fec6a7f9 @@ -712,6 +728,18 @@ packages: license_family: GPL size: 111901 timestamp: 1770937575481 +- conda: https://conda.anaconda.org/conda-forge/osx-64/actionlint-1.7.12-h5220d24_0.conda + sha256: 0a3f0f673f7a1c3dcc5bd04f759b977676e3d680ec50346e9013a0c067426d3c + md5: 7a360d28a2a40006bc138f05b93188d1 + depends: + - __osx >=11.0 + constrains: + - __osx >=10.12 + license: MIT + license_family: MIT + run_exports: {} + size: 2079880 + timestamp: 1774975813961 - conda: https://conda.anaconda.org/conda-forge/osx-64/ast-serialize-0.5.0-py310hb9b2626_1.conda noarch: python sha256: 9d38da0aa9f1034eecf97151a26686f8a6261cbfe27eb486e80e0d86e0169d33 @@ -940,6 +968,16 @@ packages: license_family: MIT size: 6927704 timestamp: 1773569854961 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/actionlint-1.7.12-h7969508_0.conda + sha256: 94a8c69857d5365990f8231a901ea102736694546497cc674962e97b3e8d8d96 + md5: 87084addab359d538cbf8493726cf3f8 + depends: + - __osx >=11.0 + license: MIT + license_family: MIT + run_exports: {} + size: 1855795 + timestamp: 1774975876774 - conda: https://conda.anaconda.org/conda-forge/osx-arm64/ast-serialize-0.5.0-py310h3b8a9b8_1.conda noarch: python sha256: 6c6e47ef2a85e7ade5c94d2388f43bd8197129e6b6305f9e8a49380b7dfbc427 @@ -1180,6 +1218,18 @@ packages: license_family: MIT size: 6443999 timestamp: 1773569857372 +- conda: https://conda.anaconda.org/conda-forge/win-64/actionlint-1.7.12-h11b0a5a_0.conda + sha256: c06ad63dd652546687ec28c131975c183a240cd7b281d14403ef3bb5c6858f76 + md5: 78b9e566476a9df08fa5840ba9e2aeae + depends: + - vc >=14.3,<15 + - vc14_runtime >=14.44.35208 + - ucrt >=10.0.20348.0 + license: MIT + license_family: MIT + run_exports: {} + size: 2152519 + timestamp: 1774975834444 - conda: https://conda.anaconda.org/conda-forge/win-64/ast-serialize-0.5.0-py310ha413424_1.conda noarch: python sha256: 6045fef43d0e5c9ce898dc89da2d4d5f81f5da5c10ca25fcc3592f663dbbec5d diff --git a/pixi.toml b/pixi.toml index 28721c0..672260f 100644 --- a/pixi.toml +++ b/pixi.toml @@ -27,6 +27,7 @@ codespell = "==2.4.2" yamllint = "==1.38.0" zizmor = "==1.23.1" mypy = "*" +actionlint = "==1.7.12" [feature.dev.tasks] lint = "prek run --all-files" From 733cfc127f957bb0574f7b487ce18babf639a098 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 10 Nov 2026 12:50:24 -0500 Subject: [PATCH 26/37] feat(bwrap): mount the prek cache read-write ~/.cache/prek (or $PREK_HOME, passed through) is shared with the host when it exists, like the npm, pixi and ccache caches. A sandbox no longer sets up every hook environment again, and so no longer builds gitleaks from source with a freshly downloaded Go toolchain (go.dev, dl.google.com, proxy.golang.org) each session. As with the other caches, the agent can change what is in it, and prek runs hooks from it on the host. Assisted-by: opencode:claude-opus-5-5 --- bin/bwclaude | 1 + bin/bwcodex | 1 + bin/bwcopilot | 1 + bin/bwopencode | 1 + bin/bwopencode2 | 1 + docs/bw-wrappers.md | 2 +- lib/bwrap_sandbox_lib.sh | 18 +++++++++++++++++- 7 files changed, 23 insertions(+), 2 deletions(-) diff --git a/bin/bwclaude b/bin/bwclaude index 0ead802..c32a7f3 100755 --- a/bin/bwclaude +++ b/bin/bwclaude @@ -669,6 +669,7 @@ main() { build_pixi_mounts build_ccache_mounts build_npm_mounts + build_prek_mounts build_user_bin_mount build_nsls2_mount build_claude_bin_mount diff --git a/bin/bwcodex b/bin/bwcodex index 304b4f2..5e4fdb2 100755 --- a/bin/bwcodex +++ b/bin/bwcodex @@ -683,6 +683,7 @@ main() { build_pixi_mounts build_ccache_mounts build_npm_mounts + build_prek_mounts build_user_bin_mount build_nsls2_mount build_codex_bin_mount diff --git a/bin/bwcopilot b/bin/bwcopilot index c4bda22..ed33c9f 100755 --- a/bin/bwcopilot +++ b/bin/bwcopilot @@ -557,6 +557,7 @@ main() { build_pixi_mounts build_ccache_mounts build_npm_mounts + build_prek_mounts build_user_bin_mount build_nsls2_mount build_dynamic_tool_mounts diff --git a/bin/bwopencode b/bin/bwopencode index f776efe..11e0900 100755 --- a/bin/bwopencode +++ b/bin/bwopencode @@ -702,6 +702,7 @@ main() { build_pixi_mounts build_ccache_mounts build_npm_mounts + build_prek_mounts build_user_bin_mount build_nsls2_mount build_dynamic_tool_mounts diff --git a/bin/bwopencode2 b/bin/bwopencode2 index 0b6d63c..3a012c7 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -1385,6 +1385,7 @@ main() { build_pixi_mounts build_ccache_mounts build_npm_mounts + build_prek_mounts build_user_bin_mount build_nsls2_mount build_opencode2_install_mount diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index fb3a8f6..9ed08d0 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -141,7 +141,7 @@ NSLS-II API in `/run/user//nsls2/creds//token`, for example `--rw-path` refuse it and refuse a directory that contains it. The creds directory itself is allowed. - **Automatic mounts cannot expose it either.** Directories the launcher mounts - whole (the npm and pixi caches, ccache, the tool data and cache directories) + whole (the npm, pixi and prek caches, ccache, the tool data and cache directories) are only checked for being blocked themselves. So, after all mounts are built, the launch stops with an error if any bind mount's source (as written or with symlinks resolved) is, contains or lies inside the N2C session directory, for diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index ae2695d..b70751d 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -1937,9 +1937,13 @@ resolve_common_paths() { # npm paths NPM_CACHE_DIR="${NPM_CONFIG_CACHE:-${_HOME}/.npm}" + # prek (pre-commit) cache: hook environments and the toolchains it + # downloads to build them (Go, Node). + PREK_CACHE_DIR="${PREK_HOME:-${XDG_CACHE_HOME}/prek}" + # ── Safety checks on all user-controllable path roots ──────── # These variables are set from env vars (XDG_*, PIXI_HOME, CCACHE_DIR, - # NPM_CONFIG_CACHE) and control where entire directory trees are mounted + # NPM_CONFIG_CACHE, PREK_HOME) and control where entire directory trees are mounted # into the sandbox. Validate each root once here so that every mount # derived from it is implicitly covered — no per-mount check needed for # sub-paths. Paths that do not yet exist are checked against the blocked @@ -1951,6 +1955,7 @@ resolve_common_paths() { _check_dir_safe "${CCACHE_CACHE_DIR}" "CCACHE_DIR (cache)" _check_dir_safe "${CCACHE_CONFIG_DIR}" "CCACHE_DIR (config)" _check_dir_safe "${NPM_CACHE_DIR}" "NPM_CONFIG_CACHE" + _check_dir_safe "${PREK_CACHE_DIR}" "PREK_HOME" } # ── Sandbox PATH construction ──────────────────────────────────── @@ -4340,6 +4345,16 @@ build_ccache_mounts() { fi } +# ── prek cache (read-write) ───────────────────────────────────── +# Shared with the host, so a sandbox does not set up every hook environment +# (and download the Go and Node toolchains for them) again. Like the npm +# and pixi caches, the agent can change what is in it. +build_prek_mounts() { + if [[ -d "${PREK_CACHE_DIR}" ]]; then + BWRAP_ARGS+=(--bind "${PREK_CACHE_DIR}" "${PREK_CACHE_DIR}") + fi +} + # ── npm config (read-only) + cache (read-write) ───────────────── build_npm_mounts() { if [[ -f "${_HOME}/.npmrc" ]]; then @@ -4666,6 +4681,7 @@ build_env_vars() { pass_through_if_set CCACHE_DIR pass_through_if_set PIXI_HOME pass_through_if_set NPM_CONFIG_CACHE + pass_through_if_set PREK_HOME # Editor pass_through_if_set EDITOR From af19ed697c45dbe91febdfe228bd1fa96784d410 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 10 Nov 2026 14:40:39 -0500 Subject: [PATCH 27/37] fix(bwrap): bound network helpers and test regressions Assisted-by: opencode:gpt-6-sol --- .github/workflows/lint.yml | 3 + AI_DISCLOSURE.md | 3 +- README.md | 1 + bin/bwopencode2 | 9 ++- docs/bw-wrappers.md | 4 ++ lib/bwrap_netlog.py | 127 ++++++++++++++++++++++++++-------- lib/bwrap_sandbox_lib.sh | 27 ++++++-- tests/test_netlog.py | 132 ++++++++++++++++++++++++++++-------- tests/test_serve_timeout.py | 89 ++++++++++++++++++++++++ 9 files changed, 327 insertions(+), 68 deletions(-) create mode 100644 tests/test_serve_timeout.py diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 3abb591..99ee1cd 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -123,6 +123,9 @@ jobs: 2>&1 | tee /tmp/prek-output.txt fi + - name: Run unit tests + run: pixi run -e dev test + - name: Write job summary if: always() env: diff --git a/AI_DISCLOSURE.md b/AI_DISCLOSURE.md index db04999..11dbd8b 100644 --- a/AI_DISCLOSURE.md +++ b/AI_DISCLOSURE.md @@ -8,6 +8,7 @@ models-used: - claude-sonnet-5 - claude-sonnet-5-5 - gpt-5.6-sol + - gpt-6-sol providers: - Anthropic - OpenAI @@ -19,7 +20,7 @@ scope: Shell wrapper scripts (bin/, lib/), the `azoidcapp` Python helper, documentation (docs/), and CI/lint configuration — all with human review and validation. -last-updated: 2026-10-24 +last-updated: 2026-11-10 --- # AI Disclosure diff --git a/README.md b/README.md index 26841fb..0583ec0 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ Add those three exports to your `~/.bashrc` or `~/.zshrc` for persistence. - `bash` >= 4.x (the `bw*` wrappers use `[[ ]]` and arrays) - `bubblewrap` (`bwrap`) >= 0.4.0 for the `bw*` wrappers - binary masking is verified on RHEL 8.10 (`bubblewrap-0.4.0-3.el8_10`) and EL9 (0.6.3) +- GNU `timeout` (coreutils) to bound the `pasta` network-backend probe - `pasta` (package `passt`, RHEL 9+) or `slirp4netns` (RHEL 8 AppStream), with `/dev/net/tun` and unprivileged user namespaces, for the private network namespace of the `bw*` wrappers; without either they stop unless run with diff --git a/bin/bwopencode2 b/bin/bwopencode2 index 3a012c7..3c0a5e2 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -713,9 +713,11 @@ trap 'kill "$srv" 2>/dev/null' INT TERM HUP # Wait up to about 30 s for the server to answer; without credentials /api/info # gives 401, which is enough to know it is listening. ready=0 -for _ in $(seq 60); do +deadline=$((SECONDS + 30)) +while ((SECONDS < deadline)); do kill -0 "$srv" 2> /dev/null || break - code=$(curl -s -o /dev/null -w '%{http_code}' "${base}/api/info") || code=000 + code=$(curl -s --noproxy '*' --connect-timeout 1 --max-time 2 \ + -o /dev/null -w '%{http_code}' "${base}/api/info") || code=000 if [[ $code == 401 || $code == 200 ]]; then ready=1 break @@ -726,7 +728,8 @@ done { if [[ $ready -eq 1 ]]; then resp=$(printf 'user = "opencode:%s"\n' "$OPENCODE_PASSWORD" | - curl -s -K - -X POST "${base}/api/pair") || resp="" + curl -s --noproxy '*' --connect-timeout 1 --max-time 5 \ + -K - -X POST "${base}/api/pair") || resp="" if [[ $resp =~ \"code\":\"([A-Za-z0-9_-]+)\" ]]; then printf '\nbwopencode2: pairing link (works once, expires in 5 minutes):\n' printf ' through the ssh tunnel: http://127.0.0.1:%s/auth/connect/%s\n' "$port" "${BASH_REMATCH[1]}" diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 9ed08d0..3958e85 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -506,6 +506,10 @@ field, for example the domains an agent reached: and `169.254.1.1` second, so lookups go on working, at once, if the relay is down (its port is then closed); pasta's own leg of a relayed lookup (UDP to `169.254.1.1:53`) is not logged as a connection. + - The relay limits itself to 16 workers and 32 queued requests. On overload, + a UDP query gets a logged `SERVFAIL` (allowing the client's next resolver + to answer); a new TCP connection is closed. Each TCP query uses its own + upstream connection, even if the client keeps its connection open. - The descriptor relay starts it before bwrap, as root of pasta's user namespace, which may bind port 53 there; it then drops every capability. It runs outside bwrap: the agent cannot see it (separate process diff --git a/lib/bwrap_netlog.py b/lib/bwrap_netlog.py index 2deed2e..d8d186c 100644 --- a/lib/bwrap_netlog.py +++ b/lib/bwrap_netlog.py @@ -30,6 +30,8 @@ # Kept to what the DNS relay needs: it starts while the sandbox starts, and # lookups made before it listens skip it (see bwrap_sandbox_lib.sh). import ctypes +import ipaddress +from concurrent.futures import ThreadPoolExecutor import os import re import signal @@ -63,6 +65,9 @@ def _hostport(addr, port): def _value(text): """A logfmt value: quoted when it contains a space, = or a quote.""" + # DNS labels are arbitrary bytes, not necessarily valid hostnames. Never + # allow one to turn into a second journal record or a terminal control. + text = "".join("\\x%02x" % ord(c) if ord(c) < 32 or ord(c) == 127 else c for c in text) if text and not re.search(r'[\s="]', text): return text return '"%s"' % text.replace("\\", "\\\\").replace('"', '\\"') @@ -310,11 +315,32 @@ def _addr(spec, port=53): class DnsRelay: - def __init__(self, listen, upstream, out=None): + def __init__(self, listen, upstream, out=None, workers=16, pending=32): self.listen = _addr(listen) self.upstream = _addr(upstream) self.out = out or sys.stdout self.lock = threading.Lock() + self.slots = threading.BoundedSemaphore(workers + pending) + self.pool = ThreadPoolExecutor(max_workers=workers, thread_name_prefix="bwrap-dns") + self.stopping = threading.Event() + + def _submit(self, fn, *args): + if not self.slots.acquire(False): + return False + try: + future = self.pool.submit(fn, *args) + except RuntimeError: + self.slots.release() + return False + future.add_done_callback(lambda _: self.slots.release()) + return True + + def close(self): + self.stopping.set() + for sock in (getattr(self, "udp", None), getattr(self, "tcp", None)): + if sock is not None: + sock.close() + self.pool.shutdown(wait=True) def log(self, query, response, proto): try: @@ -328,15 +354,21 @@ def log(self, query, response, proto): rcode, answers = parse_answers(response) except (ValueError, struct.error, IndexError): rcode, answers = "UNPARSED", [] - addrs = [a for a in answers if re.match(r"^[0-9.]+$", a) or ":" in a] - cnames = [a for a in answers if a not in addrs] + addrs, cnames = [], [] + for a in answers: + try: + ipaddress.ip_address(a) + except ValueError: + cnames.append(a) + else: + addrs.append(a) line = "bwrap-dns domain=%s type=%s proto=%s rcode=%s answers=%s%s\n" % ( _value(name), qtype, proto, rcode, ",".join(addrs) or "-", - " cname=%s" % ",".join(cnames) if cnames else "", + " cname=%s" % _value(",".join(cnames)) if cnames else "", ) with self.lock: try: @@ -348,12 +380,11 @@ def log(self, query, response, proto): def _udp_one(self, srv, query, client): response = None try: - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.settimeout(5) - s.connect(self.upstream) - s.send(query) - response = s.recv(65535) - s.close() + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + s.settimeout(5) + s.connect(self.upstream) + s.send(query) + response = s.recv(65535) except OSError: pass # Log before answering: the client connects as soon as it has the @@ -375,44 +406,80 @@ def _recv_exact(self, s, n): return data def _tcp_one(self, conn): - try: + with conn: conn.settimeout(10) - up = socket.create_connection(self.upstream, timeout=5) while True: try: hdr = self._recv_exact(conn, 2) + query = self._recv_exact(conn, struct.unpack("!H", hdr)[0]) + # DNS servers may close after one response. A fresh + # connection per query also avoids replaying a request + # after an ambiguous upstream failure. + with socket.create_connection(self.upstream, timeout=5) as up: + up.settimeout(10) + up.sendall(hdr + query) + rhdr = self._recv_exact(up, 2) + response = self._recv_exact(up, struct.unpack("!H", rhdr)[0]) + self.log(query, response, "tcp") + conn.sendall(rhdr + response) except OSError: break - query = self._recv_exact(conn, struct.unpack("!H", hdr)[0]) - up.sendall(hdr + query) - rhdr = self._recv_exact(up, 2) - response = self._recv_exact(up, struct.unpack("!H", rhdr)[0]) - self.log(query, response, "tcp") - conn.sendall(rhdr + response) - up.close() - except OSError: + + def _overloaded(self, query, client): + # A quick SERVFAIL gives clients a chance to use their next resolver. + # Only respond when we can safely copy a single complete question. + try: + if len(query) < 12 or struct.unpack("!H", query[4:6])[0] != 1: + return + _, end = _read_name(query, 12) + end += 4 # QTYPE and QCLASS + if end > len(query): + return + flags = struct.unpack("!H", query[2:4])[0] + reply = query[:2] + struct.pack("!HHHHH", 0x8002 | (flags & 0x0100), 1, 0, 0, 0) + query[12:end] + self.log(query, reply, "udp") + self.udp.sendto(reply, client) + except (ValueError, struct.error, OSError, IndexError): pass - finally: - conn.close() def bind(self): self.udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.udp.bind(self.listen) self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - self.tcp.bind(self.listen) + # Port zero is useful for isolated tests; TCP must share UDP's port. + self.tcp.bind(self.udp.getsockname()) self.tcp.listen(16) def serve(self): def tcp_loop(): - while True: - conn, _ = self.tcp.accept() - threading.Thread(target=self._tcp_one, args=(conn,), daemon=True).start() + while not self.stopping.is_set(): + try: + conn, _ = self.tcp.accept() + except socket.timeout: + continue + except OSError: + break + if not self._submit(self._tcp_one, conn): + conn.close() - threading.Thread(target=tcp_loop, daemon=True).start() - while True: - query, client = self.udp.recvfrom(65535) - threading.Thread(target=self._udp_one, args=(self.udp, query, client), daemon=True).start() + self.tcp.settimeout(0.2) + self.udp.settimeout(0.2) + tcp_thread = threading.Thread(target=tcp_loop, daemon=True) + tcp_thread.start() + try: + while not self.stopping.is_set(): + try: + query, client = self.udp.recvfrom(65535) + except socket.timeout: + continue + except OSError: + break + if not self._submit(self._udp_one, self.udp, query, client): + self._overloaded(query, client) + finally: + tcp_thread.join() + self.close() def _drop_capabilities(): diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index b70751d..eba45d6 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -2473,7 +2473,7 @@ detect_net_capabilities() { # assumed to work. _NET_TRY_REASON="" _net_try_pasta() { - local _out + local _out _rc _NET_TRY_REASON="" _PASTA_BIN="$(command -v pasta 2> /dev/null || true)" if [[ -z "${_PASTA_BIN}" || ! -x "${_PASTA_BIN}" ]]; then @@ -2482,8 +2482,14 @@ _net_try_pasta() { fi # --no-pidns (2026-09) is not in RHEL 9 / EPEL builds; without it pasta # adds a PID namespace, inside which bwrap nests its own. - if "${_PASTA_BIN}" --help 2>&1 | grep -q -e '--no-pidns'; then - _PASTA_HAS_NO_PIDNS=1 + if _out="$(timeout --signal=TERM --kill-after=2s 5s "${_PASTA_BIN}" --help 2>&1)"; then + [[ "${_out}" != *--no-pidns* ]] || _PASTA_HAS_NO_PIDNS=1 + else + _rc=$? + if [[ ${_rc} -eq 124 || ${_rc} -eq 137 ]]; then + _NET_TRY_REASON="'pasta --help' timed out after 5 seconds" + return 1 + fi fi _net_baseline_args [[ "${DRY_RUN}" -eq 0 ]] || return 0 @@ -2491,12 +2497,19 @@ _net_try_pasta() { _NET_TRY_REASON="/dev/net/tun is missing on the host" return 1 fi - if ! _out="$("${_PASTA_BIN}" "${PASTA_ARGS[@]}" -- "$(command -v true)" 2>&1)"; then - _out="${_out%%$'\n'*}" - _NET_TRY_REASON="a test run failed${_out:+: ${_out}} (are unprivileged user namespaces disabled?)" + if _out="$(timeout --signal=TERM --kill-after=2s 10s \ + "${_PASTA_BIN}" "${PASTA_ARGS[@]}" -- /usr/bin/true 2>&1)"; then + return 0 + else + _rc=$? + if [[ ${_rc} -eq 124 || ${_rc} -eq 137 ]]; then + _NET_TRY_REASON="the test run timed out after 10 seconds" + else + _out="${_out%%$'\n'*}" + _NET_TRY_REASON="a test run failed${_out:+: ${_out}} (are unprivileged user namespaces disabled?)" + fi return 1 fi - return 0 } # ── Per-user runtime directory ─────────────────────────────────── diff --git a/tests/test_netlog.py b/tests/test_netlog.py index 99d8981..d46b42a 100644 --- a/tests/test_netlog.py +++ b/tests/test_netlog.py @@ -165,41 +165,68 @@ def test_garbage(self): with self.assertRaises((ValueError, struct.error, IndexError)): netlog.parse_query(bad) + def test_logfmt_control_characters(self): + self.assertEqual(netlog._value("bad\nname\r\x1b"), "bad\\x0aname\\x0d\\x1b") + class DnsRelayTest(unittest.TestCase): def setUp(self): + self.stop = threading.Event() self.up_udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.up_udp.bind(("127.0.0.1", 0)) + self.up_udp.settimeout(0.2) port = self.up_udp.getsockname()[1] self.up_tcp = socket.socket() self.up_tcp.bind(("127.0.0.1", port)) self.up_tcp.listen() - threading.Thread(target=self._serve_udp, daemon=True).start() - threading.Thread(target=self._serve_tcp, daemon=True).start() + self.up_tcp.settimeout(0.2) + self.up_udp_thread = threading.Thread(target=self._serve_udp) + self.up_tcp_thread = threading.Thread(target=self._serve_tcp) + self.up_udp_thread.start() + self.up_tcp_thread.start() self.out = io.StringIO() self.relay = netlog.DnsRelay("127.0.0.1:0", "127.0.0.1:%d" % port, self.out) self.relay.listen = ("127.0.0.1", 0) self.relay.bind() - lport = self.relay.udp.getsockname()[1] - self.relay.tcp.close() - self.relay.tcp = socket.socket() - self.relay.tcp.bind(("127.0.0.1", lport)) - self.relay.tcp.listen() - self.addr = ("127.0.0.1", lport) - threading.Thread(target=self.relay.serve, daemon=True).start() + self.addr = self.relay.udp.getsockname() + self.relay_thread = threading.Thread(target=self.relay.serve) + self.relay_thread.start() + + def tearDown(self): + self.stop.set() + self.relay.close() + self.relay_thread.join(timeout=3) + self.up_udp.close() + self.up_tcp.close() + self.up_udp_thread.join(timeout=3) + self.up_tcp_thread.join(timeout=3) def _serve_udp(self): - while True: - q, a = self.up_udp.recvfrom(4096) - self.up_udp.sendto(answer(q), a) + while not self.stop.is_set(): + try: + q, a = self.up_udp.recvfrom(4096) + self.up_udp.sendto(answer(q), a) + except socket.timeout: + continue + except OSError: + break def _serve_tcp(self): - while True: - c, _ = self.up_tcp.accept() - n = struct.unpack("!H", c.recv(2))[0] - r = answer(c.recv(n)) - c.sendall(struct.pack("!H", len(r)) + r) - c.close() + while not self.stop.is_set(): + try: + c, _ = self.up_tcp.accept() + except socket.timeout: + continue + except OSError: + break + with c: + c.settimeout(1) + try: + n = struct.unpack("!H", c.recv(2))[0] + r = answer(c.recv(n)) + c.sendall(struct.pack("!H", len(r)) + r) + except (OSError, struct.error): + pass def wait_lines(self, n): for _ in range(50): @@ -209,22 +236,73 @@ def wait_lines(self, n): return self.out.getvalue().splitlines() def test_udp(self): - c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - c.settimeout(3) - c.sendto(query("www.example.org"), self.addr) - self.assertEqual(netlog.parse_answers(c.recv(4096))[1], ["example.org", "192.0.2.10"]) + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as c: + c.settimeout(3) + c.sendto(query("www.example.org"), self.addr) + self.assertEqual(netlog.parse_answers(c.recv(4096))[1], ["example.org", "192.0.2.10"]) self.assertEqual( self.wait_lines(1), ["bwrap-dns domain=www.example.org type=A proto=udp rcode=NOERROR answers=192.0.2.10 cname=example.org"], ) def test_tcp(self): - c = socket.create_connection(self.addr, timeout=3) - q = query("www.example.org", 28) - c.sendall(struct.pack("!H", len(q)) + q) - n = struct.unpack("!H", c.recv(2))[0] - self.assertEqual(len(c.recv(n)), n) + with socket.create_connection(self.addr, timeout=3) as c: + c.settimeout(3) + for _ in range(2): + q = query("www.example.org", 28) + c.sendall(struct.pack("!H", len(q)) + q) + n = struct.unpack("!H", c.recv(2))[0] + self.assertEqual(len(c.recv(n)), n) self.assertIn("type=AAAA proto=tcp rcode=NOERROR", self.wait_lines(1)[0]) + self.assertEqual(len(self.wait_lines(2)), 2) + + def test_dns_name_does_not_forge_log_lines(self): + q = query("bad\nname.example.org") + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as c: + c.settimeout(3) + c.sendto(q, self.addr) + c.recv(4096) + lines = self.wait_lines(1) + self.assertEqual(len(lines), 1) + self.assertIn("domain=bad\\x0aname.example.org", lines[0]) + + def test_cname_does_not_forge_log_lines(self): + q = query("www.example.org") + r = answer(q).replace(b"\x07example\x03org", b"\x07xx:\nabc\x03org") + self.relay.log(q, r, "udp") + self.assertEqual(len(self.out.getvalue().splitlines()), 1) + self.assertIn("cname=xx:\\x0aabc.org", self.out.getvalue()) + + def test_overload_is_bounded_and_fails_fast(self): + gate = threading.Event() + relay = netlog.DnsRelay("127.0.0.1:0", "127.0.0.1:53", workers=1, pending=1) + before = len([t for t in threading.enumerate() if t.name.startswith("bwrap-dns")]) + try: + self.assertTrue(relay._submit(gate.wait, 2)) + self.assertTrue(relay._submit(gate.wait, 2)) + self.assertFalse(relay._submit(gate.wait, 2)) + after = len([t for t in threading.enumerate() if t.name.startswith("bwrap-dns")]) + self.assertLessEqual(after - before, 1) + finally: + gate.set() + relay.close() + + def test_overload_returns_servfail(self): + gate = threading.Event() + relay = netlog.DnsRelay("127.0.0.1:0", "%s:%d" % self.relay.upstream, io.StringIO(), workers=1, pending=0) + relay.bind() + serve_thread = threading.Thread(target=relay.serve) + serve_thread.start() + try: + self.assertTrue(relay._submit(gate.wait, 2)) + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as c: + c.settimeout(3) + c.sendto(query("www.example.org"), relay.udp.getsockname()) + self.assertEqual(netlog.parse_answers(c.recv(4096))[0], "SERVFAIL") + finally: + gate.set() + relay.close() + serve_thread.join(timeout=3) if __name__ == "__main__": diff --git a/tests/test_serve_timeout.py b/tests/test_serve_timeout.py new file mode 100644 index 0000000..96daa19 --- /dev/null +++ b/tests/test_serve_timeout.py @@ -0,0 +1,89 @@ +"""Exercise bwopencode2 --serve's local pairing request without a real server.""" + +import os +import re +import select +import shutil +import socket +import subprocess +import tempfile +import time +import unittest +from pathlib import Path + + +WRAPPER = Path(__file__).resolve().parent.parent / "bin" / "bwopencode2" + + +class ServeTimeoutTest(unittest.TestCase): + def test_stalled_pairing_request_returns_to_server_wait(self): + if shutil.which("curl") is None: + self.skipTest("curl is not installed") + + source = WRAPPER.read_text() + match = re.search( + r"read -r -d '' SERVE_WRAPPER << 'SERVE_WRAPPER_EOF' \|\| true\n(.*?)\nSERVE_WRAPPER_EOF", source, re.S + ) + self.assertIsNotNone(match) + with tempfile.TemporaryDirectory() as tmp: + server = Path(tmp) / "server.py" + server.write_text( + """#!/usr/bin/env python3 +import http.server +import sys +import threading +import time + +class Handler(http.server.BaseHTTPRequestHandler): + def do_GET(self): + self.send_response(401) + self.end_headers() + + def do_POST(self): + time.sleep(30) # curl, not the server, must end this request + + def log_message(self, *args): + pass + +httpd = http.server.ThreadingHTTPServer(("127.0.0.1", int(sys.argv[-1])), Handler) +httpd.daemon_threads = True +threading.Timer(30, httpd.shutdown).start() +httpd.serve_forever() +""" + ) + server.chmod(0o700) + with socket.socket() as sock: + sock.bind(("127.0.0.1", 0)) + port = sock.getsockname()[1] + proc = subprocess.Popen( + ["bash", "-c", match.group(1), "bwopencode2-serve", str(server), str(port), "127.0.0.1"], + env={"PATH": os.environ.get("PATH", "/usr/bin:/bin"), "OPENCODE_PASSWORD": "test-only-password"}, + stdout=subprocess.DEVNULL, + stderr=subprocess.PIPE, + text=True, + ) + try: + deadline = time.monotonic() + 9 + lines = [] + while time.monotonic() < deadline: + ready, _, _ = select.select([proc.stderr], [], [], max(0, deadline - time.monotonic())) + if not ready: + break + line = proc.stderr.readline() + if not line: + break + lines.append(line) + if "could not get a pairing link" in line: + break + self.assertIn("could not get a pairing link", "".join(lines)) + finally: + proc.terminate() + try: + proc.communicate(timeout=3) + except subprocess.TimeoutExpired: + proc.kill() + proc.communicate(timeout=3) + + +if __name__ == "__main__": + unittest.main() From ee59027d2ffe71db9c14170200fc4e2e094ea4c4 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 12 Nov 2026 12:16:20 -0500 Subject: [PATCH 28/37] fix(bwrap): log pasta flows over stderr, not --log-file, for SELinux On Fedora and RHEL pasta runs as pasta_t, which passt's policy lets only append to user_tmp_t FIFOs. pasta opens --log-file O_RDWR (to rotate it), so the open of the netlog FIFO is denied and pasta exits before the sandbox starts; the launcher returned to the prompt with nothing on the terminal. With --debug and no --log-file pasta writes the same lines to its stderr, and an inherited append-only descriptor is allowed. Open the launcher's stderr on the FIFO with >> instead of <>, holding a temporary read-write descriptor only until the open returns: the kernel replaces any descriptor the new domain may not use with /dev/null at the exec, so a read-write one must not survive into pasta. Drop --log-size, which only mattered for a log file. The stderr format has no header line and no level column, so the condenser's version and level records go away; the pasta version moves into the start record. Fixtures and the fake pasta use the stderr format. Verified with pasta 2026_10_02 on Fedora 45 enforcing. Assisted-by: opencode:claude-fable-5-1 --- docs/bw-wrappers.md | 50 +- lib/bwrap_netlog.py | 31 +- lib/bwrap_sandbox_lib.sh | 63 +- tests/data/pasta-debug.log | 1774 +++++++++++++++++------------------ tests/data/pasta-stderr.log | 81 ++ tests/fakes/pasta-bin/pasta | 53 +- tests/test_netlog.py | 24 +- 7 files changed, 1082 insertions(+), 994 deletions(-) create mode 100644 tests/data/pasta-stderr.log diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 3958e85..640aaec 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -432,7 +432,6 @@ change them, and they are the data needed for a later egress allowlist. | Record | Content | | --- | --- | | start | `event=start session=ID launcher=... user=... uid=... pid=... backend=... flows=logged\|unavailable publish=ADDR:PORTS host_ports=... dir=...` | -| pasta | `session=ID pasta version=... pid=...`, and pasta's warnings and errors (`pasta level=error msg=...`) | | dns | `session=ID dns domain=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21`, plus `cname=...` when the answer went through aliases | | open | `session=ID flow=7 open proto=tcp dir=out src=ADDR:PORT dst=140.82.112.21:443 domain=github.com t=21.359` | | close | `session=ID flow=7 close proto=tcp dir=out dst=140.82.112.21:443 domain=github.com t=33.770 secs=12.411` | @@ -470,33 +469,38 @@ field, for example the domains an agent reached: are written; the start record says `flows=unavailable`. - When the launcher runs on a terminal it prints the tool and the session ID. - **How (pasta).** pasta logs flows only at debug level, and at debug level it - never uses syslog. So pasta gets `--debug --log-file FIFO` (and not - `--quiet`), with the FIFO in the launch's - [session directory](#runtime-directory), and a supervisor started by the - launcher (outside the sandbox) copies each line to the log: - - It holds the FIFO open read-write. pasta closes inherited descriptors - before it opens its log file; a reader that saw end-of-file in that gap - would leave pasta blocked on a full pipe, which stalls the sandbox's - network. It stops when pasta has exited, after draining the pipe, then + never uses syslog: without `--log-file` it writes every line, timestamped, + to its stderr. So pasta gets `--debug` (and not `--quiet`), with its stderr + on a FIFO in the launch's [session directory](#runtime-directory), and a + supervisor started by the launcher (outside the sandbox) copies each line + to the log: + - **Why stderr and not `--log-file`.** pasta opens its log file read-write + (to rotate it). On SELinux hosts (Fedora, RHEL) pasta runs in its own + domain, `pasta_t`, which passt's policy lets only *append* to FIFOs + labelled `user_tmp_t`; the open is denied and pasta exits before the + sandbox starts. An inherited append-only descriptor is allowed. (A + read-write one, or any other descriptor on the FIFO that pasta is not + allowed, is silently replaced with `/dev/null` at the exec into + `pasta_t`, so the launcher opens its stderr with `>>` and keeps no other + descriptor on the FIFO across the exec.) + - The supervisor holds the FIFO open read-write, so it never sees + end-of-file before pasta writes and the launcher's write-only open does + not block. It stops when pasta has exited, after draining the pipe, then writes the end record and removes the session directory. - If the tool dies, it goes on reading and drops the lines, so pasta never blocks on it. - - `--log-size` is set to 1 TiB: pasta rotates its log with `fallocate` or - `lseek`, which fail on a FIFO and would make it drop every later line. - - pasta also prints its setup to stderr until it starts the command. Its - stderr is therefore the FIFO too, so those lines go to the log instead of - your terminal. The descriptor relay (pasta's command) prints a marker there - and reopens your stderr: by path when it is a terminal or a file, else (a - pipe) through a second FIFO that the supervisor copies. If pasta stops - before the marker (a failed start), the supervisor prints pasta's lines on - your terminal, prefixed `: pasta:`, just after the launcher has - exited. + - pasta prints its setup first, and if it fails to start, its error. The + descriptor relay (pasta's command) prints a marker on the FIFO and reopens + your stderr: by path when it is a terminal or a file, else (a pipe) + through a second FIFO that the supervisor copies. If pasta stops before + the marker, the supervisor prints pasta's lines on your terminal, prefixed + `: pasta:`, just after the launcher has exited. - **Condensed.** pasta's debug log has about 34 lines per connection (state - changes, timers, ACK and buffer bookkeeping) and repeats its setup. The + changes, timers, ACK and buffer bookkeeping) and its setup dump. The supervisor sends it through `lib/bwrap_netlog.py condense`, which keeps one - line when a connection opens and one when it closes, plus pasta's warnings - and errors, and drops the rest (in a real session, 5000 lines became - 280). It writes to `systemd-cat` / `logger` itself. + line when a connection opens and one when it closes and drops the rest (in + a real session, 5000 lines became 280). It writes to `systemd-cat` / + `logger` itself. - **DNS relay (pasta).** To name destinations, `lib/bwrap_netlog.py dns` listens on `127.53.0.1:53` (UDP and TCP) in the sandbox's network namespace, forwards each lookup to `169.254.1.1` (pasta's DNS forward) and writes one diff --git a/lib/bwrap_netlog.py b/lib/bwrap_netlog.py index d8d186c..6f84711 100644 --- a/lib/bwrap_netlog.py +++ b/lib/bwrap_netlog.py @@ -4,12 +4,12 @@ platform-python) and later. bwrap_netlog.py condense --session ID [--dns-forward ADDR] -- CMD... - Read pasta's debug log (and the DNS relay's lines) on standard input - and write one line when a connection opens and one when it closes, - with the domain of the destination when the relay saw it, to CMD's - standard input (systemd-cat or logger). Everything else pasta writes - at debug level (timers, ACK and buffer bookkeeping, state changes, - its setup dump) is dropped; its warnings and errors are kept. + Read pasta's debug output (its stderr, and the DNS relay's lines) on + standard input and write one line when a connection opens and one + when it closes, with the domain of the destination when the relay saw + it, to CMD's standard input (systemd-cat or logger). Everything else + pasta writes at debug level (timers, ACK and buffer bookkeeping, state + changes, its setup dump) is dropped. bwrap_netlog.py dns LISTEN UPSTREAM PARENT_PID A DNS relay for the sandbox's network namespace: listen on LISTEN @@ -42,13 +42,15 @@ # ── condense ──────────────────────────────────────────────────────────── +# pasta's debug lines on stderr (log.c vlogmsg): "T: message", T in seconds +# since pasta started. (In its --log-file a level column follows the +# timestamp; \s+ absorbs it, should a log file ever be fed in.) _TS = re.compile(r"^(\d+\.\d+):\s+(.*)$") _FLOW = re.compile(r"^Flow (\d+) \((.*?)\):\s+(.*)$") _ENDPOINTS = re.compile( r"^(\w+) \[([^\]]*)\]:(\d+) -> \[([^\]]*)\]:(\d+) => " r"(?:(\w+) \[([^\]]*)\]:(\d+) -> \[([^\]]*)\]:(\d+)|\?)$" ) -_HEADER = re.compile(r"^pasta (\S+): (\S+) \((\d+)\)$") _DIRS = { ("TAP", "HOST"): "out", ("HOST", "SPLICE"): "publish", @@ -82,7 +84,6 @@ def __init__(self, emit, dns_forward="169.254.1.1"): self.flows = {} self.names = {} self.last_t = 0.0 - self.header_seen = False def line(self, raw): raw = raw.rstrip("\n") @@ -94,22 +95,12 @@ def line(self, raw): if raw.startswith("bwrap-dns "): self._dns(raw[len("bwrap-dns ") :]) return - m = _HEADER.match(raw) - if m and not self.header_seen: - self.header_seen = True - self.emit("pasta version=%s pid=%s" % (m.group(1), m.group(3))) - return m = _TS.match(raw) if not m: - return # pasta's stderr copy of its setup, or noise + return # not a pasta debug line (a message from the relay, noise) t = float(m.group(1)) self.last_t = max(self.last_t, t) - rest = m.group(2) - if rest.startswith(("ERROR:", "WARNING:")): - level, _, msg = rest.partition(":") - self.emit("pasta level=%s msg=%s t=%.3f" % (level.lower(), _value(msg.strip()), t)) - return - m = _FLOW.match(rest) + m = _FLOW.match(m.group(2)) if m: self._flow(int(m.group(1)), m.group(2), m.group(3).strip(), t) diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index eba45d6..f5f9061 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -2607,31 +2607,37 @@ _net_session_dir() { # linux; /dev/log, so journald or rsyslog). The launch writes the start # record itself, synchronously, which also checks that the tool works. # -# pasta at debug level never uses syslog: it writes to --log-file, and until -# it starts the command, to stderr too. So with pasta: +# pasta at debug level never uses syslog: without --log-file it writes every +# line, timestamped, to its stderr (log.c vlogmsg). So with pasta: # # launcher ─┬─ supervisor (_net_log_supervisor, background, outside the sandbox) # │ reads the netlog FIFO line by line and writes each line, # │ prefixed with the session, to the tool; at the end writes # │ the end record and removes the session directory -# └─ exec env -i ... pasta --debug --log-file FIFO ... 2<>FIFO +# └─ exec env -i ... pasta --debug ... 2>>FIFO # -- relay (prints a marker, reopens its stderr) -- bwrap ... # +# - pasta's stderr is the FIFO, opened by the launcher write-only, append. +# pasta is not given --log-file: it opens that read-write (to rotate it), +# and on SELinux hosts (Fedora, RHEL) pasta runs as pasta_t, which passt's +# policy lets only append to user_tmp_t FIFOs, so the open is denied and +# pasta exits. An inherited append-only descriptor is allowed; a read- +# write one (2<>) would be silently replaced with /dev/null at the exec +# into pasta_t, and so would any other read-write descriptor on the FIFO +# left open across the exec: the launcher closes its hold before it. # - The supervisor holds the FIFO open read-write, so it never sees EOF -# early: pasta closes inherited descriptors before it opens the log file, -# and a reader that saw EOF in that gap would leave pasta blocked on a -# full pipe, which stalls the sandbox's network. It stops reading when -# the launcher process (pasta) is gone, after draining what is left. +# early, and so the launcher's write-only open does not block. It stops +# reading when the launcher process (pasta) is gone, after draining what +# is left. pasta keeps descriptors 0-2 across its own sandboxing (it +# closes from 3 up). # - It never blocks pasta: if the tool dies it goes on reading and drops the # lines. -# - --log-size is huge: pasta rotates its log with fallocate/lseek, which -# fail on a FIFO and would make it drop every later line. -# - pasta's stderr goes to the FIFO as well (its startup lines would -# otherwise print on the terminal); the relay prints a marker there and -# then reopens the user's stderr by path (a terminal or a file), or -# through a second FIFO that the supervisor copies to it (a pipe). If -# pasta exits before the marker (a failed start), the supervisor prints -# its lines on the terminal, so its errors are not lost. +# - pasta's startup lines would otherwise print on the terminal; the relay +# prints a marker on the FIFO and then reopens the user's stderr by path +# (a terminal or a file), or through a second FIFO that the supervisor +# copies to it (a pipe). If pasta exits before the marker (a failed +# start), the supervisor prints its lines on the terminal, so its errors +# are not lost. # With slirp4netns or --share-net only the start and end records are written # (slirp4netns has no flow log); the supervisor just waits for the end. @@ -2649,8 +2655,6 @@ _NET_LOG_FLOWS=0 _NET_LOG_FIFO="" _NET_LOG_ERR_FIFO="" _NET_LOG_MARKER="" -# pasta's --log-size: 1 TiB, never reached (see above). -_NET_LOG_PASTA_SIZE=1099511627776 # _net_log_detect # List the usable logging tools, best first (_NET_LOG_TOOLS: systemd-cat @@ -2721,8 +2725,7 @@ _net_log_record() { # _net_log_start # Called by launch_sandbox just before it execs: write the start record # (which checks the tool; without one, warn and log nothing), then with -# pasta add the log options and the stderr hand-over, and start the -# supervisor. +# pasta add --debug and the stderr hand-over, and start the supervisor. _net_log_start() { local _err _launcher="${0##*/}" _t _NET_LOG_TOOL="" @@ -2758,7 +2761,7 @@ _net_log_start() { for _i in "${!PASTA_ARGS[@]}"; do [[ "${PASTA_ARGS[${_i}]}" != --quiet ]] || unset 'PASTA_ARGS[_i]' done - PASTA_ARGS=("${PASTA_ARGS[@]}" --debug --log-file "${_NET_LOG_FIFO}" --log-size "${_NET_LOG_PASTA_SIZE}") + PASTA_ARGS=("${PASTA_ARGS[@]}" --debug) # The relay restores the user's stderr: by path when it is a terminal # or a file, else through a FIFO the supervisor copies. _err="$(readlink "/proc/$$/fd/2" 2> /dev/null || true)" @@ -2803,6 +2806,9 @@ _net_log_supervisor() { fi if [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then + # Read-write, so end-of-file is never seen before pasta writes. This + # descriptor is the supervisor's alone; the launcher's own hold on + # the FIFO is closed before it execs pasta (see launch_sandbox). exec {_fifo}<> "${_NET_LOG_FIFO}" if [[ -n "${_NET_LOG_ERR_FIFO}" ]]; then cat < "${_NET_LOG_ERR_FIFO}" >&2 & @@ -2894,9 +2900,9 @@ _net_log_print_dry_run() { printf 'the launcher writes the start record (session %s) with\n# %s\n' \ "${_NET_LOG_SESSION}" "${_NET_LOG_CMD[*]}" if [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then - printf '# and pasta gets --debug --log-file /netlog --log-size %s (not --quiet)\n' "${_NET_LOG_PASTA_SIZE}" - printf '# with its stderr on that FIFO; a supervisor copies each line to the log and the\n' - printf '# relay first reopens your stderr. The end record is written when pasta exits.\n' + printf '# and pasta gets --debug (not --quiet) with its stderr on /netlog,\n' + printf '# a FIFO; a supervisor copies each line to the log and the relay first reopens\n' + printf '# your stderr. The end record is written when pasta exits.\n' if [[ -n "${_NET_LOG_PY}" ]]; then printf '# The lines are condensed (one per connection opened and closed) by ' @@ -5639,8 +5645,15 @@ launch_sandbox() { [[ "${_NET_BACKEND}" != slirp4netns ]] || _net_slirp_start if [[ -n "${_NET_LOG_FIFO}" ]]; then - # pasta's stderr goes to the network log (see "Network log"). - exec 2<> "${_NET_LOG_FIFO}" + # pasta's stderr goes to the network log (see "Network log"): a + # write-only, append descriptor, the only kind SELinux lets pasta + # keep. The hold makes the open return at once even if the reader + # is not yet there; it must not survive into pasta, so it is closed + # again before the exec. + local _hold + exec {_hold}<> "${_NET_LOG_FIFO}" + exec 2>> "${_NET_LOG_FIFO}" + exec {_hold}<&- fi exec "${_ENV_BIN}" -i \ HOME="${_HOME}" \ diff --git a/tests/data/pasta-debug.log b/tests/data/pasta-debug.log index 825138d..e131d71 100644 --- a/tests/data/pasta-debug.log +++ b/tests/data/pasta-debug.log @@ -1,900 +1,874 @@ -pasta 2026_10_02.cba3570: /usr/bin/passt (3962723) -0.0071: info: Template interface: enp7s0 (IPv4), enp7s0 (IPv6) -Template interface: enp7s0 (IPv4), enp7s0 (IPv6) -0.0071: info: Namespace interface: enp7s0 -Namespace interface: enp7s0 -0.0071: info: MAC: -MAC: -0.0071: info: host: 02:00:00:00:00:01 - host: 02:00:00:00:00:01 -0.0071: info: DHCP: -DHCP: -0.0071: info: assign: 198.51.100.7 - assign: 198.51.100.7 -0.0071: info: mask: 255.255.255.0 - mask: 255.255.255.0 -0.0071: info: router: 198.51.100.1 - router: 198.51.100.1 -0.0071: info: DNS: -DNS: -0.0072: info: 169.254.1.1 - 169.254.1.1 -0.0072: info: NDP/DHCPv6: -NDP/DHCPv6: -0.0072: info: assign: 2001:db8::7 - assign: 2001:db8::7 -0.0072: info: router: fe80::1 - router: fe80::1 -0.0072: info: our link-local: fe80::1 - our link-local: fe80::1 -0.0072: info: Inbound forwarding rules (HOST): -Inbound forwarding rules (HOST): -0.0072: info: Outbound forwarding rules (SPLICE): -Outbound forwarding rules (SPLICE): -0.0161: Sending initial ARP request for guest MAC address -Sending initial ARP request for guest MAC address -0.0162: Sending initial NDP NS request for guest MAC address -Sending initial NDP NS request for guest MAC address -0.0188: Using UDP timeout parameters, timeout: 30, stream_timeout: 120 -Using UDP timeout parameters, timeout: 30, stream_timeout: 120 -0.0188: Using TCP RTO parameters, syn_retries: 6, syn_linear_timeouts: 4, rto_max: 120 -Using TCP RTO parameters, syn_retries: 6, syn_linear_timeouts: 4, rto_max: 120 -0.0203: SO_PEEK_OFF supported -SO_PEEK_OFF supported -0.0203: TCP_INFO tcpi_snd_wnd field supported -TCP_INFO tcpi_snd_wnd field supported -0.0203: TCP_INFO tcpi_bytes_acked field supported -TCP_INFO tcpi_bytes_acked field supported -0.0203: TCP_INFO tcpi_min_rtt field supported -TCP_INFO tcpi_min_rtt field supported -0.0206: ARP announcement for 198.51.100.1 / 02:00:00:00:00:02 -ARP announcement for 198.51.100.1 / 02:00:00:00:00:02 -0.0227: info: NDP: received NS, sending NA -0.6782: Flow 0 (NEW): FREE -> NEW -0.6782: Flow 0 (INI): NEW -> INI -0.6782: Flow 0 (INI): TAP [198.51.100.7]:35358 -> [169.254.1.1]:53 => ? -0.6782: Flow 0 (TGT): INI -> TGT -0.6782: Flow 0 (TGT): TAP [198.51.100.7]:35358 -> [169.254.1.1]:53 => HOST [0.0.0.0]:35358 -> [127.0.0.53]:53 -0.6782: Flow 0 (UDP flow): TGT -> TYPED -0.6782: Flow 0 (UDP flow): TAP [198.51.100.7]:35358 -> [169.254.1.1]:53 => HOST [0.0.0.0]:35358 -> [127.0.0.53]:53 -0.6783: Flow 0 (UDP flow): Side 0 hash table insert: bucket: 260701 -0.6783: Flow 0 (UDP flow): Side 1 hash table insert: bucket: 346642 -0.6783: Flow 0 (UDP flow): TYPED -> ACTIVE -0.6783: Flow 0 (UDP flow): TAP [198.51.100.7]:35358 -> [169.254.1.1]:53 => HOST [127.0.0.1]:35358 -> [127.0.0.53]:53 -0.6842: Flow 1 (NEW): FREE -> NEW -0.6842: Flow 1 (INI): NEW -> INI -0.6842: Flow 1 (INI): TAP [2001:db8::7]:49344 -> [2606:4700:10::ac42:ad95]:443 => ? -0.6842: Flow 1 (TGT): INI -> TGT -0.6842: Flow 1 (TGT): TAP [2001:db8::7]:49344 -> [2606:4700:10::ac42:ad95]:443 => HOST [::]:0 -> [2606:4700:10::ac42:ad95]:443 -0.6842: Flow 1 (TCP connection): TGT -> TYPED -0.6842: Flow 1 (TCP connection): TAP [2001:db8::7]:49344 -> [2606:4700:10::ac42:ad95]:443 => HOST [::]:0 -> [2606:4700:10::ac42:ad95]:443 -0.6842: Flow 1 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -0.6842: Flow 1 (TCP connection): Side 0 hash table insert: bucket: 277245 -0.6842: Flow 1 (TCP connection): TYPED -> ACTIVE -0.6842: Flow 1 (TCP connection): TAP [2001:db8::7]:49344 -> [2606:4700:10::ac42:ad95]:443 => HOST [::]:0 -> [2606:4700:10::ac42:ad95]:443 -0.6842: Flow 2 (NEW): FREE -> NEW -0.6842: Flow 2 (INI): NEW -> INI -0.6842: Flow 2 (INI): TAP [198.51.100.7]:40178 -> [172.66.173.149]:443 => ? -0.6842: Flow 2 (TGT): INI -> TGT -0.6842: Flow 2 (TGT): TAP [198.51.100.7]:40178 -> [172.66.173.149]:443 => HOST [0.0.0.0]:0 -> [172.66.173.149]:443 -0.6842: Flow 2 (TCP connection): TGT -> TYPED -0.6842: Flow 2 (TCP connection): TAP [198.51.100.7]:40178 -> [172.66.173.149]:443 => HOST [0.0.0.0]:0 -> [172.66.173.149]:443 -0.6842: Flow 2 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -0.6842: Flow 2 (TCP connection): Side 0 hash table insert: bucket: 124438 -0.6843: Flow 2 (TCP connection): TYPED -> ACTIVE -0.6843: Flow 2 (TCP connection): TAP [198.51.100.7]:40178 -> [172.66.173.149]:443 => HOST [0.0.0.0]:0 -> [172.66.173.149]:443 -0.6843: Flow 3 (NEW): FREE -> NEW -0.6843: Flow 3 (INI): NEW -> INI -0.6843: Flow 3 (INI): TAP [198.51.100.7]:33998 -> [104.20.32.17]:443 => ? -0.6843: Flow 3 (TGT): INI -> TGT -0.6843: Flow 3 (TGT): TAP [198.51.100.7]:33998 -> [104.20.32.17]:443 => HOST [0.0.0.0]:0 -> [104.20.32.17]:443 -0.6843: Flow 3 (TCP connection): TGT -> TYPED -0.6843: Flow 3 (TCP connection): TAP [198.51.100.7]:33998 -> [104.20.32.17]:443 => HOST [0.0.0.0]:0 -> [104.20.32.17]:443 -0.6843: Flow 3 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -0.6843: Flow 3 (TCP connection): Side 0 hash table insert: bucket: 123425 -0.6843: Flow 3 (TCP connection): TYPED -> ACTIVE -0.6843: Flow 3 (TCP connection): TAP [198.51.100.7]:33998 -> [104.20.32.17]:443 => HOST [0.0.0.0]:0 -> [104.20.32.17]:443 -0.6843: Flow 4 (NEW): FREE -> NEW -0.6843: Flow 4 (INI): NEW -> INI -0.6843: Flow 4 (INI): TAP [2001:db8::7]:45592 -> [2606:4700:10::6814:2011]:443 => ? -0.6843: Flow 4 (TGT): INI -> TGT -0.6843: Flow 4 (TGT): TAP [2001:db8::7]:45592 -> [2606:4700:10::6814:2011]:443 => HOST [::]:0 -> [2606:4700:10::6814:2011]:443 -0.6843: Flow 4 (TCP connection): TGT -> TYPED -0.6843: Flow 4 (TCP connection): TAP [2001:db8::7]:45592 -> [2606:4700:10::6814:2011]:443 => HOST [::]:0 -> [2606:4700:10::6814:2011]:443 -0.6843: Flow 4 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -0.6843: Flow 4 (TCP connection): Side 0 hash table insert: bucket: 256789 -0.6843: Flow 4 (TCP connection): TYPED -> ACTIVE -0.6843: Flow 4 (TCP connection): TAP [2001:db8::7]:45592 -> [2606:4700:10::6814:2011]:443 => HOST [::]:0 -> [2606:4700:10::6814:2011]:443 -0.6885: Flow 3 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -0.6885: Flow 3 (TCP connection): ACK_FROM_TAP_DUE -0.6885: Flow 3 (TCP connection): timer expires in 1.000s -0.6886: Flow 3 (TCP connection): ACK_FROM_TAP_DUE dropped -0.6886: Flow 3 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED -0.6886: Flow 1 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -0.6886: Flow 1 (TCP connection): ACK_FROM_TAP_DUE -0.6886: Flow 1 (TCP connection): timer expires in 1.000s -0.6886: Flow 1 (TCP connection): CLOSED: SYN_RCVD -> CLOSED -0.6886: Flow 1 (TCP connection): Side 0 hash table remove: bucket: 277245 -0.6887: Flow 1 (FREE): ACTIVE -> FREE -0.6887: Flow 1 (FREE): TAP [2001:db8::7]:49344 -> [2606:4700:10::ac42:ad95]:443 => HOST [::]:0 -> [2606:4700:10::ac42:ad95]:443 -0.6887: Flow 3 (TCP connection): ACK_TO_TAP_DUE -0.6887: Flow 2 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -0.6887: Flow 2 (TCP connection): ACK_FROM_TAP_DUE -0.6887: Flow 2 (TCP connection): timer expires in 1.000s -0.6887: Flow 2 (TCP connection): CLOSED: SYN_RCVD -> CLOSED -0.6887: Flow 2 (TCP connection): Side 0 hash table remove: bucket: 124438 -0.6887: Flow 2 (FREE): ACTIVE -> FREE -0.6887: Flow 2 (FREE): TAP [198.51.100.7]:40178 -> [172.66.173.149]:443 => HOST [0.0.0.0]:0 -> [172.66.173.149]:443 -0.6895: Flow 4 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -0.6895: Flow 4 (TCP connection): ACK_FROM_TAP_DUE -0.6895: Flow 4 (TCP connection): timer expires in 1.000s -0.6895: Flow 4 (TCP connection): CLOSED: SYN_RCVD -> CLOSED -0.6895: Flow 4 (TCP connection): Side 0 hash table remove: bucket: 256789 -0.6895: Flow 4 (FREE): ACTIVE -> FREE -0.6895: Flow 4 (FREE): TAP [2001:db8::7]:45592 -> [2606:4700:10::6814:2011]:443 => HOST [::]:0 -> [2606:4700:10::6814:2011]:443 -0.6951: Flow 3 (TCP connection): ACK_TO_TAP_DUE dropped -0.6951: Flow 3 (TCP connection): timer expires in 0.000s -0.7048: Flow 3 (TCP connection): ACK_FROM_TAP_DUE -0.7048: Flow 3 (TCP connection): timer expires in 1.000s -0.7048: Flow 3 (TCP connection): STALLED -0.7048: Flow 3 (TCP connection): ACK_FROM_TAP_DUE dropped -0.7048: Flow 3 (TCP connection): STALLED dropped -0.7054: Flow 3 (TCP connection): ACK_TO_TAP_DUE -0.7504: Flow 3 (TCP connection): ACK_TO_TAP_DUE dropped -0.7504: Flow 3 (TCP connection): timer expires in 0.000s -0.7625: Flow 3 (TCP connection): ACK_FROM_TAP_DUE -0.7625: Flow 3 (TCP connection): timer expires in 1.000s -0.7625: Flow 3 (TCP connection): STALLED -0.8030: Flow 3 (TCP connection): ACK_FROM_TAP_DUE dropped -0.8030: Flow 3 (TCP connection): STALLED dropped -0.9127: Flow 1 (NEW): FREE -> NEW -0.9127: Flow 1 (INI): NEW -> INI -0.9127: Flow 1 (INI): TAP [198.51.100.7]:59425 -> [169.254.1.1]:53 => ? -0.9127: Flow 1 (TGT): INI -> TGT -0.9127: Flow 1 (TGT): TAP [198.51.100.7]:59425 -> [169.254.1.1]:53 => HOST [0.0.0.0]:59425 -> [127.0.0.53]:53 -0.9127: Flow 1 (UDP flow): TGT -> TYPED -0.9127: Flow 1 (UDP flow): TAP [198.51.100.7]:59425 -> [169.254.1.1]:53 => HOST [0.0.0.0]:59425 -> [127.0.0.53]:53 -0.9128: Flow 1 (UDP flow): Side 0 hash table insert: bucket: 263925 -0.9128: Flow 1 (UDP flow): Side 1 hash table insert: bucket: 136662 -0.9128: Flow 1 (UDP flow): TYPED -> ACTIVE -0.9128: Flow 1 (UDP flow): TAP [198.51.100.7]:59425 -> [169.254.1.1]:53 => HOST [127.0.0.1]:59425 -> [127.0.0.53]:53 -0.9170: Flow 2 (NEW): FREE -> NEW -0.9170: Flow 2 (INI): NEW -> INI -0.9170: Flow 2 (INI): TAP [198.51.100.7]:59092 -> [140.82.112.21]:443 => ? -0.9170: Flow 2 (TGT): INI -> TGT -0.9171: Flow 2 (TGT): TAP [198.51.100.7]:59092 -> [140.82.112.21]:443 => HOST [0.0.0.0]:0 -> [140.82.112.21]:443 -0.9171: Flow 2 (TCP connection): TGT -> TYPED -0.9171: Flow 2 (TCP connection): TAP [198.51.100.7]:59092 -> [140.82.112.21]:443 => HOST [0.0.0.0]:0 -> [140.82.112.21]:443 -0.9171: Flow 2 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -0.9171: Flow 2 (TCP connection): Side 0 hash table insert: bucket: 234639 -0.9171: Flow 2 (TCP connection): TYPED -> ACTIVE -0.9171: Flow 2 (TCP connection): TAP [198.51.100.7]:59092 -> [140.82.112.21]:443 => HOST [0.0.0.0]:0 -> [140.82.112.21]:443 -0.9270: Flow 2 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -0.9270: Flow 2 (TCP connection): ACK_FROM_TAP_DUE -0.9270: Flow 2 (TCP connection): timer expires in 1.000s -0.9270: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped -0.9270: Flow 2 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED -0.9272: Flow 2 (TCP connection): ACK_TO_TAP_DUE -0.9401: Flow 2 (TCP connection): ACK_TO_TAP_DUE dropped -0.9401: Flow 2 (TCP connection): timer expires in 0.000s -0.9404: Flow 2 (TCP connection): ACK_FROM_TAP_DUE -0.9404: Flow 2 (TCP connection): timer expires in 1.000s -0.9404: Flow 2 (TCP connection): timer expires in 1.000s -0.9404: Flow 2 (TCP connection): timer expires in 1.000s -0.9404: Flow 2 (TCP connection): STALLED -0.9404: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped -0.9404: Flow 2 (TCP connection): STALLED dropped -0.9480: Flow 2 (TCP connection): ACK_FROM_TAP_DUE -0.9480: Flow 2 (TCP connection): timer expires in 1.000s -0.9480: Flow 2 (TCP connection): STALLED -0.9480: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped -0.9480: Flow 2 (TCP connection): STALLED dropped -0.9487: Flow 2 (TCP connection): ACK_TO_TAP_DUE -0.9584: Flow 2 (TCP connection): ACK_FROM_TAP_DUE -0.9585: Flow 2 (TCP connection): STALLED -0.9617: Flow 2 (TCP connection): ACK_TO_TAP_DUE dropped -0.9617: Flow 2 (TCP connection): timer expires in 1.000s -0.9617: Flow 2 (TCP connection): timer expires in 1.000s -0.9989: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped -0.9990: Flow 2 (TCP connection): STALLED dropped -1.0251: TCP inactivity scan -1.1039: Flow 4 (NEW): FREE -> NEW -1.1039: Flow 4 (INI): NEW -> INI -1.1039: Flow 4 (INI): TAP [198.51.100.7]:42639 -> [169.254.1.1]:53 => ? -1.1039: Flow 4 (TGT): INI -> TGT -1.1039: Flow 4 (TGT): TAP [198.51.100.7]:42639 -> [169.254.1.1]:53 => HOST [0.0.0.0]:42639 -> [127.0.0.53]:53 -1.1039: Flow 4 (UDP flow): TGT -> TYPED -1.1039: Flow 4 (UDP flow): TAP [198.51.100.7]:42639 -> [169.254.1.1]:53 => HOST [0.0.0.0]:42639 -> [127.0.0.53]:53 -1.1039: Flow 4 (UDP flow): Side 0 hash table insert: bucket: 104330 -1.1039: Flow 4 (UDP flow): Side 1 hash table insert: bucket: 4393 -1.1039: Flow 4 (UDP flow): TYPED -> ACTIVE -1.1039: Flow 4 (UDP flow): TAP [198.51.100.7]:42639 -> [169.254.1.1]:53 => HOST [127.0.0.1]:42639 -> [127.0.0.53]:53 -1.1042: Flow 5 (NEW): FREE -> NEW -1.1042: Flow 5 (INI): NEW -> INI -1.1042: Flow 5 (INI): TAP [198.51.100.7]:49988 -> [140.82.112.3]:443 => ? -1.1042: Flow 5 (TGT): INI -> TGT -1.1042: Flow 5 (TGT): TAP [198.51.100.7]:49988 -> [140.82.112.3]:443 => HOST [0.0.0.0]:0 -> [140.82.112.3]:443 -1.1042: Flow 5 (TCP connection): TGT -> TYPED -1.1042: Flow 5 (TCP connection): TAP [198.51.100.7]:49988 -> [140.82.112.3]:443 => HOST [0.0.0.0]:0 -> [140.82.112.3]:443 -1.1042: Flow 5 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -1.1042: Flow 5 (TCP connection): Side 0 hash table insert: bucket: 66400 -1.1043: Flow 5 (TCP connection): TYPED -> ACTIVE -1.1043: Flow 5 (TCP connection): TAP [198.51.100.7]:49988 -> [140.82.112.3]:443 => HOST [0.0.0.0]:0 -> [140.82.112.3]:443 -1.1152: Flow 5 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -1.1152: Flow 5 (TCP connection): ACK_FROM_TAP_DUE -1.1152: Flow 5 (TCP connection): timer expires in 1.000s -1.1152: Flow 5 (TCP connection): ACK_FROM_TAP_DUE dropped -1.1152: Flow 5 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED -1.1160: Flow 5 (TCP connection): ACK_TO_TAP_DUE -1.1261: Flow 2 (TCP connection): ACK_FROM_TAP_DUE -1.1261: Flow 2 (TCP connection): timer expires in 1.000s -1.1261: Flow 2 (TCP connection): STALLED -1.1262: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped -1.1262: Flow 2 (TCP connection): STALLED dropped -1.1288: Flow 5 (TCP connection): ACK_FROM_TAP_DUE -1.1288: Flow 5 (TCP connection): STALLED -1.1288: Flow 5 (TCP connection): ACK_FROM_TAP_DUE dropped -1.1288: Flow 5 (TCP connection): ACK_TO_TAP_DUE dropped -1.1288: Flow 5 (TCP connection): STALLED dropped -1.1290: Flow 5 (TCP connection): ACK_FROM_TAP_DUE -1.1290: Flow 5 (TCP connection): timer expires in 1.000s -1.1290: Flow 5 (TCP connection): STALLED -1.1290: Flow 5 (TCP connection): ACK_FROM_TAP_DUE dropped -1.1290: Flow 5 (TCP connection): STALLED dropped -1.1299: Flow 5 (TCP connection): ACK_TO_TAP_DUE -1.1358: Flow 2 (TCP connection): ACK_FROM_TAP_DUE -1.1358: Flow 2 (TCP connection): timer expires in 1.000s -1.1358: Flow 2 (TCP connection): timer expires in 1.000s -1.1359: Flow 2 (TCP connection): timer expires in 1.000s -1.1359: Flow 2 (TCP connection): timer expires in 1.000s -1.1359: Flow 2 (TCP connection): timer expires in 1.000s -1.1359: Flow 2 (TCP connection): timer expires in 1.000s -1.1359: Flow 2 (TCP connection): timer expires in 1.000s -1.1359: Flow 2 (TCP connection): STALLED -1.1359: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped -1.1359: Flow 2 (TCP connection): STALLED dropped -1.1416: Flow 5 (TCP connection): ACK_FROM_TAP_DUE -1.1417: Flow 5 (TCP connection): STALLED -1.1448: Flow 5 (TCP connection): ACK_TO_TAP_DUE dropped -1.1449: Flow 5 (TCP connection): timer expires in 1.000s -1.1449: Flow 5 (TCP connection): timer expires in 1.000s -1.1504: Flow 2 (TCP connection): ACK_FROM_TAP_DUE -1.1504: Flow 2 (TCP connection): timer expires in 1.000s -1.1505: Flow 2 (TCP connection): STALLED -1.1505: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped -1.1505: Flow 2 (TCP connection): STALLED dropped -1.1820: Flow 5 (TCP connection): ACK_FROM_TAP_DUE dropped -1.1820: Flow 5 (TCP connection): STALLED dropped -1.2174: Flow 5 (TCP connection): ACK_FROM_TAP_DUE -1.2174: Flow 5 (TCP connection): timer expires in 1.000s -1.2175: Flow 5 (TCP connection): STALLED -1.2176: Flow 5 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2176: Flow 5 (TCP connection): STALLED dropped -1.2179: Flow 6 (NEW): FREE -> NEW -1.2179: Flow 6 (INI): NEW -> INI -1.2179: Flow 6 (INI): TAP [198.51.100.7]:57247 -> [169.254.1.1]:53 => ? -1.2179: Flow 6 (TGT): INI -> TGT -1.2179: Flow 6 (TGT): TAP [198.51.100.7]:57247 -> [169.254.1.1]:53 => HOST [0.0.0.0]:57247 -> [127.0.0.53]:53 -1.2179: Flow 6 (UDP flow): TGT -> TYPED -1.2179: Flow 6 (UDP flow): TAP [198.51.100.7]:57247 -> [169.254.1.1]:53 => HOST [0.0.0.0]:57247 -> [127.0.0.53]:53 -1.2180: Flow 6 (UDP flow): Side 0 hash table insert: bucket: 56993 -1.2180: Flow 6 (UDP flow): Side 1 hash table insert: bucket: 194914 -1.2180: Flow 6 (UDP flow): TYPED -> ACTIVE -1.2180: Flow 6 (UDP flow): TAP [198.51.100.7]:57247 -> [169.254.1.1]:53 => HOST [127.0.0.1]:57247 -> [127.0.0.53]:53 -1.2225: Flow 7 (NEW): FREE -> NEW -1.2225: Flow 7 (INI): NEW -> INI -1.2225: Flow 7 (INI): TAP [198.51.100.7]:40696 -> [185.199.109.133]:443 => ? -1.2225: Flow 7 (TGT): INI -> TGT -1.2225: Flow 7 (TGT): TAP [198.51.100.7]:40696 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 -1.2226: Flow 7 (TCP connection): TGT -> TYPED -1.2226: Flow 7 (TCP connection): TAP [198.51.100.7]:40696 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 -1.2226: Flow 7 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -1.2226: Flow 7 (TCP connection): Side 0 hash table insert: bucket: 325936 -1.2226: Flow 7 (TCP connection): TYPED -> ACTIVE -1.2226: Flow 7 (TCP connection): TAP [198.51.100.7]:40696 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 -1.2226: Flow 8 (NEW): FREE -> NEW -1.2226: Flow 8 (INI): NEW -> INI -1.2226: Flow 8 (INI): TAP [198.51.100.7]:35412 -> [185.199.110.133]:443 => ? -1.2226: Flow 8 (TGT): INI -> TGT -1.2226: Flow 8 (TGT): TAP [198.51.100.7]:35412 -> [185.199.110.133]:443 => HOST [0.0.0.0]:0 -> [185.199.110.133]:443 -1.2226: Flow 8 (TCP connection): TGT -> TYPED -1.2226: Flow 8 (TCP connection): TAP [198.51.100.7]:35412 -> [185.199.110.133]:443 => HOST [0.0.0.0]:0 -> [185.199.110.133]:443 -1.2227: Flow 8 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -1.2227: Flow 8 (TCP connection): Side 0 hash table insert: bucket: 296582 -1.2227: Flow 8 (TCP connection): TYPED -> ACTIVE -1.2227: Flow 8 (TCP connection): TAP [198.51.100.7]:35412 -> [185.199.110.133]:443 => HOST [0.0.0.0]:0 -> [185.199.110.133]:443 -1.2227: Flow 9 (NEW): FREE -> NEW -1.2227: Flow 9 (INI): NEW -> INI -1.2227: Flow 9 (INI): TAP [198.51.100.7]:55588 -> [185.199.111.133]:443 => ? -1.2227: Flow 9 (TGT): INI -> TGT -1.2227: Flow 9 (TGT): TAP [198.51.100.7]:55588 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 -1.2227: Flow 9 (TCP connection): TGT -> TYPED -1.2227: Flow 9 (TCP connection): TAP [198.51.100.7]:55588 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 -1.2227: Flow 9 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -1.2227: Flow 9 (TCP connection): Side 0 hash table insert: bucket: 261510 -1.2227: Flow 9 (TCP connection): TYPED -> ACTIVE -1.2227: Flow 9 (TCP connection): TAP [198.51.100.7]:55588 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 -1.2227: Flow 10 (NEW): FREE -> NEW -1.2227: Flow 10 (INI): NEW -> INI -1.2227: Flow 10 (INI): TAP [198.51.100.7]:52618 -> [185.199.108.133]:443 => ? -1.2227: Flow 10 (TGT): INI -> TGT -1.2227: Flow 10 (TGT): TAP [198.51.100.7]:52618 -> [185.199.108.133]:443 => HOST [0.0.0.0]:0 -> [185.199.108.133]:443 -1.2227: Flow 10 (TCP connection): TGT -> TYPED -1.2227: Flow 10 (TCP connection): TAP [198.51.100.7]:52618 -> [185.199.108.133]:443 => HOST [0.0.0.0]:0 -> [185.199.108.133]:443 -1.2227: Flow 10 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -1.2227: Flow 10 (TCP connection): Side 0 hash table insert: bucket: 325642 -1.2227: Flow 10 (TCP connection): TYPED -> ACTIVE -1.2227: Flow 10 (TCP connection): TAP [198.51.100.7]:52618 -> [185.199.108.133]:443 => HOST [0.0.0.0]:0 -> [185.199.108.133]:443 -1.2264: Flow 10 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -1.2264: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2265: Flow 10 (TCP connection): timer expires in 1.000s -1.2265: Flow 7 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -1.2265: Flow 7 (TCP connection): ACK_FROM_TAP_DUE -1.2265: Flow 7 (TCP connection): timer expires in 1.000s -1.2266: Flow 9 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -1.2266: Flow 9 (TCP connection): ACK_FROM_TAP_DUE -1.2266: Flow 9 (TCP connection): timer expires in 1.000s -1.2266: Flow 8 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -1.2266: Flow 8 (TCP connection): ACK_FROM_TAP_DUE -1.2266: Flow 8 (TCP connection): timer expires in 1.000s -1.2266: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2266: Flow 10 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED -1.2267: Flow 7 (TCP connection): CLOSED: SYN_RCVD -> CLOSED -1.2267: Flow 7 (TCP connection): Side 0 hash table remove: bucket: 325936 -1.2267: Flow 9 (TCP connection): CLOSED: SYN_RCVD -> CLOSED -1.2267: Flow 9 (TCP connection): Side 0 hash table remove: bucket: 261510 -1.2267: Flow 8 (TCP connection): CLOSED: SYN_RCVD -> CLOSED -1.2267: Flow 8 (TCP connection): Side 0 hash table remove: bucket: 296582 -1.2268: Flow 7 (FREE): ACTIVE -> FREE -1.2268: Flow 7 (FREE): TAP [198.51.100.7]:40696 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 -1.2268: Flow 8 (FREE): ACTIVE -> FREE -1.2268: Flow 8 (FREE): TAP [198.51.100.7]:35412 -> [185.199.110.133]:443 => HOST [0.0.0.0]:0 -> [185.199.110.133]:443 -1.2268: Flow 9 (FREE): ACTIVE -> FREE -1.2268: Flow 9 (FREE): TAP [198.51.100.7]:55588 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 -1.2270: Flow 10 (TCP connection): ACK_TO_TAP_DUE -1.2448: Flow 10 (TCP connection): ACK_TO_TAP_DUE dropped -1.2448: Flow 10 (TCP connection): timer expires in 0.000s -1.2592: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2593: Flow 10 (TCP connection): timer expires in 1.000s -1.2593: Flow 10 (TCP connection): timer expires in 1.000s -1.2594: Flow 10 (TCP connection): timer expires in 1.000s -1.2594: Flow 10 (TCP connection): STALLED -1.2594: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2594: Flow 10 (TCP connection): STALLED dropped -1.2609: Flow 10 (TCP connection): ACK_TO_TAP_DUE -1.2642: Flow 10 (TCP connection): ACK_TO_TAP_DUE dropped -1.2642: Flow 10 (TCP connection): timer expires in 0.000s -1.2734: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2734: Flow 10 (TCP connection): timer expires in 1.000s -1.2735: Flow 10 (TCP connection): STALLED -1.2741: Flow 10 (TCP connection): STALLED dropped -1.2741: Flow 10 (TCP connection): timer expires in 1.000s -1.2742: Flow 10 (TCP connection): STALLED -1.2742: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2742: Flow 10 (TCP connection): STALLED dropped -1.2751: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2751: Flow 10 (TCP connection): timer expires in 1.000s -1.2751: Flow 10 (TCP connection): STALLED -1.2761: Flow 10 (TCP connection): STALLED dropped -1.2761: Flow 10 (TCP connection): timer expires in 1.000s -1.2761: Flow 10 (TCP connection): STALLED -1.2761: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2761: Flow 10 (TCP connection): STALLED dropped -1.2771: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2771: Flow 10 (TCP connection): timer expires in 1.000s -1.2772: Flow 10 (TCP connection): STALLED -1.2780: Flow 10 (TCP connection): STALLED dropped -1.2780: Flow 10 (TCP connection): timer expires in 1.000s -1.2781: Flow 10 (TCP connection): STALLED -1.2781: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2781: Flow 10 (TCP connection): STALLED dropped -1.2791: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2792: Flow 10 (TCP connection): timer expires in 1.000s -1.2792: Flow 10 (TCP connection): STALLED -1.2798: Flow 10 (TCP connection): STALLED dropped -1.2798: Flow 10 (TCP connection): timer expires in 1.000s -1.2799: Flow 10 (TCP connection): STALLED -1.2799: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2799: Flow 10 (TCP connection): STALLED dropped -1.2808: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2808: Flow 10 (TCP connection): timer expires in 1.000s -1.2808: Flow 10 (TCP connection): STALLED -1.2820: Flow 10 (TCP connection): STALLED dropped -1.2821: Flow 10 (TCP connection): timer expires in 1.000s -1.2821: Flow 10 (TCP connection): STALLED -1.2821: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2821: Flow 10 (TCP connection): STALLED dropped -1.2831: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2832: Flow 10 (TCP connection): timer expires in 1.000s -1.2832: Flow 10 (TCP connection): STALLED -1.2832: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2832: Flow 10 (TCP connection): STALLED dropped -1.2841: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2841: Flow 10 (TCP connection): timer expires in 1.000s -1.2841: Flow 10 (TCP connection): STALLED -1.2847: Flow 10 (TCP connection): STALLED dropped -1.2847: Flow 10 (TCP connection): timer expires in 1.000s -1.2848: Flow 10 (TCP connection): STALLED -1.2849: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2849: Flow 10 (TCP connection): STALLED dropped -1.2853: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2853: Flow 10 (TCP connection): timer expires in 1.000s -1.2853: Flow 10 (TCP connection): STALLED -1.2862: Flow 10 (TCP connection): STALLED dropped -1.2862: Flow 10 (TCP connection): timer expires in 1.000s -1.2863: Flow 10 (TCP connection): STALLED -1.2863: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2863: Flow 10 (TCP connection): STALLED dropped -1.2871: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2871: Flow 10 (TCP connection): timer expires in 1.000s -1.2872: Flow 10 (TCP connection): STALLED -1.2877: Flow 10 (TCP connection): STALLED dropped -1.2877: Flow 10 (TCP connection): timer expires in 1.000s -1.2878: Flow 10 (TCP connection): STALLED -1.2878: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2878: Flow 10 (TCP connection): STALLED dropped -1.2886: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2886: Flow 10 (TCP connection): timer expires in 1.000s -1.2886: Flow 10 (TCP connection): STALLED -1.2890: Flow 10 (TCP connection): STALLED dropped -1.2890: Flow 10 (TCP connection): timer expires in 1.000s -1.2890: Flow 10 (TCP connection): STALLED -1.2890: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2890: Flow 10 (TCP connection): STALLED dropped -1.2893: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2893: Flow 10 (TCP connection): timer expires in 1.000s -1.2893: Flow 10 (TCP connection): STALLED -1.2894: Flow 10 (TCP connection): STALLED dropped -1.2894: Flow 10 (TCP connection): timer expires in 1.000s -1.2895: Flow 10 (TCP connection): STALLED -1.2895: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2895: Flow 10 (TCP connection): STALLED dropped -1.2899: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2899: Flow 10 (TCP connection): timer expires in 1.000s -1.2899: Flow 10 (TCP connection): STALLED -1.2901: Flow 10 (TCP connection): STALLED dropped -1.2901: Flow 10 (TCP connection): timer expires in 1.000s -1.2902: Flow 10 (TCP connection): timer expires in 1.000s -1.2902: Flow 10 (TCP connection): STALLED -1.2902: Flow 10 (TCP connection): timer expires in 1.000s -1.2902: Flow 10 (TCP connection): STALLED dropped -1.2902: Flow 10 (TCP connection): STALLED -1.2904: Flow 10 (TCP connection): STALLED dropped -1.2904: Flow 10 (TCP connection): timer expires in 1.000s -1.2905: Flow 10 (TCP connection): STALLED -1.2905: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2905: Flow 10 (TCP connection): STALLED dropped -1.2907: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2907: Flow 10 (TCP connection): timer expires in 1.000s -1.2907: Flow 10 (TCP connection): timer expires in 1.000s -1.2907: Flow 10 (TCP connection): STALLED -1.2907: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2907: Flow 10 (TCP connection): STALLED dropped -1.2936: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2936: Flow 10 (TCP connection): timer expires in 1.000s -1.2936: Flow 10 (TCP connection): STALLED -1.2938: Flow 10 (TCP connection): STALLED dropped -1.2938: Flow 10 (TCP connection): timer expires in 1.000s -1.2939: Flow 10 (TCP connection): timer expires in 1.000s -1.2939: Flow 10 (TCP connection): timer expires in 1.000s -1.2939: Flow 10 (TCP connection): STALLED -1.2941: Flow 10 (TCP connection): STALLED dropped -1.2942: Flow 10 (TCP connection): timer expires in 1.000s -1.2942: Flow 10 (TCP connection): STALLED -1.2942: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2942: Flow 10 (TCP connection): STALLED dropped -1.2944: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2944: Flow 10 (TCP connection): timer expires in 1.000s -1.2944: Flow 10 (TCP connection): timer expires in 1.000s -1.2944: Flow 10 (TCP connection): STALLED -1.2945: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2945: Flow 10 (TCP connection): STALLED dropped -1.2947: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2947: Flow 10 (TCP connection): timer expires in 1.000s -1.2948: Flow 10 (TCP connection): STALLED -1.2949: Flow 10 (TCP connection): STALLED dropped -1.2949: Flow 10 (TCP connection): timer expires in 1.000s -1.2950: Flow 10 (TCP connection): timer expires in 1.000s -1.2950: Flow 10 (TCP connection): timer expires in 1.000s -1.2950: Flow 10 (TCP connection): STALLED -1.2952: Flow 10 (TCP connection): STALLED dropped -1.2952: Flow 10 (TCP connection): timer expires in 1.000s -1.2953: Flow 10 (TCP connection): STALLED -1.2953: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2953: Flow 10 (TCP connection): STALLED dropped -1.2955: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2955: Flow 10 (TCP connection): timer expires in 1.000s -1.2955: Flow 10 (TCP connection): STALLED -1.2958: Flow 10 (TCP connection): STALLED dropped -1.2958: Flow 10 (TCP connection): timer expires in 1.000s -1.2958: Flow 10 (TCP connection): STALLED -1.2958: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2958: Flow 10 (TCP connection): STALLED dropped -1.2961: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2961: Flow 10 (TCP connection): timer expires in 1.000s -1.2961: Flow 10 (TCP connection): STALLED -1.2963: Flow 10 (TCP connection): STALLED dropped -1.2963: Flow 10 (TCP connection): timer expires in 1.000s -1.2963: Flow 10 (TCP connection): STALLED -1.2964: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2964: Flow 10 (TCP connection): STALLED dropped -1.2966: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2966: Flow 10 (TCP connection): timer expires in 1.000s -1.2967: Flow 10 (TCP connection): STALLED -1.2969: Flow 10 (TCP connection): STALLED dropped -1.2969: Flow 10 (TCP connection): timer expires in 1.000s -1.2969: Flow 10 (TCP connection): STALLED -1.2969: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2969: Flow 10 (TCP connection): STALLED dropped -1.2971: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2971: Flow 10 (TCP connection): timer expires in 1.000s -1.2972: Flow 10 (TCP connection): STALLED -1.2974: Flow 10 (TCP connection): STALLED dropped -1.2974: Flow 10 (TCP connection): timer expires in 1.000s -1.2974: Flow 10 (TCP connection): STALLED -1.2974: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2974: Flow 10 (TCP connection): STALLED dropped -1.2977: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2977: Flow 10 (TCP connection): timer expires in 1.000s -1.2977: Flow 10 (TCP connection): STALLED -1.2979: Flow 10 (TCP connection): STALLED dropped -1.2979: Flow 10 (TCP connection): timer expires in 1.000s -1.2980: Flow 10 (TCP connection): STALLED -1.2980: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2980: Flow 10 (TCP connection): STALLED dropped -1.2983: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2983: Flow 10 (TCP connection): timer expires in 1.000s -1.2983: Flow 10 (TCP connection): STALLED -1.2985: Flow 10 (TCP connection): STALLED dropped -1.2985: Flow 10 (TCP connection): timer expires in 1.000s -1.2985: Flow 10 (TCP connection): STALLED -1.2985: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2985: Flow 10 (TCP connection): STALLED dropped -1.2988: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2988: Flow 10 (TCP connection): timer expires in 1.000s -1.2988: Flow 10 (TCP connection): STALLED -1.2990: Flow 10 (TCP connection): STALLED dropped -1.2990: Flow 10 (TCP connection): timer expires in 1.000s -1.2991: Flow 10 (TCP connection): STALLED -1.2991: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2991: Flow 10 (TCP connection): STALLED dropped -1.2994: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2994: Flow 10 (TCP connection): timer expires in 1.000s -1.2994: Flow 10 (TCP connection): STALLED -1.2996: Flow 10 (TCP connection): STALLED dropped -1.2996: Flow 10 (TCP connection): timer expires in 1.000s -1.2996: Flow 10 (TCP connection): STALLED -1.2996: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.2996: Flow 10 (TCP connection): STALLED dropped -1.2999: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.2999: Flow 10 (TCP connection): timer expires in 1.000s -1.3000: Flow 10 (TCP connection): STALLED -1.3002: Flow 10 (TCP connection): STALLED dropped -1.3002: Flow 10 (TCP connection): timer expires in 1.000s -1.3004: Flow 10 (TCP connection): STALLED -1.3004: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.3004: Flow 10 (TCP connection): STALLED dropped -1.3005: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.3005: Flow 10 (TCP connection): timer expires in 1.000s -1.3006: Flow 10 (TCP connection): timer expires in 1.000s -1.3006: Flow 10 (TCP connection): STALLED -1.3007: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.3007: Flow 10 (TCP connection): STALLED dropped -1.3008: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.3008: Flow 10 (TCP connection): timer expires in 1.000s -1.3008: Flow 10 (TCP connection): STALLED -1.3011: Flow 10 (TCP connection): STALLED dropped -1.3011: Flow 10 (TCP connection): timer expires in 1.000s -1.3012: Flow 10 (TCP connection): timer expires in 1.000s -1.3012: Flow 10 (TCP connection): STALLED -1.3012: Flow 10 (TCP connection): timer expires in 1.000s -1.3012: Flow 10 (TCP connection): STALLED dropped -1.3012: Flow 10 (TCP connection): STALLED -1.3014: Flow 10 (TCP connection): STALLED dropped -1.3014: Flow 10 (TCP connection): timer expires in 1.000s -1.3014: Flow 10 (TCP connection): timer expires in 1.000s -1.3014: Flow 10 (TCP connection): STALLED -1.3014: Flow 10 (TCP connection): timer expires in 1.000s -1.3014: Flow 10 (TCP connection): STALLED dropped -1.3014: Flow 10 (TCP connection): STALLED -1.3017: Flow 10 (TCP connection): STALLED dropped -1.3017: Flow 10 (TCP connection): timer expires in 1.000s -1.3017: Flow 10 (TCP connection): STALLED -1.3017: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.3017: Flow 10 (TCP connection): STALLED dropped -1.3020: Flow 10 (TCP connection): ACK_FROM_TAP_DUE -1.3020: Flow 10 (TCP connection): timer expires in 1.000s -1.3020: Flow 10 (TCP connection): STALLED -1.3219: Flow 7 (NEW): FREE -> NEW -1.3219: Flow 7 (INI): NEW -> INI -1.3219: Flow 7 (INI): TAP [198.51.100.7]:49856 -> [169.254.1.1]:53 => ? -1.3219: Flow 7 (TGT): INI -> TGT -1.3219: Flow 7 (TGT): TAP [198.51.100.7]:49856 -> [169.254.1.1]:53 => HOST [0.0.0.0]:49856 -> [127.0.0.53]:53 -1.3219: Flow 7 (UDP flow): TGT -> TYPED -1.3219: Flow 7 (UDP flow): TAP [198.51.100.7]:49856 -> [169.254.1.1]:53 => HOST [0.0.0.0]:49856 -> [127.0.0.53]:53 -1.3219: Flow 7 (UDP flow): Side 0 hash table insert: bucket: 277953 -1.3219: Flow 7 (UDP flow): Side 1 hash table insert: bucket: 193400 -1.3219: Flow 7 (UDP flow): TYPED -> ACTIVE -1.3219: Flow 7 (UDP flow): TAP [198.51.100.7]:49856 -> [169.254.1.1]:53 => HOST [127.0.0.1]:49856 -> [127.0.0.53]:53 -1.3274: Flow 8 (NEW): FREE -> NEW -1.3274: Flow 8 (INI): NEW -> INI -1.3274: Flow 8 (INI): TAP [2001:db8::7]:38294 -> [2606:50c0:8003::154]:443 => ? -1.3274: Flow 8 (TGT): INI -> TGT -1.3275: Flow 8 (TGT): TAP [2001:db8::7]:38294 -> [2606:50c0:8003::154]:443 => HOST [::]:0 -> [2606:50c0:8003::154]:443 -1.3275: Flow 8 (TCP connection): TGT -> TYPED -1.3275: Flow 8 (TCP connection): TAP [2001:db8::7]:38294 -> [2606:50c0:8003::154]:443 => HOST [::]:0 -> [2606:50c0:8003::154]:443 -1.3275: Flow 8 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -1.3275: Flow 8 (TCP connection): Side 0 hash table insert: bucket: 189691 -1.3275: Flow 8 (TCP connection): TYPED -> ACTIVE -1.3275: Flow 8 (TCP connection): TAP [2001:db8::7]:38294 -> [2606:50c0:8003::154]:443 => HOST [::]:0 -> [2606:50c0:8003::154]:443 -1.3275: Flow 9 (NEW): FREE -> NEW -1.3275: Flow 9 (INI): NEW -> INI -1.3275: Flow 9 (INI): TAP [198.51.100.7]:40716 -> [185.199.109.133]:443 => ? -1.3276: Flow 9 (TGT): INI -> TGT -1.3276: Flow 9 (TGT): TAP [198.51.100.7]:40716 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 -1.3276: Flow 9 (TCP connection): TGT -> TYPED -1.3276: Flow 9 (TCP connection): TAP [198.51.100.7]:40716 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 -1.3276: Flow 9 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -1.3276: Flow 9 (TCP connection): Side 0 hash table insert: bucket: 13717 -1.3276: Flow 9 (TCP connection): TYPED -> ACTIVE -1.3276: Flow 9 (TCP connection): TAP [198.51.100.7]:40716 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 -1.3276: Flow 11 (NEW): FREE -> NEW -1.3276: Flow 11 (INI): NEW -> INI -1.3276: Flow 11 (INI): TAP [198.51.100.7]:55600 -> [185.199.111.133]:443 => ? -1.3276: Flow 11 (TGT): INI -> TGT -1.3276: Flow 11 (TGT): TAP [198.51.100.7]:55600 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 -1.3276: Flow 11 (TCP connection): TGT -> TYPED -1.3276: Flow 11 (TCP connection): TAP [198.51.100.7]:55600 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 -1.3276: Flow 11 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -1.3276: Flow 11 (TCP connection): Side 0 hash table insert: bucket: 280556 -1.3276: Flow 11 (TCP connection): TYPED -> ACTIVE -1.3276: Flow 11 (TCP connection): TAP [198.51.100.7]:55600 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 -1.3276: Flow 12 (NEW): FREE -> NEW -1.3276: Flow 12 (INI): NEW -> INI -1.3276: Flow 12 (INI): TAP [2001:db8::7]:36704 -> [2606:50c0:8001::154]:443 => ? -1.3276: Flow 12 (TGT): INI -> TGT -1.3276: Flow 12 (TGT): TAP [2001:db8::7]:36704 -> [2606:50c0:8001::154]:443 => HOST [::]:0 -> [2606:50c0:8001::154]:443 -1.3276: Flow 12 (TCP connection): TGT -> TYPED -1.3276: Flow 12 (TCP connection): TAP [2001:db8::7]:36704 -> [2606:50c0:8001::154]:443 => HOST [::]:0 -> [2606:50c0:8001::154]:443 -1.3276: Flow 12 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -1.3276: Flow 12 (TCP connection): Side 0 hash table insert: bucket: 206072 -1.3277: Flow 12 (TCP connection): TYPED -> ACTIVE -1.3277: Flow 12 (TCP connection): TAP [2001:db8::7]:36704 -> [2606:50c0:8001::154]:443 => HOST [::]:0 -> [2606:50c0:8001::154]:443 -1.3308: Flow 9 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -1.3308: Flow 9 (TCP connection): ACK_FROM_TAP_DUE -1.3308: Flow 9 (TCP connection): timer expires in 1.000s -1.3308: Flow 8 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -1.3308: Flow 8 (TCP connection): ACK_FROM_TAP_DUE -1.3309: Flow 8 (TCP connection): timer expires in 1.000s -1.3309: Flow 9 (TCP connection): ACK_FROM_TAP_DUE dropped -1.3309: Flow 9 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED -1.3310: Flow 8 (TCP connection): CLOSED: SYN_RCVD -> CLOSED -1.3310: Flow 8 (TCP connection): Side 0 hash table remove: bucket: 189691 -1.3310: Flow 12 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -1.3310: Flow 12 (TCP connection): ACK_FROM_TAP_DUE -1.3310: Flow 12 (TCP connection): timer expires in 1.000s -1.3311: Flow 11 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -1.3311: Flow 11 (TCP connection): ACK_FROM_TAP_DUE -1.3311: Flow 11 (TCP connection): timer expires in 1.000s -1.3313: Flow 8 (FREE): ACTIVE -> FREE -1.3313: Flow 8 (FREE): TAP [2001:db8::7]:38294 -> [2606:50c0:8003::154]:443 => HOST [::]:0 -> [2606:50c0:8003::154]:443 -1.3313: Flow 11 (TCP connection): CLOSED: SYN_RCVD -> CLOSED -1.3313: Flow 11 (TCP connection): Side 0 hash table remove: bucket: 280556 -1.3314: Flow 9 (TCP connection): ACK_TO_TAP_DUE -1.3314: Flow 12 (TCP connection): CLOSED: SYN_RCVD -> CLOSED -1.3314: Flow 12 (TCP connection): Side 0 hash table remove: bucket: 206072 -1.3315: Flow 11 (FREE): ACTIVE -> FREE -1.3315: Flow 11 (FREE): TAP [198.51.100.7]:55600 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 -1.3315: Flow 12 (FREE): ACTIVE -> FREE -1.3315: Flow 12 (FREE): TAP [2001:db8::7]:36704 -> [2606:50c0:8001::154]:443 => HOST [::]:0 -> [2606:50c0:8001::154]:443 -1.3380: Flow 9 (TCP connection): ACK_TO_TAP_DUE dropped -1.3381: Flow 9 (TCP connection): timer expires in 0.000s -1.3398: Flow 9 (TCP connection): ACK_FROM_TAP_DUE -1.3399: Flow 9 (TCP connection): timer expires in 1.000s -1.3400: Flow 9 (TCP connection): STALLED -1.3400: Flow 9 (TCP connection): ACK_FROM_TAP_DUE dropped -1.3400: Flow 9 (TCP connection): STALLED dropped -1.3418: Flow 9 (TCP connection): ACK_TO_TAP_DUE -1.3430: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped -1.3430: Flow 10 (TCP connection): STALLED dropped -1.3451: Flow 9 (TCP connection): ACK_TO_TAP_DUE dropped -1.3451: Flow 9 (TCP connection): timer expires in 0.000s -1.3670: Flow 9 (TCP connection): ACK_FROM_TAP_DUE -1.3670: Flow 9 (TCP connection): timer expires in 1.000s -1.3671: Flow 9 (TCP connection): STALLED -1.4080: Flow 9 (TCP connection): ACK_FROM_TAP_DUE dropped -1.4080: Flow 9 (TCP connection): STALLED dropped -740.9544: Flow 0 (TCP connection (spliced)): FIN_RCVD_0 -740.9545: Flow 0 (TCP connection (spliced)): FIN_SENT_1 -740.9545: Flow 0 (TCP connection (spliced)): RCVLOWAT_ACT_1 -740.9545: Flow 0 (TCP connection (spliced)): CLOSED -740.9546: Flow 0 (FREE): ACTIVE -> FREE -740.9546: Flow 0 (FREE): HOST [127.0.0.1]:51982 -> [127.65.81.1]:24700 => SPLICE [0.0.0.0]:0 -> [198.51.100.7]:24700 -740.9546: Flow 0 (NEW): FREE -> NEW -740.9547: Flow 0 (INI): NEW -> INI -740.9547: Flow 0 (INI): HOST [127.0.0.1]:38982 -> [0.0.0.0]:24700 => ? -740.9547: Flow 0 (TGT): INI -> TGT -740.9547: Flow 0 (TGT): HOST [127.0.0.1]:38982 -> [127.65.81.1]:24700 => SPLICE [0.0.0.0]:0 -> [198.51.100.7]:24700 -740.9547: Flow 0 (TCP connection (spliced)): TGT -> TYPED -740.9547: Flow 0 (TCP connection (spliced)): HOST [127.0.0.1]:38982 -> [127.65.81.1]:24700 => SPLICE [0.0.0.0]:0 -> [198.51.100.7]:24700 -740.9548: Flow 0 (TCP connection (spliced)): SPLICE_CONNECT -740.9548: Flow 0 (TCP connection (spliced)): TYPED -> ACTIVE -740.9549: Flow 0 (TCP connection (spliced)): HOST [127.0.0.1]:38982 -> [127.65.81.1]:24700 => SPLICE [0.0.0.0]:0 -> [198.51.100.7]:24700 -740.9549: Flow 0 (TCP connection (spliced)): SPLICE_ESTABLISHED -7.3984: Flow 8 (NEW): FREE -> NEW -7.3984: Flow 8 (INI): NEW -> INI -7.3984: Flow 8 (INI): TAP [198.51.100.7]:52061 -> [169.254.1.1]:53 => ? -7.3984: Flow 8 (TGT): INI -> TGT -7.3984: Flow 8 (TGT): TAP [198.51.100.7]:52061 -> [169.254.1.1]:53 => HOST [0.0.0.0]:52061 -> [127.0.0.53]:53 -7.3984: Flow 8 (UDP flow): TGT -> TYPED -7.3984: Flow 8 (UDP flow): TAP [198.51.100.7]:52061 -> [169.254.1.1]:53 => HOST [0.0.0.0]:52061 -> [127.0.0.53]:53 -7.3984: Flow 8 (UDP flow): Side 0 hash table insert: bucket: 191745 -7.3984: Flow 8 (UDP flow): Side 1 hash table insert: bucket: 265803 -7.3984: Flow 8 (UDP flow): TYPED -> ACTIVE -7.3984: Flow 8 (UDP flow): TAP [198.51.100.7]:52061 -> [169.254.1.1]:53 => HOST [127.0.0.1]:52061 -> [127.0.0.53]:53 -7.4192: Flow 11 (NEW): FREE -> NEW -7.4192: Flow 11 (INI): NEW -> INI -7.4192: Flow 11 (INI): TAP [198.51.100.7]:55606 -> [44.193.164.157]:443 => ? -7.4192: Flow 11 (TGT): INI -> TGT -7.4192: Flow 11 (TGT): TAP [198.51.100.7]:55606 -> [44.193.164.157]:443 => HOST [0.0.0.0]:0 -> [44.193.164.157]:443 -7.4192: Flow 11 (TCP connection): TGT -> TYPED -7.4192: Flow 11 (TCP connection): TAP [198.51.100.7]:55606 -> [44.193.164.157]:443 => HOST [0.0.0.0]:0 -> [44.193.164.157]:443 -7.4192: Flow 11 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -7.4193: Flow 11 (TCP connection): Side 0 hash table insert: bucket: 371710 -7.4193: Flow 11 (TCP connection): TYPED -> ACTIVE -7.4193: Flow 11 (TCP connection): TAP [198.51.100.7]:55606 -> [44.193.164.157]:443 => HOST [0.0.0.0]:0 -> [44.193.164.157]:443 -7.4193: Flow 12 (NEW): FREE -> NEW -7.4193: Flow 12 (INI): NEW -> INI -7.4193: Flow 12 (INI): TAP [198.51.100.7]:44110 -> [100.50.38.106]:443 => ? -7.4193: Flow 12 (TGT): INI -> TGT -7.4193: Flow 12 (TGT): TAP [198.51.100.7]:44110 -> [100.50.38.106]:443 => HOST [0.0.0.0]:0 -> [100.50.38.106]:443 -7.4193: Flow 12 (TCP connection): TGT -> TYPED -7.4193: Flow 12 (TCP connection): TAP [198.51.100.7]:44110 -> [100.50.38.106]:443 => HOST [0.0.0.0]:0 -> [100.50.38.106]:443 -7.4193: Flow 12 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -7.4193: Flow 12 (TCP connection): Side 0 hash table insert: bucket: 134729 -7.4193: Flow 12 (TCP connection): TYPED -> ACTIVE -7.4193: Flow 12 (TCP connection): TAP [198.51.100.7]:44110 -> [100.50.38.106]:443 => HOST [0.0.0.0]:0 -> [100.50.38.106]:443 -7.4193: Flow 13 (NEW): FREE -> NEW -7.4193: Flow 13 (INI): NEW -> INI -7.4193: Flow 13 (INI): TAP [198.51.100.7]:39966 -> [23.22.18.51]:443 => ? -7.4193: Flow 13 (TGT): INI -> TGT -7.4193: Flow 13 (TGT): TAP [198.51.100.7]:39966 -> [23.22.18.51]:443 => HOST [0.0.0.0]:0 -> [23.22.18.51]:443 -7.4193: Flow 13 (TCP connection): TGT -> TYPED -7.4193: Flow 13 (TCP connection): TAP [198.51.100.7]:39966 -> [23.22.18.51]:443 => HOST [0.0.0.0]:0 -> [23.22.18.51]:443 -7.4193: Flow 13 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -7.4193: Flow 13 (TCP connection): Side 0 hash table insert: bucket: 145688 -7.4193: Flow 13 (TCP connection): TYPED -> ACTIVE -7.4193: Flow 13 (TCP connection): TAP [198.51.100.7]:39966 -> [23.22.18.51]:443 => HOST [0.0.0.0]:0 -> [23.22.18.51]:443 -7.4193: Flow 14 (NEW): FREE -> NEW -7.4193: Flow 14 (INI): NEW -> INI -7.4193: Flow 14 (INI): TAP [198.51.100.7]:46830 -> [100.31.23.131]:443 => ? -7.4193: Flow 14 (TGT): INI -> TGT -7.4193: Flow 14 (TGT): TAP [198.51.100.7]:46830 -> [100.31.23.131]:443 => HOST [0.0.0.0]:0 -> [100.31.23.131]:443 -7.4193: Flow 14 (TCP connection): TGT -> TYPED -7.4193: Flow 14 (TCP connection): TAP [198.51.100.7]:46830 -> [100.31.23.131]:443 => HOST [0.0.0.0]:0 -> [100.31.23.131]:443 -7.4193: Flow 14 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT -7.4193: Flow 14 (TCP connection): Side 0 hash table insert: bucket: 20416 -7.4193: Flow 14 (TCP connection): TYPED -> ACTIVE -7.4193: Flow 14 (TCP connection): TAP [198.51.100.7]:46830 -> [100.31.23.131]:443 => HOST [0.0.0.0]:0 -> [100.31.23.131]:443 -7.4292: Flow 12 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -7.4292: Flow 12 (TCP connection): ACK_FROM_TAP_DUE -7.4292: Flow 12 (TCP connection): timer expires in 1.000s -7.4292: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped -7.4292: Flow 12 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED -7.4293: Flow 11 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -7.4293: Flow 11 (TCP connection): ACK_FROM_TAP_DUE -7.4293: Flow 11 (TCP connection): timer expires in 1.000s -7.4294: Flow 11 (TCP connection): CLOSED: SYN_RCVD -> CLOSED -7.4294: Flow 11 (TCP connection): Side 0 hash table remove: bucket: 371710 -7.4294: Flow 11 (FREE): ACTIVE -> FREE -7.4294: Flow 11 (FREE): TAP [198.51.100.7]:55606 -> [44.193.164.157]:443 => HOST [0.0.0.0]:0 -> [44.193.164.157]:443 -7.4294: Flow 12 (TCP connection): ACK_TO_TAP_DUE -7.4294: Flow 14 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -7.4294: Flow 14 (TCP connection): ACK_FROM_TAP_DUE -7.4295: Flow 14 (TCP connection): timer expires in 1.000s -7.4295: Flow 14 (TCP connection): CLOSED: SYN_RCVD -> CLOSED -7.4295: Flow 14 (TCP connection): Side 0 hash table remove: bucket: 20416 -7.4295: Flow 14 (FREE): ACTIVE -> FREE -7.4295: Flow 14 (FREE): TAP [198.51.100.7]:46830 -> [100.31.23.131]:443 => HOST [0.0.0.0]:0 -> [100.31.23.131]:443 -7.4296: Flow 13 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD -7.4296: Flow 13 (TCP connection): ACK_FROM_TAP_DUE -7.4296: Flow 13 (TCP connection): timer expires in 1.000s -7.4296: Flow 13 (TCP connection): CLOSED: SYN_RCVD -> CLOSED -7.4296: Flow 13 (TCP connection): Side 0 hash table remove: bucket: 145688 -7.4297: Flow 13 (FREE): ACTIVE -> FREE -7.4297: Flow 13 (FREE): TAP [198.51.100.7]:39966 -> [23.22.18.51]:443 => HOST [0.0.0.0]:0 -> [23.22.18.51]:443 -7.4423: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped -7.4423: Flow 12 (TCP connection): timer expires in 0.000s -7.4460: Flow 12 (TCP connection): ACK_FROM_TAP_DUE -7.4460: Flow 12 (TCP connection): timer expires in 1.000s -7.4461: Flow 12 (TCP connection): STALLED -7.4461: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped -7.4461: Flow 12 (TCP connection): STALLED dropped -7.4472: Flow 12 (TCP connection): ACK_FROM_TAP_DUE -7.4472: Flow 12 (TCP connection): timer expires in 1.000s -7.4473: Flow 12 (TCP connection): STALLED -7.4473: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped -7.4473: Flow 12 (TCP connection): STALLED dropped -7.4477: Flow 12 (TCP connection): ACK_TO_TAP_DUE -7.4573: Flow 12 (TCP connection): ACK_FROM_TAP_DUE -7.4573: Flow 12 (TCP connection): STALLED -7.4605: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped -7.4605: Flow 12 (TCP connection): timer expires in 1.000s -7.4605: Flow 12 (TCP connection): timer expires in 1.000s -7.4980: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped -7.4980: Flow 12 (TCP connection): STALLED dropped -7.6732: Flow 12 (TCP connection): ACK_TO_TAP_DUE -7.6925: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped -7.6926: Flow 12 (TCP connection): timer expires in 0.000s -7.6928: Flow 12 (TCP connection): ACK_TO_TAP_DUE -7.6960: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped -7.6960: Flow 12 (TCP connection): timer expires in 0.000s -7.6962: Flow 12 (TCP connection): ACK_TO_TAP_DUE -7.7059: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped -7.7059: Flow 12 (TCP connection): timer expires in 0.000s -7.7063: Flow 12 (TCP connection): ACK_TO_TAP_DUE -7.7127: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped -7.7128: Flow 12 (TCP connection): timer expires in 0.000s -7.7130: Flow 12 (TCP connection): ACK_TO_TAP_DUE -7.7163: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped -7.7163: Flow 12 (TCP connection): timer expires in 0.000s -7.7170: Flow 12 (TCP connection): ACK_TO_TAP_DUE -7.7234: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped -7.7235: Flow 12 (TCP connection): timer expires in 0.000s -746.9581: Flow 0 (TCP connection (spliced)): FIN_RCVD_1 -746.9582: Flow 0 (TCP connection (spliced)): FIN_SENT_0 -9.9313: Flow 12 (TCP connection): ACK_FROM_TAP_DUE -9.9313: Flow 12 (TCP connection): timer expires in 1.000s -9.9314: Flow 12 (TCP connection): STALLED -9.9314: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped -9.9314: Flow 12 (TCP connection): STALLED dropped -10.1216: Flow 12 (TCP connection): ACK_FROM_TAP_DUE -10.1216: Flow 12 (TCP connection): timer expires in 1.000s -10.1217: Flow 12 (TCP connection): STALLED -10.1217: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped -10.1217: Flow 12 (TCP connection): STALLED dropped -11.2100: Flow 12 (TCP connection): ACK_FROM_TAP_DUE -11.2100: Flow 12 (TCP connection): timer expires in 1.000s -11.2101: Flow 12 (TCP connection): STALLED -11.2101: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped -11.2101: Flow 12 (TCP connection): STALLED dropped -11.2139: Flow 12 (TCP connection): ACK_FROM_TAP_DUE -11.2139: Flow 12 (TCP connection): timer expires in 1.000s -11.2139: Flow 12 (TCP connection): STALLED -11.2140: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped -11.2140: Flow 12 (TCP connection): STALLED dropped -11.2181: Flow 12 (TCP connection): ACK_FROM_TAP_DUE -11.2181: Flow 12 (TCP connection): timer expires in 1.000s -11.2182: Flow 12 (TCP connection): STALLED -11.2182: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped -11.2182: Flow 12 (TCP connection): STALLED dropped -11.2184: Flow 12 (TCP connection): ACK_FROM_TAP_DUE -11.2184: Flow 12 (TCP connection): timer expires in 1.000s -11.2184: Flow 12 (TCP connection): STALLED -11.2184: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped -11.2184: Flow 12 (TCP connection): STALLED dropped -11.2484: Flow 11 (NEW): FREE -> NEW -11.2484: Flow 11 (INI): NEW -> INI -11.2484: Flow 11 (INI): TAP [198.51.100.7]:56854 -> [169.254.1.1]:53 => ? -11.2484: Flow 11 (TGT): INI -> TGT -11.2484: Flow 11 (TGT): TAP [198.51.100.7]:56854 -> [169.254.1.1]:53 => HOST [0.0.0.0]:56854 -> [127.0.0.53]:53 -11.2484: Flow 11 (UDP flow): TGT -> TYPED -11.2484: Flow 11 (UDP flow): TAP [198.51.100.7]:56854 -> [169.254.1.1]:53 => HOST [0.0.0.0]:56854 -> [127.0.0.53]:53 -11.2484: Flow 11 (UDP flow): Side 0 hash table insert: bucket: 348561 -11.2484: Flow 11 (UDP flow): Side 1 hash table insert: bucket: 21593 -11.2484: Flow 11 (UDP flow): TYPED -> ACTIVE -11.2484: Flow 11 (UDP flow): TAP [198.51.100.7]:56854 -> [169.254.1.1]:53 => HOST [127.0.0.1]:56854 -> [127.0.0.53]:53 -11.2485: Flow 13 (NEW): FREE -> NEW -11.2485: Flow 13 (INI): NEW -> INI -11.2485: Flow 13 (INI): TAP [198.51.100.7]:49064 -> [169.254.1.1]:53 => ? -11.2485: Flow 13 (TGT): INI -> TGT -11.2485: Flow 13 (TGT): TAP [198.51.100.7]:49064 -> [169.254.1.1]:53 => HOST [0.0.0.0]:49064 -> [127.0.0.53]:53 -11.2485: Flow 13 (UDP flow): TGT -> TYPED -11.2485: Flow 13 (UDP flow): TAP [198.51.100.7]:49064 -> [169.254.1.1]:53 => HOST [0.0.0.0]:49064 -> [127.0.0.53]:53 -11.2485: Flow 13 (UDP flow): Side 0 hash table insert: bucket: 125764 -11.2485: Flow 13 (UDP flow): Side 1 hash table insert: bucket: 213015 -11.2485: Flow 13 (UDP flow): TYPED -> ACTIVE -11.2485: Flow 13 (UDP flow): TAP [198.51.100.7]:49064 -> [169.254.1.1]:53 => HOST [127.0.0.1]:49064 -> [127.0.0.53]:53 -11.2487: Flow 14 (NEW): FREE -> NEW -11.2487: Flow 14 (INI): NEW -> INI -0.0012: ERROR: Listening address not allowed for -T 127.0.42.1/3000 +0.0071: Template interface: enp7s0 (IPv4), enp7s0 (IPv6) +0.0071: Namespace interface: enp7s0 +0.0071: MAC: +0.0071: host: 02:00:00:00:00:01 +0.0071: DHCP: +0.0071: assign: 198.51.100.7 +0.0071: mask: 255.255.255.0 +0.0071: router: 198.51.100.1 +0.0071: DNS: +0.0072: 169.254.1.1 +0.0072: NDP/DHCPv6: +0.0072: assign: 2001:db8::7 +0.0072: router: fe80::1 +0.0072: our link-local: fe80::1 +0.0072: Inbound forwarding rules (HOST): +0.0072: Outbound forwarding rules (SPLICE): +0.0161: Sending initial ARP request for guest MAC address +0.0162: Sending initial NDP NS request for guest MAC address +0.0188: Using UDP timeout parameters, timeout: 30, stream_timeout: 120 +0.0188: Using TCP RTO parameters, syn_retries: 6, syn_linear_timeouts: 4, rto_max: 120 +0.0203: SO_PEEK_OFF supported +0.0203: TCP_INFO tcpi_snd_wnd field supported +0.0203: TCP_INFO tcpi_bytes_acked field supported +0.0203: TCP_INFO tcpi_min_rtt field supported +0.0206: ARP announcement for 198.51.100.1 / 02:00:00:00:00:02 +0.0227: NDP: received NS, sending NA +0.6782: Flow 0 (NEW): FREE -> NEW +0.6782: Flow 0 (INI): NEW -> INI +0.6782: Flow 0 (INI): TAP [198.51.100.7]:35358 -> [169.254.1.1]:53 => ? +0.6782: Flow 0 (TGT): INI -> TGT +0.6782: Flow 0 (TGT): TAP [198.51.100.7]:35358 -> [169.254.1.1]:53 => HOST [0.0.0.0]:35358 -> [127.0.0.53]:53 +0.6782: Flow 0 (UDP flow): TGT -> TYPED +0.6782: Flow 0 (UDP flow): TAP [198.51.100.7]:35358 -> [169.254.1.1]:53 => HOST [0.0.0.0]:35358 -> [127.0.0.53]:53 +0.6783: Flow 0 (UDP flow): Side 0 hash table insert: bucket: 260701 +0.6783: Flow 0 (UDP flow): Side 1 hash table insert: bucket: 346642 +0.6783: Flow 0 (UDP flow): TYPED -> ACTIVE +0.6783: Flow 0 (UDP flow): TAP [198.51.100.7]:35358 -> [169.254.1.1]:53 => HOST [127.0.0.1]:35358 -> [127.0.0.53]:53 +0.6842: Flow 1 (NEW): FREE -> NEW +0.6842: Flow 1 (INI): NEW -> INI +0.6842: Flow 1 (INI): TAP [2001:db8::7]:49344 -> [2606:4700:10::ac42:ad95]:443 => ? +0.6842: Flow 1 (TGT): INI -> TGT +0.6842: Flow 1 (TGT): TAP [2001:db8::7]:49344 -> [2606:4700:10::ac42:ad95]:443 => HOST [::]:0 -> [2606:4700:10::ac42:ad95]:443 +0.6842: Flow 1 (TCP connection): TGT -> TYPED +0.6842: Flow 1 (TCP connection): TAP [2001:db8::7]:49344 -> [2606:4700:10::ac42:ad95]:443 => HOST [::]:0 -> [2606:4700:10::ac42:ad95]:443 +0.6842: Flow 1 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +0.6842: Flow 1 (TCP connection): Side 0 hash table insert: bucket: 277245 +0.6842: Flow 1 (TCP connection): TYPED -> ACTIVE +0.6842: Flow 1 (TCP connection): TAP [2001:db8::7]:49344 -> [2606:4700:10::ac42:ad95]:443 => HOST [::]:0 -> [2606:4700:10::ac42:ad95]:443 +0.6842: Flow 2 (NEW): FREE -> NEW +0.6842: Flow 2 (INI): NEW -> INI +0.6842: Flow 2 (INI): TAP [198.51.100.7]:40178 -> [172.66.173.149]:443 => ? +0.6842: Flow 2 (TGT): INI -> TGT +0.6842: Flow 2 (TGT): TAP [198.51.100.7]:40178 -> [172.66.173.149]:443 => HOST [0.0.0.0]:0 -> [172.66.173.149]:443 +0.6842: Flow 2 (TCP connection): TGT -> TYPED +0.6842: Flow 2 (TCP connection): TAP [198.51.100.7]:40178 -> [172.66.173.149]:443 => HOST [0.0.0.0]:0 -> [172.66.173.149]:443 +0.6842: Flow 2 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +0.6842: Flow 2 (TCP connection): Side 0 hash table insert: bucket: 124438 +0.6843: Flow 2 (TCP connection): TYPED -> ACTIVE +0.6843: Flow 2 (TCP connection): TAP [198.51.100.7]:40178 -> [172.66.173.149]:443 => HOST [0.0.0.0]:0 -> [172.66.173.149]:443 +0.6843: Flow 3 (NEW): FREE -> NEW +0.6843: Flow 3 (INI): NEW -> INI +0.6843: Flow 3 (INI): TAP [198.51.100.7]:33998 -> [104.20.32.17]:443 => ? +0.6843: Flow 3 (TGT): INI -> TGT +0.6843: Flow 3 (TGT): TAP [198.51.100.7]:33998 -> [104.20.32.17]:443 => HOST [0.0.0.0]:0 -> [104.20.32.17]:443 +0.6843: Flow 3 (TCP connection): TGT -> TYPED +0.6843: Flow 3 (TCP connection): TAP [198.51.100.7]:33998 -> [104.20.32.17]:443 => HOST [0.0.0.0]:0 -> [104.20.32.17]:443 +0.6843: Flow 3 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +0.6843: Flow 3 (TCP connection): Side 0 hash table insert: bucket: 123425 +0.6843: Flow 3 (TCP connection): TYPED -> ACTIVE +0.6843: Flow 3 (TCP connection): TAP [198.51.100.7]:33998 -> [104.20.32.17]:443 => HOST [0.0.0.0]:0 -> [104.20.32.17]:443 +0.6843: Flow 4 (NEW): FREE -> NEW +0.6843: Flow 4 (INI): NEW -> INI +0.6843: Flow 4 (INI): TAP [2001:db8::7]:45592 -> [2606:4700:10::6814:2011]:443 => ? +0.6843: Flow 4 (TGT): INI -> TGT +0.6843: Flow 4 (TGT): TAP [2001:db8::7]:45592 -> [2606:4700:10::6814:2011]:443 => HOST [::]:0 -> [2606:4700:10::6814:2011]:443 +0.6843: Flow 4 (TCP connection): TGT -> TYPED +0.6843: Flow 4 (TCP connection): TAP [2001:db8::7]:45592 -> [2606:4700:10::6814:2011]:443 => HOST [::]:0 -> [2606:4700:10::6814:2011]:443 +0.6843: Flow 4 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +0.6843: Flow 4 (TCP connection): Side 0 hash table insert: bucket: 256789 +0.6843: Flow 4 (TCP connection): TYPED -> ACTIVE +0.6843: Flow 4 (TCP connection): TAP [2001:db8::7]:45592 -> [2606:4700:10::6814:2011]:443 => HOST [::]:0 -> [2606:4700:10::6814:2011]:443 +0.6885: Flow 3 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +0.6885: Flow 3 (TCP connection): ACK_FROM_TAP_DUE +0.6885: Flow 3 (TCP connection): timer expires in 1.000s +0.6886: Flow 3 (TCP connection): ACK_FROM_TAP_DUE dropped +0.6886: Flow 3 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED +0.6886: Flow 1 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +0.6886: Flow 1 (TCP connection): ACK_FROM_TAP_DUE +0.6886: Flow 1 (TCP connection): timer expires in 1.000s +0.6886: Flow 1 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +0.6886: Flow 1 (TCP connection): Side 0 hash table remove: bucket: 277245 +0.6887: Flow 1 (FREE): ACTIVE -> FREE +0.6887: Flow 1 (FREE): TAP [2001:db8::7]:49344 -> [2606:4700:10::ac42:ad95]:443 => HOST [::]:0 -> [2606:4700:10::ac42:ad95]:443 +0.6887: Flow 3 (TCP connection): ACK_TO_TAP_DUE +0.6887: Flow 2 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +0.6887: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +0.6887: Flow 2 (TCP connection): timer expires in 1.000s +0.6887: Flow 2 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +0.6887: Flow 2 (TCP connection): Side 0 hash table remove: bucket: 124438 +0.6887: Flow 2 (FREE): ACTIVE -> FREE +0.6887: Flow 2 (FREE): TAP [198.51.100.7]:40178 -> [172.66.173.149]:443 => HOST [0.0.0.0]:0 -> [172.66.173.149]:443 +0.6895: Flow 4 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +0.6895: Flow 4 (TCP connection): ACK_FROM_TAP_DUE +0.6895: Flow 4 (TCP connection): timer expires in 1.000s +0.6895: Flow 4 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +0.6895: Flow 4 (TCP connection): Side 0 hash table remove: bucket: 256789 +0.6895: Flow 4 (FREE): ACTIVE -> FREE +0.6895: Flow 4 (FREE): TAP [2001:db8::7]:45592 -> [2606:4700:10::6814:2011]:443 => HOST [::]:0 -> [2606:4700:10::6814:2011]:443 +0.6951: Flow 3 (TCP connection): ACK_TO_TAP_DUE dropped +0.6951: Flow 3 (TCP connection): timer expires in 0.000s +0.7048: Flow 3 (TCP connection): ACK_FROM_TAP_DUE +0.7048: Flow 3 (TCP connection): timer expires in 1.000s +0.7048: Flow 3 (TCP connection): STALLED +0.7048: Flow 3 (TCP connection): ACK_FROM_TAP_DUE dropped +0.7048: Flow 3 (TCP connection): STALLED dropped +0.7054: Flow 3 (TCP connection): ACK_TO_TAP_DUE +0.7504: Flow 3 (TCP connection): ACK_TO_TAP_DUE dropped +0.7504: Flow 3 (TCP connection): timer expires in 0.000s +0.7625: Flow 3 (TCP connection): ACK_FROM_TAP_DUE +0.7625: Flow 3 (TCP connection): timer expires in 1.000s +0.7625: Flow 3 (TCP connection): STALLED +0.8030: Flow 3 (TCP connection): ACK_FROM_TAP_DUE dropped +0.8030: Flow 3 (TCP connection): STALLED dropped +0.9127: Flow 1 (NEW): FREE -> NEW +0.9127: Flow 1 (INI): NEW -> INI +0.9127: Flow 1 (INI): TAP [198.51.100.7]:59425 -> [169.254.1.1]:53 => ? +0.9127: Flow 1 (TGT): INI -> TGT +0.9127: Flow 1 (TGT): TAP [198.51.100.7]:59425 -> [169.254.1.1]:53 => HOST [0.0.0.0]:59425 -> [127.0.0.53]:53 +0.9127: Flow 1 (UDP flow): TGT -> TYPED +0.9127: Flow 1 (UDP flow): TAP [198.51.100.7]:59425 -> [169.254.1.1]:53 => HOST [0.0.0.0]:59425 -> [127.0.0.53]:53 +0.9128: Flow 1 (UDP flow): Side 0 hash table insert: bucket: 263925 +0.9128: Flow 1 (UDP flow): Side 1 hash table insert: bucket: 136662 +0.9128: Flow 1 (UDP flow): TYPED -> ACTIVE +0.9128: Flow 1 (UDP flow): TAP [198.51.100.7]:59425 -> [169.254.1.1]:53 => HOST [127.0.0.1]:59425 -> [127.0.0.53]:53 +0.9170: Flow 2 (NEW): FREE -> NEW +0.9170: Flow 2 (INI): NEW -> INI +0.9170: Flow 2 (INI): TAP [198.51.100.7]:59092 -> [140.82.112.21]:443 => ? +0.9170: Flow 2 (TGT): INI -> TGT +0.9171: Flow 2 (TGT): TAP [198.51.100.7]:59092 -> [140.82.112.21]:443 => HOST [0.0.0.0]:0 -> [140.82.112.21]:443 +0.9171: Flow 2 (TCP connection): TGT -> TYPED +0.9171: Flow 2 (TCP connection): TAP [198.51.100.7]:59092 -> [140.82.112.21]:443 => HOST [0.0.0.0]:0 -> [140.82.112.21]:443 +0.9171: Flow 2 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +0.9171: Flow 2 (TCP connection): Side 0 hash table insert: bucket: 234639 +0.9171: Flow 2 (TCP connection): TYPED -> ACTIVE +0.9171: Flow 2 (TCP connection): TAP [198.51.100.7]:59092 -> [140.82.112.21]:443 => HOST [0.0.0.0]:0 -> [140.82.112.21]:443 +0.9270: Flow 2 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +0.9270: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +0.9270: Flow 2 (TCP connection): timer expires in 1.000s +0.9270: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped +0.9270: Flow 2 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED +0.9272: Flow 2 (TCP connection): ACK_TO_TAP_DUE +0.9401: Flow 2 (TCP connection): ACK_TO_TAP_DUE dropped +0.9401: Flow 2 (TCP connection): timer expires in 0.000s +0.9404: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +0.9404: Flow 2 (TCP connection): timer expires in 1.000s +0.9404: Flow 2 (TCP connection): timer expires in 1.000s +0.9404: Flow 2 (TCP connection): timer expires in 1.000s +0.9404: Flow 2 (TCP connection): STALLED +0.9404: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped +0.9404: Flow 2 (TCP connection): STALLED dropped +0.9480: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +0.9480: Flow 2 (TCP connection): timer expires in 1.000s +0.9480: Flow 2 (TCP connection): STALLED +0.9480: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped +0.9480: Flow 2 (TCP connection): STALLED dropped +0.9487: Flow 2 (TCP connection): ACK_TO_TAP_DUE +0.9584: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +0.9585: Flow 2 (TCP connection): STALLED +0.9617: Flow 2 (TCP connection): ACK_TO_TAP_DUE dropped +0.9617: Flow 2 (TCP connection): timer expires in 1.000s +0.9617: Flow 2 (TCP connection): timer expires in 1.000s +0.9989: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped +0.9990: Flow 2 (TCP connection): STALLED dropped +1.0251: TCP inactivity scan +1.1039: Flow 4 (NEW): FREE -> NEW +1.1039: Flow 4 (INI): NEW -> INI +1.1039: Flow 4 (INI): TAP [198.51.100.7]:42639 -> [169.254.1.1]:53 => ? +1.1039: Flow 4 (TGT): INI -> TGT +1.1039: Flow 4 (TGT): TAP [198.51.100.7]:42639 -> [169.254.1.1]:53 => HOST [0.0.0.0]:42639 -> [127.0.0.53]:53 +1.1039: Flow 4 (UDP flow): TGT -> TYPED +1.1039: Flow 4 (UDP flow): TAP [198.51.100.7]:42639 -> [169.254.1.1]:53 => HOST [0.0.0.0]:42639 -> [127.0.0.53]:53 +1.1039: Flow 4 (UDP flow): Side 0 hash table insert: bucket: 104330 +1.1039: Flow 4 (UDP flow): Side 1 hash table insert: bucket: 4393 +1.1039: Flow 4 (UDP flow): TYPED -> ACTIVE +1.1039: Flow 4 (UDP flow): TAP [198.51.100.7]:42639 -> [169.254.1.1]:53 => HOST [127.0.0.1]:42639 -> [127.0.0.53]:53 +1.1042: Flow 5 (NEW): FREE -> NEW +1.1042: Flow 5 (INI): NEW -> INI +1.1042: Flow 5 (INI): TAP [198.51.100.7]:49988 -> [140.82.112.3]:443 => ? +1.1042: Flow 5 (TGT): INI -> TGT +1.1042: Flow 5 (TGT): TAP [198.51.100.7]:49988 -> [140.82.112.3]:443 => HOST [0.0.0.0]:0 -> [140.82.112.3]:443 +1.1042: Flow 5 (TCP connection): TGT -> TYPED +1.1042: Flow 5 (TCP connection): TAP [198.51.100.7]:49988 -> [140.82.112.3]:443 => HOST [0.0.0.0]:0 -> [140.82.112.3]:443 +1.1042: Flow 5 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.1042: Flow 5 (TCP connection): Side 0 hash table insert: bucket: 66400 +1.1043: Flow 5 (TCP connection): TYPED -> ACTIVE +1.1043: Flow 5 (TCP connection): TAP [198.51.100.7]:49988 -> [140.82.112.3]:443 => HOST [0.0.0.0]:0 -> [140.82.112.3]:443 +1.1152: Flow 5 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.1152: Flow 5 (TCP connection): ACK_FROM_TAP_DUE +1.1152: Flow 5 (TCP connection): timer expires in 1.000s +1.1152: Flow 5 (TCP connection): ACK_FROM_TAP_DUE dropped +1.1152: Flow 5 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED +1.1160: Flow 5 (TCP connection): ACK_TO_TAP_DUE +1.1261: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +1.1261: Flow 2 (TCP connection): timer expires in 1.000s +1.1261: Flow 2 (TCP connection): STALLED +1.1262: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped +1.1262: Flow 2 (TCP connection): STALLED dropped +1.1288: Flow 5 (TCP connection): ACK_FROM_TAP_DUE +1.1288: Flow 5 (TCP connection): STALLED +1.1288: Flow 5 (TCP connection): ACK_FROM_TAP_DUE dropped +1.1288: Flow 5 (TCP connection): ACK_TO_TAP_DUE dropped +1.1288: Flow 5 (TCP connection): STALLED dropped +1.1290: Flow 5 (TCP connection): ACK_FROM_TAP_DUE +1.1290: Flow 5 (TCP connection): timer expires in 1.000s +1.1290: Flow 5 (TCP connection): STALLED +1.1290: Flow 5 (TCP connection): ACK_FROM_TAP_DUE dropped +1.1290: Flow 5 (TCP connection): STALLED dropped +1.1299: Flow 5 (TCP connection): ACK_TO_TAP_DUE +1.1358: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +1.1358: Flow 2 (TCP connection): timer expires in 1.000s +1.1358: Flow 2 (TCP connection): timer expires in 1.000s +1.1359: Flow 2 (TCP connection): timer expires in 1.000s +1.1359: Flow 2 (TCP connection): timer expires in 1.000s +1.1359: Flow 2 (TCP connection): timer expires in 1.000s +1.1359: Flow 2 (TCP connection): timer expires in 1.000s +1.1359: Flow 2 (TCP connection): timer expires in 1.000s +1.1359: Flow 2 (TCP connection): STALLED +1.1359: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped +1.1359: Flow 2 (TCP connection): STALLED dropped +1.1416: Flow 5 (TCP connection): ACK_FROM_TAP_DUE +1.1417: Flow 5 (TCP connection): STALLED +1.1448: Flow 5 (TCP connection): ACK_TO_TAP_DUE dropped +1.1449: Flow 5 (TCP connection): timer expires in 1.000s +1.1449: Flow 5 (TCP connection): timer expires in 1.000s +1.1504: Flow 2 (TCP connection): ACK_FROM_TAP_DUE +1.1504: Flow 2 (TCP connection): timer expires in 1.000s +1.1505: Flow 2 (TCP connection): STALLED +1.1505: Flow 2 (TCP connection): ACK_FROM_TAP_DUE dropped +1.1505: Flow 2 (TCP connection): STALLED dropped +1.1820: Flow 5 (TCP connection): ACK_FROM_TAP_DUE dropped +1.1820: Flow 5 (TCP connection): STALLED dropped +1.2174: Flow 5 (TCP connection): ACK_FROM_TAP_DUE +1.2174: Flow 5 (TCP connection): timer expires in 1.000s +1.2175: Flow 5 (TCP connection): STALLED +1.2176: Flow 5 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2176: Flow 5 (TCP connection): STALLED dropped +1.2179: Flow 6 (NEW): FREE -> NEW +1.2179: Flow 6 (INI): NEW -> INI +1.2179: Flow 6 (INI): TAP [198.51.100.7]:57247 -> [169.254.1.1]:53 => ? +1.2179: Flow 6 (TGT): INI -> TGT +1.2179: Flow 6 (TGT): TAP [198.51.100.7]:57247 -> [169.254.1.1]:53 => HOST [0.0.0.0]:57247 -> [127.0.0.53]:53 +1.2179: Flow 6 (UDP flow): TGT -> TYPED +1.2179: Flow 6 (UDP flow): TAP [198.51.100.7]:57247 -> [169.254.1.1]:53 => HOST [0.0.0.0]:57247 -> [127.0.0.53]:53 +1.2180: Flow 6 (UDP flow): Side 0 hash table insert: bucket: 56993 +1.2180: Flow 6 (UDP flow): Side 1 hash table insert: bucket: 194914 +1.2180: Flow 6 (UDP flow): TYPED -> ACTIVE +1.2180: Flow 6 (UDP flow): TAP [198.51.100.7]:57247 -> [169.254.1.1]:53 => HOST [127.0.0.1]:57247 -> [127.0.0.53]:53 +1.2225: Flow 7 (NEW): FREE -> NEW +1.2225: Flow 7 (INI): NEW -> INI +1.2225: Flow 7 (INI): TAP [198.51.100.7]:40696 -> [185.199.109.133]:443 => ? +1.2225: Flow 7 (TGT): INI -> TGT +1.2225: Flow 7 (TGT): TAP [198.51.100.7]:40696 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 +1.2226: Flow 7 (TCP connection): TGT -> TYPED +1.2226: Flow 7 (TCP connection): TAP [198.51.100.7]:40696 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 +1.2226: Flow 7 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.2226: Flow 7 (TCP connection): Side 0 hash table insert: bucket: 325936 +1.2226: Flow 7 (TCP connection): TYPED -> ACTIVE +1.2226: Flow 7 (TCP connection): TAP [198.51.100.7]:40696 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 +1.2226: Flow 8 (NEW): FREE -> NEW +1.2226: Flow 8 (INI): NEW -> INI +1.2226: Flow 8 (INI): TAP [198.51.100.7]:35412 -> [185.199.110.133]:443 => ? +1.2226: Flow 8 (TGT): INI -> TGT +1.2226: Flow 8 (TGT): TAP [198.51.100.7]:35412 -> [185.199.110.133]:443 => HOST [0.0.0.0]:0 -> [185.199.110.133]:443 +1.2226: Flow 8 (TCP connection): TGT -> TYPED +1.2226: Flow 8 (TCP connection): TAP [198.51.100.7]:35412 -> [185.199.110.133]:443 => HOST [0.0.0.0]:0 -> [185.199.110.133]:443 +1.2227: Flow 8 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.2227: Flow 8 (TCP connection): Side 0 hash table insert: bucket: 296582 +1.2227: Flow 8 (TCP connection): TYPED -> ACTIVE +1.2227: Flow 8 (TCP connection): TAP [198.51.100.7]:35412 -> [185.199.110.133]:443 => HOST [0.0.0.0]:0 -> [185.199.110.133]:443 +1.2227: Flow 9 (NEW): FREE -> NEW +1.2227: Flow 9 (INI): NEW -> INI +1.2227: Flow 9 (INI): TAP [198.51.100.7]:55588 -> [185.199.111.133]:443 => ? +1.2227: Flow 9 (TGT): INI -> TGT +1.2227: Flow 9 (TGT): TAP [198.51.100.7]:55588 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.2227: Flow 9 (TCP connection): TGT -> TYPED +1.2227: Flow 9 (TCP connection): TAP [198.51.100.7]:55588 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.2227: Flow 9 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.2227: Flow 9 (TCP connection): Side 0 hash table insert: bucket: 261510 +1.2227: Flow 9 (TCP connection): TYPED -> ACTIVE +1.2227: Flow 9 (TCP connection): TAP [198.51.100.7]:55588 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.2227: Flow 10 (NEW): FREE -> NEW +1.2227: Flow 10 (INI): NEW -> INI +1.2227: Flow 10 (INI): TAP [198.51.100.7]:52618 -> [185.199.108.133]:443 => ? +1.2227: Flow 10 (TGT): INI -> TGT +1.2227: Flow 10 (TGT): TAP [198.51.100.7]:52618 -> [185.199.108.133]:443 => HOST [0.0.0.0]:0 -> [185.199.108.133]:443 +1.2227: Flow 10 (TCP connection): TGT -> TYPED +1.2227: Flow 10 (TCP connection): TAP [198.51.100.7]:52618 -> [185.199.108.133]:443 => HOST [0.0.0.0]:0 -> [185.199.108.133]:443 +1.2227: Flow 10 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.2227: Flow 10 (TCP connection): Side 0 hash table insert: bucket: 325642 +1.2227: Flow 10 (TCP connection): TYPED -> ACTIVE +1.2227: Flow 10 (TCP connection): TAP [198.51.100.7]:52618 -> [185.199.108.133]:443 => HOST [0.0.0.0]:0 -> [185.199.108.133]:443 +1.2264: Flow 10 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.2264: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2265: Flow 10 (TCP connection): timer expires in 1.000s +1.2265: Flow 7 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.2265: Flow 7 (TCP connection): ACK_FROM_TAP_DUE +1.2265: Flow 7 (TCP connection): timer expires in 1.000s +1.2266: Flow 9 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.2266: Flow 9 (TCP connection): ACK_FROM_TAP_DUE +1.2266: Flow 9 (TCP connection): timer expires in 1.000s +1.2266: Flow 8 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.2266: Flow 8 (TCP connection): ACK_FROM_TAP_DUE +1.2266: Flow 8 (TCP connection): timer expires in 1.000s +1.2266: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2266: Flow 10 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED +1.2267: Flow 7 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +1.2267: Flow 7 (TCP connection): Side 0 hash table remove: bucket: 325936 +1.2267: Flow 9 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +1.2267: Flow 9 (TCP connection): Side 0 hash table remove: bucket: 261510 +1.2267: Flow 8 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +1.2267: Flow 8 (TCP connection): Side 0 hash table remove: bucket: 296582 +1.2268: Flow 7 (FREE): ACTIVE -> FREE +1.2268: Flow 7 (FREE): TAP [198.51.100.7]:40696 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 +1.2268: Flow 8 (FREE): ACTIVE -> FREE +1.2268: Flow 8 (FREE): TAP [198.51.100.7]:35412 -> [185.199.110.133]:443 => HOST [0.0.0.0]:0 -> [185.199.110.133]:443 +1.2268: Flow 9 (FREE): ACTIVE -> FREE +1.2268: Flow 9 (FREE): TAP [198.51.100.7]:55588 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.2270: Flow 10 (TCP connection): ACK_TO_TAP_DUE +1.2448: Flow 10 (TCP connection): ACK_TO_TAP_DUE dropped +1.2448: Flow 10 (TCP connection): timer expires in 0.000s +1.2592: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2593: Flow 10 (TCP connection): timer expires in 1.000s +1.2593: Flow 10 (TCP connection): timer expires in 1.000s +1.2594: Flow 10 (TCP connection): timer expires in 1.000s +1.2594: Flow 10 (TCP connection): STALLED +1.2594: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2594: Flow 10 (TCP connection): STALLED dropped +1.2609: Flow 10 (TCP connection): ACK_TO_TAP_DUE +1.2642: Flow 10 (TCP connection): ACK_TO_TAP_DUE dropped +1.2642: Flow 10 (TCP connection): timer expires in 0.000s +1.2734: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2734: Flow 10 (TCP connection): timer expires in 1.000s +1.2735: Flow 10 (TCP connection): STALLED +1.2741: Flow 10 (TCP connection): STALLED dropped +1.2741: Flow 10 (TCP connection): timer expires in 1.000s +1.2742: Flow 10 (TCP connection): STALLED +1.2742: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2742: Flow 10 (TCP connection): STALLED dropped +1.2751: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2751: Flow 10 (TCP connection): timer expires in 1.000s +1.2751: Flow 10 (TCP connection): STALLED +1.2761: Flow 10 (TCP connection): STALLED dropped +1.2761: Flow 10 (TCP connection): timer expires in 1.000s +1.2761: Flow 10 (TCP connection): STALLED +1.2761: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2761: Flow 10 (TCP connection): STALLED dropped +1.2771: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2771: Flow 10 (TCP connection): timer expires in 1.000s +1.2772: Flow 10 (TCP connection): STALLED +1.2780: Flow 10 (TCP connection): STALLED dropped +1.2780: Flow 10 (TCP connection): timer expires in 1.000s +1.2781: Flow 10 (TCP connection): STALLED +1.2781: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2781: Flow 10 (TCP connection): STALLED dropped +1.2791: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2792: Flow 10 (TCP connection): timer expires in 1.000s +1.2792: Flow 10 (TCP connection): STALLED +1.2798: Flow 10 (TCP connection): STALLED dropped +1.2798: Flow 10 (TCP connection): timer expires in 1.000s +1.2799: Flow 10 (TCP connection): STALLED +1.2799: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2799: Flow 10 (TCP connection): STALLED dropped +1.2808: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2808: Flow 10 (TCP connection): timer expires in 1.000s +1.2808: Flow 10 (TCP connection): STALLED +1.2820: Flow 10 (TCP connection): STALLED dropped +1.2821: Flow 10 (TCP connection): timer expires in 1.000s +1.2821: Flow 10 (TCP connection): STALLED +1.2821: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2821: Flow 10 (TCP connection): STALLED dropped +1.2831: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2832: Flow 10 (TCP connection): timer expires in 1.000s +1.2832: Flow 10 (TCP connection): STALLED +1.2832: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2832: Flow 10 (TCP connection): STALLED dropped +1.2841: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2841: Flow 10 (TCP connection): timer expires in 1.000s +1.2841: Flow 10 (TCP connection): STALLED +1.2847: Flow 10 (TCP connection): STALLED dropped +1.2847: Flow 10 (TCP connection): timer expires in 1.000s +1.2848: Flow 10 (TCP connection): STALLED +1.2849: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2849: Flow 10 (TCP connection): STALLED dropped +1.2853: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2853: Flow 10 (TCP connection): timer expires in 1.000s +1.2853: Flow 10 (TCP connection): STALLED +1.2862: Flow 10 (TCP connection): STALLED dropped +1.2862: Flow 10 (TCP connection): timer expires in 1.000s +1.2863: Flow 10 (TCP connection): STALLED +1.2863: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2863: Flow 10 (TCP connection): STALLED dropped +1.2871: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2871: Flow 10 (TCP connection): timer expires in 1.000s +1.2872: Flow 10 (TCP connection): STALLED +1.2877: Flow 10 (TCP connection): STALLED dropped +1.2877: Flow 10 (TCP connection): timer expires in 1.000s +1.2878: Flow 10 (TCP connection): STALLED +1.2878: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2878: Flow 10 (TCP connection): STALLED dropped +1.2886: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2886: Flow 10 (TCP connection): timer expires in 1.000s +1.2886: Flow 10 (TCP connection): STALLED +1.2890: Flow 10 (TCP connection): STALLED dropped +1.2890: Flow 10 (TCP connection): timer expires in 1.000s +1.2890: Flow 10 (TCP connection): STALLED +1.2890: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2890: Flow 10 (TCP connection): STALLED dropped +1.2893: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2893: Flow 10 (TCP connection): timer expires in 1.000s +1.2893: Flow 10 (TCP connection): STALLED +1.2894: Flow 10 (TCP connection): STALLED dropped +1.2894: Flow 10 (TCP connection): timer expires in 1.000s +1.2895: Flow 10 (TCP connection): STALLED +1.2895: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2895: Flow 10 (TCP connection): STALLED dropped +1.2899: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2899: Flow 10 (TCP connection): timer expires in 1.000s +1.2899: Flow 10 (TCP connection): STALLED +1.2901: Flow 10 (TCP connection): STALLED dropped +1.2901: Flow 10 (TCP connection): timer expires in 1.000s +1.2902: Flow 10 (TCP connection): timer expires in 1.000s +1.2902: Flow 10 (TCP connection): STALLED +1.2902: Flow 10 (TCP connection): timer expires in 1.000s +1.2902: Flow 10 (TCP connection): STALLED dropped +1.2902: Flow 10 (TCP connection): STALLED +1.2904: Flow 10 (TCP connection): STALLED dropped +1.2904: Flow 10 (TCP connection): timer expires in 1.000s +1.2905: Flow 10 (TCP connection): STALLED +1.2905: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2905: Flow 10 (TCP connection): STALLED dropped +1.2907: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2907: Flow 10 (TCP connection): timer expires in 1.000s +1.2907: Flow 10 (TCP connection): timer expires in 1.000s +1.2907: Flow 10 (TCP connection): STALLED +1.2907: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2907: Flow 10 (TCP connection): STALLED dropped +1.2936: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2936: Flow 10 (TCP connection): timer expires in 1.000s +1.2936: Flow 10 (TCP connection): STALLED +1.2938: Flow 10 (TCP connection): STALLED dropped +1.2938: Flow 10 (TCP connection): timer expires in 1.000s +1.2939: Flow 10 (TCP connection): timer expires in 1.000s +1.2939: Flow 10 (TCP connection): timer expires in 1.000s +1.2939: Flow 10 (TCP connection): STALLED +1.2941: Flow 10 (TCP connection): STALLED dropped +1.2942: Flow 10 (TCP connection): timer expires in 1.000s +1.2942: Flow 10 (TCP connection): STALLED +1.2942: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2942: Flow 10 (TCP connection): STALLED dropped +1.2944: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2944: Flow 10 (TCP connection): timer expires in 1.000s +1.2944: Flow 10 (TCP connection): timer expires in 1.000s +1.2944: Flow 10 (TCP connection): STALLED +1.2945: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2945: Flow 10 (TCP connection): STALLED dropped +1.2947: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2947: Flow 10 (TCP connection): timer expires in 1.000s +1.2948: Flow 10 (TCP connection): STALLED +1.2949: Flow 10 (TCP connection): STALLED dropped +1.2949: Flow 10 (TCP connection): timer expires in 1.000s +1.2950: Flow 10 (TCP connection): timer expires in 1.000s +1.2950: Flow 10 (TCP connection): timer expires in 1.000s +1.2950: Flow 10 (TCP connection): STALLED +1.2952: Flow 10 (TCP connection): STALLED dropped +1.2952: Flow 10 (TCP connection): timer expires in 1.000s +1.2953: Flow 10 (TCP connection): STALLED +1.2953: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2953: Flow 10 (TCP connection): STALLED dropped +1.2955: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2955: Flow 10 (TCP connection): timer expires in 1.000s +1.2955: Flow 10 (TCP connection): STALLED +1.2958: Flow 10 (TCP connection): STALLED dropped +1.2958: Flow 10 (TCP connection): timer expires in 1.000s +1.2958: Flow 10 (TCP connection): STALLED +1.2958: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2958: Flow 10 (TCP connection): STALLED dropped +1.2961: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2961: Flow 10 (TCP connection): timer expires in 1.000s +1.2961: Flow 10 (TCP connection): STALLED +1.2963: Flow 10 (TCP connection): STALLED dropped +1.2963: Flow 10 (TCP connection): timer expires in 1.000s +1.2963: Flow 10 (TCP connection): STALLED +1.2964: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2964: Flow 10 (TCP connection): STALLED dropped +1.2966: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2966: Flow 10 (TCP connection): timer expires in 1.000s +1.2967: Flow 10 (TCP connection): STALLED +1.2969: Flow 10 (TCP connection): STALLED dropped +1.2969: Flow 10 (TCP connection): timer expires in 1.000s +1.2969: Flow 10 (TCP connection): STALLED +1.2969: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2969: Flow 10 (TCP connection): STALLED dropped +1.2971: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2971: Flow 10 (TCP connection): timer expires in 1.000s +1.2972: Flow 10 (TCP connection): STALLED +1.2974: Flow 10 (TCP connection): STALLED dropped +1.2974: Flow 10 (TCP connection): timer expires in 1.000s +1.2974: Flow 10 (TCP connection): STALLED +1.2974: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2974: Flow 10 (TCP connection): STALLED dropped +1.2977: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2977: Flow 10 (TCP connection): timer expires in 1.000s +1.2977: Flow 10 (TCP connection): STALLED +1.2979: Flow 10 (TCP connection): STALLED dropped +1.2979: Flow 10 (TCP connection): timer expires in 1.000s +1.2980: Flow 10 (TCP connection): STALLED +1.2980: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2980: Flow 10 (TCP connection): STALLED dropped +1.2983: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2983: Flow 10 (TCP connection): timer expires in 1.000s +1.2983: Flow 10 (TCP connection): STALLED +1.2985: Flow 10 (TCP connection): STALLED dropped +1.2985: Flow 10 (TCP connection): timer expires in 1.000s +1.2985: Flow 10 (TCP connection): STALLED +1.2985: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2985: Flow 10 (TCP connection): STALLED dropped +1.2988: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2988: Flow 10 (TCP connection): timer expires in 1.000s +1.2988: Flow 10 (TCP connection): STALLED +1.2990: Flow 10 (TCP connection): STALLED dropped +1.2990: Flow 10 (TCP connection): timer expires in 1.000s +1.2991: Flow 10 (TCP connection): STALLED +1.2991: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2991: Flow 10 (TCP connection): STALLED dropped +1.2994: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2994: Flow 10 (TCP connection): timer expires in 1.000s +1.2994: Flow 10 (TCP connection): STALLED +1.2996: Flow 10 (TCP connection): STALLED dropped +1.2996: Flow 10 (TCP connection): timer expires in 1.000s +1.2996: Flow 10 (TCP connection): STALLED +1.2996: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.2996: Flow 10 (TCP connection): STALLED dropped +1.2999: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.2999: Flow 10 (TCP connection): timer expires in 1.000s +1.3000: Flow 10 (TCP connection): STALLED +1.3002: Flow 10 (TCP connection): STALLED dropped +1.3002: Flow 10 (TCP connection): timer expires in 1.000s +1.3004: Flow 10 (TCP connection): STALLED +1.3004: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.3004: Flow 10 (TCP connection): STALLED dropped +1.3005: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.3005: Flow 10 (TCP connection): timer expires in 1.000s +1.3006: Flow 10 (TCP connection): timer expires in 1.000s +1.3006: Flow 10 (TCP connection): STALLED +1.3007: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.3007: Flow 10 (TCP connection): STALLED dropped +1.3008: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.3008: Flow 10 (TCP connection): timer expires in 1.000s +1.3008: Flow 10 (TCP connection): STALLED +1.3011: Flow 10 (TCP connection): STALLED dropped +1.3011: Flow 10 (TCP connection): timer expires in 1.000s +1.3012: Flow 10 (TCP connection): timer expires in 1.000s +1.3012: Flow 10 (TCP connection): STALLED +1.3012: Flow 10 (TCP connection): timer expires in 1.000s +1.3012: Flow 10 (TCP connection): STALLED dropped +1.3012: Flow 10 (TCP connection): STALLED +1.3014: Flow 10 (TCP connection): STALLED dropped +1.3014: Flow 10 (TCP connection): timer expires in 1.000s +1.3014: Flow 10 (TCP connection): timer expires in 1.000s +1.3014: Flow 10 (TCP connection): STALLED +1.3014: Flow 10 (TCP connection): timer expires in 1.000s +1.3014: Flow 10 (TCP connection): STALLED dropped +1.3014: Flow 10 (TCP connection): STALLED +1.3017: Flow 10 (TCP connection): STALLED dropped +1.3017: Flow 10 (TCP connection): timer expires in 1.000s +1.3017: Flow 10 (TCP connection): STALLED +1.3017: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.3017: Flow 10 (TCP connection): STALLED dropped +1.3020: Flow 10 (TCP connection): ACK_FROM_TAP_DUE +1.3020: Flow 10 (TCP connection): timer expires in 1.000s +1.3020: Flow 10 (TCP connection): STALLED +1.3219: Flow 7 (NEW): FREE -> NEW +1.3219: Flow 7 (INI): NEW -> INI +1.3219: Flow 7 (INI): TAP [198.51.100.7]:49856 -> [169.254.1.1]:53 => ? +1.3219: Flow 7 (TGT): INI -> TGT +1.3219: Flow 7 (TGT): TAP [198.51.100.7]:49856 -> [169.254.1.1]:53 => HOST [0.0.0.0]:49856 -> [127.0.0.53]:53 +1.3219: Flow 7 (UDP flow): TGT -> TYPED +1.3219: Flow 7 (UDP flow): TAP [198.51.100.7]:49856 -> [169.254.1.1]:53 => HOST [0.0.0.0]:49856 -> [127.0.0.53]:53 +1.3219: Flow 7 (UDP flow): Side 0 hash table insert: bucket: 277953 +1.3219: Flow 7 (UDP flow): Side 1 hash table insert: bucket: 193400 +1.3219: Flow 7 (UDP flow): TYPED -> ACTIVE +1.3219: Flow 7 (UDP flow): TAP [198.51.100.7]:49856 -> [169.254.1.1]:53 => HOST [127.0.0.1]:49856 -> [127.0.0.53]:53 +1.3274: Flow 8 (NEW): FREE -> NEW +1.3274: Flow 8 (INI): NEW -> INI +1.3274: Flow 8 (INI): TAP [2001:db8::7]:38294 -> [2606:50c0:8003::154]:443 => ? +1.3274: Flow 8 (TGT): INI -> TGT +1.3275: Flow 8 (TGT): TAP [2001:db8::7]:38294 -> [2606:50c0:8003::154]:443 => HOST [::]:0 -> [2606:50c0:8003::154]:443 +1.3275: Flow 8 (TCP connection): TGT -> TYPED +1.3275: Flow 8 (TCP connection): TAP [2001:db8::7]:38294 -> [2606:50c0:8003::154]:443 => HOST [::]:0 -> [2606:50c0:8003::154]:443 +1.3275: Flow 8 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.3275: Flow 8 (TCP connection): Side 0 hash table insert: bucket: 189691 +1.3275: Flow 8 (TCP connection): TYPED -> ACTIVE +1.3275: Flow 8 (TCP connection): TAP [2001:db8::7]:38294 -> [2606:50c0:8003::154]:443 => HOST [::]:0 -> [2606:50c0:8003::154]:443 +1.3275: Flow 9 (NEW): FREE -> NEW +1.3275: Flow 9 (INI): NEW -> INI +1.3275: Flow 9 (INI): TAP [198.51.100.7]:40716 -> [185.199.109.133]:443 => ? +1.3276: Flow 9 (TGT): INI -> TGT +1.3276: Flow 9 (TGT): TAP [198.51.100.7]:40716 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 +1.3276: Flow 9 (TCP connection): TGT -> TYPED +1.3276: Flow 9 (TCP connection): TAP [198.51.100.7]:40716 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 +1.3276: Flow 9 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.3276: Flow 9 (TCP connection): Side 0 hash table insert: bucket: 13717 +1.3276: Flow 9 (TCP connection): TYPED -> ACTIVE +1.3276: Flow 9 (TCP connection): TAP [198.51.100.7]:40716 -> [185.199.109.133]:443 => HOST [0.0.0.0]:0 -> [185.199.109.133]:443 +1.3276: Flow 11 (NEW): FREE -> NEW +1.3276: Flow 11 (INI): NEW -> INI +1.3276: Flow 11 (INI): TAP [198.51.100.7]:55600 -> [185.199.111.133]:443 => ? +1.3276: Flow 11 (TGT): INI -> TGT +1.3276: Flow 11 (TGT): TAP [198.51.100.7]:55600 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.3276: Flow 11 (TCP connection): TGT -> TYPED +1.3276: Flow 11 (TCP connection): TAP [198.51.100.7]:55600 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.3276: Flow 11 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.3276: Flow 11 (TCP connection): Side 0 hash table insert: bucket: 280556 +1.3276: Flow 11 (TCP connection): TYPED -> ACTIVE +1.3276: Flow 11 (TCP connection): TAP [198.51.100.7]:55600 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.3276: Flow 12 (NEW): FREE -> NEW +1.3276: Flow 12 (INI): NEW -> INI +1.3276: Flow 12 (INI): TAP [2001:db8::7]:36704 -> [2606:50c0:8001::154]:443 => ? +1.3276: Flow 12 (TGT): INI -> TGT +1.3276: Flow 12 (TGT): TAP [2001:db8::7]:36704 -> [2606:50c0:8001::154]:443 => HOST [::]:0 -> [2606:50c0:8001::154]:443 +1.3276: Flow 12 (TCP connection): TGT -> TYPED +1.3276: Flow 12 (TCP connection): TAP [2001:db8::7]:36704 -> [2606:50c0:8001::154]:443 => HOST [::]:0 -> [2606:50c0:8001::154]:443 +1.3276: Flow 12 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +1.3276: Flow 12 (TCP connection): Side 0 hash table insert: bucket: 206072 +1.3277: Flow 12 (TCP connection): TYPED -> ACTIVE +1.3277: Flow 12 (TCP connection): TAP [2001:db8::7]:36704 -> [2606:50c0:8001::154]:443 => HOST [::]:0 -> [2606:50c0:8001::154]:443 +1.3308: Flow 9 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.3308: Flow 9 (TCP connection): ACK_FROM_TAP_DUE +1.3308: Flow 9 (TCP connection): timer expires in 1.000s +1.3308: Flow 8 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.3308: Flow 8 (TCP connection): ACK_FROM_TAP_DUE +1.3309: Flow 8 (TCP connection): timer expires in 1.000s +1.3309: Flow 9 (TCP connection): ACK_FROM_TAP_DUE dropped +1.3309: Flow 9 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED +1.3310: Flow 8 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +1.3310: Flow 8 (TCP connection): Side 0 hash table remove: bucket: 189691 +1.3310: Flow 12 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.3310: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +1.3310: Flow 12 (TCP connection): timer expires in 1.000s +1.3311: Flow 11 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +1.3311: Flow 11 (TCP connection): ACK_FROM_TAP_DUE +1.3311: Flow 11 (TCP connection): timer expires in 1.000s +1.3313: Flow 8 (FREE): ACTIVE -> FREE +1.3313: Flow 8 (FREE): TAP [2001:db8::7]:38294 -> [2606:50c0:8003::154]:443 => HOST [::]:0 -> [2606:50c0:8003::154]:443 +1.3313: Flow 11 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +1.3313: Flow 11 (TCP connection): Side 0 hash table remove: bucket: 280556 +1.3314: Flow 9 (TCP connection): ACK_TO_TAP_DUE +1.3314: Flow 12 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +1.3314: Flow 12 (TCP connection): Side 0 hash table remove: bucket: 206072 +1.3315: Flow 11 (FREE): ACTIVE -> FREE +1.3315: Flow 11 (FREE): TAP [198.51.100.7]:55600 -> [185.199.111.133]:443 => HOST [0.0.0.0]:0 -> [185.199.111.133]:443 +1.3315: Flow 12 (FREE): ACTIVE -> FREE +1.3315: Flow 12 (FREE): TAP [2001:db8::7]:36704 -> [2606:50c0:8001::154]:443 => HOST [::]:0 -> [2606:50c0:8001::154]:443 +1.3380: Flow 9 (TCP connection): ACK_TO_TAP_DUE dropped +1.3381: Flow 9 (TCP connection): timer expires in 0.000s +1.3398: Flow 9 (TCP connection): ACK_FROM_TAP_DUE +1.3399: Flow 9 (TCP connection): timer expires in 1.000s +1.3400: Flow 9 (TCP connection): STALLED +1.3400: Flow 9 (TCP connection): ACK_FROM_TAP_DUE dropped +1.3400: Flow 9 (TCP connection): STALLED dropped +1.3418: Flow 9 (TCP connection): ACK_TO_TAP_DUE +1.3430: Flow 10 (TCP connection): ACK_FROM_TAP_DUE dropped +1.3430: Flow 10 (TCP connection): STALLED dropped +1.3451: Flow 9 (TCP connection): ACK_TO_TAP_DUE dropped +1.3451: Flow 9 (TCP connection): timer expires in 0.000s +1.3670: Flow 9 (TCP connection): ACK_FROM_TAP_DUE +1.3670: Flow 9 (TCP connection): timer expires in 1.000s +1.3671: Flow 9 (TCP connection): STALLED +1.4080: Flow 9 (TCP connection): ACK_FROM_TAP_DUE dropped +1.4080: Flow 9 (TCP connection): STALLED dropped +740.9544: Flow 0 (TCP connection (spliced)): FIN_RCVD_0 +740.9545: Flow 0 (TCP connection (spliced)): FIN_SENT_1 +740.9545: Flow 0 (TCP connection (spliced)): RCVLOWAT_ACT_1 +740.9545: Flow 0 (TCP connection (spliced)): CLOSED +740.9546: Flow 0 (FREE): ACTIVE -> FREE +740.9546: Flow 0 (FREE): HOST [127.0.0.1]:51982 -> [127.65.81.1]:24700 => SPLICE [0.0.0.0]:0 -> [198.51.100.7]:24700 +740.9546: Flow 0 (NEW): FREE -> NEW +740.9547: Flow 0 (INI): NEW -> INI +740.9547: Flow 0 (INI): HOST [127.0.0.1]:38982 -> [0.0.0.0]:24700 => ? +740.9547: Flow 0 (TGT): INI -> TGT +740.9547: Flow 0 (TGT): HOST [127.0.0.1]:38982 -> [127.65.81.1]:24700 => SPLICE [0.0.0.0]:0 -> [198.51.100.7]:24700 +740.9547: Flow 0 (TCP connection (spliced)): TGT -> TYPED +740.9547: Flow 0 (TCP connection (spliced)): HOST [127.0.0.1]:38982 -> [127.65.81.1]:24700 => SPLICE [0.0.0.0]:0 -> [198.51.100.7]:24700 +740.9548: Flow 0 (TCP connection (spliced)): SPLICE_CONNECT +740.9548: Flow 0 (TCP connection (spliced)): TYPED -> ACTIVE +740.9549: Flow 0 (TCP connection (spliced)): HOST [127.0.0.1]:38982 -> [127.65.81.1]:24700 => SPLICE [0.0.0.0]:0 -> [198.51.100.7]:24700 +740.9549: Flow 0 (TCP connection (spliced)): SPLICE_ESTABLISHED +7.3984: Flow 8 (NEW): FREE -> NEW +7.3984: Flow 8 (INI): NEW -> INI +7.3984: Flow 8 (INI): TAP [198.51.100.7]:52061 -> [169.254.1.1]:53 => ? +7.3984: Flow 8 (TGT): INI -> TGT +7.3984: Flow 8 (TGT): TAP [198.51.100.7]:52061 -> [169.254.1.1]:53 => HOST [0.0.0.0]:52061 -> [127.0.0.53]:53 +7.3984: Flow 8 (UDP flow): TGT -> TYPED +7.3984: Flow 8 (UDP flow): TAP [198.51.100.7]:52061 -> [169.254.1.1]:53 => HOST [0.0.0.0]:52061 -> [127.0.0.53]:53 +7.3984: Flow 8 (UDP flow): Side 0 hash table insert: bucket: 191745 +7.3984: Flow 8 (UDP flow): Side 1 hash table insert: bucket: 265803 +7.3984: Flow 8 (UDP flow): TYPED -> ACTIVE +7.3984: Flow 8 (UDP flow): TAP [198.51.100.7]:52061 -> [169.254.1.1]:53 => HOST [127.0.0.1]:52061 -> [127.0.0.53]:53 +7.4192: Flow 11 (NEW): FREE -> NEW +7.4192: Flow 11 (INI): NEW -> INI +7.4192: Flow 11 (INI): TAP [198.51.100.7]:55606 -> [44.193.164.157]:443 => ? +7.4192: Flow 11 (TGT): INI -> TGT +7.4192: Flow 11 (TGT): TAP [198.51.100.7]:55606 -> [44.193.164.157]:443 => HOST [0.0.0.0]:0 -> [44.193.164.157]:443 +7.4192: Flow 11 (TCP connection): TGT -> TYPED +7.4192: Flow 11 (TCP connection): TAP [198.51.100.7]:55606 -> [44.193.164.157]:443 => HOST [0.0.0.0]:0 -> [44.193.164.157]:443 +7.4192: Flow 11 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +7.4193: Flow 11 (TCP connection): Side 0 hash table insert: bucket: 371710 +7.4193: Flow 11 (TCP connection): TYPED -> ACTIVE +7.4193: Flow 11 (TCP connection): TAP [198.51.100.7]:55606 -> [44.193.164.157]:443 => HOST [0.0.0.0]:0 -> [44.193.164.157]:443 +7.4193: Flow 12 (NEW): FREE -> NEW +7.4193: Flow 12 (INI): NEW -> INI +7.4193: Flow 12 (INI): TAP [198.51.100.7]:44110 -> [100.50.38.106]:443 => ? +7.4193: Flow 12 (TGT): INI -> TGT +7.4193: Flow 12 (TGT): TAP [198.51.100.7]:44110 -> [100.50.38.106]:443 => HOST [0.0.0.0]:0 -> [100.50.38.106]:443 +7.4193: Flow 12 (TCP connection): TGT -> TYPED +7.4193: Flow 12 (TCP connection): TAP [198.51.100.7]:44110 -> [100.50.38.106]:443 => HOST [0.0.0.0]:0 -> [100.50.38.106]:443 +7.4193: Flow 12 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +7.4193: Flow 12 (TCP connection): Side 0 hash table insert: bucket: 134729 +7.4193: Flow 12 (TCP connection): TYPED -> ACTIVE +7.4193: Flow 12 (TCP connection): TAP [198.51.100.7]:44110 -> [100.50.38.106]:443 => HOST [0.0.0.0]:0 -> [100.50.38.106]:443 +7.4193: Flow 13 (NEW): FREE -> NEW +7.4193: Flow 13 (INI): NEW -> INI +7.4193: Flow 13 (INI): TAP [198.51.100.7]:39966 -> [23.22.18.51]:443 => ? +7.4193: Flow 13 (TGT): INI -> TGT +7.4193: Flow 13 (TGT): TAP [198.51.100.7]:39966 -> [23.22.18.51]:443 => HOST [0.0.0.0]:0 -> [23.22.18.51]:443 +7.4193: Flow 13 (TCP connection): TGT -> TYPED +7.4193: Flow 13 (TCP connection): TAP [198.51.100.7]:39966 -> [23.22.18.51]:443 => HOST [0.0.0.0]:0 -> [23.22.18.51]:443 +7.4193: Flow 13 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +7.4193: Flow 13 (TCP connection): Side 0 hash table insert: bucket: 145688 +7.4193: Flow 13 (TCP connection): TYPED -> ACTIVE +7.4193: Flow 13 (TCP connection): TAP [198.51.100.7]:39966 -> [23.22.18.51]:443 => HOST [0.0.0.0]:0 -> [23.22.18.51]:443 +7.4193: Flow 14 (NEW): FREE -> NEW +7.4193: Flow 14 (INI): NEW -> INI +7.4193: Flow 14 (INI): TAP [198.51.100.7]:46830 -> [100.31.23.131]:443 => ? +7.4193: Flow 14 (TGT): INI -> TGT +7.4193: Flow 14 (TGT): TAP [198.51.100.7]:46830 -> [100.31.23.131]:443 => HOST [0.0.0.0]:0 -> [100.31.23.131]:443 +7.4193: Flow 14 (TCP connection): TGT -> TYPED +7.4193: Flow 14 (TCP connection): TAP [198.51.100.7]:46830 -> [100.31.23.131]:443 => HOST [0.0.0.0]:0 -> [100.31.23.131]:443 +7.4193: Flow 14 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +7.4193: Flow 14 (TCP connection): Side 0 hash table insert: bucket: 20416 +7.4193: Flow 14 (TCP connection): TYPED -> ACTIVE +7.4193: Flow 14 (TCP connection): TAP [198.51.100.7]:46830 -> [100.31.23.131]:443 => HOST [0.0.0.0]:0 -> [100.31.23.131]:443 +7.4292: Flow 12 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +7.4292: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +7.4292: Flow 12 (TCP connection): timer expires in 1.000s +7.4292: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +7.4292: Flow 12 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED +7.4293: Flow 11 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +7.4293: Flow 11 (TCP connection): ACK_FROM_TAP_DUE +7.4293: Flow 11 (TCP connection): timer expires in 1.000s +7.4294: Flow 11 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +7.4294: Flow 11 (TCP connection): Side 0 hash table remove: bucket: 371710 +7.4294: Flow 11 (FREE): ACTIVE -> FREE +7.4294: Flow 11 (FREE): TAP [198.51.100.7]:55606 -> [44.193.164.157]:443 => HOST [0.0.0.0]:0 -> [44.193.164.157]:443 +7.4294: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.4294: Flow 14 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +7.4294: Flow 14 (TCP connection): ACK_FROM_TAP_DUE +7.4295: Flow 14 (TCP connection): timer expires in 1.000s +7.4295: Flow 14 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +7.4295: Flow 14 (TCP connection): Side 0 hash table remove: bucket: 20416 +7.4295: Flow 14 (FREE): ACTIVE -> FREE +7.4295: Flow 14 (FREE): TAP [198.51.100.7]:46830 -> [100.31.23.131]:443 => HOST [0.0.0.0]:0 -> [100.31.23.131]:443 +7.4296: Flow 13 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +7.4296: Flow 13 (TCP connection): ACK_FROM_TAP_DUE +7.4296: Flow 13 (TCP connection): timer expires in 1.000s +7.4296: Flow 13 (TCP connection): CLOSED: SYN_RCVD -> CLOSED +7.4296: Flow 13 (TCP connection): Side 0 hash table remove: bucket: 145688 +7.4297: Flow 13 (FREE): ACTIVE -> FREE +7.4297: Flow 13 (FREE): TAP [198.51.100.7]:39966 -> [23.22.18.51]:443 => HOST [0.0.0.0]:0 -> [23.22.18.51]:443 +7.4423: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.4423: Flow 12 (TCP connection): timer expires in 0.000s +7.4460: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +7.4460: Flow 12 (TCP connection): timer expires in 1.000s +7.4461: Flow 12 (TCP connection): STALLED +7.4461: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +7.4461: Flow 12 (TCP connection): STALLED dropped +7.4472: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +7.4472: Flow 12 (TCP connection): timer expires in 1.000s +7.4473: Flow 12 (TCP connection): STALLED +7.4473: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +7.4473: Flow 12 (TCP connection): STALLED dropped +7.4477: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.4573: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +7.4573: Flow 12 (TCP connection): STALLED +7.4605: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.4605: Flow 12 (TCP connection): timer expires in 1.000s +7.4605: Flow 12 (TCP connection): timer expires in 1.000s +7.4980: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +7.4980: Flow 12 (TCP connection): STALLED dropped +7.6732: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.6925: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.6926: Flow 12 (TCP connection): timer expires in 0.000s +7.6928: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.6960: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.6960: Flow 12 (TCP connection): timer expires in 0.000s +7.6962: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.7059: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.7059: Flow 12 (TCP connection): timer expires in 0.000s +7.7063: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.7127: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.7128: Flow 12 (TCP connection): timer expires in 0.000s +7.7130: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.7163: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.7163: Flow 12 (TCP connection): timer expires in 0.000s +7.7170: Flow 12 (TCP connection): ACK_TO_TAP_DUE +7.7234: Flow 12 (TCP connection): ACK_TO_TAP_DUE dropped +7.7235: Flow 12 (TCP connection): timer expires in 0.000s +746.9581: Flow 0 (TCP connection (spliced)): FIN_RCVD_1 +746.9582: Flow 0 (TCP connection (spliced)): FIN_SENT_0 +9.9313: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +9.9313: Flow 12 (TCP connection): timer expires in 1.000s +9.9314: Flow 12 (TCP connection): STALLED +9.9314: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +9.9314: Flow 12 (TCP connection): STALLED dropped +10.1216: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +10.1216: Flow 12 (TCP connection): timer expires in 1.000s +10.1217: Flow 12 (TCP connection): STALLED +10.1217: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +10.1217: Flow 12 (TCP connection): STALLED dropped +11.2100: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +11.2100: Flow 12 (TCP connection): timer expires in 1.000s +11.2101: Flow 12 (TCP connection): STALLED +11.2101: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +11.2101: Flow 12 (TCP connection): STALLED dropped +11.2139: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +11.2139: Flow 12 (TCP connection): timer expires in 1.000s +11.2139: Flow 12 (TCP connection): STALLED +11.2140: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +11.2140: Flow 12 (TCP connection): STALLED dropped +11.2181: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +11.2181: Flow 12 (TCP connection): timer expires in 1.000s +11.2182: Flow 12 (TCP connection): STALLED +11.2182: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +11.2182: Flow 12 (TCP connection): STALLED dropped +11.2184: Flow 12 (TCP connection): ACK_FROM_TAP_DUE +11.2184: Flow 12 (TCP connection): timer expires in 1.000s +11.2184: Flow 12 (TCP connection): STALLED +11.2184: Flow 12 (TCP connection): ACK_FROM_TAP_DUE dropped +11.2184: Flow 12 (TCP connection): STALLED dropped +11.2484: Flow 11 (NEW): FREE -> NEW +11.2484: Flow 11 (INI): NEW -> INI +11.2484: Flow 11 (INI): TAP [198.51.100.7]:56854 -> [169.254.1.1]:53 => ? +11.2484: Flow 11 (TGT): INI -> TGT +11.2484: Flow 11 (TGT): TAP [198.51.100.7]:56854 -> [169.254.1.1]:53 => HOST [0.0.0.0]:56854 -> [127.0.0.53]:53 +11.2484: Flow 11 (UDP flow): TGT -> TYPED +11.2484: Flow 11 (UDP flow): TAP [198.51.100.7]:56854 -> [169.254.1.1]:53 => HOST [0.0.0.0]:56854 -> [127.0.0.53]:53 +11.2484: Flow 11 (UDP flow): Side 0 hash table insert: bucket: 348561 +11.2484: Flow 11 (UDP flow): Side 1 hash table insert: bucket: 21593 +11.2484: Flow 11 (UDP flow): TYPED -> ACTIVE +11.2484: Flow 11 (UDP flow): TAP [198.51.100.7]:56854 -> [169.254.1.1]:53 => HOST [127.0.0.1]:56854 -> [127.0.0.53]:53 +11.2485: Flow 13 (NEW): FREE -> NEW +11.2485: Flow 13 (INI): NEW -> INI +11.2485: Flow 13 (INI): TAP [198.51.100.7]:49064 -> [169.254.1.1]:53 => ? +11.2485: Flow 13 (TGT): INI -> TGT +11.2485: Flow 13 (TGT): TAP [198.51.100.7]:49064 -> [169.254.1.1]:53 => HOST [0.0.0.0]:49064 -> [127.0.0.53]:53 +11.2485: Flow 13 (UDP flow): TGT -> TYPED +11.2485: Flow 13 (UDP flow): TAP [198.51.100.7]:49064 -> [169.254.1.1]:53 => HOST [0.0.0.0]:49064 -> [127.0.0.53]:53 +11.2485: Flow 13 (UDP flow): Side 0 hash table insert: bucket: 125764 +11.2485: Flow 13 (UDP flow): Side 1 hash table insert: bucket: 213015 +11.2485: Flow 13 (UDP flow): TYPED -> ACTIVE +11.2485: Flow 13 (UDP flow): TAP [198.51.100.7]:49064 -> [169.254.1.1]:53 => HOST [127.0.0.1]:49064 -> [127.0.0.53]:53 +11.2487: Flow 14 (NEW): FREE -> NEW +11.2487: Flow 14 (INI): NEW -> INI +0.0012: Listening address not allowed for -T 127.0.42.1/3000 diff --git a/tests/data/pasta-stderr.log b/tests/data/pasta-stderr.log new file mode 100644 index 0000000..fa7129d --- /dev/null +++ b/tests/data/pasta-stderr.log @@ -0,0 +1,81 @@ +0.0064: Template interface: wlp0s20f3 (IPv4), wlp0s20f3 (IPv6) +0.0065: Namespace interface: wlp0s20f3 +0.0065: MAC: +0.0065: host: 9a:55:9a:55:9a:55 +0.0065: NAT to host 127.0.0.1: 198.51.100.1 +0.0065: DHCP: +0.0065: assign: 198.51.100.47 +0.0065: mask: 255.255.255.0 +0.0065: router: 198.51.100.1 +0.0065: DNS: +0.0065: 198.51.100.1 +0.0065: DNS search list: +0.0065: example.net +0.0065: localdomain +0.0065: NAT to host ::1: fe80::1 +0.0065: NDP/DHCPv6: +0.0065: assign: 2001:db8::7 +0.0065: router: fe80::1 +0.0065: our link-local: fe80::1 +0.0065: DNS search list: +0.0065: example.net +0.0065: localdomain +0.0065: Inbound forwarding rules (HOST): +0.0065: TCP [*]:1-32767 => 1-32767 (best effort) (auto-scan) +0.0065: TCP [*]:61000-65535 => 61000-65535 (best effort) (auto-scan) +0.0065: UDP [*]:1-32767 => 1-32767 (best effort) (auto-scan) +0.0065: UDP [*]:61000-65535 => 61000-65535 (best effort) (auto-scan) +0.0065: Outbound forwarding rules (SPLICE): +0.0065: TCP [*]%lo:1-32767 => 1-32767 (best effort) (auto-scan) +0.0065: TCP [*]%lo:61000-65535 => 61000-65535 (best effort) (auto-scan) +0.0065: UDP [*]%lo:1-32767 => 1-32767 (best effort) (auto-scan) +0.0065: UDP [*]%lo:61000-65535 => 61000-65535 (best effort) (auto-scan) +0.0168: Sending initial ARP request for guest MAC address +0.0169: Sending initial NDP NS request for guest MAC address +0.0256: Using UDP timeout parameters, timeout: 30, stream_timeout: 120 +0.0257: Using TCP RTO parameters, syn_retries: 6, syn_linear_timeouts: 4, rto_max: 120 +0.0273: SO_PEEK_OFF supported +0.0273: TCP_INFO tcpi_snd_wnd field supported +0.0273: TCP_INFO tcpi_bytes_acked field supported +0.0273: TCP_INFO tcpi_min_rtt field supported +0.0278: ARP announcement for 198.51.100.246 / 02:00:00:00:00:01 +0.0291: NDP: received NS, sending NA +0.0686: Flow 0 (NEW): FREE -> NEW +0.0687: Flow 0 (INI): NEW -> INI +0.0687: Flow 0 (INI): TAP [2001:db8::7]:40506 -> [2001:db8:1::2]:443 => ? +0.0687: Flow 0 (TGT): INI -> TGT +0.0687: Flow 0 (TGT): TAP [2001:db8::7]:40506 -> [2001:db8:1::2]:443 => HOST [::]:0 -> [2001:db8:1::2]:443 +0.0687: Flow 0 (TCP connection): TGT -> TYPED +0.0687: Flow 0 (TCP connection): TAP [2001:db8::7]:40506 -> [2001:db8:1::2]:443 => HOST [::]:0 -> [2001:db8:1::2]:443 +0.0688: Flow 0 (TCP connection): TAP_SYN_RCVD: CLOSED -> SYN_SENT +0.0688: Flow 0 (TCP connection): Side 0 hash table insert: bucket: 76114 +0.0691: Flow 0 (TCP connection): TYPED -> ACTIVE +0.0691: Flow 0 (TCP connection): TAP [2001:db8::7]:40506 -> [2001:db8:1::2]:443 => HOST [::]:0 -> [2001:db8:1::2]:443 +0.1663: Flow 0 (TCP connection): TAP_SYN_ACK_SENT: SYN_SENT -> SYN_RCVD +0.1663: Flow 0 (TCP connection): ACK_FROM_TAP_DUE +0.1664: Flow 0 (TCP connection): timer expires in 1.000s +0.1665: Flow 0 (TCP connection): ACK_FROM_TAP_DUE dropped +0.1665: Flow 0 (TCP connection): ESTABLISHED: SYN_RCVD -> ESTABLISHED +0.1694: Flow 0 (TCP connection): ACK_TO_TAP_DUE +0.2696: Flow 0 (TCP connection): ACK_FROM_TAP_DUE +0.2697: Flow 0 (TCP connection): STALLED +0.2698: Flow 0 (TCP connection): ACK_FROM_TAP_DUE dropped +0.2698: Flow 0 (TCP connection): ACK_TO_TAP_DUE dropped +0.2698: Flow 0 (TCP connection): STALLED dropped +0.2725: Flow 0 (TCP connection): ACK_TO_TAP_DUE +0.3737: Flow 0 (TCP connection): ACK_FROM_TAP_DUE +0.3738: Flow 0 (TCP connection): STALLED +0.3935: Flow 0 (TCP connection): ACK_FROM_TAP_DUE dropped +0.3937: Flow 0 (TCP connection): STALLED dropped +0.4900: Flow 0 (TCP connection): ACK_FROM_TAP_DUE +0.4901: Flow 0 (TCP connection): STALLED +0.4912: Flow 0 (TCP connection): STALLED dropped +0.4913: Flow 0 (TCP connection): STALLED +0.4914: Flow 0 (TCP connection): ACK_FROM_TAP_DUE dropped +0.4915: Flow 0 (TCP connection): ACK_TO_TAP_DUE dropped +0.4915: Flow 0 (TCP connection): STALLED dropped +0.4917: Flow 0 (TCP connection): ACK_TO_TAP_DUE +0.4936: Flow 0 (TCP connection): TAP_FIN_RCVD: ESTABLISHED -> CLOSE_WAIT +0.4936: Flow 0 (TCP connection): ACTIVE_CLOSE +0.4937: Flow 0 (TCP connection): SOCK_FIN_SENT +0.4937: Flow 0 (TCP connection): ACK_TO_TAP_DUE dropped diff --git a/tests/fakes/pasta-bin/pasta b/tests/fakes/pasta-bin/pasta index c5ac830..20f2321 100755 --- a/tests/fakes/pasta-bin/pasta +++ b/tests/fakes/pasta-bin/pasta @@ -1,20 +1,22 @@ #!/usr/bin/env python3 """Fake pasta for testing the bw* launchers without /dev/net/tun. -Like the real one it closes inherited descriptors above 2 before opening ---log-file, creates a user namespace (mapping the caller to uid 0) and a -network namespace, starts the command in it and stays its parent, and -forwards -t (host -> namespace interface address) and -T (namespace -loopback -> host loopback) ports. Like the real one (fwd_rule.c), -T and -U -take no listening address: "-T ADDR/..." stops it with pasta's error. With ---dns-forward ADDR it answers DNS queries to ADDR:53 itself (every name is -192.0.2.10). Its debug lines (--debug --log-file) use pasta's format: a -timestamp, then "Flow N (KIND): ..." state changes and endpoints, plus timer -noise, so the launcher's condenser can be tested against them. The namespace gets a dummy interface with 192.168.77.2 -instead of a tap with the host's address. With --debug and --log-file it -writes header, startup and "Flow" lines to the log file (blocking writes, as -pasta's are). The failure mode is read from $FAKE_NET_DIR/pasta.fail -(bind: refuse every -t port). +Like the real one it closes inherited descriptors above 2, creates a user +namespace (mapping the caller to uid 0) and a network namespace, starts the +command in it and stays its parent, and forwards -t (host -> namespace +interface address) and -T (namespace loopback -> host loopback) ports. Like +the real one (fwd_rule.c), -T and -U take no listening address: "-T ADDR/..." +stops it with pasta's error. With --dns-forward ADDR it answers DNS queries +to ADDR:53 itself (every name is 192.0.2.10). The namespace gets a dummy +interface with 192.168.77.2 instead of a tap with the host's address. + +With --debug (and no --log-file, as the launchers run it) its debug lines go +to stderr in pasta's stderr format (log.c vlogmsg): "T: message", with +"Flow N (KIND): ..." state changes and endpoints plus timer noise, so the +launcher's condenser can be tested against them. Writes block, as pasta's +do. With --log-file they go to that file instead, with pasta's log file +header and level column. The failure mode is read from +$FAKE_NET_DIR/pasta.fail (bind: refuse every -t port). """ import os @@ -68,18 +70,29 @@ for opt, specs in (("T", fwd_T), ("U", [opts.get("-U", "none")])): os.closerange(3, 4096) log_fd = -1 -if "--log-file" in opts: - log_fd = os.open(opts["--log-file"], os.O_CREAT | os.O_TRUNC | os.O_APPEND | os.O_RDWR, 0o600) - os.write(log_fd, ("pasta fake-2026_10_02: %s (%d)\n" % (os.path.realpath(__file__), os.getpid())).encode()) +if "--debug" in opts: + if "--log-file" in opts: + log_fd = os.open(opts["--log-file"], os.O_CREAT | os.O_TRUNC | os.O_APPEND | os.O_RDWR, 0o600) + os.write(log_fd, ("pasta fake-2026_10_02: %s (%d)\n" % (os.path.realpath(__file__), os.getpid())).encode()) + else: + log_fd = 2 log_lock = threading.Lock() T0 = time.monotonic() _flow_ids = [] def log(msg, level=" "): - if log_fd >= 0 and "--debug" in opts: - with log_lock: - os.write(log_fd, ("%.4f: %s %s\n" % (time.monotonic() - T0, level, msg)).encode()) + if log_fd < 0: + return + if log_fd == 2: + line = "%.4f: %s\n" % (time.monotonic() - T0, msg) # stderr: no level column + else: + line = "%.4f: %s %s\n" % (time.monotonic() - T0, level, msg) + with log_lock: + try: + os.write(log_fd, line.encode()) + except OSError: + pass def ep(a, p): diff --git a/tests/test_netlog.py b/tests/test_netlog.py index d46b42a..8c2375d 100644 --- a/tests/test_netlog.py +++ b/tests/test_netlog.py @@ -3,8 +3,9 @@ python3 -m unittest discover -s tests -p 'test_*.py' tests/data/pasta-debug.log is part of a real pasta 2026_10_02 debug log -(--debug --log-file, with its stderr copy of the setup), addresses of the -host replaced with documentation addresses. +and tests/data/pasta-stderr.log is a whole short one, both as pasta writes +them to stderr with --debug and no --log-file ("T: message"), addresses of +the host replaced with documentation addresses. """ import io @@ -22,6 +23,7 @@ import bwrap_netlog as netlog # noqa: E402 FIXTURE = os.path.join(HERE, "data", "pasta-debug.log") +FIXTURE_STDERR = os.path.join(HERE, "data", "pasta-stderr.log") def condense(lines): @@ -89,11 +91,21 @@ def test_setup_and_noise_dropped(self): def test_dns_forward_flows_dropped(self): self.assertFalse(any("169.254.1.1:53" in x for x in self.out)) - def test_error_kept(self): - self.assertIn('pasta level=error msg="Listening address not allowed for -T 127.0.42.1/3000" t=0.001', self.out) + def test_only_flows_and_dns(self): + for line in self.out: + self.assertRegex(line, r"^(flow=\d+ (open|close|fail) |dns )") - def test_header(self): - self.assertEqual(self.out[0], "pasta version=2026_10_02.cba3570 pid=3962723") + def test_real_stderr_capture(self): + with open(FIXTURE_STDERR) as f: + out = condense(f.read().splitlines() + ["event=end session=x"]) + self.assertEqual( + out, + [ + "flow=0 open proto=tcp dir=out src=[2001:db8::7]:40506 dst=[2001:db8:1::2]:443 t=0.069", + "flow=0 close proto=tcp dir=out dst=[2001:db8:1::2]:443 t=0.494 secs=0.425 reason=session-end", + "event=end session=x", + ], + ) def test_names_from_dns(self): out = condense(["bwrap-dns domain=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21"] + self.raw) From f5126ee7b2e9d239004946e310482f63a11ba980 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 12 Nov 2026 12:17:13 -0500 Subject: [PATCH 29/37] refactor(bwrap): let the condenser read the netlog FIFO itself The bash supervisor read the FIFO line by line and piped each line into the Python condenser; the condenser now opens the FIFO, watches the launcher pid, prints pasta's early lines if the start fails, bridges the user's stderr when it is a pipe, writes the end record and removes the session directory. One process instead of a subshell, a process substitution and a pipe, and no bash read loop with sub-second timeouts. The launcher waits for the condenser's "ready" on a pipe before it opens its stderr on the FIFO, so nothing pasta writes is lost. The bash supervisor stays as the fallback when Python is missing. Assisted-by: opencode:claude-fable-5-1 --- docs/bw-wrappers.md | 34 ++++--- lib/bwrap_netlog.py | 203 ++++++++++++++++++++++++++++++++++++--- lib/bwrap_sandbox_lib.sh | 96 +++++++++++------- 3 files changed, 267 insertions(+), 66 deletions(-) diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 640aaec..5b570cf 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -431,7 +431,7 @@ change them, and they are the data needed for a later egress allowlist. | Record | Content | | --- | --- | -| start | `event=start session=ID launcher=... user=... uid=... pid=... backend=... flows=logged\|unavailable publish=ADDR:PORTS host_ports=... dir=...` | +| start | `event=start session=ID launcher=... user=... uid=... pid=... backend=... flows=logged\|unavailable pasta=VERSION publish=ADDR:PORTS host_ports=... dir=...` | | dns | `session=ID dns domain=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21`, plus `cname=...` when the answer went through aliases | | open | `session=ID flow=7 open proto=tcp dir=out src=ADDR:PORT dst=140.82.112.21:443 domain=github.com t=21.359` | | close | `session=ID flow=7 close proto=tcp dir=out dst=140.82.112.21:443 domain=github.com t=33.770 secs=12.411` | @@ -472,8 +472,8 @@ field, for example the domains an agent reached: never uses syslog: without `--log-file` it writes every line, timestamped, to its stderr. So pasta gets `--debug` (and not `--quiet`), with its stderr on a FIFO in the launch's [session directory](#runtime-directory), and a - supervisor started by the launcher (outside the sandbox) copies each line - to the log: + reader started by the launcher (outside the sandbox) turns the lines into + log records: - **Why stderr and not `--log-file`.** pasta opens its log file read-write (to rotate it). On SELinux hosts (Fedora, RHEL) pasta runs in its own domain, `pasta_t`, which passt's policy lets only *append* to FIFOs @@ -483,24 +483,28 @@ field, for example the domains an agent reached: allowed, is silently replaced with `/dev/null` at the exec into `pasta_t`, so the launcher opens its stderr with `>>` and keeps no other descriptor on the FIFO across the exec.) - - The supervisor holds the FIFO open read-write, so it never sees - end-of-file before pasta writes and the launcher's write-only open does - not block. It stops when pasta has exited, after draining the pipe, then - writes the end record and removes the session directory. + - The reader (`lib/bwrap_netlog.py condense`) opens the FIFO first; the + launcher waits for its "ready" before it opens its stderr on it, so the + open does not block and nothing is lost. The reader ignores end-of-file + and stops only when the launcher process, which is pasta by then, is + gone (it is no longer the reader's parent), after draining the pipe. Then + it writes the end record and removes the session directory. - If the tool dies, it goes on reading and drops the lines, so pasta never blocks on it. - pasta prints its setup first, and if it fails to start, its error. The descriptor relay (pasta's command) prints a marker on the FIFO and reopens your stderr: by path when it is a terminal or a file, else (a pipe) - through a second FIFO that the supervisor copies. If pasta stops before - the marker, the supervisor prints pasta's lines on your terminal, prefixed - `: pasta:`, just after the launcher has exited. + through a second FIFO that the reader copies. If pasta stops before the + marker, the reader prints pasta's lines on your terminal, prefixed + `pasta:`, just after the launcher has exited. - **Condensed.** pasta's debug log has about 34 lines per connection (state - changes, timers, ACK and buffer bookkeeping) and its setup dump. The - supervisor sends it through `lib/bwrap_netlog.py condense`, which keeps one - line when a connection opens and one when it closes and drops the rest (in - a real session, 5000 lines became 280). It writes to `systemd-cat` / - `logger` itself. + changes, timers, ACK and buffer bookkeeping) and its setup dump. The reader + keeps one line when a connection opens and one when it closes and drops + the rest (in a real session, 5000 lines became 280). It writes to + `systemd-cat` / `logger` itself. pasta's version is in the start record + (`pasta=`), from `pasta --version`. + - Without Python a bash supervisor copies pasta's lines to the log as they + are. - **DNS relay (pasta).** To name destinations, `lib/bwrap_netlog.py dns` listens on `127.53.0.1:53` (UDP and TCP) in the sandbox's network namespace, forwards each lookup to `169.254.1.1` (pasta's DNS forward) and writes one diff --git a/lib/bwrap_netlog.py b/lib/bwrap_netlog.py index 6f84711..c6693bb 100644 --- a/lib/bwrap_netlog.py +++ b/lib/bwrap_netlog.py @@ -3,13 +3,17 @@ "Network log"). Standard library only; runs on Python 3.6 (RHEL 8 platform-python) and later. - bwrap_netlog.py condense --session ID [--dns-forward ADDR] -- CMD... - Read pasta's debug output (its stderr, and the DNS relay's lines) on - standard input and write one line when a connection opens and one - when it closes, with the domain of the destination when the relay saw - it, to CMD's standard input (systemd-cat or logger). Everything else - pasta writes at debug level (timers, ACK and buffer bookkeeping, state - changes, its setup dump) is dropped. + bwrap_netlog.py condense --session ID [--dns-forward ADDR] + [--fifo PATH --parent PID [--marker TEXT] [--end-record TEXT] + [--session-dir DIR]] -- CMD... + Read pasta's debug output (its stderr, and the DNS relay's lines) and + write one line when a connection opens and one when it closes, with + the domain of the destination when the relay saw it, to CMD's + standard input (systemd-cat or logger). Everything else pasta writes + at debug level (timers, ACK and buffer bookkeeping, state changes, + its setup dump) is dropped. Without --fifo it filters standard + input; with it, it reads the FIFO (see the launcher's "Network log") + until the launcher, PID, has exited, then writes the end record. bwrap_netlog.py dns LISTEN UPSTREAM PARENT_PID A DNS relay for the sandbox's network namespace: listen on LISTEN @@ -185,23 +189,149 @@ def finish(self): self.flows.clear() +def _parent_is(pid): + """True while PID is our parent: then it is alive and its pid cannot have + been reused (the launcher keeps its pid through exec; when it exits we + are re-parented).""" + return os.getppid() == pid + + +class FifoReader: + """Read lines from a FIFO without ever blocking its writer. + + The FIFO is opened read-only and non-blocking: a read-only reader sees + EOF whenever no writer has it open, which happens before the launcher + opens its stderr on it and, briefly, if pasta is ever without the fd. + So EOF is not the end: `lines()` keeps polling until `done()` says the + session is over, and then drains what is left. The launcher opens its + end only after this reader exists (it is told, see `ready`), so its + write-only open does not block. + """ + + def __init__(self, path, done): + import select + + self.fd = os.open(path, os.O_RDONLY | os.O_NONBLOCK) + self.done = done + self.poll = select.poll() + self.poll.register(self.fd, select.POLLIN) + self.buf = b"" + + def _read(self, timeout_ms): + """Return the bytes available within TIMEOUT_MS; b"" when none. + Waits out TIMEOUT_MS even while the FIFO reports hang-up (no writer), + which poll() would otherwise report at once, over and over.""" + import time + + deadline = time.monotonic() + timeout_ms / 1000.0 + while True: + left = deadline - time.monotonic() + if left <= 0: + return b"" + if self.poll.poll(int(left * 1000)): + try: + data = os.read(self.fd, 65536) + except (BlockingIOError, InterruptedError): + data = b"" + if data: + return data + time.sleep(min(0.05, max(left, 0))) # hang-up: no writer yet, or none left + + def lines(self, poll_ms=1000): + """Yield complete lines (without the newline) until the session is + over and the FIFO is drained.""" + while True: + data = self._read(poll_ms) + if data: + self.buf += data + while b"\n" in self.buf: + line, _, self.buf = self.buf.partition(b"\n") + yield line.decode("utf-8", "replace") + continue + if self.done(): + break + # The writers are gone; whatever they left is read at once. + while True: + data = self._read(200) + if not data: + break + self.buf += data + for line in self.buf.split(b"\n"): + if line: + yield line.decode("utf-8", "replace") + self.buf = b"" + os.close(self.fd) + + +def _bridge_stderr(path, stop): + """Copy PATH (a FIFO the relay opens as the sandbox's stderr when the + user's is a pipe) to our stderr until STOP is set and it is drained.""" + reader = FifoReader(path, stop.is_set) + for line in reader.lines(): + try: + sys.stderr.write(line + "\n") + sys.stderr.flush() + except (OSError, ValueError): + pass + + def condense(argv): + """Condense pasta's debug log into the system log. + + Run by the launcher in the background, before it opens its stderr on the + FIFO and execs pasta. It reads the FIFO itself, writes one record per + connection to CMD (systemd-cat or logger), writes the end record once the + launcher (then pasta) has exited, and removes the session directory. + The relay prints MARKER on pasta's stderr when the sandbox is about to + start; pasta's lines before it are its setup and, if it fails, its error: + they go to the terminal (this process's stderr) when the launcher exits + without the marker ever arriving. + """ import subprocess session, dns_forward, cmd = "", "169.254.1.1", [] + fifo, parent, marker, end_record, session_dir, stderr_fifo = None, None, None, None, None, None i = 0 while i < len(argv): - if argv[i] == "--session": + a = argv[i] + if a == "--session": session = argv[i + 1] - i += 2 - elif argv[i] == "--dns-forward": + elif a == "--dns-forward": dns_forward = argv[i + 1] - i += 2 - elif argv[i] == "--": + elif a == "--fifo": + fifo = argv[i + 1] + elif a == "--parent": + parent = int(argv[i + 1]) + elif a == "--marker": + marker = argv[i + 1] + elif a == "--end-record": + end_record = argv[i + 1] + elif a == "--session-dir": + session_dir = argv[i + 1] + elif a == "--stderr-fifo": + stderr_fifo = argv[i + 1] + elif a == "--": cmd = argv[i + 1 :] break else: - sys.exit("bwrap_netlog condense: unknown argument %r" % argv[i]) + sys.exit("bwrap_netlog condense: unknown argument %r" % a) + i += 2 + if fifo is not None and parent is None: + sys.exit("bwrap_netlog condense: --fifo needs --parent") + + # Signals meant for the session (Ctrl-C on the terminal, a hangup, a kill + # of the process group) must not cost the end record: this process ends + # by itself once the launcher has gone. + for sig in (signal.SIGINT, signal.SIGQUIT, signal.SIGHUP, signal.SIGTERM, signal.SIGPIPE): + try: + signal.signal(sig, signal.SIG_IGN) + except (OSError, ValueError): + pass + try: + signal.signal(signal.SIGTSTP, signal.SIG_IGN) + except (OSError, ValueError, AttributeError): + pass + sink = None if cmd: sink = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) @@ -221,18 +351,61 @@ def emit(text, prefix=True): state["dead"] = True c = Condenser(emit, dns_forward) - for raw in sys.stdin.buffer: + + def feed(raw): try: - c.line(raw.decode("utf-8", "replace")) + c.line(raw) except Exception as e: # never stop reading pasta's log emit("condense level=error msg=%s" % _value(repr(e))) + + if fifo is None: + # Plain filter: standard input to the log (tests, by hand). + for raw in sys.stdin.buffer: + feed(raw.decode("utf-8", "replace")) + else: + reader = FifoReader(fifo, lambda: not _parent_is(parent)) + bridge, bridge_stop = None, threading.Event() + if stderr_fifo: + bridge = threading.Thread(target=_bridge_stderr, args=(stderr_fifo, bridge_stop)) + bridge.daemon = True + bridge.start() + # The launcher waits for this before it opens its stderr on the FIFO. + try: + os.write(3, b"ready\n") + os.close(3) + except OSError: + pass + started, early = marker is None, [] + for raw in reader.lines(): + if not started: + if raw == marker: + started = True + early = [] + continue + if len(early) < 200: + early.append(raw) + feed(raw) + if not started: + # pasta stopped before starting the sandbox: show what it said. + for raw in early: + sys.stderr.write("pasta: %s\n" % raw) + sys.stderr.flush() + if bridge: + bridge_stop.set() + bridge.join(5) c.finish() + if end_record: + emit(end_record, prefix=False) if sink: try: sink.stdin.close() except OSError: pass sink.wait() + if session_dir: + import shutil + + shutil.rmtree(session_dir, ignore_errors=True) # ── dns relay ─────────────────────────────────────────────────────────── diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index f5f9061..b9595c1 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -2610,10 +2610,12 @@ _net_session_dir() { # pasta at debug level never uses syslog: without --log-file it writes every # line, timestamped, to its stderr (log.c vlogmsg). So with pasta: # -# launcher ─┬─ supervisor (_net_log_supervisor, background, outside the sandbox) -# │ reads the netlog FIFO line by line and writes each line, -# │ prefixed with the session, to the tool; at the end writes -# │ the end record and removes the session directory +# launcher ─┬─ condenser (bwrap_netlog.py condense, background, outside the +# │ sandbox): reads the netlog FIFO, writes one record per +# │ connection to the tool; once the launcher (pasta) has gone +# │ writes the end record and removes the session directory. +# │ Without Python: _net_log_supervisor, which copies pasta's +# │ lines as they are. # └─ exec env -i ... pasta --debug ... 2>>FIFO # -- relay (prints a marker, reopens its stderr) -- bwrap ... # @@ -2625,19 +2627,19 @@ _net_session_dir() { # write one (2<>) would be silently replaced with /dev/null at the exec # into pasta_t, and so would any other read-write descriptor on the FIFO # left open across the exec: the launcher closes its hold before it. -# - The supervisor holds the FIFO open read-write, so it never sees EOF -# early, and so the launcher's write-only open does not block. It stops -# reading when the launcher process (pasta) is gone, after draining what -# is left. pasta keeps descriptors 0-2 across its own sandboxing (it -# closes from 3 up). +# - The reader opens the FIFO first (the launcher waits for its "ready"), +# so the launcher's write-only open does not block, and the reader +# ignores end-of-file: it stops only when the launcher process is gone, +# after draining what is left. pasta keeps descriptors 0-2 across its +# own sandboxing (it closes from 3 up). # - It never blocks pasta: if the tool dies it goes on reading and drops the # lines. # - pasta's startup lines would otherwise print on the terminal; the relay # prints a marker on the FIFO and then reopens the user's stderr by path # (a terminal or a file), or through a second FIFO that the supervisor # copies to it (a pipe). If pasta exits before the marker (a failed -# start), the supervisor prints its lines on the terminal, so its errors -# are not lost. +# start), the reader prints its lines on the terminal, so its errors are +# not lost. # With slirp4netns or --share-net only the start and end records are written # (slirp4netns has no flow log); the supervisor just waits for the end. @@ -2655,6 +2657,7 @@ _NET_LOG_FLOWS=0 _NET_LOG_FIFO="" _NET_LOG_ERR_FIFO="" _NET_LOG_MARKER="" +_NET_LOG_PASTA_VERSION="" # _net_log_detect # List the usable logging tools, best first (_NET_LOG_TOOLS: systemd-cat @@ -2714,6 +2717,7 @@ _net_log_record() { else _r+=" flows=unavailable" fi + [[ -z "${_NET_LOG_PASTA_VERSION}" ]] || _r+=" pasta=${_NET_LOG_PASTA_VERSION}" [[ -z "${_NET_PUBLISH_ADDR_EFFECTIVE}" ]] || _r+=" publish=${_NET_PUBLISH_ADDR_EFFECTIVE}:$(_net_join ${NET_PUBLISH[@]+"${NET_PUBLISH[@]}"})" for _i in ${NET_HOST_PORTS[@]+"${NET_HOST_PORTS[@]}"}; do _hp+="${_hp:+,}${_i}"; done [[ -z "${_hp}" ]] || _r+=" host_ports=${_hp}" @@ -2725,10 +2729,15 @@ _net_log_record() { # _net_log_start # Called by launch_sandbox just before it execs: write the start record # (which checks the tool; without one, warn and log nothing), then with -# pasta add --debug and the stderr hand-over, and start the supervisor. +# pasta add --debug and the stderr hand-over, and start the reader (the +# condenser, or without Python the supervisor), waiting until it has the +# FIFO open. _net_log_start() { - local _err _launcher="${0##*/}" _t + local _err _launcher="${0##*/}" _t _ready _rfd _NET_LOG_TOOL="" + if [[ "${_NET_BACKEND}" == pasta ]]; then + _NET_LOG_PASTA_VERSION="$("${_PASTA_BIN}" --version 2> /dev/null | awk 'NR == 1 { print $2 }' || true)" + fi for _t in ${_NET_LOG_TOOLS[@]+"${_NET_LOG_TOOLS[@]}"}; do _NET_LOG_TOOL="${_t}" _net_log_cmd @@ -2763,7 +2772,7 @@ _net_log_start() { done PASTA_ARGS=("${PASTA_ARGS[@]}" --debug) # The relay restores the user's stderr: by path when it is a terminal - # or a file, else through a FIFO the supervisor copies. + # or a file, else through a FIFO the reader copies. _err="$(readlink "/proc/$$/fd/2" 2> /dev/null || true)" if [[ "${_err}" != /* || "${_err}" == *" (deleted)" ]] || { [[ ! -c "${_err}" && ! -f "${_err}" ]]; }; then @@ -2779,12 +2788,36 @@ _net_log_start() { fi _BWRAP_FD_RELAY=("${_pre[@]}" 2 append "${_err}" ${_BWRAP_FD_RELAY[@]+"${_BWRAP_FD_RELAY[@]}"}) fi + + if [[ "${_NET_LOG_FLOWS}" -eq 1 && -n "${_NET_LOG_PY}" ]]; then + # The condenser reads the FIFO. It says "ready" on descriptor 3 + # once it has it open, so that nothing pasta writes is lost; its + # stderr is the user's, for pasta's lines if pasta fails to start. + local -a _cond=("${_NET_LOG_PY}" -I -S "${_NET_LOG_HELPER}" condense + --session "${_NET_LOG_SESSION}" --dns-forward "${_NET_DNS_FORWARD_ADDR}" + --fifo "${_NET_LOG_FIFO}" --parent "$$" --marker "${_NET_LOG_MARKER}" + --end-record "$(_net_log_record end)" --session-dir "${_NET_SESSION_DIR}") + [[ -z "${_NET_LOG_ERR_FIFO}" ]] || _cond+=(--stderr-fifo "${_NET_LOG_ERR_FIFO}") + exec {_rfd}< <(exec setsid "${_cond[@]}" -- "${_NET_LOG_CMD[@]}" 3>&1 > /dev/null < /dev/null) + IFS= read -r -t 10 _ready <&"${_rfd}" || _ready="" + exec {_rfd}<&- + if [[ "${_ready}" != ready ]]; then + printf 'Warning: %s: the network log condenser did not start; connections are not\n' "${_launcher}" >&2 + printf ' logged for this session.\n' >&2 + # Keep pasta's stderr off a FIFO no one reads. + _NET_LOG_FIFO="" + _NET_LOG_FLOWS=0 + fi + return 0 + fi _net_log_supervisor & } # _net_log_supervisor -# The background half of the network log (see "Network log"). Runs in a -# subshell; $$ is still the launcher, which is pasta (or bwrap) by then. +# The background half of the network log without Python (see "Network +# log"): copies pasta's lines as they are, prefixed with the session, or +# without pasta just waits for the end. Runs in a subshell; $$ is still +# the launcher, which is pasta (or bwrap) by then. _net_log_supervisor() { local _sink _prefix _fifo _line="" _part="" _rc _started=0 _dead=0 _bridge="" _i local -a _early=() @@ -2794,16 +2827,8 @@ _net_log_supervisor() { # the launcher has gone. trap '' INT QUIT TSTP HUP TERM PIPE exec < /dev/null > /dev/null - if [[ -n "${_NET_LOG_PY}" ]]; then - # The condenser writes session=ID itself. - exec {_sink}> >(exec setsid "${_NET_LOG_PY}" -I -S "${_NET_LOG_HELPER}" condense \ - --session "${_NET_LOG_SESSION}" --dns-forward "${_NET_DNS_FORWARD_ADDR}" \ - -- "${_NET_LOG_CMD[@]}" > /dev/null 2>&1) - _prefix="" - else - exec {_sink}> >(exec setsid "${_NET_LOG_CMD[@]}" > /dev/null 2>&1) - _prefix="session=${_NET_LOG_SESSION} " - fi + exec {_sink}> >(exec setsid "${_NET_LOG_CMD[@]}" > /dev/null 2>&1) + _prefix="session=${_NET_LOG_SESSION} " if [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then # Read-write, so end-of-file is never seen before pasta writes. This @@ -2901,18 +2926,17 @@ _net_log_print_dry_run() { "${_NET_LOG_SESSION}" "${_NET_LOG_CMD[*]}" if [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then printf '# and pasta gets --debug (not --quiet) with its stderr on /netlog,\n' - printf '# a FIFO; a supervisor copies each line to the log and the relay first reopens\n' - printf '# your stderr. The end record is written when pasta exits.\n' + printf '# a FIFO; the relay first reopens your stderr. The end record is written when\n' + printf '# pasta exits.\n' if [[ -n "${_NET_LOG_PY}" ]]; then - printf '# The lines are condensed (one per connection opened and closed) by -' - printf '# %s -I %s condense -' "${_NET_LOG_PY}" "${_NET_LOG_HELPER}" - printf '# and the relay first starts the DNS relay on %s:53 in the namespace: -' "${_NET_DNS_RELAY_ADDR}" - printf '# %s -I %s dns %s %s -' "${_NET_LOG_PY}" "${_NET_LOG_HELPER}" \ + printf '# The FIFO is read, and the lines condensed (one per connection opened and\n' + printf '# closed), by\n' + printf '# %s -I %s condense --fifo /netlog ...\n' "${_NET_LOG_PY}" "${_NET_LOG_HELPER}" + printf '# and the relay first starts the DNS relay on %s:53 in the namespace:\n' "${_NET_DNS_RELAY_ADDR}" + printf '# %s -I %s dns %s %s \n' "${_NET_LOG_PY}" "${_NET_LOG_HELPER}" \ "${_NET_DNS_RELAY_ADDR}" "${_NET_DNS_FORWARD_ADDR}" + else + printf '# A supervisor copies each line to the log as it is.\n' fi else printf '# and a supervisor writes the end record when the sandbox exits (no flow log\n' From d73a2ec0c63b52fd755b4a763c5eba7c8d90682a Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 12 Nov 2026 22:22:50 -0500 Subject: [PATCH 30/37] fix(bwrap): run slirp4netns sandboxes under an identity map RHEL 8 ships bubblewrap 0.4.0, which cannot start in a user namespace that maps only uid 0: drop_privs() tests geteuid() == 0 instead of is_privileged (fixed in 0.4.1), calls setuid() on the real uid, which that namespace does not map, and dies with "unable to drop root uid: Invalid argument". unshare --map-root-user built exactly that namespace, so every slirp4netns launch failed. The launcher now unshares and writes the id maps itself (_NET_NS_IDENTITY, Python standard library), mapping the real uid and gid to themselves; RHEL 8's util-linux (2.32) has no --map-user. The backend needs python3 or platform-python instead of unshare. slirp4netns --enable-sandbox does setegid(0) after joining the namespace, so it is no longer passed; --enable-seccomp still is. An unprivileged process may write a single map line, so the namespace maps uid 0 or the real uid, not both. pasta also maps the caller to uid 0 alone: refuse it, with the reason, when bwrap is older than 0.4.1, instead of failing at exec. The fake slirp4netns execs ip(8) after joining the namespace, and execve clears capabilities under a non-root map; raise them into the ambient set, as the real one configures the interface in-process. Remove the fake unshare, which nothing runs any more. Assisted-by: opencode:claude-opus-5-5 --- docs/bw-wrappers.md | 89 +++++++++++++---- lib/bwrap_sandbox_lib.sh | 184 ++++++++++++++++++++++++++++------- tests/fakes/_nsproxy.py | 28 ++++++ tests/fakes/bin/unshare | 35 ------- tests/test_netns_identity.py | 112 +++++++++++++++++++++ 5 files changed, 358 insertions(+), 90 deletions(-) delete mode 100755 tests/fakes/bin/unshare create mode 100644 tests/test_netns_identity.py diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 5b570cf..13553a8 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -18,13 +18,14 @@ option list; the tables below summarise the common surface. - `bubblewrap` (`bwrap`) >= 0.4.0 - binary masking is verified on RHEL 8.10 (`bubblewrap-0.4.0-3.el8_10`) and EL9 (0.6.3) - For the private network namespace, one of: `pasta` (package `passt`, - RHEL 9 and later), or `slirp4netns` >= 0.4.0 with libslirp >= 4.3.0 - (package `slirp4netns`, RHEL 8 AppStream) and util-linux `unshare`; plus - `/dev/net/tun` and unprivileged user namespaces on the host. Without them - the wrappers stop (exit status 3) unless run with `--share-net` (see - [Network](#network)). `ss` (iproute) is used to find busy ports for - `--publish`; with slirp4netns, `--publish` also needs `python3` (or RHEL 8's - `/usr/libexec/platform-python`). + RHEL 9 and later) **with bwrap >= 0.4.1** (see + [Implementation notes: pasta](#implementation-notes-pasta)), or + `slirp4netns` >= 0.4.0 with libslirp >= 4.3.0 (package `slirp4netns`, + RHEL 8 AppStream) and `python3` (or RHEL 8's + `/usr/libexec/platform-python`); plus `/dev/net/tun` and unprivileged user + namespaces on the host. Without them the wrappers stop (exit status 3) + unless run with `--share-net` (see [Network](#network)). `ss` (iproute) is + used to find busy ports for `--publish`. - For the [network log](#network-log): `systemd-cat` (systemd) with a running journal, or `logger` (util-linux) with `/dev/log`. Without either the wrappers warn and run without the log. Python 3.6+ (standard library) to @@ -541,6 +542,13 @@ field, for example the domains an agent reached: - pasta maps you to uid 0 in its user namespace, so bwrap runs the agent in a nested user namespace as your real uid and gid (`--unshare-user --uid --gid`); file ownership looks as before and the agent has no capabilities. +- **pasta needs bwrap 0.4.1 or later** and the launcher checks before using + it. In 0.4.0 `drop_privs()` tests `geteuid() == 0` instead of "installed + setuid root", so inside pasta's namespace — where uid 0 is the only mapped + id — it calls `setuid()` on an unmapped id and dies with + `unable to drop root uid: Invalid argument`. 0.4.1 changed the guard to + `is_privileged`. RHEL 8 ships 0.4.0, but also has no pasta, so in practice + this only bites a hand-built pasta or a backported one. - pasta renames the host to `pasta-`; bwrap's `--unshare-uts --hostname` restores the real name. - `--no-pidns` is passed when the installed pasta supports it (2026-09 and @@ -558,28 +566,69 @@ foreground, so the terminal, job control, the exit status and ```text launcher ─┬─ helper (background): waits for the namespace, starts slirp4netns, │ adds the --publish forwards, then releases the sandbox - └─ exec env -i ... unshare --user --map-root-user --net -- + └─ exec env -i ... python -I -c <_NET_NS_IDENTITY> bash -- bwrap --unshare-user --uid --gid --info-fd --block-fd --sync-fd ... -- ``` - **The tool does not start before the network is ready**: bwrap waits on `--block-fd` until the helper writes to it. -- **`unshare`, not bwrap's `--unshare-net`, creates the namespaces.** Because - bwrap mounts `/dev`, it nests the sandbox in a second user namespace inside - the one that owns the network namespace. slirp4netns joins the user +- **The launcher, not bwrap's `--unshare-net`, creates the namespaces.** + Because bwrap mounts `/dev`, it nests the sandbox in a second user namespace + inside the one that owns the network namespace. slirp4netns joins the user namespace of the process it is given, so it would have no rights over the - network. With `unshare` the launcher process itself holds a single user - namespace that owns the network namespace, and slirp4netns attaches to it. - `unshare` maps you to uid 0 there, so bwrap runs the agent as your real uid - and gid, as with pasta. + network. Unsharing in the launcher leaves its own process holding a single + user namespace that owns the network namespace, and slirp4netns attaches to + it. +- **The ids are mapped to themselves, not to root.** The launcher unshares and + writes the maps itself, in about fifteen lines of Python + (`_NET_NS_IDENTITY`), rather than calling `unshare --user --map-root-user + --net`, for two reasons: + - RHEL 8 is the only platform this backend exists for, and its bwrap is + 0.4.0, which cannot start under a root-only map at all (see the pasta + notes above). An identity map leaves your uid mapped, so bwrap's + `--uid`/`--gid` name an id that exists. + - Nothing in the sandbox runs as uid 0, not even for the moment before bwrap + switches back. + + util-linux grew `--map-user`/`--map-current-user` in 2.38 and could do this + directly, but RHEL 8 has 2.32, whose `unshare` offers only + `--map-root-user`. Writing a single-line map of your own id needs no + privilege, so this needs neither `newuidmap`/`newgidmap` nor `/etc/subuid` + entries — unlike rootless podman, which uses both to build a much wider map + and then runs the payload as uid 0 inside it. slirp4netns still gets full + capabilities when it joins, because the namespace is owned by your uid, + which is the uid it runs as. +- Python (`python3`, or RHEL 8's `/usr/libexec/platform-python`) is therefore + required for this backend, not only for `--publish`. Standard library only, + 3.6 or later. - slirp4netns options: `--disable-host-loopback` (otherwise the gateway address `192.168.254.2` reaches the host's `127.0.0.1` on every port), `--cidr=192.168.254.0/24` (slirp's default, `10.0.2.0/24`, lies inside BNL's 10/8), the built-in DNS at `.3` (forwards port 53 only, with libslirp 4.3.0 and later, which the launcher checks), `--mtu=65520`, and - `--enable-sandbox --enable-seccomp` when `/etc/resolv.conf` resolves inside - `/etc` or `/run`. + `--enable-seccomp` when `/etc/resolv.conf` resolves inside `/etc` or + `/run`. +- **`--enable-sandbox` is not passed**, and this is the cost of the identity + map. After joining the launcher's namespace, slirp4netns does + `setegid(0)`/`seteuid(0)` (`main.c`); with no uid 0 mapped that fails and + the launch stops with `setegid(0)` / `parent failed`. An unprivileged + process may write only one line into `uid_map` — the line mapping its own + id — so the namespace can map uid 0 *or* your uid, not both: + + | map | slirp4netns `--enable-sandbox` | bwrap 0.4.0 | + | --- | --- | --- | + | uid 0 only | works | dies, unless the agent runs as uid 0 | + | your uid (what we do) | cannot `setegid(0)` | works, agent keeps its uid | + + Your uid wins: it is what the agent runs as and what every file it writes + is owned by, whereas `--enable-sandbox` only hardens slirp4netns itself (a + mount namespace pivot-rooted into an empty directory, plus dropping + capabilities). `--enable-seccomp`, which bounds what a compromised libslirp + can call, is a separate flag with no id requirement and is still passed. + Mapping both ids needs a multi-line map, which takes `newuidmap`/`newgidmap` + and an `/etc/subuid` entry — the way rootless podman does it, and the way to + restore `--enable-sandbox` if it is wanted. - slirp4netns runs in its own session (Ctrl-C does not reach it) with a scrubbed environment (no API keys). It exits when bwrap does: its `--exit-fd` is the other end of bwrap's `--sync-fd`. @@ -593,8 +642,8 @@ launcher ─┬─ helper (background): waits for the namespace, starts slirp4ne the tool never runs, and then bwrap. The exit status is then 137, not 3. The helper signals a process only after checking that it is the launcher (the helper's own parent) or a child of the launcher. If the launcher is - already gone (`unshare` or the descriptor relay failed and printed why), the - helper notices within a poll interval, signals nothing, and exits. + already gone (the unshare or the descriptor relay failed and printed why), + the helper notices within a poll interval, signals nothing, and exits. ### Runtime directory @@ -624,7 +673,7 @@ credentials and config (`bwclaude`, `bwcopilot`, `bwcodex`), `--ro-bind-data` for generated files (the context file, `/etc/resolv.conf`, `/etc/hosts`). pasta closes every inherited descriptor above 2, so the launcher does not open them itself. It reserves their numbers; a short fixed bash script (the relay, -`_BWRAP_FD_RELAY_SHIM`) runs as the command of pasta or `unshare`, opens each descriptor at its +`_BWRAP_FD_RELAY_SHIM`) runs as the command of pasta or of the unshare step, opens each descriptor at its number, from the host file or from generated text carried in an environment variable that it then unsets, and execs bwrap. Nothing is staged on the host. diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index b9595c1..bdbdcf2 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -36,7 +36,7 @@ # Requires: bwrap >= 0.4.0 (RHEL 8) # Binary masking is verified on RHEL 8.10 (bubblewrap-0.4.0-3.el8_10) and EL9 (0.6.3). # Network: a private network namespace from pasta (package passt, RHEL 9+), -# else slirp4netns (RHEL 8) with util-linux unshare; both need /dev/net/tun +# else slirp4netns (RHEL 8) with python3; both need /dev/net/tun # and unprivileged user namespaces. Without either the launch stops # (exit 3) unless --share-net is given (see "Network isolation"). @@ -224,6 +224,37 @@ _version_ge() { [[ "$(printf '%s\n%s\n' "${a}" "${b}" | sort -V | head -n1)" == "${b}" ]] } +# Lowest bwrap usable under a user namespace that maps the caller to uid 0 and +# maps nothing else (what pasta builds, and what util-linux `unshare +# --map-root-user` writes). bubblewrap 0.4.0's drop_privs() is +# +# if (geteuid () == 0 && setuid (opt_sandbox_uid) < 0) +# die_with_error ("unable to drop root uid"); +# +# which mistakes "root in a user namespace" for "installed setuid root": it +# calls setuid() on an id that namespace does not map, gets EINVAL +# and dies with "unable to drop root uid: Invalid argument" before the sandbox +# starts. 0.4.1 changed the guard to is_privileged. RHEL 8 ships 0.4.0, one +# patch release short, so this has to be checked and not assumed. +_BWRAP_MIN_VERSION_MAPROOT=0.4.1 + +# _bwrap_version +# Print the version of the bwrap that will be used ("0.4.0"), nothing and +# return 1 if it cannot be determined. Cached: bwrap is resolved and run +# once per launch. +_BWRAP_VERSION="" +_bwrap_version() { + local _bin _out + if [[ -z "${_BWRAP_VERSION}" ]]; then + _bin="$(resolve_bwrap_bin)" || return 1 + [[ -n "${_bin}" && -x "${_bin}" ]] || return 1 + _out="$("${_bin}" --version 2> /dev/null)" || return 1 + [[ "${_out}" =~ bubblewrap\ ([0-9][0-9.]*) ]] || return 1 + _BWRAP_VERSION="${BASH_REMATCH[1]}" + fi + printf '%s' "${_BWRAP_VERSION}" +} + # Detect kernel-level security features that affect bwrap argument choice. # # TIOCSTI capability guard (Linux 5.14+): @@ -2473,13 +2504,27 @@ detect_net_capabilities() { # assumed to work. _NET_TRY_REASON="" _net_try_pasta() { - local _out _rc + local _out _rc _bver _NET_TRY_REASON="" _PASTA_BIN="$(command -v pasta 2> /dev/null || true)" if [[ -z "${_PASTA_BIN}" || ! -x "${_PASTA_BIN}" ]]; then _NET_TRY_REASON="not found on PATH (package passt)" return 1 fi + # pasta builds the user namespace itself and maps the caller to uid 0 in + # it, so bwrap has to switch back to the real uid: see + # _BWRAP_MIN_VERSION_MAPROOT. Unlike the slirp4netns backend, which maps + # the ids itself, there is nothing to work around here, so refuse the + # backend rather than fail at exec with bwrap's obscure message. + _bver="$(_bwrap_version 2> /dev/null || true)" + if [[ -z "${_bver}" ]]; then + _NET_TRY_REASON="the bwrap version could not be read ('bwrap --version'; ${_BWRAP_MIN_VERSION_MAPROOT} or later is needed)" + return 1 + fi + if ! _version_ge "${_bver}" "${_BWRAP_MIN_VERSION_MAPROOT}"; then + _NET_TRY_REASON="bwrap ${_bver} cannot run in pasta's root-mapped user namespace (${_BWRAP_MIN_VERSION_MAPROOT} or later is needed)" + return 1 + fi # --no-pidns (2026-09) is not in RHEL 9 / EPEL builds; without it pasta # adds a PID namespace, inside which bwrap nests its own. if _out="$(timeout --signal=TERM --kill-after=2s 5s "${_PASTA_BIN}" --help 2>&1)"; then @@ -2956,7 +3001,7 @@ _net_log_print_dry_run() { # │ namespace, starts `setsid slirp4netns ... $$ tap0`, waits # │ for it to be ready, adds the --publish forwards through its # │ API socket, then releases the sandbox (--block-fd) and exits -# └─ exec env -i ... unshare --user --map-root-user --net -- +# └─ exec env -i ... python -I -c <_NET_NS_IDENTITY> # bash -- bwrap --unshare-user --uid U --gid G # --info-fd 90 --block-fd 91 --sync-fd 92 ... -- tool # @@ -2965,14 +3010,14 @@ _net_log_print_dry_run() { # user's shell, so killing bwrap alone would leave the sandbox, which the # closed --block-fd would then release. # -# The namespaces come from unshare (util-linux), not from bwrap's -# --unshare-net: bwrap mounting /dev (devpts) puts the sandbox in a second -# user namespace nested in the one that owns the network namespace, and -# slirp4netns, which joins the target process's user namespace, would then -# have no rights over the network. With unshare the launcher's own process -# holds a single-level user namespace that owns the network namespace. It is -# mapped to uid 0 (as with pasta), so bwrap runs the sandbox as the real -# user with --unshare-user --uid/--gid. +# The namespaces come from the launcher itself (_NET_NS_IDENTITY), not from +# bwrap's --unshare-net: bwrap mounting /dev (devpts) puts the sandbox in a +# second user namespace nested in the one that owns the network namespace, +# and slirp4netns, which joins the target process's user namespace, would +# then have no rights over the network. Unsharing in the launcher leaves its +# own process holding a single-level user namespace that owns the network +# namespace. The real uid and gid are mapped to themselves there, so bwrap's +# --unshare-user --uid/--gid are the ids the sandbox already has. # # The tool does not start before the network is ready. slirp4netns runs in # its own session (Ctrl-C does not reach it) with a scrubbed environment, and @@ -2985,14 +3030,16 @@ _net_log_print_dry_run() { # resolver such as systemd-resolved keeps working. # --cidr 192.168.254.0/24 (BNL uses 10/8, slirp's default # range); sandbox .100, gateway .2, DNS .3. -# --enable-sandbox/--enable-seccomp as podman does, when /etc/resolv.conf -# resolves inside /etc or /run (required by it). +# --enable-seccomp as podman does, when /etc/resolv.conf resolves +# inside /etc or /run (required by --enable-sandbox, +# which the resolv.conf test was written for). +# --enable-sandbox only when the namespace maps uid 0, which this +# launcher's does not; see _NET_NS_ROOT_MAPPED. # --publish works through the API socket (add_hostfwd, IPv4 TCP, one port per # request); --host-port has no slirp4netns equivalent. _SLIRP_BIN="" _SLIRP_ARGS=() -_UNSHARE_BIN="" _NET_SLIRP_NET="192.168.254" _NET_SLIRP_CIDR="${_NET_SLIRP_NET}.0/24" _NET_SLIRP_DNS="${_NET_SLIRP_NET}.3" @@ -3042,6 +3089,76 @@ for spec in sys.argv[2:]: sys.exit("%s:%s -> sandbox port %s: %s" % (addr, hport, gport, reply.get("error", reply))) ' +# Whether the namespace the launcher builds maps uid and gid 0. +# +# It does not, and the two backends cannot both have what they want, because +# an unprivileged process may write only ONE line into uid_map -- the line +# mapping its own id: +# +# map 0 only slirp4netns --enable-sandbox works; bwrap 0.4.0 dies +# with "unable to drop root uid" unless the agent is +# left running as uid 0 (see _BWRAP_MIN_VERSION_MAPROOT) +# map the real id bwrap works and the agent keeps its own uid; +# slirp4netns --enable-sandbox cannot setegid(0) +# +# The real id wins: it is what the sandbox runs as, and the agent's uid is a +# property of every file it writes, whereas --enable-sandbox only hardens +# slirp4netns itself (a mount namespace pivot_rooted into an empty directory, +# plus dropping capabilities). --enable-seccomp is kept; it is a separate +# flag with no id requirement, and it is the filter that bounds what a +# compromised libslirp could call. +# +# Mapping both would need a multi-line map, which takes the setuid helpers +# newuidmap/newgidmap and an /etc/subuid entry -- how rootless podman does it. +# That is the way to restore --enable-sandbox if it is wanted; it needs the +# helper to write the map for the launcher's pid between the unshare and the +# exec, so it is a real change, not a flag. +_NET_NS_ROOT_MAPPED=0 + +# The identity mapper, which replaces `unshare --user --map-root-user --net`: +# unshare a user and a network namespace, map the real uid and gid to +# themselves, then exec the rest of the argument list. +# +# It does not fork, so the launcher keeps its pid: $$ is what the helper waits +# for and what slirp4netns is pointed at. +# +# An identity map, rather than util-linux's root map, for two reasons: +# - bwrap 0.4.0 (RHEL 8, the only platform this backend exists for) cannot +# start under a root-only map at all; see _BWRAP_MIN_VERSION_MAPROOT. +# - the sandbox keeps the real uid without bwrap having to switch back to +# it, so nothing in it runs as uid 0 even for the moment before bwrap +# drops privileges. +# util-linux grew --map-user/--map-current-user in 2.38 and could do this +# directly; RHEL 8 has 2.32, whose unshare offers only --map-root-user. +# +# A process may always write a single-line map of its own id without any +# helper, so this needs neither newuidmap/newgidmap nor /etc/subuid entries +# (which is how rootless podman builds a much wider map). slirp4netns still +# gets full capabilities when it joins: the namespace is owned by the real +# uid, which is the uid it runs as. +# +# Both ids must be read before the unshare; afterwards the process is the +# overflow id (65534) and a map naming that id is refused with EPERM. +_NET_NS_IDENTITY=' +import ctypes, os, sys +CLONE_NEWUSER, CLONE_NEWNET = 0x10000000, 0x40000000 +cmd = sys.argv[1:] +uid, gid = os.getuid(), os.getgid() +libc = ctypes.CDLL(None, use_errno=True) +if libc.unshare(CLONE_NEWUSER | CLONE_NEWNET) != 0: + sys.exit("netns: unshare failed: %s" % os.strerror(ctypes.get_errno())) +try: + with open("/proc/self/setgroups", "w") as f: + f.write("deny") + with open("/proc/self/uid_map", "w") as f: + f.write("%d %d 1" % (uid, uid)) + with open("/proc/self/gid_map", "w") as f: + f.write("%d %d 1" % (gid, gid)) +except OSError as e: + sys.exit("netns: writing the id maps failed: %s" % e) +os.execvp(cmd[0], cmd) +' + # _net_python # Print the Python used by the network helpers (RHEL 8's platform-python, # else python3), nothing when there is none. They need the standard @@ -3057,8 +3174,8 @@ _net_python() { } # _net_try_slirp -# Return 0 when slirp4netns can be used (set _SLIRP_BIN, _SLIRP_ARGS and, -# for --publish, _NET_PY_BIN), else 1 with the reason in _NET_TRY_REASON. +# Return 0 when slirp4netns can be used (set _SLIRP_BIN, _SLIRP_ARGS and +# _NET_PY_BIN), else 1 with the reason in _NET_TRY_REASON. # There is no test run (it would need a second sandbox); a failure at # launch stops the sandbox before the tool starts (see _net_slirp_helper). _net_try_slirp() { @@ -3069,10 +3186,11 @@ _net_try_slirp() { _NET_TRY_REASON="not found on PATH (package slirp4netns)" return 1 fi - _UNSHARE_BIN="$(command -v unshare 2> /dev/null || true)" - if [[ -z "${_UNSHARE_BIN}" || ! -x "${_UNSHARE_BIN}" ]] || - ! "${_UNSHARE_BIN}" --help 2>&1 | grep -q -e '--map-root-user'; then - _NET_TRY_REASON="needs unshare (util-linux) with --map-root-user" + # Python creates the namespaces (_NET_NS_IDENTITY) and, for --publish, + # drives the API socket. Needed for every launch, not only --publish. + _NET_PY_BIN="$(_net_python)" + if [[ -z "${_NET_PY_BIN}" ]]; then + _NET_TRY_REASON="needs python3 (or /usr/libexec/platform-python) to create the namespaces" return 1 fi if ! _out="$("${_SLIRP_BIN}" --version 2>&1)"; then @@ -3102,14 +3220,6 @@ _net_try_slirp() { return 1 fi fi - _NET_PY_BIN="" - if [[ ${#NET_PUBLISH[@]} -gt 0 ]]; then - _NET_PY_BIN="$(_net_python)" - if [[ -z "${_NET_PY_BIN}" ]]; then - _NET_TRY_REASON="--publish needs python3 (or /usr/libexec/platform-python) to configure it" - return 1 - fi - fi _SLIRP_ARGS=( --configure --mtu=65520 @@ -3118,7 +3228,11 @@ _net_try_slirp() { ) _resolv="$(readlink -f /etc/resolv.conf 2> /dev/null || true)" if [[ "${_resolv}" == /etc/* || "${_resolv}" == /run/* ]]; then - _SLIRP_ARGS+=(--enable-sandbox --enable-seccomp) + _SLIRP_ARGS+=(--enable-seccomp) + # --enable-sandbox only with a namespace that maps uid 0; see + # _NET_NS_ROOT_MAPPED. --enable-seccomp has no such requirement + # (slirp4netns.c: create_sandbox and enable_seccomp are independent). + [[ "${_NET_NS_ROOT_MAPPED}" -ne 1 ]] || _SLIRP_ARGS+=(--enable-sandbox) fi return 0 } @@ -3797,9 +3911,9 @@ build_net_isolation() { --hostname "$(uname -n)" ) else - # unshare creates the namespaces (mapping the caller to uid 0) and - # slirp4netns attaches to them; the launch descriptors are opened by - # _net_slirp_start. + # _NET_NS_IDENTITY creates the namespaces (mapping the real ids to + # themselves) and slirp4netns attaches to them; the launch + # descriptors are opened by _net_slirp_start. _dns="${_NET_SLIRP_DNS}" BWRAP_ARGS+=( --unshare-user @@ -5531,7 +5645,7 @@ print_dry_run() { # Mirror the exact invocation used by launch_sandbox: # exec -i # [ --] (pasta backend only) - # [ --user --map-root-user --net --] (slirp4netns only) + # [ -I -c <_NET_NS_IDENTITY>] (slirp4netns only) # -c bwrap-fd-relay -- # -- <_TOOL_CMD> # Values of secret-looking variables (_is_secret_env_name) are printed as @@ -5573,7 +5687,7 @@ print_dry_run() { done [[ "${_NET_BACKEND}" != pasta ]] || printf " -- \\\\\n" if [[ "${_NET_BACKEND}" == slirp4netns ]]; then - printf " %s --user --map-root-user --net -- \\\\\n" "${_UNSHARE_BIN}" + printf " %s -I -c '<_NET_NS_IDENTITY from bwrap_sandbox_lib.sh>' \\\\\n" "${_NET_PY_BIN}" fi printf " %s -c '<_BWRAP_FD_RELAY_SHIM from bwrap_sandbox_lib.sh>' bwrap-fd-relay \\\\\n" "${_BASH_BIN}" local _r=0 @@ -5660,7 +5774,7 @@ launch_sandbox() { if [[ "${_NET_BACKEND}" == pasta ]]; then _net_prefix=("${_PASTA_BIN}" "${PASTA_ARGS[@]}" --) elif [[ "${_NET_BACKEND}" == slirp4netns ]]; then - _net_prefix=("${_UNSHARE_BIN}" --user --map-root-user --net --) + _net_prefix=("${_NET_PY_BIN}" -I -c "${_NET_NS_IDENTITY}") fi _bwrap_fd_data_numbers for _n in ${_BWRAP_FD_DATA_NUMS[@]+"${_BWRAP_FD_DATA_NUMS[@]}"}; do diff --git a/tests/fakes/_nsproxy.py b/tests/fakes/_nsproxy.py index fab8cad..a8686fa 100644 --- a/tests/fakes/_nsproxy.py +++ b/tests/fakes/_nsproxy.py @@ -15,6 +15,7 @@ import json import os import socket +import struct import subprocess import threading @@ -26,6 +27,32 @@ def _die_with_parent(): libc.prctl(PR_SET_PDEATHSIG, 9, 0, 0, 0) +def _keep_caps_across_exec(): + """Make the capabilities this process holds survive execve. + + After joining a user namespace we own, this process has every capability + in it. The real slirp4netns configures the interface right there, with + ioctls in the same process. The fake execs `ip` instead, and execve + clears the capability sets unless the process is uid 0 in the namespace: + true when the launcher mapped the caller to root, false with the identity + map the slirp4netns launch uses (_NET_NS_IDENTITY). Raising each + permitted capability into the ambient set keeps them across the exec, so + the fake can do what the real program does. + """ + libc = ctypes.CDLL(None, use_errno=True) + header = ctypes.create_string_buffer(struct.pack("II", 0x20080522, 0)) # v3, this thread + data = ctypes.create_string_buffer(24) # 2 x {effective, permitted, inheritable} + if libc.capget(header, data) != 0: + return + e0, p0, i0, e1, p1, i1 = struct.unpack("6I", data.raw) + data = ctypes.create_string_buffer(struct.pack("6I", e0, p0, p0, e1, p1, p1)) + if libc.capset(header, data) != 0: + return + for cap in range(64): + if (p0 | (p1 << 32)) >> cap & 1: + libc.prctl(47, 2, cap, 0, 0) # PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE + + class NsProxy: def __init__(self, pid): a, b = socket.socketpair(socket.AF_UNIX, socket.SOCK_SEQPACKET) @@ -37,6 +64,7 @@ def __init__(self, pid): fd = os.open("/proc/%d/ns/%s" % (pid, ns), os.O_RDONLY) os.setns(fd, flag) os.close(fd) + _keep_caps_across_exec() self._serve(b) os._exit(0) b.close() diff --git a/tests/fakes/bin/unshare b/tests/fakes/bin/unshare deleted file mode 100755 index e59cb56..0000000 --- a/tests/fakes/bin/unshare +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python3 -"""Fake util-linux `unshare --user --map-root-user --net -- CMD...`. - -Like util-linux it does not fork: it unshares, writes the maps and execs CMD -in the same process. The failure mode is read from $FAKE_NET_DIR/unshare.fail -(fail: exit 1 before execing). -""" - -import os -import sys - -FAKE_DIR = os.environ.get("FAKE_NET_DIR", "/tmp/opencode/fakenet") -args = sys.argv[1:] -if args[:1] in (["--help"], ["-h"]): - print(" -U, --user[=] unshare user namespace") - print(" -n, --net[=] unshare network namespace") - print(" -r, --map-root-user map current user to root (implies --user)") - sys.exit(0) -try: - if open(os.path.join(FAKE_DIR, "unshare.fail")).read().strip() == "fail": - sys.exit("unshare: unshare failed: Operation not permitted (fake)") -except OSError: - pass -i = args.index("--") -opts, cmd = args[:i], args[i + 1 :] -assert set(opts) == {"--user", "--map-root-user", "--net"}, opts -uid, gid = os.getuid(), os.getgid() -os.unshare(os.CLONE_NEWUSER | os.CLONE_NEWNET) -with open("/proc/self/setgroups", "w") as f: - f.write("deny") -with open("/proc/self/uid_map", "w") as f: - f.write("0 %d 1" % uid) -with open("/proc/self/gid_map", "w") as f: - f.write("0 %d 1" % gid) -os.execvp(cmd[0], cmd) diff --git a/tests/test_netns_identity.py b/tests/test_netns_identity.py new file mode 100644 index 0000000..1016f44 --- /dev/null +++ b/tests/test_netns_identity.py @@ -0,0 +1,112 @@ +"""Tests for _NET_NS_IDENTITY in lib/bwrap_sandbox_lib.sh. + + python3 -m unittest discover -s tests -p 'test_*.py' + +The snippet replaces `unshare --user --map-root-user --net` for the +slirp4netns backend: it unshares a user and a network namespace, maps the +real uid and gid to themselves and execs its arguments in the same process. +The identity map matters because bubblewrap 0.4.0 (RHEL 8) cannot start under +a namespace that maps only uid 0 -- it calls setuid() on an unmapped id and +dies with "unable to drop root uid: Invalid argument". + +The snippet is read out of the shell library rather than copied, so the tests +fail if the two drift apart. +""" + +import os +import subprocess +import sys +import unittest + +HERE = os.path.dirname(os.path.abspath(__file__)) +LIB = os.path.join(HERE, "..", "lib", "bwrap_sandbox_lib.sh") + + +_SNIPPET = None + + +def snippet(): + """The value of _NET_NS_IDENTITY, as the launcher would pass it.""" + global _SNIPPET + if _SNIPPET is None: + out = subprocess.run( + ["bash", "-c", 'source "$1" 2>/dev/null; printf "%s" "${_NET_NS_IDENTITY}"', "_", LIB], + stdout=subprocess.PIPE, + check=True, + ) + _SNIPPET = out.stdout.decode() + return _SNIPPET + + +def run(*args): + """Run the snippet with ARGS as the command it execs.""" + return subprocess.run( + [sys.executable, "-I", "-c", snippet(), *args], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + timeout=30, + ) + + +def userns_available(): + probe = run("/bin/true") + return probe.returncode == 0 + + +class NetNsIdentityTests(unittest.TestCase): + def test_snippet_is_defined(self): + self.assertIn("CLONE_NEWUSER", snippet()) + self.assertIn("execvp", snippet()) + + def test_reads_ids_before_unshare(self): + """A map naming the post-unshare overflow id is refused with EPERM.""" + text = snippet() + self.assertLess( + text.index("os.getuid()"), + text.index("libc.unshare"), + "the real ids must be read before the unshare", + ) + + +@unittest.skipUnless(userns_available(), "unprivileged user namespaces are unavailable") +class NetNsIdentityRunTests(unittest.TestCase): + def read(self, path): + got = run("/bin/cat", path) + self.assertEqual(got.returncode, 0, got.stderr.decode()) + return got.stdout.decode().split() + + def test_uid_map_is_identity(self): + inside, outside, count = self.read("/proc/self/uid_map") + self.assertEqual(inside, outside) + self.assertEqual(int(outside), os.getuid()) + self.assertEqual(count, "1") + + def test_gid_map_is_identity(self): + inside, outside, count = self.read("/proc/self/gid_map") + self.assertEqual(inside, outside) + self.assertEqual(int(outside), os.getgid()) + self.assertEqual(count, "1") + + def test_setgroups_is_denied(self): + self.assertEqual(self.read("/proc/self/setgroups"), ["deny"]) + + def test_keeps_the_real_uid(self): + got = run("/usr/bin/id", "-u") + self.assertEqual(got.stdout.decode().strip(), str(os.getuid())) + + def test_network_namespace_is_new(self): + got = run("/bin/readlink", "/proc/self/ns/net") + self.assertNotEqual(got.stdout.decode().strip(), os.readlink("/proc/self/ns/net")) + + def test_does_not_fork(self): + """slirp4netns is pointed at the launcher's pid, which must not change.""" + got = run("/bin/sh", "-c", "echo $PPID") + self.assertEqual(got.stdout.decode().strip(), str(os.getpid())) + + def test_failure_to_exec_is_reported(self): + got = run("/nonexistent/command") + self.assertNotEqual(got.returncode, 0) + + +if __name__ == "__main__": + unittest.main() From 28291242adc04f390d987f0a78888455f9e04e12 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 12 Nov 2026 22:23:27 -0500 Subject: [PATCH 31/37] feat(bwrap): log DNS lookups under slirp4netns too slirp4netns has no flow log, so its sessions recorded only a start and an end. The DNS relay is not tied to pasta: it runs in the sandbox's network namespace and forwards to the backend's own resolver (192.168.254.3 here). With it, the lookups are the record of what a slirp4netns sandbox reached for. Under the identity map the relay cannot bind port 53 for itself: execve clears capabilities unless the process is uid 0 in the namespace, and the relay starts two execs after the unshare. The mapper (--dns) raises loopback and binds the relay's UDP and TCP sockets before it execs, on descriptors 88 and 89, and the relay adopts them (DnsRelay.adopt). Under pasta it still binds for itself. The netlog FIFO and its condenser now run whenever flows or lookups are logged (_NET_LOG_PIPE); the start record says dns=logged or dns=unavailable, and a failed start is labelled with the backend's name instead of always pasta. Fixes needed along the way: - the spawn relay entry grew from 6 to 8 words; _bwrap_fd_data_numbers must step the same, or it skips data entries and resolv.conf and hosts reach the sandbox empty. tests/test_fd_relay.py runs one list through both. - slirp4netns kept its pipes in the session directory and removed it, which now also holds the log's FIFO; it uses a slirp/ subdirectory when the log owns the directory. - when the condenser does not start, undo the relay's hand-over (its marker and the DNS relay's lines would print on the terminal) and remove the session directory, which nothing else would. Assisted-by: opencode:claude-opus-5-5 --- docs/bw-wrappers.md | 37 ++++-- lib/bwrap_netlog.py | 49 +++++-- lib/bwrap_sandbox_lib.sh | 240 ++++++++++++++++++++++++++++------- tests/test_fd_relay.py | 116 +++++++++++++++++ tests/test_netlog.py | 30 ++++- tests/test_netns_identity.py | 85 +++++++++++++ 6 files changed, 490 insertions(+), 67 deletions(-) create mode 100644 tests/test_fd_relay.py diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 13553a8..98ed92e 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -465,9 +465,12 @@ field, for example the domains an agent reached: Both are tried at launch without logging anything; the launcher then writes the start record itself. With neither, the launch warns and continues without the log. -- **Flows only with pasta.** slirp4netns has no flow log, so with it (and with - `--share-net`, recorded as `backend=none`) only the start and end records - are written; the start record says `flows=unavailable`. +- **Flows only with pasta; DNS with either.** slirp4netns has no flow log, so + with it the records are the start, the end and the DNS relay's lookups — + which are then the only account of what the sandbox reached for. With + `--share-net` (recorded as `backend=none`) there is no namespace to relay + in, so only the start and end records are written. The start record says + which is which: `flows=logged|unavailable dns=logged|unavailable`. - When the launcher runs on a terminal it prints the tool and the session ID. - **How (pasta).** pasta logs flows only at debug level, and at debug level it never uses syslog: without `--log-file` it writes every line, timestamped, @@ -497,7 +500,9 @@ field, for example the domains an agent reached: your stderr: by path when it is a terminal or a file, else (a pipe) through a second FIFO that the reader copies. If pasta stops before the marker, the reader prints pasta's lines on your terminal, prefixed - `pasta:`, just after the launcher has exited. + `pasta:`, just after the launcher has exited. (With slirp4netns the same + FIFO, marker and reader are used for the DNS relay's lines, and a failed + start shows the identity mapper's message prefixed `slirp4netns:`.) - **Condensed.** pasta's debug log has about 34 lines per connection (state changes, timers, ACK and buffer bookkeeping) and its setup dump. The reader keeps one line when a connection opens and one when it closes and drops @@ -506,11 +511,14 @@ field, for example the domains an agent reached: (`pasta=`), from `pasta --version`. - Without Python a bash supervisor copies pasta's lines to the log as they are. -- **DNS relay (pasta).** To name destinations, `lib/bwrap_netlog.py dns` +- **DNS relay (both backends).** To name destinations, `lib/bwrap_netlog.py dns` listens on `127.53.0.1:53` (UDP and TCP) in the sandbox's network namespace, - forwards each lookup to `169.254.1.1` (pasta's DNS forward) and writes one - `dns` line per lookup to the log, before it answers, so the name is known - when the connection that follows is logged. The condenser keeps a map from the answers' + forwards each lookup to the backend's DNS (`169.254.1.1`, pasta's DNS + forward, or `192.168.254.3`, slirp4netns's built-in one; the addresses + below are pasta's) and writes one `dns` line per lookup to the log, before + it answers, so the name is known when the connection that follows is + logged. With slirp4netns the relay cannot bind port 53 itself; see + [Implementation notes: slirp4netns](#implementation-notes-slirp4netns). The condenser keeps a map from the answers' addresses to domains. The sandbox's `/etc/resolv.conf` lists the relay first and `169.254.1.1` second, so lookups go on working, at once, if the relay is down (its port is then closed); pasta's own leg of a relayed lookup (UDP to @@ -566,7 +574,7 @@ foreground, so the terminal, job control, the exit status and ```text launcher ─┬─ helper (background): waits for the namespace, starts slirp4netns, │ adds the --publish forwards, then releases the sandbox - └─ exec env -i ... python -I -c <_NET_NS_IDENTITY> + └─ exec env -i ... python -I -c <_NET_NS_IDENTITY> [--dns ...] -- bash -- bwrap --unshare-user --uid --gid --info-fd --block-fd --sync-fd ... -- ``` @@ -609,6 +617,17 @@ launcher ─┬─ helper (background): waits for the namespace, starts slirp4ne 4.3.0 and later, which the launcher checks), `--mtu=65520`, and `--enable-seccomp` when `/etc/resolv.conf` resolves inside `/etc` or `/run`. +- **DNS lookups are logged**, and with no flow log they are the only account + of what the sandbox reached for. The relay runs inside the network + namespace and forwards to the backend's own DNS, so it is not pasta-only — + but it cannot bind port 53 for itself here. Capabilities survive `execve` + only for uid 0, and this namespace maps your uid, so by the time the relay + starts (after the mapper's exec and the descriptor relay's) it holds none. + The mapper therefore raises loopback and binds the relay's UDP and TCP + sockets while it still can, between the unshare and the exec, and leaves + them on descriptors 88 and 89 for `DnsRelay.adopt`. A fresh namespace also + has `lo` down, and slirp4netns only raises it later in `configure_network`, + so binding early removes that race too. - **`--enable-sandbox` is not passed**, and this is the cost of the identity map. After joining the launcher's namespace, slirp4netns does `setegid(0)`/`seteuid(0)` (`main.c`); with no uid 0 mapped that fails and diff --git a/lib/bwrap_netlog.py b/lib/bwrap_netlog.py index c6693bb..33edc71 100644 --- a/lib/bwrap_netlog.py +++ b/lib/bwrap_netlog.py @@ -5,7 +5,7 @@ bwrap_netlog.py condense --session ID [--dns-forward ADDR] [--fifo PATH --parent PID [--marker TEXT] [--end-record TEXT] - [--session-dir DIR]] -- CMD... + [--session-dir DIR] [--backend NAME]] -- CMD... Read pasta's debug output (its stderr, and the DNS relay's lines) and write one line when a connection opens and one when it closes, with the domain of the destination when the relay saw it, to CMD's @@ -15,12 +15,14 @@ input; with it, it reads the FIFO (see the launcher's "Network log") until the launcher, PID, has exited, then writes the end record. - bwrap_netlog.py dns LISTEN UPSTREAM PARENT_PID + bwrap_netlog.py dns LISTEN UPSTREAM PARENT_PID [UDP_FD TCP_FD] A DNS relay for the sandbox's network namespace: listen on LISTEN (ADDR or ADDR:PORT, UDP and TCP), forward every query to UPSTREAM - (pasta's DNS forward address) and print one line per query on - standard output. It exits when PARENT_PID (the descriptor relay, - which becomes bwrap) does. + (the backend's DNS address) and print one line per query on standard + output. It exits when PARENT_PID (the descriptor relay, which + becomes bwrap) does. UDP_FD and TCP_FD are sockets already bound to + LISTEN, for a namespace where this process cannot bind port 53 + itself; "-" means bind here (see DnsRelay.adopt). Output lines are logfmt (key=value), so Loki's `| logfmt` can filter them: @@ -291,6 +293,7 @@ def condense(argv): session, dns_forward, cmd = "", "169.254.1.1", [] fifo, parent, marker, end_record, session_dir, stderr_fifo = None, None, None, None, None, None + backend = "pasta" i = 0 while i < len(argv): a = argv[i] @@ -310,6 +313,8 @@ def condense(argv): session_dir = argv[i + 1] elif a == "--stderr-fifo": stderr_fifo = argv[i + 1] + elif a == "--backend": + backend = argv[i + 1] elif a == "--": cmd = argv[i + 1 :] break @@ -386,9 +391,11 @@ def feed(raw): early.append(raw) feed(raw) if not started: - # pasta stopped before starting the sandbox: show what it said. + # The backend stopped before starting the sandbox: show what it + # said (pasta's own lines; with slirp4netns, the identity + # mapper's or the descriptor relay's). for raw in early: - sys.stderr.write("pasta: %s\n" % raw) + sys.stderr.write("%s: %s\n" % (backend, raw)) sys.stderr.flush() if bridge: bridge_stop.set() @@ -615,6 +622,23 @@ def bind(self): self.tcp.bind(self.udp.getsockname()) self.tcp.listen(16) + def adopt(self, udp_fd, tcp_fd): + """Take over sockets bound before the exec that dropped our privileges. + + Binding port 53 needs CAP_NET_BIND_SERVICE, and capabilities survive + execve only for uid 0. With pasta the namespace maps the caller to 0, + so bind() works; with slirp4netns the namespace maps the real uid, the + exec chain leaves us with no capabilities at all, and the sockets are + bound instead by the process that creates the namespace and handed + over here (see _NET_NS_IDENTITY in bwrap_sandbox_lib.sh). + """ + self.udp = socket.fromfd(udp_fd, socket.AF_INET, socket.SOCK_DGRAM) + self.tcp = socket.fromfd(tcp_fd, socket.AF_INET, socket.SOCK_STREAM) + # fromfd duplicates; the inherited numbers are not needed again. + os.close(udp_fd) + os.close(tcp_fd) + self.listen = self.udp.getsockname() + def serve(self): def tcp_loop(): while not self.stopping.is_set(): @@ -661,6 +685,10 @@ def _drop_capabilities(): def dns(argv): listen, upstream, parent = argv[0], argv[1], int(argv[2]) + # Optional pre-bound sockets, "-" when this process may bind for itself. + fds = None + if len(argv) >= 5 and argv[3] != "-": + fds = (int(argv[3]), int(argv[4])) try: libc = ctypes.CDLL(None, use_errno=True) libc.prctl(1, signal.SIGTERM, 0, 0, 0) # PR_SET_PDEATHSIG @@ -670,7 +698,10 @@ def dns(argv): return relay = DnsRelay(listen, upstream) try: - relay.bind() + if fds is None: + relay.bind() + else: + relay.adopt(*fds) except OSError as e: sys.stdout.write("bwrap-dns domain=- type=- proto=- rcode=RELAY-FAILED answers=%s\n" % _value(str(e))) sys.stdout.flush() @@ -687,7 +718,7 @@ def dns(argv): def main(): if len(sys.argv) >= 2 and sys.argv[1] == "condense": condense(sys.argv[2:]) - elif len(sys.argv) == 5 and sys.argv[1] == "dns": + elif len(sys.argv) in (5, 7) and sys.argv[1] == "dns": try: dns(sys.argv[2:]) except KeyboardInterrupt: diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index bdbdcf2..72ed07a 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -2685,8 +2685,14 @@ _net_session_dir() { # copies to it (a pipe). If pasta exits before the marker (a failed # start), the reader prints its lines on the terminal, so its errors are # not lost. -# With slirp4netns or --share-net only the start and end records are written -# (slirp4netns has no flow log); the supervisor just waits for the end. +# slirp4netns has no flow log, so with it the records are the start, the end +# and the DNS relay's lookups -- which are then the only account of what the +# sandbox reached for, and the reason the relay is not left to pasta. The +# FIFO, the reader and the marker work as above, with the identity mapper +# (_NET_NS_IDENTITY) in pasta's place as the launcher's exec and as the +# writer of whatever is said before the marker. With --share-net there is +# no namespace to relay in: only start and end are written and the +# supervisor just waits for the end. _NET_LOG_TAG="bwrap-net" # Condenser and DNS relay (lib/bwrap_netlog.py), and the relay's address in @@ -2695,6 +2701,15 @@ _NET_LOG_HELPER="${BASH_SOURCE[0]%/*}/bwrap_netlog.py" _NET_LOG_PY="" _NET_DNS_RELAY=0 _NET_DNS_RELAY_ADDR="127.53.0.1" +_NET_DNS_RELAY_PORT=53 +# The DNS relay's sockets when they must be bound before the exec chain (see +# _NET_NS_IDENTITY). Fixed numbers just below the slirp4netns launch +# descriptors; _bwrap_fd_* allocates below them. +_NET_FD_DNS_UDP=88 +_NET_FD_DNS_TCP=89 +# 1 when the log needs the FIFO, the supervisor and the condenser: pasta's +# flow log, the DNS relay, or both. +_NET_LOG_PIPE=0 _NET_LOG_TOOL="" _NET_LOG_TOOLS=() _NET_LOG_SESSION="" @@ -2728,13 +2743,18 @@ _net_log_detect() { [[ -z "${_NET_LOG_TOOL}" || "${_NET_BACKEND}" != pasta ]] || _NET_LOG_FLOWS=1 # With Python the flow log is condensed (one line per open and close) # and the DNS relay names the destinations; without it pasta's lines go - # to the log as they are. + # to the log as they are. The relay runs inside the sandbox's network + # namespace and forwards to whatever DNS the backend provides, so it is + # not pasta-only: with slirp4netns, which has no flow log, the lookups + # are the only record of what the sandbox reached for. _NET_LOG_PY="" _NET_DNS_RELAY=0 - if [[ "${_NET_LOG_FLOWS}" -eq 1 && -r "${_NET_LOG_HELPER}" ]]; then + if [[ -n "${_NET_LOG_TOOL}" && "${_NET_PRIVATE}" -eq 1 && -r "${_NET_LOG_HELPER}" ]]; then _NET_LOG_PY="$(_net_python)" [[ -z "${_NET_LOG_PY}" ]] || _NET_DNS_RELAY=1 fi + _NET_LOG_PIPE=0 + [[ "${_NET_LOG_FLOWS}" -eq 0 && "${_NET_DNS_RELAY}" -eq 0 ]] || _NET_LOG_PIPE=1 SANDBOX_ENV+=("BWRAP_SESSION=${_NET_LOG_SESSION}") } @@ -2762,6 +2782,11 @@ _net_log_record() { else _r+=" flows=unavailable" fi + if [[ "${_NET_DNS_RELAY}" -eq 1 ]]; then + _r+=" dns=logged" + else + _r+=" dns=unavailable" + fi [[ -z "${_NET_LOG_PASTA_VERSION}" ]] || _r+=" pasta=${_NET_LOG_PASTA_VERSION}" [[ -z "${_NET_PUBLISH_ADDR_EFFECTIVE}" ]] || _r+=" publish=${_NET_PUBLISH_ADDR_EFFECTIVE}:$(_net_join ${NET_PUBLISH[@]+"${NET_PUBLISH[@]}"})" for _i in ${NET_HOST_PORTS[@]+"${NET_HOST_PORTS[@]}"}; do _hp+="${_hp:+,}${_i}"; done @@ -2779,6 +2804,7 @@ _net_log_record() { # FIFO open. _net_log_start() { local _err _launcher="${0##*/}" _t _ready _rfd + local -a _relay_orig=() _NET_LOG_TOOL="" if [[ "${_NET_BACKEND}" == pasta ]]; then _NET_LOG_PASTA_VERSION="$("${_PASTA_BIN}" --version 2> /dev/null | awk 'NR == 1 { print $2 }' || true)" @@ -2806,16 +2832,18 @@ _net_log_start() { "${_NET_LOG_TAG}" "${_NET_LOG_SESSION}" >&2 fi - if [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then + if [[ "${_NET_LOG_PIPE}" -eq 1 ]]; then _net_session_dir _NET_LOG_FIFO="${_NET_SESSION_DIR}/netlog" mkfifo -m 0600 "${_NET_LOG_FIFO}" _NET_LOG_MARKER="bwrap-net relay started ${_NET_LOG_SESSION}" - local _i - for _i in "${!PASTA_ARGS[@]}"; do - [[ "${PASTA_ARGS[${_i}]}" != --quiet ]] || unset 'PASTA_ARGS[_i]' - done - PASTA_ARGS=("${PASTA_ARGS[@]}" --debug) + if [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then + local _i + for _i in "${!PASTA_ARGS[@]}"; do + [[ "${PASTA_ARGS[${_i}]}" != --quiet ]] || unset 'PASTA_ARGS[_i]' + done + PASTA_ARGS=("${PASTA_ARGS[@]}" --debug) + fi # The relay restores the user's stderr: by path when it is a terminal # or a file, else through a FIFO the reader copies. _err="$(readlink "/proc/$$/fd/2" 2> /dev/null || true)" @@ -2825,33 +2853,56 @@ _net_log_start() { mkfifo -m 0600 "${_NET_LOG_ERR_FIFO}" _err="${_NET_LOG_ERR_FIFO}" fi + # Kept so that a reader that fails to start can undo the hand-over. + _relay_orig=(${_BWRAP_FD_RELAY[@]+"${_BWRAP_FD_RELAY[@]}"}) local -a _pre=(2 note "${_NET_LOG_MARKER}") if [[ "${_NET_DNS_RELAY}" -eq 1 ]]; then # The DNS relay, started by the descriptor relay in the sandbox's - # network namespace (outside bwrap), logs on the same FIFO. - _pre+=(2 spawn "${_NET_LOG_PY}" "${_NET_LOG_HELPER}" "${_NET_DNS_RELAY_ADDR}" "${_NET_DNS_FORWARD_ADDR}") + # network namespace (outside bwrap), logs on the same FIFO and + # forwards to the backend's own DNS address. Under pasta it + # binds port 53 itself; under slirp4netns the exec chain has no + # capabilities, so it takes the sockets _NET_NS_IDENTITY bound. + local _udp=- _tcp=- + if [[ "${_NET_BACKEND}" == slirp4netns ]]; then + _udp="${_NET_FD_DNS_UDP}" + _tcp="${_NET_FD_DNS_TCP}" + fi + _pre+=(2 spawn "${_NET_LOG_PY}" "${_NET_LOG_HELPER}" "${_NET_DNS_RELAY_ADDR}" "${_NET_DNS_EFFECTIVE}" "${_udp}" "${_tcp}") fi _BWRAP_FD_RELAY=("${_pre[@]}" 2 append "${_err}" ${_BWRAP_FD_RELAY[@]+"${_BWRAP_FD_RELAY[@]}"}) fi - if [[ "${_NET_LOG_FLOWS}" -eq 1 && -n "${_NET_LOG_PY}" ]]; then - # The condenser reads the FIFO. It says "ready" on descriptor 3 - # once it has it open, so that nothing pasta writes is lost; its - # stderr is the user's, for pasta's lines if pasta fails to start. + if [[ "${_NET_LOG_PIPE}" -eq 1 && -n "${_NET_LOG_PY}" ]]; then + # The condenser reads the FIFO: pasta's flows, the DNS relay's + # lookups, or both. It says "ready" on descriptor 3 once it has it + # open, so that nothing written to it is lost; its stderr is the + # user's, for what the backend said if the start fails. local -a _cond=("${_NET_LOG_PY}" -I -S "${_NET_LOG_HELPER}" condense --session "${_NET_LOG_SESSION}" --dns-forward "${_NET_DNS_FORWARD_ADDR}" --fifo "${_NET_LOG_FIFO}" --parent "$$" --marker "${_NET_LOG_MARKER}" - --end-record "$(_net_log_record end)" --session-dir "${_NET_SESSION_DIR}") + --end-record "$(_net_log_record end)" --session-dir "${_NET_SESSION_DIR}" + --backend "${_NET_BACKEND}") [[ -z "${_NET_LOG_ERR_FIFO}" ]] || _cond+=(--stderr-fifo "${_NET_LOG_ERR_FIFO}") exec {_rfd}< <(exec setsid "${_cond[@]}" -- "${_NET_LOG_CMD[@]}" 3>&1 > /dev/null < /dev/null) IFS= read -r -t 10 _ready <&"${_rfd}" || _ready="" exec {_rfd}<&- if [[ "${_ready}" != ready ]]; then - printf 'Warning: %s: the network log condenser did not start; connections are not\n' "${_launcher}" >&2 - printf ' logged for this session.\n' >&2 - # Keep pasta's stderr off a FIFO no one reads. + printf 'Warning: %s: the network log condenser did not start; connections and\n' "${_launcher}" >&2 + printf ' lookups are not logged for this session.\n' >&2 + # Keep the backend's stderr off a FIFO no one reads, and take + # back the relay's hand-over: with no FIFO its marker, and the + # DNS relay's lines, would land on the user's terminal. _NET_LOG_FIFO="" _NET_LOG_FLOWS=0 + _NET_DNS_RELAY=0 + _NET_LOG_PIPE=0 + _BWRAP_FD_RELAY=(${_relay_orig[@]+"${_relay_orig[@]}"}) + # No reader is left to remove the session directory, and its + # FIFOs would make it look owned (see _net_slirp_start): remove + # it now; slirp4netns makes a fresh one if it needs one. + rm -rf -- "${_NET_SESSION_DIR}" + _NET_SESSION_DIR="" + _NET_LOG_ERR_FIFO="" fi return 0 fi @@ -2875,10 +2926,12 @@ _net_log_supervisor() { exec {_sink}> >(exec setsid "${_NET_LOG_CMD[@]}" > /dev/null 2>&1) _prefix="session=${_NET_LOG_SESSION} " - if [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then + if [[ "${_NET_LOG_PIPE}" -eq 1 ]]; then # Read-write, so end-of-file is never seen before pasta writes. This # descriptor is the supervisor's alone; the launcher's own hold on # the FIFO is closed before it execs pasta (see launch_sandbox). + # Without Python there is no DNS relay, so here the FIFO only ever + # carries pasta's flows. exec {_fifo}<> "${_NET_LOG_FIFO}" if [[ -n "${_NET_LOG_ERR_FIFO}" ]]; then cat < "${_NET_LOG_ERR_FIFO}" >&2 & @@ -2918,9 +2971,10 @@ _net_log_supervisor() { done exec {_fifo}<&- if [[ "${_started}" -eq 0 ]]; then - # pasta stopped before starting the sandbox: show what it said. + # The namespace stopped before starting the sandbox: show what it + # said (pasta's own messages, or the descriptor relay's). for _line in ${_early[@]+"${_early[@]}"}; do - printf '%s: pasta: %s\n' "${0##*/}" "${_line}" >&2 + printf '%s: %s: %s\n' "${0##*/}" "${_NET_BACKEND}" "${_line}" >&2 done fi if [[ -n "${_bridge}" ]]; then @@ -2953,6 +3007,9 @@ _net_log_context_text() { _NET_LOG_CTX="The host records this network session in its system log (tag \`${_NET_LOG_TAG}\`, session \`${_NET_LOG_SESSION}\`, also in \`BWRAP_SESSION\`)" if [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then _NET_LOG_CTX+=", with every connection pasta makes for it" + [[ "${_NET_DNS_RELAY}" -ne 1 ]] || _NET_LOG_CTX+=" and every name it looks up" + elif [[ "${_NET_DNS_RELAY}" -eq 1 ]]; then + _NET_LOG_CTX+=", with every name it looks up (${_NET_BACKEND} has no flow log, so connections are not recorded)" fi _NET_LOG_CTX+=". The sandbox cannot read or change that log."$'\n\n' } @@ -2973,20 +3030,35 @@ _net_log_print_dry_run() { printf '# and pasta gets --debug (not --quiet) with its stderr on /netlog,\n' printf '# a FIFO; the relay first reopens your stderr. The end record is written when\n' printf '# pasta exits.\n' - if [[ -n "${_NET_LOG_PY}" ]]; then - printf '# The FIFO is read, and the lines condensed (one per connection opened and\n' - printf '# closed), by\n' - printf '# %s -I %s condense --fifo /netlog ...\n' "${_NET_LOG_PY}" "${_NET_LOG_HELPER}" - printf '# and the relay first starts the DNS relay on %s:53 in the namespace:\n' "${_NET_DNS_RELAY_ADDR}" - printf '# %s -I %s dns %s %s \n' "${_NET_LOG_PY}" "${_NET_LOG_HELPER}" \ - "${_NET_DNS_RELAY_ADDR}" "${_NET_DNS_FORWARD_ADDR}" - else - printf '# A supervisor copies each line to the log as it is.\n' - fi + elif [[ "${_NET_LOG_PIPE}" -eq 1 ]]; then + printf '# and the launcher'"'"'s stderr goes to /netlog, a FIFO, until the\n' + printf '# relay reopens yours (%s has no flow log: the FIFO carries the DNS relay'"'"'s\n' "${_NET_BACKEND}" + printf '# lines). The end record is written when the sandbox exits.\n' else printf '# and a supervisor writes the end record when the sandbox exits (no flow log\n' printf '# with %s).\n' "${_NET_BACKEND}" fi + if [[ "${_NET_LOG_PIPE}" -eq 1 && -n "${_NET_LOG_PY}" ]]; then + printf '# The FIFO is read, and the lines condensed (one per connection opened and\n' + printf '# closed, one per lookup), by\n' + printf '# %s -I %s condense --fifo /netlog ...\n' "${_NET_LOG_PY}" "${_NET_LOG_HELPER}" + elif [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then + printf '# A supervisor copies each line to the log as it is.\n' + fi + if [[ "${_NET_DNS_RELAY}" -eq 1 ]]; then + printf '# The descriptor relay starts the DNS relay on %s:%s in the namespace:\n' \ + "${_NET_DNS_RELAY_ADDR}" "${_NET_DNS_RELAY_PORT}" + if [[ "${_NET_BACKEND}" == slirp4netns ]]; then + printf '# %s -I %s dns %s %s %s %s\n' "${_NET_LOG_PY}" "${_NET_LOG_HELPER}" \ + "${_NET_DNS_RELAY_ADDR}" "${_NET_DNS_EFFECTIVE}" "${_NET_FD_DNS_UDP}" "${_NET_FD_DNS_TCP}" + printf '# on the sockets descriptors %s and %s already hold: the namespace maps your own\n' \ + "${_NET_FD_DNS_UDP}" "${_NET_FD_DNS_TCP}" + printf '# uid, so nothing after the exec can bind port %s and they are bound before it.\n' "${_NET_DNS_RELAY_PORT}" + else + printf '# %s -I %s dns %s %s - -\n' "${_NET_LOG_PY}" "${_NET_LOG_HELPER}" \ + "${_NET_DNS_RELAY_ADDR}" "${_NET_DNS_EFFECTIVE}" + fi + fi } # ── slirp4netns backend ────────────────────────────────────────── @@ -3002,9 +3074,15 @@ _net_log_print_dry_run() { # │ for it to be ready, adds the --publish forwards through its # │ API socket, then releases the sandbox (--block-fd) and exits # └─ exec env -i ... python -I -c <_NET_NS_IDENTITY> +# [--dns 127.53.0.1 53 88 89] -- # bash -- bwrap --unshare-user --uid U --gid G # --info-fd 90 --block-fd 91 --sync-fd 92 ... -- tool # +# With the network log's DNS relay the mapper also raises loopback and binds +# the relay's sockets on descriptors 88 and 89 before it execs, because after +# the exec nothing holds the capability to bind port 53 (see +# _NET_NS_IDENTITY); the descriptor relay then starts the relay on them. +# # On a failure the helper kills the still-blocked sandbox (its pid comes # from --info-fd) and then bwrap: bwrap's --die-with-parent refers to the # user's shell, so killing bwrap alone would leave the sandbox, which the @@ -3139,10 +3217,31 @@ _NET_NS_ROOT_MAPPED=0 # # Both ids must be read before the unshare; afterwards the process is the # overflow id (65534) and a map naming that id is refused with EPERM. +# +# With "--dns ADDR PORT UDPFD TCPFD" it also brings the namespace's loopback +# up and binds the DNS relay's sockets, leaving them at UDPFD and TCPFD for +# the relay that the descriptor relay spawns later. That work has to happen +# HERE, in this process, between the unshare and the exec: +# +# - creating the user namespace grants a full capability set, so this +# process has CAP_NET_ADMIN (loopback up) and CAP_NET_BIND_SERVICE +# (port 53); +# - execve recomputes the sets, and with no file capabilities and a +# non-zero euid it clears them. Under pasta the namespace maps the +# caller to 0 and capabilities survive, which is why the relay binds for +# itself there; here nothing downstream of the exec can. +# +# Bringing lo up also removes a race: slirp4netns raises lo in +# configure_network, but the descriptor relay runs before it is ready. _NET_NS_IDENTITY=' -import ctypes, os, sys +import ctypes, fcntl, os, socket, struct, sys CLONE_NEWUSER, CLONE_NEWNET = 0x10000000, 0x40000000 -cmd = sys.argv[1:] +SIOCSIFFLAGS, IFF_UP, IFF_RUNNING = 0x8914, 0x1, 0x40 +argv = sys.argv[1:] +dns = None +if argv[:1] == ["--dns"]: + dns, argv = (argv[1], int(argv[2]), int(argv[3]), int(argv[4])), argv[5:] +cmd = argv[1:] if argv[:1] == ["--"] else argv uid, gid = os.getuid(), os.getgid() libc = ctypes.CDLL(None, use_errno=True) if libc.unshare(CLONE_NEWUSER | CLONE_NEWNET) != 0: @@ -3156,6 +3255,21 @@ try: f.write("%d %d 1" % (gid, gid)) except OSError as e: sys.exit("netns: writing the id maps failed: %s" % e) +if dns is not None: + addr, port, udp_fd, tcp_fd = dns + try: + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as c: + fcntl.ioctl(c, SIOCSIFFLAGS, struct.pack("16sh22x", b"lo", IFF_UP | IFF_RUNNING)) + udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + udp.bind((addr, port)) + tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + tcp.bind((addr, port)) + tcp.listen(16) + os.dup2(udp.fileno(), udp_fd) # dup2 inherits across the exec + os.dup2(tcp.fileno(), tcp_fd) + except OSError as e: + sys.exit("netns: DNS relay sockets (%s:%d) failed: %s" % (addr, port, e)) os.execvp(cmd[0], cmd) ' @@ -3267,7 +3381,16 @@ _net_slirp_publish_fwds() { _net_slirp_start() { local _f _net_session_dir - _NET_SLIRP_DIR="${_NET_SESSION_DIR}" + if [[ -n "${_NET_LOG_FIFO}" ]]; then + # The network log owns the session directory (its FIFOs are there, + # and its reader removes the directory when the session ends), so + # the pipes get a directory of their own: the rmdir below and the + # helper's rm -rf must not take the log's FIFOs with them. + _NET_SLIRP_DIR="${_NET_SESSION_DIR}/slirp" + mkdir -m 0700 -- "${_NET_SLIRP_DIR}" + else + _NET_SLIRP_DIR="${_NET_SESSION_DIR}" + fi for _f in info block sync ready; do mkfifo -m 0600 "${_NET_SLIRP_DIR}/${_f}" done @@ -5378,18 +5501,24 @@ _bwrap_toml_string() { # note TEXT the relay prints TEXT on the descriptor (a marker on stderr). # append PATH the relay opens PATH for appending on the descriptor (the # user's stderr again). -# spawn PY HELPER LISTEN UPSTREAM the relay starts the DNS relay -# (`PY -I -S HELPER dns LISTEN UPSTREAM `) in the -# background, its output on the descriptor, and waits until it -# listens (it says so on descriptor 3; a Python start, some tens -# of milliseconds), at most 0.25 s, so the first lookups are -# logged too. +# spawn PY HELPER LISTEN UPSTREAM UDPFD TCPFD the relay starts the DNS +# relay (`PY -I -S HELPER dns LISTEN UPSTREAM UDPFD +# TCPFD`) in the background, its output on the descriptor, and +# waits until it listens (it says so on descriptor 3; a Python +# start, some tens of milliseconds), at most 0.25 s, so the +# first lookups are logged too. UDPFD and TCPFD are sockets +# already bound (slirp4netns) or "-" (pasta: the relay binds); +# the relay closes its own copies afterwards. +# Every walker of _BWRAP_FD_RELAY (_bwrap_fd_data_numbers) must step over +# exactly as many words per entry as the shim shifts: data 2, spawn 8, the +# rest 3. # _bwrap_fd_check_room -# Exit 1 when the reserved numbers would reach 90, where the slirp4netns -# launch keeps its own descriptors (_NET_FD_*). +# Exit 1 when the reserved numbers would reach _NET_FD_DNS_UDP, above which +# the DNS relay's pre-bound sockets and then the slirp4netns launch +# (_NET_FD_*, 90-99) keep their own descriptors. _bwrap_fd_check_room() { - if [[ ${_BWRAP_FD_NEXT} -ge 90 ]]; then + if [[ ${_BWRAP_FD_NEXT} -ge ${_NET_FD_DNS_UDP} ]]; then echo "Error: too many files to pass to the sandbox by descriptor." >&2 exit 1 fi @@ -5462,10 +5591,13 @@ while [[ $# -gt 0 && "$1" != -- ]]; do shift 3 ;; spawn) - coproc _bwrap_dns { exec "$3" -I -S "$4" dns "$5" "$6" "$$" 3>&1 1>&"$1" 2>&"$1" < /dev/null; } + coproc _bwrap_dns { exec "$3" -I -S "$4" dns "$5" "$6" "$$" "$7" "$8" 3>&1 1>&"$1" 2>&"$1" < /dev/null; } read -r -t 0.25 _v <&"${_bwrap_dns[0]}" eval "exec ${_bwrap_dns[0]}<&- ${_bwrap_dns[1]}>&-" - shift 6 + # Pre-bound sockets: the relay has its own copies, so close ours + # rather than leak listening sockets into the sandbox. + [[ "$7" == - ]] || eval "exec $7<&- $8<&-" + shift 8 ;; *) echo "$0: bad relay entry: $2" >&2 @@ -5507,7 +5639,9 @@ _bwrap_fd_data_numbers() { _BWRAP_FD_DATA_NUMS+=("${_BWRAP_FD_RELAY[${_r}]}") _r=$((_r + 2)) ;; - spawn) _r=$((_r + 6)) ;; + # Must match the shim's `shift` for spawn (see + # _BWRAP_FD_RELAY_SHIM); tests/test_fd_relay.py checks the two. + spawn) _r=$((_r + 8)) ;; *) _r=$((_r + 3)) ;; esac done @@ -5688,6 +5822,11 @@ print_dry_run() { [[ "${_NET_BACKEND}" != pasta ]] || printf " -- \\\\\n" if [[ "${_NET_BACKEND}" == slirp4netns ]]; then printf " %s -I -c '<_NET_NS_IDENTITY from bwrap_sandbox_lib.sh>' \\\\\n" "${_NET_PY_BIN}" + if [[ "${_NET_DNS_RELAY}" -eq 1 ]]; then + printf " --dns %s %s %s %s \\\\\n" "${_NET_DNS_RELAY_ADDR}" \ + "${_NET_DNS_RELAY_PORT}" "${_NET_FD_DNS_UDP}" "${_NET_FD_DNS_TCP}" + fi + printf " -- \\\\\n" fi printf " %s -c '<_BWRAP_FD_RELAY_SHIM from bwrap_sandbox_lib.sh>' bwrap-fd-relay \\\\\n" "${_BASH_BIN}" local _r=0 @@ -5775,6 +5914,11 @@ launch_sandbox() { _net_prefix=("${_PASTA_BIN}" "${PASTA_ARGS[@]}" --) elif [[ "${_NET_BACKEND}" == slirp4netns ]]; then _net_prefix=("${_NET_PY_BIN}" -I -c "${_NET_NS_IDENTITY}") + if [[ "${_NET_DNS_RELAY}" -eq 1 ]]; then + _net_prefix+=(--dns "${_NET_DNS_RELAY_ADDR}" "${_NET_DNS_RELAY_PORT}" + "${_NET_FD_DNS_UDP}" "${_NET_FD_DNS_TCP}") + fi + _net_prefix+=(--) fi _bwrap_fd_data_numbers for _n in ${_BWRAP_FD_DATA_NUMS[@]+"${_BWRAP_FD_DATA_NUMS[@]}"}; do diff --git a/tests/test_fd_relay.py b/tests/test_fd_relay.py new file mode 100644 index 0000000..2c4c433 --- /dev/null +++ b/tests/test_fd_relay.py @@ -0,0 +1,116 @@ +"""Tests for the descriptor relay in lib/bwrap_sandbox_lib.sh. + + python3 -m unittest discover -s tests -p 'test_*.py' + +The relay list (_BWRAP_FD_RELAY) is read by two parties that must agree on +how many words each entry takes: the shim (_BWRAP_FD_RELAY_SHIM), which +shifts past each entry as it runs, and _bwrap_fd_data_numbers, which finds +the `data` entries so the launcher can export their text. If the walker +steps differently from the shim it loses its place, skips data entries, and +their files (resolv.conf, hosts) reach the sandbox empty, with no error. +These tests give one list to both and check every data descriptor arrives. +""" + +import os +import subprocess +import sys +import unittest + +HERE = os.path.dirname(os.path.abspath(__file__)) +LIB = os.path.join(HERE, "..", "lib", "bwrap_sandbox_lib.sh") +HELPER = os.path.join(HERE, "..", "lib", "bwrap_netlog.py") + +# Run the launcher's own code: build the list, walk it, export the data the +# way launch_sandbox does, then exec the shim and read each data descriptor +# through the descriptor itself (as bwrap does), not by reopening it. +DRIVER = r""" +source "$1" 2>/dev/null +shift +_BWRAP_FD_RELAY=("$@") +declare -A _BWRAP_FD_DATA=([10]=RESOLV [11]=HOSTS [12]=CONTEXT) +_bwrap_fd_data_numbers +echo "walker: ${_BWRAP_FD_DATA_NUMS[*]}" +env=() +for n in "${_BWRAP_FD_DATA_NUMS[@]}"; do env+=("_BWRAP_FD_DATA_${n}=${_BWRAP_FD_DATA[${n}]}"); done +exec env "${env[@]}" bash -c "${_BWRAP_FD_RELAY_SHIM}" bwrap-fd-relay "${_BWRAP_FD_RELAY[@]}" -- \ + sh -c 'for n in 10 11 12; do printf "%s=%s\n" "$n" "$(eval "cat <&$n")"; done' 2>/dev/null +""" + +DATA = ["10", "data", "11", "data", "12", "data"] + + +def relay(*entries): + got = subprocess.run( + ["bash", "-c", DRIVER, "_", LIB, *entries], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + timeout=30, + ) + out = got.stdout.decode() + fields = dict(line.split("=", 1) for line in out.splitlines() if "=" in line) + walker = next((ln for ln in out.splitlines() if ln.startswith("walker:")), "") + return walker, fields, got + + +class FdRelayTest(unittest.TestCase): + def assert_all_data_arrive(self, *entries): + walker, fields, got = relay(*entries) + self.assertEqual(walker, "walker: 10 11 12", got.stderr.decode()) + self.assertEqual(fields, {"10": "RESOLV", "11": "HOSTS", "12": "CONTEXT"}) + + def test_data_alone(self): + self.assert_all_data_arrive(*DATA) + + def test_after_the_network_log_entries_pasta(self): + """note, spawn (relay binds for itself: "- -") and append first.""" + self.assert_all_data_arrive( + "2", + "note", + "MARK", + "2", + "spawn", + sys.executable, + HELPER, + "127.0.0.1:5399", + "127.0.0.1:9", + "-", + "-", + "2", + "append", + os.devnull, + *DATA, + ) + + def test_after_the_network_log_entries_slirp4netns(self): + """spawn with pre-bound descriptors, as under slirp4netns.""" + r, w = os.pipe() # two real descriptors to stand in for the sockets + try: + os.dup2(r, 88) + os.dup2(w, 89) + self.assert_all_data_arrive( + "2", + "note", + "MARK", + "2", + "spawn", + sys.executable, + HELPER, + "127.0.0.1:5398", + "127.0.0.1:9", + "88", + "89", + "2", + "append", + os.devnull, + *DATA, + ) + finally: + for fd in (r, w, 88, 89): + try: + os.close(fd) + except OSError: + pass + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_netlog.py b/tests/test_netlog.py index 8c2375d..75b9339 100644 --- a/tests/test_netlog.py +++ b/tests/test_netlog.py @@ -199,11 +199,14 @@ def setUp(self): self.out = io.StringIO() self.relay = netlog.DnsRelay("127.0.0.1:0", "127.0.0.1:%d" % port, self.out) self.relay.listen = ("127.0.0.1", 0) - self.relay.bind() + self._open(self.relay) self.addr = self.relay.udp.getsockname() self.relay_thread = threading.Thread(target=self.relay.serve) self.relay_thread.start() + def _open(self, relay): + relay.bind() + def tearDown(self): self.stop.set() self.relay.close() @@ -317,5 +320,30 @@ def test_overload_returns_servfail(self): serve_thread.join(timeout=3) +class DnsRelayAdoptTest(DnsRelayTest): + """Every DnsRelayTest case again, against pre-bound sockets. + + With slirp4netns the relay cannot bind for itself: the namespace maps the + real uid, so the execve that starts the relay clears its capabilities and + port 53 is out of reach. The process that creates the namespace binds + first and passes the descriptors (_NET_NS_IDENTITY in + bwrap_sandbox_lib.sh), which DnsRelay.adopt takes over. + """ + + def _open(self, relay): + udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + udp.bind(("127.0.0.1", 0)) + tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + tcp.bind(udp.getsockname()) + tcp.listen(16) + # Duplicate and close, as inheriting across the launcher's exec does. + udp_fd, tcp_fd = os.dup(udp.fileno()), os.dup(tcp.fileno()) + udp.close() + tcp.close() + relay.adopt(udp_fd, tcp_fd) + self.assertEqual(relay.listen, relay.udp.getsockname()) + + if __name__ == "__main__": unittest.main() diff --git a/tests/test_netns_identity.py b/tests/test_netns_identity.py index 1016f44..31bdda9 100644 --- a/tests/test_netns_identity.py +++ b/tests/test_netns_identity.py @@ -14,6 +14,7 @@ """ import os +import socket import subprocess import sys import unittest @@ -22,9 +23,34 @@ LIB = os.path.join(HERE, "..", "lib", "bwrap_sandbox_lib.sh") +INSPECT = """ +import fcntl, socket, struct, sys +SIOCGIFFLAGS, IFF_UP = 0x8913, 0x1 +udp_fd, tcp_fd = int(sys.argv[1]), int(sys.argv[2]) +c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +flags = struct.unpack('16sh22x', fcntl.ioctl(c, SIOCGIFFLAGS, struct.pack('16sh22x', b'lo', 0)))[1] +u = socket.fromfd(udp_fd, socket.AF_INET, socket.SOCK_DGRAM) +t = socket.fromfd(tcp_fd, socket.AF_INET, socket.SOCK_STREAM) +print('lo_up=%d' % bool(flags & IFF_UP)) +print('udp=%s:%d' % u.getsockname()) +print('tcp=%s:%d' % t.getsockname()) +print('udp_type=%d' % u.type) +print('listening=%d' % t.getsockopt(socket.SOL_SOCKET, socket.SO_ACCEPTCONN)) +""" + _SNIPPET = None +def shell_var(name): + """Read a plain variable out of the shell library.""" + out = subprocess.run( + ["bash", "-c", 'source "$1" 2>/dev/null; printf "%%s" "${%s}"' % name, "_", LIB], + stdout=subprocess.PIPE, + check=True, + ) + return out.stdout.decode() + + def snippet(): """The value of _NET_NS_IDENTITY, as the launcher would pass it.""" global _SNIPPET @@ -108,5 +134,64 @@ def test_failure_to_exec_is_reported(self): self.assertNotEqual(got.returncode, 0) +@unittest.skipUnless(userns_available(), "unprivileged user namespaces are unavailable") +class NetNsIdentityDnsTests(unittest.TestCase): + """The --dns pre-bind, which the DNS relay depends on under slirp4netns. + + The relay is started after two execs and the namespace maps the real uid, + so it holds no capabilities and cannot bind port 53 or raise loopback. + The mapper does both while it still can, between the unshare and the exec. + """ + + def setUp(self): + self.addr = shell_var("_NET_DNS_RELAY_ADDR") + self.port = shell_var("_NET_DNS_RELAY_PORT") + self.udp_fd = shell_var("_NET_FD_DNS_UDP") + self.tcp_fd = shell_var("_NET_FD_DNS_TCP") + + def fields(self, *extra): + got = run( + "--dns", + self.addr, + self.port, + self.udp_fd, + self.tcp_fd, + "--", + sys.executable, + "-c", + INSPECT, + self.udp_fd, + self.tcp_fd, + *extra, + ) + self.assertEqual(got.returncode, 0, got.stderr.decode()) + return dict(line.split("=", 1) for line in got.stdout.decode().split()) + + def test_descriptors_are_bound_to_the_relay_address(self): + f = self.fields() + self.assertEqual(f["udp"], "%s:%s" % (self.addr, self.port)) + self.assertEqual(f["tcp"], "%s:%s" % (self.addr, self.port)) + + def test_loopback_is_up(self): + """A fresh network namespace has lo down, so :53 could not be bound.""" + self.assertEqual(self.fields()["lo_up"], "1") + + def test_tcp_socket_is_listening(self): + self.assertEqual(self.fields()["listening"], "1") + + def test_udp_socket_is_a_datagram_socket(self): + self.assertEqual(int(self.fields()["udp_type"]), int(socket.SOCK_DGRAM)) + + def test_without_dns_the_descriptors_are_absent(self): + got = run(sys.executable, "-c", INSPECT, self.udp_fd, self.tcp_fd) + self.assertNotEqual(got.returncode, 0) + self.assertIn("Bad file descriptor", got.stderr.decode()) + + def test_a_bad_address_fails_the_launch(self): + got = run("--dns", "203.0.113.9", self.port, self.udp_fd, self.tcp_fd, "--", "/bin/true") + self.assertNotEqual(got.returncode, 0) + self.assertIn("DNS relay sockets", got.stderr.decode()) + + if __name__ == "__main__": unittest.main() From 6918ce7d347ad1cbae60f13cd244dd9bed2840a5 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 12 Nov 2026 22:44:22 -0500 Subject: [PATCH 32/37] fix(bwrap): drop the untested pasta and bwrap 0.4.0 check Nothing here ran pasta with bwrap 0.4.0: the RHEL 8 hosts tested have bwrap 0.4.0 and no pasta. The check refused pasta when bwrap was older than 0.4.1 on the strength of reading the source, so it goes, and _net_try_pasta is upstream's again. The docs say the combination is not tested. The comments and docs on the identity map now hold only what was seen or read in a named source. Gone: the util-linux version numbers, the comparison with rootless podman, what restoring --enable-sandbox would take, and the claim that nothing in the sandbox runs as uid 0 (the sandbox's own uid_map, 409598 0 1, maps it onto uid 0 of the namespace above it). Assisted-by: opencode:claude-opus-5-5 --- docs/bw-wrappers.md | 92 ++++++++++-------------- lib/bwrap_netlog.py | 13 ++-- lib/bwrap_sandbox_lib.sh | 131 ++++++++++------------------------- tests/fakes/_nsproxy.py | 13 ++-- tests/test_netns_identity.py | 7 +- 5 files changed, 90 insertions(+), 166 deletions(-) diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 98ed92e..4e15c7f 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -18,8 +18,7 @@ option list; the tables below summarise the common surface. - `bubblewrap` (`bwrap`) >= 0.4.0 - binary masking is verified on RHEL 8.10 (`bubblewrap-0.4.0-3.el8_10`) and EL9 (0.6.3) - For the private network namespace, one of: `pasta` (package `passt`, - RHEL 9 and later) **with bwrap >= 0.4.1** (see - [Implementation notes: pasta](#implementation-notes-pasta)), or + RHEL 9 and later), or `slirp4netns` >= 0.4.0 with libslirp >= 4.3.0 (package `slirp4netns`, RHEL 8 AppStream) and `python3` (or RHEL 8's `/usr/libexec/platform-python`); plus `/dev/net/tun` and unprivileged user @@ -550,13 +549,14 @@ field, for example the domains an agent reached: - pasta maps you to uid 0 in its user namespace, so bwrap runs the agent in a nested user namespace as your real uid and gid (`--unshare-user --uid --gid`); file ownership looks as before and the agent has no capabilities. -- **pasta needs bwrap 0.4.1 or later** and the launcher checks before using - it. In 0.4.0 `drop_privs()` tests `geteuid() == 0` instead of "installed - setuid root", so inside pasta's namespace — where uid 0 is the only mapped - id — it calls `setuid()` on an unmapped id and dies with - `unable to drop root uid: Invalid argument`. 0.4.1 changed the guard to - `is_privileged`. RHEL 8 ships 0.4.0, but also has no pasta, so in practice - this only bites a hand-built pasta or a backported one. +- **Not tested: pasta with bwrap 0.4.0.** pasta maps you to uid 0 alone + (its source writes `0 1`), and bwrap 0.4.0 run with `--unshare-user + --uid ` in such a namespace stopped with `unable to drop root uid: + Invalid argument` on RHEL 8.10 (see + [Implementation notes: slirp4netns](#implementation-notes-slirp4netns)). + Whether pasta with bwrap 0.4.0 hits the same error has not been tried: the + RHEL 8 hosts tested have bwrap 0.4.0 and no pasta. The launcher does not + check for it. - pasta renames the host to `pasta-`; bwrap's `--unshare-uts --hostname` restores the real name. - `--no-pidns` is passed when the installed pasta supports it (2026-09 and @@ -590,23 +590,18 @@ launcher ─┬─ helper (background): waits for the namespace, starts slirp4ne it. - **The ids are mapped to themselves, not to root.** The launcher unshares and writes the maps itself, in about fifteen lines of Python - (`_NET_NS_IDENTITY`), rather than calling `unshare --user --map-root-user - --net`, for two reasons: - - RHEL 8 is the only platform this backend exists for, and its bwrap is - 0.4.0, which cannot start under a root-only map at all (see the pasta - notes above). An identity map leaves your uid mapped, so bwrap's - `--uid`/`--gid` name an id that exists. - - Nothing in the sandbox runs as uid 0, not even for the moment before bwrap - switches back. - - util-linux grew `--map-user`/`--map-current-user` in 2.38 and could do this - directly, but RHEL 8 has 2.32, whose `unshare` offers only - `--map-root-user`. Writing a single-line map of your own id needs no - privilege, so this needs neither `newuidmap`/`newgidmap` nor `/etc/subuid` - entries — unlike rootless podman, which uses both to build a much wider map - and then runs the payload as uid 0 inside it. slirp4netns still gets full - capabilities when it joins, because the namespace is owned by your uid, - which is the uid it runs as. + (`_NET_NS_IDENTITY`), instead of calling `unshare --user --map-root-user + --net`. With that root map, bwrap 0.4.0 (RHEL 8.10) run with `--uid ` stopped with `unable to drop root uid: Invalid argument`: its + `drop_privs()` calls `setuid()` when `geteuid() == 0`, and your + uid is not mapped there (`EINVAL`). With your uid mapped to itself, bwrap + starts and the agent runs as your uid. slirp4netns joins the namespace and + configures `tap0` in it. + + Writing a single-line map of your own id needs no helper (no + `newuidmap`, no `/etc/subuid`). The `unshare` on these hosts documents + `--map-root-user` and no option that maps your own uid (its man page), so + the launcher writes the map itself. - Python (`python3`, or RHEL 8's `/usr/libexec/platform-python`) is therefore required for this backend, not only for `--publish`. Standard library only, 3.6 or later. @@ -619,35 +614,22 @@ launcher ─┬─ helper (background): waits for the namespace, starts slirp4ne `/run`. - **DNS lookups are logged**, and with no flow log they are the only account of what the sandbox reached for. The relay runs inside the network - namespace and forwards to the backend's own DNS, so it is not pasta-only — - but it cannot bind port 53 for itself here. Capabilities survive `execve` - only for uid 0, and this namespace maps your uid, so by the time the relay - starts (after the mapper's exec and the descriptor relay's) it holds none. - The mapper therefore raises loopback and binds the relay's UDP and TCP - sockets while it still can, between the unshare and the exec, and leaves - them on descriptors 88 and 89 for `DnsRelay.adopt`. A fresh namespace also - has `lo` down, and slirp4netns only raises it later in `configure_network`, - so binding early removes that race too. -- **`--enable-sandbox` is not passed**, and this is the cost of the identity - map. After joining the launcher's namespace, slirp4netns does - `setegid(0)`/`seteuid(0)` (`main.c`); with no uid 0 mapped that fails and - the launch stops with `setegid(0)` / `parent failed`. An unprivileged - process may write only one line into `uid_map` — the line mapping its own - id — so the namespace can map uid 0 *or* your uid, not both: - - | map | slirp4netns `--enable-sandbox` | bwrap 0.4.0 | - | --- | --- | --- | - | uid 0 only | works | dies, unless the agent runs as uid 0 | - | your uid (what we do) | cannot `setegid(0)` | works, agent keeps its uid | - - Your uid wins: it is what the agent runs as and what every file it writes - is owned by, whereas `--enable-sandbox` only hardens slirp4netns itself (a - mount namespace pivot-rooted into an empty directory, plus dropping - capabilities). `--enable-seccomp`, which bounds what a compromised libslirp - can call, is a separate flag with no id requirement and is still passed. - Mapping both ids needs a multi-line map, which takes `newuidmap`/`newgidmap` - and an `/etc/subuid` entry — the way rootless podman does it, and the way to - restore `--enable-sandbox` if it is wanted. + namespace and forwards to the backend's own DNS, so it is not pasta-only, + but here it cannot bind port 53 for itself: under this map, after the + mapper's exec and the descriptor relay's, it holds no capabilities (seen: + `CapEff` 0, the bind refused). The mapper, which still has them after the + unshare, brings loopback up (a new namespace starts with `lo` down) and + binds the relay's UDP and TCP sockets before its exec, and leaves them on + descriptors 88 and 89 for `DnsRelay.adopt`. +- **`--enable-sandbox` is not passed.** After joining the launcher's + namespace, slirp4netns does `setegid(0)` (`main.c`); with no uid 0 mapped + the launch stopped with `setegid(0)` / `parent failed`. Without privilege + a process may write a single line to `uid_map`, mapping its own uid + (`user_namespaces(7)`), so this namespace maps your uid and not uid 0. + slirp4netns describes `--enable-sandbox` as "create a new mount namespace + (and drop all caps except CAP_NET_BIND_SERVICE if running as the root)". + `--enable-seccomp` is a separate option (slirp4netns applies the two + independently) and is still passed; launches with it work under this map. - slirp4netns runs in its own session (Ctrl-C does not reach it) with a scrubbed environment (no API keys). It exits when bwrap does: its `--exit-fd` is the other end of bwrap's `--sync-fd`. diff --git a/lib/bwrap_netlog.py b/lib/bwrap_netlog.py index 33edc71..d18da0c 100644 --- a/lib/bwrap_netlog.py +++ b/lib/bwrap_netlog.py @@ -625,12 +625,13 @@ def bind(self): def adopt(self, udp_fd, tcp_fd): """Take over sockets bound before the exec that dropped our privileges. - Binding port 53 needs CAP_NET_BIND_SERVICE, and capabilities survive - execve only for uid 0. With pasta the namespace maps the caller to 0, - so bind() works; with slirp4netns the namespace maps the real uid, the - exec chain leaves us with no capabilities at all, and the sockets are - bound instead by the process that creates the namespace and handed - over here (see _NET_NS_IDENTITY in bwrap_sandbox_lib.sh). + Binding port 53 needs CAP_NET_BIND_SERVICE. With pasta, whose + namespace maps the caller to uid 0, the relay binds for itself + (bind()). With slirp4netns the namespace maps the real uid, and after + the exec chain the relay holds no capabilities (seen: CapEff 0, the + bind refused), so the process that creates the namespace binds the + sockets and hands them over here (see _NET_NS_IDENTITY in + bwrap_sandbox_lib.sh). """ self.udp = socket.fromfd(udp_fd, socket.AF_INET, socket.SOCK_DGRAM) self.tcp = socket.fromfd(tcp_fd, socket.AF_INET, socket.SOCK_STREAM) diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index 72ed07a..fba6f4b 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -224,37 +224,6 @@ _version_ge() { [[ "$(printf '%s\n%s\n' "${a}" "${b}" | sort -V | head -n1)" == "${b}" ]] } -# Lowest bwrap usable under a user namespace that maps the caller to uid 0 and -# maps nothing else (what pasta builds, and what util-linux `unshare -# --map-root-user` writes). bubblewrap 0.4.0's drop_privs() is -# -# if (geteuid () == 0 && setuid (opt_sandbox_uid) < 0) -# die_with_error ("unable to drop root uid"); -# -# which mistakes "root in a user namespace" for "installed setuid root": it -# calls setuid() on an id that namespace does not map, gets EINVAL -# and dies with "unable to drop root uid: Invalid argument" before the sandbox -# starts. 0.4.1 changed the guard to is_privileged. RHEL 8 ships 0.4.0, one -# patch release short, so this has to be checked and not assumed. -_BWRAP_MIN_VERSION_MAPROOT=0.4.1 - -# _bwrap_version -# Print the version of the bwrap that will be used ("0.4.0"), nothing and -# return 1 if it cannot be determined. Cached: bwrap is resolved and run -# once per launch. -_BWRAP_VERSION="" -_bwrap_version() { - local _bin _out - if [[ -z "${_BWRAP_VERSION}" ]]; then - _bin="$(resolve_bwrap_bin)" || return 1 - [[ -n "${_bin}" && -x "${_bin}" ]] || return 1 - _out="$("${_bin}" --version 2> /dev/null)" || return 1 - [[ "${_out}" =~ bubblewrap\ ([0-9][0-9.]*) ]] || return 1 - _BWRAP_VERSION="${BASH_REMATCH[1]}" - fi - printf '%s' "${_BWRAP_VERSION}" -} - # Detect kernel-level security features that affect bwrap argument choice. # # TIOCSTI capability guard (Linux 5.14+): @@ -2504,27 +2473,13 @@ detect_net_capabilities() { # assumed to work. _NET_TRY_REASON="" _net_try_pasta() { - local _out _rc _bver + local _out _rc _NET_TRY_REASON="" _PASTA_BIN="$(command -v pasta 2> /dev/null || true)" if [[ -z "${_PASTA_BIN}" || ! -x "${_PASTA_BIN}" ]]; then _NET_TRY_REASON="not found on PATH (package passt)" return 1 fi - # pasta builds the user namespace itself and maps the caller to uid 0 in - # it, so bwrap has to switch back to the real uid: see - # _BWRAP_MIN_VERSION_MAPROOT. Unlike the slirp4netns backend, which maps - # the ids itself, there is nothing to work around here, so refuse the - # backend rather than fail at exec with bwrap's obscure message. - _bver="$(_bwrap_version 2> /dev/null || true)" - if [[ -z "${_bver}" ]]; then - _NET_TRY_REASON="the bwrap version could not be read ('bwrap --version'; ${_BWRAP_MIN_VERSION_MAPROOT} or later is needed)" - return 1 - fi - if ! _version_ge "${_bver}" "${_BWRAP_MIN_VERSION_MAPROOT}"; then - _NET_TRY_REASON="bwrap ${_bver} cannot run in pasta's root-mapped user namespace (${_BWRAP_MIN_VERSION_MAPROOT} or later is needed)" - return 1 - fi # --no-pidns (2026-09) is not in RHEL 9 / EPEL builds; without it pasta # adds a PID namespace, inside which bwrap nests its own. if _out="$(timeout --signal=TERM --kill-after=2s 5s "${_PASTA_BIN}" --help 2>&1)"; then @@ -3109,8 +3064,9 @@ _net_log_print_dry_run() { # --cidr 192.168.254.0/24 (BNL uses 10/8, slirp's default # range); sandbox .100, gateway .2, DNS .3. # --enable-seccomp as podman does, when /etc/resolv.conf resolves -# inside /etc or /run (required by --enable-sandbox, -# which the resolv.conf test was written for). +# inside /etc or /run (the condition this launcher +# set for --enable-sandbox and --enable-seccomp +# together, kept as it was). # --enable-sandbox only when the namespace maps uid 0, which this # launcher's does not; see _NET_NS_ROOT_MAPPED. # --publish works through the API socket (add_hostfwd, IPv4 TCP, one port per @@ -3167,53 +3123,36 @@ for spec in sys.argv[2:]: sys.exit("%s:%s -> sandbox port %s: %s" % (addr, hport, gport, reply.get("error", reply))) ' -# Whether the namespace the launcher builds maps uid and gid 0. -# -# It does not, and the two backends cannot both have what they want, because -# an unprivileged process may write only ONE line into uid_map -- the line -# mapping its own id: +# Whether the namespace the launcher builds maps uid and gid 0. It does not: +# it maps the real uid and gid to themselves (_NET_NS_IDENTITY). Without +# privilege a process may write a single line to uid_map, mapping its own +# effective uid (user_namespaces(7)), so the map holds uid 0 or the real uid, +# not both. Seen on RHEL 8.10 (bubblewrap 0.4.0, slirp4netns 1.2.3): # -# map 0 only slirp4netns --enable-sandbox works; bwrap 0.4.0 dies -# with "unable to drop root uid" unless the agent is -# left running as uid 0 (see _BWRAP_MIN_VERSION_MAPROOT) -# map the real id bwrap works and the agent keeps its own uid; -# slirp4netns --enable-sandbox cannot setegid(0) +# map 0 only bwrap --unshare-user --uid stops with +# "unable to drop root uid: Invalid argument" (0.4.0's +# drop_privs() calls setuid() when geteuid() == 0, and +# the real uid is not mapped: EINVAL) +# map the real id bwrap starts and the sandbox runs as the real uid; +# slirp4netns --enable-sandbox stops at setegid(0) +# ("parent failed"), so it is not passed # -# The real id wins: it is what the sandbox runs as, and the agent's uid is a -# property of every file it writes, whereas --enable-sandbox only hardens -# slirp4netns itself (a mount namespace pivot_rooted into an empty directory, -# plus dropping capabilities). --enable-seccomp is kept; it is a separate -# flag with no id requirement, and it is the filter that bounds what a -# compromised libslirp could call. -# -# Mapping both would need a multi-line map, which takes the setuid helpers -# newuidmap/newgidmap and an /etc/subuid entry -- how rootless podman does it. -# That is the way to restore --enable-sandbox if it is wanted; it needs the -# helper to write the map for the launcher's pid between the unshare and the -# exec, so it is a real change, not a flag. +# --enable-sandbox is what slirp4netns describes as "create a new mount +# namespace (and drop all caps except CAP_NET_BIND_SERVICE if running as the +# root)". --enable-seccomp is a separate option (slirp4netns.c applies the two +# independently) and is still passed; launches with it work under this map. _NET_NS_ROOT_MAPPED=0 # The identity mapper, which replaces `unshare --user --map-root-user --net`: # unshare a user and a network namespace, map the real uid and gid to -# themselves, then exec the rest of the argument list. +# themselves, then exec the rest of the argument list. Writing a single-line +# map of one's own id needs no helper (no newuidmap, no /etc/subuid). The +# unshare on these hosts documents --map-root-user and no option that maps +# one's own uid (its man page), so the launcher does this itself. # # It does not fork, so the launcher keeps its pid: $$ is what the helper waits -# for and what slirp4netns is pointed at. -# -# An identity map, rather than util-linux's root map, for two reasons: -# - bwrap 0.4.0 (RHEL 8, the only platform this backend exists for) cannot -# start under a root-only map at all; see _BWRAP_MIN_VERSION_MAPROOT. -# - the sandbox keeps the real uid without bwrap having to switch back to -# it, so nothing in it runs as uid 0 even for the moment before bwrap -# drops privileges. -# util-linux grew --map-user/--map-current-user in 2.38 and could do this -# directly; RHEL 8 has 2.32, whose unshare offers only --map-root-user. -# -# A process may always write a single-line map of its own id without any -# helper, so this needs neither newuidmap/newgidmap nor /etc/subuid entries -# (which is how rootless podman builds a much wider map). slirp4netns still -# gets full capabilities when it joins: the namespace is owned by the real -# uid, which is the uid it runs as. +# for and what slirp4netns is pointed at. slirp4netns joins this namespace +# and configures tap0 in it (seen: "sent tapfd=5 for tap0" after --configure). # # Both ids must be read before the unshare; afterwards the process is the # overflow id (65534) and a map naming that id is refused with EPERM. @@ -3223,16 +3162,16 @@ _NET_NS_ROOT_MAPPED=0 # the relay that the descriptor relay spawns later. That work has to happen # HERE, in this process, between the unshare and the exec: # -# - creating the user namespace grants a full capability set, so this -# process has CAP_NET_ADMIN (loopback up) and CAP_NET_BIND_SERVICE -# (port 53); -# - execve recomputes the sets, and with no file capabilities and a -# non-zero euid it clears them. Under pasta the namespace maps the -# caller to 0 and capabilities survive, which is why the relay binds for -# itself there; here nothing downstream of the exec can. +# - after the unshare this process has a full capability set in the +# namespace, including CAP_NET_ADMIN (loopback up) and +# CAP_NET_BIND_SERVICE (port 53); +# - after an execve the set was empty under this map (CapEff 0, bind to +# port 53 refused), while under a map of uid 0 it was kept. pasta writes +# a map of uid 0 ("0 1", pasta_start_ns), and there the relay binds +# for itself. # -# Bringing lo up also removes a race: slirp4netns raises lo in -# configure_network, but the descriptor relay runs before it is ready. +# A new network namespace starts with lo down. slirp4netns brings lo up too +# (configure_network), but only once it has started, after this exec. _NET_NS_IDENTITY=' import ctypes, fcntl, os, socket, struct, sys CLONE_NEWUSER, CLONE_NEWNET = 0x10000000, 0x40000000 diff --git a/tests/fakes/_nsproxy.py b/tests/fakes/_nsproxy.py index a8686fa..70acb30 100644 --- a/tests/fakes/_nsproxy.py +++ b/tests/fakes/_nsproxy.py @@ -32,12 +32,13 @@ def _keep_caps_across_exec(): After joining a user namespace we own, this process has every capability in it. The real slirp4netns configures the interface right there, with - ioctls in the same process. The fake execs `ip` instead, and execve - clears the capability sets unless the process is uid 0 in the namespace: - true when the launcher mapped the caller to root, false with the identity - map the slirp4netns launch uses (_NET_NS_IDENTITY). Raising each - permitted capability into the ambient set keeps them across the exec, so - the fake can do what the real program does. + ioctls in the same process (configure_network). The fake execs `ip` + instead. Under the launcher's old map of uid 0 the exec kept the + capabilities; under the identity map the slirp4netns launch now uses + (_NET_NS_IDENTITY) it does not (seen: `ip link set lo up` fails with + "Operation not permitted"). Raising each permitted capability into the + ambient set keeps them across the exec, so the fake can do what the real + program does. """ libc = ctypes.CDLL(None, use_errno=True) header = ctypes.create_string_buffer(struct.pack("II", 0x20080522, 0)) # v3, this thread diff --git a/tests/test_netns_identity.py b/tests/test_netns_identity.py index 31bdda9..18c8c53 100644 --- a/tests/test_netns_identity.py +++ b/tests/test_netns_identity.py @@ -5,9 +5,10 @@ The snippet replaces `unshare --user --map-root-user --net` for the slirp4netns backend: it unshares a user and a network namespace, maps the real uid and gid to themselves and execs its arguments in the same process. -The identity map matters because bubblewrap 0.4.0 (RHEL 8) cannot start under -a namespace that maps only uid 0 -- it calls setuid() on an unmapped id and -dies with "unable to drop root uid: Invalid argument". +The identity map matters because bubblewrap 0.4.0 (seen on RHEL 8.10), run +with --unshare-user --uid in a namespace that maps only uid 0, +calls setuid() on the unmapped real uid and stops with "unable to drop root +uid: Invalid argument". The snippet is read out of the shell library rather than copied, so the tests fail if the two drift apart. From 41147bb871374a9ea956098e2c64ef27dfdd1909 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 12 Nov 2026 23:38:34 -0500 Subject: [PATCH 33/37] docs(bwrap): explain how the network log is written and read Describe the syslog line format, that only the start and end records name the user, and how to find one user's DNS lookups through their session IDs. State which sessions have no DNS or flow records. Assisted-by: opencode:claude-sonnet-5-5 --- docs/bw-wrappers.md | 68 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 4e15c7f..459f14a 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -438,9 +438,9 @@ change them, and they are the data needed for a later egress allowlist. | fail | `session=ID flow=9 fail ...`: a connection that never became active (refused, unreachable) | | end | `event=end session=ID ...`, when the sandbox has exited | -Every line is `key=value` (logfmt), so Loki's `| logfmt` can filter on any -field, for example the domains an agent reached: -`{syslog_identifier="bwrap-net"} | logfmt | dir="out"` grouped by `domain`. +Every line is `key=value` (logfmt), so a log store's logfmt parser can filter +on any field. See [Reading the network log](#reading-the-network-log) for how +to fetch the records. - `dir` is `out` (the sandbox to the network), `publish` (a host client to a `--publish` port) or `host-port` (the sandbox to a `--host-port` service). @@ -459,6 +459,11 @@ field, for example the domains an agent reached: - **Tag** (`SYSLOG_IDENTIFIER`): `bwrap-net`. `journalctl -t bwrap-net` shows all sessions; `journalctl -t bwrap-net -g ID` one. Inside the sandbox `BWRAP_SESSION` is the same ID, and the context file names it. +- **Who logged it.** Only the `start` and `end` records name the user + (`user=`, `uid=`, `launcher=`, `dir=`). Every other record (dns, open, close, + fail) carries only `session=ID`. To list what one user's agents did, first + find that user's session IDs from the `start` records, then select the other + records by those IDs. - **Tool:** `systemd-cat` when the journal's stream socket exists and accepts a connection, else `logger` (util-linux) on `/dev/log` (journald or rsyslog). Both are tried at launch without logging anything; the launcher then writes @@ -538,6 +543,63 @@ field, for example the domains an agent reached: `/usr/libexec/platform-python`, else `python3`). Without it pasta's lines go to the log unfiltered, and DNS goes straight to `169.254.1.1`. +### Reading the network log + +**Wire format.** Each record is one syslog line (RFC 5424: priority `<14>`, +timestamp, host, tag `bwrap-net`, the writer's pid) followed by the logfmt +fields, for example: + +```text +<14>1 2026-01-01T12:00:00+00:00 HOST bwrap-net 1234 - - session=0123456789ab dns domain=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21 +<14>1 2026-01-01T12:00:05+00:00 HOST bwrap-net 1234 - - session=0123456789ab flow=7 open proto=tcp dir=out src=10.0.0.5:44464 dst=140.82.112.21:443 domain=github.com t=21.359 +``` + +The records are written on the host the launcher runs on, so the first place to +read them is that host's journal. If you forward syslog to a central log store, +the records are there too, with the tag and the sending host as the fields your +pipeline keeps from the syslog header. Which names it gives them depends on the +pipeline (the journal's `SYSLOG_IDENTIFIER` is not always kept as a label), so +check the names in your store before writing a selector. The message is the +whole line above. + +**On the host (journal).** + +```bash +journalctl -t bwrap-net --since '-12h' # every session on this host +journalctl -t bwrap-net -g 'session=ID' # one session +journalctl -t bwrap-net -g 'event=start.*user=NAME' # a user's sessions +journalctl -t bwrap-net -g ' dns domain=' # DNS lookups only +``` + +Inside a sandbox, `BWRAP_SESSION` is the session ID (the context file names it +too). + +**Everything one user's agents looked up.** Records after `start` do not name +the user, so this takes two steps: + +1. List that user's `start` records (`journalctl -t bwrap-net -g + 'event=start.*user=NAME'`). They give the session IDs, and also the + launcher, directory and backend, which say whether flows and DNS were + logged. +2. Select the `dns` records of those sessions, for example + `journalctl -t bwrap-net -g 'session=(ID1|ID2) dns domain='`, and count by + `domain`. + +A and AAAA lookups are separate records (`type=`), so count by `domain` and +`type` to tell them apart, or divide by about two for the number of names +looked up. + +**What is and is not in the log.** + +- Sessions with `dns=unavailable` (`--share-net`, `backend=none`) have a start + and an end record only. +- Sessions with `flows=unavailable` (slirp4netns) have DNS records but no + `open`/`close` records. For those, DNS is the only account of what was + reached. +- `domain` on a flow is the last relayed lookup that returned that address, so + treat it as a record, not proof (see above). +- A session that is still running has no `end` record yet. + ### Implementation notes: pasta - pasta runs outside bwrap, as you, and starts the sandbox. Default pasta From c31325c52a52a84f04985df3dde43ba909a63d5a Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 12 Nov 2026 23:39:15 -0500 Subject: [PATCH 34/37] fix(bwcodex): drop --perms so it works with bwrap 0.4.0 --perms needs bwrap 0.5.0 and RHEL 8 ships 0.4.0. The daemon directory and settings file live in the sandbox's private tmpfs, so their modes guard nothing. --- bin/bwcodex | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/bwcodex b/bin/bwcodex index 5e4fdb2..1f246b5 100755 --- a/bin/bwcodex +++ b/bin/bwcodex @@ -490,9 +490,11 @@ build_codex_home_mount() { # Keep the managed daemon package in tmpfs. The complete CLI package # mounted by build_codex_bin_mount seeds it on each launch. Disable the # daemon's updater before Codex starts so it does not fetch a newer copy. - BWRAP_ARGS+=(--perms 0700 --dir "${CODEX_HOME_DIR}/app-server-daemon") + # No --perms: it needs bwrap 0.5.0 and RHEL 8 ships 0.4.0. Both paths + # live in the sandbox's private tmpfs, so their modes guard nothing. + BWRAP_ARGS+=(--dir "${CODEX_HOME_DIR}/app-server-daemon") bwrap_fd_from_data "Codex daemon settings" '{"updater":{"autoUpdateEnabled":false}}' - BWRAP_ARGS+=(--perms 0600 --file "${_BWRAP_FD}" "${CODEX_DAEMON_SETTINGS_FILE}") + BWRAP_ARGS+=(--file "${_BWRAP_FD}" "${CODEX_DAEMON_SETTINGS_FILE}") # auth.json: only persist if it already exists on the host. # Use --init-auth on first run to bootstrap persistent ChatGPT-login auth. From e8738f066e72f826f1e7f9ff2af21d5d52674b9e Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 12 Nov 2026 23:50:02 -0500 Subject: [PATCH 35/37] Change shell command from sh to bash it test Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- tests/test_fd_relay.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_fd_relay.py b/tests/test_fd_relay.py index 2c4c433..eb3e7bc 100644 --- a/tests/test_fd_relay.py +++ b/tests/test_fd_relay.py @@ -33,7 +33,7 @@ env=() for n in "${_BWRAP_FD_DATA_NUMS[@]}"; do env+=("_BWRAP_FD_DATA_${n}=${_BWRAP_FD_DATA[${n}]}"); done exec env "${env[@]}" bash -c "${_BWRAP_FD_RELAY_SHIM}" bwrap-fd-relay "${_BWRAP_FD_RELAY[@]}" -- \ - sh -c 'for n in 10 11 12; do printf "%s=%s\n" "$n" "$(eval "cat <&$n")"; done' 2>/dev/null + bash -c 'for n in 10 11 12; do printf "%s=%s\n" "$n" "$(eval "cat <&$n")"; done' 2>/dev/null """ DATA = ["10", "data", "11", "data", "12", "data"] From 2d459f10723c2f13a011569c8fa6492ca3f0c9cf Mon Sep 17 00:00:00 2001 From: "Stuart B. Wilkins" Date: Sun, 15 Nov 2026 16:47:42 -0500 Subject: [PATCH 36/37] feat(bwrap): name the user on every network log record Only the start and end records carried user=, so a dns, open, close or fail line could be attributed only by looking up its session. Every record now begins session=ID user=NAME, written by the condenser (--user) and by the fallback supervisor used without Python. The name is the account of the launcher process (id -un), not $USER, which the caller can set, and characters that could split a logfmt field are replaced. The start and end records use the same value. Assisted-by: opencode:claude-sonnet-5-5 --- docs/bw-wrappers.md | 51 +++++++++++++++++--------------- lib/bwrap_netlog.py | 36 +++++++++++++++------- lib/bwrap_sandbox_lib.sh | 21 ++++++++++--- tests/test_netlog.py | 64 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 134 insertions(+), 38 deletions(-) diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 459f14a..cfff67f 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -431,16 +431,18 @@ change them, and they are the data needed for a later egress allowlist. | Record | Content | | --- | --- | -| start | `event=start session=ID launcher=... user=... uid=... pid=... backend=... flows=logged\|unavailable pasta=VERSION publish=ADDR:PORTS host_ports=... dir=...` | -| dns | `session=ID dns domain=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21`, plus `cname=...` when the answer went through aliases | -| open | `session=ID flow=7 open proto=tcp dir=out src=ADDR:PORT dst=140.82.112.21:443 domain=github.com t=21.359` | -| close | `session=ID flow=7 close proto=tcp dir=out dst=140.82.112.21:443 domain=github.com t=33.770 secs=12.411` | -| fail | `session=ID flow=9 fail ...`: a connection that never became active (refused, unreachable) | +| start | `event=start session=ID launcher=... user=NAME uid=... pid=... backend=... flows=logged\|unavailable pasta=VERSION publish=ADDR:PORTS host_ports=... dir=...` | +| dns | `session=ID user=NAME dns domain=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21`, plus `cname=...` when the answer went through aliases | +| open | `session=ID user=NAME flow=7 open proto=tcp dir=out src=ADDR:PORT dst=140.82.112.21:443 domain=github.com t=21.359` | +| close | `session=ID user=NAME flow=7 close proto=tcp dir=out dst=140.82.112.21:443 domain=github.com t=33.770 secs=12.411` | +| fail | `session=ID user=NAME flow=9 fail ...`: a connection that never became active (refused, unreachable) | | end | `event=end session=ID ...`, when the sandbox has exited | Every line is `key=value` (logfmt), so a log store's logfmt parser can filter -on any field. See [Reading the network log](#reading-the-network-log) for how -to fetch the records. +on any field. Every record after the start names both the session and the +user who started it, so a line can be attributed on its own. See +[Reading the network log](#reading-the-network-log) for how to fetch the +records. - `dir` is `out` (the sandbox to the network), `publish` (a host client to a `--publish` port) or `host-port` (the sandbox to a `--host-port` service). @@ -459,11 +461,15 @@ to fetch the records. - **Tag** (`SYSLOG_IDENTIFIER`): `bwrap-net`. `journalctl -t bwrap-net` shows all sessions; `journalctl -t bwrap-net -g ID` one. Inside the sandbox `BWRAP_SESSION` is the same ID, and the context file names it. -- **Who logged it.** Only the `start` and `end` records name the user - (`user=`, `uid=`, `launcher=`, `dir=`). Every other record (dns, open, close, - fail) carries only `session=ID`. To list what one user's agents did, first - find that user's session IDs from the `start` records, then select the other - records by those IDs. +- **Who logged it.** Every record carries `user=NAME`, the account of the + process that ran the launcher (`id -un`, not `$USER`, which the caller can + set to anything). Characters outside `A-Za-z0-9._@-` in it are replaced by + `_`, so it cannot split or forge a field. The `start` record also has `uid=`, + `launcher=` and `dir=`. The user is the one who started the sandbox: the log + does not say which process inside it made a request. Sessions started before + this field was added have `user=` on their `start` and `end` records only; + for those, find the session ID from the `start` record and select the other + records by it. - **Tool:** `systemd-cat` when the journal's stream socket exists and accepts a connection, else `logger` (util-linux) on `/dev/log` (journald or rsyslog). Both are tried at launch without logging anything; the launcher then writes @@ -550,8 +556,8 @@ timestamp, host, tag `bwrap-net`, the writer's pid) followed by the logfmt fields, for example: ```text -<14>1 2026-01-01T12:00:00+00:00 HOST bwrap-net 1234 - - session=0123456789ab dns domain=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21 -<14>1 2026-01-01T12:00:05+00:00 HOST bwrap-net 1234 - - session=0123456789ab flow=7 open proto=tcp dir=out src=10.0.0.5:44464 dst=140.82.112.21:443 domain=github.com t=21.359 +<14>1 2026-01-01T12:00:00+00:00 HOST bwrap-net 1234 - - session=0123456789ab user=alice dns domain=github.com type=A proto=udp rcode=NOERROR answers=140.82.112.21 +<14>1 2026-01-01T12:00:05+00:00 HOST bwrap-net 1234 - - session=0123456789ab user=alice flow=7 open proto=tcp dir=out src=10.0.0.5:44464 dst=140.82.112.21:443 domain=github.com t=21.359 ``` The records are written on the host the launcher runs on, so the first place to @@ -567,6 +573,7 @@ whole line above. ```bash journalctl -t bwrap-net --since '-12h' # every session on this host journalctl -t bwrap-net -g 'session=ID' # one session +journalctl -t bwrap-net -g 'user=NAME' # everything one user's sandboxes did journalctl -t bwrap-net -g 'event=start.*user=NAME' # a user's sessions journalctl -t bwrap-net -g ' dns domain=' # DNS lookups only ``` @@ -574,16 +581,12 @@ journalctl -t bwrap-net -g ' dns domain=' # DNS lookups only Inside a sandbox, `BWRAP_SESSION` is the session ID (the context file names it too). -**Everything one user's agents looked up.** Records after `start` do not name -the user, so this takes two steps: - -1. List that user's `start` records (`journalctl -t bwrap-net -g - 'event=start.*user=NAME'`). They give the session IDs, and also the - launcher, directory and backend, which say whether flows and DNS were - logged. -2. Select the `dns` records of those sessions, for example - `journalctl -t bwrap-net -g 'session=(ID1|ID2) dns domain='`, and count by - `domain`. +**Everything one user's agents looked up.** Select the user's `dns` records, +for example `journalctl -t bwrap-net -g 'user=NAME dns domain='`, and count by +`domain`. The `start` records (`-g 'event=start.*user=NAME'`) give the session +IDs, and also the launcher, directory and backend, which say whether flows and +DNS were logged. For sessions from before `user=` was on every record, find the +session IDs first, then select by them (`-g 'session=(ID1|ID2) dns domain='`). A and AAAA lookups are separate records (`type=`), so count by `domain` and `type` to tell them apart, or divide by about two for the number of names diff --git a/lib/bwrap_netlog.py b/lib/bwrap_netlog.py index d18da0c..2552f30 100644 --- a/lib/bwrap_netlog.py +++ b/lib/bwrap_netlog.py @@ -3,7 +3,7 @@ "Network log"). Standard library only; runs on Python 3.6 (RHEL 8 platform-python) and later. - bwrap_netlog.py condense --session ID [--dns-forward ADDR] + bwrap_netlog.py condense --session ID [--user NAME] [--dns-forward ADDR] [--fifo PATH --parent PID [--marker TEXT] [--end-record TEXT] [--session-dir DIR] [--backend NAME]] -- CMD... Read pasta's debug output (its stderr, and the DNS relay's lines) and @@ -24,13 +24,15 @@ LISTEN, for a namespace where this process cannot bind port 53 itself; "-" means bind here (see DnsRelay.adopt). -Output lines are logfmt (key=value), so Loki's `| logfmt` can filter them: +Output lines are logfmt (key=value), so Loki's `| logfmt` can filter them. +Every line names the session and the user who started it (--user), so a line +can be attributed without looking up the session's start record: - session=ID flow=7 open proto=tcp dir=out src=... dst=140.82.113.22:443 domain=github.com t=21.359 - session=ID flow=7 close proto=tcp dir=out dst=140.82.113.22:443 domain=github.com t=33.770 secs=12.411 - session=ID flow=9 fail proto=tcp dir=out dst=192.0.2.1:443 src=... t=40.120 - session=ID dns domain=github.com type=A rcode=NOERROR answers=140.82.113.22 - session=ID dns domain=api.x.com type=A rcode=NOERROR answers=192.0.2.1 cname=lb.x.net + session=ID user=NAME flow=7 open proto=tcp dir=out src=... dst=140.82.113.22:443 domain=github.com t=21.359 + session=ID user=NAME flow=7 close proto=tcp dir=out dst=140.82.113.22:443 domain=github.com t=33.770 secs=12.411 + session=ID user=NAME flow=9 fail proto=tcp dir=out dst=192.0.2.1:443 src=... t=40.120 + session=ID user=NAME dns domain=github.com type=A rcode=NOERROR answers=140.82.113.22 + session=ID user=NAME dns domain=api.x.com type=A rcode=NOERROR answers=192.0.2.1 cname=lb.x.net """ # Kept to what the DNS relay needs: it starts while the sandbox starts, and @@ -81,6 +83,16 @@ def _value(text): return '"%s"' % text.replace("\\", "\\\\").replace('"', '\\"') +def record_prefix(session, user): + """The leading fields of every record the condenser writes.""" + parts = [] + if session: + parts.append("session=%s" % session) + if user: + parts.append("user=%s" % _value(user)) + return " ".join(parts) + " " if parts else "" + + class Condenser: """Turn pasta debug lines into open and close lines, one pair per flow.""" @@ -291,7 +303,7 @@ def condense(argv): """ import subprocess - session, dns_forward, cmd = "", "169.254.1.1", [] + session, user, dns_forward, cmd = "", "", "169.254.1.1", [] fifo, parent, marker, end_record, session_dir, stderr_fifo = None, None, None, None, None, None backend = "pasta" i = 0 @@ -299,6 +311,8 @@ def condense(argv): a = argv[i] if a == "--session": session = argv[i + 1] + elif a == "--user": + user = argv[i + 1] elif a == "--dns-forward": dns_forward = argv[i + 1] elif a == "--fifo": @@ -343,11 +357,13 @@ def condense(argv): out = sink.stdin if sink else sys.stdout.buffer state = {"dead": False} + lead = record_prefix(session, user) + def emit(text, prefix=True): if state["dead"]: return - if prefix and session: - text = "session=%s %s" % (session, text) + if prefix: + text = lead + text try: out.write((text + "\n").encode("utf-8", "replace")) out.flush() diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index fba6f4b..40f657b 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -2598,7 +2598,7 @@ _net_session_dir() { # # tag (SYSLOG_IDENTIFIER) bwrap-net # start record event=start session=ID launcher=... backend=... -# each pasta line session=ID +# each pasta line session=ID user=NAME # end record event=end session=ID ... # # `journalctl -t bwrap-net` shows them; `journalctl -t bwrap-net -g ID` one @@ -2668,6 +2668,7 @@ _NET_LOG_PIPE=0 _NET_LOG_TOOL="" _NET_LOG_TOOLS=() _NET_LOG_SESSION="" +_NET_LOG_USER="" _NET_LOG_FLOWS=0 _NET_LOG_FIFO="" _NET_LOG_ERR_FIFO="" @@ -2681,6 +2682,7 @@ _NET_LOG_PASTA_VERSION="" # the session id, and export BWRAP_SESSION. Called by build_net_isolation. _net_log_detect() { _NET_LOG_SESSION="$(od -An -N6 -tx1 /dev/urandom | tr -d ' \n')" + _NET_LOG_USER="$(_net_log_user)" _NET_LOG_TOOLS=() # A real launch also tries each tool without logging anything: # systemd-cat connects to the journal before running `true`, and logger @@ -2725,11 +2727,22 @@ _net_log_cmd() { esac } +# _net_log_user +# Print the name of the account that runs this launcher, for the user= field +# of every record. It is the account of the process (id -un), not $USER, +# which the caller can set to anything; characters that would split or +# forge a logfmt field are replaced. +_net_log_user() { + local _n + _n="$(bwrap_account_name)" + printf '%s\n' "${_n//[^A-Za-z0-9._@-]/_}" +} + # _net_log_record EVENT # Print the session record for EVENT (start or end). _net_log_record() { local _r _i _hp="" - _r="event=$1 session=${_NET_LOG_SESSION} launcher=${0##*/} user=${_USER:-$(id -un)} uid=$(id -u)" + _r="event=$1 session=${_NET_LOG_SESSION} launcher=${0##*/} user=${_NET_LOG_USER} uid=$(id -u)" _r+=" pid=$$ backend=${_NET_BACKEND}" if [[ "$1" == start ]]; then if [[ "${_NET_LOG_FLOWS}" -eq 1 ]]; then @@ -2833,7 +2846,7 @@ _net_log_start() { # open, so that nothing written to it is lost; its stderr is the # user's, for what the backend said if the start fails. local -a _cond=("${_NET_LOG_PY}" -I -S "${_NET_LOG_HELPER}" condense - --session "${_NET_LOG_SESSION}" --dns-forward "${_NET_DNS_FORWARD_ADDR}" + --session "${_NET_LOG_SESSION}" --user "${_NET_LOG_USER}" --dns-forward "${_NET_DNS_FORWARD_ADDR}" --fifo "${_NET_LOG_FIFO}" --parent "$$" --marker "${_NET_LOG_MARKER}" --end-record "$(_net_log_record end)" --session-dir "${_NET_SESSION_DIR}" --backend "${_NET_BACKEND}") @@ -2879,7 +2892,7 @@ _net_log_supervisor() { trap '' INT QUIT TSTP HUP TERM PIPE exec < /dev/null > /dev/null exec {_sink}> >(exec setsid "${_NET_LOG_CMD[@]}" > /dev/null 2>&1) - _prefix="session=${_NET_LOG_SESSION} " + _prefix="session=${_NET_LOG_SESSION} user=${_NET_LOG_USER} " if [[ "${_NET_LOG_PIPE}" -eq 1 ]]; then # Read-write, so end-of-file is never seen before pasta writes. This diff --git a/tests/test_netlog.py b/tests/test_netlog.py index 75b9339..886740c 100644 --- a/tests/test_netlog.py +++ b/tests/test_netlog.py @@ -163,6 +163,70 @@ def test_open_flows_closed_at_session_end(self): self.assertEqual(out[-1], "event=end session=x") +class RecordPrefixTest(unittest.TestCase): + def test_session_and_user(self): + self.assertEqual(netlog.record_prefix("0123456789ab", "alice"), "session=0123456789ab user=alice ") + + def test_missing_parts_are_left_out(self): + self.assertEqual(netlog.record_prefix("0123456789ab", ""), "session=0123456789ab ") + self.assertEqual(netlog.record_prefix("", "alice"), "user=alice ") + self.assertEqual(netlog.record_prefix("", ""), "") + + def test_user_cannot_forge_fields(self): + p = netlog.record_prefix("s", 'bob dns domain=x"\nevent=end') + self.assertEqual(len(p.splitlines()), 1) + self.assertTrue(p.startswith('session=s user="bob dns'), p) + self.assertTrue(p.endswith('" '), p) + self.assertNotIn("\n", p) + + +class CondenseCommandTest(unittest.TestCase): + """The condense command as the launcher runs it, reading standard input.""" + + def run_condense(self, *extra): + import subprocess + import tempfile + + lines = [ + "1.0000: Flow 0 (NEW): FREE -> NEW", + "1.0001: Flow 0 (TGT): TAP [198.51.100.7]:5000 -> [192.0.2.1]:443 => HOST [0.0.0.0]:0 -> [192.0.2.1]:443", + "1.0002: Flow 0 (TCP connection): TGT -> TYPED", + "1.0003: Flow 0 (TCP connection): TYPED -> ACTIVE", + "2.0000: Flow 0 (FREE): ACTIVE -> FREE", + "bwrap-dns domain=github.com type=A proto=udp rcode=NOERROR answers=192.0.2.1", + "event=end session=abc user=alice", + ] + cmd = [sys.executable, os.path.join(HERE, "..", "lib", "bwrap_netlog.py"), "condense", "--session", "abc"] + # The log tool's own output is discarded, so it writes to a file. + with tempfile.TemporaryDirectory() as d: + path = os.path.join(d, "log") + cmd += list(extra) + ["--", "tee", path] + res = subprocess.run( + cmd, + input="\n".join(lines) + "\n", + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + ) + self.assertEqual(res.returncode, 0, res.stderr) + with open(path) as f: + return f.read().splitlines() + + def test_every_record_names_session_and_user(self): + out = self.run_condense("--user", "alice") + records = [x for x in out if not x.startswith("event=")] + self.assertGreaterEqual(len(records), 3) + for line in records: + self.assertTrue(line.startswith("session=abc user=alice "), line) + self.assertEqual(out[-1], "event=end session=abc user=alice") + + def test_without_user_the_prefix_is_the_session_only(self): + for line in self.run_condense(): + if not line.startswith("event="): + self.assertTrue(line.startswith("session=abc "), line) + self.assertNotIn("user=", line) + + class DnsParseTest(unittest.TestCase): def test_query(self): self.assertEqual(netlog.parse_query(query("WWW.Example.org", 28)), ("www.example.org", "AAAA")) From d1c9bad088018db363f6135cdc27f7428e5545fa Mon Sep 17 00:00:00 2001 From: "Stuart B. Wilkins" Date: Mon, 16 Nov 2026 08:21:08 -0500 Subject: [PATCH 37/37] fix(bwrap): address the Copilot review of the network log - Keep draining pasta's log when the log tool stays alive but stops reading: records wait in a queue bounded in bytes (8 MiB) for a writer thread that writes what is waiting in one go, are dropped and counted when it is full (the next record carries a note), and the tool is killed at the end if it has not taken them. The end record and the queue share one 5 s deadline. Without a log command the records still go straight to standard output. - Forward the piped-stderr bridge's bytes as they arrive and unchanged, instead of decoding and holding them until a newline. - Record via= when only the port differs (--host-port 5432:15432). - Log a complete TCP DNS query that fails upstream (rcode=TIMEOUT), once; a client that closes between or inside queries, or sends less than a DNS header, is not logged. - tests/fake-net-run: bind a custom FAKE_NET_DIR at the fakes' fixed path, so settings survive the launchers' environment scrub. - Say in the docs and the launcher comment that the bash fallback (no Python) still writes to the tool synchronously. Assisted-by: opencode:claude-sonnet-5-5 Assisted-by: opencode:gpt-6-astra Assisted-by: opencode:gpt-6.1-sol Assisted-by: opencode:claude-fable-5-2 Assisted-by: opencode:claude-opus-5-5 --- AI_DISCLOSURE.md | 5 +- docs/bw-wrappers.md | 21 ++- lib/bwrap_netlog.py | 246 +++++++++++++++++++++++++++++----- lib/bwrap_sandbox_lib.sh | 8 +- tests/fake-net-run | 10 +- tests/test_netlog.py | 281 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 526 insertions(+), 45 deletions(-) diff --git a/AI_DISCLOSURE.md b/AI_DISCLOSURE.md index 11dbd8b..1dfb2b6 100644 --- a/AI_DISCLOSURE.md +++ b/AI_DISCLOSURE.md @@ -2,13 +2,16 @@ tools-used: - OpenCode models-used: + - claude-fable-5-2 - claude-opus-4-8 - claude-opus-5-5 - claude-sonnet-4-6 - claude-sonnet-5 - claude-sonnet-5-5 - gpt-5.6-sol + - gpt-6-astra - gpt-6-sol + - gpt-6.1-sol providers: - Anthropic - OpenAI @@ -20,7 +23,7 @@ scope: Shell wrapper scripts (bin/, lib/), the `azoidcapp` Python helper, documentation (docs/), and CI/lint configuration — all with human review and validation. -last-updated: 2026-11-10 +last-updated: 2026-11-16 --- # AI Disclosure diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index cfff67f..1242348 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -446,8 +446,9 @@ records. - `dir` is `out` (the sandbox to the network), `publish` (a host client to a `--publish` port) or `host-port` (the sandbox to a `--host-port` service). - `via=` is the address on the other side when it differs (the sandbox port - for `publish`). `t` is seconds since pasta started; `secs` is the + `via=` is the address and port on the other side when either differs (the + sandbox port for `publish`; for `--host-port 5432:15432` the host port, since + the address is the same). `t` is seconds since pasta started; `secs` is the connection's duration. Flows still open when the session ends get a close with `reason=session-end`. - `flow` is pasta's flow slot, which it reuses: it identifies a connection @@ -503,12 +504,22 @@ records. and stops only when the launcher process, which is pasta by then, is gone (it is no longer the reader's parent), after draining the pipe. Then it writes the end record and removes the session directory. - - If the tool dies, it goes on reading and drops the lines, so pasta never - blocks on it. + - With Python, if the tool dies, or stays alive but stops reading, the + reader goes on reading and drops the lines, so pasta never blocks on it. + Lines wait in memory, up to 8 MiB, for a writer thread (which writes what + is waiting in one go); when that is full they are dropped (a tool that is + only slow, behind a burst larger than that, loses lines too), and the next + line that is queued carries a + `condense level=warn msg=log-tool-behind dropped=N` note in front of it. At the end the end record and the queue get 5 seconds in all, and + the tool 5 more to exit, before it is killed; if it was not reading, the + reader says on its stderr how many records were lost. Without Python the + bash supervisor writes synchronously, and a tool that stops reading stalls + it. - pasta prints its setup first, and if it fails to start, its error. The descriptor relay (pasta's command) prints a marker on the FIFO and reopens your stderr: by path when it is a terminal or a file, else (a pipe) - through a second FIFO that the reader copies. If pasta stops before the + through a second FIFO that the reader copies, byte for byte and as it + arrives (a prompt without a newline is not held back). If pasta stops before the marker, the reader prints pasta's lines on your terminal, prefixed `pasta:`, just after the launcher has exited. (With slirp4netns the same FIFO, marker and reader are used for the DNS relay's lines, and a failed diff --git a/lib/bwrap_netlog.py b/lib/bwrap_netlog.py index 2552f30..bf6c451 100644 --- a/lib/bwrap_netlog.py +++ b/lib/bwrap_netlog.py @@ -37,6 +37,7 @@ # Kept to what the DNS relay needs: it starts while the sandbox starts, and # lookups made before it listens skip it (see bwrap_sandbox_lib.sh). +import collections import ctypes import ipaddress from concurrent.futures import ThreadPoolExecutor @@ -47,6 +48,7 @@ import struct import sys import threading +import time # ── condense ──────────────────────────────────────────────────────────── @@ -169,7 +171,9 @@ def _fields(self, idx, f, event): if event == "open": parts.append("src=%s" % _hostport(*f["src"])) parts.append("dst=%s" % _hostport(*f["dst"])) - if f["tdst"][0] != f["dst"][0] and f["tdst"][0] not in ("0.0.0.0", "::"): + # The whole endpoint: --host-port 5432:15432 keeps the address and + # changes only the port. + if f["tdst"] != f["dst"] and f["tdst"][0] not in ("0.0.0.0", "::"): parts.append("via=%s" % _hostport(*f["tdst"])) name = self.names.get(f["dst"][0]) if name and direction == "out": @@ -211,12 +215,13 @@ def _parent_is(pid): class FifoReader: - """Read lines from a FIFO without ever blocking its writer. + """Read from a FIFO (bytes with `chunks()`, lines with `lines()`) without + ever blocking its writer. The FIFO is opened read-only and non-blocking: a read-only reader sees EOF whenever no writer has it open, which happens before the launcher opens its stderr on it and, briefly, if pasta is ever without the fd. - So EOF is not the end: `lines()` keeps polling until `done()` says the + So EOF is not the end: `chunks()` keeps polling until `done()` says the session is over, and then drains what is left. The launcher opens its end only after this reader exists (it is told, see `ready`), so its write-only open does not block. @@ -235,8 +240,6 @@ def _read(self, timeout_ms): """Return the bytes available within TIMEOUT_MS; b"" when none. Waits out TIMEOUT_MS even while the FIFO reports hang-up (no writer), which poll() would otherwise report at once, over and over.""" - import time - deadline = time.monotonic() + timeout_ms / 1000.0 while True: left = deadline - time.monotonic() @@ -251,16 +254,13 @@ def _read(self, timeout_ms): return data time.sleep(min(0.05, max(left, 0))) # hang-up: no writer yet, or none left - def lines(self, poll_ms=1000): - """Yield complete lines (without the newline) until the session is - over and the FIFO is drained.""" + def chunks(self, poll_ms=1000): + """Yield the bytes as they arrive, whole or partial lines, until the + session is over and the FIFO is drained.""" while True: data = self._read(poll_ms) if data: - self.buf += data - while b"\n" in self.buf: - line, _, self.buf = self.buf.partition(b"\n") - yield line.decode("utf-8", "replace") + yield data continue if self.done(): break @@ -269,26 +269,147 @@ def lines(self, poll_ms=1000): data = self._read(200) if not data: break + yield data + os.close(self.fd) + + def lines(self, poll_ms=1000): + """Yield the non-empty lines (without the newline), the last one even + with no newline, until the session is over and the FIFO is drained.""" + self.buf = b"" + for data in self.chunks(poll_ms): self.buf += data - for line in self.buf.split(b"\n"): - if line: - yield line.decode("utf-8", "replace") + while b"\n" in self.buf: + line, _, self.buf = self.buf.partition(b"\n") + if line: + yield line.decode("utf-8", "replace") + if self.buf: + yield self.buf.decode("utf-8", "replace") self.buf = b"" - os.close(self.fd) def _bridge_stderr(path, stop): """Copy PATH (a FIFO the relay opens as the sandbox's stderr when the - user's is a pipe) to our stderr until STOP is set and it is drained.""" + user's is a pipe) to our stderr until STOP is set and it is drained. + + The bytes are passed on as they arrive and unchanged: a prompt or a + progress line without a newline must not wait for one, and output that is + not UTF-8 must not be rewritten.""" reader = FifoReader(path, stop.is_set) - for line in reader.lines(): + for data in reader.chunks(): try: - sys.stderr.write(line + "\n") sys.stderr.flush() + out = getattr(sys.stderr, "buffer", None) + if out is not None: + out.write(data) + out.flush() + else: + sys.stderr.write(data.decode("utf-8", "replace")) + sys.stderr.flush() except (OSError, ValueError): pass +# The log tool can stop reading its standard input without exiting. Records +# wait in memory, up to this many bytes, for a writer thread, and are dropped +# when that is full, so the condenser keeps draining pasta's log (a full FIFO +# would stall the sandbox's network). The bound is generous: a tool that is +# merely slow, behind a burst, still gets every record. At the end the end +# record and the queue get SINK_CLOSE_WAIT seconds in all, and then the tool +# that many more to exit, before it is killed. +SINK_BYTES = 8 * 1024 * 1024 +SINK_CLOSE_WAIT = 5 + + +class BoundedSink: + """Write records to the log tool from a thread, never blocking the caller. + + put() queues a record and returns at once; when LIMIT bytes are already + waiting the record is dropped and counted, and the next record that is + queued has a note (from NOTE, called with the count) of how many were lost + in front of it, in the same item. The thread writes what is waiting in + one go. A write error means the tool is gone: later records are dropped. + """ + + def __init__(self, out, limit=None, note=None): + self.out = out + self.limit = limit or SINK_BYTES + self.note = note + self.items = collections.deque() + self.size = 0 + self.dropped = 0 # since the last record that was queued + self.dropped_total = 0 + self.dead = False + self.closing = False + self.cond = threading.Condition() + self.thread = threading.Thread(target=self._run) + self.thread.daemon = True + self.thread.start() + + def _run(self): + while True: + with self.cond: + while not self.items and not self.closing: + self.cond.wait() + if not self.items: + return + batch, n = [], 0 + while self.items and n < 65536: + batch.append(self.items.popleft()) + n += len(batch[-1]) + self.size -= n + self.cond.notify_all() + if self.dead: + continue + try: + view = memoryview(b"".join(batch)) + while view: + view = view[(self.out.write(view) or 0) :] + self.out.flush() + except (BrokenPipeError, OSError, ValueError): + self.dead = True + + def put(self, data, wait=0): + """Queue DATA; wait up to WAIT seconds for room. True if queued. + + When records were dropped, the note about them goes in front of DATA + as part of the same item, so it is never lost on its own and the + count is cleared only once something is queued.""" + with self.cond: + if self.dead or self.closing: + return False + item = data + if self.dropped and self.note: + item = self.note(self.dropped) + data + + # An empty queue takes anything, so one item larger than the limit + # cannot be refused for ever. + def room(): + return self.size == 0 or self.size + len(item) <= self.limit + + if wait > 0 and not room(): + self.cond.wait_for(lambda: self.dead or room(), wait) + if self.dead: + return False + if not room(): + self.dropped += 1 + self.dropped_total += 1 + return False + self.items.append(item) + self.size += len(item) + self.dropped = 0 + self.cond.notify_all() + return True + + def close(self, wait): + """Let the thread finish what is queued, for up to WAIT seconds. + True if it did; False if the tool is stalled. Safe to call again.""" + with self.cond: + self.closing = True + self.cond.notify_all() + self.thread.join(wait) + return not self.thread.is_alive() + + def condense(argv): """Condense pasta's debug log into the system log. @@ -351,25 +472,44 @@ def condense(argv): except (OSError, ValueError, AttributeError): pass - sink = None + proc = None if cmd: - sink = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) - out = sink.stdin if sink else sys.stdout.buffer - state = {"dead": False} - + # Unbuffered: the writer thread's write is the pipe's, so it can be + # ended by killing the tool (see below) and nothing is left in a + # buffer nobody can flush. + proc = subprocess.Popen( + cmd, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, bufsize=0 + ) lead = record_prefix(session, user) + # With a log tool the records go through a bounded queue, so a tool that + # stops reading costs records, not pasta's log. Without one (by hand, in + # tests) they go to standard output as before, where a slow reader slows + # the filter down instead of losing lines. + sink = ( + BoundedSink( + proc.stdin, + note=lambda n: (lead + "condense level=warn msg=log-tool-behind dropped=%d\n" % n).encode(), + ) + if proc + else None + ) + state = {"dead": False} - def emit(text, prefix=True): - if state["dead"]: - return + def emit(text, prefix=True, wait=0): if prefix: text = lead + text + data = (text + "\n").encode("utf-8", "replace") + if sink is not None: + return sink.put(data, wait) + if state["dead"]: + return False try: - out.write((text + "\n").encode("utf-8", "replace")) - out.flush() + sys.stdout.buffer.write(data) + sys.stdout.buffer.flush() except (BrokenPipeError, OSError): - # The log tool is gone: keep reading, so pasta never blocks. state["dead"] = True + return False + return True c = Condenser(emit, dns_forward) @@ -417,14 +557,35 @@ def feed(raw): bridge_stop.set() bridge.join(5) c.finish() - if end_record: - emit(end_record, prefix=False) - if sink: + if sink is None: + if end_record: + emit(end_record, prefix=False) + else: + deadline = time.monotonic() + SINK_CLOSE_WAIT + if end_record: + emit(end_record, prefix=False, wait=SINK_CLOSE_WAIT) + flushed = sink.close(max(0.0, deadline - time.monotonic())) + if not flushed: + # The tool is not reading: end it, which also ends the writer + # thread's blocked write, then let the thread finish before the + # pipe is closed. What it could not take is only said here. + unwritten = len(sink.items) + proc.kill() + sink.close(1) + sys.stderr.write( + "bwrap_netlog condense: the log tool stopped reading; %d record(s) dropped, %d queued but not written;" + " the end record may be missing\n" % (sink.dropped_total, unwritten) + ) + sys.stderr.flush() try: - sink.stdin.close() + proc.stdin.close() except OSError: pass - sink.wait() + try: + proc.wait(SINK_CLOSE_WAIT) + except subprocess.TimeoutExpired: + proc.kill() + proc.wait() if session_dir: import shutil @@ -599,6 +760,13 @@ def _tcp_one(self, conn): try: hdr = self._recv_exact(conn, 2) query = self._recv_exact(conn, struct.unpack("!H", hdr)[0]) + except OSError: + # The client closed or went idle between queries, or in + # the middle of one: no complete query, nothing to log. + break + if len(query) < 12: + break # shorter than a DNS header: not a query + try: # DNS servers may close after one response. A fresh # connection per query also avoids replaying a request # after an ambiguous upstream failure. @@ -607,7 +775,13 @@ def _tcp_one(self, conn): up.sendall(hdr + query) rhdr = self._recv_exact(up, 2) response = self._recv_exact(up, struct.unpack("!H", rhdr)[0]) - self.log(query, response, "tcp") + except OSError: + # A complete query that got no answer: logged like the + # UDP case (rcode=TIMEOUT), once, then the client is let go. + self.log(query, None, "tcp") + break + self.log(query, response, "tcp") + try: conn.sendall(rhdr + response) except OSError: break diff --git a/lib/bwrap_sandbox_lib.sh b/lib/bwrap_sandbox_lib.sh index 40f657b..75e3bfb 100644 --- a/lib/bwrap_sandbox_lib.sh +++ b/lib/bwrap_sandbox_lib.sh @@ -2632,8 +2632,12 @@ _net_session_dir() { # ignores end-of-file: it stops only when the launcher process is gone, # after draining what is left. pasta keeps descriptors 0-2 across its # own sandboxing (it closes from 3 up). -# - It never blocks pasta: if the tool dies it goes on reading and drops the -# lines. +# - It never blocks pasta: if the tool dies, or stays alive but stops +# reading, it goes on reading and drops the lines (a queue bounded in +# bytes and a writer thread; the tool is killed at the end if it still has not taken +# what is queued). That is the condenser, with Python. The bash +# supervisor below writes to the tool synchronously, so a tool that stops +# reading stalls it, and with it pasta. # - pasta's startup lines would otherwise print on the terminal; the relay # prints a marker on the FIFO and then reopens the user's stderr by path # (a terminal or a file), or through a second FIFO that the supervisor diff --git a/tests/fake-net-run b/tests/fake-net-run index 3415042..b525689 100755 --- a/tests/fake-net-run +++ b/tests/fake-net-run @@ -7,7 +7,11 @@ # device at /dev/net/tun (/dev/null), and plain-file copies of /etc/resolv.conf # and /etc/hosts (a file that is itself a bwrap data mount cannot be bound # again). The real pasta is hidden; --backend slirp4netns leaves the fake -# pasta off PATH too. The fakes read failure modes from $FAKE_NET_DIR. +# pasta off PATH too. The fakes read failure modes from $FAKE_NET_DIR. The +# launchers scrub the environment before they start a backend, so the fakes +# there fall back to /tmp/opencode/fakenet: a different $FAKE_NET_DIR is bound +# at that path inside the outer bwrap, so the fakes find the same settings (and +# the directory exists for the fake slirp4netns's log). # FAKE_NET_LOG=journal (default), syslog, both or none puts a socket file at # /run/systemd/journal/stdout, /dev/log, both or neither (so the launcher # picks the fake systemd-cat or logger, which write to @@ -49,11 +53,15 @@ syslog | both) logsock+=(--bind "${FAKE_NET_DIR}/devlog.sock" /dev/log) ;; esac +fixed=/tmp/opencode/fakenet +fakedir=() +[[ "${FAKE_NET_DIR}" == "${fixed}" ]] || fakedir=(--bind "${FAKE_NET_DIR}" "${fixed}") path="${here}/fakes/bin:${PATH}" [[ "${backend}" != pasta ]] || path="${here}/fakes/pasta-bin:${path}" exec bwrap --dev-bind / / --dev /dev --dev-bind /dev/null /dev/net/tun \ --ro-bind /dev/null /usr/bin/passt \ "${etc[@]}" \ ${logsock[@]+"${logsock[@]}"} \ + ${fakedir[@]+"${fakedir[@]}"} \ --chdir "${PWD}" \ -- env PATH="${path}" "$@" diff --git a/tests/test_netlog.py b/tests/test_netlog.py index 886740c..56ab70c 100644 --- a/tests/test_netlog.py +++ b/tests/test_netlog.py @@ -13,10 +13,13 @@ import re import socket import struct +import subprocess import sys +import tempfile import threading import time import unittest +from unittest import mock HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(HERE, "..", "lib")) @@ -133,6 +136,28 @@ def test_publish_and_host_port(self): ], ) + def test_host_port_translation_keeps_the_port(self): + # --host-port 5432:15432: the sandbox connects to 127.0.0.1:15432 and + # pasta carries it to the host's 127.0.0.1:5432; the address is the + # same on both sides, the port is not. + t = "1.0000: Flow 4 " + out = condense( + [ + t + "(NEW): FREE -> NEW", + t + "(TGT): SPLICE [127.0.0.1]:40000 -> [127.0.0.1]:15432 => HOST [0.0.0.0]:0 -> [127.0.0.1]:5432", + t + "(TCP connection (spliced)): TGT -> TYPED", + t + "(TCP connection (spliced)): TYPED -> ACTIVE", + "2.0000: Flow 4 (FREE): ACTIVE -> FREE", + ] + ) + self.assertEqual( + out, + [ + "flow=4 open proto=tcp dir=host-port src=127.0.0.1:40000 dst=127.0.0.1:15432 via=127.0.0.1:5432 t=1.000", + "flow=4 close proto=tcp dir=host-port dst=127.0.0.1:15432 via=127.0.0.1:5432 t=2.000 secs=1.000", + ], + ) + def test_failed_connection(self): t = "1.0000: Flow 2 " out = condense( @@ -227,6 +252,228 @@ def test_without_user_the_prefix_is_the_session_only(self): self.assertNotIn("user=", line) +class BridgeStderrTest(unittest.TestCase): + def test_bytes_are_forwarded_at_once_and_unchanged(self): + class Err(object): + def __init__(self): + self.buffer = io.BytesIO() + + def flush(self): + pass + + def write(self, text): + raise AssertionError("text written, bytes expected") + + err = Err() + stop = threading.Event() + with tempfile.TemporaryDirectory() as d: + path = os.path.join(d, "stderr") + os.mkfifo(path) + with mock.patch.object(sys, "stderr", err): + t = threading.Thread(target=netlog._bridge_stderr, args=(path, stop)) + t.daemon = True + t.start() + fd = None + for _ in range(100): # the reader opens its end first; ENXIO until then + try: + fd = os.open(path, os.O_WRONLY | os.O_NONBLOCK) + break + except OSError: + time.sleep(0.05) + self.assertIsNotNone(fd, "the bridge never opened the FIFO") + try: + # A prompt with no newline, and bytes that are not UTF-8. + os.write(fd, b"password: \xff\xfe") + for _ in range(60): + if err.buffer.getvalue(): + break + time.sleep(0.05) + self.assertEqual(err.buffer.getvalue(), b"password: \xff\xfe") + finally: + os.close(fd) + stop.set() + t.join(timeout=5) + self.assertFalse(t.is_alive()) + + +class BoundedSinkTest(unittest.TestCase): + def test_a_sink_that_never_reads_does_not_block_and_drops(self): + r, w = os.pipe() + out = os.fdopen(w, "wb", 0) + sink = netlog.BoundedSink(out, limit=10000, note=lambda n: b"dropped %d\n" % n) + sent = [] + + def feed(): + # Far more than the pipe (64 KiB) and the limit hold, with pauses + # so the writer thread gets to fill the pipe. + for i in range(20000): + sink.put(b"x" * 100 + b"\n") + sent.append(101) + if i % 50 == 0: + time.sleep(0.002) + + feeder = threading.Thread(target=feed) + feeder.daemon = True + try: + feeder.start() + feeder.join(30) # a put() that blocked would never finish + self.assertFalse(feeder.is_alive(), "put() blocked on a sink that does not read") + self.assertGreater(sum(sent), 1000000) + self.assertGreater(sink.dropped, 0) + self.assertGreater(sink.size, 9000) # full, to within one record + self.assertFalse(sink.close(0.2)) # still stalled: reported, not waited out + finally: + os.close(r) # the blocked write now fails, and the rest is dropped + try: + self.assertTrue(sink.close(5)) + self.assertTrue(sink.dead) + finally: + out.close() + + def test_everything_is_written_when_the_sink_reads(self): + out = io.BytesIO() + sink = netlog.BoundedSink(out) + for i in range(100): + sink.put(b"line %d\n" % i) + self.assertTrue(sink.close(5)) + self.assertEqual(out.getvalue().splitlines()[0], b"line 0") + self.assertEqual(len(out.getvalue().splitlines()), 100) + + def test_a_burst_to_a_healthy_sink_loses_nothing(self): + out = io.BytesIO() + sink = netlog.BoundedSink(out) + for i in range(50000): + self.assertTrue(sink.put(b"line %d\n" % i)) + self.assertTrue(sink.close(10)) + self.assertEqual(len(out.getvalue().splitlines()), 50000) + self.assertEqual(sink.dropped, 0) + + def test_a_note_says_how_many_were_lost(self): + gate = threading.Event() + + class Slow(object): + def __init__(self): + self.data = b"" + + def write(self, data): + gate.wait(5) + self.data += bytes(data) + return len(data) + + def flush(self): + pass + + out = Slow() + sink = netlog.BoundedSink(out, limit=4, note=lambda n: b"lost %d\n" % n) + self.assertTrue(sink.put(b"a\n")) # the writer takes this and blocks + for _ in range(100): + if not sink.items: + break + time.sleep(0.02) + results = [sink.put(b"b\n") for _ in range(10)] + self.assertIn(False, results) + lost = sink.dropped + self.assertGreater(lost, 0) + gate.set() + for _ in range(100): # the writer drains what is waiting + if not sink.items: + break + time.sleep(0.05) + self.assertTrue(sink.put(b"z\n", wait=2)) + self.assertTrue(sink.close(5)) + self.assertIn(b"lost %d\nz\n" % lost, out.data) # the note and the record are one item + self.assertEqual(sink.dropped, 0) + + def test_a_record_waits_for_room_when_asked_to(self): + gate = threading.Event() + + class Slow(object): + def write(self, data): + gate.wait(5) + return len(data) + + def flush(self): + pass + + sink = netlog.BoundedSink(Slow(), limit=4) + self.assertTrue(sink.put(b"a\n")) + for _ in range(100): + if not sink.items: + break + time.sleep(0.02) + self.assertTrue(sink.put(b"bb\n")) + self.assertFalse(sink.put(b"cc\n")) # no room, no waiting + threading.Timer(0.3, gate.set).start() + self.assertTrue(sink.put(b"dd\n", wait=5)) # room once the writer takes the first + self.assertTrue(sink.close(5)) + + +class StalledLogToolTest(unittest.TestCase): + """condense with a log tool that stays alive but stops reading.""" + + LIB = os.path.join(HERE, "..", "lib") + + def run_condense(self, lines, args, wait=None, limit=None): + script = "import sys; sys.path.insert(0, %r); import bwrap_netlog as n; %s%sn.condense(sys.argv[1:])" % ( + self.LIB, + "n.SINK_CLOSE_WAIT = %d; " % wait if wait else "", + "n.SINK_BYTES = %d; " % limit if limit else "", + ) + return subprocess.run( + [sys.executable, "-c", script, "--session", "abc"] + args, + input=lines.encode(), + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + timeout=60, + ) + + @staticmethod + def dns_lines(n): + return "".join( + "bwrap-dns domain=host%d.example.org type=A proto=udp rcode=NOERROR answers=192.0.2.1\n" % i + for i in range(n) + ) + + def test_condense_finishes_and_kills_the_tool(self): + with tempfile.TemporaryDirectory() as d: + pidfile = os.path.join(d, "pid") + # Never reads its standard input: the pipe fills, and the old code + # blocked in write() and stopped reading its own input. + tool = ["sh", "-c", "echo $$ > %s; exec sleep 120" % pidfile] + res = self.run_condense( + self.dns_lines(30000), ["--end-record", "event=end session=abc", "--"] + tool, wait=1, limit=100000 + ) + self.assertEqual(res.returncode, 0, res.stderr.decode()) + self.assertIn(b"the log tool stopped reading", res.stderr) + self.assertIn(b"the end record may be missing", res.stderr) + self.assertRegex(res.stderr.decode(), r"reading; [1-9][0-9]* record\(s\) dropped") + with open(pidfile) as f: + pid = int(f.read()) + with self.assertRaises(OSError): # killed and reaped + os.kill(pid, 0) + + def test_a_healthy_tool_gets_every_record_and_the_end_record(self): + with tempfile.TemporaryDirectory() as d: + out = os.path.join(d, "out") + res = self.run_condense( + self.dns_lines(20000), ["--end-record", "event=end session=abc", "--", "tee", out], wait=1 + ) + self.assertEqual(res.returncode, 0, res.stderr.decode()) + with open(out) as f: + got = f.read().splitlines() + self.assertEqual(res.stderr, b"") + self.assertEqual(len(got), 20001) + self.assertEqual( + got[0], "session=abc dns domain=host0.example.org type=A proto=udp rcode=NOERROR answers=192.0.2.1" + ) + self.assertEqual(got[-1], "event=end session=abc") + + def test_without_a_tool_records_go_to_standard_output(self): + res = self.run_condense(self.dns_lines(5000), []) + self.assertEqual(res.returncode, 0, res.stderr.decode()) + self.assertEqual(len(res.stdout.splitlines()), 5000) + + class DnsParseTest(unittest.TestCase): def test_query(self): self.assertEqual(netlog.parse_query(query("WWW.Example.org", 28)), ("www.example.org", "AAAA")) @@ -256,6 +503,7 @@ def setUp(self): self.up_tcp.bind(("127.0.0.1", port)) self.up_tcp.listen() self.up_tcp.settimeout(0.2) + self.up_tcp_answers = True self.up_udp_thread = threading.Thread(target=self._serve_udp) self.up_tcp_thread = threading.Thread(target=self._serve_tcp) self.up_udp_thread.start() @@ -300,6 +548,8 @@ def _serve_tcp(self): break with c: c.settimeout(1) + if not self.up_tcp_answers: + continue # accept and close: the lookup fails upstream try: n = struct.unpack("!H", c.recv(2))[0] r = answer(c.recv(n)) @@ -335,6 +585,37 @@ def test_tcp(self): self.assertIn("type=AAAA proto=tcp rcode=NOERROR", self.wait_lines(1)[0]) self.assertEqual(len(self.wait_lines(2)), 2) + def test_tcp_query_that_fails_upstream_is_logged_once(self): + self.up_tcp_answers = False + with socket.create_connection(self.addr, timeout=3) as c: + c.settimeout(3) + q = query("www.example.org") + c.sendall(struct.pack("!H", len(q)) + q) + self.assertEqual(c.recv(2), b"") # let go without an answer + self.wait_lines(1) + time.sleep(0.3) # a second record, if there were one, would be here by now + self.assertEqual( + self.out.getvalue().splitlines(), + ["bwrap-dns domain=www.example.org type=A proto=tcp rcode=TIMEOUT answers=-"], + ) + + def test_tcp_message_shorter_than_a_dns_header_is_dropped_unlogged(self): + with socket.create_connection(self.addr, timeout=3) as c: + c.settimeout(3) + c.sendall(struct.pack("!H", 0)) # a zero-length query + self.assertEqual(c.recv(2), b"") + time.sleep(0.3) + self.assertEqual(self.out.getvalue(), "") + + def test_tcp_client_that_closes_without_a_query_is_not_logged(self): + with socket.create_connection(self.addr, timeout=3): + pass + with socket.create_connection(self.addr, timeout=3) as c: + q = query("www.example.org") + c.sendall(struct.pack("!H", len(q)) + q[:5]) # closes in the middle of one + time.sleep(0.5) + self.assertEqual(self.out.getvalue(), "") + def test_dns_name_does_not_forge_log_lines(self): q = query("bad\nname.example.org") with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as c: