diff --git a/compose/layered.yml b/compose/layered.yml index e642a32b..72d6197d 100644 --- a/compose/layered.yml +++ b/compose/layered.yml @@ -3,6 +3,36 @@ # All state and app volumes are EXTERNAL and cannot be deleted by down -v. name: winebot-layered services: + # Optional-startup controller: second flock domain serializes the *entire* + # Compose bootstrap before state-init, through runtime writer admission. + # Isolated disposable controller, no network or access to other state. + deployment-gate: + image: ${WINEBOT_APP_BUNDLE_IMAGE:?pin application pack to sha256 digest} + entrypoint: ["/bin/sh", "/usr/local/bin/winebot-deployment-gate"] + user: "0:0" + network_mode: none + read_only: true + cap_drop: [ALL] + cap_add: [DAC_OVERRIDE] + security_opt: [no-new-privileges:true] + pids_limit: 32 + mem_limit: 128m + tmpfs: + - /run:rw,nosuid,nodev,mode=0700,size=1m + volumes: + - type: volume + source: artifacts + target: /state/artifacts + volume: + nocopy: true + healthcheck: + test: ["CMD", "test", "-f", "/run/winebot-deployment-ready"] + interval: 1s + timeout: 2s + retries: 15 + start_period: 1s + restart: "no" + app-seed: image: ${WINEBOT_APP_BUNDLE_IMAGE:?pin application pack to sha256 digest} user: "0:0" @@ -17,6 +47,9 @@ services: restart: "no" state-init: + depends_on: + deployment-gate: + condition: service_healthy image: ${WINEBOT_APP_BUNDLE_IMAGE:?pin application pack to sha256 digest} entrypoint: ["/bin/sh", "/usr/local/bin/winebot-prepare-state"] user: "0:0" @@ -53,6 +86,8 @@ services: - /run:rw,nosuid,nodev,mode=0755,size=64m - /var/tmp:rw,nosuid,nodev,mode=1777,size=64m depends_on: + deployment-gate: + condition: service_healthy app-seed: condition: service_completed_successfully state-init: diff --git a/docker/app-bundle.Dockerfile b/docker/app-bundle.Dockerfile index 784a94d4..5f083b98 100644 --- a/docker/app-bundle.Dockerfile +++ b/docker/app-bundle.Dockerfile @@ -6,8 +6,9 @@ FROM ${APP_PACK_BUILDER_IMAGE} COPY apps/ /opt/winebot/app-payload/ COPY scripts/layers/seed-apps.sh /usr/local/bin/winebot-seed-apps COPY scripts/layers/prepare-state.sh /usr/local/bin/winebot-prepare-state +COPY scripts/layers/deployment-gate.sh /usr/local/bin/winebot-deployment-gate RUN set -eu; \ - chmod 0555 /usr/local/bin/winebot-seed-apps /usr/local/bin/winebot-prepare-state; \ + chmod 0555 /usr/local/bin/winebot-seed-apps /usr/local/bin/winebot-prepare-state /usr/local/bin/winebot-deployment-gate; \ command -v flock >/dev/null 2>&1; \ test -d /opt/winebot/app-payload; \ test -z "$(find /opt/winebot/app-payload -type l -print -quit)"; \ diff --git a/formal/LAYERED_DEPLOYMENT_GATE.md b/formal/LAYERED_DEPLOYMENT_GATE.md new file mode 100644 index 00000000..5306690b --- /dev/null +++ b/formal/LAYERED_DEPLOYMENT_GATE.md @@ -0,0 +1,66 @@ +# Layered deployment-gate R1 — cooperative same-host admission + +## Why two separate locks + +WineBot's runtime keeps `/artifacts/sessions/.winebot-active-writer.lock` +throughout its process lifetime (one admitted state writer). The one-shot +`state-init` now uses this same runtime lock, but releases it before the +Compose runtime can acquire it. That gap permits a second stack to initialize +state and win a race to start. We do **not** claim a seamless transfer of the +runtime lock FD across containers. + +An **independent deployment gate** instead takes an exclusive, nonblocking +flock on `/state/artifacts/sessions/.winebot-deployment-gate.lock` and +**retains the open file description** for the entire lifetime of a small, +isolated OCI service. Both `state-init` and `winebot-interactive` depend +on the gate's health, which is only declared after lock admission. As the +controller stays alive across service startup, a second conforming Compose +project with the same external artifacts volume cannot start its own +`state-init` even in the gap between initial setup and WineBot runtime lock. + +## State machine / linearization points + +`Absent → GateHeld → StateInit → RuntimeAdmission → Running → Stopped`. + +- The gate's successful kernel `flock` is the deployment-serialization + point for compliant Compose projects on one Linux host, one local mount. +- Only after the gate is healthy can init change volume owner metadata. +- Init separately takes the *runtime writer lock* before mutations; its exit + releases that short-lived lock. +- Runtime obtains the same runtime writer lock **while gate remains held**. +- Rival deployment gate rejects immediately with `lock_contended` and its + dependent init is not eligible to start. +- Runtime stays guarded by its runtime writer lock even if the deployment + gate later dies. A different gate may start, but its state-init will then + fail to acquire the runtime writer lock. +- Gate restart uses a persisted lock inode but never unlinks it. Kernel + process exit releases the OFD, requiring fresh admission. + +## Threat/operational limits — release BLOCKED + +This is cooperative same-host Linux flock behavior, *not* a Byzantine +filesystem or consensus algorithm. It requires all mutating deployments to +use this Compose dependency graph and both locking components. Operators +using `docker run` without the controller, malicious same-UID writers, +a privileged host attacker, compromised/multi-host NFS, independent +uncoordinated orchestrators, and state owners pointing at mismatched +external mounts are out of model. Filesystem path identity + OCI digest +attestation still need host-level qualification. Deployment gate currently +runs root with cap DAC_OVERRIDE and artifacts RW; security review is a +specific release gate. Controller health is admission evidence, not an +authenticated long-lived lease or an atomic cross-layer mutation. + +Persistent-owner pointer CAS is **not yet wired** to restart flow. A passing +two-gate race test does not imply such pointer activation, human STOP +priority, native Windows/WinBot equivalence, WinInspect v0.5 acceptance, +or promotion authorization. + +## Qualification + +`scripts/ci/qualify-disposable-layers.sh` tests real isolated gates +competitively on synthetic Docker external volumes, tests release and +reacquire and existing state replay. Full `wine11-disposable-layer-hil.yml` +tests two distinct Compose projects with a real source-built Wine11 runtime, +then existing competing initializer, extended soak, WM recovery, SIGKILL +and durable user-state replay. Both are public disposable CI only; no +real host state or private data. Never merge on source-only green. diff --git a/scripts/ci/qualify-disposable-layers.sh b/scripts/ci/qualify-disposable-layers.sh index 35aae96f..c513e4cc 100755 --- a/scripts/ci/qualify-disposable-layers.sh +++ b/scripts/ci/qualify-disposable-layers.sh @@ -12,7 +12,11 @@ fail() { [[ "${GITHUB_RUN_ATTEMPT:-}" =~ ^[0-9]+$ ]] || fail invalid_identity prefix="winebot-ci-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}" volumes=() +deployment_gate_cid="" cleanup() { + if [ -n "$deployment_gate_cid" ]; then + docker rm -f "$deployment_gate_cid" >/dev/null 2>&1 || true + fi for v in "${volumes[@]}"; do docker volume rm "$v" >/dev/null 2>&1 || true done @@ -75,6 +79,34 @@ docker run --rm --network none --read-only --cap-drop ALL \ --security-opt no-new-privileges -v "$WINEBOT_APP_VOLUME:/winebot-apps" \ "$app_image" >/dev/null 2>&1 || fail reverified_app +# Hold the cross-container deployment lease while first and replay state +# initializers complete; the lock must reject a second independent process. +deployment_gate_cid="$(docker run -d --network none --read-only --cap-drop ALL \ + --cap-add DAC_OVERRIDE --security-opt no-new-privileges \ + --memory 128m --pids-limit 32 --tmpfs /run:rw,nosuid,nodev,mode=0700 \ + --entrypoint /bin/sh \ + -v "$WINEBOT_ARTIFACT_VOLUME:/state/artifacts" \ + "$app_image" /usr/local/bin/winebot-deployment-gate 2>/dev/null)" || + fail deployment_gate_start +docker exec "$deployment_gate_cid" test -f /run/winebot-deployment-ready \ + >/dev/null 2>&1 || fail deployment_gate_not_ready +gate_log="$(mktemp)" +if docker run --rm --network none --read-only --cap-drop ALL \ + --cap-add DAC_OVERRIDE --security-opt no-new-privileges \ + --memory 128m --pids-limit 32 --tmpfs /run:rw,nosuid,nodev,mode=0700 \ + --entrypoint /bin/sh \ + -v "$WINEBOT_ARTIFACT_VOLUME:/state/artifacts" \ + "$app_image" /usr/local/bin/winebot-deployment-gate >"$gate_log" 2>&1; then + rm -f "$gate_log" + fail competing_gate_admitted +fi +if ! grep -Fxq 'DEPLOYMENT_GATE stage=lock_contended' "$gate_log"; then + rm -f "$gate_log" + fail competing_gate_unclassified +fi +rm -f "$gate_log" +echo 'LAYER_RDTE stage=competing_gate_rejected' + # One-shot root task prepares only volume root ownership, never user contents. docker run --rm --network none --read-only --cap-drop ALL --cap-add CHOWN --cap-add DAC_OVERRIDE \ --security-opt no-new-privileges --memory 256m --pids-limit 64 \ @@ -132,6 +164,20 @@ docker run --rm --network none --read-only --user 1000:1000 --cap-drop ALL \ test ! -e /tmp/not-retained ' >/dev/null 2>&1 || fail state_replay +# Prove the separate gate OFD is released when its holder exits. A new +# identical gate must then successfully acquire it (synthetic volumes only). +docker rm -f "$deployment_gate_cid" >/dev/null 2>&1 || fail gate_stop +deployment_gate_cid="$(docker run -d --network none --read-only --cap-drop ALL \ + --cap-add DAC_OVERRIDE --security-opt no-new-privileges \ + --memory 128m --pids-limit 32 --tmpfs /run:rw,nosuid,nodev,mode=0700 \ + --entrypoint /bin/sh \ + -v "$WINEBOT_ARTIFACT_VOLUME:/state/artifacts" \ + "$app_image" /usr/local/bin/winebot-deployment-gate 2>/dev/null)" || + fail gate_restart +docker exec "$deployment_gate_cid" test -f /run/winebot-deployment-ready \ + >/dev/null 2>&1 || fail gate_restart_not_ready +echo 'LAYER_RDTE stage=gate_release_reacquire_passed' + # A different app pack must refuse the old versioned volume without mutation. printf 'app-fixture-v2\n' > apps/ci-fixture/version.txt docker build -f docker/app-bundle.Dockerfile -t winebot-apps:fixture-v2 . >/dev/null 2>&1 || diff --git a/scripts/ci/qualify-wine11-layered.sh b/scripts/ci/qualify-wine11-layered.sh index 331f8398..4b3f1d1c 100755 --- a/scripts/ci/qualify-wine11-layered.sh +++ b/scripts/ci/qualify-wine11-layered.sh @@ -28,6 +28,7 @@ volumes=("$WINEBOT_APP_VOLUME" "$WINEBOT_PREFIX_VOLUME" "$WINEBOT_HOME_VOLUME" "$WINEBOT_CONFIG_VOLUME" "$WINEBOT_USERDATA_VOLUME" "$WINEBOT_ARTIFACT_VOLUME" "$WINEBOT_SHARED_VOLUME") cleanup() { + docker compose -p "$prefix-challenger" -f compose/layered.yml down --remove-orphans >/dev/null 2>&1 || true docker compose -f compose/layered.yml down --remove-orphans >/dev/null 2>&1 || true for volume in "${volumes[@]}"; do docker volume rm "$volume" >/dev/null 2>&1 || true @@ -191,6 +192,25 @@ docker compose -f compose/layered.yml exec -T winebot-interactive /bin/sh -ec ' if touch /opt/winebot/unauthorized-root-write >/dev/null 2>&1; then exit 14; fi ' >/dev/null 2>&1 || fail state_write_or_readonly +# A *different* Compose project with the same external persistent volumes +# cannot advance its initializer while the first deployment gate is healthy. +# This verifies the real dependency graph (not only a no-deps helper). +if docker compose -p "$prefix-challenger" -f compose/layered.yml \ + up --no-build -d state-init >/dev/null 2>&1; then + fail competing_deployment_admitted +fi +challenger_init="$(docker compose -p "$prefix-challenger" -f compose/layered.yml \ + ps -a -q state-init 2>/dev/null || true)" +if [ -n "$challenger_init" ]; then + challenger_state="$(docker inspect --format '{{.State.Status}}' "$challenger_init" 2>/dev/null || true)" + case "$challenger_state" in + running) fail competing_initializer_running ;; + esac +fi +docker compose -p "$prefix-challenger" -f compose/layered.yml down --remove-orphans \ + >/dev/null 2>&1 || fail competing_deployment_cleanup +echo 'WINE11_LAYER_HIL stage=competing_deployment_rejected' + # Another Compose project using the same external volumes may NOT # initialize persistent state while an admitted WineBot runtime is alive. # Only fixed status/marker is published; raw Compose/container text stays diff --git a/scripts/layers/deployment-gate.sh b/scripts/layers/deployment-gate.sh new file mode 100755 index 00000000..0ea153ff --- /dev/null +++ b/scripts/layers/deployment-gate.sh @@ -0,0 +1,40 @@ +#!/bin/sh +# Hold a *separate* host-local, cooperative deployment admission until Compose +# stops this container. This spans state-init's release of runtime-writer flock. +# NOT distributed consensus, a hostile same-UID defense, or a privilege boundary. +set -eu +fail() { + printf 'DEPLOYMENT_GATE stage=%s\n' "$1" >&2 + exit 73 +} +[ "$(id -u)" = 0 ] || fail invalid_identity +command -v flock >/dev/null 2>&1 || fail lock_unavailable +parent=/state/artifacts +root="$parent/sessions" +lock="$root/.winebot-deployment-gate.lock" +[ -d "$parent" ] && [ ! -L "$parent" ] || fail mount_unavailable +if [ ! -d "$root" ]; then + [ ! -e "$root" ] && [ ! -L "$root" ] || fail lock_identity_invalid + mkdir -m 0700 "$root" >/dev/null 2>&1 || fail lock_unavailable +fi +[ -d "$root" ] && [ ! -L "$root" ] || fail lock_identity_invalid +if [ ! -e "$lock" ]; then + [ ! -L "$lock" ] || fail lock_identity_invalid + (umask 077; : > "$lock") 2>/dev/null || fail lock_unavailable +fi +[ -f "$lock" ] && [ ! -L "$lock" ] || fail lock_identity_invalid +[ "$(stat -c '%u:%g:%a:%h' "$lock" 2>/dev/null)" = 0:0:600:1 ] || + fail lock_identity_invalid +exec 9<>"$lock" || fail lock_unavailable +[ "$(stat -c '%d:%i' "$lock" 2>/dev/null)" = "$(stat -Lc '%d:%i' /proc/self/fd/9)" ] || + fail lock_identity_invalid +flock -n -x 9 >/dev/null 2>&1 || fail lock_contended +[ ! -L "$lock" ] && + [ "$(stat -c '%d:%i' "$lock")" = "$(stat -Lc '%d:%i' /proc/self/fd/9)" ] || + fail lock_identity_invalid +# Probe before claiming healthy; readiness only after the exclusive lease. +: > /run/winebot-deployment-ready || fail readiness_unavailable +echo 'DEPLOYMENT_GATE stage=admitted' +# PID1 retains the same exact OFD across exec; SIGTERM/SIGKILL releases it. +# A separate runtime writer lock remains necessary after gate loss. +exec sleep infinity diff --git a/tests/test_disposable_layer_contract.py b/tests/test_disposable_layer_contract.py index ba6ca8e8..53e0848c 100644 --- a/tests/test_disposable_layer_contract.py +++ b/tests/test_disposable_layer_contract.py @@ -51,13 +51,32 @@ def test_external_volumes_and_disposable_runtime_are_enforced(self): self.assertIn("read_only: true", source) # Every runtime state/app volume refuses Docker image copy-up, or # Compose may initialize/chown it before one-shot seed/state services. - self.assertEqual(source.count("nocopy: true"), 7) + self.assertEqual(source.count("nocopy: true"), 8) self.assertIn("WINEBOT_STATE_MODEL: layered", source) self.assertIn("WINEBOT_INPUT_TRACE_WINDOWS: \"1\"", source) self.assertIn("127.0.0.1:8000:8000", source) self.assertNotIn(" build:", source) self.assertNotIn("actions/upload-artifact@", source) + def test_deployment_gate_spans_state_initializer_to_runtime_admission(self): + compose = (ROOT / "compose/layered.yml").read_text() + gate = (ROOT / "scripts/layers/deployment-gate.sh").read_text() + image = (ROOT / "docker/app-bundle.Dockerfile").read_text() + fixture = (ROOT / "scripts/ci/qualify-disposable-layers.sh").read_text() + hil = (ROOT / "scripts/ci/qualify-wine11-layered.sh").read_text() + self.assertIn(" deployment-gate:", compose) + self.assertEqual(compose.count("condition: service_healthy"), 2) + self.assertIn("winebot-deployment-gate", image) + self.assertIn("flock -n -x 9", gate) + self.assertLess(gate.index("flock -n -x 9"), + gate.index("winebot-deployment-ready")) + self.assertIn("exec sleep infinity", gate) + self.assertIn("stage=competing_gate_rejected", fixture) + self.assertIn("stage=gate_release_reacquire_passed", fixture) + self.assertIn("stage=competing_deployment_rejected", hil) + self.assertIn('up --no-build -d state-init', hil) + self.assertNotIn("docker volume rm", gate) + def test_layered_api_is_enabled_without_unauthenticated_fallback(self): compose = (ROOT / "compose/layered.yml").read_text(encoding="utf-8") start = (ROOT / "scripts/init/30-start-services.sh").read_text(