# CAIN-42 × TALOS: Threat Model & Security Architecture
## Evolution 5 — Operation Double Sovereign

---

### 1. Adversary Model & Attack Vectors

We consider an adversary who has achieved partial or full compromise of an autonomous AI agent, tool runtime, or untrusted workflow:

| Threat Vector | Adversary Capability | CAIN-42 × TALOS Defense Mechanism | Residual Risk / Limitation |
|:---|:---|:---|:---|
| **T1: Prompt Injection / Tool Hijacking** | Injects instructions into agent prompt causing agent to propose dangerous actions (e.g. deleting files, exfiltrating data). | CAIN-42 evaluates proposal against formal policy algebra; high-impact actions require human approval; TALOS restricts execution to narrow Landlock paths. | Actions within permitted scope that are semantically malicious cannot be distinguished from intended operations without fine-grained policy rules. |
| **T2: Envelope Tampering & Key Substitution** | Intercepts envelope and alters `action`, `target`, or `resource_scope` to escalate privileges. | TALOS independently validates Ed25519 signature over RFC 8785 canonical bytes. Modified byte alters hash, causing `INVALID_SIGNATURE`. | Compromise of CAIN private signing key bypasses signature checks until key revocation is propagated. |
| **T3: Replay & Salami Attacks** | Re-sends previously approved envelope to repeat side-effects or drain resources. | `TalosDurableLedger` atomically consumes `(decision_id, nonce)` under WAL immediate transaction. Duplicate attempts receive `REPLAYED`. | External side effects outside TALOS mediation must enforce idempotency keys independently. |
| **T4: Concurrency Race Condition** | Spawns multiple threads using the identical envelope simultaneously to execute twice before state commits. | `BEGIN IMMEDIATE` locks SQLite database across threads, guaranteeing serializable commit. Exactly 1 request succeeds; $N-1$ receive `REPLAYED`. | Multi-node deployments require distributed consensus (BFT/Raft) rather than single-node SQLite WAL. |
| **T5: Filesystem & Path Traversal Escape** | Agent process attempts relative path escape (`../../etc/shadow`) or symlink trickery. | Landlock ABI 8 resolves realpaths and restricts access at the Linux VFS layer. Syscalls accessing unauthorized paths return `EACCES` / `EPERM`. | Landlock does not restrict file descriptors opened prior to sandbox activation; `close_fds=True` is required. |
| **T6: Unauthorized Network Exfiltration** | Confined worker opens raw TCP socket to exfiltrate database records or secrets. | Landlock ABI 8 TCP port restriction (`tcp_connect_ports=()`) blocks `connect(2)` at the kernel layer, raising `EACCES`. | Raw packet sockets or UDP are not restricted by Landlock ABI 8; requires seccomp BPF or network namespaces. |
| **T7: Verifier Crash & Bypass** | Adversary induces process crash or SIGKILL in verifier hoping execution will proceed unmonitored. | Fail-closed lifecycle: Child process is never spawned unless TALOS returns `ALLOW`. Failure during spawn returns `ENFORCEMENT_UNAVAILABLE`. | Stale processes spawned before a crash require cleanup by supervisor/cgroup daemon. |
| **T8: Ungoverned Alternate Tool Path** | Adversary calls `talos-coder`'s `run_command` directly, bypassing CAIN gateway. | `TalosGovernedCommandRunner` wraps tool execution; requests without valid `CainTalosEnvelope` fail closed with exit code 126. | Any unintegrated microservice retaining raw `subprocess.run(shell=True)` remains an open bypass until wrapped. |

---

### 2. Trust Boundaries

```
[ UNTRUSTED AI AGENT / LLM ]
           │ (Proposes ActionRequest)
           ▼
┌──────────────────────────────────────────────┐
│        CAIN-42 FORMAL DECISION PLANE         │
│  - Evaluates Principal DID & Tenant          │
│  - Evaluates Policy Algebra & Preconditions  │
│  - Flags Required Human Approval             │
│  - Issues Signed CainTalosEnvelope (Ed25519) │
└──────────────────────┬───────────────────────┘
                       │ (Signed Envelope)
                       ▼
┌──────────────────────────────────────────────┐
│             TALOS VERIFY KERNEL              │
│  - Independent Cryptographic Validation      │
│  - Single-Use Nonce Ledger (Atomic WAL)      │
│  - Revocation & Freshness Checks             │
│  - Operation Scope & Identity Matching       │
└──────────────────────┬───────────────────────┘
                       │ (Supervised Fork / Execve)
                       ▼
┌──────────────────────────────────────────────┐
│       ISOLATED OS EXECUTION BOUNDARY         │
│  - Linux Landlock ABI 8 (FS + TCP)           │
│  - PR_SET_NO_NEW_PRIVS                       │
│  - RLIMIT_AS (Memory) & RLIMIT_CORE (0)      │
│  - Disposable Subprocess Execution           │
└──────────────────────────────────────────────┘
```

---

### 3. Non-Negotiable Operational Invariants

1. **No Authorization → No Protected Execution:** Without a cryptographically verified `ALLOW` envelope, execution is halted fail-closed.
2. **Denial Immutability:** Neither engine can override a denial from the other.
3. **Evidence Immutability:** Execution receipts and audit records are appended to tamper-evident ledgers and cannot be retroactively excised.
