oficina / equipo Public

hosted: pin the bwrap build's RPM closure and build offline

Review img-r1 (1): dnf installed the compiler and its dependencies by
name inside the build, with network, and ran their scriptlets before
anything checked their bytes.

build-hosted-bwrap.sh now pins all 48 RPMs of the build's closure over
the pinned container (gcc, binutils, glibc-devel, libcap-devel, meson,
ninja-build, tar, xz and their dependencies, no weak dependencies), each
by its repository pkgId from AL2023 2023.12.20261102, the same release
as the image's packages and the container. The host fetches each from
its content-addressed URL and checks its SHA-256. Inside the container,
started with --network=none, the sums are checked again, every RPM must
pass rpm -K against Amazon Linux 2023's key, and rpm installs them with
_pkgverify_level all. No package manager resolves anything. The two
builds are still identical and still 1ebd5a3d...; the pin is unchanged.

TestBuildHostedBwrapPinsItsClosure holds the script to this.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev b0b5cd49668222ce45ade9ffe3d6ba5d09c7fee4 parent 22fc85c
2 files changed, +168 −24
  • internal/hostedimage/bwrapbuild_test.go +74 −0
  • scripts/build-hosted-bwrap.sh +94 −24
diff --git a/internal/hostedimage/bwrapbuild_test.go b/internal/hostedimage/bwrapbuild_test.go
new file mode 100644
index 00000000..a5ad92b2
--- /dev/null
+++ b/internal/hostedimage/bwrapbuild_test.go
@@ -0,0 +1,74 @@
+package hostedimage
+
+import (
+ "os"
+ "path/filepath"
+ "regexp"
+ "strings"
+ "testing"
+)
+
+// TestBuildHostedBwrapPinsItsClosure holds scripts/build-hosted-bwrap.sh
+// (which makes the image's bubblewrap) to its build being pinned by bytes
+// before anything runs: every RPM of the build's closure is named with
+// its SHA-256 and fetched from its content-addressed URL on the host,
+// checked there, checked again inside the container with its signature
+// required, and only then installed. The containers that install and
+// compile reach no network, and no package manager resolves anything
+// during the build.
+func TestBuildHostedBwrapPinsItsClosure(t *testing.T) {
+ raw, err := os.ReadFile(filepath.Join(repoRoot, "scripts", "build-hosted-bwrap.sh"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ var code []string // the script without its comments
+ for _, l := range strings.Split(string(raw), "\n") {
+ if t := strings.TrimSpace(l); t != "" && !strings.HasPrefix(t, "#") {
+ code = append(code, l)
+ }
+ }
+ text := strings.Join(code, "\n")
+
+ m := regexp.MustCompile(`(?s)\nRPMS=\(\n(.*?)\n\)`).FindStringSubmatch("\n" + text)
+ if m == nil {
+ t.Fatal("no RPMS=( ... ) list of pinned packages")
+ }
+ entry := regexp.MustCompile(`^\t([A-Za-z0-9][A-Za-z0-9._+-]*\.rpm) ([0-9a-f]{64})$`)
+ names := map[string]bool{}
+ for _, l := range strings.Split(m[1], "\n") {
+ e := entry.FindStringSubmatch(l)
+ if e == nil {
+ t.Fatalf("RPMS entry %q is not NAME.rpm and its SHA-256", l)
+ }
+ names[strings.SplitN(e[1], "-", 2)[0]] = true
+ }
+ for _, need := range []string{"gcc", "cpp", "binutils", "glibc", "libcap", "meson", "ninja", "tar", "xz"} {
+ if !names[need] {
+ t.Errorf("RPMS pins no %s package: the closure is not pinned whole", need)
+ }
+ }
+ for _, want := range []string{
+ "https://cdn.amazonlinux.com/al2023/blobstore/$sha/$name", // content-addressed
+ `[[ $(sum "$rpms/$name") == "$sha" ]]`, // checked on the host
+ "_pkgverify_level all", // signatures required at install
+ } {
+ if !strings.Contains(text, want) {
+ t.Errorf("the script does not %q", want)
+ }
+ }
+ if !regexp.MustCompile(`rpm -K[^\n]*`).MatchString(text) {
+ t.Error("the script does not check each RPM's signature (rpm -K) before installing")
+ }
+ runs := regexp.MustCompile(`podman run[^\n]*`).FindAllString(text, -1)
+ if len(runs) == 0 {
+ t.Fatal("no podman run")
+ }
+ for _, r := range runs {
+ if !strings.Contains(r, "--network=none") {
+ t.Errorf("%q reaches the network", r)
+ }
+ }
+ if regexp.MustCompile(`\b(dnf|microdnf|yum)\b`).MatchString(text) {
+ t.Error("a package manager resolves packages during the build")
+ }
+}
diff --git a/scripts/build-hosted-bwrap.sh b/scripts/build-hosted-bwrap.sh
index 038b53b5..aec6ba79 100755
--- a/scripts/build-hosted-bwrap.sh
+++ b/scripts/build-hosted-bwrap.sh
@@ -8,10 +8,16 @@
# scripts/build-hosted-bwrap.sh [--repin] OUT_DIR
#
# Every input is pinned below: the source tarball by SHA-256, the build
-# container by digest, each build package by its exact version, and the
-# whole build root (every installed package) by one digest. The build runs
-# twice, each time in a fresh container, with SOURCE_DATE_EPOCH and fixed
-# compiler flags and paths, and the two binaries must be the same bytes.
+# container by digest, and every RPM the build installs (the compiler,
+# meson, the headers and all they depend on over the container) by its
+# SHA-256. The RPMs are fetched on the host from their content-addressed
+# URLs and checked there; inside the container each one's signature is
+# checked against Amazon Linux 2023's key before anything is installed,
+# and rpm refuses an unsigned one. The containers that install and compile
+# have no network, and no package manager resolves anything. The build
+# runs twice, each time in a fresh container, with SOURCE_DATE_EPOCH and
+# fixed compiler flags and paths, and the two binaries must be the same
+# bytes.
# The result must also be the pinned hosted bubblewrap
# (infra/hosted/image/manifest.json's BubblewrapSHA256, which
# internal/runner/containment's HostedBubblewrapSHA256 equals); --repin
@@ -22,10 +28,10 @@
# package list and the digest). A release passes OUT_DIR's bwrap to
# scripts/release-hosted.sh in its --image-inputs directory.
#
-# Needs rootless podman and the network (the source tarball and Amazon
-# Linux's package repository). The container image is pulled by digest if
-# it is not already present and is left in place; remove it with
-# podman rmi when done. No AWS account is used.
+# Needs rootless podman and, on the host only, the network (the source
+# tarball, the RPMs and the container image). The container image is
+# pulled by digest if it is not already present and is left in place;
+# remove it with podman rmi when done. No AWS account is used.
set -euo pipefail
BWRAP_VERSION=0.11.0
@@ -35,20 +41,66 @@ BWRAP_URL=https://github.com/containers/bubblewrap/releases/download/v$BWRAP_VER
BWRAP_SHA256=988fd6b232dafa04b8b8198723efeaccdb3c6aa9c1c7936219d5791a8b7a8646
# public.ecr.aws/amazonlinux/amazonlinux:2023.12.20261102.0, linux/amd64:
# the release the hosted image's base (lambda/microvms:al2023-minimal) is
-# built from. Its dnf repository is locked to that release (repoguid).
+# built from (repoguid 0d08b707..., as the image's packages).
IMAGE=public.ecr.aws/amazonlinux/amazonlinux@sha256:3bbae8a3ad757535c4a031e6dbc67b18e6e6cb120436c3f067841da262993455
-PACKAGES=(
- gcc-11.5.0-5.amzn2023.0.5.x86_64
- binutils-2.41-50.amzn2023.0.5.x86_64
- glibc-devel-2.34-231.amzn2023.0.5.x86_64
- libcap-devel-2.73-1.amzn2023.0.8.x86_64
- meson-0.63.3-1.amzn2023.0.2.noarch
- ninja-build-1.10.2-2.amzn2023.0.3.x86_64
- tar-1.34-1.amzn2023.0.4.x86_64
- xz-5.2.5-9.amzn2023.0.2.x86_64
+# The build's RPMs: gcc, binutils, glibc-devel, libcap-devel, meson,
+# ninja-build, tar and xz, with their whole dependency closure over the
+# container (no weak dependencies), all from release 2023.12.20261102.
+# Each SHA-256 is the repository's pkgId (its primary database, checked
+# against repomd.xml), and the URL is the content-addressed blobstore's.
+# Pairs: each line is one RPM's file name and SHA-256.
+RPMS=(
+ amazon-rpm-config-228-11.amzn2023.0.1.noarch.rpm 8aca94832831f034776e2f5ad72f21e98908fef08001269c0f7f83108c062e5a
+ annobin-docs-12.69-1.amzn2023.0.1.noarch.rpm ad85247ead80bc4c649c77c560a84109f54113da73c3d257ad9e459dc1e3e4ea
+ annobin-plugin-gcc-12.69-1.amzn2023.0.1.x86_64.rpm 702514317cde75a130c43fe59bf609cd649be83fcc904c1f759e47f5d9a2b550
+ binutils-2.41-50.amzn2023.0.5.x86_64.rpm 2fc60af14f4e788bf634f0f3a2445bc6d3e69f2c3a4bc6ddfe8052a21ffe0916
+ cpp-11.5.0-5.amzn2023.0.5.x86_64.rpm f3d30581358013d2c4b38e3847c5771cf6ed75ffa2b0a820c1102cb4f4e33d10
+ dwz-0.16-2.amzn2023.0.1.x86_64.rpm c006d4e78303aee56e3231823b00b3b99fad10773c003300b3aaf6d21b708f95
+ efi-srpm-macros-5-4.amzn2023.0.5.noarch.rpm aefbe46f4b9c3c347e1b40e3c60d195dbb5fe755261d3ba413e5467825d8c067
+ elfutils-debuginfod-client-0.188-3.amzn2023.0.3.x86_64.rpm b206deb9032b5814a2aaff0032486dfd7ff7cb31c56271523bb9d71e9c785a86
+ emacs-filesystem-28.2-3.amzn2023.0.13.noarch.rpm 161074c19d30f9884cda810b92f27a9467385d20e67d78360365bb9a34d48d24
+ file-5.39-7.amzn2023.0.4.x86_64.rpm e277d8e882282554c306fdda323c66a6f1f42205bf4d635c4b319d8838d0af27
+ findutils-4.8.0-2.amzn2023.0.2.x86_64.rpm 4763abd3a2a81cb51afa04747abf45ce6c5e78701c067647fc866b7b7db4aca1
+ fonts-srpm-macros-2.0.5-12.amzn2023.0.2.noarch.rpm 890e01220bfb07f52b1236613dae236f0e953ea4a290a16131ead830f796afae
+ gc-8.0.4-5.amzn2023.0.2.x86_64.rpm 7b67c1f680de0109562beb6c0d38c5b73fbdb8b38084fd8c4a1d5f7498ad6934
+ gcc-11.5.0-5.amzn2023.0.5.x86_64.rpm 417e5f14d74b47832303734e61657e8c6e1ca9ac2f763cf6e3f2e69fed3e31a2
+ gcc-plugin-annobin-11.5.0-5.amzn2023.0.5.x86_64.rpm 820505e91964d226e666fcc5c7af36c4bd76453e4849194783a268b349fae4f3
+ ghc-srpm-macros-1.5.0-4.amzn2023.0.2.noarch.rpm dcbdf808089e9ebfcd206184d9442317a32407d64e24b209dd572f664c5d4c82
+ glibc-devel-2.34-231.amzn2023.0.5.x86_64.rpm 2bb98d295e4d06fb55f794ccd2195a60e6fbf4a626409ffd879b580d579952cc
+ glibc-gconv-extra-2.34-231.amzn2023.0.5.x86_64.rpm cfb567c7b66e546d57fdc32dd65cfb5922a1f6840ace8a5f9c05a864c7cb2902
+ glibc-headers-x86-2.34-231.amzn2023.0.5.noarch.rpm 017d35cbf5eeebb06040ce923aaf27537ab30d35be6983f27f5c9f9c12f14eda
+ go-srpm-macros-3.8.0-1.amzn2023.0.1.noarch.rpm 8ee85ca0bc1a1584f792c4de277b6b92620897a4a2bd4d13f20b1e383f917992
+ guile22-2.2.7-2.amzn2023.0.3.x86_64.rpm ca522d7ca0b31ff847bed8e8ac8cc635b8a43ff0607c3071676318d1ca5bc94f
+ jansson-2.14-0.amzn2023.x86_64.rpm 4b17c5ffaf7d5621092e4c58e6fb7d9bd712bd4054fd3848b171bbd1c0810d27
+ kernel-srpm-macros-1.0-14.amzn2023.0.3.noarch.rpm 5eee77e5915f9682165286ab18535be48991a2ee3a33c4d46005719eef926c46
+ kernel6.18-headers-6.18.56-125.164.amzn2023.x86_64.rpm 1e0d6d7d4493e75cbeb0a39dc4c19a1763d97745c3766edd5e739bfa63acc753
+ libcap-devel-2.73-1.amzn2023.0.8.x86_64.rpm d8ee12193098537e96ff24437084d74426ba44180925c8e2c48be893b323e61f
+ libmpc-1.2.1-2.amzn2023.0.2.x86_64.rpm 592d25c4a8348a256613c20a327e900e4420194b5556aeed637bc7c95b02d60c
+ libpkgconf-1.8.0-4.amzn2023.0.2.x86_64.rpm cac5720bf06197fdcebb45751c7632c82c84e1e1a6134b04045ddada315bd366
+ libtool-ltdl-2.4.7-1.amzn2023.0.3.x86_64.rpm 2829b65412cf3e7f627bf6f3440c81d1405344038a2ee6bef11c0090edf5d5cf
+ libxcrypt-devel-4.4.33-7.amzn2023.x86_64.rpm 63f87017e1eefcf48ba9d7e79bda375085b5e599026ce411611fe6d2a65e715d
+ lua-srpm-macros-1-4.amzn2023.0.2.noarch.rpm a4635385a45e9db8e7ec7298f8a865209387cd04519aa7e38a908cef8bcf767a
+ make-4.3-5.amzn2023.0.2.x86_64.rpm a70e9233a140c6fc6bdd2cefdbcad360eea6f5a7635b0020dd08e5d3fae0b97f
+ meson-0.63.3-1.amzn2023.0.2.noarch.rpm 0ce75469c14702429bf07934f33a1a8c6583f9bbf3713b8aad2b902bdad0e9bc
+ ninja-build-1.10.2-2.amzn2023.0.3.x86_64.rpm 3ad8f48a86c9e47ca12397107d7e34fdade758a9e4d010c3aef43b8a55f36b99
+ ocaml-srpm-macros-6-6.amzn2023.0.2.noarch.rpm 8edc9e18a14d0a4a2a1a0ca859498f66350909d9eeb1c4d4404ed2f094f767d2
+ openblas-srpm-macros-2-9.amzn2023.0.2.noarch.rpm 2d32376b3214eb36b22121c571f0841eba01722229d9a6d469feabe6ebeafe38
+ package-notes-srpm-macros-0.4-18.amzn2023.0.5.noarch.rpm c5ea27e9cdea1ac9ec51cdc498abcfca726fef62b289a0bdfb586edb8b507cb5
+ perl-srpm-macros-1-39.amzn2023.0.2.noarch.rpm 1d62079720ffea1f90d9c9f1070020cc865cba1bbb44d9ffe65395a210ce14e4
+ pkgconf-1.8.0-4.amzn2023.0.2.x86_64.rpm dda5ec1c3d6a7f8c25b0baac55d1c2d3c6531b4fd347b958b5c97165e419f536
+ pkgconf-m4-1.8.0-4.amzn2023.0.2.noarch.rpm 8bcb13bff65de797dcb49d43fd88e10fe580f23c8ef75c972563692206cdf518
+ pkgconf-pkg-config-1.8.0-4.amzn2023.0.2.x86_64.rpm a67f35a13a14d95a0cc4408f6713bcffef50f43912b44ecd43887440253b16a3
+ python-srpm-macros-3.9-41.amzn2023.0.6.noarch.rpm 1d5ed29e2a350a6664ebabe04a041a557f0377e29f68b49bddcd915f06f68280
+ python3-setuptools-59.6.0-2.amzn2023.0.6.noarch.rpm ab7d6b19735439ed54ed4248846bfe0937872bdad7906c54d70e04b7353a8228
+ rust-toolset-srpm-macros-1.99.0-1.amzn2023.noarch.rpm fd4475cc66ce5e8f83839397ce4a22a7e624ea71b8c95fc69d9f3f3f68aa3da5
+ tar-1.34-1.amzn2023.0.4.x86_64.rpm 06d66285ecaec0f943b4dc24290c6dfefd291f1d2d6e0a3fb26b966ee7c27f55
+ unzip-6.0-68.amzn2023.0.2.x86_64.rpm 62ed7ebfb7a6bae33ce9f36832844954fddf0bd1765a26df220190c43454db64
+ vim-filesystem-9.2.920-1.amzn2023.0.1.noarch.rpm ebd419cb44a2cbe07174baa179a82979fcef7729ebe8845a909dbf8afe3ad057
+ xz-5.2.5-9.amzn2023.0.2.x86_64.rpm 1845134e5d0e5cbf56bddd147359de6f934d3b957d01f78e97b0ea0b29d0dc44
+ zip-3.0-28.amzn2023.0.4.x86_64.rpm ff81ebc3568501611146772656b374eec642e6eea5356ace803fd2093df1caa0
)
# SHA-256 of the build root's package list (rpm -qa NEVRAs, sorted, one
-# per line) once PACKAGES are installed: nothing else may differ.
+# per line) once RPMS are installed: nothing else may differ.
BUILDROOT_SHA256=aeb556a0b03311f45d161f928cf96e8cf44ad797b9a26f982d7db79dc74c370d
# The release's date (2024-10-30T16:28:43Z), for anything that stamps one.
SOURCE_DATE_EPOCH=1730305723
@@ -83,14 +135,32 @@ curl --fail --silent --show-error --location --max-redirs 5 --proto '=https' --p
--max-time 300 --output "$scratch/src/bubblewrap.tar.xz" "$BWRAP_URL" || die "downloading $BWRAP_URL failed"
[[ $(sum "$scratch/src/bubblewrap.tar.xz") == "$BWRAP_SHA256" ]] || die "the source tarball is not $BWRAP_SHA256"
+rpms=$scratch/rpms
+mkdir -p "$rpms"
+echo "build-hosted-bwrap: fetching $((${#RPMS[@]} / 2)) RPMs"
+: >"$rpms/SHA256SUMS"
+for ((i = 0; i < ${#RPMS[@]}; i += 2)); do
+ name=${RPMS[i]} sha=${RPMS[i + 1]}
+ [[ $name == *.rpm && $sha =~ ^[0-9a-f]{64}$ ]] || die "RPMS entry $name $sha is not NAME.rpm SHA256"
+ curl --fail --silent --show-error --location --max-redirs 5 --proto '=https' --proto-redir '=https' \
+ --max-time 300 --output "$rpms/$name" "https://cdn.amazonlinux.com/al2023/blobstore/$sha/$name" || die "downloading $name failed"
+ [[ $(sum "$rpms/$name") == "$sha" ]] || die "$name is not its pinned $sha"
+ echo "$sha /rpms/$name" >>"$rpms/SHA256SUMS"
+done
+
# The build, run as root inside the container. Fixed paths (/build), a
# fixed environment, explicit flags, no build directory or date in the
# binary, the symbol table stripped.
cat >"$scratch/src/build.sh" <<'EOF'
set -eu
-dnf --assumeyes --quiet --setopt=install_weak_deps=False install "$@" >/dev/null
+sha256sum --check --strict --quiet /rpms/SHA256SUMS
+rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-amazon-linux-2023
+test "$(rpm -q --qf '%{VERSION}\n' gpg-pubkey)" = d832c631
+for f in /rpms/*.rpm; do
+ rpm -K "$f" | grep -q ': digests signatures OK$' || { echo "build root: $f is not signed by Amazon Linux 2023's key" >&2; exit 1; }
+done
+rpm --install --quiet --define '_pkgverify_level all' /rpms/*.rpm
rpm -qa --qf '%{NAME}-%{EPOCH}:%{VERSION}-%{RELEASE}.%{ARCH}\n' | sed 's/-(none):/-/' | LC_ALL=C sort >/out/buildroot.txt
-for p in "$@"; do rpm -q "$p" >/dev/null || { echo "build root: $p is not installed" >&2; exit 1; }; done
mkdir /build
cd /build
tar -xJf /src/bubblewrap.tar.xz
@@ -108,13 +178,13 @@ build() { # N
local dir=$scratch/out$1
mkdir -p "$dir"
echo "build-hosted-bwrap: build $1 in a fresh $IMAGE"
- podman run --rm --pull=missing \
+ podman run --rm --pull=missing --network=none \
--env LC_ALL=C --env LANG=C --env TZ=UTC --env SOURCE_DATE_EPOCH=$SOURCE_DATE_EPOCH \
--env BWRAP_VERSION=$BWRAP_VERSION \
--env CFLAGS="-O2 -g0 -pipe -fstack-protector-strong -D_FORTIFY_SOURCE=2 -fno-ident -ffile-prefix-map=/build=. -frandom-seed=bwrap" \
--env LDFLAGS="-Wl,-z,relro -Wl,-z,now -Wl,--build-id=sha1" \
- --volume "$scratch/src:/src:ro" --volume "$dir:/out" \
- "$IMAGE" sh -eu /src/build.sh "${PACKAGES[@]}" || die "build $1 failed"
+ --volume "$scratch/src:/src:ro" --volume "$rpms:/rpms:ro" --volume "$dir:/out" \
+ "$IMAGE" sh -eu /src/build.sh || die "build $1 failed"
[[ -f $dir/bwrap ]] || die "build $1 made no bwrap"
}
build 1