From 6237f9e99a4330e862c70783c006d516e4a7784f Mon Sep 17 00:00:00 2001 From: James Ross Date: Sun, 8 Nov 2026 04:47:21 -0800 Subject: [PATCH 1/9] Build the shared Rust base image flyingrobots/rust-dev images/rust-dev/Dockerfile builds from the rust image pinned by index digest in images/rust-dev/base-digests (Rust 1.96.0). It adds clippy, rustfmt, the wasm32-unknown-unknown target, and cargo-deny 0.20.2 from its release binary checked against the published SHA-256 for each architecture. It runs as UID 10001, declares no VOLUME, carries the owner and role labels, and prepares /scratch, /target, and /cargo-registry so new named volumes are writable by that user. scripts/build-image builds under the workstation git-locks keys host/heavy-work and host/docker/devcontainers-dev/ with a 30-minute timeout; on a CI runner it builds without the lock. tests/smoke-image checks the labels, user, empty Config.Volumes, toolchain versions, target, and cargo-deny in an offline, read-only, unprivileged container. Local evidence (arm64, OrbStack): build exit 0; smoke test PASS (rustc 1.96.0, clippy 0.1.96, rustfmt 1.9.0, wasm32 target, cargo-deny 0.20.2, uid 10001). A before/after Docker inventory showed no new container, volume, or network from these runs. Closes #2 --- .github/workflows/ci.yml | 23 +++++++++++++++ images/rust-dev/Dockerfile | 54 ++++++++++++++++++++++++++++++++++++ images/rust-dev/base-digests | 3 ++ scripts/build-image | 29 +++++++++++++++++++ tests/smoke-image | 26 +++++++++++++++++ 5 files changed, 135 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 images/rust-dev/Dockerfile create mode 100644 images/rust-dev/base-digests create mode 100755 scripts/build-image create mode 100755 tests/smoke-image diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..8bcfecd --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,23 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + +permissions: + contents: read + +jobs: + image: + name: Build and smoke-test rust-dev + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - uses: actions/checkout@v4 + - name: Shellcheck + run: shellcheck scripts/* tests/* + - name: Build flyingrobots/rust-dev:1.96.0 + run: scripts/build-image 1.96.0 + - name: Smoke test + run: tests/smoke-image 1.96.0 diff --git a/images/rust-dev/Dockerfile b/images/rust-dev/Dockerfile new file mode 100644 index 0000000..43b5df4 --- /dev/null +++ b/images/rust-dev/Dockerfile @@ -0,0 +1,54 @@ +# Shared Rust development image for the flyingrobots repositories. +# Rules: docs/hygiene.md. Build only with scripts/build-image, which passes a digest-pinned base. +ARG BASE_IMAGE=rust@sha256:58fe97504a0e4cbba5d85599619a589923d3e779472a6fb0840d58d1c4ba99d7 +FROM ${BASE_IMAGE} + +ARG RUST_VERSION +ARG CARGO_DENY_VERSION=0.20.2 +ARG CARGO_DENY_SHA256_AARCH64=995c82be0defc7a025cae49a2aa2644ce8245c9a3318fc4103907c6a285e8c7d +ARG CARGO_DENY_SHA256_X86_64=9f12ed4c49936e09b48bf862b595cde2fe64fcbd9d74dfacac6131ca824c8d5f +ARG TARGETARCH + +LABEL org.flyingrobots.owner="devcontainers" \ + org.flyingrobots.role="base-image" \ + org.flyingrobots.rust-version="${RUST_VERSION}" \ + org.opencontainers.image.source="https://github.com/flyingrobots/devcontainers" + +# The base image must already carry the requested toolchain. +RUN test "$(rustc --version | cut -d' ' -f2)" = "${RUST_VERSION}" + +RUN rustup component add clippy rustfmt \ + && rustup target add wasm32-unknown-unknown \ + && rustup default "${RUST_VERSION}" + +RUN set -eu; \ + case "${TARGETARCH}" in \ + arm64) triple=aarch64-unknown-linux-musl; sum="${CARGO_DENY_SHA256_AARCH64}" ;; \ + amd64) triple=x86_64-unknown-linux-musl; sum="${CARGO_DENY_SHA256_X86_64}" ;; \ + *) echo "unsupported TARGETARCH ${TARGETARCH}" >&2; exit 1 ;; \ + esac; \ + name="cargo-deny-${CARGO_DENY_VERSION}-${triple}"; \ + curl -fsSL -o /tmp/deny.tgz "https://github.com/EmbarkStudios/cargo-deny/releases/download/${CARGO_DENY_VERSION}/${name}.tar.gz"; \ + echo "${sum} /tmp/deny.tgz" | sha256sum -c -; \ + tar -xzf /tmp/deny.tgz -C /tmp; \ + install -m 0755 "/tmp/${name}/cargo-deny" /usr/local/bin/cargo-deny; \ + rm -rf /tmp/deny.tgz "/tmp/${name}" + +# Unprivileged user and the mount points for the named volumes (docs/hygiene.md section 5). +# New named volumes copy this ownership, so the user can write to them. +RUN groupadd --gid 10001 dev \ + && useradd --uid 10001 --gid 10001 --no-create-home --home-dir /tmp/home --shell /usr/sbin/nologin dev \ + && mkdir -p /scratch /target /cargo-registry \ + && chown 10001:10001 /scratch /target /cargo-registry \ + && chmod -R go-w /usr/local/rustup /usr/local/cargo + +ENV RUSTUP_TOOLCHAIN=${RUST_VERSION} \ + CARGO_HOME=/cargo-registry \ + CARGO_TARGET_DIR=/target \ + CARGO_INCREMENTAL=0 \ + HOME=/tmp/home \ + GIT_CONFIG_NOSYSTEM=1 \ + PATH=/usr/local/cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + +USER 10001:10001 +WORKDIR /scratch/work diff --git a/images/rust-dev/base-digests b/images/rust-dev/base-digests new file mode 100644 index 0000000..24b0b76 --- /dev/null +++ b/images/rust-dev/base-digests @@ -0,0 +1,3 @@ +# Rust version, then the pinned rust image index digest (multi-arch). +# Update a line only on purpose, with the new digest from `docker buildx imagetools inspect rust:`. +1.96.0 rust@sha256:58fe97504a0e4cbba5d85599619a589923d3e779472a6fb0840d58d1c4ba99d7 diff --git a/scripts/build-image b/scripts/build-image new file mode 100755 index 0000000..a432a92 --- /dev/null +++ b/scripts/build-image @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# Build flyingrobots/rust-dev: from the digest pinned in images/rust-dev/base-digests. +# Runs under the workstation git-locks keys for this repository's Docker worker. +set -euo pipefail +version="${1:?usage: scripts/build-image }" +root="$(cd "$(dirname "$0")/.." && pwd)" +base="$(awk -v v="$version" '$1 == v { print $2 }' "$root/images/rust-dev/base-digests")" +if [[ -z "$base" ]]; then + echo "build-image: no pinned digest for Rust $version in images/rust-dev/base-digests" >&2 + exit 2 +fi +tag="flyingrobots/rust-dev:$version" +build=(docker build + --file "$root/images/rust-dev/Dockerfile" + --build-arg "BASE_IMAGE=$base" + --build-arg "RUST_VERSION=$version" + --label "org.flyingrobots.base-digest=$base" + --tag "$tag" + "$root/images/rust-dev") +# CI runners are single-use machines with no shared git-locks store. +if [[ "${CI:-}" == "true" ]]; then + exec "${build[@]}" +fi +store="${GIT_LOCKS_STORE:-/Users/Shared/git-locks/workstation.git}" +exec env GIT_LOCKS_STORE="$store" git locks with \ + --job "devcontainers-build-$(uuidgen)" --holder "${USER:-agent}-build-image" \ + --ttl 2400 --wait 600 --note "build $tag" \ + host/heavy-work host/docker/devcontainers-dev/ -- \ + perl -e 'alarm 1800; exec @ARGV' "${build[@]}" diff --git a/tests/smoke-image b/tests/smoke-image new file mode 100755 index 0000000..948ec8a --- /dev/null +++ b/tests/smoke-image @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# Smoke test for flyingrobots/rust-dev:. Runs one locked-down, labeled, offline container. +set -euo pipefail +version="${1:?usage: tests/smoke-image }" +image="flyingrobots/rust-dev:$version" +fail() { echo "smoke-image: FAIL: $*" >&2; exit 1; } + +[[ "$(docker image inspect "$image" --format '{{json .Config.Volumes}}')" == "null" ]] || fail "image declares VOLUME" +[[ "$(docker image inspect "$image" --format '{{index .Config.Labels "org.flyingrobots.owner"}}')" == "devcontainers" ]] || fail "owner label missing" +[[ "$(docker image inspect "$image" --format '{{index .Config.Labels "org.flyingrobots.role"}}')" == "base-image" ]] || fail "role label missing" +[[ "$(docker image inspect "$image" --format '{{.Config.User}}')" == "10001:10001" ]] || fail "image does not run as 10001" + +out="$(docker run --rm --name devcontainers-smoke-run \ + --label org.flyingrobots.owner=devcontainers --label org.flyingrobots.role=dev-run \ + --network none --read-only --tmpfs /tmp:rw,size=64m,mode=1777 \ + --cap-drop ALL --security-opt no-new-privileges --pids-limit 256 --memory 1g --memory-swap 1g --cpus 1 \ + "$image" sh -c 'rustc --version; cargo --version; cargo clippy --version; rustfmt --version; rustup target list --installed; cargo deny --version; id -u; git --version')" +echo "$out" +grep -q "^rustc $version " <<<"$out" || fail "rustc is not $version" +grep -q '^clippy ' <<<"$out" || fail "clippy missing" +grep -q '^rustfmt ' <<<"$out" || fail "rustfmt missing" +grep -q '^wasm32-unknown-unknown$' <<<"$out" || fail "wasm32 target missing" +grep -q '^cargo-deny 0.20.2$' <<<"$out" || fail "cargo-deny 0.20.2 missing" +grep -qx '10001' <<<"$out" || fail "container does not run as uid 10001" +docker ps -a --filter name=^devcontainers-smoke-run$ --format '{{.Names}}' | grep -q . && fail "smoke container was not removed" +echo "smoke-image: PASS ($image)" From fe3d3cd8730ec9f381f2020c976661a7c32e1455 Mon Sep 17 00:00:00 2001 From: James Ross Date: Sun, 8 Nov 2026 04:56:37 -0800 Subject: [PATCH 2/9] Address review findings on the base image - Drop RUSTUP_TOOLCHAIN: a repository's rust-toolchain.toml must match the image, or the run fails offline (the smoke test checks this). - Keep CARGO_HOME at the read-only image path; only the registry and git caches are volume mount points, so no run can plant a cargo config. - Drop chmod -R go-w: under a read-only root it added little and copied the whole toolchain into a second layer (image 3.11 GB -> 2.38 GB). - Default RUST_VERSION to 1.96.0; comment every non-obvious choice. - build-image: re-exec under the git-locks keys before any container runs, refuse to start below 50 GiB free on the host or in the Docker VM, keep a timeout in CI, detect CI with GITHUB_ACTIONS, and build without provenance/SBOM attestations so an unchanged rebuild keeps its image ID. - smoke-image: run under the lock, name each container devcontainers-dev-run, use create/start/rm with an EXIT trap, add --user, --ulimit nofile, CARGO_NET_OFFLINE, and a timeout, and assert the cargo, clippy, and rustfmt versions. - CI: pin actions/checkout by SHA and rebuild once to check the image ID. - hygiene.md and README: align paths and volumes with the image, record the remaining unpinned rustup component download, and add usage. Local evidence (arm64): two consecutive builds gave the same image ID (sha256:eefd78343ac9...); smoke test PASS. --- .github/workflows/ci.yml | 13 +++++-- README.md | 16 +++++++++ docs/hygiene.md | 18 ++++++++-- images/rust-dev/Dockerfile | 42 +++++++++++++--------- scripts/build-image | 42 ++++++++++++++++------ tests/smoke-image | 73 ++++++++++++++++++++++++++++++-------- 6 files changed, 158 insertions(+), 46 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8bcfecd..f13c9d8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,12 +12,19 @@ jobs: image: name: Build and smoke-test rust-dev runs-on: ubuntu-latest - timeout-minutes: 30 + timeout-minutes: 45 steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 - name: Shellcheck - run: shellcheck scripts/* tests/* + run: shellcheck scripts/* tests/smoke-image - name: Build flyingrobots/rust-dev:1.96.0 run: scripts/build-image 1.96.0 - name: Smoke test run: tests/smoke-image 1.96.0 + - name: Unchanged rebuild reuses the cache (same image ID) + run: | + first="$(docker image inspect -f '{{.Id}}' flyingrobots/rust-dev:1.96.0)" + scripts/build-image 1.96.0 + second="$(docker image inspect -f '{{.Id}}' flyingrobots/rust-dev:1.96.0)" + echo "first=$first second=$second" + test "$first" = "$second" diff --git a/README.md b/README.md index dc64760..d6e3b36 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,19 @@ containers, never on the host. The rules are in [`docs/hygiene.md`](docs/hygiene.md). Work is tracked in GitHub Issues with `issue-dag/2` frontmatter. + +## Build and test the base image + +```sh +scripts/build-image 1.96.0 # builds flyingrobots/rust-dev:1.96.0 under the git-locks keys +tests/smoke-image 1.96.0 # checks it in offline, read-only, unprivileged containers +``` + +Pinned base digests live in `images/rust-dev/base-digests`, one line per Rust +version. The image runs as UID 10001. Mount points for the named volumes: +`/scratch` (copy, logs, exports), `/target` (build output), +`/usr/local/cargo/registry`, and `/usr/local/cargo/git` (cargo caches). + +The guarded runner `bin/devrun` is not here yet (flyingrobots/devcontainers#3 +and #5). Until it lands, these two scripts are the only supported way to use +Docker from this repository. diff --git a/docs/hygiene.md b/docs/hygiene.md index dd1d4a9..977fc5b 100644 --- a/docs/hygiene.md +++ b/docs/hygiene.md @@ -40,9 +40,13 @@ enforces every rule here; a rule that only lives in this file is not enforced. - Non-root user with a fixed UID (`10001`), `--user 10001:10001`. - `--cap-drop ALL`, `--security-opt no-new-privileges`. -- `--read-only` root filesystem. Writable places: `/work` (the copy, in a tmpfs - or the scratch volume), `/tmp` (tmpfs with a size limit), and the named +- `--read-only` root filesystem. Writable places: `/scratch/work` (the copy, + inside the scratch volume), `/tmp` (tmpfs with a size limit), and the named volumes in section 5. +- `CARGO_HOME` is the image path `/usr/local/cargo`, which is read-only at run + time, so no run can plant a cargo `config.toml` or a `credentials.toml`. +- No `RUSTUP_TOOLCHAIN` in the image or the run: a repository's + `rust-toolchain.toml` must match the image, or the run fails offline. - Limits on every run: `--pids-limit`, `--memory` with `--memory-swap` equal to it, `--cpus`, `--ulimit nofile`. - Never `--privileged`, `--network host`, `--pid host`, `--ipc host`, or added @@ -64,6 +68,10 @@ enforces every rule here; a rule that only lives in this file is not enforced. - Base images are pinned by digest (`rust@sha256:…`), not by tag. The digest is in the receipt. +- Known gap: `clippy`, `rustfmt`, and the `wasm32-unknown-unknown` standard + library come from the rustup server at image build time. rustup checks each + file against the channel manifest for that exact version, but the manifest + itself is not pinned here. `cargo-deny` is pinned by SHA-256. - Every cargo command uses `--locked`. A changed `Cargo.lock` leaves the container only through an explicit patch export. - Images build with the default builder. No new `buildx` builder. A new image is @@ -78,12 +86,16 @@ and `org.flyingrobots.role=`. Cleanup selects by these labels only. | --- | --- | --- | | Base image | `flyingrobots/rust-dev:` | `base-image` | | Run container | `-dev-run` (one at a time, under a lock) | `dev-run` | -| Cargo registry cache | `-cargo-registry` (per project: no cross-project cache poisoning) | `cargo-registry` | +| Cargo registry cache, at `/usr/local/cargo/registry` | `-cargo-registry` (per project: no cross-project cache poisoning) | `cargo-registry` | +| Cargo git cache, at `/usr/local/cargo/git` | `-cargo-git` | `cargo-git` | | Build output, iteration | `-target` | `target` | | Build output, evidence | `-target-evidence`, emptied before each merge-gate run | `target-evidence` | | Logs, test data, exports | `-scratch` | `scratch` | - No anonymous volumes. Images must not declare `VOLUME`. +- The cargo caches are writable only in the fetch phase. The build phase mounts + them read-only, so a hostile build script cannot poison the cache for a later + run. - **Evidence runs use a fresh target.** A merge-gate run never reuses build output, so a stale cache cannot fabricate a passing result. diff --git a/images/rust-dev/Dockerfile b/images/rust-dev/Dockerfile index 43b5df4..63519ed 100644 --- a/images/rust-dev/Dockerfile +++ b/images/rust-dev/Dockerfile @@ -1,12 +1,18 @@ # Shared Rust development image for the flyingrobots repositories. -# Rules: docs/hygiene.md. Build only with scripts/build-image, which passes a digest-pinned base. +# Rules: docs/hygiene.md. Build with scripts/build-image, which passes the digest from base-digests. + +# Fallback only. scripts/build-image always passes BASE_IMAGE from images/rust-dev/base-digests; +# keep this line equal to the 1.96.0 entry there. ARG BASE_IMAGE=rust@sha256:58fe97504a0e4cbba5d85599619a589923d3e779472a6fb0840d58d1c4ba99d7 FROM ${BASE_IMAGE} -ARG RUST_VERSION +ARG RUST_VERSION=1.96.0 +# Published checksums: https://github.com/EmbarkStudios/cargo-deny/releases/download// +# cargo-deny--.tar.gz.sha256 ARG CARGO_DENY_VERSION=0.20.2 ARG CARGO_DENY_SHA256_AARCH64=995c82be0defc7a025cae49a2aa2644ce8245c9a3318fc4103907c6a285e8c7d ARG CARGO_DENY_SHA256_X86_64=9f12ed4c49936e09b48bf862b595cde2fe64fcbd9d74dfacac6131ca824c8d5f +# Set by BuildKit (the default builder). An empty value fails the build below. ARG TARGETARCH LABEL org.flyingrobots.owner="devcontainers" \ @@ -14,18 +20,20 @@ LABEL org.flyingrobots.owner="devcontainers" \ org.flyingrobots.rust-version="${RUST_VERSION}" \ org.opencontainers.image.source="https://github.com/flyingrobots/devcontainers" -# The base image must already carry the requested toolchain. +# The pinned base must carry the requested toolchain. RUN test "$(rustc --version | cut -d' ' -f2)" = "${RUST_VERSION}" +# Known gap (docs/hygiene.md section 4): these components come from the rustup server at build +# time. rustup checks each one against the channel manifest for this exact version; the manifest +# itself is not pinned in this repository. RUN rustup component add clippy rustfmt \ - && rustup target add wasm32-unknown-unknown \ - && rustup default "${RUST_VERSION}" + && rustup target add wasm32-unknown-unknown RUN set -eu; \ case "${TARGETARCH}" in \ arm64) triple=aarch64-unknown-linux-musl; sum="${CARGO_DENY_SHA256_AARCH64}" ;; \ amd64) triple=x86_64-unknown-linux-musl; sum="${CARGO_DENY_SHA256_X86_64}" ;; \ - *) echo "unsupported TARGETARCH ${TARGETARCH}" >&2; exit 1 ;; \ + *) echo "unsupported or empty TARGETARCH '${TARGETARCH}' (build with BuildKit)" >&2; exit 1 ;; \ esac; \ name="cargo-deny-${CARGO_DENY_VERSION}-${triple}"; \ curl -fsSL -o /tmp/deny.tgz "https://github.com/EmbarkStudios/cargo-deny/releases/download/${CARGO_DENY_VERSION}/${name}.tar.gz"; \ @@ -34,21 +42,23 @@ RUN set -eu; \ install -m 0755 "/tmp/${name}/cargo-deny" /usr/local/bin/cargo-deny; \ rm -rf /tmp/deny.tgz "/tmp/${name}" -# Unprivileged user and the mount points for the named volumes (docs/hygiene.md section 5). -# New named volumes copy this ownership, so the user can write to them. +# Unprivileged user (fixed UID per docs/hygiene.md section 2) and the mount points for the named +# volumes (section 5). New named volumes copy the ownership of these directories, so UID 10001 can +# write to them. CARGO_HOME stays at the image path /usr/local/cargo: containers run with a +# read-only root, so no run can plant a cargo config there. Only its registry and git caches are +# volumes. RUN groupadd --gid 10001 dev \ && useradd --uid 10001 --gid 10001 --no-create-home --home-dir /tmp/home --shell /usr/sbin/nologin dev \ - && mkdir -p /scratch /target /cargo-registry \ - && chown 10001:10001 /scratch /target /cargo-registry \ - && chmod -R go-w /usr/local/rustup /usr/local/cargo + && mkdir -p /scratch /target /usr/local/cargo/registry /usr/local/cargo/git \ + && chown 10001:10001 /scratch /target /usr/local/cargo/registry /usr/local/cargo/git -ENV RUSTUP_TOOLCHAIN=${RUST_VERSION} \ - CARGO_HOME=/cargo-registry \ - CARGO_TARGET_DIR=/target \ +# No RUSTUP_TOOLCHAIN: a repository's rust-toolchain.toml must match this image, or the run fails +# offline instead of silently building with another compiler. CARGO_INCREMENTAL=0 keeps audit +# builds free of incremental state. GIT_CONFIG_NOSYSTEM=1 ignores any system git config. +ENV CARGO_TARGET_DIR=/target \ CARGO_INCREMENTAL=0 \ HOME=/tmp/home \ - GIT_CONFIG_NOSYSTEM=1 \ - PATH=/usr/local/cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + GIT_CONFIG_NOSYSTEM=1 USER 10001:10001 WORKDIR /scratch/work diff --git a/scripts/build-image b/scripts/build-image index a432a92..074ab93 100755 --- a/scripts/build-image +++ b/scripts/build-image @@ -1,6 +1,8 @@ #!/usr/bin/env bash # Build flyingrobots/rust-dev: from the digest pinned in images/rust-dev/base-digests. -# Runs under the workstation git-locks keys for this repository's Docker worker. +# On the workstation it runs under the git-locks keys host/heavy-work and host/docker/devcontainers-dev/ +# (docs/hygiene.md section 10), with a 30-minute timeout inside a 40-minute lease (section 7), and it +# refuses to start below 50 GiB free on the host or in the Docker VM (section 8). set -euo pipefail version="${1:?usage: scripts/build-image }" root="$(cd "$(dirname "$0")/.." && pwd)" @@ -15,15 +17,35 @@ build=(docker build --build-arg "BASE_IMAGE=$base" --build-arg "RUST_VERSION=$version" --label "org.flyingrobots.base-digest=$base" + # No provenance or SBOM attestations: they carry a per-build timestamp, so an unchanged rebuild + # would get a new image ID (issue #2 acceptance criterion: same ID on an unchanged rebuild). + --provenance=false --sbom=false --tag "$tag" "$root/images/rust-dev") -# CI runners are single-use machines with no shared git-locks store. -if [[ "${CI:-}" == "true" ]]; then - exec "${build[@]}" +timeout_s=1800 + +# GitHub Actions runners are single-use machines with no shared git-locks store. Keep the timeout. +if [[ "${GITHUB_ACTIONS:-}" == "true" ]]; then + exec timeout "$timeout_s" "${build[@]}" fi -store="${GIT_LOCKS_STORE:-/Users/Shared/git-locks/workstation.git}" -exec env GIT_LOCKS_STORE="$store" git locks with \ - --job "devcontainers-build-$(uuidgen)" --holder "${USER:-agent}-build-image" \ - --ttl 2400 --wait 600 --note "build $tag" \ - host/heavy-work host/docker/devcontainers-dev/ -- \ - perl -e 'alarm 1800; exec @ARGV' "${build[@]}" + +if [[ -z "${DEVCONTAINERS_LOCK_HELD:-}" ]]; then + # Default store: the shared workstation store on this macOS machine. + exec env GIT_LOCKS_STORE="${GIT_LOCKS_STORE:-/Users/Shared/git-locks/workstation.git}" DEVCONTAINERS_LOCK_HELD=1 \ + git locks with --job "devcontainers-build-$(uuidgen)" --holder "${USER:-agent}-build-image" \ + --ttl 2400 --wait 600 --note "build $tag" host/heavy-work host/docker/devcontainers-dev/ -- "$0" "$@" +fi + +min_kib=$((50 * 1024 * 1024)) +host_free="$(df -Pk "$root" | awk 'NR == 2 { print $4 }')" +vm_free="$(docker run --rm --name devcontainers-dev-run --network none \ + --label org.flyingrobots.owner=devcontainers --label org.flyingrobots.role=dev-run \ + --read-only --cap-drop ALL --security-opt no-new-privileges --user 10001:10001 \ + --entrypoint df "$base" -Pk / | awk 'NR == 2 { print $4 }')" +if (( host_free < min_kib || vm_free < min_kib )); then + echo "build-image: refusing to start: less than 50 GiB free (host ${host_free} KiB, Docker VM ${vm_free} KiB)" >&2 + exit 3 +fi + +# perl's alarm is the timeout, because macOS has no coreutils timeout by default. +exec perl -e 'alarm shift; exec @ARGV' "$timeout_s" "${build[@]}" diff --git a/tests/smoke-image b/tests/smoke-image index 948ec8a..bfe81ff 100755 --- a/tests/smoke-image +++ b/tests/smoke-image @@ -1,26 +1,71 @@ #!/usr/bin/env bash -# Smoke test for flyingrobots/rust-dev:. Runs one locked-down, labeled, offline container. +# Smoke test for flyingrobots/rust-dev:. Each check runs in one locked-down, labeled, +# offline container named devcontainers-dev-run (docs/hygiene.md sections 2, 3, 5, 7, 10). +# On the workstation the whole test runs under the same git-locks keys as scripts/build-image. set -euo pipefail version="${1:?usage: tests/smoke-image }" image="flyingrobots/rust-dev:$version" +name=devcontainers-dev-run + +if [[ "${GITHUB_ACTIONS:-}" != "true" && -z "${DEVCONTAINERS_LOCK_HELD:-}" ]]; then + exec env GIT_LOCKS_STORE="${GIT_LOCKS_STORE:-/Users/Shared/git-locks/workstation.git}" DEVCONTAINERS_LOCK_HELD=1 \ + git locks with --job "devcontainers-smoke-$(uuidgen)" --holder "${USER:-agent}-smoke-image" \ + --ttl 900 --wait 600 --note "smoke $image" host/heavy-work host/docker/devcontainers-dev/ -- "$0" "$@" +fi + fail() { echo "smoke-image: FAIL: $*" >&2; exit 1; } +leftover_check() { + status=$? + docker rm -f "$name" >/dev/null 2>&1 || true + if docker ps -a --format '{{.Names}}' | grep -qx "$name"; then + echo "smoke-image: FAIL: $name still exists" >&2 + exit 1 + fi + exit "$status" +} +trap leftover_check EXIT + +# run_in SCRIPT: create, start, and remove one container; print its output. +run_in() { + docker create --name "$name" \ + --label org.flyingrobots.owner=devcontainers --label org.flyingrobots.role=dev-run \ + --user 10001:10001 --network none --read-only --tmpfs /tmp:rw,nosuid,nodev,size=64m,mode=1777 \ + --cap-drop ALL --security-opt no-new-privileges \ + --pids-limit 256 --memory 1g --memory-swap 1g --cpus 1 --ulimit nofile=1024:1024 \ + --env CARGO_NET_OFFLINE=true \ + "$image" bash -c "$1" >/dev/null + local rc=0 + output="$(perl -e 'alarm 120; exec @ARGV' docker start -a "$name")" || rc=$? + docker rm -f "$name" >/dev/null + return "$rc" +} [[ "$(docker image inspect "$image" --format '{{json .Config.Volumes}}')" == "null" ]] || fail "image declares VOLUME" [[ "$(docker image inspect "$image" --format '{{index .Config.Labels "org.flyingrobots.owner"}}')" == "devcontainers" ]] || fail "owner label missing" [[ "$(docker image inspect "$image" --format '{{index .Config.Labels "org.flyingrobots.role"}}')" == "base-image" ]] || fail "role label missing" [[ "$(docker image inspect "$image" --format '{{.Config.User}}')" == "10001:10001" ]] || fail "image does not run as 10001" +docker image inspect "$image" --format '{{range .Config.Env}}{{println .}}{{end}}' | grep -q '^RUSTUP_TOOLCHAIN=' && fail "image sets RUSTUP_TOOLCHAIN" + +run_in 'rustc --version; cargo --version; cargo clippy --version; rustfmt --version; rustup target list --installed; cargo deny --version; id -u; echo "CARGO_HOME=${CARGO_HOME:-unset}"' +echo "$output" +v="${version//./\\.}" +grep -q "^rustc $v " <<<"$output" || fail "rustc is not $version" +grep -q "^cargo $v " <<<"$output" || fail "cargo is not $version" +grep -q "^clippy 0\.1\.$(cut -d. -f2 <<<"$version") " <<<"$output" || fail "clippy does not match $version" +grep -q '^rustfmt [0-9.]*-stable' <<<"$output" || fail "rustfmt missing" +grep -q '^wasm32-unknown-unknown$' <<<"$output" || fail "wasm32 target missing" +grep -q '^cargo-deny 0\.20\.2$' <<<"$output" || fail "cargo-deny 0.20.2 missing" +grep -qx '10001' <<<"$output" || fail "container does not run as uid 10001" +grep -qx 'CARGO_HOME=/usr/local/cargo' <<<"$output" || fail "CARGO_HOME is not the read-only image path" + +# A repository that pins another toolchain must fail offline, not build with this compiler. +if run_in 'mkdir -p /tmp/p && cd /tmp/p && printf "[toolchain]\nchannel = \"1.80.0\"\n" > rust-toolchain.toml && rustc --version'; then + fail "a mismatched rust-toolchain.toml did not fail" +fi +echo "smoke-image: mismatched rust-toolchain.toml fails offline (expected)" + +# The image path for CARGO_HOME is read-only at run time. +if run_in 'touch /usr/local/cargo/config.toml'; then fail "CARGO_HOME is writable"; fi +echo "smoke-image: CARGO_HOME is read-only (expected)" -out="$(docker run --rm --name devcontainers-smoke-run \ - --label org.flyingrobots.owner=devcontainers --label org.flyingrobots.role=dev-run \ - --network none --read-only --tmpfs /tmp:rw,size=64m,mode=1777 \ - --cap-drop ALL --security-opt no-new-privileges --pids-limit 256 --memory 1g --memory-swap 1g --cpus 1 \ - "$image" sh -c 'rustc --version; cargo --version; cargo clippy --version; rustfmt --version; rustup target list --installed; cargo deny --version; id -u; git --version')" -echo "$out" -grep -q "^rustc $version " <<<"$out" || fail "rustc is not $version" -grep -q '^clippy ' <<<"$out" || fail "clippy missing" -grep -q '^rustfmt ' <<<"$out" || fail "rustfmt missing" -grep -q '^wasm32-unknown-unknown$' <<<"$out" || fail "wasm32 target missing" -grep -q '^cargo-deny 0.20.2$' <<<"$out" || fail "cargo-deny 0.20.2 missing" -grep -qx '10001' <<<"$out" || fail "container does not run as uid 10001" -docker ps -a --filter name=^devcontainers-smoke-run$ --format '{{.Names}}' | grep -q . && fail "smoke container was not removed" echo "smoke-image: PASS ($image)" From 20a0afba5b80a5481935c88cf801fc29b3a831f7 Mon Sep 17 00:00:00 2001 From: James Ross Date: Sun, 8 Nov 2026 04:57:51 -0800 Subject: [PATCH 3/9] Scope a shellcheck SC2016 exception in smoke-image The single-quoted ${CARGO_HOME} must expand inside the container. --- tests/smoke-image | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/smoke-image b/tests/smoke-image index bfe81ff..08087b7 100755 --- a/tests/smoke-image +++ b/tests/smoke-image @@ -46,6 +46,7 @@ run_in() { [[ "$(docker image inspect "$image" --format '{{.Config.User}}')" == "10001:10001" ]] || fail "image does not run as 10001" docker image inspect "$image" --format '{{range .Config.Env}}{{println .}}{{end}}' | grep -q '^RUSTUP_TOOLCHAIN=' && fail "image sets RUSTUP_TOOLCHAIN" +# shellcheck disable=SC2016 # \${CARGO_HOME} must expand inside the container, not on the host. run_in 'rustc --version; cargo --version; cargo clippy --version; rustfmt --version; rustup target list --installed; cargo deny --version; id -u; echo "CARGO_HOME=${CARGO_HOME:-unset}"' echo "$output" v="${version//./\\.}" From 25b2dfec263ca07ab78c3380d96008c6b3dec919 Mon Sep 17 00:00:00 2001 From: James Ross Date: Sun, 8 Nov 2026 05:00:45 -0800 Subject: [PATCH 4/9] Create HOME for runs and keep leftover containers for inspection - The image creates /tmp/home (HOME) owned by UID 10001, and every smoke container creates it again on its fresh /tmp tmpfs; the smoke test checks that git can write a global config there. - The smoke container name stays fixed (docs/hygiene.md section 5). A leftover from an interrupted run now stops the test with a clear message, and the exit trap removes only containers this run created, so the leftover stays for inspection. --- images/rust-dev/Dockerfile | 6 ++++-- tests/smoke-image | 25 ++++++++++++++++++++----- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/images/rust-dev/Dockerfile b/images/rust-dev/Dockerfile index 63519ed..c424b61 100644 --- a/images/rust-dev/Dockerfile +++ b/images/rust-dev/Dockerfile @@ -49,9 +49,11 @@ RUN set -eu; \ # volumes. RUN groupadd --gid 10001 dev \ && useradd --uid 10001 --gid 10001 --no-create-home --home-dir /tmp/home --shell /usr/sbin/nologin dev \ - && mkdir -p /scratch /target /usr/local/cargo/registry /usr/local/cargo/git \ - && chown 10001:10001 /scratch /target /usr/local/cargo/registry /usr/local/cargo/git + && mkdir -p /scratch /target /usr/local/cargo/registry /usr/local/cargo/git /tmp/home \ + && chown 10001:10001 /scratch /target /usr/local/cargo/registry /usr/local/cargo/git /tmp/home +# HOME is /tmp/home. The image creates it; a run that mounts a fresh tmpfs on /tmp must create it +# again (devrun and tests/smoke-image do). # No RUSTUP_TOOLCHAIN: a repository's rust-toolchain.toml must match this image, or the run fails # offline instead of silently building with another compiler. CARGO_INCREMENTAL=0 keeps audit # builds free of incremental state. GIT_CONFIG_NOSYSTEM=1 ignores any system git config. diff --git a/tests/smoke-image b/tests/smoke-image index 08087b7..822d7f4 100755 --- a/tests/smoke-image +++ b/tests/smoke-image @@ -14,29 +14,40 @@ if [[ "${GITHUB_ACTIONS:-}" != "true" && -z "${DEVCONTAINERS_LOCK_HELD:-}" ]]; t fi fail() { echo "smoke-image: FAIL: $*" >&2; exit 1; } +created=0 leftover_check() { status=$? - docker rm -f "$name" >/dev/null 2>&1 || true - if docker ps -a --format '{{.Names}}' | grep -qx "$name"; then - echo "smoke-image: FAIL: $name still exists" >&2 - exit 1 + # Remove only a container this run created; a pre-existing leftover stays for inspection. + if (( created )); then + docker rm -f "$name" >/dev/null 2>&1 || true + if docker ps -a --format '{{.Names}}' | grep -qx "$name"; then + echo "smoke-image: FAIL: $name still exists" >&2 + exit 1 + fi fi exit "$status" } trap leftover_check EXIT # run_in SCRIPT: create, start, and remove one container; print its output. +# The name is fixed on purpose (docs/hygiene.md section 5): a leftover from an interrupted run is a +# conflict that this test reports, not litter that a fresh random name would hide. run_in() { + if docker ps -a --format '{{.Names}}' | grep -qx "$name"; then + fail "leftover container $name exists (interrupted run?); inspect it, then: docker rm -f $name" + fi docker create --name "$name" \ --label org.flyingrobots.owner=devcontainers --label org.flyingrobots.role=dev-run \ --user 10001:10001 --network none --read-only --tmpfs /tmp:rw,nosuid,nodev,size=64m,mode=1777 \ --cap-drop ALL --security-opt no-new-privileges \ --pids-limit 256 --memory 1g --memory-swap 1g --cpus 1 --ulimit nofile=1024:1024 \ --env CARGO_NET_OFFLINE=true \ - "$image" bash -c "$1" >/dev/null + "$image" bash -c 'mkdir -p "$HOME" && eval "$1"' smoke "$1" >/dev/null + created=1 local rc=0 output="$(perl -e 'alarm 120; exec @ARGV' docker start -a "$name")" || rc=$? docker rm -f "$name" >/dev/null + created=0 return "$rc" } @@ -65,6 +76,10 @@ if run_in 'mkdir -p /tmp/p && cd /tmp/p && printf "[toolchain]\nchannel = \"1.80 fi echo "smoke-image: mismatched rust-toolchain.toml fails offline (expected)" +# HOME is writable after the run creates it on the tmpfs. +run_in 'git config --global user.name smoke && test "$(git config --global user.name)" = smoke' || fail "HOME is not writable" +echo "smoke-image: HOME is writable (expected)" + # The image path for CARGO_HOME is read-only at run time. if run_in 'touch /usr/local/cargo/config.toml'; then fail "CARGO_HOME is writable"; fi echo "smoke-image: CARGO_HOME is read-only (expected)" From 18fa29dcbe38ed86af985b67168928e03639bf1b Mon Sep 17 00:00:00 2001 From: James Ross Date: Sun, 8 Nov 2026 05:19:25 -0800 Subject: [PATCH 5/9] Keep the copy out of reach of planted cargo config and bound the build preflight Baker Street case 2026-11-08-devcontainers-pr6 (repairs W-R-1..4, 8, 9, 11). - The copy moves to /work, the root of its own -work volume. Its only parent is the read-only root, so no writable directory above the copy can hold a .cargo/config.toml that cargo would read (F-2). The hygiene rules now say the runner empties the work and scratch volumes, hidden entries included, before each copy-in, and that CARGO_HOME is read-only only under --read-only (F-4 wording). - docs/hygiene.md says which run rules nothing enforces until devrun lands (F-6). The README no longer says the runner mounts binds (F-7). - scripts/build-image refuses a non-docker buildx driver, pulls the base under a 300 s alarm, and runs the free-space check with the run limits under a 60 s alarm. It refuses a leftover run container and removes only its own after a failure. Total 2160 s, below the 2400 s lease (F-3). - tests/smoke-image: add the missing SC2016 directive (F-1). Setup failures and timeouts stop the test. The negative checks need a marker from inside the container and the read-only error text (F-5). New checks: working directory /work, owned by 10001. --- README.md | 6 +++--- docs/hygiene.md | 30 +++++++++++++++++++++++------- images/rust-dev/Dockerfile | 19 +++++++++++-------- scripts/build-image | 38 +++++++++++++++++++++++++++++++++----- tests/smoke-image | 31 +++++++++++++++++++++---------- 5 files changed, 91 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index d6e3b36..2ec8b30 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,8 @@ containers, never on the host. - One base image per Rust toolchain, pinned to the version in each repository's `rust-toolchain.toml`. - One guarded runner that every repository calls. The runner claims `git-locks` - keys, applies a timeout and resource limits, mounts only named volumes and - binds, and audits Docker for litter before and after each run. + keys, applies a timeout and resource limits, mounts only named volumes (never + a host path), and audits Docker for litter before and after each run. The rules are in [`docs/hygiene.md`](docs/hygiene.md). Work is tracked in GitHub Issues with `issue-dag/2` frontmatter. @@ -22,7 +22,7 @@ tests/smoke-image 1.96.0 # checks it in offline, read-only, unprivileged con Pinned base digests live in `images/rust-dev/base-digests`, one line per Rust version. The image runs as UID 10001. Mount points for the named volumes: -`/scratch` (copy, logs, exports), `/target` (build output), +`/work` (the copy), `/scratch` (inputs, logs, exports), `/target` (build output), `/usr/local/cargo/registry`, and `/usr/local/cargo/git` (cargo caches). The guarded runner `bin/devrun` is not here yet (flyingrobots/devcontainers#3 diff --git a/docs/hygiene.md b/docs/hygiene.md index 977fc5b..ca0d22a 100644 --- a/docs/hygiene.md +++ b/docs/hygiene.md @@ -5,6 +5,12 @@ is hostile. The host, its git repositories, its credentials, and other projects' Docker resources must stay unreachable and unchanged. The runner (`bin/devrun`) enforces every rule here; a rule that only lives in this file is not enforced. +**Status:** `bin/devrun` is not merged yet (flyingrobots/devcontainers#3). Until +it is, the rules about runs (copy-in, network phases, run-phase mounts, prep, +outputs, audit) are its specification, and nothing enforces them. +`scripts/build-image` and `tests/smoke-image` enforce the rules that apply to an +image build and a smoke test. + ## 1. Source isolation - **Copy in, never mount.** No host path is bind-mounted into a container: not @@ -40,13 +46,22 @@ enforces every rule here; a rule that only lives in this file is not enforced. - Non-root user with a fixed UID (`10001`), `--user 10001:10001`. - `--cap-drop ALL`, `--security-opt no-new-privileges`. -- `--read-only` root filesystem. Writable places: `/scratch/work` (the copy, - inside the scratch volume), `/tmp` (tmpfs with a size limit), and the named - volumes in section 5. -- `CARGO_HOME` is the image path `/usr/local/cargo`, which is read-only at run - time, so no run can plant a cargo `config.toml` or a `credentials.toml`. +- `--read-only` root filesystem. Writable places: `/work` (the copy, at the + root of the work volume), `/scratch`, `/tmp` (tmpfs with a size limit), and + the other named volumes in section 5. +- **No writable directory above the copy.** Cargo reads `.cargo/config.toml` in + the working directory and in every parent directory. The copy is the root of + its own volume, so its only parent is the read-only root. Before each copy-in, + the runner empties the work and scratch volumes completely, hidden entries + included, so a `.cargo/config.toml` that one run leaves behind never reaches + the next. +- `CARGO_HOME` is the image path `/usr/local/cargo`. It is read-only only + because the run uses `--read-only`: the base image leaves it and the toolchain + world-writable. Under `--read-only`, a run cannot plant a cargo `config.toml` + or a `credentials.toml` there. - No `RUSTUP_TOOLCHAIN` in the image or the run: a repository's - `rust-toolchain.toml` must match the image, or the run fails offline. + `rust-toolchain.toml` must match the image. Otherwise rustup tries to install + the other toolchain, and that fails on the read-only root. - Limits on every run: `--pids-limit`, `--memory` with `--memory-swap` equal to it, `--cpus`, `--ulimit nofile`. - Never `--privileged`, `--network host`, `--pid host`, `--ipc host`, or added @@ -90,7 +105,8 @@ and `org.flyingrobots.role=`. Cleanup selects by these labels only. | Cargo git cache, at `/usr/local/cargo/git` | `-cargo-git` | `cargo-git` | | Build output, iteration | `-target` | `target` | | Build output, evidence | `-target-evidence`, emptied before each merge-gate run | `target-evidence` | -| Logs, test data, exports | `-scratch` | `scratch` | +| The copy, at `/work` | `-work`, emptied before each copy-in | `work` | +| Inputs, logs, test data, exports, at `/scratch` | `-scratch`, emptied before each copy-in | `scratch` | - No anonymous volumes. Images must not declare `VOLUME`. - The cargo caches are writable only in the fetch phase. The build phase mounts diff --git a/images/rust-dev/Dockerfile b/images/rust-dev/Dockerfile index c424b61..047325f 100644 --- a/images/rust-dev/Dockerfile +++ b/images/rust-dev/Dockerfile @@ -44,18 +44,21 @@ RUN set -eu; \ # Unprivileged user (fixed UID per docs/hygiene.md section 2) and the mount points for the named # volumes (section 5). New named volumes copy the ownership of these directories, so UID 10001 can -# write to them. CARGO_HOME stays at the image path /usr/local/cargo: containers run with a -# read-only root, so no run can plant a cargo config there. Only its registry and git caches are -# volumes. +# write to them. The copy lives at /work, the root of its own volume: its only parent directory is +# the root filesystem, so no writable directory above the copy can hold a .cargo/config.toml that +# cargo would read. CARGO_HOME stays at the image path /usr/local/cargo, with only its registry and +# git caches as volumes. CARGO_HOME and the toolchain are read-only only because every run uses +# --read-only: the base image leaves those directories world-writable. RUN groupadd --gid 10001 dev \ && useradd --uid 10001 --gid 10001 --no-create-home --home-dir /tmp/home --shell /usr/sbin/nologin dev \ - && mkdir -p /scratch /target /usr/local/cargo/registry /usr/local/cargo/git /tmp/home \ - && chown 10001:10001 /scratch /target /usr/local/cargo/registry /usr/local/cargo/git /tmp/home + && mkdir -p /work /scratch /target /usr/local/cargo/registry /usr/local/cargo/git /tmp/home \ + && chown 10001:10001 /work /scratch /target /usr/local/cargo/registry /usr/local/cargo/git /tmp/home # HOME is /tmp/home. The image creates it; a run that mounts a fresh tmpfs on /tmp must create it # again (devrun and tests/smoke-image do). -# No RUSTUP_TOOLCHAIN: a repository's rust-toolchain.toml must match this image, or the run fails -# offline instead of silently building with another compiler. CARGO_INCREMENTAL=0 keeps audit +# No RUSTUP_TOOLCHAIN: a repository's rust-toolchain.toml must match this image. Otherwise rustup +# tries to install the other toolchain, which fails on the read-only root, instead of silently +# building with this compiler. CARGO_INCREMENTAL=0 keeps audit # builds free of incremental state. GIT_CONFIG_NOSYSTEM=1 ignores any system git config. ENV CARGO_TARGET_DIR=/target \ CARGO_INCREMENTAL=0 \ @@ -63,4 +66,4 @@ ENV CARGO_TARGET_DIR=/target \ GIT_CONFIG_NOSYSTEM=1 USER 10001:10001 -WORKDIR /scratch/work +WORKDIR /work diff --git a/scripts/build-image b/scripts/build-image index 074ab93..589e172 100755 --- a/scripts/build-image +++ b/scripts/build-image @@ -2,7 +2,8 @@ # Build flyingrobots/rust-dev: from the digest pinned in images/rust-dev/base-digests. # On the workstation it runs under the git-locks keys host/heavy-work and host/docker/devcontainers-dev/ # (docs/hygiene.md section 10), with a 30-minute timeout inside a 40-minute lease (section 7), and it -# refuses to start below 50 GiB free on the host or in the Docker VM (section 8). +# refuses to start below 50 GiB free on the host or in the Docker VM (section 8). Time budget inside +# the lease: base pull 300 s + free-space check 60 s + build 1800 s = 2160 s, below the 2400 s lease. set -euo pipefail version="${1:?usage: scripts/build-image }" root="$(cd "$(dirname "$0")/.." && pwd)" @@ -23,6 +24,14 @@ build=(docker build --tag "$tag" "$root/images/rust-dev") timeout_s=1800 +limits=(--pids-limit 32 --memory 128m --memory-swap 128m --cpus 0.5 --ulimit nofile=256:256) + +# Only a docker-driver builder loads the result into the local image store under its tag (section 4). +driver="$(docker buildx inspect | awk '$1 == "Driver:" { print $2; exit }')" +if [[ "$driver" != "docker" ]]; then + echo "build-image: the selected buildx builder uses driver '${driver:-unknown}'; select a docker-driver builder first" >&2 + exit 4 +fi # GitHub Actions runners are single-use machines with no shared git-locks store. Keep the timeout. if [[ "${GITHUB_ACTIONS:-}" == "true" ]]; then @@ -36,16 +45,35 @@ if [[ -z "${DEVCONTAINERS_LOCK_HELD:-}" ]]; then --ttl 2400 --wait 600 --note "build $tag" host/heavy-work host/docker/devcontainers-dev/ -- "$0" "$@" fi +# perl's alarm is the timeout, because macOS has no coreutils timeout by default. +alarm() { local s="$1"; shift; perl -e 'alarm shift; exec @ARGV' "$s" "$@"; } + +if ! docker image inspect "$base" >/dev/null 2>&1; then + echo "build-image: pulling $base" >&2 + alarm 300 docker pull --quiet "$base" >/dev/null +fi min_kib=$((50 * 1024 * 1024)) host_free="$(df -Pk "$root" | awk 'NR == 2 { print $4 }')" -vm_free="$(docker run --rm --name devcontainers-dev-run --network none \ +run_name=devcontainers-dev-run +# A leftover from an interrupted run stays for inspection; this script never removes it. +if docker ps -a --format '{{.Names}}' | grep -qx "$run_name"; then + echo "build-image: leftover container $run_name exists (interrupted run?); inspect it, then: docker rm -f $run_name" >&2 + exit 3 +fi +# The alarm stops only the docker client, so on failure remove the container this script started. +vm_out="$(alarm 60 docker run --rm --pull never --name "$run_name" --network none \ --label org.flyingrobots.owner=devcontainers --label org.flyingrobots.role=dev-run \ - --read-only --cap-drop ALL --security-opt no-new-privileges --user 10001:10001 \ - --entrypoint df "$base" -Pk / | awk 'NR == 2 { print $4 }')" + --read-only --cap-drop ALL --security-opt no-new-privileges --user 10001:10001 "${limits[@]}" \ + --entrypoint df "$base" -Pk /)" || { + docker rm -f "$run_name" >/dev/null 2>&1 || true + echo "build-image: could not measure Docker VM free space (failed or timed out)" >&2 + exit 3 +} +vm_free="$(awk 'NR == 2 { print $4 }' <<<"$vm_out")" +[[ "$vm_free" =~ ^[0-9]+$ ]] || { echo "build-image: unexpected df output: $vm_out" >&2; exit 3; } if (( host_free < min_kib || vm_free < min_kib )); then echo "build-image: refusing to start: less than 50 GiB free (host ${host_free} KiB, Docker VM ${vm_free} KiB)" >&2 exit 3 fi -# perl's alarm is the timeout, because macOS has no coreutils timeout by default. exec perl -e 'alarm shift; exec @ARGV' "$timeout_s" "${build[@]}" diff --git a/tests/smoke-image b/tests/smoke-image index 822d7f4..4b61679 100755 --- a/tests/smoke-image +++ b/tests/smoke-image @@ -29,7 +29,9 @@ leftover_check() { } trap leftover_check EXIT -# run_in SCRIPT: create, start, and remove one container; print its output. +# run_in SCRIPT: create, start, and remove one container; put its stdout in $output and return the +# container's exit status. A setup failure (create, timeout, or remove) stops the test: it is never +# counted as an expected failure. # The name is fixed on purpose (docs/hygiene.md section 5): a leftover from an interrupted run is a # conflict that this test reports, not litter that a fresh random name would hide. run_in() { @@ -42,11 +44,12 @@ run_in() { --cap-drop ALL --security-opt no-new-privileges \ --pids-limit 256 --memory 1g --memory-swap 1g --cpus 1 --ulimit nofile=1024:1024 \ --env CARGO_NET_OFFLINE=true \ - "$image" bash -c 'mkdir -p "$HOME" && eval "$1"' smoke "$1" >/dev/null + "$image" bash -c 'mkdir -p "$HOME" && eval "$1"' smoke "$1" >/dev/null || fail "docker create failed" created=1 local rc=0 output="$(perl -e 'alarm 120; exec @ARGV' docker start -a "$name")" || rc=$? - docker rm -f "$name" >/dev/null + (( rc != 142 )) || fail "container timed out after 120 s" + docker rm -f "$name" >/dev/null || fail "could not remove $name" created=0 return "$rc" } @@ -55,10 +58,11 @@ run_in() { [[ "$(docker image inspect "$image" --format '{{index .Config.Labels "org.flyingrobots.owner"}}')" == "devcontainers" ]] || fail "owner label missing" [[ "$(docker image inspect "$image" --format '{{index .Config.Labels "org.flyingrobots.role"}}')" == "base-image" ]] || fail "role label missing" [[ "$(docker image inspect "$image" --format '{{.Config.User}}')" == "10001:10001" ]] || fail "image does not run as 10001" +[[ "$(docker image inspect "$image" --format '{{.Config.WorkingDir}}')" == "/work" ]] || fail "working directory is not /work" docker image inspect "$image" --format '{{range .Config.Env}}{{println .}}{{end}}' | grep -q '^RUSTUP_TOOLCHAIN=' && fail "image sets RUSTUP_TOOLCHAIN" # shellcheck disable=SC2016 # \${CARGO_HOME} must expand inside the container, not on the host. -run_in 'rustc --version; cargo --version; cargo clippy --version; rustfmt --version; rustup target list --installed; cargo deny --version; id -u; echo "CARGO_HOME=${CARGO_HOME:-unset}"' +run_in 'rustc --version; cargo --version; cargo clippy --version; rustfmt --version; rustup target list --installed; cargo deny --version; id -u; echo "CARGO_HOME=${CARGO_HOME:-unset}"; echo "work=$(stat -c %u:%g /work)"' echo "$output" v="${version//./\\.}" grep -q "^rustc $v " <<<"$output" || fail "rustc is not $version" @@ -69,19 +73,26 @@ grep -q '^wasm32-unknown-unknown$' <<<"$output" || fail "wasm32 target missing" grep -q '^cargo-deny 0\.20\.2$' <<<"$output" || fail "cargo-deny 0.20.2 missing" grep -qx '10001' <<<"$output" || fail "container does not run as uid 10001" grep -qx 'CARGO_HOME=/usr/local/cargo' <<<"$output" || fail "CARGO_HOME is not the read-only image path" +grep -qx 'work=10001:10001' <<<"$output" || fail "/work is not owned by 10001" -# A repository that pins another toolchain must fail offline, not build with this compiler. -if run_in 'mkdir -p /tmp/p && cd /tmp/p && printf "[toolchain]\nchannel = \"1.80.0\"\n" > rust-toolchain.toml && rustc --version'; then +# A repository that pins another toolchain must fail, not build with this compiler. The marker shows +# the container reached the check; the error must be rustup failing to install on the read-only root. +if run_in 'mkdir -p /tmp/p && cd /tmp/p && printf "[toolchain]\nchannel = \"1.80.0\"\n" > rust-toolchain.toml && echo reached && rustc --version 2>&1'; then fail "a mismatched rust-toolchain.toml did not fail" fi -echo "smoke-image: mismatched rust-toolchain.toml fails offline (expected)" +grep -qx reached <<<"$output" || fail "mismatched-toolchain check did not run" +grep -q 'Read-only file system' <<<"$output" || fail "mismatched toolchain failed for another reason: $output" +echo "smoke-image: mismatched rust-toolchain.toml fails on the read-only root (expected)" # HOME is writable after the run creates it on the tmpfs. +# shellcheck disable=SC2016 # $(git config ...) must expand inside the container, not on the host. run_in 'git config --global user.name smoke && test "$(git config --global user.name)" = smoke' || fail "HOME is not writable" echo "smoke-image: HOME is writable (expected)" -# The image path for CARGO_HOME is read-only at run time. -if run_in 'touch /usr/local/cargo/config.toml'; then fail "CARGO_HOME is writable"; fi -echo "smoke-image: CARGO_HOME is read-only (expected)" +# Under --read-only, the image path for CARGO_HOME cannot take a planted config. +if run_in 'echo reached && touch /usr/local/cargo/config.toml 2>&1'; then fail "CARGO_HOME is writable"; fi +grep -qx reached <<<"$output" || fail "CARGO_HOME check did not run" +grep -q 'Read-only file system' <<<"$output" || fail "CARGO_HOME check failed for another reason: $output" +echo "smoke-image: CARGO_HOME is read-only under --read-only (expected)" echo "smoke-image: PASS ($image)" From 93bc31618299d8d08a6bcc87de04859ab0097443 Mon Sep 17 00:00:00 2001 From: James Ross Date: Sun, 8 Nov 2026 05:35:42 -0800 Subject: [PATCH 6/9] Remove only containers this run created and scope the toolchain claim Baker Street case 2026-11-08-devcontainers-pr6, fresh blind code pass findings (blind Holmes H-F-1, H-F-2, H-F-3, H-F-5, H-F-7; blind Moriarty M-F-1, M-F-2, M-F-6). - scripts/build-image: the leftover check fails closed when docker ps fails, and the free-space container carries a per-run label. After a failure or timeout the script removes only a container with that label, so a same-named container that this run did not create stays. - tests/smoke-image: the same fail-closed leftover listing; an explicit check that the image exists; the RUSTUP_TOOLCHAIN check no longer pipes into grep -q under pipefail; ownership checks for /scratch, /target, and the cargo cache mount points as well as /work. - Lock key host/docker/devcontainers-dev-run/, the workstation registry form host/docker// (hygiene section 10). - docs/hygiene.md section 2 and the Dockerfile comment: the toolchain check catches an honest channel mismatch only; a hostile repository can run its own compiler (path toolchain, build.rustc). The writable list includes /dev/shm and /dev/mqueue. --- docs/hygiene.md | 19 +++++++++++++------ images/rust-dev/Dockerfile | 7 ++++--- scripts/build-image | 18 ++++++++++++------ tests/smoke-image | 20 ++++++++++++-------- 4 files changed, 41 insertions(+), 23 deletions(-) diff --git a/docs/hygiene.md b/docs/hygiene.md index ca0d22a..a06ccbe 100644 --- a/docs/hygiene.md +++ b/docs/hygiene.md @@ -47,8 +47,9 @@ image build and a smoke test. - Non-root user with a fixed UID (`10001`), `--user 10001:10001`. - `--cap-drop ALL`, `--security-opt no-new-privileges`. - `--read-only` root filesystem. Writable places: `/work` (the copy, at the - root of the work volume), `/scratch`, `/tmp` (tmpfs with a size limit), and - the other named volumes in section 5. + root of the work volume), `/scratch`, `/tmp` (tmpfs with a size limit), the + other named volumes in section 5, and Docker's own `/dev/shm` and + `/dev/mqueue`. - **No writable directory above the copy.** Cargo reads `.cargo/config.toml` in the working directory and in every parent directory. The copy is the root of its own volume, so its only parent is the read-only root. Before each copy-in, @@ -60,8 +61,12 @@ image build and a smoke test. world-writable. Under `--read-only`, a run cannot plant a cargo `config.toml` or a `credentials.toml` there. - No `RUSTUP_TOOLCHAIN` in the image or the run: a repository's - `rust-toolchain.toml` must match the image. Otherwise rustup tries to install - the other toolchain, and that fails on the read-only root. + `rust-toolchain.toml` channel must match the image. Otherwise rustup tries to + install the other toolchain, and that fails on the read-only root. This + catches an honest version mismatch only. A hostile repository can still run + its own compiler, for example through a `path` toolchain or `build.rustc` in + its own `.cargo/config.toml`, so a run's output never proves which compiler + built it. The isolation rules contain such a compiler like any other code. - Limits on every run: `--pids-limit`, `--memory` with `--memory-swap` equal to it, `--cpus`, `--ulimit nofile`. - Never `--privileged`, `--network host`, `--pid host`, `--ipc host`, or added @@ -151,8 +156,10 @@ and `org.flyingrobots.role=`. Cleanup selects by these labels only. ## 10. Locks -- Each run claims `host/heavy-work` and `host/docker/-dev/` in the - workstation `git-locks` store, with a lease longer than the timeout. +- Each run claims `host/heavy-work` and `host/docker/-dev-run/` in the + workstation `git-locks` store, with a lease longer than the timeout. The + second key follows the workstation registry rule + `host/docker//`. ## 11. Not ours diff --git a/images/rust-dev/Dockerfile b/images/rust-dev/Dockerfile index 047325f..13af5f7 100644 --- a/images/rust-dev/Dockerfile +++ b/images/rust-dev/Dockerfile @@ -56,9 +56,10 @@ RUN groupadd --gid 10001 dev \ # HOME is /tmp/home. The image creates it; a run that mounts a fresh tmpfs on /tmp must create it # again (devrun and tests/smoke-image do). -# No RUSTUP_TOOLCHAIN: a repository's rust-toolchain.toml must match this image. Otherwise rustup -# tries to install the other toolchain, which fails on the read-only root, instead of silently -# building with this compiler. CARGO_INCREMENTAL=0 keeps audit +# No RUSTUP_TOOLCHAIN: a repository's rust-toolchain.toml channel must match this image. Otherwise +# rustup tries to install the other toolchain, which fails on the read-only root. This catches an +# honest mismatch only: a repository can still run its own compiler (a path toolchain, build.rustc). +# docs/hygiene.md section 2. CARGO_INCREMENTAL=0 keeps audit # builds free of incremental state. GIT_CONFIG_NOSYSTEM=1 ignores any system git config. ENV CARGO_TARGET_DIR=/target \ CARGO_INCREMENTAL=0 \ diff --git a/scripts/build-image b/scripts/build-image index 589e172..95a5a32 100755 --- a/scripts/build-image +++ b/scripts/build-image @@ -1,6 +1,6 @@ #!/usr/bin/env bash # Build flyingrobots/rust-dev: from the digest pinned in images/rust-dev/base-digests. -# On the workstation it runs under the git-locks keys host/heavy-work and host/docker/devcontainers-dev/ +# On the workstation it runs under the git-locks keys host/heavy-work and host/docker/devcontainers-dev-run/ # (docs/hygiene.md section 10), with a 30-minute timeout inside a 40-minute lease (section 7), and it # refuses to start below 50 GiB free on the host or in the Docker VM (section 8). Time budget inside # the lease: base pull 300 s + free-space check 60 s + build 1800 s = 2160 s, below the 2400 s lease. @@ -42,7 +42,7 @@ if [[ -z "${DEVCONTAINERS_LOCK_HELD:-}" ]]; then # Default store: the shared workstation store on this macOS machine. exec env GIT_LOCKS_STORE="${GIT_LOCKS_STORE:-/Users/Shared/git-locks/workstation.git}" DEVCONTAINERS_LOCK_HELD=1 \ git locks with --job "devcontainers-build-$(uuidgen)" --holder "${USER:-agent}-build-image" \ - --ttl 2400 --wait 600 --note "build $tag" host/heavy-work host/docker/devcontainers-dev/ -- "$0" "$@" + --ttl 2400 --wait 600 --note "build $tag" host/heavy-work host/docker/devcontainers-dev-run/ -- "$0" "$@" fi # perl's alarm is the timeout, because macOS has no coreutils timeout by default. @@ -55,17 +55,23 @@ fi min_kib=$((50 * 1024 * 1024)) host_free="$(df -Pk "$root" | awk 'NR == 2 { print $4 }')" run_name=devcontainers-dev-run -# A leftover from an interrupted run stays for inspection; this script never removes it. -if docker ps -a --format '{{.Names}}' | grep -qx "$run_name"; then +run_id="build-$(uuidgen)" +# A leftover from an interrupted run stays for inspection; this script never removes it. A failed +# listing stops the script (set -e), so an error never reads as "no leftover". +leftover="$(docker ps -a -q --filter "name=^${run_name}\$")" +if [[ -n "$leftover" ]]; then echo "build-image: leftover container $run_name exists (interrupted run?); inspect it, then: docker rm -f $run_name" >&2 exit 3 fi -# The alarm stops only the docker client, so on failure remove the container this script started. +# The alarm stops only the docker client, so on failure remove the container this run started. It +# carries this run's own label, so a container that someone else created under the name stays. vm_out="$(alarm 60 docker run --rm --pull never --name "$run_name" --network none \ --label org.flyingrobots.owner=devcontainers --label org.flyingrobots.role=dev-run \ + --label "org.flyingrobots.run=$run_id" \ --read-only --cap-drop ALL --security-opt no-new-privileges --user 10001:10001 "${limits[@]}" \ --entrypoint df "$base" -Pk /)" || { - docker rm -f "$run_name" >/dev/null 2>&1 || true + mine="$(docker ps -a -q --filter "label=org.flyingrobots.run=$run_id" || true)" + [[ -z "$mine" ]] || docker rm -f "$mine" >/dev/null 2>&1 || true echo "build-image: could not measure Docker VM free space (failed or timed out)" >&2 exit 3 } diff --git a/tests/smoke-image b/tests/smoke-image index 4b61679..d677250 100755 --- a/tests/smoke-image +++ b/tests/smoke-image @@ -10,7 +10,7 @@ name=devcontainers-dev-run if [[ "${GITHUB_ACTIONS:-}" != "true" && -z "${DEVCONTAINERS_LOCK_HELD:-}" ]]; then exec env GIT_LOCKS_STORE="${GIT_LOCKS_STORE:-/Users/Shared/git-locks/workstation.git}" DEVCONTAINERS_LOCK_HELD=1 \ git locks with --job "devcontainers-smoke-$(uuidgen)" --holder "${USER:-agent}-smoke-image" \ - --ttl 900 --wait 600 --note "smoke $image" host/heavy-work host/docker/devcontainers-dev/ -- "$0" "$@" + --ttl 900 --wait 600 --note "smoke $image" host/heavy-work host/docker/devcontainers-dev-run/ -- "$0" "$@" fi fail() { echo "smoke-image: FAIL: $*" >&2; exit 1; } @@ -20,7 +20,7 @@ leftover_check() { # Remove only a container this run created; a pre-existing leftover stays for inspection. if (( created )); then docker rm -f "$name" >/dev/null 2>&1 || true - if docker ps -a --format '{{.Names}}' | grep -qx "$name"; then + if [[ -n "$(docker ps -a -q --filter "name=^${name}\$")" ]]; then echo "smoke-image: FAIL: $name still exists" >&2 exit 1 fi @@ -35,9 +35,9 @@ trap leftover_check EXIT # The name is fixed on purpose (docs/hygiene.md section 5): a leftover from an interrupted run is a # conflict that this test reports, not litter that a fresh random name would hide. run_in() { - if docker ps -a --format '{{.Names}}' | grep -qx "$name"; then - fail "leftover container $name exists (interrupted run?); inspect it, then: docker rm -f $name" - fi + local leftover + leftover="$(docker ps -a -q --filter "name=^${name}\$")" || fail "could not list containers" + [[ -z "$leftover" ]] || fail "leftover container $name exists (interrupted run?); inspect it, then: docker rm -f $name" docker create --name "$name" \ --label org.flyingrobots.owner=devcontainers --label org.flyingrobots.role=dev-run \ --user 10001:10001 --network none --read-only --tmpfs /tmp:rw,nosuid,nodev,size=64m,mode=1777 \ @@ -54,15 +54,17 @@ run_in() { return "$rc" } +docker image inspect "$image" >/dev/null 2>&1 || fail "image $image not found; build it with scripts/build-image $version" [[ "$(docker image inspect "$image" --format '{{json .Config.Volumes}}')" == "null" ]] || fail "image declares VOLUME" [[ "$(docker image inspect "$image" --format '{{index .Config.Labels "org.flyingrobots.owner"}}')" == "devcontainers" ]] || fail "owner label missing" [[ "$(docker image inspect "$image" --format '{{index .Config.Labels "org.flyingrobots.role"}}')" == "base-image" ]] || fail "role label missing" [[ "$(docker image inspect "$image" --format '{{.Config.User}}')" == "10001:10001" ]] || fail "image does not run as 10001" [[ "$(docker image inspect "$image" --format '{{.Config.WorkingDir}}')" == "/work" ]] || fail "working directory is not /work" -docker image inspect "$image" --format '{{range .Config.Env}}{{println .}}{{end}}' | grep -q '^RUSTUP_TOOLCHAIN=' && fail "image sets RUSTUP_TOOLCHAIN" +env_list="$(docker image inspect "$image" --format '{{range .Config.Env}}{{println .}}{{end}}')" +if grep -q '^RUSTUP_TOOLCHAIN=' <<<"$env_list"; then fail "image sets RUSTUP_TOOLCHAIN"; fi # shellcheck disable=SC2016 # \${CARGO_HOME} must expand inside the container, not on the host. -run_in 'rustc --version; cargo --version; cargo clippy --version; rustfmt --version; rustup target list --installed; cargo deny --version; id -u; echo "CARGO_HOME=${CARGO_HOME:-unset}"; echo "work=$(stat -c %u:%g /work)"' +run_in 'rustc --version; cargo --version; cargo clippy --version; rustfmt --version; rustup target list --installed; cargo deny --version; id -u; echo "CARGO_HOME=${CARGO_HOME:-unset}"; for d in /work /scratch /target /usr/local/cargo/registry /usr/local/cargo/git; do echo "owner $d $(stat -c %u:%g "$d")"; done' echo "$output" v="${version//./\\.}" grep -q "^rustc $v " <<<"$output" || fail "rustc is not $version" @@ -73,7 +75,9 @@ grep -q '^wasm32-unknown-unknown$' <<<"$output" || fail "wasm32 target missing" grep -q '^cargo-deny 0\.20\.2$' <<<"$output" || fail "cargo-deny 0.20.2 missing" grep -qx '10001' <<<"$output" || fail "container does not run as uid 10001" grep -qx 'CARGO_HOME=/usr/local/cargo' <<<"$output" || fail "CARGO_HOME is not the read-only image path" -grep -qx 'work=10001:10001' <<<"$output" || fail "/work is not owned by 10001" +for d in /work /scratch /target /usr/local/cargo/registry /usr/local/cargo/git; do + grep -qx "owner $d 10001:10001" <<<"$output" || fail "$d is not owned by 10001 (a new named volume copies this ownership)" +done # A repository that pins another toolchain must fail, not build with this compiler. The marker shows # the container reached the check; the error must be rustup failing to install on the read-only root. From 32eb7316ca241476ca9027ce914a490b022bf50b Mon Sep 17 00:00:00 2001 From: James Ross Date: Sun, 8 Nov 2026 05:37:38 -0800 Subject: [PATCH 7/9] Declare the upstream base image as a retained build input Fresh blind code pass finding M-F-3: on a machine without the base, scripts/build-image pulls rust@sha256:, which carries no org.flyingrobots labels. docs/hygiene.md section 5 now lists it with its name, why it stays, and when to remove it. --- docs/hygiene.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/hygiene.md b/docs/hygiene.md index a06ccbe..2d77809 100644 --- a/docs/hygiene.md +++ b/docs/hygiene.md @@ -105,6 +105,7 @@ and `org.flyingrobots.role=`. Cleanup selects by these labels only. | Resource | Name | Role label | | --- | --- | --- | | Base image | `flyingrobots/rust-dev:` | `base-image` | +| Upstream base, the build input | `rust@sha256:` from `images/rust-dev/base-digests`, pulled by digest. It carries only upstream labels. Keep it while its digest line exists; after removing the line, remove it with `docker image rm`. | none (upstream image) | | Run container | `-dev-run` (one at a time, under a lock) | `dev-run` | | Cargo registry cache, at `/usr/local/cargo/registry` | `-cargo-registry` (per project: no cross-project cache poisoning) | `cargo-registry` | | Cargo git cache, at `/usr/local/cargo/git` | `-cargo-git` | `cargo-git` | From f23bbe7a42fc92410c6a06ff42c5c18e7ba51564 Mon Sep 17 00:00:00 2001 From: James Ross Date: Sun, 8 Nov 2026 05:53:17 -0800 Subject: [PATCH 8/9] Keep repository-chosen code out of the fetch phase Baker Street decision check, hypothesis N-1, confirmed by execution in the image: with no dependencies and offline, cargo fetch ran a repository's build.rustc-wrapper, build.rustc, and path toolchain (twice each, for rustc -vV and the target-info query). In the fetch phase the cargo caches are writable, so such code could poison cached sources for later runs, which contradicts the cache rule in docs/hygiene.md section 5. docs/hygiene.md section 3 now requires the fetch copy to have no .cargo/config files and no rust-toolchain files, and the fetch command to run with RUSTUP_TOOLCHAIN, RUSTC, and empty RUSTC_WRAPPER and RUSTC_WORKSPACE_WRAPPER. In the same probe these overrides stopped all four hooks. The Dockerfile comment says the work volume persists and the runner must empty it. --- docs/hygiene.md | 21 ++++++++++++++++----- images/rust-dev/Dockerfile | 3 ++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/docs/hygiene.md b/docs/hygiene.md index 2d77809..18bd329 100644 --- a/docs/hygiene.md +++ b/docs/hygiene.md @@ -76,8 +76,19 @@ image build and a smoke test. ## 3. Network -- **Phase 1, fetch:** `cargo fetch --locked` with network access, writing only - to the project's registry volume. +- **Phase 1, fetch:** `cargo fetch --locked` with network access and the cargo + caches writable. It must run no code that the repository chooses, because a + program that runs now can rewrite cached sources for every later run. + `cargo fetch` asks the compiler for target information, so a repository's + `build.rustc-wrapper`, `build.rustc`, or `path` toolchain would run here. + Therefore: + - The fetch copy contains no `.cargo/config` or `.cargo/config.toml` file + and no `rust-toolchain` or `rust-toolchain.toml` file. + - The fetch command runs with `RUSTUP_TOOLCHAIN` set to the image's version, + `RUSTC=/usr/local/cargo/bin/rustc`, and `RUSTC_WRAPPER` and + `RUSTC_WORKSPACE_WRAPPER` set empty. These override the same settings in + any configuration file. + - `cargo fetch` runs no build scripts and no procedural macros. - **Phase 2, build and test:** `--network none` and `CARGO_NET_OFFLINE=true`. A build script cannot download or send anything. - A command that truly needs the network in phase 2 must say so with @@ -115,9 +126,9 @@ and `org.flyingrobots.role=`. Cleanup selects by these labels only. | Inputs, logs, test data, exports, at `/scratch` | `-scratch`, emptied before each copy-in | `scratch` | - No anonymous volumes. Images must not declare `VOLUME`. -- The cargo caches are writable only in the fetch phase. The build phase mounts - them read-only, so a hostile build script cannot poison the cache for a later - run. +- The cargo caches are writable only in the fetch phase, which runs no code + that the repository chooses (section 3). The build phase mounts them + read-only, so a hostile build script cannot poison the cache for a later run. - **Evidence runs use a fresh target.** A merge-gate run never reuses build output, so a stale cache cannot fabricate a passing result. diff --git a/images/rust-dev/Dockerfile b/images/rust-dev/Dockerfile index 13af5f7..322ef39 100644 --- a/images/rust-dev/Dockerfile +++ b/images/rust-dev/Dockerfile @@ -46,7 +46,8 @@ RUN set -eu; \ # volumes (section 5). New named volumes copy the ownership of these directories, so UID 10001 can # write to them. The copy lives at /work, the root of its own volume: its only parent directory is # the root filesystem, so no writable directory above the copy can hold a .cargo/config.toml that -# cargo would read. CARGO_HOME stays at the image path /usr/local/cargo, with only its registry and +# cargo would read. The work volume itself persists, so the runner must empty it before each +# copy-in (docs/hygiene.md section 2). CARGO_HOME stays at the image path /usr/local/cargo, with only its registry and # git caches as volumes. CARGO_HOME and the toolchain are read-only only because every run uses # --read-only: the base image leaves those directories world-writable. RUN groupadd --gid 10001 dev \ From 5295c2203c4eff21eecce0153cce45c7190e27cf Mon Sep 17 00:00:00 2001 From: James Ross Date: Sun, 8 Nov 2026 06:04:08 -0800 Subject: [PATCH 9/9] Enforce timeouts by killing the docker client, not by SIGALRM CodeRabbit review on f23bbe7, confirmed by execution: under perl -e 'alarm 3; exec ...' docker start -a, a container running sleep 30 kept the client waiting 30 s, and the client exited 0. docker run and docker start -a forward SIGALRM to the container, so the alarm never stopped them, and a hung smoke check could count as a pass. - Both scripts use a deadline helper: perl forks the command and kills it with SIGKILL after the limit (exit 124). Killing the client does not stop a container, so cleanup stays with the existing removal paths. - tests/smoke-image reads the result from docker inspect: a container still running after the client returns has timed out, a start error stops the test, and the exit status is the container's own. A status of 142 from the command is no longer read as a timeout. - scripts/build-image refuses storage drivers other than overlay2 and overlayfs, because only then is the free-space container's / on the image store's filesystem. Mutation calibration: with one smoke check replaced by sleep 30 and a 5 s limit, the f23bbe7 smoke test reported PASS after 33 s; this one fails with a timeout after 7 s and leaves no container. --- scripts/build-image | 27 +++++++++++++++++++++------ tests/smoke-image | 24 +++++++++++++++++++----- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/scripts/build-image b/scripts/build-image index 95a5a32..63d21b9 100755 --- a/scripts/build-image +++ b/scripts/build-image @@ -45,13 +45,28 @@ if [[ -z "${DEVCONTAINERS_LOCK_HELD:-}" ]]; then --ttl 2400 --wait 600 --note "build $tag" host/heavy-work host/docker/devcontainers-dev-run/ -- "$0" "$@" fi -# perl's alarm is the timeout, because macOS has no coreutils timeout by default. -alarm() { local s="$1"; shift; perl -e 'alarm shift; exec @ARGV' "$s" "$@"; } +# perl is the timeout tool, because macOS has no coreutils timeout by default. +# deadline SECONDS COMMAND...: run COMMAND; after SECONDS, kill it with SIGKILL and return 124. A +# plain alarm is not enough: docker run and docker start -a forward SIGALRM to the container instead +# of exiting, so the client would keep waiting. Killing the client does not stop a container. +deadline() { + perl -e '$t = shift; $p = fork; die "fork: $!" unless defined $p; + if (!$p) { exec @ARGV or exit 127 } + $SIG{ALRM} = sub { kill "KILL", $p; waitpid $p, 0; exit 124 }; + alarm $t; waitpid $p, 0; exit($? & 127 ? 128 + ($? & 127) : $? >> 8)' "$@" +} if ! docker image inspect "$base" >/dev/null 2>&1; then echo "build-image: pulling $base" >&2 - alarm 300 docker pull --quiet "$base" >/dev/null + deadline 300 docker pull --quiet "$base" >/dev/null fi +# The free-space container's / is on the image store's filesystem only for overlay storage. Refuse +# other storage, such as a stargz snapshotter, rather than measure the wrong filesystem. +storage="$(docker info -f '{{.Driver}}')" +case "$storage" in + overlay2|overlayfs) ;; + *) echo "build-image: cannot check free space for Docker storage driver '$storage'; use overlay2 or overlayfs" >&2; exit 3 ;; +esac min_kib=$((50 * 1024 * 1024)) host_free="$(df -Pk "$root" | awk 'NR == 2 { print $4 }')" run_name=devcontainers-dev-run @@ -63,9 +78,9 @@ if [[ -n "$leftover" ]]; then echo "build-image: leftover container $run_name exists (interrupted run?); inspect it, then: docker rm -f $run_name" >&2 exit 3 fi -# The alarm stops only the docker client, so on failure remove the container this run started. It +# The deadline stops only the docker client, so on failure remove the container this run started. It # carries this run's own label, so a container that someone else created under the name stays. -vm_out="$(alarm 60 docker run --rm --pull never --name "$run_name" --network none \ +vm_out="$(deadline 60 docker run --rm --pull never --name "$run_name" --network none \ --label org.flyingrobots.owner=devcontainers --label org.flyingrobots.role=dev-run \ --label "org.flyingrobots.run=$run_id" \ --read-only --cap-drop ALL --security-opt no-new-privileges --user 10001:10001 "${limits[@]}" \ @@ -82,4 +97,4 @@ if (( host_free < min_kib || vm_free < min_kib )); then exit 3 fi -exec perl -e 'alarm shift; exec @ARGV' "$timeout_s" "${build[@]}" +deadline "$timeout_s" "${build[@]}" diff --git a/tests/smoke-image b/tests/smoke-image index d677250..99f5fb7 100755 --- a/tests/smoke-image +++ b/tests/smoke-image @@ -13,6 +13,16 @@ if [[ "${GITHUB_ACTIONS:-}" != "true" && -z "${DEVCONTAINERS_LOCK_HELD:-}" ]]; t --ttl 900 --wait 600 --note "smoke $image" host/heavy-work host/docker/devcontainers-dev-run/ -- "$0" "$@" fi +# deadline SECONDS COMMAND...: run COMMAND; after SECONDS, kill it with SIGKILL and return 124. A +# plain alarm is not enough: docker run and docker start -a forward SIGALRM to the container instead +# of exiting, so the client would keep waiting. Killing the client does not stop a container. +deadline() { + perl -e '$t = shift; $p = fork; die "fork: $!" unless defined $p; + if (!$p) { exec @ARGV or exit 127 } + $SIG{ALRM} = sub { kill "KILL", $p; waitpid $p, 0; exit 124 }; + alarm $t; waitpid $p, 0; exit($? & 127 ? 128 + ($? & 127) : $? >> 8)' "$@" +} + fail() { echo "smoke-image: FAIL: $*" >&2; exit 1; } created=0 leftover_check() { @@ -30,8 +40,9 @@ leftover_check() { trap leftover_check EXIT # run_in SCRIPT: create, start, and remove one container; put its stdout in $output and return the -# container's exit status. A setup failure (create, timeout, or remove) stops the test: it is never -# counted as an expected failure. +# container's exit status, read from Docker, not from the client. A setup failure (create, start, +# timeout, or remove) stops the test: it is never counted as an expected failure. A container that +# is still running when the client returns has timed out; the exit trap removes it. # The name is fixed on purpose (docs/hygiene.md section 5): a leftover from an interrupted run is a # conflict that this test reports, not litter that a fresh random name would hide. run_in() { @@ -46,9 +57,12 @@ run_in() { --env CARGO_NET_OFFLINE=true \ "$image" bash -c 'mkdir -p "$HOME" && eval "$1"' smoke "$1" >/dev/null || fail "docker create failed" created=1 - local rc=0 - output="$(perl -e 'alarm 120; exec @ARGV' docker start -a "$name")" || rc=$? - (( rc != 142 )) || fail "container timed out after 120 s" + output="$(deadline 120 docker start -a "$name")" || true + local state running rc start_error + state="$(docker inspect -f '{{.State.Running}} {{.State.ExitCode}} {{.State.Error}}' "$name")" || fail "could not inspect $name" + read -r running rc start_error <<<"$state" + [[ "$running" == false ]] || fail "container timed out after 120 s" + [[ -z "$start_error" ]] || fail "container did not start: $start_error" docker rm -f "$name" >/dev/null || fail "could not remove $name" created=0 return "$rc"