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/.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/.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/AI_DISCLOSURE.md b/AI_DISCLOSURE.md index db04999..1dfb2b6 100644 --- a/AI_DISCLOSURE.md +++ b/AI_DISCLOSURE.md @@ -2,12 +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 @@ -19,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-10-24 +last-updated: 2026-11-16 --- # AI Disclosure diff --git a/README.md b/README.md index f3f4d5c..0583ec0 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,14 @@ 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 + `--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` @@ -96,13 +104,18 @@ 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 (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) | | `--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 `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 | `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 34549a8..c32a7f3 100755 --- a/bin/bwclaude +++ b/bin/bwclaude @@ -85,6 +85,29 @@ # 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 +# 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 +# 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 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 +# 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 @@ -164,6 +187,42 @@ 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 + ;; + --share-net) + 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 + ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 @@ -263,6 +322,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 @@ -354,14 +414,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 +437,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 @@ -593,6 +642,7 @@ main() { bwrap_resolve_tool claude detect_kernel_capabilities + detect_net_capabilities resolve_common_paths resolve_claude_paths detect_shell @@ -608,6 +658,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 @@ -618,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 58bbecb..1f246b5 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 @@ -102,6 +102,29 @@ # 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 +# 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 +# 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 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 +# 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 @@ -186,6 +209,42 @@ 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 + ;; + --share-net) + 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 + ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 @@ -293,6 +352,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 @@ -406,36 +466,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 @@ -446,17 +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") - 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 + # 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+=(--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. @@ -620,6 +658,7 @@ main() { bwrap_resolve_tool codex detect_kernel_capabilities + detect_net_capabilities resolve_common_paths resolve_codex_paths detect_shell @@ -636,6 +675,7 @@ main() { build_proc_dev_tmp build_sys_mounts build_etc_mounts + build_net_isolation build_managed_skills build_home_tmpfs build_workdir_mount @@ -645,6 +685,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 915d519..ed33c9f 100755 --- a/bin/bwcopilot +++ b/bin/bwcopilot @@ -70,6 +70,29 @@ # 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 +# 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 +# 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 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 +# 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 @@ -144,6 +167,42 @@ 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 + ;; + --share-net) + 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 + ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 @@ -247,6 +306,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 @@ -351,16 +411,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 } @@ -478,6 +530,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 @@ -493,6 +546,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 @@ -503,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 05c0123..11e0900 100755 --- a/bin/bwopencode +++ b/bin/bwopencode @@ -82,6 +82,29 @@ # 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 +# 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 +# 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 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 +# 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 @@ -156,6 +179,42 @@ 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 + ;; + --share-net) + 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 + ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 @@ -242,6 +301,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 +532,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 +669,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 +688,7 @@ main() { build_proc_dev_tmp build_sys_mounts build_etc_mounts + build_net_isolation build_managed_config build_managed_skills build_home_tmpfs @@ -639,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 c39bdc2..3c0a5e2 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -79,6 +79,13 @@ # 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) 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.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. +# # Usage: # bwopencode2 [bwopencode2-options] [opencode2 arguments...] # bwopencode2 --help @@ -93,6 +100,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 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 +# 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. @@ -110,6 +124,29 @@ # --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 +# 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 +# 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 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 +# 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 @@ -145,6 +182,32 @@ 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="" +# 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 +# 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 +253,58 @@ 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 + ;; + --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 + ;; + --share-net) + 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 + ;; --ro-path) if [[ $# -lt 2 ]]; then echo "Error: --ro-path requires a path argument." >&2 @@ -237,6 +352,28 @@ 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, 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, + 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 + 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, 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, @@ -288,6 +425,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 @@ -304,6 +442,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 @@ -311,10 +450,358 @@ 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: +# - 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. +# 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, and it +# 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. + +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 _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, 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 %s:%s\n' "${_p}" "${_addr}" "${_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 "${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 + 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 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 _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((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/${_addr}/${_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 + # 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)" + _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 'start=%s\n' "$(bwrap_proc_start_time "$$")" + 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 "${SERVE_ADDR}" "${_p}" || continue + else + serve_claim_port "${_p}" || continue + 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 + fi + SERVE_PORT="${_p}" + return 0 + done + + if [[ -n "${SERVE_PORT_REQUEST}" ]]; then + 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 + 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 +} + +# 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 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 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 + +# 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 +deadline=$((SECONDS + 30)) +while ((SECONDS < deadline)); do + kill -0 "$srv" 2> /dev/null || break + 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 + fi + sleep 0.5 +done + +{ + if [[ $ready -eq 1 ]]; then + resp=$(printf 'user = "opencode:%s"\n' "$OPENCODE_PASSWORD" | + 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]}" + 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' "$bind:$port" + fi +} >&2 + +wait "$srv" +SERVE_WRAPPER_EOF + +# Choose port and password, and give the sandbox its command. +serve_prepare() { + SERVE_ADDR="$(net_publish_prefix).1" + serve_choose_port + serve_make_password + 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}" + # 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=() +} +# Tell the user how to reach the server (stderr, before the exec). +serve_print_info() { + local _p="${SERVE_PORT}" _host _alias _addr="${SERVE_ADDR}" + 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}" + 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' + 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 + 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 -# 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: @@ -698,9 +1185,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 @@ -786,6 +1274,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 +1300,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) 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 [[ -n "${SANDBOX_CONTEXT_PATH}" && -z "${TEST_CMD}" ]] || return 0 @@ -829,17 +1329,32 @@ 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 + # --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 @@ -857,6 +1372,7 @@ main() { build_proc_dev_tmp build_sys_mounts build_etc_mounts + build_net_isolation build_managed_config build_managed_skills build_managed_extras @@ -872,6 +1388,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 @@ -889,6 +1406,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..1242348 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -17,6 +17,18 @@ 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) +- 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 `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 + condense it and to name destinations (DNS relay). ## Managed system paths @@ -86,7 +98,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 (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) | @@ -95,6 +107,11 @@ 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 `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) | `--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 @@ -124,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 @@ -224,12 +241,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 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` @@ -257,14 +278,482 @@ 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, provided by the first usable +backend: + +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). 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. +- **`--share-net`** shares the host network instead: the behaviour before + private namespaces, for workflows that need the host's loopback services or + ports, or hosts where neither backend works. The host's `/etc/resolv.conf` + and `/etc/hosts` are mounted, host loopback services are reachable, sandbox + listeners are visible on the host, and the + [sandbox context file](#sandbox-context) says so. It cannot be combined with + the forwarding options or `--net-backend` (exit status 1). Prefer + `--host-port` / `--publish` for single ports. +- **`--net-backend pasta|slirp4netns`** uses only that backend (no + fallthrough), for testing the slirp4netns path on a host with pasta, or for + diagnosis. + +`BWRAP_NET_BACKEND` in the sandbox is `pasta`, `slirp4netns` or `none` +(`--share-net`). + +| | pasta | slirp4netns | `--share-net` | +| --- | --- | --- | --- | +| Address in the sandbox | the host's | `192.168.254.100` (IPv4 only) | the host's | +| 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` | 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`, 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 +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. + +### 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` | `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` 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 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 +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): both backends deliver 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. + +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 or claimed by another of your sandboxes on `ADDR`. + +**`--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 +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 + +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=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. 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). + `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 + only between its open and its close. +- pasta logs no byte counts. +- `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 + `BWRAP_SESSION` is the same ID, and the context file names it. +- **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 + the start record itself. With neither, the launch warns and continues + without the log. +- **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, + 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 + 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 + 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 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. + - 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, 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 + 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 + 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 (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 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 + `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 + 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`. + +### 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 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 +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 '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 +``` + +Inside a sandbox, `BWRAP_SESSION` is the session ID (the context file names it +too). + +**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 +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 + 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. +- **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 + 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. + +### Implementation notes: slirp4netns + +slirp4netns cannot start the sandbox; it attaches to an existing network +namespace. The launch has two steps, and the launcher still `exec`s in the +foreground, so the terminal, job control, the exit status and +`--die-with-parent` behave as before: + +```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> [--dns ...] -- + 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. +- **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. 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`), 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. +- 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-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 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`. +- `--publish` uses slirp4netns's API socket (`add_hostfwd`, one request per + 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. + 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 (the unshare or the descriptor relay failed and printed why), + the helper notices within a poll interval, signals nothing, and exits. + +### 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 +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 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. + ## Account check Every `bw*` launcher runs `/opt/nsls2/libexec/agent-account-check` first @@ -437,9 +926,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 +1072,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,15 +1084,18 @@ 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 `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 +1109,113 @@ 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. +- **Published like `--publish`.** The server's port is added to + [`--publish`](#opening-ports), so it is reachable on + 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. 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 + (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.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`, 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 + `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 the link twice, for the tunnel + (`http://127.0.0.1:/auth/connect/`) and for this host + (`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 + 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 + 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 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 + 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. 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` Launches the [Claude CLI](https://docs.claude.com/en/docs/claude-code/overview) inside a bubblewrap sandbox. @@ -723,7 +1323,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 @@ -734,5 +1334,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_share`, `net_set_backend`, +`net_print_help` and `build_net_isolation` (see [Network](#network)). diff --git a/lib/bwrap_netlog.py b/lib/bwrap_netlog.py new file mode 100644 index 0000000..bf6c451 --- /dev/null +++ b/lib/bwrap_netlog.py @@ -0,0 +1,922 @@ +#!/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 [--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 + 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 [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 + (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. +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 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 +# lookups made before it listens skip it (see bwrap_sandbox_lib.sh). +import collections +import ctypes +import ipaddress +from concurrent.futures import ThreadPoolExecutor +import os +import re +import signal +import socket +import struct +import sys +import threading +import time + +# ── 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+)|\?)$" +) +_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.""" + # 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('"', '\\"') + + +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.""" + + 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 + + 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 = _TS.match(raw) + if not m: + 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) + m = _FLOW.match(m.group(2)) + 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("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 + 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"])) + # 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": + parts.append("domain=%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 _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 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: `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. + """ + + 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.""" + 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 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: + yield data + 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 + 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 + 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"" + + +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. + + 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 data in reader.chunks(): + try: + 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. + + 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, 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 + while i < len(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": + 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 == "--backend": + backend = argv[i + 1] + elif a == "--": + cmd = argv[i + 1 :] + break + else: + 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 + + proc = None + if cmd: + # 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, 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: + sys.stdout.buffer.write(data) + sys.stdout.buffer.flush() + except (BrokenPipeError, OSError): + state["dead"] = True + return False + return True + + c = Condenser(emit, dns_forward) + + def feed(raw): + try: + 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: + # 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("%s: %s\n" % (backend, raw)) + sys.stderr.flush() + if bridge: + bridge_stop.set() + bridge.join(5) + c.finish() + 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: + proc.stdin.close() + except OSError: + pass + try: + proc.wait(SINK_CLOSE_WAIT) + except subprocess.TimeoutExpired: + proc.kill() + proc.wait() + if session_dir: + import shutil + + shutil.rmtree(session_dir, ignore_errors=True) + + +# ── 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, 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: + 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", [] + 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" % _value(",".join(cnames)) if cnames else "", + ) + with self.lock: + try: + self.out.write(line) + self.out.flush() + except OSError: + pass + + def _udp_one(self, srv, query, client): + response = None + try: + 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 + # 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"" + 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): + with conn: + conn.settimeout(10) + while True: + 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. + 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]) + 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 + + 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 + + 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) + # Port zero is useful for isolated tests; TCP must share UDP's port. + 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. 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) + # 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(): + try: + conn, _ = self.tcp.accept() + except socket.timeout: + continue + except OSError: + break + if not self._submit(self._tcp_one, conn): + conn.close() + + 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(): + """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]) + # 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 + except (OSError, AttributeError): + pass + if os.getppid() != parent: + return + relay = DnsRelay(listen, upstream) + try: + 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() + 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) in (5, 7) 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 0c1fad1..75e3bfb 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,12 @@ # - 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 / --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")")" @@ -29,6 +35,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). +# Network: a private network namespace from pasta (package passt, RHEL 9+), +# 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"). # Guard against double-sourcing. [[ -n "${_BWRAP_SANDBOX_LIB_SOURCED:-}" ]] && return 0 @@ -58,6 +68,23 @@ 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_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 +# "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="" +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=() @@ -83,6 +110,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). @@ -1059,6 +1094,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 @@ -1899,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 @@ -1913,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 ──────────────────────────────────── @@ -2184,10 +2227,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 @@ -2204,6 +2246,1883 @@ build_etc_mounts() { ) } +# ═══════════════════════════════════════════════════════════════════ +# Network isolation +# ═══════════════════════════════════════════════════════════════════ +# 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 +# +# 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 +# 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). +# 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 +# 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_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_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). +_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, 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=() + +# 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, +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. 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 + 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. + --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 + 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). + +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 +} + +# 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() { + NET_SHARE=1 +} + +# _net_forward_options +# 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) + printf '%s' "${_opts[*]}" +} + +# detect_net_capabilities +# 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 _fwd _want="${NET_BACKEND_FORCE}" _pasta_reason _slirp_reason + _NET_PRIVATE=0 + _NET_BACKEND=none + _NET_SHARED_REASON="" + _fwd="$(_net_forward_options)" + if [[ "${NET_SHARE}" -eq 1 ]]; then + if [[ -n "${_fwd}" ]]; then + 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 + + _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 ]]; then + _slirp_reason="does not support --host-port (pasta only)" + elif _net_try_slirp; then + _NET_BACKEND=slirp4netns + _NET_PRIVATE=1 + return 0 + else + _slirp_reason="${_NET_TRY_REASON}" + fi + fi + + 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 ' 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 +} + +# _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 _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 + # --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 + [[ "${_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 + if [[ ! -c /dev/net/tun ]]; then + _NET_TRY_REASON="/dev/net/tun is missing on the host" + return 1 + fi + 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 +} + +# ── 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 +} + +# ── 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 user=NAME +# 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: without --log-file it writes every +# line, timestamped, to its stderr (log.c vlogmsg). So with pasta: +# +# 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 ... +# +# - 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 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, 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 +# 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. +# 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 +# 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_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="" +_NET_LOG_USER="" +_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 +# 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_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 + # 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 + # 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. 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 [[ -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}") +} + +# _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_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=${_NET_LOG_USER} 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 + 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 + [[ -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 --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 _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)" + fi + 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 + _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 + 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_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}" + 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)" + 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 + # 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 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_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}" --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}") + [[ -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 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 + _net_log_supervisor & +} + +# _net_log_supervisor +# 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=() + 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) + _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 + # 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 & + _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 '%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 '%s%s\n' "${_prefix}" "${_part}${_line}" 1>&"${_sink}" 2> /dev/null || _dead=1 + _part="" + done + exec {_fifo}<&- + if [[ "${_started}" -eq 0 ]]; then + # 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: %s: %s\n' "${0##*/}" "${_NET_BACKEND}" "${_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" + [[ "${_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' +} + +# _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 (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' + 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 ────────────────────────────────────────── +# 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 ... 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 +# closed --block-fd would then release. +# +# 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 +# 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-seccomp as podman does, when /etc/resolv.conf resolves +# 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 +# request); --host-port has no slirp4netns equivalent. + +_SLIRP_BIN="" +_SLIRP_ARGS=() +_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))) +' + +# 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 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 +# +# --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. 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. 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. +# +# 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: +# +# - 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. +# +# 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 +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: + 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) +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) +' + +# _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 +# _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 + # 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 + _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 + _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-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 +} + +# _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 session directory, removed once open), start the +# helper in the background and close the helper's descriptors in the +# launcher. +_net_slirp_start() { + local _f + _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 + # 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" + # 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" + 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="" _rc _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 + _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 + 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>&- + + # 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 + 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_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 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]}" +} + +# _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). 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 _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 + while IFS= read -r -t 5 _line <&93; do + _info+="${_line}" + [[ "${_line}" != *"}"* ]] || break + done + if [[ "${_info}" =~ \"child-pid\":\ *([0-9]+) ]]; then + _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 + [[ -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. +_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_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 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" + 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_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}") +} + +# _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_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) +} + +# ── 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 2> /dev/null < "/proc/$1/stat" || 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_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() { + 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), 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##* }" + [[ "${_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_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 (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() { + 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 + 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 + 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 + fi + + _prefix="$(net_publish_prefix)" + if [[ "${DRY_RUN}" -eq 1 ]]; then + _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="${_prefix}.${_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 ${_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 +} + +# _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 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 +# 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() { + local _spec _host _sbx _i _hosts _resolv + _NET_CONFIGURED=1 + _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 + BWRAP_ARGS+=( + --ro-bind-try /etc/resolv.conf /etc/resolv.conf + --ro-bind-try /etc/hosts /etc/hosts + ) + SANDBOX_ENV+=( + "BWRAP_NET_BACKEND=none" + "BWRAP_NET_PUBLISH=" + "BWRAP_NET_PUBLISH_ADDR=" + "BWRAP_NET_HOST_PORTS=" + ) + return 0 + fi + 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 + # _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 + --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 + 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#*:}" + 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, 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%%:*}" + _sbx="${_spec#*:}" + if [[ "${_host}" == "${_sbx}" ]]; then + _tspec+=("${_host}") + else + _tspec+=("${_sbx}:${_host}") + fi + _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_join "${_tspec[@]}")" + fi + done + 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 ]] && + ! 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 + _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) + + if [[ -r /etc/hosts ]]; then + _hosts="$(< /etc/hosts)" + else + _hosts="127.0.0.1 localhost"$'\n'"::1 localhost" + fi + bwrap_fd_from_data "generated /etc/hosts" "${_hosts}" + BWRAP_ARGS+=(--ro-bind-data "${_BWRAP_FD}" /etc/hosts) + + SANDBOX_ENV+=( + "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[@]}"})" + ) +} + +# _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 [[ "${_NET_PRIVATE}" -ne 1 ]]; then + if [[ "${NET_SHARE}" -eq 1 && -t 2 ]]; then + printf '%s: network: shared with the host (--share-net)\n' "${_launcher}" >&2 + fi + return 0 + fi + if [[ ${_n_fwd} -gt 0 || -t 2 ]]; then + _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 + 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 +} + +# _net_require_configured +# 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 ]] || + [[ "${_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 +} + # ── OpenCode config requirement ─────────────────────────────────── # require_opencode_config WRAPPER USER_CONFIG_DIR # Exit 2 unless at least a minimal OpenCode config exists: a system-wide @@ -2662,6 +4581,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 @@ -2988,6 +4917,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 @@ -3019,8 +4949,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 @@ -3174,6 +5105,66 @@ _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}" + if [[ "${_NET_PRIVATE}" -ne 1 ]]; then + _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:" + _t+=" bind \`127.0.0.1\` unless told otherwise.${_nl}${_nl}" + _net_log_context_text + _CTX_NET="${_t}${_NET_LOG_CTX}" + return 0 + fi + _t+="This sandbox has its own network namespace (${_NET_BACKEND})." + _t+=" Outbound connections work and leave from the host's address;" + 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." + _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 + _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#*|}\` here reaches the host's \`${_d%%|*}\`.${_nl}" + done + _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\` and \`BWRAP_NET_HOST_PORTS\` hold the same information.${_nl}${_nl}" + _net_log_context_text + _CTX_NET="${_t}${_NET_LOG_CTX}" +} + # _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 @@ -3261,7 +5252,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 && "${_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." + 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}" @@ -3286,6 +5282,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." @@ -3386,7 +5386,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 +5397,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 +5435,174 @@ _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. +# 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). +# 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 _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 ${_NET_FD_DNS_UDP} ]]; 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. +bwrap_fd_from_file() { + local _path="$1" + if [[ ! -f "${_path}" || ! -r "${_path}" ]]; then + 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}") +} + +# 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_check_room + _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 + ;; + note) + printf "%s\n" "$3" >&"$1" + shift 3 + ;; + append) + 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" "$$" "$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]}>&-" + # 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 + 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 + case "${_BWRAP_FD_RELAY[$((_r + 1))]}" in + data) + _BWRAP_FD_DATA_NUMS+=("${_BWRAP_FD_RELAY[${_r}]}") + _r=$((_r + 2)) + ;; + # 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 +} + # ═══════════════════════════════════════════════════════════════════ # Shared launch functions # ═══════════════════════════════════════════════════════════════════ @@ -3559,14 +5726,22 @@ _is_secret_env_name() { print_dry_run() { verify_n2c_state_not_exposed - local _BWRAP_BIN _ENV_BIN + _net_require_configured + 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 + # [ --] (pasta backend 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 # 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 +5756,46 @@ 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 + local _p=0 + [[ "${_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))]}" + _p=$((_p + 2)) + ;; + *) + printf " %s \\\\\n" "${PASTA_ARGS[${_p}]}" + _p=$((_p + 1)) + ;; + esac + done + [[ "${_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 + 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 +5829,12 @@ 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 + [[ "${_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/ *$//' + done exit 0 } @@ -3634,17 +5850,58 @@ 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. +# +# pasta starts the descriptor relay in the sandbox's network namespace (see +# "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 - local _BWRAP_BIN _ENV_BIN + _net_require_configured + 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=() _net_prefix=() + _net_print_report + _net_log_start + if [[ "${_NET_BACKEND}" == pasta ]]; then + _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 + _data_env+=("_BWRAP_FD_DATA_${_n}=${_BWRAP_FD_DATA["${_n}"]}") + done + [[ "${_NET_BACKEND}" != slirp4netns ]] || _net_slirp_start + if [[ -n "${_NET_LOG_FIFO}" ]]; then + # 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}" \ USER="${_USER}" \ LOGNAME="${_USER}" \ PATH="/usr/bin:/bin" \ ${SANDBOX_ENV[@]+"${SANDBOX_ENV[@]}"} \ + ${_data_env[@]+"${_data_env[@]}"} \ + ${_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[@]}"} } 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 70b8faf..672260f 100644 --- a/pixi.toml +++ b/pixi.toml @@ -27,7 +27,9 @@ 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" 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..e131d71 --- /dev/null +++ b/tests/data/pasta-debug.log @@ -0,0 +1,874 @@ +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/fake-net-run b/tests/fake-net-run new file mode 100755 index 0000000..b525689 --- /dev/null +++ b/tests/fake-net-run @@ -0,0 +1,67 @@ +#!/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. 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 +# $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 +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/fakes/_nsproxy.py b/tests/fakes/_nsproxy.py new file mode 100644 index 0000000..70acb30 --- /dev/null +++ b/tests/fakes/_nsproxy.py @@ -0,0 +1,182 @@ +"""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 struct +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) + + +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 (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 + 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) + 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) + _keep_caps_across_exec() + 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"] == "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()) + 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 udp(self, host, port): + return self._request(op="udp", 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, 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: + c, peer = listener.accept() + except OSError: + return + try: + t = connect() + except OSError: + c.close() + continue + 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() + + +def port_range(spec): + """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/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/pasta-bin/pasta b/tests/fakes/pasta-bin/pasta new file mode 100755 index 0000000..20f2321 --- /dev/null +++ b/tests/fakes/pasta-bin/pasta @@ -0,0 +1,265 @@ +#!/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, 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 +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 + +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") +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) + +log_fd = -1 +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: + 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): + 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: + 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) + 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") + os.close(ready_w) + 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) + 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)", "info: ") + +import socket # noqa: E402 + +for spec in fwd_t: + addr, _, ports = spec.rpartition("/") + 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: + 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), + 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 = [] + 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("127.0.0.1", 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) +_, 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)) diff --git a/tests/test_fd_relay.py b/tests/test_fd_relay.py new file mode 100644 index 0000000..eb3e7bc --- /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[@]}" -- \ + 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"] + + +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 new file mode 100644 index 0000000..56ab70c --- /dev/null +++ b/tests/test_netlog.py @@ -0,0 +1,694 @@ +"""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 +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 +import os +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")) +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): + 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_only_flows_and_dns(self): + for line in self.out: + self.assertRegex(line, r"^(flow=\d+ (open|close|fail) |dns )") + + 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) + 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("domain=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_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( + [ + 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 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 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")) + + 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) + + 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() + 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() + 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._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() + 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 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 not self.stop.is_set(): + try: + c, _ = self.up_tcp.accept() + except socket.timeout: + continue + except OSError: + 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)) + c.sendall(struct.pack("!H", len(r)) + r) + except (OSError, struct.error): + pass + + 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): + 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): + 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_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: + 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) + + +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 new file mode 100644 index 0000000..18c8c53 --- /dev/null +++ b/tests/test_netns_identity.py @@ -0,0 +1,198 @@ +"""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 (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. +""" + +import os +import socket +import subprocess +import sys +import unittest + +HERE = os.path.dirname(os.path.abspath(__file__)) +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 + 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) + + +@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() 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()