Skip to content

Commit 7927d95

Browse files
committed
fix(spikes): address Codex review — fd leak, unbounded input read, BUILD cwd
- sandboy (P1): close inherited descriptors before execve. Landlock/seccomp do not revoke already-open fds, so a descriptor leaked by the launcher (open file / live socket without FD_CLOEXEC) would pass into the wrapped command and bypass the FS/port allowlists. Mark every fd > 2 CLOEXEC via one close_range() before handoff; stdio kept. README documents it; this is the second audited unsafe at the syscall seam. - adapter host (P2): cap artifact/stdout/stderr reads. The host did an unbounded fs::read on untrusted tool output before any limit applied, which could OOM the host regardless of the guest memory cap. Add read_capped() (metadata size-check before allocating) + --max-input-bytes (default 64 MiB). - BUILD.md (P2): fix cwd — after the step-2 subshells cwd stays in audit/adapters, so `cd ../../host` resolved to Own.NET/host. Use `cd host` and keep the run step's cwd consistent. All three are correctness/security fixes on the spikes; still authored-not- built here (network-restricted), consistent with the rest of the branch. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LMA4JwvgPBCZuEbAiG5LJL
1 parent b94e477 commit 7927d95

4 files changed

Lines changed: 68 additions & 16 deletions

File tree

‎audit/adapters/BUILD.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@ cargo install wasm-tools # inspect/validate components (optional
3131
## 3. Build the host
3232

3333
```bash
34-
cd ../../host
34+
# from audit/adapters (step 2 used subshells, so cwd is still here)
35+
cd host
3536
cargo build --release
3637
# -> target/release/own-adapter-host
38+
cd ..
3739
```
3840

3941
## 4. Run it — happy path
4042

