#415·claurst

Security Vulnerability: Read/Edit workspace fence TOCTOU — symlink swapped in after canonicalize() lets a workspace file read any file on the host (CWE-367, CWE-59)

Author: qc9cCreated Sep 26, 2026Updated Sep 26, 2026

Summary

The Read tool is the one file operation claurst allows silently in the default mode: a read whose path resolves inside the workspace roots is auto-allowed with no prompt, and a read whose path resolves outside is turned into an interactive "Ask". That in/out decision is the only thing standing between the model and any file on the host.

The permission check and the actual read do not look at the same object. The permission fence resolves the path once with std::fs::canonicalize, decides Allow/Ask from the resolved location, then the executor throws the resolved path away and re-opens the original path string with tokio::fs::read_to_string. The two calls are separate syscalls with no shared file descriptor and no O_NOFOLLOW. If a workspace-internal file is a benign regular file when the fence resolves it, and becomes a symlink pointing outside the workspace before read_to_string runs, the fence returns Allow and the read follows the symlink out to whatever it now points at. The model receives the contents of a host file it was never permitted to read, with no prompt.

This is a time-of-check/time-of-use bug (CWE-367) combined with symlink following (CWE-59). It defeats the workspace containment fence itself, so it also affects Edit, Glob and Grep, which reuse the same fence.

Affected Component

File Lines Function
src-rust/crates/core/src/lib.rs 2220-2239 is_path_within_allowed_roots (the fence)
src-rust/crates/core/src/lib.rs 2296-2385 PermissionManager::evaluate (Allow/Ask decision)
src-rust/crates/tools/src/lib.rs 411-422 ToolContext::path_is_within_workspace (twin fence)
src-rust/crates/tools/src/file_read.rs 58-124 (read at 113) FileReadTool::execute
src-rust/crates/tools/src/file_edit.rs 63-137 (read at 92, write at 135) FileEditTool::execute

Verified on pinned commit 3d3f2dcf53432632fe68f30a0ffc04f1f2094861. Still present on upstream HEAD b0637c97ec34144387cbf2f74f65df6d16a6cef1 (2026-09-02): fence byte-identical at core/src/lib.rs:2727 / tools/src/lib.rs:451; file_read.rs:116 still calls tokio::fs::read_to_string(&path) after the check; file_edit.rs:95 read + :144 write_atomic(&path, ...). A repo-wide grep for O_NOFOLLOW, openat, openat2, O_PATH, from_raw_fd returns zero hits — no sink in the codebase binds the check to the use.

Root cause

rust
// src-rust/crates/core/src/lib.rs:2220-2239
fn is_path_within_allowed_roots(
    path: &str,
    working_dir: Option<&std::path::Path>,
    allowed_roots: &[std::path::PathBuf],
) -> bool {
    let canonical_path = std::fs::canonicalize(path)
        .unwrap_or_else(|_| std::path::PathBuf::from(path));
    // ... build canonicalized roots ...
    roots.iter().any(|root| canonical_path.starts_with(root))   // line 2238
}

canonicalize resolves every .. and symlink and returns the real location, used only for the starts_with comparison. It is never kept. The twin ToolContext::path_is_within_workspace (tools/src/lib.rs:411-422) is the same shape.

The decision this feeds, in PermissionManager::evaluate (core/src/lib.rs:2296-2385):

rust
let read_in_workspace = path.is_some_and(|target| {                 // 2351
    is_path_within_allowed_roots(target, working_dir, allowed_roots)
});
let should_ask_read = match tool_name {                             // 2354
    "ListMcpResources" | "ReadMcpResource" => true,
    _ if matches!(level, PermissionLevel::Read) && path.is_some() => !read_in_workspace,
    _ => false,
};
match level {
    PermissionLevel::Read if !should_ask_read => PermissionDecision::Allow,   // 2375
    PermissionLevel::Read | ... => PermissionDecision::Ask { reason },        // 2382
}

Then the executor, FileReadTool::execute (tools/src/file_read.rs:58-124):

rust
let path = ctx.resolve_path(&params.file_path);            // 64
if let Err(e) = ctx.check_permission_for_path(              // 68  <- runs canonicalize()
        self.name(), &format!("Read {}", path.display()), path.clone(), true) {
    return ToolResult::error(e.to_string());
}
if !path.exists() { ... }                                   // 78  <- re-opens raw name
if path.is_dir() { ... }                                    // 83  <- re-opens raw name
let content = match tokio::fs::read_to_string(&path).await  // 113 <- re-opens raw name, FOLLOWS symlink

