| 1 | """Check packaged ELF load segments against the page size of the released Vinix kernel.""" |
| 2 | import argparse |
| 3 | import csv |
| 4 | import struct |
| 5 | import subprocess |
| 6 | import sys |
| 7 | import tarfile |
| 8 | from pathlib import Path |
| 9 | |
| 10 | |
| 11 | def check(stream, size, arch): |
| 12 | header = stream.read(64) |
| 13 | if not header.startswith(b"\x7fELF"): |
| 14 | return None |
| 15 | machine, page = (183, 16384) if arch == "aarch64" else (62, 4096) |
| 16 | if len(header) != 64 or header[4:7] != bytes((2, 1, 1)): |
| 17 | raise ValueError("invalid ELF64 little-endian header") |
| 18 | kind, target = struct.unpack_from("<HH", header, 16) |
| 19 | if target != machine: |
| 20 | raise ValueError(f"unexpected ELF machine {target}") |
| 21 | if kind == 1: |
| 22 | return None |
| 23 | if kind not in (2, 3): |
| 24 | raise ValueError(f"unexpected ELF type {kind}") |
| 25 | offset, = struct.unpack_from("<Q", header, 32) |
| 26 | stride, count = struct.unpack_from("<HH", header, 54) |
| 27 | if stride != 56 or not count or offset < 64 or offset + stride * count > size: |
| 28 | raise ValueError("invalid program header table") |
| 29 | remaining = offset - len(header) |
| 30 | while remaining: |
| 31 | chunk = stream.read(min(remaining, 65536)) |
| 32 | if not chunk: |
| 33 | raise ValueError("truncated data before the program headers") |
| 34 | remaining -= len(chunk) |
| 35 | loads = 0 |
| 36 | for index in range(count): |
| 37 | kind, _, off, addr, _, filesz, memsz, _ = struct.unpack("<II6Q", stream.read(stride)) |
| 38 | if kind != 1: |
| 39 | continue |
| 40 | if filesz > memsz or off > size or filesz > size - off: |
| 41 | raise ValueError(f"invalid PT_LOAD {index}") |
| 42 | if memsz: |
| 43 | loads += 1 |
| 44 | if off % page != addr % page: |
| 45 | return f"PT_LOAD {index}, offset 0x{off:x}, vaddr 0x{addr:x}, page {page}" |
| 46 | if not loads: |
| 47 | raise ValueError("ELF lacks a nonempty PT_LOAD") |
| 48 | return "" |
| 49 | |
| 50 | |
| 51 | def audit(args): |
| 52 | writer = csv.writer(sys.stdout, delimiter="\t", lineterminator="\n") |
| 53 | writer.writerow(["archive", "member", "result", "detail"]) |
| 54 | incompatible = 0 |
| 55 | for path in args.archives: |
| 56 | total = failed = 0 |
| 57 | with subprocess.Popen(["zstd", "-qdc", "--", path], stdout=subprocess.PIPE) as proc: |
| 58 | with tarfile.open(fileobj=proc.stdout, mode="r|") as archive: |
| 59 | for member in archive: |
| 60 | if not member.isfile(): |
| 61 | continue |
| 62 | with archive.extractfile(member) as contents: |
| 63 | try: |
| 64 | reason = check(contents, member.size, args.arch) |
| 65 | except (ValueError, struct.error) as error: |
| 66 | raise ValueError(f"{path}:{member.name}: {error}") from error |
| 67 | if reason is None: |
| 68 | continue |
| 69 | total += 1 |
| 70 | failed += bool(reason) |
| 71 | writer.writerow([path.name, member.name, "incompatible" if reason else "compatible", reason]) |
| 72 | # Drain any padding beyond the final tar member before checking zstd. |
| 73 | while proc.stdout.read(65536): |
| 74 | pass |
| 75 | if proc.wait(): |
| 76 | raise ValueError(f"could not decompress {path}") |
| 77 | writer.writerow([path.name, "", "summary", f"{failed} incompatible of {total} executables and DSOs"]) |
| 78 | incompatible += failed |
| 79 | return int(bool(incompatible) and not args.inventory) |
| 80 | |
| 81 | |
| 82 | if __name__ == "__main__": |
| 83 | parser = argparse.ArgumentParser(description=__doc__) |
| 84 | parser.add_argument("arch", choices=("aarch64", "x86_64")) |
| 85 | parser.add_argument("archives", type=Path, nargs="+") |
| 86 | parser.add_argument("--inventory", action="store_true", help="record incompatible files without failing the survey") |
| 87 | sys.exit(audit(parser.parse_args())) |
| 88 | |