| 1 | """Use the installed musl loader to relocate each dynamic executable in a signed root.""" |
| 2 | import argparse |
| 3 | import os |
| 4 | from pathlib import Path |
| 5 | import shlex |
| 6 | import struct |
| 7 | import subprocess |
| 8 | |
| 9 | |
| 10 | def interpreter(path): |
| 11 | with path.open("rb") as stream: |
| 12 | header = stream.read(64) |
| 13 | if header[:4] != b"\x7fELF": |
| 14 | return None |
| 15 | if len(header) != 64 or header[4:6] != bytes((2, 1)): |
| 16 | raise ValueError(f"unexpected ELF header in {path}") |
| 17 | if struct.unpack_from("<H", header, 16)[0] not in (2, 3): |
| 18 | return None |
| 19 | offset, = struct.unpack_from("<Q", header, 32) |
| 20 | stride, count = struct.unpack_from("<HH", header, 54) |
| 21 | if stride != 56 or offset + count * stride > path.stat().st_size: |
| 22 | raise ValueError(f"invalid ELF program headers in {path}") |
| 23 | stream.seek(offset) |
| 24 | for _ in range(count): |
| 25 | kind, _, position, _, _, length, _, _ = struct.unpack("<II6Q", stream.read(stride)) |
| 26 | if kind == 3: |
| 27 | if length < 2 or length > 4096: |
| 28 | raise ValueError(f"invalid ELF interpreter in {path}") |
| 29 | stream.seek(position) |
| 30 | raw = stream.read(length) |
| 31 | if len(raw) != length or not raw.endswith(b"\0"): |
| 32 | raise ValueError(f"truncated ELF interpreter in {path}") |
| 33 | return os.fsdecode(raw[:-1]) |
| 34 | return None |
| 35 | |
| 36 | |
| 37 | def commands(arch, sdk): |
| 38 | root = sdk / "root" |
| 39 | expected = f"/lib/ld-musl-{arch}.so.1" |
| 40 | for directory, _, files in os.walk(root): |
| 41 | for name in sorted(files): |
| 42 | path = Path(directory) / name |
| 43 | if path.is_symlink() or not path.is_file(): |
| 44 | continue |
| 45 | loader = interpreter(path) |
| 46 | if loader is not None: |
| 47 | if loader != expected: |
| 48 | raise ValueError(f"unexpected interpreter {loader} in {path}") |
| 49 | yield [loader, "--list", "/" + path.relative_to(root).as_posix()] |
| 50 | |
| 51 | |
| 52 | def main(args): |
| 53 | subprocess.run([args.auditor, args.arch, args.client], check=True) |
| 54 | entries = sorted(commands(args.arch, args.client)) |
| 55 | if not entries: |
| 56 | raise ValueError("root has no dynamically linked executables") |
| 57 | if args.fixture: |
| 58 | with args.fixture.open("x") as output: |
| 59 | output.write("#!/bin/sh\nset -eu\nexport LC_ALL=C\nunset LD_LIBRARY_PATH LD_PRELOAD\n" |
| 60 | "trap '/sbin/poweroff -f' EXIT\n") |
| 61 | for cmd in entries: |
| 62 | output.write(shlex.join(cmd) + " >/dev/null\n") |
| 63 | output.write(f"echo 'Relocated {len(entries)} Vinix executables'\n" |
| 64 | "echo 'VPAC FIXTURE GUEST PASS'\n") |
| 65 | if args.proot: |
| 66 | for cmd in entries: |
| 67 | result = subprocess.run([args.proot, "-r", args.client / "root", "-w", "/", *cmd], |
| 68 | text=True, capture_output=True) |
| 69 | if result.returncode: |
| 70 | raise ValueError(f"{cmd[-1]}: musl exited {result.returncode}\n{result.stderr}") |
| 71 | print(cmd[-1]) |
| 72 | print(f"Relocated {len(entries)} {args.arch} executables in the signed root") |
| 73 | |
| 74 | |
| 75 | if __name__ == "__main__": |
| 76 | parser = argparse.ArgumentParser(description=__doc__) |
| 77 | parser.add_argument("arch", choices=("x86_64", "aarch64")) |
| 78 | for arg in ("client", "auditor"): |
| 79 | parser.add_argument(arg, type=lambda p: Path(p).resolve()) |
| 80 | parser.add_argument("--fixture", type=Path, help="write a guest script for the Vinix boot harness") |
| 81 | parser.add_argument("--proot", type=Path, help="also check a host-architecture root in unprivileged PRoot") |
| 82 | options = parser.parse_args() |
| 83 | if not (options.fixture or options.proot): |
| 84 | parser.error("supply --fixture or --proot") |
| 85 | if options.proot and options.arch != os.uname().machine: |
| 86 | parser.error("use the Vinix guest fixture for a foreign architecture") |
| 87 | main(options) |
| 88 | |