"""Compare the release's shared-library interface to its package-owned replacement.""" import argparse import csv import hashlib from pathlib import Path import shutil import subprocess import tarfile import tempfile def interface(path, arch): report = subprocess.check_output(["readelf", "-W", "-h", "-d", "--dyn-syms", path], text=True) machine = "AArch64" if arch == "aarch64" else "Advanced Micro Devices X86-64" fields = [line.strip() for line in report.splitlines()] if not any(line.startswith("Machine:") and line.split(":", 1)[1].strip() == machine for line in fields): raise ValueError(f"unexpected ELF target in {path}") soname, = [line.split("[", 1)[1].split("]", 1)[0] for line in fields if "(SONAME)" in line] exports = set() for line in fields: row = line.split() if (len(row) >= 8 and row[0].endswith(":") and row[0][:-1].isdigit() and row[4] in ("GLOBAL", "WEAK", "UNIQUE") and row[5] in ("DEFAULT", "PROTECTED") and row[6] not in ("UND", "ABS")): kind = "FUNC" if row[3] in ("FUNC", "IFUNC") else row[3] exports.add((row[7], kind, row[2] if kind in ("OBJECT", "TLS") else "")) if not exports: raise ValueError(f"no exported dynamic symbols in {path}") return soname, exports def run(args): with args.initramfs.open("rb") as source: if hashlib.file_digest(source, "sha256").hexdigest() != args.sha256: raise ValueError("release initramfs checksum mismatch") subprocess.run([args.auditor, args.arch, args.client], check=True) root = (args.client / "root").resolve() expected = {path: (package, soname) for package, path, soname in args.library} if len(expected) != len(args.library): raise ValueError("each release path must occur only once") results = {} with tempfile.TemporaryDirectory() as temp, tarfile.open(args.initramfs, "r|gz") as contents: for entry in contents: path = entry.name.removeprefix("./") if path not in expected: continue package, name = expected[path] if not entry.isfile() or path in results or Path(name).name != name: raise ValueError(f"expected one regular release library at {path}") reference = Path(temp) / "reference" with contents.extractfile(entry) as source, reference.open("wb") as copy: shutil.copyfileobj(source, copy) old_name, old = interface(reference, args.arch) alias = Path(path).with_name(name) current = (root / alias).resolve() current_path = current.relative_to(root) records = [d for d in (args.client / "db/local").glob("*/desc") if f"\n%NAME%\n{package}\n" in "\n" + d.read_text()] record, = records owned = set(record.with_name("files").read_text().splitlines()) if alias.as_posix() not in owned or current_path.as_posix() not in owned: raise ValueError(f"{package} must own both {alias} and {current_path}") new_name, new = interface(current, args.arch) if old_name != name or new_name != name: raise ValueError(f"{path}: expected {name}, found {old_name} and {new_name}") missing = sorted(old - new) results[path] = (package, name, current_path, len(old), len(new), len(missing), ",".join(symbol for symbol, _, _ in missing)) if set(results) != set(expected): raise ValueError(f"missing release libraries: {sorted(set(expected) - set(results))}") with args.output.open("x") as stream: table = csv.writer(stream, delimiter="\t", lineterminator="\n") table.writerow(("release_path", "package", "soname", "installed_path", "old_exports", "new_exports", "missing_count", "missing_symbols")) for path, result in sorted(results.items()): table.writerow((path, *result)) return int(any(row[-2] for row in results.values())) if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("arch", choices=("aarch64", "x86_64")) for arg in ("client", "auditor", "initramfs", "output"): parser.add_argument(arg, type=Path) parser.add_argument("sha256", help="published initramfs digest") parser.add_argument("--library", nargs=3, action="append", metavar=("PACKAGE", "RELEASE_PATH", "SONAME"), required=True) raise SystemExit(run(parser.parse_args()))