| 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}") |
| 81 | |