From e301ebe826767c0a97c8d99095f38502558657ed Mon Sep 17 00:00:00 2001 From: diviocity Date: Mon, 30 Nov 2026 04:14:49 +0100 Subject: [PATCH] Measure exported interfaces and dynamic imports across the release --- PROFILES.md | 4 ++ tests/library-upgrades.py | 91 ++++++++++++++++++++++++++++++--------- tests/release-imports.py | 59 +++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 20 deletions(-) create mode 100644 tests/release-imports.py diff --git a/PROFILES.md b/PROFILES.md index 5be4cc4..e54a97f 100644 --- a/PROFILES.md +++ b/PROFILES.md @@ -111,3 +111,7 @@ The arm64 desktop init uses the Vinix V generator and freestanding syscall imple `tests/library-upgrades.py` reads the actual shared libraries in the checksum-pinned release initramfs and the replacement files in an audited root. It checks the ELF target, SONAME, provider ownership and dynamic exports, including symbol versions and exported data sizes. PCRE2's two x86_64 libraries retained all 77 and 6 compared exports, and zlib retained all 95. Their new implementation filenames are in `foundation/release-library-interfaces-01/interfaces.tsv`, receipt SHA-256 `11ddb4daf9f4b2d231c512b3b9e16f92dd2ccdb1192fff0130a6be5037fa971b`. OpenSSL 3.5.9-3 retained all 580 libssl exports. Its libcrypto lacked the three published NIST prime-field method symbols for 224, 256 and 521 bits. See `foundation/release-ssl-interfaces-01/known-differences.sha256`, SHA-256 `679aab5f1a2d7d4beb4853872ba45b30a4b875c27fb70b5ca6ffa708340105ce`. OpenSSL 3.5.9-4 on x86_64 enables those three methods. Its recipe creates and frees an EC group with each method. The fresh signed installation supplies all 5,426 published libcrypto exports and all 580 libssl exports. See `foundation/openssl-ec-04/interfaces.tsv` and its receipt manifest, SHA-256 `269a3326b99eacb2691fd370bf17f828750b28d5feaff4be6c48217c4a7134b3`. + +To survey the whole release initramfs, pass `--system-providers providers.tsv` to `tests/library-upgrades.py` in place of individual library arguments. It examines every regular shared-library ELF in `/lib` and `/usr/lib`, resolves its SONAME to an installed owner, and records absent providers and differing exports. The 129-package x86_64 root fully covered the exports for 51 of 69 regular library paths. Twelve had no installed provider and six had export differences, including the earlier OpenSSL build. Multiple paths in the release contain the same library. The nine-package arm64 bootstrap root covered its loader, had 68 missing providers and contained no replacement for the release's native OBS module without a SONAME. See `foundation/system-library-interfaces-01`, receipt SHA-256 `cdf961b747c2abcf7c90f0a5d011c783a16ad97e8aedb091f317c76628870381`. + +`tests/release-imports.py ARCHIVE SHA256 REPORT SYMBOL...` searches undefined dynamic symbols in the original binaries. Across 240 amd64 image files, 114 amd64 X11 files, 221 arm64 image files and 120 arm64 X11 files, no binary imported `_dlstart` or Zstd's deprecated experimental `ZSTD_copyCCtx` and `ZSTD_copyDCtx`, removed upstream in 1.6.0. The same scan found no direct imports of the five differing GMP helpers or the nine MPFR cache and default-state symbols. It found `ZSTD_decompress` in all four layers and `mpfr_init2` in both images. The scan measures link-time imports. MPFR's per-thread defaults need a runtime test independently. See `foundation/release-dynamic-imports-01`, receipt SHA-256 `2e5ff31cdbc7c255a15ba8b8da25749c1633e102245f89a95fe8dcdcdd9072a5`. diff --git a/tests/library-upgrades.py b/tests/library-upgrades.py index 1b22e79..c0912bc 100644 --- a/tests/library-upgrades.py +++ b/tests/library-upgrades.py @@ -3,6 +3,7 @@ import argparse import csv import hashlib from pathlib import Path +import re import shutil import subprocess import tarfile @@ -15,7 +16,10 @@ def interface(path, arch): 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] + names = [line.split("[", 1)[1].split("]", 1)[0] for line in fields if "(SONAME)" in line] + if len(names) > 1: + raise ValueError(f"multiple SONAME entries in {path}") + soname = names[0] if names else "" exports = set() for line in fields: row = line.split() @@ -29,52 +33,97 @@ def interface(path, arch): return soname, exports +def package_owners(client): + owners = {} + for desc in (client / "db/local").glob("*/desc"): + name, = [block.splitlines()[1] for block in desc.read_text().split("\n\n") if block.startswith("%NAME%\n")] + records = desc.with_name("files").read_text().split("\n\n") + for path in next((b.splitlines()[1:] for b in records if b.startswith("%FILES%\n")), []): + if not path.endswith("/"): + if path in owners: + raise ValueError(f"duplicate owner for {path}") + owners[path] = name + return owners + + 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): + owners = package_owners(args.client) + expected = {path: (package, soname) for package, path, soname in args.library or []} + if len(expected) != len(args.library or []): raise ValueError("each release path must occur only once") results = {} + coverage = {} + installed_interfaces = {} 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: + if args.system_providers: + original = Path(path) + if (not entry.isfile() or original.parent.as_posix() not in ("lib", "usr/lib") or + not re.search(r"\.so($|\.)", original.name)): + continue + elif path not in expected: continue - package, name = expected[path] - if not entry.isfile() or path in results or Path(name).name != name: + if not entry.isfile() or path in coverage: 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) + with contents.extractfile(entry) as source: + magic = source.read(4) + if args.system_providers and magic != b"\x7fELF": + continue + with reference.open("wb") as copy: + copy.write(magic) + shutil.copyfileobj(source, copy) old_name, old = interface(reference, args.arch) + if args.system_providers and not old_name: + coverage[path] = ("", "", "no SONAME") + continue + package, name = expected.get(path, ("", old_name)) + if not name or Path(name).name != name or old_name != name: + raise ValueError(f"{path}: expected {name}, found {old_name}") alias = Path(path).with_name(name) + alias_owner = owners.get(alias.as_posix()) + if args.system_providers: + if not alias_owner: + coverage[path] = (name, "", "provider absent") + continue + package = alias_owner 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: + if package != alias_owner or package != owners.get(current_path.as_posix()): 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}") + if current not in installed_interfaces: + installed_interfaces[current] = interface(current, args.arch) + new_name, new = installed_interfaces[current] + if new_name != name: + raise ValueError(f"{current_path}: expected {name}, found {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): + coverage[path] = (name, package, "missing exports" if missing else "compared") + if expected and set(results) != set(expected): raise ValueError(f"missing release libraries: {sorted(set(expected) - set(results))}") + if args.system_providers: + if not coverage: + raise ValueError("no release system libraries found") + with args.system_providers.open("x") as stream: + table = csv.writer(stream, delimiter="\t", lineterminator="\n") + table.writerow(("release_path", "soname", "installed_owner", "status")) + table.writerows((path, *record) for path, record in sorted(coverage.items())) 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 any(row[-2] for row in results.values()): + return 1 + return 2 if len(coverage) > len(results) else 0 if __name__ == "__main__": @@ -83,6 +132,8 @@ if __name__ == "__main__": 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) + selection = parser.add_mutually_exclusive_group(required=True) + selection.add_argument("--library", nargs=3, action="append", metavar=("PACKAGE", "RELEASE_PATH", "SONAME")) + selection.add_argument("--system-providers", type=Path, metavar="REPORT", + help="compare all regular shared libraries in /lib and /usr/lib and record every provider") raise SystemExit(run(parser.parse_args())) diff --git a/tests/release-imports.py b/tests/release-imports.py new file mode 100644 index 0000000..148ba87 --- /dev/null +++ b/tests/release-imports.py @@ -0,0 +1,59 @@ +"""Find dynamic imports of specified symbols in a checksum-pinned release archive.""" +import argparse +import csv +import hashlib +from pathlib import Path +import re +import shutil +import subprocess +import tarfile +import tempfile + + +def scan(args): + symbols = set(args.symbol) + with args.archive.open("rb") as stream: + if hashlib.file_digest(stream, "sha256").hexdigest() != args.sha256: + raise ValueError("release archive checksum mismatch") + results = [] + files = 0 + with tempfile.TemporaryDirectory() as temp, tarfile.open(args.archive, "r|gz") as archive: + elf = Path(temp) / "member" + for entry in archive: + if not entry.isfile(): + continue + with archive.extractfile(entry) as stream: + header = stream.read(4) + if header != b"\x7fELF": + continue + with elf.open("wb") as copy: + copy.write(header) + shutil.copyfileobj(stream, copy) + files += 1 + report = subprocess.check_output(["readelf", "-Wd", "--dyn-syms", elf], text=True) + needed = ",".join(re.findall(r"\(NEEDED\)[^\n]*?\[([^\]]+)\]", report)) + for line in report.splitlines(): + cols = line.split() + if len(cols) < 8 or cols[6] != "UND" or not cols[0].removesuffix(":").isdigit(): + continue + if cols[7].split("@", 1)[0] in symbols: + results.append((entry.name.removeprefix("./"), cols[7], cols[4], needed)) + if not files: + raise ValueError("release archive contains no regular ELF files") + with args.output.open("x") as out: + table = csv.writer(out, delimiter="\t", lineterminator="\n") + table.writerow(("release_path", "import", "binding", "needed")) + table.writerows(sorted(results)) + for name in sorted(symbols): + count = sum(symbol.split("@", 1)[0] == name for _, symbol, _, _ in results) + print(f"{name}\t{count}") + print(f"Scanned {files} regular ELF files") + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("archive", type=Path) + parser.add_argument("sha256") + parser.add_argument("output", type=Path) + parser.add_argument("symbol", nargs="+") + scan(parser.parse_args()) -- 2.56.1