|
| 1 | +# capsec-deep |
| 2 | + |
| 3 | +MIR-based deep analysis driver for capsec. Uses `rustc`'s Mid-level IR to detect ambient authority usage that syntactic analysis misses — macro-expanded FFI calls, trait dispatch, and generic instantiation. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +- Nightly Rust toolchain (pinned in `rust-toolchain.toml`) |
| 8 | +- `rustc-dev` and `llvm-tools` components |
| 9 | + |
| 10 | +## Install |
| 11 | + |
| 12 | +```bash |
| 13 | +cd crates/capsec-deep |
| 14 | +cargo install --path . |
| 15 | +``` |
| 16 | + |
| 17 | +This installs the `capsec-driver` binary, which `cargo capsec audit --deep` invokes automatically. |
| 18 | + |
| 19 | +## How it works |
| 20 | + |
| 21 | +`capsec-driver` is a custom Rust compiler driver. When invoked via `RUSTC_WRAPPER`, it intercepts every crate compilation, runs the normal compiler pipeline through type checking, then walks the MIR of every function looking for: |
| 22 | + |
| 23 | +- **Authority calls** — `std::fs::*`, `std::net::*`, `std::env::*`, `std::process::*` resolved through the full type system (including macro expansion) |
| 24 | +- **FFI calls** — any call to a `DefKind::ForeignFn` item (catches `-sys` crate wrappers like `libgit2-sys`, `sqlite3-sys`) |
| 25 | + |
| 26 | +Findings are written as JSONL to a temp file, which the main `cargo-capsec` CLI reads, merges with syntactic findings, and feeds into the cross-crate export map system for transitive propagation. |
| 27 | + |
| 28 | +## Architecture |
| 29 | + |
| 30 | +``` |
| 31 | +cargo capsec audit --deep |
| 32 | + └→ cargo check (with RUSTC_WRAPPER=capsec-driver) |
| 33 | + └→ capsec-driver replaces rustc for each crate |
| 34 | + └→ after_analysis callback: |
| 35 | + 1. Walk MIR BasicBlocks → TerminatorKind::Call |
| 36 | + 2. Extract callee DefId → tcx.def_path_str() |
| 37 | + 3. Classify against authority patterns |
| 38 | + 4. Check tcx.is_foreign_item() for FFI |
| 39 | + 5. Write JSONL to $CAPSEC_DEEP_OUTPUT |
| 40 | +``` |
| 41 | + |
| 42 | +## Standalone testing |
| 43 | + |
| 44 | +```bash |
| 45 | +# Test on a single file |
| 46 | +CAPSEC_DEEP_DEBUG=1 cargo run -- --edition 2024 tests/fixtures/simple_fs.rs |
| 47 | + |
| 48 | +# Test FFI detection through macros |
| 49 | +CAPSEC_DEEP_DEBUG=1 cargo run -- --edition 2024 tests/fixtures/macro_ffi.rs |
| 50 | +``` |
| 51 | + |
| 52 | +## Excluded from workspace |
| 53 | + |
| 54 | +This crate requires nightly and is listed in the workspace `exclude` list. It builds independently and does not affect `cargo test --workspace` or `cargo check --workspace` on stable. |
0 commit comments