Skip to content
Closed
Show file tree
Hide file tree
Changes from 10 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
17 changes: 10 additions & 7 deletions src/resolver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@
// 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: published into `DirEntry.fd` on success when `store_fd`, closed otherwise.
pub fn read_directory_with_iterator<I: DirEntryIterator>(
&mut self,
dir_maybe_trail_slash: &[u8],
Expand Down Expand Up @@ -1227,14 +1228,13 @@
},
};

// 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);
}
});

Check failure on line 1237 in src/resolver/lib.rs

View check run for this annotation

Claude / Claude Code Review

Partial-readdir failure closes fd already copied into Scanner's dirs_to_scan queue

Since e3694aba the guard closes `handle` on any `readdir` Err, but `readdir` sets `dir.fd = handle` *before* iterating and `add_entry_with_store` calls `iterator.next(entry, self.fd)` per entry — so on a partial failure (some getdents64 batches succeed, then one fails: the PR's own "EIO mid-iteration" case) `Scanner::next` has already pushed `ScanEntry { relative_dir: handle }` into `dirs_to_scan`. The guard then closes that fd, and the scan loop's `open_dir_at(entry.relative_dir, name)` runs ag
Comment thread
robobun marked this conversation as resolved.
Resolved

// if we get this far, it's a real directory, so we can just store the dir name.
// An in-place refresh always keeps the slot's existing interned name: callers
Expand All @@ -1259,7 +1259,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
184 changes: 184 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,184 @@
// 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}/unreachable.test.ts`] =
`import { test } from "bun:test"; test("unreachable-marker", () => {});`;
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+)/);
// unreachable-marker stays undiscovered iff the shim made the marker dirs
// unreadable; if bun stops routing getdents64 through libc syscall() this trips.
expect({
leaked: m?.[0],
shimFired: !stderr.includes("unreachable-marker"),
onePass: stderr.includes("1 pass"),
exitCode,
}).toEqual({
leaked: "LEAKED=0",
shimFired: true,
onePass: true,
exitCode: 0,
});
});