3535import re
3636import subprocess
3737import sys
38+ import tempfile
3839from pathlib import Path
3940
4041import execbinding as eb # the single implementation of the harness-digest formula
4142
4243T0_PATH = "docs/notes/p022-263-t0-protocol-freeze.md"
4344STEP7_NOTE = "docs/notes/p022-263a-step7-environment-capture.md"
44- BINDING_ARTIFACTS = (
45- "docs/evidence/calibration/p022-263a-policy-freeze.json" ,
46- "docs/evidence/calibration/p022-263a-design-constants.json" ,
47- "docs/evidence/calibration/p022-263a-training-preregistration.json" ,
48- )
45+ # Each artifact binds the digest at its own exact path. Named here so the check
46+ # reads the binding rather than searching the file for a hopeful substring.
47+ BINDING_ARTIFACTS = {
48+ "docs/evidence/calibration/p022-263a-policy-freeze.json" :
49+ ("measurement_harness_digest" ,),
50+ "docs/evidence/calibration/p022-263a-design-constants.json" :
51+ ("bound_measurement_harness_digest" ,),
52+ "docs/evidence/calibration/p022-263a-training-preregistration.json" :
53+ ("bindings" , "measurement_harness_digest" ),
54+ }
4955STEP7_TOOLS = ("scripts/step7/envcapture.py" , "scripts/step7/hostqual.py" ,
5056 "scripts/step7/execbinding.py" )
5157DIGEST_RE = re .compile (r"measurement_harness_digest\s*\n?\s*([0-9a-f]{64})" )
@@ -57,6 +63,183 @@ def blob(repo: Path, commit: str, path: str) -> bytes | None:
5763 return proc .stdout if proc .returncode == 0 else None
5864
5965
66+ # The witness runs INSIDE the candidate tree's own modules, in a subprocess, so
67+ # that importing them cannot bind to this gate's copies and a passing result
68+ # cannot come from anything but the code being merged.
69+ WITNESS_SOURCE = r"""
70+ import json, sys, subprocess
71+ from pathlib import Path
72+ sys.path.insert(0, "scripts/step7")
73+ import hostqual as hq
74+ import execbinding as eb
75+
76+ tmp = Path(sys.argv[1])
77+ POWER = {"platform": "windows", "plan_guid": hq.WIN_ACCEPTED_PLANS[0],
78+ "processor_min_ac": 100, "processor_max_ac": 100,
79+ "processor_min_dc": 100, "processor_max_dc": 100}
80+ hq.power_snapshot = lambda: dict(POWER)
81+ failures = []
82+
83+ def repo_with(name, files):
84+ root = tmp / name
85+ root.mkdir(parents=True, exist_ok=True)
86+ run = lambda *a: subprocess.run(["git", "-C", str(root), *a], capture_output=True, check=True)
87+ if not (root / ".git").exists():
88+ run("init", "-q"); run("config", "user.email", "w@x"); run("config", "user.name", "w")
89+ for rel, text in files.items():
90+ f = root / rel; f.parent.mkdir(parents=True, exist_ok=True)
91+ f.write_text(text, encoding="utf-8")
92+ run("add", "-A"); run("commit", "-q", "-m", "w")
93+ return root, subprocess.run(["git", "-C", str(root), "rev-parse", "HEAD"],
94+ capture_output=True, text=True, check=True).stdout.strip()
95+
96+ def w(name, doc):
97+ f = tmp / name; f.write_text(json.dumps(doc), encoding="utf-8"); return f
98+
99+ # --- W3/W4: the authority state must bite on both readers -------------------
100+ for label, status, flag, must_pass in (("FROZEN+false", "FROZEN.", "false", False),
101+ ("NOT_FROZEN+true", "NOT_FROZEN.", "true", False),
102+ ("FROZEN+true", "FROZEN.", "true", True)):
103+ body = "```text\nStatus:\n %s\n collection_authorized: %s\n```\n" % (status, flag)
104+ root, commit = repo_with("t0-" + flag + status[:4], {"t0.md": body})
105+ got = hq.bind_t0(root, "t0.md", commit)[1]["result"] == "pass"
106+ if got != must_pass:
107+ failures.append("hostqual accepted " + label if got else
108+ "hostqual refused " + label)
109+ try:
110+ eb.t0_at(root, "t0.md", commit); accepted = True
111+ except eb.BindingRefused:
112+ accepted = False
113+ if accepted != must_pass:
114+ failures.append("execbinding accepted " + label if accepted else
115+ "execbinding refused " + label)
116+
117+ # --- W1/W2: the campaign link must be verified, not merely mentioned --------
118+ froot, fcommit = repo_with("freeze", {"d7/payload.json": '{"kind": "d7"}\n',
119+ "d7/attestation.json": '{"kind": "att"}\n'})
120+ payload, att = froot / "d7/payload.json", froot / "d7/attestation.json"
121+ blob = subprocess.run(["git", "-C", str(froot), "rev-parse", fcommit + ":d7/payload.json"],
122+ capture_output=True, text=True, check=True).stdout.strip()
123+ binding = w("binding.json", {"kind": "own.net/p022/execution-binding"})
124+ link = {"kind": hq.CAMPAIGN_LINK_SCHEMA, "schema": 1,
125+ "execution_binding_sha256": hq.sha256_file(binding),
126+ "d7_payload": {"path": "d7/payload.json", "sha256": hq.sha256_file(payload),
127+ "blob_sha": blob, "commit": fcommit},
128+ "d7_attestation": {"path": "d7/attestation.json", "sha256": hq.sha256_file(att)},
129+ "recorded_at": "w"}
130+ if hq.check_campaign_link(link, binding, froot)["result"] != "pass":
131+ failures.append("a correct campaign link was refused")
132+ wrong = dict(link, execution_binding_sha256="f" * 64)
133+ if hq.check_campaign_link(wrong, binding, froot)["result"] != "fail":
134+ failures.append("a link naming another execution binding was accepted")
135+ payload.write_text('{"kind": "d7", "edited": true}\n', encoding="utf-8")
136+ if hq.check_campaign_link(link, binding, froot)["result"] != "fail":
137+ failures.append("a freeze edited after linking was accepted")
138+
139+ # --- W5: a campaign cannot be swapped between preflight and postflight ------
140+ QUAL = {"kind": hq.QUALIFICATION_SCHEMA, "schema": 1, "stratum": "linux",
141+ "t0": {"commit": "c" * 40, "path": "t0.md", "blob_sha": "b" * 40,
142+ "sha256": "f" * 64, "status": "FROZEN"},
143+ "environment_id": "env-1", "host_fingerprint": "sha256:abc",
144+ "provisioning": {"sha256": "0" * 64}, "environment_manifest": {"sha256": "1" * 64},
145+ "qualification_tool": {"sha256": "2" * 64}, "power_snapshot": dict(POWER),
146+ "predicate": {k: "pass" for k in hq.PREDICATE_KEYS},
147+ "memory_metric": hq.STRATUM_METRIC["linux"], "qualified": True, "qualified_at": "w"}
148+ MANIFEST = {"schema": hq.ENVCAPTURE_SCHEMA,
149+ "identity": {"environment_id": {"status": "observed", "value": "env-1"},
150+ "host_fingerprint": {"status": "observed", "value": "sha256:abc"}},
151+ "provenance": {"ci": False}}
152+ QUAL["environment_identity_sha256"] = hq.canonical_sha256(MANIFEST["identity"])
153+ CAND = b"candidate bytes"
154+ cand = tmp / "cand.bin"; cand.write_bytes(CAND)
155+ qpath = w("qual.json", QUAL)
156+ stratum_block = lambda m, extra: {"qualification_sha256": hq.sha256_file(qpath),
157+ "environment_id": "env-1", "host_fingerprint": "sha256:abc",
158+ "environment_identity_sha256": QUAL["environment_identity_sha256"],
159+ "candidate_sha256": hq.sha256_bytes(CAND),
160+ "candidate_bytes": len(CAND), "memory_metric": m, **extra}
161+ BINDING = {"kind": eb.BINDING_SCHEMA, "schema": 1,
162+ "t0": {"commit": "c" * 40, "path": "t0.md", "blob_sha": "b" * 40, "sha256": "f" * 64},
163+ "instrument": {"accepted_commit": "a" * 40, "harness_digest": "d" * 64},
164+ "workloads": {"path": eb.WORKLOAD_MANIFEST, "manifest_sha256": "9" * 64},
165+ "linux": stratum_block(hq.STRATUM_METRIC["linux"], {}),
166+ "windows": stratum_block(hq.STRATUM_METRIC["windows"], {})}
167+ bpath = w("exec-binding.json", BINDING)
168+ mpath = w("manifest.json", MANIFEST)
169+ dpath = w("declaration.json", {"kind": hq.DECLARATION_SCHEMA, "schema": 1,
170+ "no_campaign_workload": True,
171+ "no_interactive_user_workload": True,
172+ "no_prohibited_background_job_active": True,
173+ "operator": "w", "recorded_at": "w"})
174+ probe = tmp / "probe.json"; probe.write_text("{}", encoding="utf-8")
175+ quiet = {"eligible": True, "reason": "", "samples": [0.01], "mean": 0.01, "max": 0.01}
176+
177+ def link_for(binding_file, root, commit_sha, payload_file, att_file, blob_sha):
178+ return {"kind": hq.CAMPAIGN_LINK_SCHEMA, "schema": 1,
179+ "execution_binding_sha256": hq.sha256_file(binding_file),
180+ "d7_payload": {"path": "d7/payload.json", "sha256": hq.sha256_file(payload_file),
181+ "blob_sha": blob_sha, "commit": commit_sha},
182+ "d7_attestation": {"path": "d7/attestation.json", "sha256": hq.sha256_file(att_file)},
183+ "recorded_at": "w"}
184+
185+ r2, c2 = repo_with("freeze2", {"d7/payload.json": '{"kind": "d7", "campaign": "two"}\n',
186+ "d7/attestation.json": '{"kind": "att"}\n'})
187+ p2, a2 = r2 / "d7/payload.json", r2 / "d7/attestation.json"
188+ b2 = subprocess.run(["git", "-C", str(r2), "rev-parse", c2 + ":d7/payload.json"],
189+ capture_output=True, text=True, check=True).stdout.strip()
190+ r3, c3 = repo_with("freeze3", {"d7/payload.json": '{"kind": "d7", "campaign": "three"}\n',
191+ "d7/attestation.json": '{"kind": "att"}\n'})
192+ p3, a3 = r3 / "d7/payload.json", r3 / "d7/attestation.json"
193+ b3 = subprocess.run(["git", "-C", str(r3), "rev-parse", c3 + ":d7/payload.json"],
194+ capture_output=True, text=True, check=True).stdout.strip()
195+ l2 = w("link2.json", link_for(bpath, r2, c2, p2, a2, b2))
196+ l3 = w("link3.json", link_for(bpath, r3, c3, p3, a3, b3))
197+
198+ pre = hq.session_eligibility(bpath, qpath, mpath, dpath, cand, l2, r2, quiesce_result=quiet)
199+ if not pre["eligible"]:
200+ failures.append("a correctly linked session was refused: " + str(pre["reasons"])[:120])
201+ ppath = w("preflight.json", pre)
202+ after = hq.session_admissibility(bpath, qpath, ppath, mpath, cand, probe, l3, r3)
203+ if after["admissible"]:
204+ failures.append("a campaign swapped between preflight and postflight was admissible")
205+
206+ print(json.dumps(failures))
207+ """
208+
209+
210+ def witness (repo : Path , commit : str ) -> list [str ]:
211+ """Run the target tree's own tools and require them to refuse.
212+
213+ A name in a source file proves nothing: a comment, a dead function or
214+ `def check_campaign_link(): pass` all satisfy a lexical check, and an earlier
215+ revision of this gate was satisfied by exactly that — its own fixture shipped
216+ the no-op and called the world compliant. So the tools are extracted from the
217+ target tree, imported in a subprocess, and driven against attacks they are
218+ claimed to stop.
219+ """
220+ with tempfile .TemporaryDirectory () as raw :
221+ work = Path (raw )
222+ for tool in STEP7_TOOLS :
223+ data = blob (repo , commit , tool )
224+ if data is None :
225+ return [f"{ tool } is absent" ]
226+ target = work / tool
227+ target .parent .mkdir (parents = True , exist_ok = True )
228+ target .write_bytes (data )
229+ script = work / "witness.py"
230+ script .write_text (WITNESS_SOURCE , encoding = "utf-8" )
231+ fixtures = work / "fixtures"
232+ fixtures .mkdir ()
233+ proc = subprocess .run ([sys .executable , str (script ), str (fixtures )],
234+ capture_output = True , text = True , cwd = str (work ), check = False )
235+ if proc .returncode != 0 :
236+ tail = (proc .stderr or proc .stdout ).strip ().splitlines ()[- 1 :] or ["<no output>" ]
237+ return [f"the machinery could not be exercised: { tail [0 ][:160 ]} " ]
238+ try :
239+ return list (json .loads (proc .stdout .strip ().splitlines ()[- 1 ]))
240+ except (ValueError , IndexError ):
241+ return [f"the witness produced no verdict: { proc .stdout [:120 ]!r} " ]
242+
60243def check (name : str , ok : bool , detail : str ) -> dict [str , object ]:
61244 return {"check" : name , "result" : "pass" if ok else "fail" , "detail" : detail }
62245
@@ -93,31 +276,42 @@ def gate(repo: Path, commit: str) -> list[dict[str, object]]:
93276 "this tree does not contain" ))
94277
95278 rebound = []
96- for path in BINDING_ARTIFACTS :
279+ for path , field in BINDING_ARTIFACTS . items () :
97280 raw = blob (repo , commit , path )
98281 if raw is None :
99282 rebound .append (f"{ Path (path ).name } : absent" )
100- elif expected not in raw .decode ("utf-8" , "replace" ):
101- rebound .append (f"{ Path (path ).name } : still bound to an older digest" )
283+ continue
284+ try :
285+ doc = json .loads (raw .decode ("utf-8" ))
286+ except ValueError as exc :
287+ rebound .append (f"{ Path (path ).name } : not readable JSON ({ exc } )" )
288+ continue
289+ # The exact field, by its own path in that artifact. A substring search
290+ # would accept the right digest sitting in any passing field while the
291+ # real binding stayed old — which is the same defect one level down.
292+ node : object = doc
293+ for key in field :
294+ node = node .get (key ) if isinstance (node , dict ) else None
295+ if node != expected :
296+ got = str (node )[:12 ] if node is not None else "<absent>"
297+ rebound .append (f"{ Path (path ).name } : { '.' .join (field )} is { got } , not { expected [:12 ]} " )
102298 results .append (check (
103299 "steps_4_5_6_rebound" , not rebound ,
104300 "; " .join (rebound ) if rebound else
105301 f"the policy freeze, the design constants and the training preregistration all bind "
106302 f"{ expected [:12 ]} " ))
107303
108304 missing_tools = [t for t in STEP7_TOOLS if blob (repo , commit , t ) is None ]
109- hostqual = (blob (repo , commit , "scripts/step7/hostqual.py" ) or b"" ).decode ("utf-8" , "replace" )
110- enforces_link = "CAMPAIGN_LINK_SCHEMA" in hostqual and "check_campaign_link" in hostqual
111- enforces_authority = "collection_authorized is" in hostqual or (
112- "authorized is not True" in hostqual )
113- results .append (check (
114- "step7_machinery_present" , not missing_tools and enforces_link and enforces_authority ,
115- "; " .join (filter (None , [
116- f"missing: { missing_tools } " if missing_tools else "" ,
117- "" if enforces_link else "hostqual does not enforce the campaign link" ,
118- "" if enforces_authority else "hostqual does not enforce the authority state" ,
119- ])) or "capture, qualification and binding tools are present, and the campaign link and "
120- "authority state are enforced" ))
305+ if missing_tools :
306+ results .append (check ("step7_machinery_enforces" , False , f"missing: { missing_tools } " ))
307+ else :
308+ refusals = witness (repo , commit )
309+ results .append (check (
310+ "step7_machinery_enforces" , not refusals ,
311+ "; " .join (refusals ) if refusals else
312+ "the target tree's own tools were run and refused all four attacks: a link naming "
313+ "another binding, a freeze edited after linking, FROZEN with collection_authorized "
314+ "false, and a campaign swapped between preflight and postflight" ))
121315
122316 note = (blob (repo , commit , STEP7_NOTE ) or b"" ).decode ("utf-8" , "replace" )
123317 revoked = "AUTOMATIC AUTHORISATION OF THE FIRST STEP-7 COLLECTION IS REVOKED" in note
0 commit comments