Skip to content

Commit 8234fe9

Browse files
committed
fix(spikes): address CodeRabbit review — compile error + hardening nits
Critical: - adapter host: `options: read_opt(&args.options_file)` was missing the cap arg added last commit — a compile error I introduced. Now passes args.max_input_bytes like stdout/stderr, so options is capped too. Correctness / security: - adapter host: read_capped now does a single bounded read (take(cap+1)) instead of metadata-then-read, closing the stat/read TOCTOU gap. - sandboy policy: an unknown syscall name in an EXPLICIT seccomp_deny is now a hard error (silently dropping it would weaken the author's filter, and a wrapped step's stderr is easy to lose in CI). DEFAULT_DENY keeps best-effort skip-with-warn (cross-arch portability is its stated reason). - sandboy main: replace brittle exit-code classification (err text contains("policy")) with distinction by origin — config/usage errors exit 2, runtime/confinement errors exit 1. Quality: - infersharp adapter: O(n^2) rule dedup (Vec linear scan) -> HashSet. - adapter host Cargo.toml: make the wasmtime "28" pin explicitly a placeholder with a security note (bump to current supported LTS before real build; can't verify the current train from the offline authoring sandbox). - MD040: add `text` language to fenced diagram/tree blocks (README, UNIFIED). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LMA4JwvgPBCZuEbAiG5LJL
1 parent 7927d95 commit 8234fe9

7 files changed

Lines changed: 79 additions & 50 deletions

File tree

‎audit/adapters/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ it. The WASM boundary contains the parse crash; the host validates the meaning.
4545

4646
## Layout
4747

48-
```
48+
```text
4949
adapters/
5050
wit/world.wit canonical ownaudit:adapter@0.1.0 world
5151
host/ own-adapter-host (Rust, wasmtime component model)
@@ -64,7 +64,7 @@ the same sandboxed, validated, provenance-stamped path.
6464

6565
## Where it plugs into the pipeline
6666

67-
```
67+
```text
6868
tool runs ──▶ host reads stdout/files ──▶ own-adapter-host ──▶ .wasm adapter
6969
│
7070
SARIF bytes back

‎audit/adapters/UNIFIED.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ shouldn't tell:
2626
Every tool converges on **validated SARIF**, then scoring/dedup/report are
2727
tool-agnostic. The adapter is the on-ramp:
2828

29-
```
29+
```text
3030
Roslyn ─analyze──▶ roslyn.sarif ─▶ passthrough.wasm ─┐
3131
CodeQL ─db,analyze▶ codeql.sarif ─▶ passthrough.wasm ─┼─▶ own-adapter-host
3232
Infer# ─analyze──▶ report.json ─▶ infersharp.wasm ─┘ (validate + cap +

‎audit/adapters/components/infersharp/src/lib.rs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,11 @@ impl Guest for Component {
6262
let bugs: Vec<InferBug> = serde_json::from_slice(raw)
6363
.map_err(|e| format!("parsing Infer# report.json: {e}"))?;
6464

65-
// Distinct rules, in first-seen order.
65+
// Distinct rules, in first-seen order (HashSet for O(1) dedup).
6666
let mut rules: Vec<serde_json::Value> = Vec::new();
67-
let mut seen: Vec<String> = Vec::new();
67+
let mut seen: std::collections::HashSet<&str> = std::collections::HashSet::new();
6868
for b in &bugs {
69-
if !seen.iter().any(|r| r == &b.bug_type) {
70-
seen.push(b.bug_type.clone());
69+
if seen.insert(b.bug_type.as_str()) {
7170
let name = if b.bug_type_hum.is_empty() {
7271
b.bug_type.clone()
7372
} else {

‎audit/adapters/host/Cargo.toml‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ path = "src/main.rs"
1010

1111
[dependencies]
1212
# Native host deps — fetched from crates.io when you build locally.
13-
# Component model has been stable in wasmtime since v13; pin to whatever is
14-
# current when you build (see BUILD.md — this crate was authored, not compiled,
15-
# in a network-restricted sandbox, so a minor API nudge may be needed).
13+
# SECURITY: `"28"` is a PLACEHOLDER floor, not a considered pin. Wasmtime ships a
14+
# major monthly and backports security fixes only to the LTS and the two most
15+
# recent trains — v28 is outside that window. This crate's whole job is safely
16+
# sandboxing untrusted output, so before building for real, bump to the current
17+
# supported LTS (or a recent train) and confirm the component-model API still
18+
# matches (see BUILD.md — authored, not compiled, in a network-restricted sandbox).
1619
wasmtime = { version = "28", features = ["component-model"] }
1720
anyhow = "1"
1821
serde_json = "1"

‎audit/adapters/host/src/main.rs‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,23 @@ fn parse_args() -> Result<Args> {
9494
Ok(a)
9595
}
9696

97-
/// Read a file, but refuse before allocating if it exceeds `cap`. Untrusted
98-
/// tool output can be arbitrarily large; an unbounded `fs::read` here would OOM
99-
/// the host regardless of the guest's memory cap (which only bounds the
100-
/// component, not this pre-read). Size-check via metadata, then read.
97+
/// Read a file, bounded to `cap` bytes. Untrusted tool output can be
98+
/// arbitrarily large; an unbounded `fs::read` here would OOM the host
99+
/// regardless of the guest's memory cap (which only bounds the component, not
100+
/// this pre-read). A single bounded read (`take(cap + 1)`) enforces the limit
101+
/// *while* reading — no stat/read TOCTOU gap.
101102
fn read_capped(path: &std::path::Path, cap: u64) -> Result<Vec<u8>> {
102-
let len = std::fs::metadata(path)
103-
.with_context(|| format!("stat {}", path.display()))?
104-
.len();
105-
if len > cap {
106-
bail!("input {} is {len} bytes, exceeds --max-input-bytes {cap}", path.display());
103+
use std::io::Read;
104+
let file = std::fs::File::open(path).with_context(|| format!("open {}", path.display()))?;
105+
let mut buf = Vec::new();
106+
let n = file
107+
.take(cap.saturating_add(1))
108+
.read_to_end(&mut buf)
109+
.with_context(|| format!("reading {}", path.display()))?;
110+
if n as u64 > cap {
111+
bail!("input {} exceeds --max-input-bytes {cap}", path.display());
107112
}
108-
std::fs::read(path).with_context(|| format!("reading {}", path.display()))
113+
Ok(buf)
109114
}
110115

111116
fn read_opt(p: &Option<PathBuf>, cap: u64) -> Result<Vec<u8>> {
@@ -178,7 +183,7 @@ fn run() -> Result<()> {
178183
stderr: read_opt(&args.stderr_file, args.max_input_bytes)?,
179184
artifacts,
180185
base_uri: args.base_uri.clone(),
181-
options: read_opt(&args.options_file)?,
186+
options: read_opt(&args.options_file, args.max_input_bytes)?,
182187
};
183188

184189
// --- Instantiate and call ---------------------------------------------

‎sandboy/src/main.rs‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,41 @@ use landlock::{
3333
use policy::Policy;
3434

3535
fn main() {
36-
if let Err(e) = run() {
36+
// Exit codes, distinguished by error *origin* rather than message text:
37+
// 2 = config/usage error (bad args, unreadable/invalid policy);
38+
// 1 = runtime/confinement error (Landlock/seccomp/fd/exec).
39+
let (policy_path, argv) = parse_args().unwrap_or_else(|e| {
3740
eprintln!("sandboy: {e:#}");
38-
std::process::exit(if e.to_string().contains("policy") { 2 } else { 1 });
41+
std::process::exit(2);
42+
});
43+
let policy = Policy::load(&policy_path)
44+
.with_context(|| format!("loading policy {policy_path}"))
45+
.unwrap_or_else(|e| {
46+
eprintln!("sandboy: {e:#}");
47+
std::process::exit(2);
48+
});
49+
if argv.is_empty() {
50+
eprintln!("sandboy: no command after `--`");
51+
std::process::exit(2);
52+
}
53+
if let Err(e) = confine_and_exec(&policy, &argv) {
54+
eprintln!("sandboy: {e:#}");
55+
std::process::exit(1);
3956
}
4057
}
4158

42-
fn run() -> Result<()> {
43-
let (policy_path, argv) = parse_args()?;
44-
let policy = Policy::load(&policy_path)
45-
.with_context(|| format!("loading policy {policy_path}"))?;
46-
let (prog, prog_args) = argv
47-
.split_first()
48-
.ok_or_else(|| anyhow!("no command after `--`"))?;
59+
fn confine_and_exec(policy: &Policy, argv: &[String]) -> Result<()> {
60+
let (prog, prog_args) = argv.split_first().expect("argv non-empty (checked in main)");
4961

5062
// 1. no_new_privs — required to install a seccomp filter unprivileged.
5163
set_no_new_privs()?;
5264

5365
// 2. Landlock: filesystem + TCP-port scope. Best-effort so an older kernel
5466
// degrades (and reports) instead of hard-failing.
55-
apply_landlock(&policy)?;
67+
apply_landlock(policy)?;
5668

5769
// 3. seccomp denylist: strip dangerous syscalls, allow the rest.
58-
apply_seccomp(&policy)?;
70+
apply_seccomp(policy)?;
5971

6072
// 4. Close inherited descriptors. Landlock/seccomp do NOT revoke
6173
// already-open fds, so any descriptor the launcher leaked (an open file,
@@ -158,7 +170,7 @@ fn apply_seccomp(policy: &Policy) -> Result<()> {
158170
// Denylist model: default = Allow (broad freedom), listed syscalls = EPERM.
159171
// An empty rule vec means "match unconditionally".
160172
let mut rules: BTreeMap<i64, Vec<seccompiler::SeccompRule>> = BTreeMap::new();
161-
for &nr in &policy.seccomp_deny_numbers() {
173+
for nr in policy.seccomp_deny_numbers()? {
162174
rules.insert(nr, vec![]);
163175
}
164176

‎sandboy/src/policy.rs‎

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,33 @@ impl Policy {
3535
}
3636

3737
/// Resolve the effective denylist to raw syscall numbers for this arch.
38-
/// Unknown names are skipped with a warning (a name may not exist on every
39-
/// arch), so a typo weakens the filter loudly rather than failing the run.
40-
pub fn seccomp_deny_numbers(&self) -> Vec<i64> {
41-
let names: Vec<&str> = match &self.seccomp_deny {
42-
Some(v) => v.iter().map(String::as_str).collect(),
43-
None => DEFAULT_DENY.to_vec(),
44-
};
45-
names
46-
.iter()
47-
.filter_map(|n| match syscall_number(n) {
48-
Some(nr) => Some(nr),
49-
None => {
50-
eprintln!("sandboy: warning: unknown syscall in denylist: {n} (skipped)");
51-
None
52-
}
53-
})
54-
.collect()
38+
///
39+
/// An **explicit** `seccomp_deny` from the policy author is authoritative:
40+
/// an unresolved name (typo, or unsupported on this arch) is a **hard
41+
/// error** — silently dropping it would weaken the filter the author asked
42+
/// for, and a wrapped step's stderr is easy to lose in a gate/CI context.
43+
/// The curated `DEFAULT_DENY` keeps best-effort skip-with-warn, since
44+
/// cross-arch portability is its stated reason.
45+
pub fn seccomp_deny_numbers(&self) -> anyhow::Result<Vec<i64>> {
46+
match &self.seccomp_deny {
47+
Some(names) => names
48+
.iter()
49+
.map(|n| {
50+
syscall_number(n)
51+
.ok_or_else(|| anyhow::anyhow!("unknown syscall in seccomp_deny: {n}"))
52+
})
53+
.collect(),
54+
None => Ok(DEFAULT_DENY
55+
.iter()
56+
.filter_map(|n| match syscall_number(n) {
57+
Some(nr) => Some(nr),
58+
None => {
59+
eprintln!("sandboy: warning: default-deny syscall unknown on this arch: {n} (skipped)");
60+
None
61+
}
62+
})
63+
.collect()),
64+
}
5565
}
5666
}
5767

0 commit comments

Comments
 (0)