4143
```bash
42-
cd ..
44+
# from audit/adapters
4345
./host/target/release/own-adapter-host \
4446
--component components/infersharp.wasm \
4547
--tool infersharp \

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct Args {
4949
fuel: u64,
5050
epoch_ms: u64,
5151
max_bytes: usize,
52+
max_input_bytes: u64,
5253
max_results: usize,
5354
}
5455

@@ -59,6 +60,7 @@ fn parse_args() -> Result<Args> {
5960
fuel: 2_000_000_000,
6061
epoch_ms: 5_000,
6162
max_bytes: 64 * 1024 * 1024,
63+
max_input_bytes: 64 * 1024 * 1024,
6264
max_results: 200_000,
6365
..Default::default()
6466
};
@@ -84,16 +86,31 @@ fn parse_args() -> Result<Args> {
8486
"--fuel" => a.fuel = next()?.parse().context("bad --fuel")?,
8587
"--epoch-ms" => a.epoch_ms = next()?.parse().context("bad --epoch-ms")?,
8688
"--max-bytes" => a.max_bytes = next()?.parse().context("bad --max-bytes")?,
89+
"--max-input-bytes" => a.max_input_bytes = next()?.parse().context("bad --max-input-bytes")?,
8790
"--max-results" => a.max_results = next()?.parse().context("bad --max-results")?,
8891
other => bail!("unknown flag: {other}"),
8992
}
9093
}
9194
Ok(a)
9295
}
9396

94-
fn read_opt(p: &Option<PathBuf>) -> Result<Vec<u8>> {
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.
101+
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());
107+
}
108+
std::fs::read(path).with_context(|| format!("reading {}", path.display()))
109+
}
110+
111+
fn read_opt(p: &Option<PathBuf>, cap: u64) -> Result<Vec<u8>> {
95112
match p {
96-
Some(path) => std::fs::read(path).with_context(|| format!("reading {}", path.display())),
113+
Some(path) => read_capped(path, cap),
97114
None => Ok(Vec::new()),
98115
}
99116
}
@@ -150,15 +167,15 @@ fn run() -> Result<()> {
150167
for (name, path) in &args.artifacts {
151168
artifacts.push(NamedBlob {
152169
name: name.clone(),
153-
bytes: std::fs::read(path).with_context(|| format!("reading {}", path.display()))?,
170+
bytes: read_capped(path, args.max_input_bytes)?,
154171
});
155172
}
156173
let input = RawInput {
157174
tool: tool.clone(),
158175
argv: args.argv.clone(),
159176
exit_code: args.exit_code,
160-
stdout: read_opt(&args.stdout_file)?,
161-
stderr: read_opt(&args.stderr_file)?,
177+
stdout: read_opt(&args.stdout_file, args.max_input_bytes)?,
178+
stderr: read_opt(&args.stderr_file, args.max_input_bytes)?,
162179
artifacts,
163180
base_uri: args.base_uri.clone(),
164181
options: read_opt(&args.options_file)?,

‎sandboy/README.md‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,23 @@ cargo build --release # needs Linux; the crates are Linux-only
6666
`demo.sh` shows a write inside the worktree succeeding, and a write to `$HOME`,
6767
a `ptrace`, and a connect to a non-allowlisted port all being denied.
6868

69-
### The one `unsafe`
69+
### The audited `unsafe`
7070

7171
Sandboy is the syscall-boundary crate, so — unlike the analyzer core
72-
(`unsafe_code = forbid`) — it permits **one** narrowly-scoped, audited `unsafe`:
73-
the `prctl(PR_SET_NO_NEW_PRIVS)` needed before an unprivileged seccomp install.
74-
All other unsafe lives inside the `landlock`/`seccompiler` crates ("берём
75-
готовое", ADR §2). Nothing user-derived reaches the prctl.
72+
(`unsafe_code = forbid`) — it permits **two** narrowly-scoped, audited `unsafe`
73+
calls at the syscall seam: `prctl(PR_SET_NO_NEW_PRIVS)` (required before an
74+
unprivileged seccomp install) and `close_range(.., CLOSE_RANGE_CLOEXEC)` (closes
75+
the inherited-fd hole, below). All other unsafe lives inside the
76+
`landlock`/`seccompiler` crates ("берём готовое", ADR §2). Nothing user-derived
77+
reaches either call.
78+
79+
### Inherited descriptors
80+
81+
Landlock scopes *new* opens by path and seccomp filters *syscalls* — **neither
82+
revokes a descriptor that is already open**. If the launcher (gate/orchestrator)
83+
leaks an fd — an open file or a live socket without `FD_CLOEXEC` — it would pass
84+
into the wrapped command and bypass the FS/port allowlists entirely. So before
85+
`execve`, sandboy marks every fd > 2 close-on-exec; stdio (0,1,2) is kept.
7686

7787
## Wiring into 007 (the actual use)
7888

‎sandboy/src/main.rs‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ fn run() -> Result<()> {
5757
// 3. seccomp denylist: strip dangerous syscalls, allow the rest.
5858
apply_seccomp(&policy)?;
5959

60-
// 4. Hand off. execve replaces us; the two confinements persist into it.
60+
// 4. Close inherited descriptors. Landlock/seccomp do NOT revoke
61+
// already-open fds, so any descriptor the launcher leaked (an open file,
62+
// a live socket) would pass straight into the untrusted command and
63+
// bypass the FS/port allowlists. Mark every fd > 2 close-on-exec so it
64+
// vanishes at execve; stdio (0,1,2) is kept.
65+
close_inherited_fds()?;
66+
67+
// 5. Hand off. execve replaces us; the confinements persist into it.
6168
// `exec()` only returns on failure.
6269
let err = Command::new(prog).args(prog_args).exec();
6370
Err(err).with_context(|| format!("exec {prog}"))
@@ -170,10 +177,10 @@ fn apply_seccomp(policy: &Policy) -> Result<()> {
170177
Ok(())
171178
}
172179

173-
/// The one audited `unsafe`: a single prctl to set no_new_privs, without which
174-
/// an unprivileged seccomp install is rejected. Sandboy is the syscall-boundary
180+
/// Audited `unsafe` #1: a single prctl to set no_new_privs, without which an
181+
/// unprivileged seccomp install is rejected. Sandboy is the syscall-boundary
175182
/// crate, so — unlike the analyzer core (`unsafe_code = forbid`) — it permits
176-
/// narrowly-scoped, audited unsafe at exactly this seam. Nothing user-derived
183+
/// narrowly-scoped, audited unsafe at exactly these seams. Nothing user-derived
177184
/// touches this call.
178185
fn set_no_new_privs() -> Result<()> {
179186
// SAFETY: PR_SET_NO_NEW_PRIVS takes fixed constant args, has no memory
@@ -184,3 +191,19 @@ fn set_no_new_privs() -> Result<()> {
184191
}
185192
Ok(())
186193
}
194+
195+
/// Audited `unsafe` #2: mark every descriptor above stdio close-on-exec, in one
196+
/// `close_range` syscall (Linux 5.11+, below our Landlock floor of 5.13). This
197+
/// closes the inherited-fd hole around Landlock (which scopes new opens by path,
198+
/// not descriptors already handed to us).
199+
fn close_inherited_fds() -> Result<()> {
200+
// SAFETY: close_range with CLOSE_RANGE_CLOEXEC only sets FD_CLOEXEC on the
201+
// [3, u32::MAX] descriptor range; fixed flag, no memory effects. rc checked.
202+
let rc = unsafe {
203+
libc::close_range(3, libc::c_uint::MAX, libc::CLOSE_RANGE_CLOEXEC as libc::c_int)
204+
};
205+
if rc != 0 {
206+
bail!("close_range(CLOEXEC) failed: {}", std::io::Error::last_os_error());
207+
}
208+
Ok(())
209+
}

0 commit comments

Comments
 (0)