#!/usr/bin/env python3 """Verify a CAIN-42 Evolution #7 live SECCOMP bundle without trusting any CAIN website or importing CAIN code. Needs Python 3.8+, `cryptography`, and verify_pbft_qc_bundle.py (published next to this file). python3 verify_e7_seccomp.py Checks (each PASS/FAIL): DECISION the CAIN decision the ZoD rests on is quorum-certified by the live cluster CERTIFICATE the ZoD authorization certificate re-hashes and is signed by the hypervisor CONSENSUS that certificate was ordered by the live cluster: >= 3 of the 4 pinned signers, digest recomputed here APPROVAL the REQUIRE_APPROVAL decision carries a human approval for exactly this ZoD CHAIN the evidence chain re-hashes, links and is signed by the hypervisor RUN the published command and output hash to the argv_hash / stdout_sha256 the hypervisor signed for this ZoD, and the run exited 0 KERNEL the output shows seccomp mode 2 with no_new_privs, every escape syscall refused with EPERM (clone3: ENOSYS), raw/netlink sockets refused, egress unreachable, and ordinary fork still working FILTER seccomp_filter.hex.txt (the cBPF program, hex) hashes to the declared filter hash, which is in the measurement the hypervisor registered for this instance (operator-asserted binding; no external attestation of the loaded filter) """ from __future__ import annotations import base64 import hashlib import json import sys import urllib.request from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from cryptography.exceptions import InvalidSignature # noqa: E402 from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey # noqa: E402 D_EVIDENCE, D_ZOD_AUTH, D_APPROVAL, D_ZOD_ORDER = ("CAIN45/EVIDENCE/v1", "CAIN45/ZOD-AUTHORIZATION-CERTIFICATE/v1", "CAIN45/HUMAN-APPROVAL/v1", "CAIN45/ZOD-AUTHORIZATION-ORDER/v1") def canon(o): return json.dumps(o, sort_keys=True, separators=(",", ":"), ensure_ascii=True).encode() def digest(domain, fields): return hashlib.sha256(canon({"domain": domain, **fields})).hexdigest() def sig_ok(pub, sig, domain, fields): try: Ed25519PublicKey.from_public_bytes(base64.b64decode(pub)).verify(base64.b64decode(sig), digest(domain, fields).encode()) return True except (InvalidSignature, ValueError, TypeError): return False def load(base, name): if base.startswith("http"): with urllib.request.urlopen(base.rstrip("/") + "/" + name, timeout=30) as r: return json.loads(r.read()) return json.loads((Path(base) / name).read_text()) def chain_ok(chain, hv): prev, bad = "0" * 64, [] for i, e in enumerate(chain, 1): f = {k: e[k] for k in ("seq", "prev_hash", "at", "zod_id", "event", "data")} if e["seq"] != i or e["prev_hash"] != prev or digest(D_EVIDENCE, f) != e["entry_hash"] \ or not sig_ok(hv, e["signature_b64"], D_EVIDENCE, f): bad.append(i) prev = e["entry_hash"] return not bad, bad DENIED = ("unshare_user", "unshare_net", "setns", "mount", "ptrace_attach_1", "bpf", "keyctl", "io_uring_setup", "perf_event_open", "chroot", "kexec_load", "init_module", "clone_newuser", "socket_packet", "socket_netlink") def h(o): return hashlib.sha256(canon(o)).hexdigest() def chain_ok(chain, hv): prev, bad = "0" * 64, [] for i, e in enumerate(chain, 1): f = {k: e[k] for k in ("seq", "prev_hash", "at", "zod_id", "event", "data")} if e["seq"] != i or e["prev_hash"] != prev or digest(D_EVIDENCE, f) != e["entry_hash"] \ or not sig_ok(hv, e["signature_b64"], D_EVIDENCE, f): bad.append(i) prev = e["entry_hash"] return not bad, bad def load_bytes(base, name): if base.startswith("http"): with urllib.request.urlopen(base.rstrip("/") + "/" + name, timeout=30) as r: return r.read() return (Path(base) / name).read_bytes() def verify(base): import verify_pbft_qc_bundle as Q run, chain, decision = load(base, "SECCOMP_RUN.json"), load(base, "EVIDENCE_CHAIN.json"), load(base, "decision.json") qcs, membership, ap = load(base, "qcs.json"), load(base, "membership.json"), load(base, "approval.json") mb = Q.Membership(membership["membership"]) hv, zd, out = run["hypervisor_public_key_b64"], run["zod"], [] dq, c = qcs["cain_decision"], decision["consensus"] dop = {"action": "fabric_decision", "resource": f"fabric/{decision['decision_id']}", "data": {"decision_id": decision["decision_id"], "commitment_sha256": c["commitment_sha256"], "pre_verdict": c["pre_verdict"]}} want = Q.proposal_request_digest({"operation": dop, "request_id": decision["decision_id"], "client_id": "cain-hosted-fabric"}) ok, why = Q.verify_qc(dq, mb) out.append(("DECISION", ok and dq["proposal_digest"] == want and dq["sequence"] == c["sequence"], f"{decision['decision_id']} pre-verdict {c['pre_verdict']}, QC seq {dq['sequence']}: {why or 'quorum ok'}")) cert = zd["consensus_certificate"] body = {k: v for k, v in cert.items() if k not in ("consensus", "certificate_digest", "signature_b64", "issuer_public_key_b64")} good = digest(D_ZOD_AUTH, body) == cert["certificate_digest"] and sig_ok(hv, cert["signature_b64"], D_ZOD_AUTH, body) \ and cert["zod_id"] == zd["zod_id"] and cert["decision"]["decision_id"] == decision["decision_id"] out.append(("CERTIFICATE", good, zd["zod_id"])) con = cert["consensus"] op = {"action": "zod_authorization", "resource": f"zod/{zd['zod_id']}", "data": {"zod_id": zd["zod_id"], "certificate_digest": cert["certificate_digest"], "domain": D_ZOD_ORDER}} want = Q.proposal_request_digest({"operation": op, "request_id": con["request_id"], "client_id": con["client_id"]}) qc = qcs["zod"] ok, why = Q.verify_qc(qc, mb) out.append(("CONSENSUS", ok and qc["proposal_digest"] == want and qc["sequence"] == con["sequence"], f"QC seq {qc['sequence']} signers {', '.join(qc.get('signer_set') or [])}: {why or 'quorum ok, digest matches'}")) spec_hash = hashlib.sha256(canon(zd["spec"])).hexdigest() af = {"human_id": ap["human_id"], "zod_id": zd["zod_id"], "decision_id": decision["decision_id"], "spec_hash": spec_hash, "expires_at": ap["expires_at"]} approver = run["approver_public_key_b64"] out.append(("APPROVAL", c["pre_verdict"] == "REQUIRE_APPROVAL" and cert["human_approval"]["signature_b64"] == ap["signature_b64"] and sig_ok(approver, ap["signature_b64"], D_APPROVAL, af), f"approval by {ap['human_id']} bound into the certificate")) cok, bad = chain_ok(chain, hv) out.append(("CHAIN", cok, f"{len(chain)} entries" + (f"; broken at {bad[:5]}" if bad else ", linked and signed"))) started = [e for e in chain if e["zod_id"] == zd["zod_id"] and e["event"] == "EXECUTION_STARTED"] result = [e for e in chain if e["zod_id"] == zd["zod_id"] and e["event"] == "EXECUTION_RESULT"] rok = len(started) == 1 and len(result) == 1 and started[0]["data"]["argv_hash"] == h(run["argv"]) \ and result[0]["data"]["stdout_sha256"] == h(run["stdout"]) and result[0]["data"]["returncode"] == 0 == run["returncode"] out.append(("RUN", rok, "published command and output match the hashes the hypervisor signed")) try: k = json.loads(run["stdout"]) except ValueError: k = {} refused = [n for n in DENIED if k.get(n) == "EPERM"] kok = k.get("Seccomp") == "2" and k.get("NoNewPrivs") == "1" and len(refused) == len(DENIED) \ and k.get("clone3") == "ENOSYS" and k.get("egress") in ("ENETUNREACH", "EPERM") and k.get("fork_exit") == 7 \ and k.get("uid") == 65534 out.append(("KERNEL", kok, f"seccomp mode {k.get('Seccomp')}, {len(refused)}/{len(DENIED)} escape calls EPERM, " f"clone3 {k.get('clone3')}, egress {k.get('egress')}, fork ok={k.get('fork_exit') == 7}")) fb = bytes.fromhex(load_bytes(base, "seccomp_filter.hex.txt").decode().strip()) fh = hashlib.sha256(fb).hexdigest() reg = [e for e in chain if e["event"] == "INSTANCE_REGISTERED"] fok = fh == run["seccomp_filter_sha256"] == run["measurement"]["seccomp_filter_sha256"] and len(fb) % 8 == 0 \ and any(e["data"].get("measurement") == h(run["measurement"]) for e in reg) out.append(("FILTER", fok, f"{len(fb) // 8} cBPF instructions, sha256 {fh[:16]}..., in the registered measurement " "(operator-asserted binding)")) return out def main(argv): if len(argv) != 1: print(__doc__) return 2 res = verify(argv[0]) for name, ok, detail in res: print(f"[{'PASS' if ok else 'FAIL'}] {name:12s} {detail}") good = all(ok for _, ok, _ in res) print(f"\n{sum(ok for _, ok, _ in res)}/{len(res)} checks") print("VERIFIED" if good else "NOT VERIFIED") return 0 if good else 1 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))