-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.rs
More file actions
420 lines (378 loc) · 14.7 KB
/
main.rs
File metadata and controls
420 lines (378 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! capsec-driver — MIR-based deep analysis for capsec.
//!
//! This binary is used as `RUSTC_WRAPPER` to intercept compilation
//! of all crates (workspace + dependencies) and analyze their MIR for
//! ambient authority usage.
//!
//! It walks every function's MIR, extracts `TerminatorKind::Call` targets,
//! classifies them against known authority patterns (FS, NET, ENV, PROC, FFI),
//! and writes findings as JSONL to a file specified by `$CAPSEC_DEEP_OUTPUT`.
//!
//! Requires nightly Rust with the `rustc-dev` component installed.
#![feature(rustc_private)]
extern crate rustc_driver;
extern crate rustc_hir;
extern crate rustc_interface;
extern crate rustc_middle;
extern crate rustc_session;
extern crate rustc_span;
use rustc_driver::Compilation;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_interface::interface::Compiler;
use rustc_middle::mir::TerminatorKind;
use rustc_middle::ty::TyCtxt;
use serde::Serialize;
use std::io::Write;
/// A finding from MIR analysis — matches the capsec Finding JSON schema.
#[derive(Debug, Clone, Serialize)]
struct DeepFinding {
file: String,
function: String,
function_line: usize,
call_line: usize,
call_col: usize,
call_text: String,
category: String,
subcategory: String,
risk: String,
description: String,
is_build_script: bool,
crate_name: String,
crate_version: String,
is_deny_violation: bool,
is_transitive: bool,
}
/// Authority category for a detected call.
#[derive(Debug, Clone)]
struct AuthorityMatch {
category: &'static str,
subcategory: &'static str,
risk: &'static str,
description: &'static str,
}
/// Classifies a resolved function path against known authority patterns.
/// Risk values use PascalCase to match the `Risk` enum serialization in cargo-capsec.
fn classify_authority(path: &str) -> Option<AuthorityMatch> {
// Filesystem
if path.starts_with("std::fs::") || path.starts_with("core::fs::") {
let subcategory = if path.contains("write") || path.contains("create") || path.contains("remove") || path.contains("rename") {
"write"
} else {
"read"
};
let risk = if path.contains("remove_dir_all") {
"Critical"
} else if path.contains("write") || path.contains("remove") || path.contains("create") {
"High"
} else {
"Medium"
};
return Some(AuthorityMatch {
category: "Fs",
subcategory,
risk,
description: "Filesystem access",
});
}
// File::open, File::create
if (path.contains("::File::open") || path.contains("::File::create"))
&& (path.starts_with("std::") || path.contains("fs::"))
{
let (sub, risk) = if path.contains("create") {
("write", "High")
} else {
("read", "Medium")
};
return Some(AuthorityMatch {
category: "Fs",
subcategory: sub,
risk,
description: "File access",
});
}
// OpenOptions
if path.contains("OpenOptions") && path.contains("open") {
return Some(AuthorityMatch {
category: "Fs",
subcategory: "read+write",
risk: "Medium",
description: "File access with custom options",
});
}
// Tokio filesystem
if path.starts_with("tokio::fs::") {
return Some(AuthorityMatch {
category: "Fs",
subcategory: "async",
risk: "Medium",
description: "Async filesystem access",
});
}
// Network — std
if path.starts_with("std::net::") {
let risk = if path.contains("connect") || path.contains("bind") {
"High"
} else {
"Medium"
};
return Some(AuthorityMatch {
category: "Net",
subcategory: "connect",
risk,
description: "Network access",
});
}
// Network — tokio
if path.starts_with("tokio::net::") {
return Some(AuthorityMatch {
category: "Net",
subcategory: "async_connect",
risk: "High",
description: "Async network access",
});
}
// Network — reqwest
if path.starts_with("reqwest::") {
return Some(AuthorityMatch {
category: "Net",
subcategory: "http",
risk: "High",
description: "HTTP request",
});
}
// Network — hyper
if path.starts_with("hyper::") && (path.contains("request") || path.contains("bind") || path.contains("connect")) {
return Some(AuthorityMatch {
category: "Net",
subcategory: "http",
risk: "High",
description: "Hyper HTTP",
});
}
// Environment
if path.starts_with("std::env::") {
let (sub, risk) = if path.contains("set_var") || path.contains("remove_var") || path.contains("set_current_dir") {
("write", "High")
} else {
("read", "Medium")
};
return Some(AuthorityMatch {
category: "Env",
subcategory: sub,
risk,
description: "Environment access",
});
}
// Process
if path.starts_with("std::process::") {
return Some(AuthorityMatch {
category: "Process",
subcategory: "spawn",
risk: "Critical",
description: "Process spawning",
});
}
None
}
/// The capsec analysis callbacks — hooks into the compiler after type checking.
struct CapsecCallbacks;
impl rustc_driver::Callbacks for CapsecCallbacks {
fn after_analysis<'tcx>(
&mut self,
_compiler: &Compiler,
tcx: TyCtxt<'tcx>,
) -> Compilation {
let crate_name = tcx.crate_name(LOCAL_CRATE).to_string();
let crate_version = std::env::var("CAPSEC_CRATE_VERSION").unwrap_or_else(|_| "0.0.0".to_string());
let debug = std::env::var("CAPSEC_DEEP_DEBUG").is_ok();
if debug {
eprintln!("[capsec-deep] Analyzing crate: {crate_name}");
}
// Skip std/core/alloc — not useful for authority analysis
if matches!(
crate_name.as_str(),
"std" | "core" | "alloc" | "compiler_builtins"
| "rustc_std_workspace_core" | "rustc_std_workspace_alloc"
| "panic_unwind" | "panic_abort" | "unwind"
| "hashbrown" | "std_detect" | "rustc_demangle"
| "addr2line" | "gimli" | "miniz_oxide" | "adler2" | "object" | "memchr"
| "cfg_if" | "libc"
) {
if debug {
eprintln!("[capsec-deep] Skipping stdlib/low-level crate: {crate_name}");
}
return Compilation::Continue;
}
// Skip proc-macro crates (compile-time only, not runtime authority)
if tcx.crate_types().contains(&rustc_session::config::CrateType::ProcMacro) {
if debug {
eprintln!("[capsec-deep] Skipping proc-macro crate: {crate_name}");
}
return Compilation::Continue;
}
// Detect build scripts
let is_build_script = crate_name == "build_script_build"
|| crate_name.starts_with("build_script_");
let mut findings: Vec<DeepFinding> = Vec::new();
let source_map = tcx.sess.source_map();
// Walk all local function bodies
for local_def_id in tcx.hir_body_owners() {
let def_id = local_def_id.to_def_id();
let def_kind = tcx.def_kind(def_id);
// Only analyze functions and methods
if !matches!(def_kind, DefKind::Fn | DefKind::AssocFn) {
continue;
}
// Get the function path and span
let fn_path = tcx.def_path_str(def_id);
let fn_span = tcx.def_span(def_id);
let fn_name = tcx.item_name(def_id).to_string();
let fn_loc = source_map.lookup_char_pos(fn_span.lo());
let fn_file = match &fn_loc.file.name {
rustc_span::FileName::Real(real) => real
.local_path()
.map(|p| p.display().to_string())
.unwrap_or_else(|| format!("{real:?}")),
other => format!("{other:?}"),
};
let fn_line = fn_loc.line;
// Get the optimized MIR for this function
let mir = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
tcx.optimized_mir(def_id)
})) {
Ok(mir) => mir,
Err(_) => {
if debug {
eprintln!("[capsec-deep] Skipping {fn_path}: MIR unavailable");
}
continue;
}
};
// Walk all basic blocks looking for Call terminators
for (_bb, bb_data) in mir.basic_blocks.iter_enumerated() {
let Some(terminator) = &bb_data.terminator else {
continue;
};
if let TerminatorKind::Call { func, .. } = &terminator.kind {
// Extract the callee DefId from the function operand
let Some((callee_def_id, _generic_args)) = func.const_fn_def() else {
continue; // indirect call (fn pointer, vtable) — skip
};
let callee_path = tcx.def_path_str(callee_def_id);
// Get call site location
let call_span = terminator.source_info.span;
let call_loc = source_map.lookup_char_pos(call_span.lo());
let call_line = call_loc.line;
let call_col = call_loc.col_display;
// Check 1: Authority pattern match (FS, NET, ENV, PROC)
if let Some(auth) = classify_authority(&callee_path) {
findings.push(DeepFinding {
file: fn_file.clone(),
function: fn_name.clone(),
function_line: fn_line,
call_line,
call_col,
call_text: callee_path.clone(),
category: auth.category.to_string(),
subcategory: auth.subcategory.to_string(),
risk: auth.risk.to_string(),
description: format!("{}: {}", auth.description, callee_path),
is_build_script,
crate_name: crate_name.clone(),
crate_version: crate_version.clone(),
is_deny_violation: false,
is_transitive: false,
});
}
// Check 2: FFI — calls to foreign functions
if tcx.is_foreign_item(callee_def_id) {
findings.push(DeepFinding {
file: fn_file.clone(),
function: fn_name.clone(),
function_line: fn_line,
call_line,
call_col,
call_text: callee_path.clone(),
category: "Ffi".to_string(),
subcategory: "ffi_call".to_string(),
risk: "High".to_string(),
description: format!("Calls FFI function {callee_path}()"),
is_build_script,
crate_name: crate_name.clone(),
crate_version: crate_version.clone(),
is_deny_violation: false,
is_transitive: false,
});
}
}
}
}
if debug {
eprintln!("[capsec-deep] Found {} findings in {crate_name}", findings.len());
}
// Write findings as JSONL — buffer all lines and write in a single batch
// to avoid interleaving when cargo compiles multiple crates in parallel.
if let Ok(output_path) = std::env::var("CAPSEC_DEEP_OUTPUT") {
let mut batch = String::new();
for finding in &findings {
if let Ok(json) = serde_json::to_string(finding) {
batch.push_str(&json);
batch.push('\n');
}
}
if !batch.is_empty() {
if let Ok(mut file) = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&output_path)
{
let _ = file.write_all(batch.as_bytes());
}
}
} else if debug {
// Print to stderr in debug mode when no output file specified
for finding in &findings {
if let Ok(json) = serde_json::to_string_pretty(finding) {
eprintln!("{json}");
}
}
}
Compilation::Continue
}
}
/// Checks if this invocation is a cargo probe (e.g., `--print=cfg`) rather than
/// an actual compilation. Cargo calls the wrapper with these flags to learn about
/// the target — we must delegate to real rustc for these.
fn is_target_info_query(args: &[String]) -> bool {
args.iter().any(|a| {
a.starts_with("--print")
|| a == "-vV"
|| a == "--version"
})
}
fn main() {
// Install the ICE hook for useful panic reports
rustc_driver::install_ice_hook(
"https://github.com/auths-dev/capsec/issues",
|_| (),
);
let mut args: Vec<String> = std::env::args().collect();
// When used as RUSTC_WORKSPACE_WRAPPER, cargo invokes us as:
// capsec-driver /path/to/real/rustc <rustc-args...>
// The second arg is the real rustc path — we need to strip it since
// run_compiler expects args[0] to be the binary name followed by rustc flags.
if args.len() > 1 && (args[1].ends_with("rustc") || args[1].contains("/rustc")) {
args.remove(1);
}
// For cargo probe calls (--print=cfg, --version, etc.), run as plain rustc
// without our analysis callbacks. This is the pattern used by Miri and Clippy.
if is_target_info_query(&args) {
let mut callbacks = rustc_driver::TimePassesCallbacks::default();
rustc_driver::run_compiler(&args, &mut callbacks);
return;
}
// For actual compilations, run with our analysis callbacks
let mut callbacks = CapsecCallbacks;
rustc_driver::run_compiler(&args, &mut callbacks);
}