"""Measure Linux partial-page tail remapping in fresh writer/reader processes.""" # SPDX-License-Identifier: MIT import argparse import datetime import hashlib import json import pathlib import platform import subprocess import tempfile CASES = ( ("shared-no-sync", "shared", "no-sync"), ("shared-sync", "shared", "sync"), ("private-sync", "private", "sync"), ("untouched-sync", "untouched", "sync"), ) def run(command): return subprocess.run(command, check=True, capture_output=True, text=True, timeout=30).stdout.strip() def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--temp-dir", type=pathlib.Path, action="append", required=True, help="Test location; repeat to compare filesystems.") parser.add_argument("--build-dir", type=pathlib.Path, help="Executable temporary directory, separate from test files.") parser.add_argument("--repetitions", type=int, default=5) args = parser.parse_args() if platform.system() != "Linux": parser.error("This is a Linux behavior probe, not a portable conformance suite.") if not 1 <= args.repetitions <= 100: parser.error("repetitions must be between 1 and 100") if any(not path.is_dir() for path in args.temp_dir): parser.error("each --temp-dir must be an existing directory") source = pathlib.Path(__file__).with_name("mmap-tail.c") flags = ["-std=c11", "-O2", "-Wall", "-Wextra", "-Werror"] groups = [] with tempfile.TemporaryDirectory(prefix="hesela-tail-build-", dir=args.build_dir) as build: executable = pathlib.Path(build) / "probe" run(["cc", *flags, str(source), "-o", str(executable)]) for location in args.temp_dir: with tempfile.TemporaryDirectory(prefix="hesela-tail-files-", dir=location) as tmp: filesystem = run(["stat", "-f", "-c", "%T", tmp]) mount_type = run(["findmnt", "-n", "-o", "FSTYPE", "--target", tmp]) repetitions = [] for iteration in range(args.repetitions): rows = [] for name, mode, sync in CASES: path = pathlib.Path(tmp) / (str(iteration) + "-" + name) writer = json.loads(run([str(executable), "write", str(path), mode, sync])) # The writer has exited before starting this independent reader. reader = json.loads(run([str(executable), "read", str(path)])) if mode != "shared" and reader["tail"] != 0: raise RuntimeError("private or untouched control leaked a tail store") rows.append({"case": name, "writer": writer, "reader": reader}) repetitions.append(rows) groups.append({ "filesystemMagicLabel": filesystem, "mountFilesystemType": mount_type, "allRepetitionsIdentical": all(row == repetitions[0] for row in repetitions), "observations": repetitions, }) result = { "schemaVersion": 1, "evidenceType": "measured-linux-api-behavior", "license": "CC0-1.0", "sourceLicense": "MIT", "generatedAt": datetime.datetime.now(datetime.timezone.utc).isoformat(), "environment": { "kernel": platform.release(), "machine": platform.machine(), "python": platform.python_version(), "compiler": run(["cc", "--version"]).splitlines()[0], "compileFlags": flags, }, "sourceSha256": hashlib.sha256(source.read_bytes()).hexdigest(), "runnerSha256": hashlib.sha256(pathlib.Path(__file__).read_bytes()).hexdigest(), "repetitions": args.repetitions, "casesPerFilesystemPerRepetition": len(CASES), "filesystemRuns": groups, "limitations": [ "One host; repetitions are consistency checks, not independent reliability samples.", "No reboot, power loss, forced cache eviction, file growth or concurrent writer.", "The reader can use cached data; a visible tail byte is not persistent file content.", "The shared no-sync result may change with background writeback; no universal outcome is asserted.", "Named mode-0600 files live inside private temporary directories and are removed after the run.", "The untouched control also calls MS_SYNC; baseline payload initialization uses fsync in every case.", "No unauthorized access, information disclosure across principals or exploit is tested.", ], } print(json.dumps(result, indent=2)) if __name__ == "__main__": main()