diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c7ce88a..e8c8853 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -4,8 +4,8 @@ on: push: branches: [main] # Release track gated by tag prefix: - # stage2-v* → the reusable loader binaries (append your own container image to run it). - tags: ['stage2-v*'] + # v* → the reusable loader binaries (append your own container image to run it). + tags: ['v*'] pull_request: branches: [main] workflow_dispatch: @@ -45,6 +45,7 @@ jobs: run: | make pack-${{ matrix.arch }} cp target/${{ matrix.arch }}-unknown-linux-musl/release/bootstrap stage2-loader-${{ matrix.arch }} + cp tools/stage2.replay.json . # The reusable loader binary + a packed demo (loader + the pivot-init /init) for inspection # (`unzip -l`) or a local boot test. @@ -54,13 +55,14 @@ jobs: name: stage2-${{ matrix.arch }} path: | stage2-loader-${{ matrix.arch }} + stage2.replay.json build/${{ matrix.arch }}/pack/stage2 - # ---- Release track (tag: stage2-v*): the reusable loader binaries --------- + # ---- Release track (tag: v*): the reusable loader binaries --------- release: runs-on: ubuntu-latest needs: build - if: startsWith(github.ref, 'refs/tags/stage2-v') + if: startsWith(github.ref, 'refs/tags/v') permissions: contents: write id-token: write @@ -79,6 +81,7 @@ jobs: subject-path: | stage2-loader-x86_64 stage2-loader-aarch64 + stage2.replay.json - name: Create release uses: softprops/action-gh-release@718ea10b132b3b2eba29c1007bb80653f286566b # v3.0.1 @@ -87,7 +90,102 @@ jobs: files: | stage2-loader-x86_64 stage2-loader-aarch64 + stage2.replay.json draft: false prerelease: true generate_release_notes: true make_latest: true + + # ---- erofs-builder image (tag: v*) --------------------------------- + # End-user forge: mkruntime + pinned erofs/verity/ext4 tools + this release's loaders for BOTH + # arches baked in (`--arch `), so packing needs no separate loader download. Multi-arch so + # it runs identically on amd64 and arm64 build hosts. Consumers pin the digest appended to the + # release notes below, not the tag. + release-erofs-builder: + runs-on: ubuntu-latest + needs: release + if: startsWith(github.ref, 'refs/tags/v') + permissions: + contents: write + packages: write + id-token: write + attestations: write + + steps: + - name: Checkout repository + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 + + # The same artifacts the `release` job attested and published. + - name: Download loader artifacts + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + with: + pattern: stage2-* + merge-multiple: true + + - name: Set up QEMU (arm64 layers on the amd64 runner) + uses: docker/setup-qemu-action@99012661954931238ded8c8b007157a8430204e1 # v4.4.0 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v4.1.0 + + - name: Log in to GitHub Container Registry + uses: docker/login-action@650006c6eb7dba73a995cc03b0b2d7f5ca915bee # v4.2.0 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata for Docker + id: meta + uses: docker/metadata-action@80c7e94dd9b9319bd5eb7a0e0fe9291e23a2a2e9 # v6.1.0 + with: + images: ghcr.io/${{ github.repository }}-erofs-builder + tags: | + type=ref,event=tag + flavor: | + latest=false + + - name: Build and push multi-arch erofs-builder + id: push + uses: docker/build-push-action@f9f3042f7e2789586610d6e8b85c8f03e5195baf # v7.2.0 + with: + context: . + file: ./Dockerfile.erofs-builder + target: release + platforms: linux/amd64,linux/arm64 + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + annotations: ${{ steps.meta.outputs.annotations }} + provenance: true + sbom: true + + - name: Attest erofs-builder image + uses: actions/attest-build-provenance@977bb373ede98d70efdf65b84cb5f73e068dcc2a # v3.0.0 + with: + subject-name: ghcr.io/${{ github.repository }}-erofs-builder + subject-digest: ${{ steps.push.outputs.digest }} + push-to-registry: true + + - name: Append image digest + baked loader hashes to the release notes + env: + GH_TOKEN: ${{ github.token }} + TAG: ${{ github.ref_name }} + DIGEST: ${{ steps.push.outputs.digest }} + run: | + gh release view "$TAG" -R "${{ github.repository }}" --json body -q .body > notes.md + { + echo + echo "### erofs-builder image" + echo + echo '```' + echo "ghcr.io/${{ github.repository }}-erofs-builder@${DIGEST}" + echo '```' + echo + echo "Bakes in (\`--arch \`):" + echo + echo '```' + sha256sum stage2-loader-x86_64 stage2-loader-aarch64 + echo '```' + } >> notes.md + gh release edit "$TAG" -R "${{ github.repository }}" --notes-file notes.md diff --git a/Dockerfile.build b/Dockerfile.build index f9df7d5..f9f73b1 100644 --- a/Dockerfile.build +++ b/Dockerfile.build @@ -22,7 +22,6 @@ RUN apt-get -qq update && \ openssl \ ovmf \ python3 \ - python3-pip \ qemu-efi-aarch64 \ rpm \ rpm2cpio \ @@ -30,8 +29,7 @@ RUN apt-get -qq update && \ util-linux \ uuid-runtime \ wget \ - xz-utils && \ - pip3 install --break-system-packages virt-firmware + xz-utils # Reproducible builds environment ENV SOURCE_DATE_EPOCH=0 diff --git a/Dockerfile.erofs-builder b/Dockerfile.erofs-builder index c0a757a..6a7c868 100644 --- a/Dockerfile.erofs-builder +++ b/Dockerfile.erofs-builder @@ -12,19 +12,32 @@ # of what the script uses -- touch -d @epoch, stat -c, tar, install, awk -- so no coreutils. # # Kept entirely separate from the canonical Dockerfile.build (which the top-level Makefile syncs -# byte-identically into the harness repos and must stay lean) — nothing here touches it. Alpine -# is pinned so the tools' *output* is reproducible; the artifacts are byte-verified CI==local. -# TODO: pin by digest (FROM alpine@sha256:...) once chosen, same discipline as stage1's pins. -FROM alpine:3.22 +# byte-identically into the harness repos and must stay lean) -- nothing here touches it. +# +# This is an END-USER artifact: its tools determine the bytes of rootfs.erofs + the verity root, +# i.e. the packed stage2's hash and what PCR14 sees. So the base is pinned by digest (multi-arch +# index) and every package by exact version -- a rebuild after Alpine moves a package fails loudly +# instead of silently forging a different payload. Bump pins together and re-verify CI==local. +# +# Two stages: +# tools the forge alone; `make docker-build-erofs` builds this for local dev, where `make pack` +# passes a freshly built loader via --bootstrap. +# release tools + the release's attested stage2-loader-{x86_64,aarch64} (from the build +# context), so `--arch ` packs with the released loader -- no separate download. +# Published multi-arch (linux/amd64 + linux/arm64) to ghcr on stage2-v* tags. +FROM alpine:3.22@sha256:2e0b81d60597f449baa56dc25d355367869d2dca4cd57e866f1e2b52af3eb55f AS tools RUN apk add --no-cache \ - erofs-utils \ - cryptsetup \ - e2fsprogs \ - zip + erofs-utils=1.8.6-r0 \ + cryptsetup=2.7.5-r1 \ + e2fsprogs=1.47.2-r2 \ + zip=3.0-r13 COPY tools/mkruntime.sh /usr/local/bin/mkruntime RUN chmod 0755 /usr/local/bin/mkruntime WORKDIR /work ENTRYPOINT ["/usr/local/bin/mkruntime"] + +FROM tools AS release +COPY --chmod=0755 stage2-loader-x86_64 stage2-loader-aarch64 /usr/local/share/lockboot/ diff --git a/Makefile b/Makefile index 40b2ee8..d20c712 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ # The container image travels as a self-extracting zip appended to the loader (make pack); # tests borrow the stage0 harness + existing swtpm, exactly as stage1's chain test does. -.PHONY: all lock check test fmt fmt-fix clippy ci build build-x86_64 build-aarch64 clean docker-build-base docker-build-erofs docker-shell-base pack +.PHONY: all lock check test fmt fmt-fix clippy ci build build-amd64 build-x86_64 build-arm64 build-aarch64 clean docker-build-base docker-build-erofs docker-shell-base pack pack-amd64 pack-arm64 ARCHS = x86_64 aarch64 all: build @@ -27,7 +27,7 @@ docker-build-base: # Build-time-only forge for the runtime-image artifacts (mkfs.erofs + veritysetup + mkfs.ext4); # nothing it produces ships to the instance. See Dockerfile.erofs-builder + tools/mkruntime.sh. docker-build-erofs: - docker build -f Dockerfile.erofs-builder -t $(EROFS_IMAGE) . + docker build -f Dockerfile.erofs-builder --target tools -t $(EROFS_IMAGE) . # ---- Docker run plumbing (keep identical across repos; mirrors stage0/stage1) ---- USER_ID := $(shell stat -c %u .) @@ -101,6 +101,8 @@ ci: docker-build-base # ---- Build the loader (payload-agnostic; the image is appended separately by `pack`) ---- build: build-x86_64 +build-amd64: build-x86_64 +build-arm64: build-aarch64 build-x86_64 build-aarch64: build-%: $(DOCKER_RUN) $(DOCKER_SAMEUSER) $(BUILD_IMAGE) bash -c "\ rustup target add $*-unknown-linux-musl && \ @@ -116,6 +118,8 @@ INIT_PAYLOAD ?= pivot-init pack: @echo "needs an arch suffix, e.g. 'make pack-x86_64'." >&2; exit 2 +pack-amd64: pack-x86_64 +pack-arm64: pack-aarch64 pack-%: docker-build-base docker-build-erofs $(DOCKER_RUN) $(DOCKER_SAMEUSER) $(BUILD_IMAGE) bash -c "\ rustup target add $*-unknown-linux-musl && \ @@ -134,74 +138,14 @@ clean: $(DOCKER_RUN) $(DOCKER_SAMEUSER) $(BUILD_IMAGE) $(CARGO) clean # ---- Boot-through-harness test (borrows the sibling stage0 boot.disk + stage1 UKI) ---- -# Boots stage0 -> stage1 UKI -> our payload under QEMU/KVM + swtpm, exactly as stage1's -# chain test does. PAYLOAD selects which built example/bin to admit as stage2 (default: the -# derive probe). sha256 admission; no signing needed for a local boot test. +# Boots stage0 -> stage1 UKI -> our packed stage2 under QEMU/KVM + swtpm, exactly as stage1's +# chain test does. sha256 admission; no signing needed for a local boot test. STAGE0_DIR ?= ../stage0 STAGE0_BOOT_DISK ?= $(STAGE0_DIR)/build/$*/boot.disk STAGE1_DIR ?= ../stage1 STAGE1_UKI ?= $(STAGE1_DIR)/tools/build-uki/$*/linux.efi SERVE_HOST ?= 10.0.2.1:8000 -PAYLOAD ?= derive-probe PERSIST_GROW ?= 268435456 # bytes to grow the boot.disk copy (room for p2 + p3) -PERSIST_PAYLOAD ?= blockdev-probe - -.PHONY: test-chain -test-chain: - @echo "needs an arch suffix, e.g. 'make test-chain-x86_64'." >&2; exit 2 - -test-chain-%: - @[ -f "$(STAGE0_BOOT_DISK)" ] || { echo "missing stage0 boot.disk: $(STAGE0_BOOT_DISK) (cd $(STAGE0_DIR) && make build-$*)" >&2; exit 1; } - @[ -f "$(STAGE1_UKI)" ] || { echo "missing stage1 UKI: $(STAGE1_UKI) (cd $(STAGE1_DIR) && make $*)" >&2; exit 1; } - $(DOCKER_RUN) $(DOCKER_SAMEUSER) $(BUILD_IMAGE) bash -c "\ - rustup target add $*-unknown-linux-musl && \ - $(CARGO) build --release --locked --target $*-unknown-linux-musl \ - -p bootstrap --example $(PAYLOAD)" - @D="build/$*/chain"; rm -rf "$$D"; mkdir -p "$$D"; H="http://$(SERVE_HOST)"; \ - cp "$(STAGE1_UKI)" "$$D/linux.efi"; \ - cp "target/$*-unknown-linux-musl/release/examples/$(PAYLOAD)" "$$D/stage2"; \ - UKI_SHA=$$(sha256sum "$$D/linux.efi" | cut -d' ' -f1); \ - S2_SHA=$$(sha256sum "$$D/stage2" | cut -d' ' -f1); \ - printf '{\n "_stage1": { "%s": { "payload": { "url": "%s/linux.efi", "sha256": "%s" } } },\n "_stage2": { "%s": { "payload": { "url": "%s/stage2", "sha256": "%s" } } }\n}\n' \ - "$*" "$$H" "$$UKI_SHA" "$*" "$$H" "$$S2_SHA" > "$$D/user-data.json"; \ - echo "user-data: UKI $$UKI_SHA stage2($(PAYLOAD)) $$S2_SHA"; \ - $(DOCKER_RUN) $(DOCKER_OPT_KVM) \ - -e YES_INSIDE_DOCKER_DO_DANGEROUS_IPTABLES=1 --cap-add=NET_ADMIN --device=/dev/net/tun \ - $(HARNESS_IMAGE) --kind stage0 --arch $* \ - --boot-disk "$(STAGE0_BOOT_DISK)" \ - --serve-dir "$$D" --user-data "$$D/user-data.json" - -# ---- Persistence dry-run: GPT rebuild + kernel re-read + dm-crypt on a GROWN disk ---- -# Like test-chain, but boots a WRITABLE, grown copy of stage0's boot.disk so blockdev can -# add p2/p3 into the new tail. Admits the blockdev-probe (partition -> dm-crypt -> sentinel). -.PHONY: test-persist -test-persist: - @echo "needs an arch suffix, e.g. 'make test-persist-x86_64'." >&2; exit 2 - -test-persist-%: - @[ -f "$(STAGE0_BOOT_DISK)" ] || { echo "missing stage0 boot.disk: $(STAGE0_BOOT_DISK)" >&2; exit 1; } - @[ -f "$(STAGE1_UKI)" ] || { echo "missing stage1 UKI: $(STAGE1_UKI)" >&2; exit 1; } - $(DOCKER_RUN) $(DOCKER_SAMEUSER) $(BUILD_IMAGE) bash -c "\ - rustup target add $*-unknown-linux-musl && \ - $(CARGO) build --release --locked --target $*-unknown-linux-musl \ - -p bootstrap --example $(PERSIST_PAYLOAD)" - @D="build/$*/persist"; rm -rf "$$D"; mkdir -p "$$D"; H="http://$(SERVE_HOST)"; \ - cp "$(STAGE1_UKI)" "$$D/linux.efi"; \ - cp "target/$*-unknown-linux-musl/release/examples/$(PERSIST_PAYLOAD)" "$$D/stage2"; \ - cp "$(STAGE0_BOOT_DISK)" "$$D/boot.disk"; \ - cp "$$(dirname "$(STAGE0_BOOT_DISK)")/efi-vars.ovmf" "$$D/efi-vars.ovmf"; \ - truncate -s +$(PERSIST_GROW) "$$D/boot.disk"; \ - echo "grown boot.disk -> $$(stat -c%s "$$D/boot.disk") bytes"; \ - UKI_SHA=$$(sha256sum "$$D/linux.efi" | cut -d' ' -f1); \ - S2_SHA=$$(sha256sum "$$D/stage2" | cut -d' ' -f1); \ - printf '{\n "_stage1": { "%s": { "payload": { "url": "%s/linux.efi", "sha256": "%s" } } },\n "_stage2": { "%s": { "payload": { "url": "%s/stage2", "sha256": "%s" } } }\n}\n' \ - "$*" "$$H" "$$UKI_SHA" "$*" "$$H" "$$S2_SHA" > "$$D/user-data.json"; \ - echo "user-data: UKI $$UKI_SHA stage2($(PERSIST_PAYLOAD)) $$S2_SHA"; \ - $(DOCKER_RUN) $(DOCKER_OPT_KVM) \ - -e YES_INSIDE_DOCKER_DO_DANGEROUS_IPTABLES=1 --cap-add=NET_ADMIN --device=/dev/net/tun \ - $(HARNESS_IMAGE) --kind stage0 --arch $* \ - --boot-disk "$$D/boot.disk" \ - --serve-dir "$$D" --user-data "$$D/user-data.json" # ---- Full loader test: verity erofs root under an ephemeral overlay + encrypted /data + pivot ---- # `pack`s the loader + a pivot-init /init into a stage2 (loader ELF + appended payload.zip), then @@ -230,4 +174,35 @@ test-pivot-%: pack-% -e YES_INSIDE_DOCKER_DO_DANGEROUS_IPTABLES=1 --cap-add=NET_ADMIN --device=/dev/net/tun \ $(HARNESS_IMAGE) --kind stage0 --arch $* \ --boot-disk "$$D/boot.disk" \ - --serve-dir "$$D" --user-data "$$D/user-data.json" + --serve-dir "$$D" --user-data "$$D/user-data.json" \ + $(if $(HARNESS_TIMEOUT),--timeout $(HARNESS_TIMEOUT)) + +# ---- Self-test: stage2's part of the workspace `make selftest` (PASS/FAIL, non-zero on FAIL) ---- +# Two full-chain boots through test-pivot: +# pivot pivot-init on the default grown disk: config + attestation + /data resize + persistence +# + overlay-root ephemerality across an in-guest reboot (PIVOT_TEST=PASS). +# grow grow-probe on a 10 GiB sparse grow: /data grows past one ext4 meta_bg (~8 GiB), fresh +# read/write, sparse hole reads zero (GROW_TEST=PASS). The sparse file costs no host disk; +# the time is guest-side resize + I/O (~30 s measured on both arches). +# Every boot is time-bounded (HARNESS_TIMEOUT -> qemu-test.sh --timeout). Needs the sibling stage0 +# boot.disk + stage1 UKI and the lockboot:harness image (workspace `make selftest-`). +SELFTEST_TIMEOUT_x86_64 ?= 300 +SELFTEST_TIMEOUT_aarch64 ?= 300 +.PHONY: selftest +selftest: + @echo "needs an arch suffix, e.g. 'make selftest-x86_64'." >&2; exit 2 +selftest-%: + @fail=0; T=$(SELFTEST_TIMEOUT_$*); sum="build/$*/selftest.summary"; mkdir -p build/$*; : > "$$sum"; \ + for m in "pivot:PIVOT_TEST=PASS:INIT_PAYLOAD=pivot-init" "grow:GROW_TEST=PASS:INIT_PAYLOAD=grow-probe PERSIST_GROW=10G"; do \ + name="$${m%%:*}"; rest="$${m#*:}"; marker="$${rest%%:*}"; vars="$${rest#*:}"; \ + log="build/$*/selftest-$$name.log"; \ + echo "==================== stage2 test [$$name] $$vars -> $$log"; \ + $(MAKE) --no-print-directory test-pivot-$* $$vars HARNESS_TIMEOUT=$$T > "$$log" 2>&1 || true; \ + if grep -q "$$marker" "$$log"; then \ + echo "PASS [$$name]" | tee -a "$$sum"; \ + else \ + echo "FAIL [$$name] (see $$log)" | tee -a "$$sum"; fail=1; \ + fi; \ + done; \ + echo "==================== stage2 self-test summary ($*)"; cat "$$sum"; \ + if [ "$$fail" = 0 ]; then echo "ALL STAGE2 SELF-TESTS PASSED ($*)"; else echo "SOME STAGE2 SELF-TESTS FAILED ($*)"; exit 1; fi diff --git a/PCR-BINDING.md b/PCR-BINDING.md index 1128696..bee37ab 100644 --- a/PCR-BINDING.md +++ b/PCR-BINDING.md @@ -198,18 +198,6 @@ dm-verity). Revisit only if binding the GPT itself becomes desirable. securityfs first) on both boots and diff — expected to show the boot aggregate as the only varying entry, and to enumerate exactly which modules IMA measures. -## How to reproduce / investigate - -```bash -# In-guest reboot test (two boots, one QEMU invocation), prints key_fp + all 24 PCRs/boot: -make test-persist-x86_64 PERSIST_PAYLOAD=reboot-probe -# Diff the two boots' pcrNN= lines to see which PCRs move. -``` - -- `tpm_derive::read_all_pcrs()` — reads all 24 PCRs (SHA-256 bank) for cross-boot diffing. -- `reboot-probe` prints `key_fp`, the raw p3 ciphertext at the sentinel offset, and every - PCR on each boot. - ## References - TCG PC Client Platform Firmware Profile — PCR usage, `EV_EFI_GPT_EVENT`, `EV_EFI_ACTION`, diff --git a/README.md b/README.md index 0c42e50..2bdae7c 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,24 @@ packaging: `docker export → mkfs.erofs → veritysetup → zip → append to t ## Using it +### From a release + +Each `v*` release publishes a multi-arch (`linux/amd64` + `linux/arm64`) erofs-builder image +with that release's loaders for both arches baked in, so there is no separate loader download. Pin +it by the digest in the release notes, and verify its provenance first: + +```bash +IMG=ghcr.io/lockboot/stage2-erofs-builder@sha256: +gh attestation verify "oci://$IMG" --repo lockboot/stage2 +docker export "$(docker create --rm myimage)" \ + | docker run --rm -i "$IMG" --arch aarch64 - - > stage2 # or --arch x86_64 +``` + +`--arch` picks the target arch of the loader, independent of the host's: an amd64 host can pack an +aarch64 stage2 and vice versa, with identical output. + +### From source + Build the loader and the packaging image once: ```bash diff --git a/crates/bootstrap/examples/pcr-dump.rs b/crates/bootstrap/examples/pcr-dump.rs new file mode 100644 index 0000000..84c9c39 --- /dev/null +++ b/crates/bootstrap/examples/pcr-dump.rs @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 + +//! Boot-through-harness `/init` that prints the PCR state the payload starts with: every PCR in +//! every bank as `PCR ` (the format workspace tools/pcr-replay `--check` reads), +//! plus the sha256 of the config the loader forwarded on stdin (the digest bound into PCR15). +//! Runs as PID 1 after the loader has pivoted (/sys is moved in). Not production. + +use std::fs; +use std::io::Read; + +fn main() { + println!("=== pcr-dump (PID {}) ===", std::process::id()); + let mut cfg = Vec::new(); + match std::io::stdin().read_to_end(&mut cfg) { + Ok(_) => { + let d = tpm_derive::config_digest(&cfg); + let hex: String = d.iter().map(|b| format!("{b:02x}")).collect(); + println!("CONFIG bytes={} sha256={hex}", cfg.len()); + } + Err(e) => println!("CONFIG error: {e}"), + } + for bank in ["sha1", "sha256", "sha384", "sha512"] { + for i in 0..24 { + if let Ok(v) = fs::read_to_string(format!("/sys/class/tpm/tpm0/pcr-{bank}/{i}")) { + println!("PCR {bank} {i:02} {}", v.trim().to_lowercase()); + } + } + } + println!("=== PCR-DUMP-END ==="); + // Let the console drain before powering off: a 115200-baud serial console is still sending + // the last lines when the kernel powers down (GCP lost the tail of the dump without this). + let _ = std::io::Write::flush(&mut std::io::stdout()); + std::thread::sleep(std::time::Duration::from_secs(3)); + // SAFETY: PID 1 power-off; sync then poweroff. + unsafe { + libc::sync(); + libc::reboot(libc::LINUX_REBOOT_CMD_POWER_OFF); + } +} diff --git a/crates/bootstrap/src/main.rs b/crates/bootstrap/src/main.rs index ad465a3..dbfb216 100644 --- a/crates/bootstrap/src/main.rs +++ b/crates/bootstrap/src/main.rs @@ -87,6 +87,17 @@ fn run() -> Result<()> { ) .context("mount overlay root")?; + // The resolver stage1 learned by DHCP (udhcpc writes it to our /etc). The workload's image + // carries only an empty placeholder (docker export leaves one), so without this it resolves + // nothing, and musl falls back to 127.0.0.1 and times out. Written to the tmpfs upper: the + // image is untouched, and it's DHCP's data, untrusted like the network it describes. + // Best-effort: a workload that needs no DNS boots either way. + if let Ok(conf) = std::fs::read("/etc/resolv.conf") { + if let Err(e) = std::fs::write(format!("{NEWROOT}/etc/resolv.conf"), conf) { + eprintln!("loader: resolv.conf not passed on: {e}"); + } + } + // Encrypted persistent /data inside the new root — the ONLY thing that survives a reboot. pivot::mkdir(DATA); let reinit = blockdev::crypt_mount_or_mkfs(&data_part, &key, &payload.data_base, DATA) diff --git a/tools/mkruntime.sh b/tools/mkruntime.sh index c011f65..2d19e89 100644 --- a/tools/mkruntime.sh +++ b/tools/mkruntime.sh @@ -8,7 +8,8 @@ # fill the disk; meta_bg => unbounded growth). # verity.json { root_hash, salt, data_blocks } for the loader (payload::Verity). # With --bootstrap, the loader ELF is prepended and the zip self-adjusted, so the output is a -# ready-to-run packed stage2 (what `make pack` produces) instead of a bare zip. +# ready-to-run packed stage2 (what `make pack` produces) instead of a bare zip. The released image +# bakes in the release's loaders for both arches; --arch selects one of those. # # Reproducible: with default knobs the output is byte-identical across builds (fixed UUID + salt, # SOURCE_DATE_EPOCH, no lazy init, fixed mtimes + `zip -X`). Byte-verified CI==local. @@ -18,10 +19,11 @@ usage() { cat <<'EOF' mkruntime.sh -- forge the deterministic stage2 payload (runs in lockboot:erofs-builder). -Usage: mkruntime.sh [--bootstrap ] +Usage: mkruntime.sh [--arch | --bootstrap ] - --bootstrap prepend this loader ELF and self-adjust the zip, so is a - ready-to-run packed stage2 (ELF + payload) instead of a bare zip. + --arch prepend the loader for baked into this image (the released + stage2-loader-), so is a ready-to-run packed stage2. + --bootstrap prepend this loader ELF instead (e.g. a local build), same result. one of: - read a tar stream on stdin (a container rootfs) a tar archive of a container rootfs @@ -39,17 +41,21 @@ Examples: # bare payload.zip from a single static PID-1 binary: mkruntime.sh ./myinit payload.zip - # a ready-to-run stage2 from a Docker image (no docker dependency baked in here): + # a ready-to-run aarch64 stage2 from a Docker image, using the baked-in loader: docker export "$(docker create --rm myimage)" | \ - mkruntime.sh --bootstrap ./bootstrap - stage2 + mkruntime.sh --arch aarch64 - stage2 EOF } +LOADER_DIR=/usr/local/share/lockboot BOOTSTRAP="" +ARCH="" while [ $# -gt 0 ]; do case "$1" in --bootstrap) BOOTSTRAP="${2:?--bootstrap needs an path}"; shift 2 ;; --bootstrap=*) BOOTSTRAP="${1#*=}"; shift ;; + --arch) ARCH="${2:?--arch needs x86_64 or aarch64}"; shift 2 ;; + --arch=*) ARCH="${1#*=}"; shift ;; -h|--help) usage; exit 0 ;; --) shift; break ;; --*) echo "unknown option: $1" >&2; usage >&2; exit 2 ;; @@ -57,6 +63,15 @@ while [ $# -gt 0 ]; do esac done +if [ -n "$ARCH" ]; then + [ -z "$BOOTSTRAP" ] || { echo "--arch and --bootstrap are mutually exclusive" >&2; exit 2; } + case "$ARCH" in + x86_64|aarch64) BOOTSTRAP="$LOADER_DIR/stage2-loader-$ARCH" ;; + *) echo "--arch: expected x86_64 or aarch64, got: $ARCH" >&2; exit 2 ;; + esac + [ -f "$BOOTSTRAP" ] || { echo "--arch: no baked loader at $BOOTSTRAP (dev image? use --bootstrap)" >&2; exit 1; } +fi + SRC_ROOTFS="${1:?missing ; see 'mkruntime.sh --help'}" OUT="${2:?missing ; see 'mkruntime.sh --help'}" diff --git a/tools/stage2.replay.json b/tools/stage2.replay.json new file mode 100644 index 0000000..322a546 --- /dev/null +++ b/tools/stage2.replay.json @@ -0,0 +1,21 @@ +{ + "schema": "pcr-replay/v1", + "name": "stage2", + "about": "The stage2 loader, from stage1's handoff to its payload's /init: it binds the config it was handed (stdin) into PCR15. The config bytes are the user-data document verbatim when _stage2 was admitted directly, or stage1's re-serialization of the manifest-merged document when it was resolved through a signed manifest. A verifier needs only their sha256, which the operator can disclose without the contents (stage2 records it in /run/lockboot/stage2.attest); the hash hides the config only if it is not guessable, so include a random nonce if that matters. Same for both arches; static (workspace tools/pcr-replay verifies it).", + "inputs": { + "config": {"about": "sha256 of the exact config bytes stage2 received on stdin"} + }, + "unextended": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23], + "unpredictable": {"10": "IMA (the kernel's own measurement log)"}, + "events": [ + { + "pcr": 15, + "type": "unlogged", + "by": "stage2", + "about": "TPM2_PCR_Extend with the raw sha256(config), sha256 bank only (tpm-derive extend_config_pcr); other banks' PCR15 stay at reset", + "derive": {"input": "config"}, + "digest": "raw", + "banks": ["sha256"] + } + ] +}