Line 68 resolves and judges the object; lines 78, 83 and 113 each re-open the same path string by name. read_to_string follows whatever the name resolves to at that instant, and there are several intervening syscalls between the check and the read, so the race window is wide.

Attack scenario

Prerequisites

  • The agent is working in a cloned/untrusted workspace (default posture: host execution, OS sandbox off, in-workspace reads auto-allowed).
  • A concurrent local mutator exists in the same session (a background process the agent is induced to leave running, or any co-process on the host — this is the standard local-race precondition).

Steps

  1. victim.txt sits inside the workspace as an ordinary regular file.
  2. The model issues Read victim.txt. check_permission_for_path canonicalizes <workspace>/victim.txt, sees a regular file inside the roots, returns Allow — no prompt.
  3. Before read_to_string runs (during the exists/is_dir/extension calls in between), the mutator replaces victim.txt with ln -s /etc/ssh/ssh_host_rsa_key victim.txt (or ~/.aws/credentials, ~/.ssh/id_rsa, /etc/shadow when root).
  4. read_to_string follows the symlink and returns the host file's contents as the tool result, straight into the model context.

A direct Read /etc/ssh/ssh_host_rsa_key would resolve outside the workspace and trigger an Ask; the race converts that Ask into a silent Allow.

Impact

An out-of-workspace read the product is designed to gate behind an interactive prompt becomes a silent, unattended read of an arbitrary host file — SSH host keys, ~/.ssh/id_rsa, ~/.aws/credentials, .env files, /etc/shadow when root — delivered into the model context, from where it's trivially exfiltrated (echoed into agent output, written to a workspace file, or sent by any allowed network tool).

Because the fence is reused:

  • FileEditTool::execute (file_edit.rs:63-137) does the same check then read_to_string (line 92) / write (line 135) on the raw name: a read-modify-write through a swapped symlink can both read outside-secret content and write outside the fence.
  • Glob/Grep call path_is_within_workspace per result while walking with follow_links(true), widening the race surface for enumeration.

CVSS 3.1: AV:L/AC:H/PR:N/UI:R/S:C/C:H/I:N/A:N — 5.5 (Medium). CWE-367 (primary), CWE-59.

I found no CVE, GHSA, OSV entry, or existing issue describing this check/use handle mismatch — this is my assessment, not a vendor confirmation.

Recommended Fix

The check and the use need to act on one kernel handle: open the file once with O_NOFOLLOW (refusing a symlinked final component), verify containment against the opened descriptor's real path (via /proc/self/fd/<fd> on Linux) rather than re-resolving the mutable name, then read/write from that same descriptor.

rust
// crates/tools/src/lib.rs — new helper
#[cfg(unix)]
pub async fn open_read_within_workspace(
    &self,
    path: &std::path::Path,
) -> std::io::Result<tokio::fs::File> {
    use std::os::unix::fs::OpenOptionsExt;
    use std::os::unix::io::AsRawFd;
    let file = tokio::fs::OpenOptions::new()
        .read(true)
        .custom_flags(nix::libc::O_NOFOLLOW)
        .open(path)
        .await?;
    let real = std::fs::read_link(format!("/proc/self/fd/{}", file.as_raw_fd()))
        .unwrap_or_else(|_| path.to_path_buf());
    if !self.path_is_within_workspace(&real) {
        return Err(std::io::Error::new(
            std::io::ErrorKind::PermissionDenied,
            "resolved file escapes the workspace",
        ));
    }
    Ok(file)
}

FileReadTool::execute then reads through ctx.open_read_within_workspace(&path) instead of tokio::fs::read_to_string(&path), and FileEditTool::execute uses the read variant plus a matching open_write_within_workspace (same shape, .write(true).truncate(true)) for its write-back. I have a full patch (all three call sites + the two new helpers) ready to share — happy to open a PR against this issue if useful, similar to how #199 was handled for #123.

Disclosure

Reported directly as a public issue following this repo's own established practice for security reports (see #123 → #199). No CVE has been requested yet; open to coordinating one if you'd like.

I also have a one-command, isolated PoC (a racer that wins the check/use window + a Docker harness) that I verified triggers on the unpatched build and does not trigger after the patch above — happy to send it over privately if that's useful for verifying the fix.