Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/resolver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,9 @@
// https://twitter.com/jarredsumner/status/1655464485245845506
/// Caller borrows the returned `EntriesOption`. When `FeatureFlags::ENABLE_ENTRY_CACHE`
/// is `false`, it is not safe to store this pointer past the current function call.
///
/// `maybe_handle` is owned: on success it is published into the cached
/// `DirEntry.fd` when `store_fd`; on any error before that it is closed.

Check warning on line 1173 in src/resolver/lib.rs

View check run for this annotation

Claude / Claude Code Review

Doc comment overstates the maybe_handle ownership guarantee — pre-guard early returns still leak a caller-supplied fd

The new doc comment says `maybe_handle` is owned and "on any error before that it is closed", but the scopeguard is only armed at line 1235 — three exits precede it (`get_or_put(dir)?` at 1195, the cache-hit `cached => return Ok(cached)` at 1211, and the cached-NotFound return at 1213-1219) while `maybe_handle` is still an unconsumed `Option<Fd>` with no Drop. The Scanner passes `generation=0` after disarming its `Dir` via `into_raw()`, so the cache-hit return at 1211 is reachable in a normal `b
Comment thread
robobun marked this conversation as resolved.
Resolved
Comment thread
robobun marked this conversation as resolved.
Resolved
pub fn read_directory_with_iterator<I: DirEntryIterator>(
&mut self,
dir_maybe_trail_slash: &[u8],
Expand Down Expand Up @@ -1227,12 +1230,11 @@
},
};

// Close the handle on every exit path. Use
// scopeguard so close happens even if `readdir`/`put` early-return with `?`.
let should_close_handle = !had_handle && (!store_fd || self.need_to_close_files());
let _close_guard = scopeguard::guard(handle, move |h| {
if should_close_handle {
let _ = bun_sys::close(h);
let close_even_if_published = !store_fd || self.need_to_close_files();
let handle_published = core::cell::Cell::new(false);
let _close_guard = scopeguard::guard((), |()| {
if !handle_published.get() || (!had_handle && close_even_if_published) {
let _ = bun_sys::close(handle);
}
});
Comment thread
robobun marked this conversation as resolved.
Resolved

Expand All @@ -1259,7 +1261,10 @@
});
let mut entries = match self.readdir(store_fd, prev, dir, generation, handle, iterator)
{
Ok(e) => e,
Ok(e) => {
handle_published.set(true);
e
}
Comment thread
robobun marked this conversation as resolved.
Resolved
Err(err) => {
if let Some(existing) = in_place {
// SAFETY: see above.
Expand Down
177 changes: 177 additions & 0 deletions test/js/bun/resolve/resolver-readdir-error-fd-leak.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// The resolver opens a directory and then iterates it via getdents64. When the
// open succeeds but iteration fails, and the resolver was asked to cache the
// fd (store_fd, set by bun test / --hot / --watch / install), the fd must be
// closed on that error path. The guard that closes it used to be gated on the
// "don't store" condition, so a store_fd run leaked one directory fd per
// failed read.
//
// getdents64 is reached through libc's variadic syscall(), so an LD_PRELOAD
// shim can make it fail with EIO for fds whose /proc/self/fd target contains a
// marker string. The inner process runs under --hot (store_fd=true), resolves
// one path per marker directory, then counts open fds that still point at a
// marker directory.
import { afterAll, beforeAll, expect, test } from "bun:test";
import { bunEnv, bunExe, isLinux, isMusl, tempDir } from "harness";
import { join } from "node:path";

const cc = Bun.which("cc") || Bun.which("gcc") || Bun.which("clang");
const DIR_COUNT = 16;
const MARKER = "__FAIL_GETDENTS__";

const SHIM_C = /* c */ `
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/syscall.h>

static long (*real_syscall)(long, long, long, long, long, long, long);

