"""Verify that a release ISO contains the exact prepared Vinix initramfs.""" import argparse import gzip import hashlib from pathlib import Path import subprocess import tarfile import tempfile BLOCK_SIZE = tarfile.BLOCKSIZE CHUNK_SIZE = 1024**2 IDENTITY = "./.vinix-image-id" class PrefixedReader: def __init__(self, prefix, stream): self.prefix = prefix self.stream = stream def read(self, size): prefix, self.prefix = self.prefix[:size], self.prefix[size:] return prefix + self.stream.read(size - len(prefix)) def require_end_of_archive(stream): if stream.read(2 * BLOCK_SIZE) != bytes(2 * BLOCK_SIZE): raise ValueError("missing tar end marker") while padding := stream.read(CHUNK_SIZE): if padding.strip(b"\0"): raise ValueError("unexpected data after the tar end marker") def check_stream(prepared, compressed): with prepared.open("rb") as expected: checksum = hashlib.file_digest(expected, "sha256").hexdigest() identity = checksum[:16].encode() expected.seek(0) with tarfile.open(fileobj=expected, mode="r:") as archive: for entry in archive: if entry.name in (IDENTITY, IDENTITY[2:]): raise ValueError("prepared tar already contains an image identity") payload_end = archive.offset expected.seek(payload_end) require_end_of_archive(expected) expected.seek(0) comment = b"vinix-image-id=" + identity + b"\0" header = compressed.read(10 + len(comment)) if header[:4] != b"\x1f\x8b\x08\x10" or header[10:] != comment: raise ValueError("ISO gzip header has the wrong image identity") with gzip.GzipFile(fileobj=PrefixedReader(header, compressed)) as actual: remaining = payload_end while remaining: chunk = expected.read(min(CHUNK_SIZE, remaining)) if actual.read(len(chunk)) != chunk: raise ValueError(f"ISO initramfs differs from the prepared tar near byte {expected.tell() - len(chunk)}") remaining -= len(chunk) entry = tarfile.TarInfo.frombuf(actual.read(BLOCK_SIZE), "utf-8", "strict") if entry.name != IDENTITY or not entry.isfile() or entry.size != len(identity) + 1: raise ValueError("expected one appended image identity entry") if actual.read(BLOCK_SIZE) != (identity + b"\n").ljust(BLOCK_SIZE, b"\0"): raise ValueError("ISO tar has the wrong image identity") require_end_of_archive(actual) return checksum def check_payload(prepared, embedded): with embedded.open("rb") as compressed: return check_stream(prepared, compressed) def verify_iso_stream(prepared, iso): with subprocess.Popen(["bsdtar", "-xOf", iso.resolve(), "boot/initramfs.tar.gz"], stdout=subprocess.PIPE) as extraction: with extraction.stdout as compressed: checksum = check_stream(prepared, compressed) if extraction.wait(): raise ValueError("could not read the ISO initramfs") return checksum def verify_iso(prepared, iso): with tempfile.TemporaryDirectory(prefix="vinix-iso-initramfs-") as temporary: embedded = Path(temporary) / "initramfs.tar.gz" result = subprocess.run(["xorriso", "-osirrox", "on", "-indev", iso.resolve(), "-extract", "/boot/initramfs.tar.gz", embedded], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) if result.returncode: raise ValueError("could not extract the ISO initramfs\n" + result.stdout) return check_payload(prepared, embedded) if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("prepared", type=Path, help="audited uncompressed initramfs.tar") parser.add_argument("iso", type=Path, help="assembled Vinix release ISO") parser.add_argument("--stream", action="store_true", help="read the ISO member with bsdtar without a temporary file") args = parser.parse_args() try: verify = verify_iso_stream if args.stream else verify_iso checksum = verify(args.prepared, args.iso) except (OSError, EOFError, ValueError, tarfile.TarError) as error: parser.exit(1, f"{args.iso}: {error}\n") print(f"{checksum} prepared initramfs verified in {args.iso}")