From e827dd6f818964f440f1d24c8e33c30193ff416e Mon Sep 17 00:00:00 2001 From: diviocity Date: Mon, 30 Nov 2026 17:37:40 +0100 Subject: [PATCH] Assemble a Vinix desktop initramfs from signed packages --- tests/FEED.md | 9 +++++ tests/native-layer.py | 78 ++++++++++++++++++++++++++++++++++++++++ tools/stage-image.py | 84 ++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 tests/native-layer.py diff --git a/tests/FEED.md b/tests/FEED.md index a93b371..d1345d9 100644 --- a/tests/FEED.md +++ b/tests/FEED.md @@ -151,6 +151,15 @@ python3 tools/stage-image.py "$arch" "$client" "$repo" "$output" "$work/audit" " The tool audits the package-owned root, makes a separate copy, and seeds the verification keyring using the distributor's fingerprint. It copies the local package records into `/var/lib/vpac/local`, checks their versions against the signed database, and installs the repository under `/var/lib/vinix/repository/$arch`. It verifies the database, file index and each included package signature, and compares the payload hashes with the signed database. The sync cache receives independent copies of the database and signature. Assembly exposes the final root only after these checks pass. +Pass the native desktop programs and assets in a staging directory with `--native-layer`. Assembly rejects a native file at a package-owned path. `native-layer.tsv` records the installed file hashes and the symbolic link targets. `--initramfs` writes the root-owned USTAR archive for Vinix's ISO builder. + +```sh +python3 tools/stage-image.py "$arch" "$client" "$repo" "$output" "$work/audit" \ + "$trusted_fingerprint" --native-layer "$desktop_layer" --initramfs +``` + +Use `$output/initramfs.tar` as `VINIX_AMD64_INITRAMFS` or `VINIX_AARCH64_INITRAMFS` in Vinix's architecture-specific ISO assembly command. The worker checked that adding a native command and its relative symlink kept the signed package database and executable modes. A second layer attempting to replace the signed `usr/bin/pkg` was rejected before exposing the output root or archive. See `foundation/native-layer-image-01`. Run `python3 tests/native-layer.py` for the five layer regression checks. The worker's receipt SHA-256 is `0d79749f6461ed629c39b929099d89d05d7c9037b7e0ccd4e3c792d6fae5968f`. + To publish only the runtime closure selected by the release plan, use `tools/release-feed.py` with the matching `coverage.tsv`. It follows runtime dependency edges and rechecks every planned archive's version and SHA-256 before signing. The worker cold-installed `busybox-init` by name from this feed and audited six packages, 832 owned paths and 15 ELFs. The same test refused an incomplete X11 closure and a changed archive digest before creating either repository. See `foundation/exact-release-feed-proof-01`, receipt SHA-256 `e50d2c759f1e0045a9ef6e42522e02e6de4cfd03d9f51d6b5d6045d23d5cbdc3`. ```sh diff --git a/tests/native-layer.py b/tests/native-layer.py new file mode 100644 index 0000000..d56de47 --- /dev/null +++ b/tests/native-layer.py @@ -0,0 +1,78 @@ +"""Check that a desktop layer adds native files without replacing the vpac root.""" +import csv +import hashlib +import io +from pathlib import Path +import runpy +import tempfile +import unittest + + +install_native_layer = runpy.run_path( + Path(__file__).resolve().parents[1] / "tools/stage-image.py" +)["install_native_layer"] + + +class NativeLayer(unittest.TestCase): + def setUp(self): + work = tempfile.TemporaryDirectory() + self.addCleanup(work.cleanup) + self.root = Path(work.name) / "root" + self.source = Path(work.name) / "native" + for tree in (self.root, self.source): + (tree / "usr/bin").mkdir(parents=True) + self.package_command = self.root / "usr/bin/pkg" + self.package_command.write_bytes(b"package-owned command") + + def install(self): + receipt = io.StringIO() + install_native_layer(self.source, self.root, csv.writer(receipt, delimiter="\t")) + receipt.seek(0) + return list(csv.reader(receipt, delimiter="\t")) + + def test_preserves_package_files_and_records_added_native_files(self): + program = self.source / "usr/bin/vinix-desktop" + program.write_bytes(b"native desktop") + program.chmod(0o755) + (self.source / "usr/bin/vinix-system-information").symlink_to("vinix-desktop") + + rows = self.install() + + self.assertEqual(self.package_command.read_bytes(), b"package-owned command") + self.assertEqual((self.root / "usr/bin/vinix-desktop").stat().st_mode & 0o777, 0o755) + self.assertEqual(rows, [ + ["usr/bin/vinix-desktop", "file", "0755", hashlib.sha256(b"native desktop").hexdigest()], + ["usr/bin/vinix-system-information", "link", "", "vinix-desktop"], + ]) + + def test_refuses_to_replace_a_package_owner(self): + (self.source / "usr/bin/pkg").write_bytes(b"shadow") + + with self.assertRaisesRegex(ValueError, "shadows a staged path"): + self.install() + self.assertEqual(self.package_command.read_bytes(), b"package-owned command") + + def test_refuses_to_traverse_a_destination_symlink(self): + (self.root / "usr/share").symlink_to("bin") + (self.source / "usr/share").mkdir() + (self.source / "usr/share/new-file").touch() + + with self.assertRaisesRegex(ValueError, "directory shadows a staged file"): + self.install() + self.assertFalse((self.root / "usr/bin/new-file").exists()) + + def test_refuses_paths_that_bypass_image_metadata(self): + (self.source / "var/lib/vpac").mkdir(parents=True) + with self.assertRaisesRegex(ValueError, "reserved path"): + self.install() + self.assertFalse((self.root / "var/lib/vpac").exists()) + + def test_refuses_links_that_leave_the_image(self): + (self.source / "usr/bin/vinix-desktop").symlink_to("../../../outside") + with self.assertRaisesRegex(ValueError, "leaves the guest root"): + self.install() + self.assertFalse((self.root / "usr/bin/vinix-desktop").is_symlink()) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/stage-image.py b/tools/stage-image.py index b613e5c..c58fdc4 100644 --- a/tools/stage-image.py +++ b/tools/stage-image.py @@ -1,12 +1,21 @@ """Assemble an offline image root from a signed vpac client and its package repository.""" import argparse +import csv import hashlib +import os import shutil +import stat import subprocess import sys import tarfile import tempfile -from pathlib import Path +from pathlib import Path, PurePosixPath + + +NATIVE_RESERVED = tuple(PurePosixPath(path) for path in ( + ".vinix-image-id", "etc/alpine-release", "etc/apk", "lib/apk", "sbin/apk", + "usr/bin/apk", "usr/sbin/apk", "etc/vpac.d", "var/lib/vpac", "var/lib/vinix/repository", +)) def package_fields(data): @@ -31,6 +40,51 @@ def write_directory(root, relative): return directory +def native_link_target(relative, link): + target = PurePosixPath(link) + depth = 0 if target.is_absolute() else len(relative.parent.parts) + for name in target.parts[1:] if target.is_absolute() else target.parts: + depth += -1 if name == ".." else 1 + if depth < 0: + raise ValueError(f"native link leaves the guest root: /{relative}") + + +def install_native_layer(source, root, table): + def walk(directory, destination): + for origin in sorted(directory.iterdir()): + target = destination / origin.name + relative = target.relative_to(root) + if any(relative.is_relative_to(path) for path in NATIVE_RESERVED): + raise ValueError(f"native layer touches a reserved path: /{relative}") + mode = origin.lstat().st_mode + exists = os.path.lexists(target) + if stat.S_ISDIR(mode): + if exists and (target.is_symlink() or not target.is_dir()): + raise ValueError(f"native directory shadows a staged file: /{relative}") + if not exists: + target.mkdir() + walk(origin, target) + if not exists: + shutil.copystat(origin, target) + continue + if exists: + raise ValueError(f"native file shadows a staged path: /{relative}") + if stat.S_ISLNK(mode): + link = os.readlink(origin) + native_link_target(relative, link) + target.symlink_to(link) + table.writerow((relative, "link", "", link)) + elif stat.S_ISREG(mode): + shutil.copy2(origin, target) + with target.open("rb") as stream: + digest = hashlib.file_digest(stream, "sha256").hexdigest() + table.writerow((relative, "file", f"{stat.S_IMODE(mode):04o}", digest)) + else: + raise ValueError(f"unsupported native file type: /{relative}") + + walk(source, root) + + def snapshot(repo, dest, ring, arch, gpg_home): def verify(path): subprocess.run(["gpgv", "--homedir", gpg_home, "--keyring", ring, str(path) + ".sig", path], @@ -78,13 +132,18 @@ def snapshot(repo, dest, ring, arch, gpg_home): def stage(args): client, repo, output = args.client.resolve(), args.repo.resolve(), args.output.resolve() - if any(output.is_relative_to(path) for path in (client, repo)): - raise ValueError("put image output outside the client and repository") + native = args.native_layer.resolve(strict=True) if args.native_layer else None + sources = (client, repo, native) if native else (client, repo) + if any(output.is_relative_to(path) for path in sources): + raise ValueError("put image output outside its input directories") + if native and not native.is_dir(): + raise ValueError("expected a staging directory for the native layer") output.mkdir() audit = subprocess.check_output([args.auditor, args.arch, client], text=True) (output / "audited-root.log").write_text(audit) with tempfile.TemporaryDirectory(dir=output) as directory: - root = Path(directory) / "image" + staging = Path(directory) + root = staging / "image" subprocess.run(["cp", "-a", "--reflink=auto", client / "root", root], check=True) # This compares the installed certificate with the independently supplied pin. seed = Path(__file__).resolve().with_name("keyring.py") @@ -109,6 +168,21 @@ def stage(args): sync = write_directory(root, "var/lib/vpac/sync") for name in ("vinix.db", "vinix.db.sig"): shutil.copy(package_dir / name, sync / name) + if native: + manifest = staging / "native-layer.tsv" + with manifest.open("x", newline="") as records: + table = csv.writer(records, delimiter="\t", lineterminator="\n") + table.writerow(("path", "type", "mode", "sha256_or_link")) + install_native_layer(native, root, table) + if args.initramfs: + if os.path.lexists(root / ".vinix-image-id"): + raise ValueError("the ISO builder assigns the image identity") + image = staging / "initramfs.tar" + subprocess.run(["tar", "--format=ustar", "--sort=name", "--numeric-owner", + "--owner=0", "--group=0", "-cf", image, "-C", root, "."], check=True) + for artifact in ("native-layer.tsv", "initramfs.tar"): + if (staging / artifact).exists(): + (staging / artifact).rename(output / artifact) root.rename(output / "root") print(audit, end="") print(f"staged {len(indexed)} signed seed packages and the installed database") @@ -120,4 +194,6 @@ if __name__ == "__main__": for arg in ("client", "repo", "output", "auditor"): parser.add_argument(arg, type=Path) parser.add_argument("fingerprint", help="trusted distributor fingerprint from outside the feed") + parser.add_argument("--native-layer", type=Path, help="Vinix-built programs and assets to add to the image") + parser.add_argument("--initramfs", action="store_true", help="write a root-owned USTAR archive for the ISO builder") stage(parser.parse_args()) -- 2.56.1