"""Verify an in-place signed upgrade of the compiler and X11 permission fixes.""" import argparse import csv import json from pathlib import Path import stat import subprocess import tempfile PACKAGES = {"binutils", "freeglut", "gcc", "vinix-pkg", "xf86-video-fbdev", "xinit", "xorg-server"} def installed(db): results = {} for desc in (db / "local").glob("*/desc"): fields = {} for field in desc.read_text().split("\n\n"): key, _, value = field.partition("\n") fields[key] = value.strip() name = fields["%NAME%"] if name in results or fields["%VALIDATION%"] != "pgp": raise ValueError(f"{desc}: expected a unique signature-validated package") results[name] = fields["%VERSION%"] return results def versions(feed): with (feed / "packages.tsv").open() as stream: return {row["name"]: row["version"] for row in csv.DictReader(stream, delimiter="\t")} def inaccessible(root): results = [] for base in (root / "usr", root / "etc/X11"): for path in [base, *base.rglob("*")]: mode = path.lstat().st_mode if ((stat.S_ISDIR(mode) and not mode & stat.S_IXOTH) or (stat.S_ISREG(mode) and not mode & stat.S_IROTH)): results.append(path.relative_to(root).as_posix()) return sorted(results) def audit(args): initial = installed(args.client / "db") if len(initial) != 138: raise ValueError("expected the original signed desktop-plus-X11 root") planned = versions(args.feed) if not set(initial) <= planned.keys(): raise ValueError("the updated feed omits installed packages") expected = {name: planned[name] for name in initial} if {name for name in initial if expected[name] != initial[name]} != PACKAGES: raise ValueError("expected the permission-fixed packages and the vinix-pkg migration") args.output.mkdir() before = inaccessible(args.client / "root") if not before: raise ValueError("fixture does not contain the original restrictive modes") (args.output / "before.txt").write_text("\n".join(before) + "\n") with tempfile.TemporaryDirectory(prefix="vinix-mode-upgrade-", dir=args.scratch) as area: client = Path(area) for name in ("root", "db", "hooks"): subprocess.run(["cp", "-a", args.client / name, client / name], check=True) keyring = client / "keyring" subprocess.run(["cp", "-a", args.keyring, keyring], check=True) cache = client / "cache" cache.mkdir() config = client / "config.vile" url = (args.feed / args.arch).as_uri() config.write_text("\n".join(( f"path-root = {json.dumps(str(client / 'root') + '/')}", f"path-db = {json.dumps(str(client / 'db') + '/')}", f"path-cache = [{json.dumps(str(cache) + '/')}]", f"path-gpg = {json.dumps(str(keyring) + '/')}", f"path-log = {json.dumps(str(client / 'vpac.log'))}", f"path-hook = [{json.dumps(str(client / 'hooks') + '/')}]", f"pkg-arch = {json.dumps(args.arch)}", 'network-siglevel = ["package", "database"]', 'network-user = ""', 'network-disable-sandbox = true', f'repositories = list(sync-repo("vinix", [{json.dumps(url)}]))', )) + "\n") transaction_log = args.output / "transaction.log" with transaction_log.open("x") as log: result = subprocess.run(["fakeroot", "--", "vpac", "--config", config, "u", "--sync", "--noconfirm"], stdout=log, stderr=subprocess.STDOUT) if (client / "vpac.log").is_file(): (args.output / "vpac.log").write_bytes((client / "vpac.log").read_bytes()) result.check_returncode() if any(line.startswith("error:") for line in transaction_log.read_text().splitlines()): raise ValueError(f"vpac reported an upgrade error in {transaction_log}") if installed(client / "db") != expected: raise ValueError("the installed versions do not match the signed upgrade") with (args.output / "installed-root.log").open("x") as log: subprocess.run([args.auditor, args.arch, client], check=True, stdout=log, stderr=subprocess.STDOUT) after = inaccessible(client / "root") (args.output / "after.txt").write_text("".join(path + "\n" for path in after)) if after: raise ValueError(f"{len(after)} public paths are still restricted after the upgrade") if installed(args.client / "db") != initial or inaccessible(args.client / "root") != before: raise ValueError("the original client changed during the upgrade test") print(f"{args.arch}: upgraded {len(PACKAGES)} signed packages and restored {len(before)} public paths") if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("arch", choices=("x86_64", "aarch64")) for name in ("client", "feed", "keyring", "auditor", "scratch", "output"): parser.add_argument(name, type=lambda path: Path(path).resolve()) try: audit(parser.parse_args()) except (ValueError, OSError, subprocess.SubprocessError) as error: parser.exit(1, f"{error}\n")