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..39bca84 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -74,12 +74,12 @@ jobs: CODESPELL_VER=$(pixi list -e dev --json | jq -r '.[] | select(.name=="codespell") | .version') YAMLLINT_VER=$(pixi list -e dev --json | jq -r '.[] | select(.name=="yamllint") | .version') ZIZMOR_VER=$(pixi list -e dev --json | jq -r '.[] | select(.name=="zizmor") | .version') + ACTIONLINT_VER=$(pixi list -e dev --json | jq -r '.[] | select(.name=="actionlint") | .version') # Extract versions from .pre-commit-config.yaml rev: tags # (strip wrapper-specific build suffixes where applicable) PRECOMMIT_HOOKS_VER=$(grep -A1 'repo: https://github.com/pre-commit/pre-commit-hooks' .pre-commit-config.yaml | grep 'rev:' | awk '{print $2}') MARKDOWNLINT_VER=$(grep -A1 'repo: https://github.com/igorshubovych/markdownlint-cli' .pre-commit-config.yaml | grep 'rev:' | awk '{print $2}') - ACTIONLINT_VER=$(grep -A1 'repo: https://github.com/rhysd/actionlint' .pre-commit-config.yaml | grep 'rev:' | awk '{print $2}') # shfmt: pre-commit-shfmt adds -N build numbers (v3.12.0-2 -> v3.12.0) SHFMT_VER=$(grep -A1 'repo: https://github.com/scop/pre-commit-shfmt' .pre-commit-config.yaml | grep 'rev:' | awk '{print $2}' | sed 's/-[0-9]\+$//') # eratta: shellcheck-py: Python wrapper adds .N to upstream shellcheck version (v0.11.0.1 -> v0.11.0) @@ -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..11dbd8b 100644 --- a/AI_DISCLOSURE.md +++ b/AI_DISCLOSURE.md @@ -8,6 +8,7 @@ models-used: - claude-sonnet-5 - claude-sonnet-5-5 - gpt-5.6-sol + - gpt-6-sol providers: - Anthropic - OpenAI @@ -19,7 +20,7 @@ scope: Shell wrapper scripts (bin/, lib/), the `azoidcapp` Python helper, documentation (docs/), and CI/lint configuration — all with human review and validation. -last-updated: 2026-10-24 +last-updated: 2026-11-10 --- # AI Disclosure diff --git a/README.md b/README.md index f3f4d5c..64b763b 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ docs/ Per-script and per-library documentation | `bwcopilot` | GitHub Copilot CLI in a bubblewrap sandbox | [docs/bw-wrappers.md](docs/bw-wrappers.md) | | `bwopencode` | OpenCode in a bubblewrap sandbox | [docs/bw-wrappers.md](docs/bw-wrappers.md) | | `bwopencode2` | OpenCode 2 (`opencode2`) in a bubblewrap sandbox | [docs/bw-wrappers.md](docs/bw-wrappers.md) | +| `bwopenchamber` | OpenChamber (web UI for OpenCode) in a bubblewrap sandbox | [docs/bw-wrappers.md](docs/bw-wrappers.md) | | `opencode2-db` | Back up, check and recover the OpenCode 2 database | [docs/opencode2-db.md](docs/opencode2-db.md) | | `gh-protect-branch` | Apply NSLS-II standard branch protection to a GitHub repo (all branches); enables secret scanning and push protection; optionally restricts branch creation to named patterns | [docs/gh-protect-branch.md](docs/gh-protect-branch.md) | | `pemdecompose` | List and verify certificates in a PEM file | [docs/pemdecompose.md](docs/pemdecompose.md) | @@ -71,6 +72,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` @@ -86,6 +95,7 @@ Add those three exports to your `~/.bashrc` or `~/.zshrc` for persistence. ```text bwopencode [bwopencode-options] [opencode arguments...] bwopencode2 [bwopencode2-options] [opencode2 arguments...] +bwopenchamber [bwopenchamber-options] bwclaude [bwclaude-options] [claude arguments...] bwcopilot [bwcopilot-options] [copilot arguments...] bwcodex [bwcodex-options] [codex arguments...] @@ -96,13 +106,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/bwopenchamber b/bin/bwopenchamber new file mode 100755 index 0000000..5c94ddb --- /dev/null +++ b/bin/bwopenchamber @@ -0,0 +1,695 @@ +#!/usr/bin/env bash +# bwopenchamber — Run OpenChamber (the web UI for OpenCode) inside a bubblewrap +# (bwrap) sandbox. +# +# OpenChamber (npm @openchamber/web, https://openchamber.dev) is a Node.js web +# server that starts its own `opencode serve` and adds a terminal, git and +# worktree handling, scheduled tasks and so on. All of that does work, so all +# of it runs INSIDE this one sandbox: the server, its OpenCode, the browser +# terminal's shells, git, and every tool call the agent makes. Only the browser +# is outside, on another machine, reached through an ssh tunnel. Do not point +# an OpenChamber that runs outside a sandbox at a sandboxed OpenCode: its +# terminal, git and worktree commands, and the tools it gives the agent, would +# run unsandboxed. +# +# It sources bwopencode2 for the OpenCode 2 mounts, environment, database guard +# and the --serve port pool, so a sandbox fix made there applies here too. +# +# Requires: Node.js >= 24.14 (see och_resolve_node for where it is looked for: +# the `node` on PATH if it is new enough, else a side-by-side node-24 such as the +# RHEL 10 nodejs24 package; BWOPENCHAMBER_NODE names one), OpenChamber installed in +# /opt/nsls2/openchamber (BWOPENCHAMBER_DIR overrides; mounted read-only) and +# OpenCode 2 in /opt/nsls2/opencode2, and the same OpenCode config +# bwopencode2 needs. +# +# What differs from bwopencode2 --serve: +# - The server is OpenChamber. OpenChamber starts and owns its OpenCode; +# OPENCODE_HOST, OPENCODE_PORT, OPENCODE_SKIP_START and every other host +# OPENCODE_* or OPENCHAMBER_* variable never reach it. +# - The port comes from the same pool as `bwopencode2 --serve`, claimed in the +# same state directory, so one ssh_config alias (--ssh-config) covers both. +# - The policy is a read-only /etc/openchamber/policy.json that this launcher +# generates inside the sandbox (the host's file is not visible there): +# enterprise mode (no tunnels, no public relay, no classification service, no +# adding model providers in the app), the installed OpenCode 2 pinned, and +# allowNetworkAccess true because the server listens on the private +# network's interface. It is a file, not environment variables, because +# without one a saved setting (settings.json, which the agent can write) +# overrides OPENCODE_BINARY, and only a policy file outranks the settings. +# - OpenChamber's data directory (settings, projects, chats, worktrees) is +# kept on the host in ~/.local/share/bwopenchamber, apart from any +# OpenChamber run outside a sandbox. Its run/ directory, where OpenChamber +# writes the UI password in plain text, is an empty tmpfs. +# - No OpenChamber command-line argument is passed through: the server is +# always `serve --foreground` on the published address. +# +# Usage: +# bwopenchamber [bwopenchamber-options] +# bwopenchamber --help +# +# Options: see --help. + +set -euo pipefail + +SCRIPT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" +# shellcheck source=bwopencode2 +source "${SCRIPT_DIR}/bwopencode2" + +# ═══════════════════════════════════════════════════════════════════ +# Tool-specific state +# ═══════════════════════════════════════════════════════════════════ + +OCH_DIR="${BWOPENCHAMBER_DIR:-/opt/nsls2/openchamber}" +OCH_CLI_REL="node_modules/@openchamber/web/bin/cli.js" +OCH_MIN_NODE_MAJOR=24 +OCH_MIN_NODE_MINOR=14 +# Where versioned Node binaries (node-24, nodejs24) are looked for, besides PATH. +OCH_NODE_DIRS="${BWOPENCHAMBER_NODE_DIRS:-/usr/bin:/usr/local/bin}" +OCH_CLI="" +OCH_SERVE=1 +OCH_SSH_CONFIG=0 +OCH_DATA_SRC="" +OCH_DATA_DIR="" +OCH_JWT_SECRET="" + +# ═══════════════════════════════════════════════════════════════════ +# Replacements for functions of bwopencode2 +# ═══════════════════════════════════════════════════════════════════ + +# The TUI tweaks of build_opencode2_env do not apply to a web server. +build_opentui_zellij_env() { :; } + +parse_wrapper_args() { + while [[ $# -gt 0 ]]; do + case "$1" in + --help | -h) + SHOW_HELP=1 + shift + ;; + --dry-run) + DRY_RUN=1 + shift + ;; + --exec) + if [[ $# -lt 2 ]]; then + echo "Error: --exec requires a command argument." >&2 + exit 1 + fi + TEST_CMD="$2" + shift 2 + ;; + --github-tokens) + # shellcheck disable=SC2034 # read by build_opencode2_env in bwopencode2 + FORWARD_GH_TOKENS=1 + shift + ;; + --new-session) + # shellcheck disable=SC2034 # consumed by sourced bwrap_sandbox_lib.sh + FORCE_NEW_SESSION=1 + shift + ;; + --n2c-token) + if [[ $# -lt 2 ]]; then + echo "Error: --n2c-token requires a token name or 'all'." >&2 + exit 1 + fi + add_n2c_token "$2" + shift 2 + ;; + --no-sandbox-context) + # shellcheck disable=SC2034 # consumed by sourced bwrap_sandbox_lib.sh + SANDBOX_CONTEXT_ENABLED=0 + shift + ;; + --port) + if [[ $# -lt 2 ]]; then + echo "Error: --port requires a port number." >&2 + exit 1 + fi + SERVE_PORT_REQUEST="$2" + shift 2 + ;; + --ssh-config) + OCH_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 + ;; + --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 + exit 1 + fi + EXTRA_RO_PATHS+=("$2") + shift 2 + ;; + --rw-path) + if [[ $# -lt 2 ]]; then + echo "Error: --rw-path requires a path argument." >&2 + exit 1 + fi + EXTRA_RW_PATHS+=("$2") + shift 2 + ;; + *) + break + ;; + esac + done + # shellcheck disable=SC2034 # consumed by library's print_dry_run/launch_sandbox + TOOL_ARGS=("$@") +} + +show_help() { + cat << 'HELP_EOF' +bwopenchamber — OpenChamber (web UI for OpenCode) inside a bubblewrap sandbox + +OpenChamber, its OpenCode, the browser terminal, git and every tool call run +inside this one sandbox. Open the UI from a browser on another machine through +an ssh tunnel; the command, the URL and a password (new for each session) are +printed at start. The server listens on the sandbox's private network and only +its port is published, on your own loopback address 127.H1.H2.1. That address +is not access control: any user of this host can connect to it, and the password +is what keeps them out. + +Wrapper options: + -h, --help Show this message. + --dry-run Print the bwrap command that would be run, then exit. + --port N Use port N (inside the range) instead of the first free one of + the range 24700-24709 (BWOPENCODE2_SERVE_PORTS=LO-HI changes + it; the pool is shared with `bwopencode2 --serve`). Fails if + it is taken. + --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. The same block + serves `bwopencode2 --serve`. + --exec CMD Run CMD inside the sandbox instead of the server (a shell for + diagnostics). CMD is split on whitespace; no port, password + or server is set up. + --publish PORT[:SANDBOXPORT] + Also publish a server of the sandbox (for example a project's + dev server, for OpenChamber's Preview) on your address + 127.H1.H2.1. May be repeated. + --host-port PORT[:SANDBOXPORT] + Make the host's 127.0.0.1:PORT reachable in the sandbox + (pasta only). May be repeated. + --share-net Share the host network instead of a private one (the server + then binds your address 127.H1.H2.1 itself). Only for a host + without pasta or slirp4netns; OpenCode's own port is then on + the host's loopback too, behind its own random password. + --net-backend pasta|slirp4netns + Use only this backend for the private network. + --n2c-token NAME + Mount the N2C access token NAME (read-only) so skills inside + the sandbox can call NSLS-II APIs. May be repeated; `all` + mounts every token present. Off by default. + --ro-path PATH Mount PATH read-only into the sandbox. May be repeated. + --rw-path PATH Mount PATH read-write into the sandbox. May be repeated. + Only the directory you start in is mounted read-write, so a + project OpenChamber opens elsewhere needs --rw-path; start + in a parent directory that is not a git repository to give it + several projects. + --github-tokens Forward any GH_TOKEN_* environment variables into the sandbox + (read-only tokens for the gh CLI). Off by default. + --new-session Force bwrap --new-session (breaks terminal resize). + --no-sandbox-context + Do not generate the sandbox context file. + +Node.js 24.14 or newer is needed. The launcher uses BWOPENCHAMBER_NODE if set, else +the node on PATH if it is new enough, else the newest node-24 / nodejs24 in /usr/bin +or /usr/local/bin that is (the RHEL 10 nodejs24 package installs node-24 next to the +default node). BWOPENCHAMBER_DIR names another OpenChamber install. + +The password is random for each session unless BWOPENCODE2_SERVE_PASSWORD is set +(at least 12 characters; a fixed one is shared by every session that uses it). +Browser sign-ins end with the session: their signing key is new for each session. + +The agent can read the credentials OpenChamber gives its own OpenCode (the UI +and server passwords). Nothing leaves the sandbox, but a misbehaving agent +could change settings or answer its own permission prompts; see docs/bw-wrappers.md. + +No OpenChamber argument is passed through, and nothing else in OpenChamber's +own configuration is read from the host: tunnels, the public relay, the +classification service and adding model providers in the app are off (enterprise +mode), and OpenChamber's data is kept in ~/.local/share/bwopenchamber. Do not +sign in to GitHub or add SSH keys inside OpenChamber: the agent can read +anything it stores. Use --github-tokens for read-only access. + +Database guard: as for bwopencode2, the OpenCode 2 database is on NFS; set +OPENCODE2_DB_GUARD=off to skip the checks once. Not applied to --exec or +--dry-run. + +HELP_EOF + net_print_help + exit 0 +} + +# Same shape as bwopencode2's, with an OpenChamber note. The server's own +# OpenCode is not run by this launcher, so the database guard identifies OpenCode +# processes by the installed binary. +guard_opencode2_db() { + [[ "${DRY_RUN}" -eq 0 && -z "${TEST_CMD}" ]] || return 0 + + local _lib="${SCRIPT_DIR}/../lib/opencode2-db-lock.sh" + [[ -r "${_lib}" ]] || return 0 + # shellcheck source=../lib/opencode2-db-lock.sh + source "${_lib}" + oc2db_needs_guard serve || return 0 + + # shellcheck disable=SC2034 # read by oc2db_* in opencode2-db-lock.sh + OC2DB_PROG=bwopenchamber + # shellcheck disable=SC2153 # OC2_DATA_SRC is set by resolve_opencode2_paths + oc2db_guard "${OC2_DATA_SRC}" "${OC2_INSTALL_BIN}" || exit 1 +} + +# ═══════════════════════════════════════════════════════════════════ +# OpenChamber-specific functions +# ═══════════════════════════════════════════════════════════════════ + +# Refuse what this launcher does not do; validate --port. +och_check_args() { + if [[ -n "${TEST_CMD}" ]]; then + OCH_SERVE=0 + if [[ -n "${SERVE_PORT_REQUEST}" ]]; then + echo "Error: --port does not apply to --exec." >&2 + exit 1 + fi + elif [[ ${#TOOL_ARGS[@]} -gt 0 ]]; then + echo "Error: bwopenchamber takes no OpenChamber arguments (got '${TOOL_ARGS[*]}'); the server is always started the same way." >&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: --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 +} + +# och_node_version BIN: print BIN's Node.js version (MAJOR.MINOR.PATCH), or fail. +och_node_version() { + local _v + _v="$("$1" --version 2> /dev/null)" || return 1 + _v="${_v%%$'\n'*}" + _v="${_v#v}" + [[ "${_v}" =~ ^[0-9]+\.[0-9]+ ]] || return 1 + printf '%s' "${_v}" +} + +# och_node_ok VERSION: true when VERSION is at least the minimum OpenChamber needs. +och_node_ok() { + [[ "$1" =~ ^([0-9]+)\.([0-9]+) ]] || return 1 + local _major="${BASH_REMATCH[1]}" _minor="${BASH_REMATCH[2]}" + ((_major > OCH_MIN_NODE_MAJOR || (_major == OCH_MIN_NODE_MAJOR && _minor >= OCH_MIN_NODE_MINOR))) +} + +# och_version_newer A B: true when version A is newer than version B. +och_version_newer() { + [[ "$1" != "$2" && "$(printf '%s\n%s\n' "$1" "$2" | sort -V | tail -n 1)" == "$1" ]] +} + +# Find a Node.js that OpenChamber can run on (24.14 or newer) and set _TOOL_BIN and +# _TOOL_CMD the way bwrap_resolve_tool does. The system `node` can be older than +# that while a newer one is installed next to it (RHEL 10 keeps nodejs 22 as the +# default and installs nodejs24 as node-24), so the order is: +# 1. BWOPENCHAMBER_NODE, when set: only that one, and an error if it is too old; +# 2. `node` on PATH (the user's own choice), when it is new enough; +# 3. the newest of the versioned binaries (node-24, nodejs24, ...) in +# OCH_NODE_DIRS that is new enough. +# When none is, the error lists what was found with versions and says what to do. +och_resolve_node() { + local _c _v _dir _best="" _best_v="" _f _path_node="" + local -a _cands=() _tried=() + + if [[ -n "${BWOPENCHAMBER_NODE:-}" ]]; then + _cands=("${BWOPENCHAMBER_NODE}") + else + _path_node="$(bwrap_find_tool node 2> /dev/null || true)" + [[ -z "${_path_node}" ]] || _cands+=("${_path_node}") + local -a _dirs + IFS=: read -ra _dirs <<< "${OCH_NODE_DIRS}" + for _dir in "${_dirs[@]}"; do + [[ -d "${_dir}" ]] || continue + for _f in "${_dir}"/node-[0-9]* "${_dir}"/nodejs[0-9]* "${_dir}"/node[0-9]*; do + [[ -f "${_f}" && -x "${_f}" ]] && _cands+=("${_f}") + done + done + fi + + local _first_ok="" + for _c in ${_cands[@]+"${_cands[@]}"}; do + if [[ ! -x "${_c}" ]]; then + _tried+=("${_c} (not executable)") + continue + fi + if ! _v="$(och_node_version "${_c}")"; then + _tried+=("${_c} (no version)") + continue + fi + _tried+=("${_c} ${_v}") + och_node_ok "${_v}" || continue + # The node on PATH wins when it is new enough (not just the first candidate: + # with no node on PATH the first one is a versioned binary). + if [[ -n "${_path_node}" && "${_c}" == "${_path_node}" ]]; then + _first_ok="${_c}" + break + fi + if [[ -z "${_best}" ]] || och_version_newer "${_v}" "${_best_v}"; then + _best="${_c}" + _best_v="${_v}" + fi + done + [[ -z "${_first_ok}" ]] || _best="${_first_ok}" + + if [[ -z "${_best}" ]]; then + echo "Error: OpenChamber needs Node.js ${OCH_MIN_NODE_MAJOR}.${OCH_MIN_NODE_MINOR} or newer, and none was found." >&2 + if [[ "${#_tried[@]}" -gt 0 ]]; then + echo " Looked at:" >&2 + printf ' %s\n' "${_tried[@]}" >&2 + else + echo " No node was found on PATH or in ${OCH_NODE_DIRS}." >&2 + fi + echo " RHEL 10: install nodejs${OCH_MIN_NODE_MAJOR} (it installs node-${OCH_MIN_NODE_MAJOR} next to the default node)." >&2 + echo " RHEL 8 and 9: dnf module enable nodejs:${OCH_MIN_NODE_MAJOR} and install nodejs." >&2 + echo " Or set BWOPENCHAMBER_NODE=/path/to/node (for example from pixi: pixi exec -s nodejs=${OCH_MIN_NODE_MAJOR})." >&2 + exit 1 + fi + + _TOOL_BIN="${_best}" + if [[ -n "${TEST_CMD}" ]]; then + read -ra _TOOL_CMD <<< "${TEST_CMD}" + else + _TOOL_CMD=("${_TOOL_BIN}") + fi +} + +# OpenChamber installed, and the OpenCode 2 binary that the policy file will name. +och_check_install() { + _bwrap_canonicalize_safe "${OCH_DIR}" "OpenChamber install dir" + OCH_DIR="${_BWRAP_CANON}" + OCH_CLI="${OCH_DIR}/${OCH_CLI_REL}" + if [[ ! -f "${OCH_CLI}" ]]; then + echo "Error: OpenChamber is not installed (no ${OCH_CLI})." >&2 + echo " It is installed by the ansible base role; BWOPENCHAMBER_DIR names another install." >&2 + exit 1 + fi + if [[ ! -x "${OC2_INSTALL_BIN}" ]]; then + echo "Error: OpenCode 2 is not installed (no ${OC2_INSTALL_BIN}); OpenChamber is told to use only that binary." >&2 + exit 1 + fi +} + +och_resolve_paths() { + OCH_DATA_SRC="${_HOME}/.local/share/bwopenchamber" + OCH_DATA_DIR="${XDG_DATA_HOME}/openchamber" + # Bound whole and read-write, so a symlink here must not lead to a directory + # that contains a blocked path (~/.config/gh, ~/.ssh): the bind check, not + # _check_dir_safe, which only looks at the path itself. The resolved path is + # what is checked, created and mounted. + _bwrap_canonicalize_safe "${OCH_DATA_SRC}" "OpenChamber data dir" + OCH_DATA_SRC="${_BWRAP_CANON}" +} + +# A random signing key for the browser sign-in cookies, new for each session. +# Without one OpenChamber writes a key to jwt-secret in its data directory and +# reuses it, so a browser that signed in once stays signed in across restarts +# and after the password changed (seen with 2.7.0), and the agent can read the +# file. With OPENCODE_JWT_SECRET set it neither reads nor writes the file. +och_make_jwt_secret() { + if command -v openssl > /dev/null 2>&1; then + OCH_JWT_SECRET="$(openssl rand -hex 32)" + else + OCH_JWT_SECRET="$(head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n')" + fi + if [[ ${#OCH_JWT_SECRET} -lt 64 ]]; then + echo "Error: could not generate a random sign-in key." >&2 + exit 1 + fi +} + +# Choose port and password and publish the port; give the sandbox its command. +# Same publish rules as `bwopencode2 --serve`: always your first address +# 127.H1.H2.1, which the ssh_config alias forwards to. +och_prepare() { + SERVE_ADDR="$(net_publish_prefix).1" + serve_choose_port + serve_make_password + och_make_jwt_secret + 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}" + # shellcheck disable=SC2034 # read by bwrap_sandbox_lib.sh + NET_PUBLISH_ADDR="${SERVE_ADDR}" + # shellcheck disable=SC2034 # read by bwrap_sandbox_lib.sh + NET_PUBLISH_ADDR_FIXED_BY="bwopenchamber" + fi + _TOOL_CMD=("${_TOOL_BIN}" "${OCH_CLI}" serve --foreground --host "${SERVE_BIND}" --port "${SERVE_PORT}") + TOOL_ARGS=() +} + +# Tell the user how to reach the server (stderr, before the exec). +och_print_info() { + local _p="${SERVE_PORT}" _host _alias _addr="${SERVE_ADDR}" + if [[ "${NET_SHARE}" -eq 0 && "${_NET_PUBLISH_ADDR_EFFECTIVE}" != "${_addr}" ]]; then + echo "Error: the port was published on ${_NET_PUBLISH_ADDR_EFFECTIVE}, not ${_addr}." >&2 + exit 1 + fi + _host="$(serve_host_name)" + _alias="$(serve_ssh_alias)" + { + printf '\nbwopenchamber: sandboxed OpenChamber 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 (bwopenchamber --ssh-config): ssh %s\n' "${_alias}" + printf ' 2. Open http://127.0.0.1:%s/ there (on this host itself: http://%s:%s/)\n' "${_p}" "${_addr}" "${_p}" + printf ' and sign in with the 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 ' Do not sign in to GitHub or add SSH keys in OpenChamber: the agent can read them.\n' + printf ' Keep the same port number at both ends of the tunnel: the browser cookie is named by it.\n\n' + } >&2 +} + +# ── Mounts ─────────────────────────────────────────────────────── +# OpenChamber's install (read-only) and its data directory (read-write). The +# data directory's run/ holds OpenChamber's pid files and instance files, and +# the instance file contains the UI password in plain text, so it is an empty +# tmpfs: nothing there persists and the password never reaches the host. +build_openchamber_mounts() { + BWRAP_ARGS+=(--ro-bind "${OCH_DIR}" "${OCH_DIR}") + _MOUNTED_PREFIXES["${OCH_DIR}"]=1 + BWRAP_ARGS+=( + --bind "${OCH_DATA_SRC}" "${OCH_DATA_DIR}" + --tmpfs "${OCH_DATA_DIR}/run" + ) +} + +# The policy file OpenChamber reads from its fixed path (see the header). It is +# generated here and mounted read-only from a descriptor, so the agent cannot +# change it and the host's /etc/openchamber/policy.json plays no part. It holds +# no secret. Must run after build_etc_mounts (which sets up /etc). +och_policy_json() { + local _bin + _bin="$(_bwrap_json_quote "${OC2_INSTALL_BIN}")" || return 1 + printf '{\n' + printf ' "enterpriseMode": true,\n' + printf ' "organization": "NSLS-II (bwopenchamber)",\n' + printf ' "allowNetworkAccess": true,\n' + printf ' "allowedExtensions": [],\n' + printf ' "allowLocalExtensions": false,\n' + printf ' "opencodeBinary": %s,\n' "${_bin}" + printf ' "hideBuiltinSkillCatalogs": true\n' + printf '}' +} + +build_openchamber_policy() { + local _text + if ! _text="$(och_policy_json)"; then + echo "Error: bwopenchamber: cannot write the OpenChamber policy (unusual characters in ${OC2_INSTALL_BIN})." >&2 + exit 1 + fi + bwrap_fd_from_data "OpenChamber policy file, mounted at /etc/openchamber/policy.json" "${_text}" + BWRAP_ARGS+=( + --dir /etc/openchamber + --ro-bind-data "${_BWRAP_FD}" /etc/openchamber/policy.json + ) +} + +# Must run AFTER build_opencode2_env, which passes AIFAPIM_* and the allowed +# OPENCODE_* switches; every other host variable was already dropped. +build_openchamber_env() { + # shellcheck disable=SC2034 # consumed by sourced bwrap_sandbox_lib.sh + SANDBOX_ENV+=( + "OPENCHAMBER_DATA_DIR=${OCH_DATA_DIR}" + "OPENCHAMBER_ENTERPRISE_MODE=1" + "OPENCHAMBER_PUSH_RELAY_DISABLED=true" + "OPENCODE_BINARY=${OC2_INSTALL_BIN}" + ) + if [[ "${OCH_SERVE}" -eq 1 ]]; then + SANDBOX_ENV+=( + "OPENCHAMBER_UI_PASSWORD=${SERVE_PASSWORD}" + "OPENCODE_JWT_SECRET=${OCH_JWT_SECRET}" + ) + fi +} + +# Sandbox context for the agent: what it is running under. +build_opencode2_context() { + # shellcheck disable=SC2034 # consumed by sourced bwrap_sandbox_lib.sh + SANDBOX_CONTEXT_NOTES=( + "This session runs under OpenChamber, a web UI started by \`bwopenchamber\`, which runs its own \`opencode serve\` inside this sandbox. Do not start or connect to any other OpenCode server." + "Do not sign in to GitHub, add SSH keys or tokens in OpenChamber: they would be stored where this agent can read them. Read-only \`GH_TOKEN_*\` variables are passed in with \`--github-tokens\`." + "OpenChamber puts its own credentials into the environment this agent inherits (\`OPENCHAMBER_UI_PASSWORD\`, \`OPENCODE_PASSWORD\`, \`OPENCODE_SERVER_PASSWORD\`, \`OPENCODE_JWT_SECRET\`). They are the human user's controls over this session: never read, print or use them, and do not call the OpenChamber or OpenCode HTTP APIs of this session with them." + "Plan mode writes plan files to \`~/.opencode/plan/\`, which is not mounted: they are lost at exit unless the plan-mirror plugin keeps them." + ) + build_sandbox_context bwopenchamber + [[ -n "${SANDBOX_CONTEXT_PATH}" && -z "${TEST_CMD}" ]] || return 0 + + local _path_json + if ! _path_json="$(_bwrap_json_quote "${SANDBOX_CONTEXT_PATH}")"; then + echo "Warning: bwopenchamber: cannot pass the sandbox context file to OpenCode (unusual characters in the path)." >&2 + return 0 + fi + SANDBOX_ENV+=("OPENCODE_CONFIG_CONTENT={\"instructions\":[${_path_json}]}") +} + +# ═══════════════════════════════════════════════════════════════════ +# Main +# ═══════════════════════════════════════════════════════════════════ + +main() { + bwrap_check_account + parse_wrapper_args "$@" + + if [[ "${SHOW_HELP}" -eq 1 ]]; then + show_help + # show_help never returns + fi + + serve_parse_range + if [[ "${OCH_SSH_CONFIG}" -eq 1 ]]; then + serve_print_ssh_config + exit 0 + fi + och_check_args + + och_resolve_node + + detect_kernel_capabilities + resolve_common_paths + och_check_install + resolve_opencode2_paths + och_resolve_paths + # The port is added to --publish, so it comes before the network backend + # is chosen. + if [[ "${OCH_SERVE}" -eq 1 ]]; then + och_prepare + fi + detect_net_capabilities + # Fail before any host-state change (ensure_host_dirs) if nothing is configured. + require_opencode_config bwopenchamber "${OC2_CONFIG_SRC}" + detect_shell + # Skip host-state mutations on dry-run so --dry-run remains side-effect-free. + if [[ "${DRY_RUN}" -eq 0 ]]; then + ensure_host_dirs + mkdir -p "${OCH_DATA_SRC}" + fi + guard_opencode2_db + parse_all_git_includes + resolve_config_symlinks + init_sandbox_path + + # Build bwrap argument list + build_base_sandbox + build_proc_dev_tmp + build_sys_mounts + build_etc_mounts + build_net_isolation + build_managed_config + build_managed_skills + build_managed_extras + build_home_tmpfs + build_workdir_mount + build_persistent_dotdir + build_opencode2_config_mount + build_opencode2_authoring_mounts + build_opencode2_data_mount + build_opencode2_cache_mount + build_opencode2_state_dir + build_openchamber_mounts + build_openchamber_policy + build_git_mounts + build_pixi_mounts + build_ccache_mounts + build_npm_mounts + build_prek_mounts + build_user_bin_mount + build_nsls2_mount + build_opencode2_install_mount + build_dynamic_tool_mounts + build_n2c_token_mounts + build_extra_path_mounts + + # Environment variables + build_env_vars + build_opencode2_env + build_openchamber_env + build_opencode2_context + + # Mask dangerous binaries as the very last step: a mask is hidden by any + # later bind that covers it (e.g. --ro-path /usr or /usr/bin). + build_binary_masks + + # Launch + if [[ "${OCH_SERVE}" -eq 1 ]]; then + och_print_info + fi + if [[ "${DRY_RUN}" -eq 1 ]]; then + print_dry_run + else + launch_sandbox + fi +} + +# Run only when executed, so tests can source this file. +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + main "$@" +fi 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..c9b85b6 100755 --- a/bin/bwopencode2 +++ b/bin/bwopencode2 @@ -79,6 +79,19 @@ # 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. +# +# --serve-open is --serve plus opening the signed-in web UI on the SAME host: a +# helper on the host (outside the sandbox) waits for the server, asks it for +# its own one-time sign-in link and starts google-chrome --app=LINK (or +# chromium, else firefox or the registered default browser). Never started +# without the option; --serve on its own opens nothing. +# # Usage: # bwopencode2 [bwopencode2-options] [opencode2 arguments...] # bwopencode2 --help @@ -93,6 +106,15 @@ # --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-open Like --serve, and also open the web UI, already signed in, in a +# browser on this host (see above). --serve alone opens nothing. +# --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 +132,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 @@ -133,8 +178,9 @@ set -euo pipefail -# Source the shared sandbox library. -SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" +# Source the shared sandbox library. The directory comes from this file, not +# from $0, so it is right when bwopenchamber or a test sources this file. +SCRIPT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" # shellcheck source=../lib/bwrap_sandbox_lib.sh source "${SCRIPT_DIR}/../lib/bwrap_sandbox_lib.sh" @@ -145,6 +191,36 @@ 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_OPEN=0 +SERVE_OPEN_BIN="" +SERVE_OPEN_APP=0 +SERVE_OPEN_WHY="" +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 +266,63 @@ parse_wrapper_args() { SANDBOX_CONTEXT_ENABLED=0 shift ;; + --serve) + SERVE=1 + shift + ;; + --serve-open) + SERVE=1 + SERVE_OPEN=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 +370,41 @@ 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; once the server is up the + ssh command, a one-time sign-in link (valid 5 minutes; press + Enter for a new one) and the password are printed; 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-open Everything --serve does (it needs no --serve next to it), and on a + host with a desktop session also open the web UI, already signed + in, in a browser on this host (never started without this option; + --serve alone opens nothing). The browser is started outside the sandbox + with a one-time link of its own: google-chrome --app=LINK (an + app window), else google-chrome-stable, chromium, + chromium-browser, else firefox LINK, else xdg-open LINK but only + when a default web browser is registered (xdg-mime). + BWOPENCODE2_SERVE_BROWSER names another browser. Opens nothing, + and says so, when neither DISPLAY nor WAYLAND_DISPLAY is set or no + browser is found. Not for use over ssh from another machine: use + the printed ssh command there. + --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 +456,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 +473,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 +481,699 @@ 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 or --serve-open." >&2 + exit 1 + fi + return 0 + fi + if [[ -n "${TEST_CMD}" ]]; then + echo "Error: --serve and --serve-open cannot be combined with --exec." >&2 + exit 1 + fi + if [[ ${#TOOL_ARGS[@]} -gt 0 ]]; then + echo "Error: --serve and --serve-open take 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 + if [[ "${SERVE_OPEN}" -eq 1 ]]; then + serve_open_pick_browser + 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 +# how to reach it, with a one-time sign-in link so the browser needs no password +# (the web UI's "Use password" button still works), and print a new link each +# time Enter is pressed (a link works once and expires after 5 minutes, and +# setting up an ssh tunnel can take longer than that). It runs as +# `bash -c SERVE_WRAPPER bwopencode2-serve BINARY PORT BIND SSHHOST SSHALIAS PWKIND MODE WHY` +# (PWKIND is `random` or `fixed`; MODE is `full` for --serve, the block with the +# ssh command, `quiet` for --serve-open when the host helper opens a browser and +# prints the result itself, and `local` for --serve-open when no browser can be +# started, then WHY says why). 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 ssh_host=${4:-localhost} ssh_alias=${5:-} pw_kind=${6:-random} +mode=${7:-full} why=${8:-} +# The server's own address for the requests below, and the host-side address +# for the printed links (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} +code="" +stop=0 + +"$bin" serve --hostname "$bind" --port "$port" & +srv=$! +trap 'stop=1; kill "$srv" 2>/dev/null' INT TERM HUP + +# Ask the server for a one-time pairing code; sets $code, empty on failure. A +# backslash or double quote in a fixed password is escaped for curl's config. +pair() { + local pw=$OPENCODE_PASSWORD resp + code="" + pw=${pw//\\/\\\\} + pw=${pw//\"/\\\"} + resp=$(printf 'user = "opencode:%s"\n' "$pw" | + curl -s --noproxy '*' --connect-timeout 1 --max-time 5 \ + -K - -X POST "${base}/api/pair") || resp="" + if [[ $resp =~ \"code\":\"([A-Za-z0-9_-]+)\" ]]; then + code=${BASH_REMATCH[1]} + fi +} + +# Print the instructions for the current $code (or the plain page without one). +show() { + local path=/ + [[ -z $code ]] || path=/auth/connect/$code + { + if [[ -n $code ]]; then + printf '\nbwopencode2: the OpenCode web UI is up on port %s.\n' "$port" + else + printf '\nbwopencode2: the OpenCode web UI is up on port %s, but the server gave\n' "$port" + printf ' no sign-in link (could not get a pairing link): use the password below.\n' + fi + printf '\n From your own machine (the server is reachable on this host only):\n' + printf ' 1. forward the port: ssh -L %s:%s:%s %s\n' "$port" "$host_addr" "$port" "$ssh_host" + [[ -z $ssh_alias ]] || printf ' or: ssh %s (the ssh_config alias, see bwopencode2 --serve-ssh-config)\n' "$ssh_alias" + if [[ -n $code ]]; then + printf ' 2. open the sign-in link (works once, expires in 5 minutes):\n' + else + printf ' 2. open the page and click "Use password":\n' + fi + printf ' http://127.0.0.1:%s%s\n' "$port" "$path" + printf ' (keep the same port number at both ends: the login cookie is named by it)\n' + printf ' or as an app window:\n' + printf ' google-chrome --app=http://127.0.0.1:%s%s\n' "$port" "$path" + printf '\n On this host:\n' + printf ' http://%s:%s%s\n' "$host_addr" "$port" "$path" + printf ' (or start with --serve-open instead of --serve to have a browser opened here)\n' + if [[ -n $code ]]; then + printf '\n Link used or expired? Press Enter here for a new one.\n' + fi + if [[ $pw_kind == random ]]; then + printf ' Password (new for this session): %s\n\n' "$OPENCODE_PASSWORD" + else + printf ' Password: the value of BWOPENCODE2_SERVE_PASSWORD (shared by every session that uses it)\n\n' + fi + } >&2 +} + +# --serve-open: only a link to open on this machine, with the password for the +# page's "Use password" if the link fails. $1 is a first line, or empty. +show_local() { + local url="http://${host_addr}:${port}/" + [[ -z $code ]] || url="http://${host_addr}:${port}/auth/connect/${code}" + { + printf '\n' + [[ -z $1 ]] || printf 'bwopencode2: %s\n' "$1" + if [[ -n $code ]]; then + printf ' Open this on this machine: %s\n' "$url" + printf ' (works once, expires in 5 minutes; press Enter for a new link)\n' + else + printf ' Open this on this machine and click "Use password": %s\n' "$url" + fi + if [[ $pw_kind == random ]]; then + printf ' If the link fails, click "Use password": %s\n\n' "$OPENCODE_PASSWORD" + else + printf ' Password: the value of BWOPENCODE2_SERVE_PASSWORD (shared by every session that uses it)\n\n' + fi + } >&2 +} + +# What to print for the current $code, by mode. In quiet mode the host helper +# prints the first message, so nothing is printed until Enter asks for a link. +announce() { + case $mode in + full) show ;; + local) show_local "could not open a browser (${why})." ;; + *) [[ $1 == first ]] || show_local "" ;; + esac +} + +# 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 + http=$(curl -s --noproxy '*' --connect-timeout 1 --max-time 2 \ + -o /dev/null -w '%{http_code}' "${base}/api/info") || http=000 + if [[ $http == 401 || $http == 200 ]]; then + ready=1 + break + fi + sleep 0.5 +done + +if [[ $ready -eq 1 ]]; then + if [[ $mode != quiet ]]; then + pair + fi + announce first + # Enter asks for a new link. The server was started as a background job, so + # it has no stdin and cannot take the key. Without a terminal, or at end of + # input, stop listening for it: the loop must not spin. + if [[ -t 0 ]]; then + while ((!stop)) && kill -0 "$srv" 2> /dev/null; do + if IFS= read -r -t 1 _; then + pair + announce again + elif (($? <= 128)); then + break + fi + done + fi +else + printf 'bwopencode2: the server did not start listening on %s.\n' "$bind:$port" >&2 +fi + +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 + local _mode=full + if [[ "${SERVE_OPEN}" -eq 1 ]]; then + # The host helper opens the browser and reports; without one, the wrapper + # prints the link to open by hand. + _mode=local + [[ -z "${SERVE_OPEN_BIN}" ]] || _mode=quiet + fi + _TOOL_CMD=(/usr/bin/bash -c "${SERVE_WRAPPER}" bwopencode2-serve "${_TOOL_BIN}" "${SERVE_PORT}" "${SERVE_BIND}" + "$(serve_host_name)" "$(serve_ssh_alias)" "$([[ "${SERVE_PASSWORD_RANDOM}" -eq 1 ]] && echo random || echo fixed)" + "${_mode}" "${SERVE_OPEN_WHY}") + TOOL_ARGS=() +} +# Tell the user what starts (stderr, before the exec). The ssh command, the +# sign-in link and the password are printed by the wrapper inside the sandbox +# once the server answers (see SERVE_WRAPPER). +serve_print_info() { + local _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 + if [[ "${SERVE_OPEN}" -eq 1 && "${DRY_RUN}" -eq 0 ]]; then + # One line: the host helper reports whether the browser opened, and the + # link to open by hand if it did not. + printf '\nbwopencode2: starting the sandboxed OpenCode 2 web UI on %s:%s\n' "${_addr}" "${SERVE_PORT}" >&2 + return 0 + fi + { + printf '\nbwopencode2: starting the sandboxed OpenCode 2 web UI on %s:%s (this host only)\n' "${_addr}" "${SERVE_PORT}" + if [[ "${SERVE_OPEN}" -eq 0 ]]; then + printf ' The ssh command, a one-time sign-in link and the password are printed once the server is up.\n' + fi + if [[ "${DRY_RUN}" -eq 1 ]]; then + printf ' Password: (not shown on --dry-run)\n' + fi + if [[ "${SERVE_OPEN}" -eq 1 ]]; then + if [[ -n "${SERVE_OPEN_BIN}" ]]; then + if [[ "${SERVE_OPEN_APP}" -eq 1 ]]; then + printf ' --serve-open: %s --app=http://%s:%s/auth/connect/CODE will be started on this host once the server is up.\n' \ + "${SERVE_OPEN_BIN}" "${_addr}" "${SERVE_PORT}" + else + printf ' --serve-open: %s http://%s:%s/auth/connect/CODE will be started on this host once the server is up.\n' \ + "${SERVE_OPEN_BIN}" "${_addr}" "${SERVE_PORT}" + fi + else + printf ' --serve-open: nothing will be opened (%s); the link to open by hand is printed once the server is up.\n' "${SERVE_OPEN_WHY}" + fi + fi + printf '\n' + } >&2 +} + +# ── --serve-open: a browser on the same host ───────────────────────── +# The browser is started by the launcher's host side, outside the sandbox, as +# the user: it is the user's own browser opening the user's own web UI. The +# sandbox cannot start it, and only does so when --serve-open is given. A +# helper in the background waits for the server, asks it for a one-time pairing +# link of its own (the sandbox prints another one for the ssh case) and starts +# the browser with it, detached, so closing the terminal's server does not close +# the window. It ends by itself after about a minute, or when the launcher has +# gone (the launcher's pid does not change at exec). +# +# The link is on the browser's command line, so another user of this host can +# read it with ps for the moment the browser starts. It works once and expires +# after 5 minutes, and the published address already accepts connections from +# every local user, with the password as the only protection. + +# Browsers tried, in order, when BWOPENCODE2_SERVE_BROWSER is not set; then the +# default browser through xdg-open (see serve_open_default_browser). +SERVE_OPEN_CANDIDATES=(google-chrome google-chrome-stable chromium chromium-browser firefox) + +# True when a default web browser is registered for http, the scheme of the link +# (it is on 127.H1.H2.1, without TLS). Without one, xdg-open does not fail: it +# hands the link to whatever opens a URL as a file (on GNOME, the text editor), +# which would also use up the one-time link. +serve_open_default_browser() { + local _h + command -v xdg-mime > /dev/null 2>&1 || return 1 + _h="$(xdg-mime query default x-scheme-handler/http 2> /dev/null)" || return 1 + [[ -n "${_h}" ]] +} + +# Choose the browser: sets SERVE_OPEN_BIN (empty when nothing will be opened, +# with SERVE_OPEN_WHY saying why) and SERVE_OPEN_APP (1 for Chromium-based +# browsers, which take --app=URL). +serve_open_pick_browser() { + local _b="" _cand + SERVE_OPEN_BIN="" + SERVE_OPEN_APP=0 + SERVE_OPEN_WHY="" + if [[ -z "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ]]; then + SERVE_OPEN_WHY="neither DISPLAY nor WAYLAND_DISPLAY is set, so there is no desktop session here" + return 0 + fi + if [[ -n "${BWOPENCODE2_SERVE_BROWSER:-}" ]]; then + _b="$(command -v -- "${BWOPENCODE2_SERVE_BROWSER}" 2> /dev/null)" || _b="" + if [[ -z "${_b}" ]]; then + echo "Error: BWOPENCODE2_SERVE_BROWSER='${BWOPENCODE2_SERVE_BROWSER}' is not a command on PATH." >&2 + exit 1 + fi + else + for _cand in ${SERVE_OPEN_CANDIDATES[@]+"${SERVE_OPEN_CANDIDATES[@]}"}; do + _b="$(command -v -- "${_cand}" 2> /dev/null)" && break + _b="" + done + if [[ -z "${_b}" ]] && serve_open_default_browser; then + _b="$(command -v -- xdg-open 2> /dev/null)" || _b="" + fi + if [[ -z "${_b}" ]]; then + SERVE_OPEN_WHY="no web browser found: none of ${SERVE_OPEN_CANDIDATES[*]-} is installed and no default browser is registered" + return 0 + fi + fi + SERVE_OPEN_BIN="${_b}" + case "$(basename -- "${_b}")" in + *chrom* | *edge* | *brave*) SERVE_OPEN_APP=1 ;; + esac +} + +# Ask the server for a one-time pairing code; prints it, or nothing. The +# password reaches curl on stdin, never on a command line. +serve_request_code() { + local _pw="${SERVE_PASSWORD}" _resp + _pw="${_pw//\\/\\\\}" + _pw="${_pw//\"/\\\"}" + _resp="$(printf 'user = "opencode:%s"\n' "${_pw}" | + curl -s --noproxy '*' --connect-timeout 1 --max-time 5 \ + -K - -X POST "http://${SERVE_ADDR}:${SERVE_PORT}/api/pair")" || _resp="" + if [[ "${_resp}" =~ \"code\":\"([A-Za-z0-9_-]+)\" ]]; then + printf '%s' "${BASH_REMATCH[1]}" + fi +} + +# How long the helper waits for a browser it started: one that exits with an +# error within this time (xdg-open with no handler, a browser that cannot start) +# counts as not opened. One still running, or that exited 0 (Chrome handing the +# link to a window that is already open), counts as opened. +SERVE_OPEN_WAIT_SECONDS=8 + +# The lines that give a link to open by hand (a fresh one-time link, because a +# failed attempt may have used the first) and what to do if it fails. $1: the +# link, $2: its code (empty when the server gave none). +serve_open_link_lines() { + if [[ -n "$2" ]]; then + printf ' Open this on this machine: %s\n' "$1" + printf ' (works once, expires in 5 minutes; press Enter for a new link)\n' + else + printf ' Open this on this machine and click "Use password": %s\n' "$1" + fi + if [[ "${SERVE_PASSWORD_RANDOM}" -eq 1 ]]; then + printf ' If the link fails, click "Use password": %s\n' "${SERVE_PASSWORD}" + else + printf ' Password: the value of BWOPENCODE2_SERVE_PASSWORD (shared by every session that uses it)\n' + fi +} + +# Say that no browser opened, with the link to open by hand. $1: why. +serve_open_fallback() { + local _code _url="http://${SERVE_ADDR}:${SERVE_PORT}/" + _code="$(serve_request_code)" + [[ -z "${_code}" ]] || _url="${_url}auth/connect/${_code}" + { + printf '\nbwopencode2: could not open a browser (%s).\n' "$1" + serve_open_link_lines "${_url}" "${_code}" + printf '\n' + } >&2 +} + +# Start the browser command $@ detached and wait up to SERVE_OPEN_WAIT_SECONDS: +# returns its exit status if it ended in that time, else 0. +serve_open_browser() { + local _bpid _i _rc=0 + if command -v setsid > /dev/null 2>&1 && setsid --help 2>&1 | grep -q -- --wait; then + setsid -w "$@" < /dev/null > /dev/null 2>&1 & + else + "$@" < /dev/null > /dev/null 2>&1 & + fi + _bpid=$! + for ((_i = 0; _i < SERVE_OPEN_WAIT_SECONDS * 5; _i++)); do + if ! kill -0 "${_bpid}" 2> /dev/null; then + wait "${_bpid}" && _rc=0 || _rc=$? + break + fi + sleep 0.2 + done + return "${_rc}" +} + +# The helper body. $1 and $2: the launcher's pid and start time, to notice that +# it has gone. Runs in a background subshell (see serve_open_start). Prints one +# line when the browser opened, else the link to open by hand. +serve_open_run() { + local _pid="$1" _start="$2" _base="http://${SERVE_ADDR}:${SERVE_PORT}" + local _http _ready=0 _deadline _code _url _rc=0 + local -a _cmd + _deadline=$((SECONDS + 60)) + while ((SECONDS < _deadline)); do + bwrap_proc_is "${_pid}" "${_start}" || return 0 + _http="$(curl -s --noproxy '*' --connect-timeout 1 --max-time 2 \ + -o /dev/null -w '%{http_code}' "${_base}/api/info")" || _http=000 + if [[ "${_http}" == 401 || "${_http}" == 200 ]]; then + _ready=1 + break + fi + sleep 0.5 + done + if [[ "${_ready}" -eq 0 ]]; then + serve_open_fallback "the server did not answer on ${SERVE_ADDR}:${SERVE_PORT}" + return 0 + fi + _code="$(serve_request_code)" + if [[ -n "${_code}" ]]; then + _url="${_base}/auth/connect/${_code}" + else + _url="${_base}/" + fi + _cmd=("${SERVE_OPEN_BIN}") + if [[ "${SERVE_OPEN_APP}" -eq 1 ]]; then + _cmd+=("--app=${_url}") + else + _cmd+=("${_url}") + fi + serve_open_browser "${_cmd[@]}" || _rc=$? + if [[ "${_rc}" -ne 0 ]]; then + serve_open_fallback "$(basename -- "${SERVE_OPEN_BIN}") exited with status ${_rc}" + return 0 + fi + printf '\nbwopencode2: OpenCode web UI on %s:%s, opened in %s.\n' \ + "${SERVE_ADDR}" "${SERVE_PORT}" "$(basename -- "${SERVE_OPEN_BIN}")" >&2 + if [[ -z "${_code}" ]]; then + if [[ "${SERVE_PASSWORD_RANDOM}" -eq 1 ]]; then + printf ' The server gave no sign-in link: click "Use password": %s\n' "${SERVE_PASSWORD}" >&2 + else + printf ' The server gave no sign-in link: click "Use password" and enter the value of BWOPENCODE2_SERVE_PASSWORD.\n' >&2 + fi + fi +} + +# Close every descriptor above 2 (not 255, which bash reads this script through +# and keeps to itself; it holds no lock). +# The launcher holds the database session lock and the sandbox's own descriptors +# open; a browser that inherited them would keep the lock for as long as its +# window lives, and refuse the next launch. +serve_open_close_fds() { + local _f _n + for _f in /proc/self/fd/*; do + _n="${_f##*/}" + [[ "${_n}" =~ ^[0-9]+$ ]] || continue + ((_n > 2 && _n != 255)) || continue + eval "exec ${_n}>&-" 2> /dev/null || true + done +} + +# The helper as a background job: it ignores the terminal's signals (the +# server's Ctrl-C is not its own), reads nothing and holds none of the +# launcher's descriptors. +serve_open_detached() { + trap '' INT QUIT HUP + exec < /dev/null + serve_open_close_fds + serve_open_run "$@" +} + +# Start the helper in the background, just before the sandbox replaces this +# process. Not on --dry-run, and not when no browser was chosen. +serve_open_start() { + [[ "${SERVE_OPEN}" -eq 1 && -n "${SERVE_OPEN_BIN}" && "${DRY_RUN}" -eq 0 ]] || return 0 + local _start + _start="$(bwrap_proc_start_time "$$")" + serve_open_detached "$$" "${_start}" & + disown +} # ── 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 +1557,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 +1646,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 +1672,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 +1701,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 +1744,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 +1760,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,11 +1778,19 @@ main() { build_binary_masks # Launch + if [[ "${SERVE}" -eq 1 ]]; then + serve_print_info + fi if [[ "${DRY_RUN}" -eq 1 ]]; then print_dry_run else + serve_open_start launch_sandbox fi } -main "$@" +# Run only when executed, so bwopenchamber can source this file for its +# functions. +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + main "$@" +fi diff --git a/docs/bw-wrappers.md b/docs/bw-wrappers.md index 56e1aca..da7fbc1 100644 --- a/docs/bw-wrappers.md +++ b/docs/bw-wrappers.md @@ -6,8 +6,9 @@ masked privileged binaries, ephemeral auth on shared accounts, and filesystem isolation from the rest of the host. See the NSLS-II coding-agents documentation for the policy these wrappers implement. -All five wrappers source the shared library `lib/bwrap_sandbox_lib.sh` -for sandbox construction. Each wrapper's `--help` is the authoritative +All six wrappers use the shared library `lib/bwrap_sandbox_lib.sh` +for sandbox construction (`bwopenchamber` through `bwopencode2`, which it +sources). Each wrapper's `--help` is the authoritative option list; the tables below summarise the common surface. [bwrap]: https://github.com/containers/bubblewrap @@ -17,6 +18,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 +99,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 +108,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 +142,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 +242,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 +279,471 @@ 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 on the other side when it differs (the sandbox port + for `publish`). `t` is seconds since pasta started; `secs` is the + connection's duration. Flows still open when the session ends get a close + with `reason=session-end`. +- `flow` is pasta's flow slot, which it reuses: it identifies a connection + only between its open and its close. +- pasta logs no byte counts. +- `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. + - If the tool dies, it goes on reading and drops the lines, so pasta never + blocks on it. + - pasta prints its setup first, and if it fails to start, its error. The + descriptor relay (pasta's command) prints a marker on the FIFO and reopens + your stderr: by path when it is a terminal or a file, else (a pipe) + through a second FIFO that the reader copies. If pasta stops before the + marker, the reader prints pasta's lines on your terminal, prefixed + `pasta:`, just after the launcher has exited. (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 +916,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`, @@ -557,8 +1036,13 @@ host. Before the sandbox starts, `bwopencode2` calls `oc2db_guard` host, is using the same data directory, or after the last daily check found damage; - it holds a shared lock for the life of the sandbox (a file descriptor that - the final `exec` passes on to `bwrap`), so sessions on this host and version - can run together; + the final `exec` passes on to `bwrap`; with a private network see "Where the + lock lives" below), so sessions on this host and version can run together. The + descriptor is numbered 200 or higher on purpose: the launcher hands the sandbox + its files and data on descriptors 10 and up, and the relay opens those over + whatever is there. With the lock on bash's own choice (10 or 11) it was closed, + and so released, whenever the launcher passed two or more data descriptors, as + `bwopenchamber` does (found in review of NSLS2/n2snscripts#43); - it starts a daily background backup (`opencode2-db backup --if-due`) and keeps the newest 3 compressed copies. @@ -568,6 +1052,30 @@ files are in `~/.local/share/opencode2/opencode-db-guard`, which is not mounted in the sandbox. The rules, the recovery steps (`opencode2-db restore` and `rebuild`) and the limits are in [opencode2-db.md](opencode2-db.md). +#### Where the lock lives with a private network + +The text above describes `--share-net`, where the launcher `exec`s into `bwrap` +and the descriptor goes with it. With a private network the launcher `exec`s into +pasta instead, and pasta closes every descriptor above 2 that it inherits (see +"File descriptor relay"), so neither pasta, `bwrap` nor OpenCode holds the lock. +Checked on a real launch (`fuser` on the lock file while a `bwopencode2` session +ran on pasta): the only holders were the network log's helper processes, a forked +copy of the launcher shell and its `bwrap_netlog.py condense` child. The lock +was held for the whole session and was gone after it ended (`opencode2-db +status`: `none held`), so the guard works, but **by inheritance, not by design**: +the helper was started before the `exec`, so it received the descriptor. + +By reading the code (not tested), the lock would not be held when: + +- no log tool can write (neither `systemd-cat` nor `logger`), because the launcher + then starts no helper ("this network session is not logged"), or +- the helper is stopped while OpenCode keeps running. + +In those cases a second host or OpenCode build could pass the guard while the +session runs. The same applies to `bwopenchamber`, which goes through the same +launch. Passing the lock on deliberately is a change to the launch code of +NSLS2/n2snscripts#37. + ### What is kept out of the sandbox - **`service.json`**: OpenCode 2 keeps background-service settings (including @@ -583,9 +1091,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 +1103,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 +1128,480 @@ 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-open [--serve-port N] +bwopencode2 --serve-ssh-config +``` + +Two ways to reach it, both signed in with a one-time link and no password to +type: + +- **From your own machine, over ssh** (the usual case). When the server is up + the launcher prints one block with the `ssh -L` command (and the `ssh + -oc` alias), the sign-in link for `http://127.0.0.1:PORT`, and the same + link as a `google-chrome --app=...` command, which opens the UI as an app + window without tabs or an address bar. A link works once and expires after 5 + minutes, and setting up a tunnel can take longer than that, so **press Enter + in the server's terminal for a new link** (the block is printed again). +- **On the same host** (a desktop or remote desktop session), start with + `--serve-open` instead of `--serve`: the same server, and the browser opens + by itself, already signed in. + See [`--serve-open`](#--serve-open-a-browser-on-the-same-host). + +- **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 one block: the `ssh -L` command and the + alias, the link through the tunnel + (`http://127.0.0.1:/auth/connect/`), the same as a + `google-chrome --app=...` command, the link for this host + (`http://127.H1.H2.1:/...`) and the password. Open a link to be signed + in, with the 30-day cookie, without typing the password. A link works once + and expires after 5 minutes. **Enter** in the terminal asks the server for a + new code and prints the block again; it is only read when the wrapper has a + terminal, and at end of input the wrapper stops reading and keeps waiting + for the server. If the server gives no link, the block shows the plain page + and the password and you click **Use 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 (a backslash or double quote in a + fixed password is escaped for curl's config file). 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 with the sign-in block. 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` and `--serve-open` take 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, and opens no browser. +- **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. + +#### `--serve-open`: a browser on the same host + +For a session on a workstation or a remote desktop, where the browser runs on +the host that runs the server: + +```text +bwopencode2 --serve-open [--serve-port N] +``` + +- **Its own option, not an add-on.** `--serve-open` is `--serve` plus the + browser, so it needs no `--serve` next to it (`--serve --serve-open` means + the same). `--serve` alone never opens anything. +- **Only when asked.** Without `--serve-open` nothing is ever opened, also + with a display available (an `ssh -X` session has one, and Chrome should not + start over X forwarding by surprise). +- **Started on the host, outside the sandbox.** The sandbox cannot start a + browser and does not try. Before the launcher replaces itself with the + sandbox it starts a small helper in the background (the launcher's pid does + not change at `exec`, so the helper can tell when the launcher has gone). The + helper waits up to about a minute for the server to answer on + `127.H1.H2.1:`, asks it for **a one-time pairing code of its own** + and starts the browser detached (`setsid`, output discarded), so ending the + server does not close the window. The password reaches `curl` on its standard + input. It opens nothing when the launcher has exited. +- **What the terminal shows.** Nothing of the ssh block: `--serve-open` is for + the host you are on. The launcher prints one line before the sandbox starts, + then either + + ```text + bwopencode2: OpenCode web UI on 127.52.255.1:24700, opened in google-chrome. + ``` + + or, if no browser opened, a link to open by hand and the password: + + ```text + bwopencode2: could not open a browser (no web browser found: ...). + Open this on this machine: http://127.52.255.1:24700/auth/connect/ + (works once, expires in 5 minutes; press Enter for a new link) + If the link fails, click "Use password": + ``` + + The helper decides after starting the browser: one that exits with an error + within 8 seconds (`xdg-open` with no handler, a browser that cannot start) + counts as not opened, and one still running, or that exited 0 (Chrome handing + the link to a window that is already open), counts as opened. A browser that + fails later is not noticed. The fallback link is a **new** code, because a + failed attempt may already have used the first. The same fallback is printed + by the helper when the server never answered, and by the sandbox's wrapper + when no browser could be chosen (no desktop session, none installed; the line + says why). Enter prints a new link in the same short form. The password is printed only in the + fallback (random password; with `BWOPENCODE2_SERVE_PASSWORD` the line names the + variable), never when the browser opened. `--serve` prints the full block + described above, unchanged. +- **Which browser.** The first of `google-chrome`, `google-chrome-stable`, + `chromium`, `chromium-browser` on `PATH`, started as `BROWSER + --app=LINK` (an app window); else `firefox LINK`; else `xdg-open LINK`, but + only when a default web browser is registered for `http`, the scheme of the + link (`xdg-mime query default x-scheme-handler/http` prints one). Without one `xdg-open` does not fail: on + GNOME it opens the link in the text editor, which shows "Could Not Open File, + HTTP Client Error: Unauthorized" and has used up the one-time link (seen on a + workstation with no browser installed, and the reason for this rule). + `BWOPENCODE2_SERVE_BROWSER` names another command, which must exist + (otherwise the launch stops): a name containing `chrom`, `edge` or `brave` + gets `--app=`, any other (`firefox`, `xdg-open`) gets the plain link. +- **Holds none of the launcher's descriptors.** The launcher has the database + session lock (see Database guard) and the sandbox's own descriptors open when + the helper starts. A browser that inherited them would keep the lock for as + long as its window stays open, and the next `bwopencode2` on the same data + directory would be refused. The helper closes every descriptor above 2 before + it starts the browser (found in Copilot's review of PR #43, reproduced: the + browser held descriptor 200, the lock, until its window closed; the test + fails without the fix). Bash keeps descriptor 255, its handle on the script + file, to itself; it holds no lock. +- **No desktop session.** If neither `DISPLAY` nor `WAYLAND_DISPLAY` is set, or + no browser is found, the launch continues and the launcher says that nothing + will be opened and why; the ssh instructions then apply. +- **The link is on the browser's command line.** Another user of the host can + read it with `ps` for the moment the browser starts. It works once and + expires after 5 minutes, but a local user who gets there first would hold a + signed-in session for 30 days (the published address already accepts + connections from every local user, and the password is the only thing that + stops them). Do not use `--serve-open` on a host where other people log in. +- **Only a browser signs in.** The server answers `GET /auth/connect/` + with `302` to `/` and the login cookie only when the request asks for HTML + (`Accept: text/html`, which every browser sends). Any other client (`curl`, + the GNOME text editor through gvfs) gets `200` with a JSON token, no cookie, + and the code is used up. Checked with OpenCode 2.2.7: `Accept: text/html` + alone gives `302` and the cookie (`Sec-Fetch-*` headers alone do not), and + a second use of the same code gives `401`. +- **Cookie.** The cookie goes into the browser's normal profile, named by the + port as above. Opening `http://127.H1.H2.1:/` again later stays signed + in for as long as the session lives. + +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. + +The sign-in block, Enter for a new link and `--serve-open` were tested without a +real OpenCode or browser: `tests/test_serve_signin.py` runs the wrapper against +a fake server (the right password reaches it through curl, including a quote and +a backslash; a new code on Enter through a pseudo-terminal; no spinning at end +of input; a failed pairing) and the host helper against a stub browser (the +`--app=` link, the plain link, the login page when no link came, a launcher +that has gone, a helper that survives the launcher's `exec`, nothing on +`--dry-run`); `tests/test-bwopencode2-serve.sh` makes 57 checks through +`--dry-run` (arguments, browser choice, no display). Not checked, because it +needs a real launch and a browser: that Chrome opens an app window signed in, +Enter at a terminal inside `bwrap --new-session`, and the helper's output on a +terminal. A real launch on a workstation with no browser, with a stand-in +browser script, showed the helper starting it once the server answered, with +the link of its own; that the link redirects to `/` with the cookie was +checked with `curl` against a throwaway server (see "Only a browser signs in"). + +## `bwopenchamber` + +Launches [OpenChamber](https://openchamber.dev) (npm `@openchamber/web`), a web +UI for OpenCode, inside a bubblewrap sandbox. OpenChamber is a Node.js server +that starts its own `opencode serve` and adds a browser terminal, git and +worktree handling, scheduled tasks and session goals on top of it. + +```text +bwopenchamber [bwopenchamber-options] +bwopenchamber --ssh-config +``` + +**Everything runs in the one sandbox.** The OpenChamber server, its OpenCode, +the browser terminal's shells, git, worktree commands, project start commands, +scheduled tasks and every tool call the agent makes are processes of this +sandbox. Only the browser is outside, on another machine, reached through an +ssh tunnel. This is deliberate: OpenChamber is not only a screen. An +OpenChamber that runs outside a sandbox in front of a sandboxed OpenCode +(`OPENCODE_HOST`, or a Windows or desktop install) would run the terminal, git +and worktree commands unsandboxed, and gives the agent tools that call back into +it. Do not connect one to `bwopencode2 --serve`. + +It is a launcher of its own, not a mode of `bwopencode2`: it sources +`bin/bwopencode2` for the OpenCode 2 mounts, environment, [database +guard](#database-guard) and the [`--serve`](#--serve-the-web-ui-from-a-browser) +port pool, so a fix made there applies here. Moving that shared code into the +library is a follow-up. + +### What it needs + +- **Node.js 24.14 or newer** (OpenChamber's own requirement). The launcher looks + for one in this order: `BWOPENCHAMBER_NODE`, if set (only that one, and an error + if it is too old); the `node` on `PATH`, if it is new enough; the newest + versioned binary (`node-24`, `nodejs24`) in `/usr/bin` and `/usr/local/bin` that + is new enough. That finds the RHEL 10 `nodejs24` package, which installs + `node-24` next to the default `node` (22). On RHEL 8 and 9 the base role enables + the `nodejs:24` module, which puts a new enough `node` on `PATH`. When none + qualifies, the error lists what it found with versions and says what to do. To + use a Node from elsewhere, for example pixi, set `BWOPENCHAMBER_NODE` to its + path: `BWOPENCHAMBER_NODE=$(pixi exec -s nodejs=24 -- sh -c 'command -v node')`. +- **OpenChamber** in `/opt/nsls2/openchamber` (`node_modules/@openchamber/web`), + mounted read-only. `BWOPENCHAMBER_DIR` names another install. The ansible base + role does not install it yet. +- **OpenCode 2** in `/opt/nsls2/opencode2` and an OpenCode config, as for + `bwopencode2`. +- A private network backend (`pasta` or `slirp4netns`), or `--share-net`: see + [Network](#network). + +### Reaching it + +- **Port and address.** The port comes from the same pool as `bwopencode2 + --serve` (24700-24709, `BWOPENCODE2_SERVE_PORTS=LO-HI`), claimed in the same + state directory, so a port is never given to both and **one ssh_config block + covers both**: `bwopenchamber --ssh-config` prints the same block as + `bwopencode2 --serve-ssh-config`. `--port N` asks for one port. It is published + like `--publish`, always on your first publish address `127.H1.H2.1`, and the + launcher prints the `ssh -L` command and the URL. +- **Private network.** The server listens on `0.0.0.0` inside the sandbox's + private network, where only the published port reaches it. **The published + address is not access control**: any local user can connect to any `127/8` + address (see [Opening ports](#opening-ports)), so another user of the host can + reach this port, and the UI password is what keeps them out. OpenCode's own + port (a random one on `127.0.0.1` inside the sandbox) is not published and the + host cannot connect to it (checked). With `--share-net` the server binds your + publish address on the host network instead, and OpenCode's port is then on the + host's loopback too, behind its own random password; use it only where neither + backend works. +- **Password.** Random for each session, printed once; `BWOPENCODE2_SERVE_PASSWORD` + (12 characters or more) sets a fixed one for every session that uses it. It + reaches the server as `OPENCHAMBER_UI_PASSWORD` in the sandbox environment, + never on a command line. +- **Sign-in key.** The browser sign-in cookies are signed with a key that is + random for each session (`OPENCODE_JWT_SECRET`), so a sign-in ends with the + session that issued it. Without it OpenChamber 2.7.0 writes a key to + `jwt-secret` in its data directory and reuses it: a browser that signed in once + stayed signed in across restarts and after the password changed (12 hours, or 7 + days on a trusted device), and the agent could read the file. Checked: with a + new key a cookie is refused after a restart (401), signing in again works, and + no `jwt-secret` file is written. Reload the page after a restart and it asks + for the password. The key is still in the environment of the agent's shell + (checked: `printenv | grep -c OPENCODE_JWT_SECRET` prints 1), so an agent can + still sign a cookie for the current session; the change limits that to the + session instead of leaving a reusable key on disk (see the known limit below). +- **Preview.** A dev server started by the agent is not reachable from your + browser by itself. Publish its port with `--publish PORT` (also on your + `127.H1.H2.1`) and add a forward for it. +- **No arguments.** The server is always `serve --foreground`, on the published + address. `--host`, `--lan`, `tunnel`, `startup`, `connect-url` and every other + OpenChamber argument are refused. `--exec CMD` runs `CMD` in the same sandbox + (a shell for diagnostics) without a server, password or port. + +### Policy: what is turned off + +- **The policy is a read-only file the launcher generates**, mounted from a + descriptor at `/etc/openchamber/policy.json` inside the sandbox (the host's + file is not visible there and plays no part). It sets enterprise mode (no + tunnels (Cloudflare, ngrok, Tailscale), no public relay, no classification + service (Jev), no adding model providers in the app, no outside integrations + catalogue, no extensions that send data off the machine, generic + push-notification titles), `allowNetworkAccess: true` (the server listens on the + private network's interface; enterprise mode refuses that by default), the + installed OpenCode 2 as `opencodeBinary`, and hides the four built-in + third-party skills catalogues. It holds no secret. `OPENCHAMBER_ENTERPRISE_MODE=1` + and `OPENCHAMBER_PUSH_RELAY_DISABLED=true` are also set, so that if the file were + missing the server would fail closed (enterprise mode refuses a network address + without `allowNetworkAccess`) and not run unpinned. +- **Why a file and not environment variables.** Without a policy file, a saved + `opencodeBinary` in OpenChamber's `settings.json` overrides `OPENCODE_BINARY`. + That file is in the persistent data directory, which the agent can write, so the + agent could name another executable and OpenChamber would start it as + "OpenCode" with OpenChamber's environment, bypassing the pin and the database + guard's identity check. Only a policy file outranks the saved setting. Copilot + review found this; checked with 2.7.0: with the environment alone a fake + OpenCode named in `settings.json` ran, and with the read-only policy file + OpenChamber started the installed OpenCode and the fake never ran. The first + version of this launcher passed the pin as `OPENCODE_BINARY` only and said it was + pinned; that was wrong. +- **The host's own policy** (`/etc/openchamber/policy.json` deployed by ansible) + governs an OpenChamber run outside `bwopenchamber`; the launcher's file takes its + place inside the sandbox. +- **Host variables.** The launcher never passes `OPENCODE_HOST`, `OPENCODE_PORT`, + `OPENCODE_SKIP_START`, a server password or any other host `OPENCODE_*` or + `OPENCHAMBER_*` variable: the sandbox environment starts clean, and + `AIFAPIM_*`, the allowlisted `OPENCODE_*` switches and, with + `--github-tokens`, the `GH_TOKEN_*` variables are added as for `bwopencode2`. +- **Which model providers may be used** is decided by OpenCode, not + OpenChamber (the managed `/etc/opencode` config is mounted as for + `bwopencode2`). + +### Data and mounts + +- Everything `bwopencode2` mounts (config read-only, OpenCode data and cache, + authoring directories, git, pixi, npm, N2C tokens with `--n2c-token`), plus the + OpenChamber install read-only and OpenChamber's own data directory. +- **OpenChamber's data** (settings, projects, chats, worktrees, logs) is + `~/.local/share/bwopenchamber` on the host, mounted at + `~/.local/share/openchamber` in the sandbox: apart from any OpenChamber run + outside a sandbox, which is never mounted. It is bound whole and read-write, + so if it is a symlink the launcher resolves it and refuses a target that + contains a blocked path (for example a directory holding `~/.config/gh`), and + mounts the resolved path (found in Copilot's review of PR #43; the same check + as for the install directory). +- **`run/` is an empty tmpfs.** OpenChamber writes its pid file and an instance + file there, and the instance file contains the UI password in plain text (checked + with 2.7.0), so it must not reach the persistent directory. +- **No sign-in key is kept.** `jwt-secret` is not written (see Sign-in key). A + file left by an earlier run stays in the data directory but is not used. +- **Projects.** Only the directory you start in (its git root) is mounted + read-write, so a project that OpenChamber opens elsewhere needs `--rw-path`. + Starting in a parent directory that is not itself a git repository, such as + `~/Projects`, gives it all the repositories below. +- **Do not sign in to GitHub or add SSH keys in OpenChamber.** Whatever it + stores is readable by the agent, and it can merge pull requests and push. + Use `--github-tokens` for read-only access. The agent is told this in the + sandbox context. + +### Connecting from another machine + +Forward the port (`ssh -L`, or the alias from `--ssh-config`) and open +`http://127.0.0.1:PORT/` in a browser or the installed PWA; sign in with the +password. Keep the same port number at both ends, since the login cookie is +named by it. + +### Known limit: the agent can read its own controls + +OpenChamber puts its credentials into the environment of the OpenCode it starts, +and so of the agent's shell and the browser terminal (its own code records them +in `OPENCHAMBER_INJECTED_ENV`). Checked with 2.7.0: an agent session's shell has +`OPENCHAMBER_UI_PASSWORD`, `OPENCODE_SERVER_PASSWORD`, `OPENCODE_PASSWORD` and +`OPENCODE_JWT_SECRET` set. With them the agent can sign in to OpenChamber (or +sign a cookie itself), or call its own OpenCode server's API, and so change +settings, start sessions or scheduled tasks and answer its own permission +prompts. Nothing leaves the sandbox: everything it could drive runs +inside it, and the sandbox boundary is what was checked below. + +- **The launcher cannot remove them.** OpenChamber writes them into its own + environment. Hiding them from the agent's shell only would not help either: a + process of the same user in the sandbox can read the server's environment from + `/proc//environ` (checked in a `bwopencode2` sandbox). +- **The agent is told in the sandbox context** not to read, print or use them. + That is an instruction, not a control. +- **`bwopencode2 --serve` has the same property** with `OPENCODE_PASSWORD`, which + its section lists as unchecked. +- Where the agent's permission rules ask before risky actions, the agent holding + these credentials can answer its own prompts through the API, so those prompts + are not a barrier against a misbehaving agent here. + +### What was checked + +**A real launch on a workstation** (`bwopenchamber --port 24700`, pasta, OpenCode +2.2.7, OpenChamber 2.7.0): + +- The host has one new listener, `127.52.255.1:24700`, and nothing on 3000 or on + OpenCode's port. Without a password the API gives 401, the same port on + `127.0.0.1` is refused, and OpenCode's own port is refused from the host. +- A browser signed in through an `ssh -L` tunnel, and an agent session worked + through the model gateway. +- OpenChamber's browser terminal runs inside the sandbox: `BWRAP_SANDBOX` is + `bwopenchamber`, `~/.ssh` does not exist, `ssh` is masked and + `BWRAP_NET_BACKEND` is `pasta`. +- After Ctrl-C the OpenCode that OpenChamber started is gone. +- The database guard refused to start next to an older OpenCode build, as + designed. + +**In a private network namespace without the launcher**, OpenChamber 2.7.0 with +the launcher's environment: the server answers on `0.0.0.0`, 401 and 200 as +above, the managed OpenCode receives `OPENCODE_CONFIG_CONTENT` (the sandbox +context), `run/` holds the UI password in plain text, and the sign-in key behaves +as described under Reaching it. With a `settings.json` naming a fake OpenCode, +the environment alone let the fake run; with a read-only policy file at +`/etc/openchamber/policy.json` pinning the installed OpenCode the fake never ran, +and the server still listened on `0.0.0.0` using `allowNetworkAccess` from the +file. + +**Tests:** `tests/test-bwopenchamber.sh` (104 checks through `--dry-run`: argument +refusals, the environment and mounts built, the generated policy file, hostile +host variables dropped, password and sign-in key handling, Node and install +checks, and that a port held by another session does not change the result: the +harness stubs the port probe, which Copilot found made the tests depend on running +sessions). + +**Not checked:** git and worktrees from OpenChamber, whether the host listener +belongs to pasta or to the network log's Python helper (`ss` names +`platform-python`), and the shared-library move that is left for after #37. + +To repeat the host checks (do not capture the banner, it prints the password): + +```text +bwopenchamber --port 24700 # in a project directory +ss -ltnp # on the host: only 127.H1.H2.1:24700 +curl -s -o /dev/null -w '%{http_code}\n' http://127.H1.H2.1:24700/auth/session # 401 +# in the browser through the tunnel: sign in, open the terminal and run +# echo $BWRAP_SANDBOX; ls ~/.ssh; ssh -V # bwopenchamber; fails; fails +# Ctrl-C, then: pgrep -af 'opencode.exe serve' # none of this session's +``` + ## `bwclaude` Launches the [Claude CLI](https://docs.claude.com/en/docs/claude-code/overview) inside a bubblewrap sandbox. @@ -723,7 +1709,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 +1720,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..2552f30 --- /dev/null +++ b/lib/bwrap_netlog.py @@ -0,0 +1,748 @@ +#!/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 ctypes +import ipaddress +from concurrent.futures import ThreadPoolExecutor +import os +import re +import signal +import socket +import struct +import sys +import threading + +# ── 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"])) + if f["tdst"][0] != f["dst"][0] and f["tdst"][0] not in ("0.0.0.0", "::"): + parts.append("via=%s" % _hostport(*f["tdst"])) + name = self.names.get(f["dst"][0]) + if name and direction == "out": + parts.append("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 lines from a FIFO without ever blocking its writer. + + The FIFO is opened read-only and non-blocking: a read-only reader sees + EOF whenever no writer has it open, which happens before the launcher + opens its stderr on it and, briefly, if pasta is ever without the fd. + So EOF is not the end: `lines()` keeps polling until `done()` says the + session is over, and then drains what is left. The launcher opens its + end only after this reader exists (it is told, see `ready`), so its + write-only open does not block. + """ + + def __init__(self, path, done): + import select + + self.fd = os.open(path, os.O_RDONLY | os.O_NONBLOCK) + self.done = done + self.poll = select.poll() + self.poll.register(self.fd, select.POLLIN) + self.buf = b"" + + def _read(self, timeout_ms): + """Return the bytes available within TIMEOUT_MS; b"" when none. + Waits out TIMEOUT_MS even while the FIFO reports hang-up (no writer), + which poll() would otherwise report at once, over and over.""" + import time + + deadline = time.monotonic() + timeout_ms / 1000.0 + while True: + left = deadline - time.monotonic() + if left <= 0: + return b"" + if self.poll.poll(int(left * 1000)): + try: + data = os.read(self.fd, 65536) + except (BlockingIOError, InterruptedError): + data = b"" + if data: + return data + time.sleep(min(0.05, max(left, 0))) # hang-up: no writer yet, or none left + + def lines(self, poll_ms=1000): + """Yield complete lines (without the newline) until the session is + over and the FIFO is drained.""" + while True: + data = self._read(poll_ms) + if data: + self.buf += data + while b"\n" in self.buf: + line, _, self.buf = self.buf.partition(b"\n") + yield line.decode("utf-8", "replace") + continue + if self.done(): + break + # The writers are gone; whatever they left is read at once. + while True: + data = self._read(200) + if not data: + break + self.buf += data + for line in self.buf.split(b"\n"): + if line: + yield line.decode("utf-8", "replace") + self.buf = b"" + os.close(self.fd) + + +def _bridge_stderr(path, stop): + """Copy PATH (a FIFO the relay opens as the sandbox's stderr when the + user's is a pipe) to our stderr until STOP is set and it is drained.""" + reader = FifoReader(path, stop.is_set) + for line in reader.lines(): + try: + sys.stderr.write(line + "\n") + sys.stderr.flush() + except (OSError, ValueError): + pass + + +def condense(argv): + """Condense pasta's debug log into the system log. + + Run by the launcher in the background, before it opens its stderr on the + FIFO and execs pasta. It reads the FIFO itself, writes one record per + connection to CMD (systemd-cat or logger), writes the end record once the + launcher (then pasta) has exited, and removes the session directory. + The relay prints MARKER on pasta's stderr when the sandbox is about to + start; pasta's lines before it are its setup and, if it fails, its error: + they go to the terminal (this process's stderr) when the launcher exits + without the marker ever arriving. + """ + import subprocess + + session, 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 + + sink = None + if cmd: + sink = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + out = sink.stdin if sink else sys.stdout.buffer + state = {"dead": False} + + lead = record_prefix(session, user) + + def emit(text, prefix=True): + if state["dead"]: + return + if prefix: + text = lead + text + try: + out.write((text + "\n").encode("utf-8", "replace")) + out.flush() + except (BrokenPipeError, OSError): + # The log tool is gone: keep reading, so pasta never blocks. + state["dead"] = True + + c = Condenser(emit, dns_forward) + + 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 end_record: + emit(end_record, prefix=False) + if sink: + try: + sink.stdin.close() + except OSError: + pass + sink.wait() + if session_dir: + import shutil + + shutil.rmtree(session_dir, ignore_errors=True) + + +# ── dns relay ─────────────────────────────────────────────────────────── + +_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]) + # DNS servers may close after one response. A fresh + # connection per query also avoids replaying a request + # after an ambiguous upstream failure. + with socket.create_connection(self.upstream, timeout=5) as up: + up.settimeout(10) + up.sendall(hdr + query) + rhdr = self._recv_exact(up, 2) + response = self._recv_exact(up, struct.unpack("!H", rhdr)[0]) + self.log(query, response, "tcp") + conn.sendall(rhdr + response) + except OSError: + break + + 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..40f657b 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,1879 @@ 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 it goes on reading and drops the +# lines. +# - pasta's startup lines would otherwise print on the terminal; the relay +# prints a marker on the FIFO and then reopens the user's stderr by path +# (a terminal or a file), or through a second FIFO that the supervisor +# copies to it (a pipe). If pasta exits before the marker (a failed +# start), the 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 +4577,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 +4913,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 +4945,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 +5101,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 +5248,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 +5278,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 +5382,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 +5393,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 +5431,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 +5722,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 +5752,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 +5825,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 +5846,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/lib/opencode2-db-lock.sh b/lib/opencode2-db-lock.sh index c4a20f7..caebf6d 100644 --- a/lib/opencode2-db-lock.sh +++ b/lib/opencode2-db-lock.sh @@ -391,9 +391,16 @@ oc2db_guard() { # Take the session lock last. Shared: any number of sessions of this # version on this host may hold it together. - if ! { exec {OC2DB_SESSION_FD}<> "${_locks}/${_own}"; } 2> /dev/null || + # The lock descriptor is a high number (200 up), not bash's {var} choice, which + # starts at 10: bwrap_sandbox_lib.sh hands the sandbox its files and data on + # descriptors 10 up and its relay opens those over whatever is there, closing + # the lock (and so releasing it) while the sandbox runs. + oc2db_free_fd 200 249 + if [[ -z "${OC2DB_SESSION_FD}" ]] || + ! { eval "exec ${OC2DB_SESSION_FD}<> \"\${_locks}/\${_own}\""; } 2> /dev/null || ! flock -s -n "${OC2DB_SESSION_FD}" 2> /dev/null; then oc2db_warn "cannot take the session lock ${_locks}/${_own}; continuing without it" + [[ -z "${OC2DB_SESSION_FD}" ]] || eval "exec ${OC2DB_SESSION_FD}>&-" 2> /dev/null OC2DB_SESSION_FD="" elif [[ -n "${_bin_id}" ]]; then printf '%s\n' "${_bin_id}" 1>&"${OC2DB_SESSION_FD}" 2> /dev/null || true @@ -405,6 +412,20 @@ oc2db_guard() { return 0 } +# oc2db_free_fd FIRST LAST: set OC2DB_SESSION_FD to the first descriptor number +# in FIRST..LAST that is not open, or to "" when there is none. +oc2db_free_fd() { + local _n + OC2DB_SESSION_FD="" + for ((_n = $1; _n <= $2; _n++)); do + if ! { true <&"${_n}"; } 2> /dev/null; then + OC2DB_SESSION_FD="${_n}" + return 0 + fi + done + return 1 +} + # Start the daily backup in the background. The job closes the session lock # descriptor first, so it never keeps the session lock alive after OpenCode # exits; while it reads the database it holds its own backup lock @@ -423,7 +444,7 @@ oc2db_backup_if_due() { command -v ionice > /dev/null 2>&1 && _run=(ionice -c 3 nice -n 19) command -v setsid > /dev/null 2>&1 && _run=(setsid "${_run[@]}") { - [[ -n "${OC2DB_SESSION_FD}" ]] && exec {OC2DB_SESSION_FD}>&- + [[ -n "${OC2DB_SESSION_FD}" ]] && eval "exec ${OC2DB_SESSION_FD}>&-" exec "${_run[@]}" "${_tool}" --data-dir "${_data}" \ backup --if-due --delay "${OC2DB_BACKUP_DELAY:-30}" --keep "${_keep}" \ < /dev/null > "${_gdir}/backup.log" 2>&1 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/bwopenchamber-harness b/tests/bwopenchamber-harness new file mode 100755 index 0000000..39e6ec2 --- /dev/null +++ b/tests/bwopenchamber-harness @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# Run bwopenchamber's main() with one change for tests: resolve_bwrap_bin accepts +# the bwrap of the development sandbox, which is not owned by root there. The +# check itself is the library's and is not weakened for real runs. +# +# tests/bwopenchamber-harness [bwopenchamber options] +set -euo pipefail +here="$(dirname "$(readlink -f "$0")")" +# shellcheck source=../bin/bwopenchamber +source "${here}/../bin/bwopenchamber" +resolve_bwrap_bin() { + echo /usr/bin/bwrap +} +# The port pool is probed with real binds on the user's publish address, which +# depends on the uid and not on the temporary HOME. A real bwopenchamber or +# bwopencode2 --serve session holding 24700 would make every dry run pick 24701 +# and fail the tests that expect the first port, or fail outright with a full +# pool. Stub the probe so the tests do not depend on running sessions. +serve_port_is_free() { + return 0 +} +# A stub OpenCode 2 binary, so the tests need no installation. +# shellcheck disable=SC2034 # read by bwopencode2's functions +[[ -z "${HARNESS_OC2_BIN:-}" ]] || OC2_INSTALL_BIN="${HARNESS_OC2_BIN}" +main "$@" diff --git a/tests/bwopencode2-harness b/tests/bwopencode2-harness new file mode 100755 index 0000000..70a0578 --- /dev/null +++ b/tests/bwopencode2-harness @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# Run bwopencode2's main() with the changes tests need, so --serve and +# --serve-open can be dry-run without an installation: +# - resolve_bwrap_bin accepts the bwrap of the development sandbox, which is +# not owned by root there (the library's check is not weakened for real runs); +# - serve_port_is_free does not probe real sockets, so a running session +# holding 24700 does not change the port the tests expect; +# - the OpenCode 2 binary is a stub (HARNESS_OC2_BIN). +# +# tests/bwopencode2-harness [bwopencode2 options] +set -euo pipefail +here="$(dirname "$(readlink -f "$0")")" +# shellcheck source=../bin/bwopencode2 +source "${here}/../bin/bwopencode2" +resolve_bwrap_bin() { + echo /usr/bin/bwrap +} +serve_port_is_free() { + return 0 +} +# shellcheck disable=SC2034 # read by bwopencode2's functions +[[ -z "${HARNESS_OC2_BIN:-}" ]] || OC2_INSTALL_BIN="${HARNESS_OC2_BIN}" +# The browsers --serve-open looks for, so a test does not depend on the host's +# (HARNESS_SERVE_OPEN_CANDIDATES set to nothing means none). +# shellcheck disable=SC2034 # read by bwopencode2's functions +if [[ -n "${HARNESS_SERVE_OPEN_CANDIDATES+x}" ]]; then + read -r -a SERVE_OPEN_CANDIDATES <<<"${HARNESS_SERVE_OPEN_CANDIDATES}" +fi +main "$@" 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..3415042 --- /dev/null +++ b/tests/fake-net-run @@ -0,0 +1,59 @@ +#!/bin/bash +# Run a bw* launcher against the fake network backends in tests/fakes. +# +# tests/fake-net-run [--backend pasta|slirp4netns] LAUNCHER [ARGS...] +# +# An outer bwrap supplies what the development sandbox lacks: a character +# device at /dev/net/tun (/dev/null), and plain-file copies of /etc/resolv.conf +# and /etc/hosts (a file that is itself a bwrap data mount cannot be bound +# again). The real pasta is hidden; --backend slirp4netns leaves the fake +# pasta off PATH too. The fakes read failure modes from $FAKE_NET_DIR. +# FAKE_NET_LOG=journal (default), syslog, both or none puts a socket file at +# /run/systemd/journal/stdout, /dev/log, both or neither (so the launcher +# picks the fake systemd-cat or logger, which write to +# $FAKE_NET_DIR/journal.log). +set -euo pipefail +here="$(dirname "$(readlink -f "$0")")" +backend=pasta +if [[ "${1:-}" == --backend ]]; then + backend="$2" + shift 2 +fi +export FAKE_NET_DIR="${FAKE_NET_DIR:-/tmp/opencode/fakenet}" +mkdir -p "${FAKE_NET_DIR}/etc" +cat /etc/resolv.conf >"${FAKE_NET_DIR}/etc/resolv.conf" +cat /etc/hosts >"${FAKE_NET_DIR}/etc/hosts" +# A fresh /etc: the plain copies, plus every other entry bound as it is. +etc=(--tmpfs /etc) +for e in /etc/* /etc/.[!.]*; do + [[ -e "${e}" || -L "${e}" ]] || continue + case "${e}" in + /etc/resolv.conf | /etc/hosts) ;; + *) etc+=(--ro-bind "${e}" "${e}") ;; + esac +done +etc+=(--ro-bind "${FAKE_NET_DIR}/etc/resolv.conf" /etc/resolv.conf) +etc+=(--ro-bind "${FAKE_NET_DIR}/etc/hosts" /etc/hosts) +logsock=() +mksock() { + rm -f "$1" + python3 -c 'import socket, sys; socket.socket(socket.AF_UNIX).bind(sys.argv[1])' "$1" +} +case "${FAKE_NET_LOG:-journal}" in +journal | both) + mksock "${FAKE_NET_DIR}/journal.sock" + logsock+=(--dir /run/systemd/journal --bind "${FAKE_NET_DIR}/journal.sock" /run/systemd/journal/stdout) + ;;& +syslog | both) + mksock "${FAKE_NET_DIR}/devlog.sock" + logsock+=(--bind "${FAKE_NET_DIR}/devlog.sock" /dev/log) + ;; +esac +path="${here}/fakes/bin:${PATH}" +[[ "${backend}" != pasta ]] || path="${here}/fakes/pasta-bin:${path}" +exec bwrap --dev-bind / / --dev /dev --dev-bind /dev/null /dev/net/tun \ + --ro-bind /dev/null /usr/bin/passt \ + "${etc[@]}" \ + ${logsock[@]+"${logsock[@]}"} \ + --chdir "${PWD}" \ + -- env PATH="${path}" "$@" diff --git a/tests/fakes/_nsproxy.py b/tests/fakes/_nsproxy.py new file mode 100644 index 0000000..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-bwopenchamber.sh b/tests/test-bwopenchamber.sh new file mode 100755 index 0000000..5ae87aa --- /dev/null +++ b/tests/test-bwopenchamber.sh @@ -0,0 +1,323 @@ +#!/usr/bin/env bash +# Tests for bin/bwopenchamber (argument handling, the environment and mounts it +# builds), through --dry-run. +# +# Needs bash and bwrap. Uses a temporary HOME, a stub node, a stub OpenCode 2 +# binary and a stub OpenChamber install, so it touches no real data and needs +# none of them installed. --share-net keeps it independent of pasta and +# slirp4netns. tests/bwopenchamber-harness accepts the development machine's +# bwrap; the library's owner check is not weakened for real runs. +# Run on any host: tests/test-bwopenchamber.sh +# +# What it cannot show: a real launch (the sandbox, the private network, the +# server answering), which needs a host where bwrap is root-owned. See +# docs/bw-wrappers.md ("bwopenchamber", manual check). + +# shellcheck disable=SC2016 # patterns are single quoted on purpose +set -uo pipefail + +ROOT="$(CDPATH='' cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +HARNESS="${ROOT}/tests/bwopenchamber-harness" +T="$(mktemp -d "${TMPDIR:-/tmp}/bwopenchamber-test.XXXXXX")" +trap 'rm -rf "${T}"' EXIT + +PASS=0 +FAIL=0 +check() { # check DESCRIPTION CONDITION-COMMAND... + local _desc="$1" + shift + if "$@"; then + PASS=$((PASS + 1)) + printf 'ok %s\n' "${_desc}" + else + FAIL=$((FAIL + 1)) + printf 'FAIL %s\n' "${_desc}" + fi +} +contains() { grep -qF -- "$2" <<< "$1"; } +lacks() { ! grep -qF -- "$2" <<< "$1"; } +rc_is() { [[ "${RC}" -eq "$1" ]]; } +matches() { [[ "$1" =~ $2 ]]; } +# Number of lines of --dry-run output that set environment variable $2 (a value is +# printed REDACTED when the name holds PASSWORD or SECRET, so count the names). +env_lines() { grep -c "^ $2=" <<< "$1"; } + +# ── fixtures ──────────────────────────────────────────────────────────── + +mkdir -p "${T}/home/.config/opencode2/opencode" "${T}/work" "${T}/oc2" +echo '{}' > "${T}/home/.config/opencode2/opencode/opencode.json" +mkdir -p "${T}/app/node_modules/@openchamber/web/bin" +echo '// stub' > "${T}/app/node_modules/@openchamber/web/bin/cli.js" +printf '#!/bin/sh\necho "opencode v2.2.7"\n' > "${T}/oc2/opencode.exe" +chmod +x "${T}/oc2/opencode.exe" +make_node() { # make_node DIR VERSION + mkdir -p "$1" + printf '#!/bin/sh\necho "v%s"\n' "$2" > "$1/node" + chmod +x "$1/node" +} +make_node "${T}/node/24.14.0" 24.14.0 +make_node "${T}/node/24.13.9" 24.13.9 +make_node "${T}/node/22.23.2" 22.23.2 +make_node "${T}/node/25.1.0" 25.1.0 +NODE="${T}/node/24.14.0" +make_node_as() { # make_node_as DIR NAME VERSION: a stub Node under another name, as nodejs24 installs node-24 + mkdir -p "$1" + printf '#!/bin/sh\necho "v%s"\n' "$3" > "$1/$2" + chmod +x "$1/$2" +} +mkdir -p "${T}/no-nodes" +make_node_as "${T}/nodebin24" node-24 24.14.0 +make_node_as "${T}/nodebin" node-24 24.14.0 +make_node_as "${T}/nodebin" node-25 25.1.0 +make_node_as "${T}/nodebin" nodejs22 22.23.2 +mkdir -p "${T}/nodebin" +printf '#!/bin/sh\necho hello\n' > "${T}/nodebin/node-99" +printf '#!/bin/sh\necho "v26.0.0"\n' > "${T}/nodebin/node-26" +chmod +x "${T}/nodebin/node-99" +chmod -x "${T}/nodebin/node-26" +make_node_as "${T}/nodebin22" nodejs22 22.23.2 +printf '#!/bin/sh\necho hello\n' > "${T}/nodebin22/node-99" +chmod +x "${T}/nodebin22/node-99" + +# run [env VAR=VALUE ...] -- ARGS...: dry-run the launcher; sets OUT, ERR, RC. +# Host variables of the kinds the launcher must not pass on are cleared first. +run() { + local -a _env=() + while [[ $# -gt 0 && "$1" != -- ]]; do + _env+=("$1") + shift + done + shift + OUT="$(cd "${T}/work" && env -u OPENCODE_HOST -u OPENCODE_PORT -u OPENCODE_SKIP_START \ + -u OPENCODE_SERVER_PASSWORD -u OPENCODE_PASSWORD -u OPENCHAMBER_HOST \ + -u OPENCHAMBER_UI_PASSWORD -u OPENCHAMBER_RELAY_URL -u OPENCODE2_DB_GUARD \ + -u BWOPENCODE2_SERVE_PASSWORD -u BWOPENCODE2_SERVE_PORTS \ + -u XDG_CONFIG_HOME -u XDG_DATA_HOME -u XDG_CACHE_HOME -u BWOPENCHAMBER_NODE \ + HOME="${T}/home" PATH="${NODE}:${PATH}" BWOPENCHAMBER_DIR="${T}/app" \ + BWOPENCHAMBER_NODE_DIRS="${T}/no-nodes" \ + HARNESS_OC2_BIN="${T}/oc2/opencode.exe" ${_env[@]+"${_env[@]}"} \ + "${HARNESS}" --dry-run --share-net "$@" 2> "${T}/err")" + RC=$? + ERR="$(cat "${T}/err")" +} + +# ── the normal launch ──────────────────────────────────────────────────── + +run -- +check 'a plain launch succeeds' rc_is 0 +check 'runs the server in the foreground' contains "${OUT}" 'cli.js serve --foreground --host ' +check 'takes the first port of the shared pool' contains "${OUT}" '--port 24700' +check 'runs the node that was found on PATH' contains "${OUT}" "-- ${NODE}/node " +check 'runs the stub install, not a host path' contains "${OUT}" "${T}/app/node_modules/@openchamber/web/bin/cli.js" +check 'mounts the install read-only' contains "${OUT}" "--ro-bind ${T}/app ${T}/app" +check 'enterprise mode is on' contains "${OUT}" 'OPENCHAMBER_ENTERPRISE_MODE=1' +check 'network access is not granted through the environment (the policy file does it)' [ "$(env_lines "${OUT}" OPENCHAMBER_ALLOW_NETWORK_ACCESS)" -eq 0 ] +check 'the push relay is off' contains "${OUT}" 'OPENCHAMBER_PUSH_RELAY_DISABLED=true' +check 'OpenCode is the installed binary' contains "${OUT}" "OPENCODE_BINARY=${T}/oc2/opencode.exe" +check 'the UI password is set and hidden' contains "${OUT}" "OPENCHAMBER_UI_PASSWORD='REDACTED'" +check 'a sign-in signing key is set and hidden' contains "${OUT}" "OPENCODE_JWT_SECRET='REDACTED'" +check 'the agent is told not to use the credentials it inherits' contains "${OUT}" 'never read, print or use them' +check 'a tmpfs covers the run directory (UI password file)' contains "${OUT}" "${T}/home/.local/share/openchamber/run" +check 'the data directory is the launcher own, not an OpenChamber outside a sandbox' \ + contains "${OUT}" "--bind ${T}/home/.local/share/bwopenchamber ${T}/home/.local/share/openchamber" +check 'the policy file is mounted read-only from a descriptor' contains "${OUT}" '--ro-bind-data' +check '... at the path OpenChamber reads' contains "${OUT}" '/etc/openchamber/policy.json' +check 'no host policy file is bound' lacks "${OUT}" '--ro-bind /etc/openchamber' +# The generated file is printed after the command as '# ' lines (--dry-run). +policy_text="$(sed -n '/filled by the relay with the OpenChamber policy file/,/^$/p' <<< "${OUT}" | sed -n 's/^# //p' | grep -E '^ *[{}"]')" +check 'the policy file is valid JSON' python3 -c 'import json,sys; json.loads(sys.argv[1])' "${policy_text}" +check 'the policy turns enterprise mode on' contains "${policy_text}" '"enterpriseMode": true' +check 'the policy allows the private network address (a file, so it governs)' contains "${policy_text}" '"allowNetworkAccess": true' +check 'the policy pins the installed OpenCode, which a saved setting then cannot override' contains "${policy_text}" "\"opencodeBinary\": \"${T}/oc2/opencode.exe\"" +check 'the policy allows no extension' contains "${policy_text}" '"allowedExtensions": []' +check 'the policy hides the built-in skills catalogues' contains "${policy_text}" '"hideBuiltinSkillCatalogs": true' +check 'the policy holds no secret' lacks "${policy_text}" 'PASSWORD' +check 'the printed info names the ssh tunnel' contains "${ERR}" 'ssh -L 24700:127.' +check 'the sandbox is named' contains "${OUT}" 'BWRAP_SANDBOX=bwopenchamber' +check 'the agent is told what it runs under' contains "${OUT}" 'This session runs under OpenChamber' + +# ── the port pool does not depend on running sessions ──────────────────── + +# Hold the first port on this user's publish address, as a real bwopenchamber or +# bwopencode2 --serve session would, then dry-run again: the stubbed probe means +# the answer does not change (without it the dry run would pick the next port). +run -- --ssh-config +busy_addr="$(sed -n 's/.*LocalForward 24700 \(127\.[0-9.]*\):24700.*/\1/p' <<< "${OUT}")" +python3 -c 'import socket, sys, time +s = socket.socket() +s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) +s.bind((sys.argv[1], 24700)) +s.listen() +time.sleep(40)' "${busy_addr}" & +busy_pid=$! +sleep 1 +check 'the test holds the first port on the publish address' matches "${busy_addr}" '^127\.[0-9]+\.[0-9]+\.1$' +run -- +check 'a port held by another session does not change the port the tests expect' contains "${OUT}" '--port 24700' +kill "${busy_pid}" 2> /dev/null +wait "${busy_pid}" 2> /dev/null + +# ── the data directory is bound whole, read-write ─────────────────────── + +# A symlink as the data directory must not lead to a directory that contains a +# blocked path (here ~/.config/gh): the whole tree would be bound read-write. +mkdir -p "${T}/home/.config/gh" "${T}/elsewhere/data" "${T}/home/.local/share" +echo x > "${T}/home/.config/gh/hosts.yml" +ln -s "${T}/home/.config" "${T}/home/.local/share/bwopenchamber" +run -- +check 'a data directory that is a symlink to one containing .config/gh is refused' rc_is 1 +check '... and the blocked path is named' contains "${ERR}" 'contains the blocked path' +check '... and nothing was mounted' lacks "${OUT}" '/share/openchamber' +rm "${T}/home/.local/share/bwopenchamber" + +ln -s "${T}/elsewhere/data" "${T}/home/.local/share/bwopenchamber" +run -- +check 'a data directory that is a symlink to a harmless directory works' rc_is 0 +check '... and is mounted by its resolved path' contains "${OUT}" "--bind ${T}/elsewhere/data ${T}/home/.local/share/openchamber" +rm "${T}/home/.local/share/bwopenchamber" +rmdir "${T}/home/.local/share" 2> /dev/null || true + +run -- +check 'a plain data directory still works' rc_is 0 +check '... and is mounted as before' contains "${OUT}" "--bind ${T}/home/.local/share/bwopenchamber ${T}/home/.local/share/openchamber" + +# ── arguments ──────────────────────────────────────────────────────────── + +for _args in '--host 0.0.0.0' '--lan' 'tunnel start' 'startup enable' 'connect-url --relay' '--ui-password x'; do + # shellcheck disable=SC2086 # word splitting of the argument list is the point + run -- ${_args} + check "refuses the OpenChamber argument '${_args}'" rc_is 1 + check " ... and says why ('${_args}')" contains "${ERR}" 'takes no OpenChamber arguments' +done + +run -- --port 24705 +check '--port inside the range is used' contains "${OUT}" '--port 24705' +run -- --port 3000 +check '--port outside the range is refused' rc_is 1 +run -- --port abc +check '--port that is not a number is refused' rc_is 1 +run -- --port +check '--port without a value is refused' rc_is 1 + +run BWOPENCODE2_SERVE_PORTS=24800-24802 -- +check 'the port range can be changed' contains "${OUT}" '--port 24800' +run BWOPENCODE2_SERVE_PORTS=49370-49380 -- +check 'a range that holds the shared OpenCode service port is refused' rc_is 1 + +# ── what the host cannot inject ────────────────────────────────────────── + +run OPENCODE_HOST=http://127.0.0.1:49374 OPENCODE_PORT=49374 OPENCODE_SKIP_START=true \ + OPENCODE_SERVER_PASSWORD=hostsecret1 OPENCODE_PASSWORD=hostsecret2 OPENCODE_JWT_SECRET=hostsecret4 \ + OPENCHAMBER_HOST=0.0.0.0 OPENCHAMBER_UI_PASSWORD=hostsecret3 OPENCHAMBER_RELAY_URL=wss://x.example/ws -- +check 'launch with hostile host variables succeeds' rc_is 0 +check 'OPENCODE_HOST is not passed' lacks "${OUT}" 'OPENCODE_HOST=' +check 'OPENCODE_PORT is not passed' lacks "${OUT}" 'OPENCODE_PORT=' +check 'OPENCODE_SKIP_START is not passed' lacks "${OUT}" 'OPENCODE_SKIP_START' +# --dry-run prints a variable whose name holds PASSWORD or SECRET as REDACTED, so +# the values cannot show: check the names, and that ours is the only one of each. +check 'no host OPENCODE_SERVER_PASSWORD is passed' lacks "${OUT}" 'OPENCODE_SERVER_PASSWORD=' +check 'no host OPENCODE_PASSWORD is passed' lacks "${OUT}" 'OPENCODE_PASSWORD=' +check 'the UI password is ours alone, not the host one' [ "$(env_lines "${OUT}" OPENCHAMBER_UI_PASSWORD)" -eq 1 ] +check 'the signing key is ours alone, not the host one' [ "$(env_lines "${OUT}" OPENCODE_JWT_SECRET)" -eq 1 ] +check 'no host secret value is printed' lacks "${OUT}${ERR}" 'hostsecret' +check 'OPENCHAMBER_HOST is not passed' lacks "${OUT}" 'OPENCHAMBER_HOST=' +check 'no relay URL is passed' lacks "${OUT}" 'OPENCHAMBER_RELAY_URL' +check 'the dropped OPENCODE_* names are reported, not their values' contains "${ERR}" 'OPENCODE_HOST' + +# ── password ───────────────────────────────────────────────────────────── + +run BWOPENCODE2_SERVE_PASSWORD=short -- +check 'a fixed password under 12 characters is refused' rc_is 1 +run BWOPENCODE2_SERVE_PASSWORD=a-fixed-password-123 -- +check 'a fixed password is accepted' rc_is 0 +check '... and passed to the server as the UI password' contains "${OUT}" "OPENCHAMBER_UI_PASSWORD='REDACTED'" +check '... and never printed' lacks "${OUT}${ERR}" 'a-fixed-password-123' + +# ── the sign-in signing key ────────────────────────────────────────────── + +# och_make_jwt_secret in a subshell, so the launcher's options do not reach this script. +jwt_pair="$( + # shellcheck source=../bin/bwopenchamber + source "${ROOT}/bin/bwopenchamber" + och_make_jwt_secret + a="${OCH_JWT_SECRET}" + och_make_jwt_secret + printf '%s %s' "${a}" "${OCH_JWT_SECRET}" +)" +jwt_a="${jwt_pair% *}" +jwt_b="${jwt_pair#* }" +check 'the signing key is 64 hex characters' matches "${jwt_a}" '^[0-9a-f]{64}$' +check 'a new key is made each time' [ "${jwt_a}" != "${jwt_b}" ] + +# ── --exec ─────────────────────────────────────────────────────────────── + +run -- --exec 'bash -c true' +check '--exec runs the command, not the server' contains "${OUT}" '-- bash -c true' +check '--exec sets up no UI password' [ "$(env_lines "${OUT}" OPENCHAMBER_UI_PASSWORD)" -eq 0 ] +check '--exec sets up no signing key' [ "$(env_lines "${OUT}" OPENCODE_JWT_SECRET)" -eq 0 ] +check '--exec starts no server' lacks "${OUT}" 'serve --foreground' +run -- --exec 'bash -c true' --port 24701 +check '--exec with --port is refused' rc_is 1 + +# ── Node and installs ──────────────────────────────────────────────────── + +NODE="${T}/node/24.13.9" run -- +check 'Node 24.13 is refused' rc_is 1 +check '... and the version is named' contains "${ERR}" '24.13.9' +NODE="${T}/node/22.23.2" run -- +check 'Node 22 is refused' rc_is 1 +NODE="${T}/node/25.1.0" run -- +check 'Node 25 is accepted' rc_is 0 +NODE="${T}/node/24.14.0" + +# ── which Node is used ─────────────────────────────────────────────────── + +# A side-by-side node-24 is found when PATH's node is too old (RHEL 10: nodejs 22 is +# the default, nodejs24 installs node-24). +NODE="${T}/node/22.23.2" run BWOPENCHAMBER_NODE_DIRS="${T}/nodebin24" -- +check 'node 22 on PATH and a node-24 beside it: launches' rc_is 0 +check '... with the node-24' contains "${OUT}" "-- ${T}/nodebin24/node-24 " +NODE="${T}/node/22.23.2" run BWOPENCHAMBER_NODE_DIRS="${T}/nodebin" -- +check 'several versioned nodes: the newest that is new enough is used' contains "${OUT}" "-- ${T}/nodebin/node-25 " +check '... not one that is not executable' lacks "${OUT}" "${T}/nodebin/node-26" +NODE="${T}/node/24.14.0" run BWOPENCHAMBER_NODE_DIRS="${T}/nodebin" -- +check 'a new enough node on PATH wins over versioned ones' contains "${OUT}" "-- ${T}/node/24.14.0/node " +NODE="${T}/node/22.23.2" run BWOPENCHAMBER_NODE="${T}/node/25.1.0/node" -- +check 'BWOPENCHAMBER_NODE is used' contains "${OUT}" "-- ${T}/node/25.1.0/node " +NODE="${T}/node/24.14.0" run BWOPENCHAMBER_NODE="${T}/node/22.23.2/node" -- +check 'BWOPENCHAMBER_NODE that is too old is an error, even with a good node on PATH' rc_is 1 +check '... naming it and its version' contains "${ERR}" "${T}/node/22.23.2/node 22.23.2" +NODE="${T}/node/22.23.2" run BWOPENCHAMBER_NODE_DIRS="${T}/nodebin22" -- +check 'no node that is new enough: refused' rc_is 1 +check '... listing what was found with versions' contains "${ERR}" "${T}/node/22.23.2/node 22.23.2" +check '... and a binary that is not node' contains "${ERR}" "${T}/nodebin22/node-99 (no version)" +check '... and what to do on RHEL 10' contains "${ERR}" 'install nodejs24' +check '... and the override' contains "${ERR}" 'BWOPENCHAMBER_NODE=/path/to/node' +# With no node on PATH at all, the first versioned binary must not win just for being +# first: the newest one that is new enough does. +mkdir -p "${T}/nonode-bin" +for _f in /usr/bin/*; do + case "${_f##*/}" in node | node-* | nodejs*) ;; *) ln -sf "${_f}" "${T}/nonode-bin/${_f##*/}" ;; esac +done +NODE="${T}/no-nodes" run PATH="${T}/nonode-bin" BWOPENCHAMBER_NODE_DIRS="${T}/nodebin" -- +check 'no node on PATH: launches with a versioned node' rc_is 0 +check '... the newest of them, not the first found' contains "${OUT}" "-- ${T}/nodebin/node-25 " +NODE="${T}/node/24.14.0" run -- --exec 'bash -c true' +check '--exec also finds a node' rc_is 0 +NODE="${T}/node/24.14.0" + +run BWOPENCHAMBER_DIR="${T}/missing" -- +check 'a missing install is refused' rc_is 1 +check '... with a message' contains "${ERR}" 'OpenChamber is not installed' + +# ── --ssh-config and --help ────────────────────────────────────────────── + +run -- --ssh-config +check '--ssh-config prints the forwards' contains "${OUT}" 'LocalForward 24700 127.' +check '--ssh-config prints every port of the pool' contains "${OUT}" 'LocalForward 24709 127.' +run -- --help +check '--help succeeds' rc_is 0 +check '--help says no argument is passed through' contains "${OUT}" 'No OpenChamber argument is passed through' + +printf '\n%d passed, %d failed\n' "${PASS}" "${FAIL}" +[[ "${FAIL}" -eq 0 ]] diff --git a/tests/test-bwopencode2-serve.sh b/tests/test-bwopencode2-serve.sh new file mode 100755 index 0000000..17989db --- /dev/null +++ b/tests/test-bwopencode2-serve.sh @@ -0,0 +1,195 @@ +#!/usr/bin/env bash +# Tests for bwopencode2 --serve and --serve-open (argument handling, what is +# printed, the browser choice), through --dry-run. The sign-in block printed +# inside the sandbox and the host-side helper are tested in +# tests/test_serve_signin.py. +# +# Needs bash and bwrap. Uses a temporary HOME and a stub OpenCode 2 binary. +# --share-net keeps it independent of pasta and slirp4netns. +# Run on any host: tests/test-bwopencode2-serve.sh + +# shellcheck disable=SC2016 # patterns are single quoted on purpose +set -uo pipefail + +ROOT="$(CDPATH='' cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +HARNESS="${ROOT}/tests/bwopencode2-harness" +T="$(mktemp -d "${TMPDIR:-/tmp}/bwopencode2-serve-test.XXXXXX")" +trap 'rm -rf "${T}"' EXIT + +PASS=0 +FAIL=0 +check() { # check DESCRIPTION CONDITION-COMMAND... + local _desc="$1" + shift + if "$@"; then + PASS=$((PASS + 1)) + printf 'ok %s\n' "${_desc}" + else + FAIL=$((FAIL + 1)) + printf 'FAIL %s\n' "${_desc}" + fi +} +contains() { grep -qF -- "$2" <<< "$1"; } +lacks() { ! grep -qF -- "$2" <<< "$1"; } +rc_is() { [[ "${RC}" -eq "$1" ]]; } + +# ── fixtures ──────────────────────────────────────────────────────────── + +mkdir -p "${T}/home/.config/opencode2/opencode" "${T}/work" "${T}/oc2" "${T}/bin" +echo '{}' > "${T}/home/.config/opencode2/opencode/opencode.json" +printf '#!/bin/sh\necho "opencode v2.2.7"\n' > "${T}/oc2/opencode.exe" +chmod +x "${T}/oc2/opencode.exe" +# Browsers: stubs that record that they ran (a dry run must never run one). +for _b in google-chrome chromium xdg-open firefox; do + printf '#!/bin/sh\ntouch "%s/ran-%s"\n' "${T}" "${_b}" > "${T}/bin/${_b}" + chmod +x "${T}/bin/${_b}" +done + +# xdg-mime: prints the default browser registered for http in the test, if any (the +# file default-browser holds its name, https-only-browser the one for https only). +printf '#!/bin/sh\n[ "$3" = x-scheme-handler/http ] && cat "%s/default-browser" 2>/dev/null\n' "${T}" > "${T}/bin/xdg-mime" +chmod +x "${T}/bin/xdg-mime" + +# run [VAR=VALUE ...] -- ARGS...: dry-run the launcher; sets OUT, ERR, RC. A +# desktop session is assumed (DISPLAY) unless the caller sets DISPLAY= ; PATH +# has the stub browsers first, so a browser the host has does not matter for +# the tests that name one. +run() { + local -a _env=() + while [[ $# -gt 0 && "$1" != -- ]]; do + _env+=("$1") + shift + done + shift + OUT="$(cd "${T}/work" && env -u OPENCODE_PASSWORD -u OPENCODE_SERVER_PASSWORD \ + -u BWOPENCODE2_SERVE_PASSWORD -u BWOPENCODE2_SERVE_PORTS -u BWOPENCODE2_SERVE_BROWSER \ + -u OPENCODE2_DB_GUARD -u DISPLAY -u WAYLAND_DISPLAY \ + -u XDG_CONFIG_HOME -u XDG_DATA_HOME -u XDG_CACHE_HOME \ + HOME="${T}/home" PATH="${T}/bin:${PATH}" DISPLAY=:0 \ + HARNESS_OC2_BIN="${T}/oc2/opencode.exe" ${_env[@]+"${_env[@]}"} \ + "${HARNESS}" --dry-run --share-net "$@" 2> "${T}/err")" + RC=$? + ERR="$(cat "${T}/err")" +} + +# ── --serve without --serve-open ───────────────────────────────────────── + +run -- --serve +check 'a plain --serve succeeds' rc_is 0 +check '... says it is starting and where' contains "${ERR}" 'starting the sandboxed OpenCode 2 web UI on 127.' +check '... says the sign-in details follow once the server is up' contains "${ERR}" 'printed once the server is up' +check '... does not mention --serve-open' lacks "${ERR}" '--serve-open' +check 'the sandbox command is the wrapper, given the ssh host and alias' contains "${OUT}" 'bwopencode2-serve' +check '... and a random password is announced to it as such' contains "${OUT}" '-oc random' +check 'the password is passed to the server (hidden)' contains "${OUT}" "OPENCODE_PASSWORD='REDACTED'" +check 'no browser ran' [ ! -e "${T}/ran-google-chrome" ] + +check '--serve runs the wrapper in full mode' contains "${OUT}" 'random full' +run -- --serve-open +check '--serve-open with a browser runs the wrapper quietly (the host helper reports)' contains "${OUT}" 'random quiet' +run DISPLAY= -- --serve-open +check '--serve-open with no browser runs the wrapper in local mode' contains "${OUT}" 'random local' +check '... and gives it the reason' contains "${OUT}" 'neither\ DISPLAY\ nor\ WAYLAND_DISPLAY\ is\ set' + +run BWOPENCODE2_SERVE_PASSWORD=a-fixed-password-123 -- --serve +check 'a fixed password is announced to the wrapper as fixed' contains "${OUT}" '-oc fixed' +check '... and never printed' lacks "${OUT}${ERR}" 'a-fixed-password-123' + +# ── --serve-open: arguments ────────────────────────────────────────────── + +run -- --serve-open +check '--serve-open works by itself, without --serve' rc_is 0 +check '... starts the same server as --serve' contains "${ERR}" 'starting the sandboxed OpenCode 2 web UI on 127.' +check '... and opens the browser' contains "${ERR}" "${T}/bin/google-chrome --app=http://127." +check '... with the same sandbox command as --serve' contains "${OUT}" 'bwopencode2-serve' +check '... and a random password announced to it' contains "${OUT}" '-oc random' +run -- --serve --serve-open +check '--serve --serve-open together mean the same' contains "${ERR}" "${T}/bin/google-chrome --app=http://127." +run -- --serve +check '--serve by itself opens nothing' lacks "${ERR}" '--serve-open' +check '... and starts no browser' [ ! -e "${T}/ran-google-chrome" ] +run -- --serve-open --exec true +check '--serve-open with --exec is refused (as --serve is)' rc_is 1 +check '... and says why' contains "${ERR}" '--serve and --serve-open cannot be combined with --exec' +run -- --serve-open --serve-ssh-config +check '--serve-ssh-config still just prints the block' rc_is 0 +run -- --serve-open extra-argument +check '--serve-open takes no opencode2 arguments' rc_is 1 +run -- --serve-open --publish-addr 127.9.9.9 +check '--serve-open cannot be combined with --publish-addr' rc_is 1 + +# ── --serve-open: the browser ──────────────────────────────────────────── + +run -- --serve --serve-open +check '--serve-open succeeds on a dry run' rc_is 0 +check 'a Chromium-based browser is preferred and gets --app' contains "${ERR}" "${T}/bin/google-chrome --app=http://127." +check '... with a one-time link of its own' contains "${ERR}" '/auth/connect/CODE' +check '... started on this host once the server is up' contains "${ERR}" 'will be started on this host once the server is up' +check 'a dry run starts no browser' [ ! -e "${T}/ran-google-chrome" ] + +run BWOPENCODE2_SERVE_BROWSER=xdg-open -- --serve --serve-open +check 'xdg-open gets the plain link' contains "${ERR}" "${T}/bin/xdg-open http://127." +check '... without --app' lacks "${ERR}" '--app=' + +run BWOPENCODE2_SERVE_BROWSER=firefox -- --serve-open +check 'another browser gets the plain link' contains "${ERR}" "${T}/bin/firefox http://127." + +run BWOPENCODE2_SERVE_BROWSER=chromium -- --serve --serve-open +check 'chromium named by the user gets --app' contains "${ERR}" "${T}/bin/chromium --app=http://127." + +run BWOPENCODE2_SERVE_BROWSER=no-such-browser-here -- --serve --serve-open +check 'a named browser that does not exist is an error' rc_is 1 +check '... that names the variable' contains "${ERR}" 'BWOPENCODE2_SERVE_BROWSER' + +run DISPLAY= -- --serve --serve-open +check 'no desktop session: still a normal launch' rc_is 0 +check '... says nothing will be opened and why' contains "${ERR}" 'nothing will be opened (neither DISPLAY nor WAYLAND_DISPLAY is set' +check '... and says the link to open by hand is printed' contains "${ERR}" 'link to open by hand' +check '... and names no browser' lacks "${ERR}" '--app=' + +run DISPLAY= WAYLAND_DISPLAY=wayland-0 -- --serve --serve-open +check 'WAYLAND_DISPLAY alone counts as a desktop session' contains "${ERR}" 'will be started on this host' + +# ── --serve-open: no browser installed (a workstation without one) ────── + +# HARNESS_SERVE_OPEN_CANDIDATES= : none of the usual browsers is installed. The +# stub xdg-open is on PATH, as the real one is on a GNOME host, but no default +# browser is registered: xdg-open would hand the link to the text editor. +rm -f "${T}/default-browser" +run HARNESS_SERVE_OPEN_CANDIDATES= -- --serve --serve-open +check 'no browser and no default browser: still a normal launch' rc_is 0 +check '... nothing will be opened, and why' contains "${ERR}" 'nothing will be opened (no web browser found' +check '... xdg-open is not used' lacks "${ERR}" 'xdg-open' +check '... and no browser ran' [ ! -e "${T}/ran-xdg-open" ] + +echo 'firefox.desktop' > "${T}/default-browser" +run HARNESS_SERVE_OPEN_CANDIDATES= -- --serve --serve-open +check 'a registered default browser is reached through xdg-open' contains "${ERR}" "${T}/bin/xdg-open http://127." +check '... without --app' lacks "${ERR}" '--app=' +rm -f "${T}/default-browser" + +# A default browser for https only: the link is http, so it does not count. +printf '#!/bin/sh\n[ "$3" = x-scheme-handler/https ] && echo firefox.desktop\n' > "${T}/bin/xdg-mime" +run HARNESS_SERVE_OPEN_CANDIDATES= -- --serve --serve-open +check 'a default browser for https only is not used for the http link' contains "${ERR}" 'nothing will be opened (no web browser found' +printf '#!/bin/sh\n[ "$3" = x-scheme-handler/http ] && cat "%s/default-browser" 2>/dev/null\n' "${T}" > "${T}/bin/xdg-mime" + +run HARNESS_SERVE_OPEN_CANDIDATES=firefox -- --serve --serve-open +check 'firefox is used when it is installed' contains "${ERR}" "${T}/bin/firefox http://127." +check '... without --app' lacks "${ERR}" '--app=' + +run HARNESS_SERVE_OPEN_CANDIDATES="chromium firefox" -- --serve --serve-open +check 'a Chromium-based browser is tried before firefox' contains "${ERR}" "${T}/bin/chromium --app=http://127." + +# ── nothing else changed ───────────────────────────────────────────────── + +run -- --serve --serve-port 24705 +check '--serve-port still works' contains "${ERR}" ':24705 (this host only)' +run -- --serve-open --serve-port 24705 +check '--serve-port works with --serve-open by itself' contains "${ERR}" ':24705 (this host only)' +run -- --serve-port 24705 +check '--serve-port without --serve or --serve-open is refused' rc_is 1 +check '... and says why' contains "${ERR}" '--serve-port needs --serve or --serve-open' + +printf '\n%d passed, %d failed\n' "${PASS}" "${FAIL}" +[[ "${FAIL}" -eq 0 ]] diff --git a/tests/test-opencode2-db-guard.sh b/tests/test-opencode2-db-guard.sh index 582e7f7..4f16b1c 100755 --- a/tests/test-opencode2-db-guard.sh +++ b/tests/test-opencode2-db-guard.sh @@ -130,6 +130,32 @@ OUT="$(run_guard "${D}")" check "second session of the same version on this host is allowed" contains "${OUT}" 'rc=0' check "the running session holds its lock" bash -c '! flock -x -n "$1" true' _ "$(locks_of "${D}")/session@${HOST}@2.2.5.lock" +# The sandbox library hands the sandbox data on descriptors 10 up and its relay +# opens them over whatever is there (here 10 to 29, so the number bash would pick +# for the lock is among them). The session lock must not be on one of +# those, or the relay closes it and the lock is released while the sandbox runs. +D="$(new_data relay)" +LOCK="$(locks_of "${D}")/session@${HOST}@2.2.5.lock" +OUT="$( + bash -c ' + source "$1" + source "$2" + oc2db_guard "$3" "$4" > /dev/null 2>&1 || exit 1 + echo "fd=${OC2DB_SESSION_FD}" + _entries=() + for _n in $(seq 10 29); do + export "_BWRAP_FD_DATA_${_n}=data" + _entries+=("${_n}" data) + done + exec /usr/bin/bash -c "$_BWRAP_FD_RELAY_SHIM" bwrap-fd-relay "${_entries[@]}" -- /usr/bin/sleep 4 +' _ "${ROOT}/lib/bwrap_sandbox_lib.sh" "${LIB}" "${D}" "${NEW}" 2> /dev/null & + sleep 1.5 + flock -x -n "${LOCK}" true && echo "lock=released" || echo "lock=held" + wait +)" +check "the session lock is on a descriptor the relay does not use" bash -c '[[ "$(sed -n "s/^fd=//p" <<< "$1")" -ge 200 ]]' _ "${OUT}" +check "the session lock is still held after the relay opened descriptors 10 to 29" contains "${OUT}" 'lock=held' + D="$(new_data otherhost)" hold_lock "$(locks_of "${D}")/session@elsewhere.example@2.2.5.lock" 4 OUT="$(run_guard "${D}")" 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..886740c --- /dev/null +++ b/tests/test_netlog.py @@ -0,0 +1,413 @@ +"""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 sys +import threading +import time +import unittest + +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, os.path.join(HERE, "..", "lib")) +import bwrap_netlog as netlog # noqa: E402 + +FIXTURE = os.path.join(HERE, "data", "pasta-debug.log") +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_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 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_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) + 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_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_signin.py b/tests/test_serve_signin.py new file mode 100644 index 0000000..2ecf8dd --- /dev/null +++ b/tests/test_serve_signin.py @@ -0,0 +1,566 @@ +"""Exercise bwopencode2 --serve's sign-in output and --serve-open without a real server. + +The wrapper that runs inside the sandbox (SERVE_WRAPPER) is cut out of +bin/bwopencode2 and run with a fake OpenCode server. The host-side helper +(serve_open_run) is run by sourcing bin/bwopencode2 with a stub browser. +""" + +import os +import pty +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" +PASSWORD = "test-only-password" + +# GET gives 401 (the server is listening). POST /api/pair checks the Basic +# password the launcher is meant to send on curl's stdin and gives a new code +# each time (code1, code2, ...). FAKE_PAIR=fail answers 500 instead. +FAKE_SERVER = """#!/usr/bin/env python3 +import base64 +import http.server +import os +import sys +import threading + +expected = "Basic " + base64.b64encode(("opencode:" + os.environ["OPENCODE_PASSWORD"]).encode()).decode() +count = [0] + +class Handler(http.server.BaseHTTPRequestHandler): + def do_GET(self): + self.send_response(401) + self.end_headers() + + def do_POST(self): + if os.environ.get("FAKE_PAIR") == "fail": + self.send_response(500) + self.end_headers() + return + if self.headers.get("Authorization") != expected or self.path != "/api/pair": + self.send_response(401) + self.end_headers() + return + count[0] += 1 + body = ('{"code":"code%d"}' % count[0]).encode() + self.send_response(200) + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + 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(60, httpd.shutdown).start() +httpd.serve_forever() +""" + + +def wrapper_source(): + match = re.search( + r"read -r -d '' SERVE_WRAPPER << 'SERVE_WRAPPER_EOF' \|\| true\n(.*?)\nSERVE_WRAPPER_EOF", + WRAPPER.read_text(), + re.S, + ) + assert match is not None + return match.group(1) + + +def free_port(): + with socket.socket() as sock: + sock.bind(("127.0.0.1", 0)) + return sock.getsockname()[1] + + +class Reader: + """Read a process's stderr in pieces (select on a buffered file object misses data).""" + + def __init__(self, proc): + self.fd = proc.stderr.fileno() + self.buf = "" + + def until(self, text, timeout=12): + """Return what was read up to the end of the line that holds text.""" + deadline = time.monotonic() + timeout + while text not in self.buf or "\n" not in self.buf[self.buf.index(text) :]: + remaining = deadline - time.monotonic() + if remaining <= 0: + break + ready, _, _ = select.select([self.fd], [], [], remaining) + if not ready: + break + chunk = os.read(self.fd, 65536) + if not chunk: + break + self.buf += chunk.decode() + if text in self.buf: + end = self.buf.find("\n", self.buf.index(text)) + 1 or len(self.buf) + else: + end = len(self.buf) + out, self.buf = self.buf[:end], self.buf[end:] + return out + + def pending(self, wait): + """Anything more printed within wait seconds.""" + ready, _, _ = select.select([self.fd], [], [], wait) + return os.read(self.fd, 65536).decode() if ready else "" + + +def stop(proc): + proc.terminate() + try: + proc.communicate(timeout=5) + except subprocess.TimeoutExpired: + proc.kill() + proc.communicate(timeout=5) + + +class WrapperTest(unittest.TestCase): + def setUp(self): + if shutil.which("curl") is None: + self.skipTest("curl is not installed") + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + self.server = Path(self.tmp.name) / "server.py" + self.server.write_text(FAKE_SERVER) + self.server.chmod(0o700) + self.port = free_port() + + def start(self, password=PASSWORD, stdin=subprocess.DEVNULL, extra_env=None, pw_kind="random", mode="full", why=""): + env = { + "PATH": os.environ.get("PATH", "/usr/bin:/bin"), + "OPENCODE_PASSWORD": password, + "BWRAP_NET_PUBLISH_ADDR": "127.9.9.1", + } + env.update(extra_env or {}) + proc = subprocess.Popen( + [ + "bash", + "-c", + wrapper_source(), + "bwopencode2-serve", + str(self.server), + str(self.port), + "127.0.0.1", + "testhost.example.org", + "testhost-oc", + pw_kind, + mode, + why, + ], + env=env, + stdin=stdin, + stdout=subprocess.DEVNULL, + stderr=subprocess.PIPE, + text=True, + ) + self.addCleanup(stop, proc) + self.reader = Reader(proc) + return proc + + def test_block_has_ssh_command_links_and_password(self): + self.start() + out = self.reader.until("Password (new for this session)") + port = self.port + self.assertIn(f"ssh -L {port}:127.9.9.1:{port} testhost.example.org", out) + self.assertIn("ssh testhost-oc", out) + self.assertIn(f"http://127.0.0.1:{port}/auth/connect/code1", out) + self.assertIn(f"google-chrome --app=http://127.0.0.1:{port}/auth/connect/code1", out) + self.assertIn(f"http://127.9.9.1:{port}/auth/connect/code1", out) + self.assertIn("Press Enter here for a new one", out) + self.assertIn(f"Password (new for this session): {PASSWORD}", out) + + def test_fixed_password_is_not_printed(self): + self.start(pw_kind="fixed") + out = self.reader.until("BWOPENCODE2_SERVE_PASSWORD") + self.assertIn("/auth/connect/code1", out) + self.assertNotIn(PASSWORD, out) + + def test_quote_and_backslash_in_the_password_reach_the_server(self): + # The fake server accepts only the exact password, so a code means curl + # received it unchanged through its config file. + self.start(password='pa"ss\\word-123') + out = self.reader.until("Password (new for this session)") + self.assertIn("/auth/connect/code1", out) + + def test_failed_pairing_points_at_the_password(self): + self.start(extra_env={"FAKE_PAIR": "fail"}) + out = self.reader.until("Password (new for this session)") + self.assertIn("could not get a pairing link", out) + self.assertIn(f"http://127.0.0.1:{self.port}/\n", out) + self.assertNotIn("/auth/connect/", out) + self.assertNotIn("Press Enter", out) + + def test_local_mode_is_only_a_link_and_the_password(self): + # --serve-open with no browser to start: the link for this machine, why, + # and the password for the page's "Use password"; none of the ssh block. + self.start(mode="local", why="no web browser found") + out = self.reader.until("If the link fails") + port = self.port + self.assertIn("could not open a browser (no web browser found).", out) + self.assertIn(f"Open this on this machine: http://127.9.9.1:{port}/auth/connect/code1", out) + self.assertIn("press Enter for a new link", out) + self.assertIn(f'If the link fails, click "Use password": {PASSWORD}', out) + for text in ("ssh", "google-chrome", "127.0.0.1", "On this host"): + self.assertNotIn(text, out) + + def test_local_mode_with_a_fixed_password_does_not_print_it(self): + self.start(mode="local", why="no desktop", pw_kind="fixed") + out = self.reader.until("BWOPENCODE2_SERVE_PASSWORD") + self.assertIn("/auth/connect/code1", out) + self.assertNotIn(PASSWORD, out) + + def test_local_mode_without_a_link_points_at_the_page_and_password(self): + self.start(mode="local", why="no desktop", extra_env={"FAKE_PAIR": "fail"}) + out = self.reader.until("If the link fails") + self.assertIn(f'click "Use password": http://127.9.9.1:{self.port}/', out) + self.assertNotIn("/auth/connect/", out) + self.assertNotIn("press Enter", out) + + def test_quiet_mode_prints_nothing_until_enter_asks_for_a_link(self): + # The host helper reports whether the browser opened; the wrapper stays silent. + master, slave = pty.openpty() + try: + self.start(stdin=slave, mode="quiet") + os.close(slave) + self.assertEqual(self.reader.pending(3), "") + os.write(master, b"\n") + out = self.reader.until("If the link fails") + self.assertIn(f"Open this on this machine: http://127.9.9.1:{self.port}/auth/connect/code1", out) + self.assertNotIn("could not open a browser", out) + self.assertNotIn("ssh", out) + finally: + os.close(master) + + def test_enter_prints_a_new_link(self): + master, slave = pty.openpty() + try: + self.start(stdin=slave) + os.close(slave) + first = self.reader.until("Password (new for this session)") + self.assertIn("/auth/connect/code1", first) + os.write(master, b"\n") + second = self.reader.until("Password (new for this session)") + self.assertIn("/auth/connect/code2", second) + self.assertNotIn("/auth/connect/code1", second) + finally: + os.close(master) + + def test_end_of_input_does_not_spin(self): + # A closed terminal makes read fail at once; the wrapper must keep + # waiting for the server and not loop printing links. + master, slave = pty.openpty() + proc = self.start(stdin=slave) + os.close(slave) + out = self.reader.until("Password (new for this session)") + self.assertIn("/auth/connect/code1", out) + os.close(master) + time.sleep(2) + self.assertIsNone(proc.poll()) + self.assertEqual(self.reader.pending(0.5), "", "the wrapper printed more after end of input") + + +class OpenHelperTest(unittest.TestCase): + """serve_open_run, the host-side helper of --serve-open.""" + + def setUp(self): + if shutil.which("curl") is None: + self.skipTest("curl is not installed") + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + tmp = Path(self.tmp.name) + server = tmp / "server.py" + server.write_text(FAKE_SERVER) + self.port = free_port() + self.argv_log = tmp / "browser-argv" + self.browser = tmp / "browser" + self.browser.write_text(f"#!/bin/sh\nprintf '%s\\n' \"$@\" > {self.argv_log}\n") + self.browser.chmod(0o700) + self.server_env = { + "PATH": os.environ.get("PATH", "/usr/bin:/bin"), + "OPENCODE_PASSWORD": PASSWORD, + } + self.server = server + + def start_server(self, **extra): + proc = subprocess.Popen( + ["python3", str(self.server), "serve", "--port", str(self.port)], + env={**self.server_env, **extra}, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + self.addCleanup(lambda: (proc.kill(), proc.wait())) + return proc + + def run_helper(self, app, pid_expr='"$$"', timeout=30, browser=None, random=1): + script = f""" +set -euo pipefail +source "{WRAPPER}" +SERVE_ADDR=127.0.0.1 +SERVE_PORT={self.port} +SERVE_PASSWORD='{PASSWORD}' +SERVE_PASSWORD_RANDOM={random} +SERVE_OPEN_WAIT_SECONDS=1 +SERVE_OPEN_BIN="{browser or self.browser}" +SERVE_OPEN_APP={app} +serve_open_run {pid_expr} "$(bwrap_proc_start_time "$$")" +""" + # The stub browser is started in the background and detached; give it a moment. + result = subprocess.run( + ["bash", "-c", script], + env={"PATH": os.environ.get("PATH", "/usr/bin:/bin"), "HOME": self.tmp.name}, + capture_output=True, + text=True, + timeout=timeout, + ) + for _ in range(30): + if self.argv_log.exists() and self.argv_log.read_text().endswith("\n"): + break + time.sleep(0.1) + return result + + def test_chromium_family_gets_an_app_window_with_a_signed_in_link(self): + self.start_server() + result = self.run_helper(app=1) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual( + self.argv_log.read_text().split("\n")[:-1], + [f"--app=http://127.0.0.1:{self.port}/auth/connect/code1"], + ) + self.assertIn(f"OpenCode web UI on 127.0.0.1:{self.port}, opened in browser.", result.stderr) + self.assertEqual(result.stderr.strip().count("\n"), 0, "one line when the browser opened") + self.assertNotIn(PASSWORD, result.stderr) + + def test_other_browsers_get_the_plain_link(self): + self.start_server() + result = self.run_helper(app=0) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual( + self.argv_log.read_text().split("\n")[:-1], + [f"http://127.0.0.1:{self.port}/auth/connect/code1"], + ) + + def test_no_link_opens_the_login_page(self): + self.start_server(FAKE_PAIR="fail") + result = self.run_helper(app=1) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual( + self.argv_log.read_text().split("\n")[:-1], + [f"--app=http://127.0.0.1:{self.port}/"], + ) + self.assertIn("opened in browser.", result.stderr) + self.assertIn(f'The server gave no sign-in link: click "Use password": {PASSWORD}', result.stderr) + + def _failing_browser(self, status): + stub = Path(self.tmp.name) / f"failing-browser-{status}" + stub.write_text(f"#!/bin/sh\nprintf '%s\\n' \"$@\" >> {self.argv_log}.tries\nexit {status}\n") + stub.chmod(0o700) + return stub + + def test_a_browser_that_fails_gives_a_fresh_link_and_the_password(self): + # The failed attempt used code1 (the browser may have fetched it), so the + # link to open by hand must be a new one. + self.start_server() + result = self.run_helper(app=1, browser=self._failing_browser(3)) + self.assertEqual(result.returncode, 0, result.stderr) + err = result.stderr + self.assertIn("could not open a browser (failing-browser-3 exited with status 3).", err) + self.assertIn(f"Open this on this machine: http://127.0.0.1:{self.port}/auth/connect/code2", err) + self.assertIn("press Enter for a new link", err) + self.assertIn(f'If the link fails, click "Use password": {PASSWORD}', err) + self.assertNotIn("opened in", err) + tries = Path(f"{self.argv_log}.tries").read_text() + self.assertIn("/auth/connect/code1", tries) + + def test_a_fixed_password_is_not_printed_in_the_fallback(self): + self.start_server() + result = self.run_helper(app=1, browser=self._failing_browser(1), random=0) + self.assertIn("could not open a browser", result.stderr) + self.assertIn("BWOPENCODE2_SERVE_PASSWORD", result.stderr) + self.assertNotIn(PASSWORD, result.stderr) + + def test_a_browser_that_exits_cleanly_counts_as_opened(self): + # Chrome hands the link to a window that is already open and exits 0. + self.start_server() + result = self.run_helper(app=1, browser=self._failing_browser(0)) + self.assertIn("opened in failing-browser-0.", result.stderr) + self.assertNotIn("could not open", result.stderr) + + def test_a_browser_still_running_after_the_wait_counts_as_opened(self): + self.start_server() + slow = Path(self.tmp.name) / "slow-opened-browser" + slow.write_text("#!/bin/sh\nsleep 6\n") + slow.chmod(0o700) + started = time.monotonic() + result = self.run_helper(app=1, browser=slow) + self.assertLess(time.monotonic() - started, 5, "the helper must not wait for the browser to end") + self.assertIn("opened in slow-opened-browser.", result.stderr) + + def test_launcher_gone_opens_nothing(self): + # A pid that cannot be the launcher: the helper must stop without waiting for the server. + started = time.monotonic() + result = self.run_helper(app=1, pid_expr="999999") + self.assertLess(time.monotonic() - started, 10) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertFalse(self.argv_log.exists()) + + def test_start_runs_the_helper_in_the_background(self): + # serve_open_start returns at once (the launcher then execs the sandbox); + # the helper opens the browser later, while the launcher's pid is alive. + # The shell ends in exec, as the launcher does: the pid must stay the same. + self.start_server() + script = f""" +set -euo pipefail +source "{WRAPPER}" +SERVE_ADDR=127.0.0.1 +SERVE_PORT={self.port} +SERVE_PASSWORD='{PASSWORD}' +SERVE_OPEN=1 +SERVE_OPEN_BIN="{self.browser}" +SERVE_OPEN_APP=1 +DRY_RUN=0 +started=$SECONDS +serve_open_start +((SECONDS - started < 2)) || {{ echo "serve_open_start blocked" >&2; exit 1; }} +exec sleep 4 +""" + result = subprocess.run( + ["bash", "-c", script], + env={"PATH": os.environ.get("PATH", "/usr/bin:/bin"), "HOME": self.tmp.name}, + capture_output=True, + text=True, + timeout=30, + ) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual( + self.argv_log.read_text().split("\n")[:-1], + [f"--app=http://127.0.0.1:{self.port}/auth/connect/code1"], + ) + + def test_the_browser_does_not_inherit_the_launchers_descriptors(self): + # The launcher holds the database session lock on a descriptor; a browser + # that kept it would hold the lock for as long as its window lives and + # refuse the next launch. The stub keeps running while the lock is tried. + self.start_server() + lock = Path(self.tmp.name) / "session.lock" + fds = Path(self.tmp.name) / "browser-fds" + slow = Path(self.tmp.name) / "slow-browser" + slow.write_text(f"#!/bin/sh\nls /proc/$$/fd > {fds}\nsleep 8\n") + slow.chmod(0o700) + script = f""" +set -euo pipefail +source "{WRAPPER}" +SERVE_ADDR=127.0.0.1 +SERVE_PORT={self.port} +SERVE_PASSWORD='{PASSWORD}' +SERVE_OPEN=1 +SERVE_OPEN_BIN="{slow}" +SERVE_OPEN_APP=1 +DRY_RUN=0 +exec 200>> "{lock}" +flock -n 200 +serve_open_start +exec sleep 3 +""" + subprocess.run( + ["bash", "-c", script], + env={"PATH": os.environ.get("PATH", "/usr/bin:/bin"), "HOME": self.tmp.name}, + check=True, + timeout=30, + ) + for _ in range(60): + if fds.exists() and fds.read_text().endswith("\n"): + break + time.sleep(0.1) + self.assertNotIn("200", fds.read_text().split()) + # The launcher (the sleep that holds the lock, like the sandbox) has ended and + # the browser still runs: the lock must be free. + free = subprocess.run(["flock", "-n", str(lock), "true"]) + self.assertEqual(free.returncode, 0, "the browser still holds the launcher's lock") + + def test_dry_run_starts_nothing(self): + self.start_server() + script = f""" +set -euo pipefail +source "{WRAPPER}" +SERVE_ADDR=127.0.0.1 +SERVE_PORT={self.port} +SERVE_PASSWORD='{PASSWORD}' +SERVE_OPEN=1 +SERVE_OPEN_BIN="{self.browser}" +DRY_RUN=1 +serve_open_start +sleep 3 +""" + result = subprocess.run( + ["bash", "-c", script], + env={"PATH": os.environ.get("PATH", "/usr/bin:/bin"), "HOME": self.tmp.name}, + capture_output=True, + text=True, + timeout=30, + ) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertFalse(self.argv_log.exists()) + + def test_the_password_is_not_in_the_browser_arguments(self): + self.start_server() + self.run_helper(app=1) + self.assertNotIn(PASSWORD, self.argv_log.read_text()) + # The pairing code is the only secret the link holds. + self.assertIsNotNone(re.search(r"/auth/connect/code\d+\n$", self.argv_log.read_text())) + + +class StartMessageTest(unittest.TestCase): + """serve_print_info: what the launcher prints before the sandbox starts.""" + + def message(self, serve_open, dry_run=0): + script = f""" +set -euo pipefail +source "{WRAPPER}" +SERVE_ADDR=127.52.255.1 +SERVE_PORT=24700 +NET_SHARE=1 +SERVE_OPEN={serve_open} +SERVE_OPEN_BIN=/usr/bin/google-chrome +SERVE_OPEN_APP=1 +DRY_RUN={dry_run} +serve_print_info +""" + result = subprocess.run( + ["bash", "-c", script], + env={"PATH": os.environ.get("PATH", "/usr/bin:/bin"), "HOME": "/nonexistent"}, + capture_output=True, + text=True, + timeout=30, + ) + self.assertEqual(result.returncode, 0, result.stderr) + return result.stderr + + def test_serve_open_prints_one_line(self): + lines = [x for x in self.message(1).split("\n") if x] + self.assertEqual(lines, ["bwopencode2: starting the sandboxed OpenCode 2 web UI on 127.52.255.1:24700"]) + + def test_serve_keeps_its_text(self): + out = self.message(0) + self.assertIn("starting the sandboxed OpenCode 2 web UI on 127.52.255.1:24700 (this host only)", out) + self.assertIn( + "The ssh command, a one-time sign-in link and the password are printed once the server is up.", out + ) + self.assertNotIn("--serve-open", out) + + def test_serve_open_dry_run_names_the_browser_it_would_start(self): + out = self.message(1, dry_run=1) + self.assertIn("/usr/bin/google-chrome --app=http://127.52.255.1:24700/auth/connect/CODE will be started", out) + + +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..5bfb94c --- /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 + data = "" + fd = proc.stderr.fileno() + # os.read, not readline: select cannot see lines a buffered reader already holds. + while time.monotonic() < deadline and "could not get a pairing link" not in data: + ready, _, _ = select.select([fd], [], [], max(0, deadline - time.monotonic())) + if not ready: + break + chunk = os.read(fd, 65536) + if not chunk: + break + data += chunk.decode() + self.assertIn("could not get a pairing link", data) + finally: + proc.terminate() + try: + proc.communicate(timeout=3) + except subprocess.TimeoutExpired: + proc.kill() + proc.communicate(timeout=3) + + +if __name__ == "__main__": + unittest.main()