Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

fix(gateway): close tunnel fork signal race

Rahman8d6c28704958
diff --git a/SECURITY.md b/SECURITY.md
index d51b681e..e919bc55 100644
--- a/SECURITY.md
+++ b/SECURITY.md
@@ -35,6 +35,8 @@ application roles, but they still share the same deployment and underlying Unix
- Gateway/update lifecycle state is owner-only and fail-closed: public tunneling requires verified
loopback-only exposure; offline recovery uses checkout-scoped receipts, an exclusive process-identity
lock, and durable pre-quiesce recovery intent rather than treating Git HEAD as deployment proof.
+ Tunnel startup additionally uses a parent/child release handshake, so Cloudflared cannot execute
+ before its process lifetime is tracked; interruption before release makes the held child self-exit.
- Use `NEXT_PUBLIC_OS_DEMO=1` only in a separate mock-only public demo checkout.
The core gateway client follows the same immutable-artifact rule as managed apps: MSO pins the
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index ff9ae89a..85a02c3d 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -13,6 +13,7 @@ this is the *what*, and it is what Settings → About shows as “What's new”.
**Fixed**
+- `gateway` close tunnel fork signal race
- `gateway` serialize offline recovery state
- `gateway` harden update and proxy edges
- `gateway` close lifecycle review gaps
diff --git a/docs/PROGRESS.md b/docs/PROGRESS.md
index 90ceffa0..ffbe20cf 100644
--- a/docs/PROGRESS.md
+++ b/docs/PROGRESS.md
@@ -32,7 +32,11 @@ interactive CLI use emits a throttled Git-backed update notice when `origin/main
recovery: offline deployment state is keyed by canonical checkout, the whole offline transaction is
serialized with a PID/start-ticks lock, recovery intent is durable before an owned runtime is quiesced,
and an interrupted post-quiesce state write is reconciled on retry. The shared private-state atomic
-writer now propagates write/rename failures explicitly even when called from a shell conditional.
+writer now propagates write/rename failures explicitly even when called from a shell conditional. A
+full pre-push run then exposed a fork/signal race before the tunnel fingerprint existed. Tunnel launch
+now uses a held-child handshake: the scrubbed child cannot `exec cloudflared` until its parent records
+PID + kernel start-ticks and releases an owner-only gate; if the parent disappears first, the child
+self-terminates. This closes the last pre-persistence orphan window without weakening process identity.
The global response policy additionally sends `X-Robots-Tag: noindex, nofollow, noarchive` because an
MSO control plane should not be indexed merely because a temporary HTTPS endpoint exists. The
diff --git a/scripts/lib/gateway-held-child.sh b/scripts/lib/gateway-held-child.sh
new file mode 100755
index 00000000..b30d7845
--- /dev/null
+++ b/scripts/lib/gateway-held-child.sh
@@ -0,0 +1,25 @@
+#!/usr/bin/env bash
+# Hold a newly-forked tunnel child until its parent records PID/start-ticks.
+# If the parent disappears before release, exit instead of orphaning a tunnel.
+set -euo pipefail
+
+parent_pid="${1:-}"; parent_ticks="${2:-}"; gate="${3:-}"; shift 3 || exit 2
+[[ "$parent_pid" =~ ^[0-9]+$ && "$parent_ticks" =~ ^[0-9]+$ ]] || exit 2
+case "$gate" in /*) ;; *) exit 2 ;; esac
+[ "$#" -gt 0 ] || exit 2
+cmd=("$@")
+
+for ((i=0; i<500; i++)); do
+ if [ -f "$gate" ] && [ ! -L "$gate" ]; then
+ [ "$(stat -c '%u' -- "$gate" 2>/dev/null || true)" = "$(id -u)" ] || exit 126
+ [ "$(stat -c '%a' -- "$gate" 2>/dev/null || true)" = 600 ] || exit 126
+ exec "${cmd[@]}"
+ fi
+
+ [ -r "/proc/$parent_pid/stat" ] || exit 125
+ IFS= read -r line <"/proc/$parent_pid/stat" || exit 125
+ rest="${line##*) }"; set -- $rest
+ [ "${20:-}" = "$parent_ticks" ] || exit 125
+ sleep 0.01
+done
+exit 124
diff --git a/scripts/lib/gateway-pending.sh b/scripts/lib/gateway-pending.sh
new file mode 100755
index 00000000..1acdabee
--- /dev/null
+++ b/scripts/lib/gateway-pending.sh
@@ -0,0 +1,72 @@
+#!/usr/bin/env bash
+# Handshake + provisional child identity for the fork -> cloudflared exec window.
+
+TUNNEL_PENDING_PID=0
+TUNNEL_PENDING_TICKS=''
+TUNNEL_PENDING_GATE=''
+TUNNEL_SPAWN_PID=0
+
+gateway_track_pending_tunnel() {
+ local pid="$1" ticks i
+ TUNNEL_PENDING_PID="$pid"; TUNNEL_PENDING_TICKS=''
+ for i in $(seq 1 20); do
+ ticks="$(gateway_proc_start_ticks "$pid" 2>/dev/null || true)"
+ [ -n "$ticks" ] && { TUNNEL_PENDING_TICKS="$ticks"; GATEWAY_PENDING_CLEANUP=1; return 0; }
+ gateway_pid_alive "$pid" || return 1
+ sleep 0.01
+ done
+ return 1
+}
+
+gateway_pending_gate_cleanup() {
+ local gate="${TUNNEL_PENDING_GATE:-}"
+ [ -n "$gate" ] || return 0
+ if [ -e "$gate" ] || [ -L "$gate" ]; then
+ mso_private_state_remove_file "$gate" >/dev/null 2>&1 || true
+ fi
+ TUNNEL_PENDING_GATE=''
+}
+
+gateway_stop_pending_tunnel() {
+ local pid="${TUNNEL_PENDING_PID:-0}" ticks="${TUNNEL_PENDING_TICKS:-}" live i
+ gateway_pending_gate_cleanup
+ [ "$pid" -gt 1 ] 2>/dev/null && [ -n "$ticks" ] || return 0
+ live="$(gateway_proc_start_ticks "$pid" 2>/dev/null || true)"
+ [ "$live" = "$ticks" ] || { TUNNEL_PENDING_PID=0; TUNNEL_PENDING_TICKS=''; return 0; }
+ kill "$pid" 2>/dev/null || true
+ for i in $(seq 1 25); do
+ live="$(gateway_proc_start_ticks "$pid" 2>/dev/null || true)"
+ [ "$live" != "$ticks" ] && break
+ sleep 0.04
+ done
+ live="$(gateway_proc_start_ticks "$pid" 2>/dev/null || true)"
+ [ "$live" != "$ticks" ] || kill -KILL "$pid" 2>/dev/null || true
+ TUNNEL_PENDING_PID=0; TUNNEL_PENDING_TICKS=''
+}
+
+gateway_spawn_held_tunnel() {
+ local parent_ticks gate pid safe_path safe_lang i
+ parent_ticks="$(gateway_proc_start_ticks $$)" || return 1
+ safe_path="${PATH:-/usr/local/bin:/usr/bin:/bin}"; safe_lang="${LANG:-C.UTF-8}"
+ for i in $(seq 1 20); do
+ gate="$STATE_ROOT/.tunnel-release.$$.$RANDOM"
+ [ ! -e "$gate" ] && [ ! -L "$gate" ] && break
+ gate=''
+ done
+ [ -n "${gate:-}" ] || return 1
+ TUNNEL_PENDING_GATE="$gate"
+
+ # The helper starts in a scrubbed environment and cannot exec cloudflared until
+ # this parent has recorded the child lifetime. If this parent dies first, the
+ # helper observes the missing/reused parent PID and exits on its own.
+ nohup env -i "HOME=$HOME" "PATH=$safe_path" "LANG=$safe_lang" \
+ /bin/bash "$ROOT/scripts/lib/gateway-held-child.sh" "$$" "$parent_ticks" "$gate" "$@" \
+ >>"$CF_LOG" 2>&1 & pid=$!
+ if ! gateway_track_pending_tunnel "$pid"; then
+ kill "$pid" 2>/dev/null || true
+ gateway_pending_gate_cleanup
+ return 1
+ fi
+ printf '1\n' | mso_private_state_atomic_write "$gate" >/dev/null || { gateway_stop_pending_tunnel; return 1; }
+ TUNNEL_SPAWN_PID="$pid"
+}
diff --git a/scripts/lib/gateway-tunnel.sh b/scripts/lib/gateway-tunnel.sh
index 5438aab7..90f84980 100644
--- a/scripts/lib/gateway-tunnel.sh
+++ b/scripts/lib/gateway-tunnel.sh
@@ -60,24 +60,25 @@ gateway_validate_named_tunnel() {
}
gateway_spawn_tunnel() {
- local pid identity safe_path safe_lang
- local -a clean_env
+ local pid identity
gateway_private_file "$CF_LOG"; : >"$CF_LOG"
- safe_path="${PATH:-/usr/local/bin:/usr/bin:/bin}"; safe_lang="${LANG:-C.UTF-8}"
# The parent intentionally has OS_LOGIN_PASSWORD, session secrets and BYOK keys
- # for the Next runtime. cloudflared needs none of them. Start it from an empty
- # environment so a tunnel/client compromise cannot read unrelated MSO secrets.
- clean_env=(env -i "HOME=$HOME" "PATH=$safe_path" "LANG=$safe_lang")
+ # for the Next runtime. The held-child helper scrubs that environment and cannot
+ # exec cloudflared until this parent has persisted the child lifetime identity.
if [ "$GATEWAY_MODE" = temporary ]; then
[ -z "${OS_PUBLIC_ORIGIN:-}" ] || gateway_info "warning: temporary gateway leaves existing OS_PUBLIC_ORIGIN unchanged"
- nohup "${clean_env[@]}" "$CLOUDFLARED" tunnel --no-autoupdate --url "$LOCAL_URL" >>"$CF_LOG" 2>&1 & pid=$!
+ gateway_spawn_held_tunnel "$CLOUDFLARED" tunnel --no-autoupdate --url "$LOCAL_URL" || return 1
+ pid="$TUNNEL_SPAWN_PID"
identity="$(gateway_wait_spawn_identity "$pid" "$CLOUDFLARED" tunnel --no-autoupdate --url "$LOCAL_URL" 2>/dev/null || true)"
else
- nohup "${clean_env[@]}" "$CLOUDFLARED" tunnel --config "$GATEWAY_CONFIG" --no-autoupdate run "$GATEWAY_TUNNEL" >>"$CF_LOG" 2>&1 & pid=$!
+ gateway_spawn_held_tunnel "$CLOUDFLARED" tunnel --config "$GATEWAY_CONFIG" --no-autoupdate run "$GATEWAY_TUNNEL" || return 1
+ pid="$TUNNEL_SPAWN_PID"
identity="$(gateway_wait_spawn_identity "$pid" "$CLOUDFLARED" tunnel --config "$GATEWAY_CONFIG" --no-autoupdate run "$GATEWAY_TUNNEL" 2>/dev/null || true)"
fi
- [ -n "$identity" ] || { kill "$pid" 2>/dev/null || true; return 1; }
+ [ -n "$identity" ] || { gateway_stop_pending_tunnel; return 1; }
+ gateway_pending_gate_cleanup
TUNNEL_IDENTITY="$identity"
+ TUNNEL_PENDING_PID=0; TUNNEL_PENDING_TICKS=''
GATEWAY_PENDING_CLEANUP=1
}
@@ -148,7 +149,8 @@ gateway_probe_public() {
}
gateway_cleanup_failed_start() {
- [ "${TUNNEL_IDENTITY:-null}" = null ] || gateway_stop_identity "$TUNNEL_IDENTITY"
+ if [ "${TUNNEL_IDENTITY:-null}" != null ]; then gateway_stop_identity "$TUNNEL_IDENTITY"
+ else gateway_stop_pending_tunnel; fi
[ "${RUNTIME_STARTED_NOW:-false}" != true ] || gateway_stop_identity "$RUNTIME_IDENTITY"
}
diff --git a/scripts/mso-gateway b/scripts/mso-gateway
index 0aea951b..da6afe5f 100755
--- a/scripts/mso-gateway
+++ b/scripts/mso-gateway
@@ -51,6 +51,8 @@ LOCK_HELD=0
. "$ROOT/scripts/lib/gateway-recovery.sh"
# shellcheck source=scripts/lib/gateway-runtime.sh
. "$ROOT/scripts/lib/gateway-runtime.sh"
+# shellcheck source=scripts/lib/gateway-pending.sh
+. "$ROOT/scripts/lib/gateway-pending.sh"
# shellcheck source=scripts/lib/gateway-tunnel.sh
. "$ROOT/scripts/lib/gateway-tunnel.sh"
case "${1:-status}" in
diff --git a/scripts/mso-gateway.test.ts b/scripts/mso-gateway.test.ts
index b002aa78..955d31cf 100644
--- a/scripts/mso-gateway.test.ts
+++ b/scripts/mso-gateway.test.ts
@@ -32,8 +32,7 @@ describe("mso public gateway", () => {
expect(run(["stop"], f.baseEnv)).toContain("MSO remains loopback-only");
expect(fs.existsSync(state.path)).toBe(false);
expect(() => process.kill(state.value.tunnelIdentity.pid, 0)).toThrow();
- });
-
+ }, 15_000);
it("verifies a Quick Tunnel through Cloudflare DoH when the local resolver still has NXDOMAIN", () => {