| 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 | class PrefixedReader: |
| 18 | def __init__(self, prefix, stream): |
| 19 | self.prefix = prefix |
| 20 | self.stream = stream |
| 21 | |
| 22 | def read(self, size): |
| 23 | prefix, self.prefix = self.prefix[:size], self.prefix[size:] |
| 24 | return prefix + self.stream.read(size - len(prefix)) |
| 25 | |
| 26 | |
| 27 | def require_end_of_archive(stream): |
| 28 | if stream.read(2 * BLOCK_SIZE) != bytes(2 * BLOCK_SIZE): |
| 29 | raise ValueError("missing tar end marker") |
| 30 | while padding := stream.read(CHUNK_SIZE): |
| 31 | if padding.strip(b"\0"): |
| 32 | raise ValueError("unexpected data after the tar end marker") |
| 33 | |
| 34 | |
| 35 | def check_stream(prepared, compressed): |
| 36 | with prepared.open("rb") as expected: |
| 37 | checksum = hashlib.file_digest(expected, "sha256").hexdigest() |
| 38 | identity = checksum[:16].encode() |
| 39 | expected.seek(0) |
| 40 | with tarfile.open(fileobj=expected, mode="r:") as archive: |
| 41 | for entry in archive: |
| 42 | if entry.name in (IDENTITY, IDENTITY[2:]): |
| 43 | raise ValueError("prepared tar already contains an image identity") |
| 44 | payload_end = archive.offset |
| 45 | expected.seek(payload_end) |
| 46 | require_end_of_archive(expected) |
| 47 | expected.seek(0) |
| 48 | |
| 49 | comment = b"vinix-image-id=" + identity + b"\0" |
| 50 | header = compressed.read(10 + len(comment)) |
| 51 | if header[:4] != b"\x1f\x8b\x08\x10" or header[10:] != comment: |
| 52 | raise ValueError("ISO gzip header has the wrong image identity") |
| 53 | with gzip.GzipFile(fileobj=PrefixedReader(header, compressed)) as actual: |
| 54 | remaining = payload_end |
| 55 | while remaining: |
| 56 | chunk = expected.read(min(CHUNK_SIZE, remaining)) |
| 57 | if actual.read(len(chunk)) != chunk: |
| 58 | raise ValueError(f"ISO initramfs differs from the prepared tar near byte {expected.tell() - len(chunk)}") |
| 59 | remaining -= len(chunk) |
| 60 | entry = tarfile.TarInfo.frombuf(actual.read(BLOCK_SIZE), "utf-8", "strict") |
| 61 | if entry.name != IDENTITY or not entry.isfile() or entry.size != len(identity) + 1: |
| 62 | raise ValueError("expected one appended image identity entry") |
| 63 | if actual.read(BLOCK_SIZE) != (identity + b"\n").ljust(BLOCK_SIZE, b"\0"): |
| 64 | raise ValueError("ISO tar has the wrong image identity") |
| 65 | require_end_of_archive(actual) |
| 66 | return checksum |
| 67 | |
| 68 | |
| 69 | def check_payload(prepared, embedded): |
| 70 | with embedded.open("rb") as compressed: |
| 71 | return check_stream(prepared, compressed) |
| 72 | |
| 73 | |
| 74 | def verify_iso_stream(prepared, iso): |
| 75 | with subprocess.Popen(["bsdtar", "-xOf", iso.resolve(), "boot/initramfs.tar.gz"], |
| 76 | stdout=subprocess.PIPE) as extraction: |
| 77 | with extraction.stdout as compressed: |
| 78 | checksum = check_stream(prepared, compressed) |
| 79 | if extraction.wait(): |
| 80 | raise ValueError("could not read the ISO initramfs") |
| 81 | return checksum |
| 82 | |
| 83 | |
| 84 | def verify_iso(prepared, iso): |
| 85 | with tempfile.TemporaryDirectory(prefix="vinix-iso-initramfs-") as temporary: |
| 86 | embedded = Path(temporary) / "initramfs.tar.gz" |
| 87 | result = subprocess.run(["xorriso", "-osirrox", "on", "-indev", iso.resolve(), |
| 88 | "-extract", "/boot/initramfs.tar.gz", embedded], |
| 89 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 90 | if result.returncode: |
| 91 | raise ValueError("could not extract the ISO initramfs\n" + result.stdout) |
| 92 | return check_payload(prepared, embedded) |
| 93 | |
| 94 | |
| 95 | if __name__ == "__main__": |
| 96 | parser = argparse.ArgumentParser(description=__doc__) |
| 97 | parser.add_argument("prepared", type=Path, help="audited uncompressed initramfs.tar") |
| 98 | parser.add_argument("iso", type=Path, help="assembled Vinix release ISO") |
| 99 | parser.add_argument("--stream", action="store_true", help="read the ISO member with bsdtar without a temporary file") |
| 100 | args = parser.parse_args() |
| 101 | try: |
| 102 | verify = verify_iso_stream if args.stream else verify_iso |
| 103 | checksum = verify(args.prepared, args.iso) |
| 104 | except (OSError, EOFError, ValueError, tarfile.TarError) as error: |
| 105 | parser.exit(1, f"{args.iso}: {error}\n") |
| 106 | print(f"{checksum} prepared initramfs verified in {args.iso}") |
| 107 | |