Skip to content

Commit 4a8e658

Browse files
committed
P-037 A2.0: seal the executed Rust artifacts per take and attest them after the run
Hostile review of 1113188 found the executable had the same TOCTOU the population had just been cured of. Both tools qualified target/release/<bin> once, before the run, and then executed that shared, mutable path for the whole measurement. A cargo build landing mid-run (anyone's, any crate that rebuilds the bin) replaces the file under the launcher; the population stays intact, the tree stays clean, the snapshot says is_evidence=true, and artifacts.sha256 still names the build that did not run. The fix is the same shape as the population's: prevention plus attestation. build_rust_artifact the package's own artifacts are cleaned first and the compiler-artifact message must report the executable as NOT fresh: cargo judges freshness by source fingerprints, never by the output's bytes, so a target/ file rewritten by anyone before the take would otherwise be qualified as is. Only the package is recompiled and linked; dependencies stay cached (both bins: ~20 s). seal_artifact after qualification the executable is copied into a private, unique take directory outside the checkout, the copy is re-hashed against the qualified digest, and the run executes ONLY the copy: own-shadow-engine through engine_identity(sealed), own-cli as OWEN_RUST_CORE=sealed. finalize_run the one post-run attestation both tools call before writing: population intact, every sealed executable still the qualified build, tree clean. Any failure is not evidence and the reason is kept on the record. record_problems every artifact must carry an executed attestation whose digest equals the qualified digest and whose post_run_intact is true. Control 10d, in the real lifecycle rather than on a dict: a qualified build is sealed; the shared target/ file is rewritten and the run is unaffected; the sealed copy is rewritten and finalize_run drops is_evidence, marks the artifact tampered, and record_problems refuses the snapshot. Records with an executed digest other than the qualified one, without post_run_intact, or never sealed are refused as well. The artifacts CLI seals and attests on the CI runner too. Refs #304. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QfeUtLRm6KFRWcTGQZxQQb
1 parent 1113188 commit 4a8e658

4 files changed

Lines changed: 239 additions & 38 deletions

File tree

‎scripts/p037_evidence.py‎

Lines changed: 143 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import shutil
5757
import subprocess
5858
import sys
59+
import tempfile
5960
from collections.abc import Iterable
6061
from pathlib import Path
6162
from typing import Any
@@ -605,11 +606,24 @@ def build_rust_artifact(package: str, binary: str, *, repo: Path = ROOT) -> dict
605606
guessed target/ layout; the digest is taken from that file; the lock file,
606607
toolchain and source commit are recorded next to it.
607608
"""
609+
# The package's own artifacts are removed first so that the executable this
610+
# call qualifies is the one THIS build produced from HEAD's sources: cargo
611+
# judges freshness by source fingerprints, never by the output's bytes, so
612+
# a target/ file rewritten by anyone would otherwise be "fresh" and qualified
613+
# as is. Dependencies stay cached; only the package is recompiled and linked.
614+
clean = ["cargo", "clean", "--release", "-p", package]
608615
argv = [
609616
"cargo", "build", "--locked", "--release", "--message-format=json",
610617
"-p", package, "--bin", binary,
611618
]
612619
try:
620+
cleaned = subprocess.run(
621+
clean, cwd=repo / "rust", capture_output=True, text=True, check=False, timeout=600,
622+
)
623+
if cleaned.returncode != 0:
624+
raise EvidenceRefused(
625+
f"{' '.join(clean)} failed: {cleaned.stderr.strip()[-800:]}"
626+
)
613627
proc = subprocess.run(
614628
argv, cwd=repo / "rust", capture_output=True, text=True, check=False,
615629
timeout=1800,
@@ -621,6 +635,7 @@ def build_rust_artifact(package: str, binary: str, *, repo: Path = ROOT) -> dict
621635
f"{' '.join(argv)} failed: {proc.stderr.strip()[-1200:]}"
622636
)
623637
executable: str | None = None
638+
fresh: bool | None = None
624639
for line in proc.stdout.splitlines():
625640
try:
626641
message = json.loads(line)
@@ -639,10 +654,16 @@ def build_rust_artifact(package: str, binary: str, *, repo: Path = ROOT) -> dict
639654
and isinstance(message.get("executable"), str)
640655
):
641656
executable = str(message["executable"])
657+
fresh = bool(message.get("fresh"))
642658
if executable is None:
643659
raise EvidenceRefused(
644660
f"cargo reported no compiler-artifact executable for {package}/{binary}"
645661
)
662+
if fresh:
663+
raise EvidenceRefused(
664+
f"cargo did not rebuild {package}/{binary} after cleaning it; refusing to "
665+
"qualify an executable this build did not produce"
666+
)
646667
path = Path(executable)
647668
try:
648669
raw = path.read_bytes()
@@ -669,6 +690,8 @@ def build_rust_artifact(package: str, binary: str, *, repo: Path = ROOT) -> dict
669690
"source_commit": current_commit(repo=repo),
670691
"dirty": tree_is_dirty(repo=repo),
671692
"cargo_argv": argv,
693+
"cargo_clean_argv": clean,
694+
"rebuilt_by_this_call": True,
672695
"rustc": rust["rustc"],
673696
"cargo": rust["cargo"],
674697
"host": rust["host"],
@@ -699,6 +722,94 @@ def artifact_problems(artifact: dict[str, Any]) -> list[str]:
699722
return problems
700723

701724

725+
def new_take_dir() -> Path:
726+
"""A private, unique directory for one take's sealed executables. Outside
727+
the checkout on purpose: nothing that runs against the tree can reach it."""
728+
return Path(tempfile.mkdtemp(prefix="p037-take-"))
729+
730+
731+
def seal_artifact(artifact: dict[str, Any], take_dir: Path) -> dict[str, Any]:
732+
"""Copy the qualified executable into the take's private directory; the run
733+
executes ONLY that copy.
734+
735+
target/release/<bin> is shared mutable state: any cargo build, anyone's,
736+
can replace it while a take is running, and the launcher would open the new
737+
file under the old digest on record. Sealing prevents that; finalize_run's
738+
re-hash of the sealed copy afterwards attests that prevention held.
739+
"""
740+
source = Path(str(artifact["executable"]))
741+
try:
742+
raw = source.read_bytes()
743+
except OSError as exc:
744+
raise EvidenceRefused(f"qualified executable cannot be read for sealing: {exc}") from exc
745+
if hashlib.sha256(raw).hexdigest() != artifact["sha256"] or len(raw) != artifact["bytes"]:
746+
raise EvidenceRefused(
747+
f"{source} no longer matches the qualified build at sealing time"
748+
)
749+
take_dir.mkdir(parents=True, exist_ok=True)
750+
sealed = take_dir / source.name
751+
sealed.write_bytes(raw)
752+
sealed.chmod(0o700)
753+
check = sealed.read_bytes()
754+
if hashlib.sha256(check).hexdigest() != artifact["sha256"] or len(check) != artifact["bytes"]:
755+
raise EvidenceRefused("the sealed copy does not match the qualified build")
756+
artifact["executed"] = {
757+
"sealed_path": str(sealed),
758+
"sha256": artifact["sha256"],
759+
"bytes": len(check),
760+
}
761+
return artifact
762+
763+
764+
def executed_artifact_problems(artifact: dict[str, Any]) -> list[str]:
765+
"""Post-run attestation: the file that ran still IS the qualified build."""
766+
executed = artifact.get("executed")
767+
if not isinstance(executed, dict) or not isinstance(executed.get("sealed_path"), str):
768+
return ["artifact was never sealed for execution"]
769+
try:
770+
raw = Path(str(executed["sealed_path"])).read_bytes()
771+
except OSError as exc:
772+
return [f"the sealed executable cannot be read after the run: {exc}"]
773+
digest = hashlib.sha256(raw).hexdigest()
774+
if digest != artifact.get("sha256") or len(raw) != artifact.get("bytes"):
775+
return ["the executable that ran no longer matches the qualified build's digest"]
776+
return []
777+
778+
779+
def finalize_run(
780+
snapshot: dict[str, Any], provenance: dict[str, Any], root: Path, *, repo: Path = ROOT
781+
) -> None:
782+
"""The post-run attestations every take makes before writing its record.
783+
784+
The population must still re-hash to its blobs, every sealed executable
785+
must still re-hash to its qualified build, and the tree must still be
786+
clean. Any failure makes the snapshot not evidence; the reasons are kept.
787+
"""
788+
tampered = population_intact(provenance, root, repo=repo)
789+
snapshot["post_run_population_intact"] = not tampered
790+
if tampered:
791+
snapshot["is_evidence"] = False
792+
snapshot["population_tampered"] = tampered
793+
artifacts = snapshot.get("artifacts")
794+
if isinstance(artifacts, dict):
795+
for name, artifact in artifacts.items():
796+
if not isinstance(artifact, dict):
797+
continue
798+
problems = executed_artifact_problems(artifact)
799+
executed = artifact.get("executed")
800+
if not isinstance(executed, dict):
801+
executed = {}
802+
artifact["executed"] = executed
803+
executed["post_run_intact"] = not problems
804+
if problems:
805+
snapshot["is_evidence"] = False
806+
tampered_artifacts = snapshot.setdefault("artifact_tampered", {})
807+
tampered_artifacts[name] = problems
808+
if tree_is_dirty(repo=repo):
809+
snapshot["is_evidence"] = False
810+
snapshot["post_run_dirty"] = True
811+
812+
702813
def sanitized_env(**overrides: str) -> dict[str, str]:
703814
"""The child environment for every launcher/extractor process."""
704815
env = {k: v for k, v in os.environ.items() if k != EXTRA_REF_ENV}
@@ -874,6 +985,18 @@ def record_problems(record: dict[str, Any], *, repo: Path = ROOT) -> list[str]:
874985
problems.append(f"artifact {name!r} was built on a dirty tree")
875986
if not isinstance(artifact.get("sha256"), str):
876987
problems.append(f"artifact {name!r} carries no sha256")
988+
executed = artifact.get("executed")
989+
if not isinstance(executed, dict):
990+
problems.append(f"artifact {name!r} carries no executed attestation")
991+
else:
992+
if executed.get("sha256") != artifact.get("sha256"):
993+
problems.append(
994+
f"artifact {name!r} executed a file other than the qualified build"
995+
)
996+
if executed.get("post_run_intact") is not True:
997+
problems.append(
998+
f"artifact {name!r} does not attest it stayed intact through the run"
999+
)
8771000
return problems
8781001

8791002

@@ -987,17 +1110,26 @@ def _cli_profile() -> int:
9871110

9881111
def _cli_artifacts() -> int:
9891112
rc = 0
990-
for name, (package, binary) in RUST_ARTIFACTS.items():
991-
artifact = build_rust_artifact(package, binary)
992-
problems = artifact_problems(artifact)
993-
for problem in problems:
994-
print(f"FAIL[{name}]: {problem}")
995-
rc = 1
996-
print(
997-
f"{'ok' if not problems else 'FAIL'}[{name}] {artifact['executable']} "
998-
f"sha256={artifact['sha256']} bytes={artifact['bytes']} "
999-
f"source={artifact['source_commit'][:12]} host={artifact['host']}"
1000-
)
1113+
take_dir = new_take_dir()
1114+
try:
1115+
for name, (package, binary) in RUST_ARTIFACTS.items():
1116+
artifact = build_rust_artifact(package, binary)
1117+
problems = artifact_problems(artifact)
1118+
if not problems:
1119+
seal_artifact(artifact, take_dir)
1120+
problems = executed_artifact_problems(artifact)
1121+
for problem in problems:
1122+
print(f"FAIL[{name}]: {problem}")
1123+
rc = 1
1124+
print(
1125+
f"{'ok' if not problems else 'FAIL'}[{name}] {artifact['executable']} "
1126+
f"sha256={artifact['sha256']} bytes={artifact['bytes']} "
1127+
f"source={artifact['source_commit'][:12]} host={artifact['host']}"
1128+
+ (f" sealed={artifact['executed']['sealed_path']}" if "executed" in artifact
1129+
else "")
1130+
)
1131+
finally:
1132+
shutil.rmtree(take_dir, ignore_errors=True)
10011133
return rc
10021134

10031135

‎scripts/p037_mos_snapshot.py‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import argparse
3434
import json
3535
import os
36+
import shutil
3637
import subprocess
3738
import sys
3839
import tempfile
@@ -197,18 +198,17 @@ def take(source: str, out: Path, timeout: float, population_commit: str) -> int:
197198
profile = ev.execution_profile(include_rust=True)
198199
artifact = ev.build_rust_artifact("own-shadow", "own-shadow-engine")
199200
_refuse(ev.artifact_problems(artifact))
200-
adapter = engine_identity(str(artifact["executable"]))
201-
if adapter["sha256"] != artifact["sha256"]:
202-
raise ev.EvidenceRefused("the adapter about to run is not the qualified build")
203201
lease = ev.acquire_population(ev.materialization_root(
204202
provenance["population_commit"], provenance["analysis_manifest_sha256"]))
205203
except (RuntimeError, SystemExit, ev.EvidenceRefused) as exc:
206204
print(f"REFUSED: {exc}", file=sys.stderr)
207205
return 2
206+
take_dir = ev.new_take_dir()
208207
try:
209-
return _measure(source, out, timeout, provenance, profile, artifact, adapter)
208+
return _measure(source, out, timeout, provenance, profile, artifact, take_dir)
210209
finally:
211210
ev.release_population(lease)
211+
shutil.rmtree(take_dir, ignore_errors=True)
212212

213213

214214
def _measure(
@@ -218,9 +218,15 @@ def _measure(
218218
provenance: dict[str, Any],
219219
profile: dict[str, Any],
220220
artifact: dict[str, Any],
221-
adapter: dict[str, Any],
221+
take_dir: Path,
222222
) -> int:
223223
try:
224+
# The run executes the SEALED copy only; target/release/ is shared and
225+
# any later cargo build may rewrite it mid-run.
226+
ev.seal_artifact(artifact, take_dir)
227+
adapter = engine_identity(str(artifact["executed"]["sealed_path"]))
228+
if adapter["sha256"] != artifact["sha256"]:
229+
raise ev.EvidenceRefused("the adapter about to run is not the qualified build")
224230
root = ev.materialize_population(provenance)
225231
_refuse(ev.external_ancestor_problems(root))
226232
files = ev.analysis_paths(provenance, root)
@@ -272,14 +278,7 @@ def _measure(
272278
flush=True,
273279
)
274280

275-
tampered = ev.population_intact(provenance, root)
276-
snapshot["post_run_population_intact"] = not tampered
277-
if tampered:
278-
snapshot["is_evidence"] = False
279-
snapshot["population_tampered"] = tampered
280-
if ev.tree_is_dirty():
281-
snapshot["is_evidence"] = False
282-
snapshot["post_run_dirty"] = True
281+
ev.finalize_run(snapshot, provenance, root)
283282
if failures or parity_moved:
284283
snapshot["is_evidence"] = False
285284
if failures:

‎scripts/p037_verdict_snapshot.py‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
import argparse
5151
import json
52+
import shutil
5253
import subprocess
5354
import sys
5455
from pathlib import Path
@@ -107,21 +108,21 @@ def take(engine: str, out: Path, dirs: tuple[str, ...], population_commit: str)
107108
raise ev.EvidenceRefused("the tree is dirty before the run; evidence starts clean")
108109
profile = ev.execution_profile(include_rust=engine == "rust")
109110
artifacts: dict[str, Any] = {}
110-
overrides: dict[str, str] = {}
111111
if engine == "rust":
112112
artifact = ev.build_rust_artifact("own-cli", "own-cli")
113113
_refuse(ev.artifact_problems(artifact))
114114
artifacts["own-cli"] = artifact
115-
overrides["OWEN_RUST_CORE"] = str(artifact["executable"])
116115
lease = ev.acquire_population(ev.materialization_root(
117116
provenance["population_commit"], provenance["analysis_manifest_sha256"]))
118117
except ev.EvidenceRefused as exc:
119118
print(f"REFUSED: {exc}", file=sys.stderr)
120119
return 2
120+
take_dir = ev.new_take_dir()
121121
try:
122-
return _measure(engine, out, dirs, provenance, profile, artifacts, overrides)
122+
return _measure(engine, out, dirs, provenance, profile, artifacts, take_dir)
123123
finally:
124124
ev.release_population(lease)
125+
shutil.rmtree(take_dir, ignore_errors=True)
125126

126127

127128
def _measure(
@@ -131,9 +132,15 @@ def _measure(
131132
provenance: dict[str, Any],
132133
profile: dict[str, Any],
133134
artifacts: dict[str, Any],
134-
overrides: dict[str, str],
135+
take_dir: Path,
135136
) -> int:
137+
overrides: dict[str, str] = {}
136138
try:
139+
for artifact in artifacts.values():
140+
# The launcher runs the SEALED copy only; target/release/ is shared
141+
# and any later cargo build may rewrite it mid-run.
142+
ev.seal_artifact(artifact, take_dir)
143+
overrides["OWEN_RUST_CORE"] = str(artifact["executed"]["sealed_path"])
137144
root = ev.materialize_population(provenance)
138145
_refuse(ev.external_ancestor_problems(root))
139146
files = ev.analysis_paths(provenance, root)
@@ -166,14 +173,7 @@ def _measure(
166173
print(f" [{i:3}/{len(files)}] {rel} ({n} finding(s))", flush=True)
167174
broken = [k for k, r in snap["files"].items()
168175
if "parse_error" in r or "reference_contamination" in r]
169-
tampered = ev.population_intact(provenance, root)
170-
snap["post_run_population_intact"] = not tampered
171-
if tampered:
172-
snap["is_evidence"] = False
173-
snap["population_tampered"] = tampered
174-
if ev.tree_is_dirty():
175-
snap["is_evidence"] = False
176-
snap["post_run_dirty"] = True
176+
ev.finalize_run(snap, provenance, root)
177177
if broken:
178178
# A snapshot with an unreadable run is not a snapshot with fewer findings.
179179
# The first version of this tool asked for a `--severity note` that does

0 commit comments

Comments
 (0)