| 1 | """Verify an in-place signed upgrade of the compiler and X11 permission fixes.""" |
| 2 | |
| 3 | import argparse |
| 4 | import csv |
| 5 | import json |
| 6 | from pathlib import Path |
| 7 | import stat |
| 8 | import subprocess |
| 9 | import tempfile |
| 10 | |
| 11 | |
| 12 | PACKAGES = {"binutils", "freeglut", "gcc", "vinix-pkg", "xf86-video-fbdev", "xinit", "xorg-server"} |
| 13 | |
| 14 | |
| 15 | def installed(db): |
| 16 | results = {} |
| 17 | for desc in (db / "local").glob("*/desc"): |
| 18 | fields = {} |
| 19 | for field in desc.read_text().split("\n\n"): |
| 20 | key, _, value = field.partition("\n") |
| 21 | fields[key] = value.strip() |
| 22 | name = fields["%NAME%"] |
| 23 | if name in results or fields["%VALIDATION%"] != "pgp": |
| 24 | raise ValueError(f"{desc}: expected a unique signature-validated package") |
| 25 | results[name] = fields["%VERSION%"] |
| 26 | return results |
| 27 | |
| 28 | |
| 29 | def versions(feed): |
| 30 | with (feed / "packages.tsv").open() as stream: |
| 31 | return {row["name"]: row["version"] for row in csv.DictReader(stream, delimiter="\t")} |
| 32 | |
| 33 | |
| 34 | def inaccessible(root): |
| 35 | results = [] |
| 36 | for base in (root / "usr", root / "etc/X11"): |
| 37 | for path in [base, *base.rglob("*")]: |
| 38 | mode = path.lstat().st_mode |
| 39 | if ((stat.S_ISDIR(mode) and not mode & stat.S_IXOTH) |
| 40 | or (stat.S_ISREG(mode) and not mode & stat.S_IROTH)): |
| 41 | results.append(path.relative_to(root).as_posix()) |
| 42 | return sorted(results) |
| 43 | |
| 44 | |
| 45 | def audit(args): |
| 46 | initial = installed(args.client / "db") |
| 47 | if len(initial) != 138: |
| 48 | raise ValueError("expected the original signed desktop-plus-X11 root") |
| 49 | planned = versions(args.feed) |
| 50 | if not set(initial) <= planned.keys(): |
| 51 | raise ValueError("the updated feed omits installed packages") |
| 52 | expected = {name: planned[name] for name in initial} |
| 53 | if {name for name in initial if expected[name] != initial[name]} != PACKAGES: |
| 54 | raise ValueError("expected the permission-fixed packages and the vinix-pkg migration") |
| 55 | |
| 56 | args.output.mkdir() |
| 57 | before = inaccessible(args.client / "root") |
| 58 | if not before: |
| 59 | raise ValueError("fixture does not contain the original restrictive modes") |
| 60 | (args.output / "before.txt").write_text("\n".join(before) + "\n") |
| 61 | with tempfile.TemporaryDirectory(prefix="vinix-mode-upgrade-", dir=args.scratch) as area: |
| 62 | client = Path(area) |
| 63 | for name in ("root", "db", "hooks"): |
| 64 | subprocess.run(["cp", "-a", args.client / name, client / name], check=True) |
| 65 | keyring = client / "keyring" |
| 66 | subprocess.run(["cp", "-a", args.keyring, keyring], check=True) |
| 67 | cache = client / "cache" |
| 68 | cache.mkdir() |
| 69 | config = client / "config.vile" |
| 70 | url = (args.feed / args.arch).as_uri() |
| 71 | config.write_text("\n".join(( |
| 72 | f"path-root = {json.dumps(str(client / 'root') + '/')}", |
| 73 | f"path-db = {json.dumps(str(client / 'db') + '/')}", |
| 74 | f"path-cache = [{json.dumps(str(cache) + '/')}]", |
| 75 | f"path-gpg = {json.dumps(str(keyring) + '/')}", |
| 76 | f"path-log = {json.dumps(str(client / 'vpac.log'))}", |
| 77 | f"path-hook = [{json.dumps(str(client / 'hooks') + '/')}]", |
| 78 | f"pkg-arch = {json.dumps(args.arch)}", |
| 79 | 'network-siglevel = ["package", "database"]', |
| 80 | 'network-user = ""', |
| 81 | 'network-disable-sandbox = true', |
| 82 | f'repositories = list(sync-repo("vinix", [{json.dumps(url)}]))', |
| 83 | )) + "\n") |
| 84 | transaction_log = args.output / "transaction.log" |
| 85 | with transaction_log.open("x") as log: |
| 86 | result = subprocess.run(["fakeroot", "--", "vpac", "--config", config, "u", |
| 87 | "--sync", "--noconfirm"], stdout=log, stderr=subprocess.STDOUT) |
| 88 | if (client / "vpac.log").is_file(): |
| 89 | (args.output / "vpac.log").write_bytes((client / "vpac.log").read_bytes()) |
| 90 | result.check_returncode() |
| 91 | if any(line.startswith("error:") for line in transaction_log.read_text().splitlines()): |
| 92 | raise ValueError(f"vpac reported an upgrade error in {transaction_log}") |
| 93 | if installed(client / "db") != expected: |
| 94 | raise ValueError("the installed versions do not match the signed upgrade") |
| 95 | with (args.output / "installed-root.log").open("x") as log: |
| 96 | subprocess.run([args.auditor, args.arch, client], check=True, stdout=log, stderr=subprocess.STDOUT) |
| 97 | after = inaccessible(client / "root") |
| 98 | (args.output / "after.txt").write_text("".join(path + "\n" for path in after)) |
| 99 | if after: |
| 100 | raise ValueError(f"{len(after)} public paths are still restricted after the upgrade") |
| 101 | if installed(args.client / "db") != initial or inaccessible(args.client / "root") != before: |
| 102 | raise ValueError("the original client changed during the upgrade test") |
| 103 | print(f"{args.arch}: upgraded {len(PACKAGES)} signed packages and restored {len(before)} public paths") |
| 104 | |
| 105 | |
| 106 | if __name__ == "__main__": |
| 107 | parser = argparse.ArgumentParser(description=__doc__) |
| 108 | parser.add_argument("arch", choices=("x86_64", "aarch64")) |
| 109 | for name in ("client", "feed", "keyring", "auditor", "scratch", "output"): |
| 110 | parser.add_argument(name, type=lambda path: Path(path).resolve()) |
| 111 | try: |
| 112 | audit(parser.parse_args()) |
| 113 | except (ValueError, OSError, subprocess.SubprocessError) as error: |
| 114 | parser.exit(1, f"{error}\n") |
| 115 | |