long syscall(long number, ...) {
va_list ap;
long a, b, c, d, e, f;
va_start(ap, number);
a = va_arg(ap, long); b = va_arg(ap, long); c = va_arg(ap, long);
d = va_arg(ap, long); e = va_arg(ap, long); f = va_arg(ap, long);
va_end(ap);
if (!real_syscall)
real_syscall = (long (*)(long, long, long, long, long, long, long))dlsym(RTLD_NEXT, "syscall");
if (number == SYS_getdents64) {
char link[64], target[4096];
snprintf(link, sizeof link, "/proc/self/fd/%ld", a);
ssize_t n = readlink(link, target, sizeof(target) - 1);
if (n > 0) {
target[n] = 0;
if (strstr(target, "${MARKER}")) {
errno = EIO;
return -1;
}
}
}
return real_syscall(number, a, b, c, d, e, f);
}
`;

const INNER = /* ts */ `
import { readdirSync, readlinkSync } from "node:fs";
const N = ${DIR_COUNT};
let failed = 0;
for (let i = 0; i < N; i++) {
try {
require.resolve("./d" + i + "${MARKER}/m.ts");
} catch {
failed++;
}
}
let leaked = 0;
for (const name of readdirSync("/proc/self/fd")) {
try {
const target = readlinkSync("/proc/self/fd/" + name);
if (target.includes("${MARKER}")) leaked++;
} catch {}
}
console.log("FAILED=" + failed + " LEAKED=" + leaked);
process.exit(0);
`;

let dir: ReturnType<typeof tempDir>;
let shimPath: string;

beforeAll(async () => {
if (!isLinux || isMusl || !cc) return;
const files: Record<string, string> = {
"shim.c": SHIM_C,
"inner.ts": INNER,
"package.json": "{}",
};
for (let i = 0; i < DIR_COUNT; i++) files[`d${i}${MARKER}/m.ts`] = `export {};\n`;
dir = tempDir("resolver-readdir-fail-leak", files);
shimPath = join(String(dir), "shim.so");
await using ccProc = Bun.spawn({
cmd: [cc, "-shared", "-fPIC", "-o", shimPath, join(String(dir), "shim.c"), "-ldl"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [ccOut, ccErr, ccExit] = await Promise.all([ccProc.stdout.text(), ccProc.stderr.text(), ccProc.exited]);
if (ccExit !== 0) throw new Error(`shim compile failed: ${ccErr || ccOut}`);
});

afterAll(() => {
dir?.[Symbol.dispose]();
});

// LD_PRELOAD interposition requires a dynamic libc; the musl build links libc
// statically, so the shim cannot intercept syscall() there.
const skip = !isLinux || isMusl || !cc;

function shimEnv() {
const existing = bunEnv.LD_PRELOAD;
return { ...bunEnv, LD_PRELOAD: existing ? `${shimPath}:${existing}` : shimPath };
}

test.skipIf(skip)(
"resolver closes the directory fd when readdir fails after a successful open (store_fd, fresh handle)",
async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), "--hot", "inner.ts"],
cwd: String(dir),
env: shimEnv(),
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
// FAILED=DIR_COUNT proves the shim actually fired; if bun stops routing
// getdents64 through libc's syscall() the resolves succeed and this trips.
expect({ stdout: stdout.trim(), stderr, exitCode }).toEqual({
stdout: `FAILED=${DIR_COUNT} LEAKED=0`,
stderr: "",
exitCode: 0,
});
},
);

// The `bun test` scanner opens each subdirectory itself and hands the fd to
// read_directory_with_iterator(Some(fd)), which is the had_handle=true arm of
// the same guard. The scanner's Dir guard is disarmed via into_raw(), so the
// function owns the fd on that path too.
test.skipIf(skip)(
"resolver closes a caller-supplied directory fd when readdir fails (bun test scanner)",
async () => {
const files: Record<string, string> = {
"package.json": "{}",
"count.test.ts": /* ts */ `
import { test } from "bun:test";
import { readdirSync, readlinkSync } from "node:fs";
test("count", () => {
let leaked = 0;
for (const name of readdirSync("/proc/self/fd")) {
try {
if (readlinkSync("/proc/self/fd/" + name).includes("${MARKER}")) leaked++;
} catch {}
}
console.log("LEAKED=" + leaked);
});
`,
};
for (let i = 0; i < DIR_COUNT; i++) files[`d${i}${MARKER}/noop.txt`] = "";
using scanDir = tempDir("resolver-readdir-scanner-leak", files);
await using proc = Bun.spawn({
cmd: [bunExe(), "test", "."],
cwd: String(scanDir),
env: shimEnv(),
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
const m = stdout.match(/LEAKED=(\d+)/);
expect({ leaked: m?.[0], stderr: stderr.includes("1 pass"), exitCode }).toEqual({
leaked: "LEAKED=0",
stderr: true,
exitCode: 0,
});

Check warning on line 175 in test/js/bun/resolve/resolver-readdir-error-fd-leak.test.ts

View check run for this annotation

Claude / Claude Code Review

Second test's assertion neither proves the shim fired nor surfaces output on failure

Unlike the first test — which asserts `FAILED=${DIR_COUNT}` per its own comment to prove the shim fired — this test only checks `LEAKED=0` / `stderr.includes("1 pass")` / `exitCode: 0`, all of which are also satisfied by a run where the shim never intercepted getdents64 (or the Scanner stopped passing `Some(handle)`), so it can go vacuous silently. Collapsing stderr to a boolean and stdout to a regex capture also means a failure diff shows `stderr: false` / `leaked: undefined` instead of what `b
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
);
Loading