Verify the prepared package root in release ISOs
@@ -160,6 +160,8 @@ python3 tools/stage-image.py "$arch" "$client" "$repo" "$output" "$work/audit" \
160160
161161Use `$output/initramfs.tar` as `VINIX_AMD64_INITRAMFS` or `VINIX_AARCH64_INITRAMFS` in Vinix's architecture-specific ISO assembly command. Run `python3 tests/native-layer.py` for the layer regression checks.
162162
163+Run `python3 tools/check-iso-initramfs.py "$output/initramfs.tar" "$iso"` against each assembled ISO before accepting its release evidence. The checker compares the embedded tar payload byte for byte with the audited input and verifies the image identity that the ISO builder writes into the gzip header and appends to the tar. Run `python3 tests/iso-initramfs.py` to exercise rejection of altered payloads, wrong identities and added tar entries.
164+
163165The x86_64 ISO proof in `foundation/native-layer-image-01` cold-installed 45 signed packages and audited 7,761 owned files and links and 162 target ELFs. The native layer used five checksum-pinned programs from the November 28 Vinix release. Assembly preserved the signed database, program modes and relative app link. An attempted replacement of the package-owned `usr/bin/pkg` failed before exposing an output root or archive. The worker compared the ISO's embedded initramfs with the staged archive, then booted the ISO in QEMU. The fixture reached registration at 1024×768 and sampled at least 24 framebuffer colors. The receipt SHA-256 is `6cbcbaa46d5642228b8ec8512270916de131968e47dc4776f2e6843d00dbd143`.
164166
165167The cold and staged roots are archived in `foundation/archived-native-layer-proof-roots-01`, receipt SHA-256 `d398eb5cdf41ff6f666a976d4c9e3f23570035a5b8a5e0b6f7d8fb395434e549`. Use `recycled-tar/restore.sh` in the proof directory to recover the original USTAR archive from the checked gzip copy. The recovery receipt SHA-256 is `3ad2c9c703ac8d0494cbd8486808967575ac4f178924e60a76b85f48f5c9fd58`.
@@ -0,0 +1,68 @@
1+import gzip
2+import hashlib
3+import io
4+from pathlib import Path
5+import runpy
6+import tarfile
7+import tempfile
8+import unittest
9+
10+
11+CHECKER = runpy.run_path(Path(__file__).resolve().parents[1] / "tools/check-iso-initramfs.py")
12+
13+
14+class EmbeddedInitramfs(unittest.TestCase):
15+ def setUp(self):
16+ scratch = tempfile.TemporaryDirectory()
17+ self.addCleanup(scratch.cleanup)
18+ self.prepared = Path(scratch.name) / "initramfs.tar"
19+ self.embedded = Path(scratch.name) / "initramfs.tar.gz"
20+ self.package = b"installed package payload\n"
21+ self.prepared.write_bytes(self.archive(self.package))
22+ self.checksum = hashlib.sha256(self.prepared.read_bytes()).hexdigest()
23+ self.identity = self.checksum[:16].encode()
24+
25+ @staticmethod
26+ def archive(package, identity=None, extra=False):
27+ buffer = io.BytesIO()
28+ with tarfile.open(fileobj=buffer, mode="w", format=tarfile.USTAR_FORMAT) as archive:
29+ files = [("./usr/bin/pkg", package)]
30+ if identity is not None:
31+ files.append(("./.vinix-image-id", identity + b"\n"))
32+ if extra:
33+ files.append(("./extra", b"extra file\n"))
34+ for name, data in files:
35+ entry = tarfile.TarInfo(name)
36+ entry.size = len(data)
37+ archive.addfile(entry, io.BytesIO(data))
38+ return buffer.getvalue()
39+
40+ def write_embedded(self, *, changed=False, header=None, identity=None, extra=False):
41+ root = self.archive(b"changed package payload\n" if changed else self.package,
42+ self.identity if identity is None else identity, extra)
43+ data = bytearray(gzip.compress(root, mtime=0))
44+ data[3] = 0x10
45+ data[10:10] = b"vinix-image-id=" + (self.identity if header is None else header) + b"\0"
46+ self.embedded.write_bytes(data)
47+
48+ def test_exact_package_bytes_and_identity(self):
49+ self.write_embedded()
50+ self.assertEqual(CHECKER["check_payload"](self.prepared, self.embedded), self.checksum)
51+
52+ def test_rejects_changed_payload_with_original_identity(self):
53+ self.write_embedded(changed=True)
54+ with self.assertRaisesRegex(ValueError, "differs from the prepared tar"):
55+ CHECKER["check_payload"](self.prepared, self.embedded)
56+
57+ def test_rejects_bad_identity_and_added_tar_entry(self):
58+ for kwargs, error in (({"header": b"0" * 16}, "gzip header"),
59+ ({"identity": b"0" * 16}, "ISO tar"),
60+ ({"extra": True}, "missing tar end marker")):
61+ with self.subTest(kwargs=kwargs):
62+ self.write_embedded(**kwargs)
63+ with self.assertRaisesRegex(ValueError, error):
64+ CHECKER["check_payload"](self.prepared, self.embedded)
65+
66+
67+if __name__ == "__main__":
68+ unittest.main()
@@ -0,0 +1,80 @@
1+"""Verify that a release ISO contains the exact prepared Vinix initramfs."""
2+
3+import argparse
4+import gzip
5+import hashlib
6+from pathlib import Path
7+import subprocess
8+import tarfile
9+import tempfile
10+
11+
12+BLOCK_SIZE = tarfile.BLOCKSIZE
13+CHUNK_SIZE = 1024**2
14+IDENTITY = "./.vinix-image-id"
15+
16+
17+def require_end_of_archive(stream):
18+ if stream.read(2 * BLOCK_SIZE) != bytes(2 * BLOCK_SIZE):
19+ raise ValueError("missing tar end marker")
20+ while padding := stream.read(CHUNK_SIZE):
21+ if padding.strip(b"\0"):
22+ raise ValueError("unexpected data after the tar end marker")
23+
24+
25+def check_payload(prepared, embedded):
26+ with prepared.open("rb") as expected, embedded.open("rb") as compressed:
27+ checksum = hashlib.file_digest(expected, "sha256").hexdigest()
28+ identity = checksum[:16].encode()
29+ expected.seek(0)
30+ with tarfile.open(fileobj=expected, mode="r:") as archive:
31+ for entry in archive:
32+ if entry.name in (IDENTITY, IDENTITY[2:]):
33+ raise ValueError("prepared tar already contains an image identity")
34+ payload_end = archive.offset
35+ expected.seek(payload_end)
36+ require_end_of_archive(expected)
37+ expected.seek(0)
38+
39+ header = compressed.read(10)
40+ comment = b"vinix-image-id=" + identity + b"\0"
41+ if header[:4] != b"\x1f\x8b\x08\x10" or compressed.read(len(comment)) != comment:
42+ raise ValueError("ISO gzip header has the wrong image identity")
43+ compressed.seek(0)
44+ with gzip.GzipFile(fileobj=compressed) as actual:
45+ remaining = payload_end
46+ while remaining:
47+ chunk = expected.read(min(CHUNK_SIZE, remaining))
48+ if actual.read(len(chunk)) != chunk:
49+ raise ValueError(f"ISO initramfs differs from the prepared tar near byte {expected.tell() - len(chunk)}")
50+ remaining -= len(chunk)
51+ entry = tarfile.TarInfo.frombuf(actual.read(BLOCK_SIZE), "utf-8", "strict")
52+ if entry.name != IDENTITY or not entry.isfile() or entry.size != len(identity) + 1:
53+ raise ValueError("expected one appended image identity entry")
54+ if actual.read(BLOCK_SIZE) != (identity + b"\n").ljust(BLOCK_SIZE, b"\0"):
55+ raise ValueError("ISO tar has the wrong image identity")
56+ require_end_of_archive(actual)
57+ return checksum
58+
59+
60+def verify_iso(prepared, iso):
61+ with tempfile.TemporaryDirectory(prefix="vinix-iso-initramfs-") as temporary:
62+ embedded = Path(temporary) / "initramfs.tar.gz"
63+ result = subprocess.run(["xorriso", "-osirrox", "on", "-indev", iso.resolve(),
64+ "-extract", "/boot/initramfs.tar.gz", embedded],
65+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
66+ if result.returncode:
67+ raise ValueError("could not extract the ISO initramfs\n" + result.stdout)
68+ return check_payload(prepared, embedded)
69+
70+
71+if __name__ == "__main__":
72+ parser = argparse.ArgumentParser(description=__doc__)
73+ parser.add_argument("prepared", type=Path, help="audited uncompressed initramfs.tar")
74+ parser.add_argument("iso", type=Path, help="assembled Vinix release ISO")
75+ args = parser.parse_args()
76+ try:
77+ checksum = verify_iso(args.prepared, args.iso)
78+ except (OSError, EOFError, ValueError, tarfile.TarError) as error:
79+ parser.exit(1, f"{args.iso}: {error}\n")
80+ print(f"{checksum} prepared initramfs verified in {args.iso}")