Repository navigation
Dedupe extracted embedded native modules in compiled binaries - #29587
Conversation
|
Updated 2:44 PM PT - Aug 18th, 2026
✅ @robobun, your commit dedad50c3147565a1f7cd5e91f980e890f9dfb82 passed in 🧪 To try this PR locally: bunx bun-pr 29587That installs a local version of the PR into your bun-29587 --bun |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughAdds a nullable per-file extracted path field and implements mutex-serialized lazy extraction into a private per-user extract directory with content-hash deduplication and atomic placement; adds a Linux regression test verifying single-copy extraction across workers and restarts; minor Win32 API call reference change. Changes
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@test/regression/issue/29585.test.ts`:
- Around line 29-84: Both countExtractedCopies and removeExtractedCopies
duplicate the byte-by-byte matching logic; extract that into a small helper
(e.g., matchesBytes or fileMatchesBytes) that takes a file path (or Bun.File)
and the target Uint8Array and returns a boolean, and then call that helper from
both countExtractedCopies and removeExtractedCopies; implement the helper to
first check size/equality and then use a single buffer comparison
(Buffer.from(...).equals(...) or an equivalent fast comparison) to replace the
manual loop and reduce duplication.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 8e7815d7-60ea-4fd9-a129-37c8a1c1de98
📒 Files selected for processing (1)
test/regression/issue/29585.test.ts
…test Addresses review feedback on #29587: - Security (CWE-377): the content-hashed tmpfile lived directly in /tmp, where any local user could pre-create `.bun-{hash}.{ext}` at the known name and plant a malicious shared library — the size-only reuse check would then hand that file to dlopen(). Extraction now goes into `{tmpdir}/bun-{uid}/`, which we create with mode 0700 (POSIX) and verify on every use (owner == uid, (mode & 0077) == 0, is-a-directory). Refuses to extract if the subdir can't be trusted. Opened with O_NOFOLLOW so a racing symlink can't redirect us. - Stale cache: the fast path returned the cached path without checking it still existed. After systemd-tmpfiles swept the extracted file, every subsequent dlopen() in that process would fail with ENOENT for the rest of its lifetime. The fast path now stat()s the cached path and, on ENOENT, clears the cache and falls through to re-extract. - Test hermeticity: the test read and rm'd files in the shared /tmp, which could race with concurrent runs or unrelated processes that happened to have a byte-identical .so there. It now points the spawned binary at a per-test `BUN_TMPDIR` so extraction stays sandboxed, and walks one level into that dir to find the `bun-{uid}/` subdir. - Test: drop the brittle `expect(cc.stderr).toBe("")` — gcc/clang/ld can emit benign notes on success. Match the existing precedent in addr32.test.ts / garbage-env.test.ts (exit code only). - Test: add a third run that deletes the extracted file mid-test to prove the cache self-heals.
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/bun.js/ModuleLoader.zig`:
- Around line 167-174: The openExtractDir() implementation in ModuleLoader.zig
uses bun.sys.open(...) which on Windows loses O.NOFOLLOW semantics; change the
subdir_fd acquisition to branch on Environment.os and call
bun.sys.openDirAtWindowsA(bun.FD.cwd(), abs_z, .{ .no_follow = true, .read_only
= true }) when Environment.os == .windows, otherwise keep the existing
bun.sys.open(...) call, and preserve the current switch that maps .result to fd
and .err to return null so the error handling for subdir_fd remains identical.
In `@test/regression/issue/29585.test.ts`:
- Around line 55-62: The test currently serializes worker startup by awaiting
each worker's ready promise inside the creation loop; change it to first create
all Worker instances (using new Worker(import.meta.url) and pushing to workers
array), attach each worker's ready resolver via Promise.withResolvers and
w.addEventListener("message", () => resolve(), { once: true }) but do NOT await
inside the loop — instead collect all the per-worker promises and after the loop
call await Promise.all(collectedPromises) so all dlopen() calls run concurrently
and the test exercises real concurrent worker extraction and atomic write+rename
convergence.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 03f648b5-ff32-4c02-bebc-44b569cf40cd
📒 Files selected for processing (3)
src/bun.js/ModuleLoader.zigsrc/sys.zigtest/regression/issue/29585.test.ts
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/bun.js/ModuleLoader.zig`:
- Around line 174-182: The mkdir call using bun.sys.mkdirA(abs_z, 0o700) is
vulnerable to the caller's umask; after creating the directory you must
explicitly enforce owner rwx (0700) rather than relying on the mode argument.
Update the code around the mkdir switch to, on POSIX success (and also when
EXIST path is accepted), call the appropriate API to set the mode to 0o700
(e.g., bun.sys.chmodA(abs_z, 0o700) or open+bun.sys.fchmod on the created
directory) and re-check permissions/ownership; apply the same change to the
other occurrence mentioned (lines ~203-214) so the directory always ends up with
owner read/write/execute regardless of umask.
- Around line 49-61: The fast-path that returns file.extracted_path must also
validate the file size like the fstatat() branch: after bun.sys.stat(cached)
succeeds, perform the same size check (compare the stat result's st_size to the
expected size stored on the File/Module struct — e.g. file.expected_size or
file.extracted_size used by the fstatat branch) and only memcpy/return path_buf
if sizes match; otherwise free(cached) and set file.extracted_path = null to
fall through to the recreate path.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 8aea0577-83a7-412c-812b-0fc79d1e274e
📒 Files selected for processing (2)
src/bun.js/ModuleLoader.zigtest/regression/issue/29585.test.ts
There was a problem hiding this comment.
All my prior feedback has been addressed and I have no further findings, but this rewrites security-sensitive tempfile/dlopen path resolution (CWE-377 threat model on shared hosts) and warrants a human reviewer's sign-off. Also note build-zig is currently red on f795e58.
Extended reasoning...
Overview
This PR rewrites resolveEmbeddedFile in src/bun.js/ModuleLoader.zig to deduplicate extracted embedded native modules from bun build --compile binaries. It introduces a per-user 0700 extract directory under tmpdir, content-hashed filenames, an in-process mutex-guarded cache on StandaloneModuleGraph.File, atomic scratch-write+rename, fast-path revalidation via lstat with uid/size/type checks, and a randomized fallback directory when the canonical path is squatted. It also adds a Linux regression test and a one-line sys.zig change routing Windows mkdirA through bun.windows.CreateDirectoryW.
Security risks
The core of this change is a CWE-377 (predictable tempfile) defense for code that hands a filesystem path directly to dlopen(). The threat model — another local user pre-creating predictable paths in shared /tmp, symlink races, sticky-bit DoS via squatting, systemd-tmpfiles aging out the extract dir, setuid euid/ruid mismatch — was iterated on across several review rounds and the current revision addresses each: per-user 0700 subdir opened with O_NOFOLLOW, ownership/mode verified via fstat, geteuid() used for ownership comparison, fast-path revalidation checks st.uid/IFREG/size via lstat, and a randomized fallback dir avoids the squatting DoS. I have no remaining findings, but the consequence of a miss here is local-attacker RCE via dlopen, so the threat-model reasoning deserves a second pair of human eyes.
Level of scrutiny
High. This is production-critical native-module loading that runs in every compiled binary using embedded .so/.node files, and it directly governs which on-disk path is passed to dlopen(). The logic is ~180 new lines with platform branches (POSIX vs Windows) and subtle ordering requirements (mkdir → open NOFOLLOW → fchmod → fstat verify).
Other factors
- All 10+ prior inline comments (from me and CodeRabbit) are marked resolved and the diff reflects the fixes.
- CI (
build-zig) is failing across all platforms on commit f795e58; the only commit since (df142df) is a doc-comment move, so the build is likely still red and needs investigation before merge. - The regression test is Linux-only and hermetic (isolated
BUN_TMPDIR), but Windows/macOS paths are untested.
The `readdirSync(tmpdir).toBeEmpty()` check was verifying that BunProcess.cpp's `deleteAfter` successfully unlinked the extracted .node. #29587 drops `deleteAfter` on POSIX because the file is now content-hashed and cached — so an empty tmpdir isn't the right assertion anymore. My replacement (uniqueness of content hashes per extracted file) was redundant with test/regression/issue/29585.test.ts, which already exercises the dedup logic end-to-end. Just drop the FS-state check here. The existing `stdout === "hello world!"` assertion is enough — it proves the compiled binary loaded its embedded addons successfully. The dedup-correctness is covered by 29585.test.ts.
There was a problem hiding this comment.
No further issues from my side — all the earlier CWE-377/tempfile concerns look addressed. Given this is security-sensitive dlopen/tempfile handling that's been through several hardening iterations, I'd like a human to give it a final look (and confirm the two x64-asan CI failures on 0ac4c97 are unrelated).
Extended reasoning...
Overview
This PR rewrites resolveEmbeddedFile in src/bun.js/ModuleLoader.zig to deduplicate extracted embedded native modules (.so/.node) from bun build --compile binaries. It introduces a per-user 0700 {tmpdir}/bun-{euid}/ subdirectory, content-hashed filenames, atomic scratch-then-rename extraction, an in-process File.extracted_path cache guarded by a global mutex, and a random-named fallback directory if the canonical one is squatted. It also drops the POSIX deleteAfter unlink in BunProcess.cpp, adds a one-line sys.zig change, updates test/napi/napi.test.ts, and adds a Linux regression test.
Security risks
This is squarely security-sensitive: the output path is handed straight to dlopen(), so any weakness in the trust check is local RCE. Across the review iterations the design moved from a predictable /tmp/.bun-{hash}.so (CWE-377) to a per-euid 0700 subdir opened with O_NOFOLLOW, fstat-verified for ISDIR && st.uid == geteuid() && (mode & 0o777) == 0o700, with fchmod after mkdir to defeat umask, a random-named fallback to avoid DoS-via-squatting, and a fast-path lstat that re-checks owner/type/size before reuse. The Windows branch uses openDirAtWindowsA(.no_follow=true) and relies on per-user %TEMP% ACLs. All the specific holes I flagged earlier are now closed in the diff, but the threat surface (multi-user shared /tmp, systemd-tmpfiles, setuid, symlink races) is subtle enough that a maintainer should sign off on the final shape.
Level of scrutiny
High. ~180 lines of new Zig in the module loader, cross-platform filesystem/permission logic, behavioural change in BunProcess.cpp (POSIX no longer unlinks the extracted .node after dlopen), and an existing test's assertion was relaxed. This is not a mechanical change.
Other factors
- All prior
claude[bot]andcoderabbitaiinline comments are marked resolved, and the bug hunter found nothing new on this revision. - CI on 0ac4c97 reports two x64-asan failures (
test-worker-nested-uncaught.jscrash,load-file-loader-a-lot.test.tscode 1). They aren't files this PR edits, but the PR does touch worker-adjacent loading; worth confirming they're pre-existing flakes. - The new worker regression test is gated to debug builds with a comment about a pre-existing release shutdown race — a maintainer should be aware that's being skipped.
- I'm not approving per the guideline against auto-approving security-sensitive permission/tempfile code.
There was a problem hiding this comment.
No further issues from my side — all prior feedback has been addressed. Deferring to a human for final sign-off since this is security-sensitive tempfile/dlopen handling (CWE-377) with cross-platform branches and a behavior change (dropping the POSIX post-dlopen unlink).
Extended reasoning...
Overview
This PR rewrites resolveEmbeddedFile in src/bun.js/ModuleLoader.zig to dedupe extraction of embedded native modules (.so/.node) from bun build --compile binaries. It introduces a per-user 0700 {tmpdir}/bun-{euid}/ subdir, content-hashed filenames, an in-process cache on StandaloneModuleGraph.File, atomic write+rename, a random-subdir fallback when the canonical name is squatted, and owner/size validation on the fast path. It also drops the POSIX deleteAfter unlink in BunProcess.cpp (since the file is now intentionally persisted), updates napi.test.ts accordingly, adds a regression test, and tweaks mkdirA on Windows.
Security risks
This is squarely CWE-377 (insecure temporary file) territory feeding into dlopen(). The PR went through several security-hardening iterations during review: per-user 0700 subdir + O_NOFOLLOW open + ownership/mode verification, geteuid() for setuid correctness, lstat+uid+size checks on the cached fast path, fchmod-after-mkdir to defeat restrictive umasks, and a random-name fallback to prevent squat DoS. The current revision looks correct to me, but the threat model (multi-user shared hosts, systemd-tmpfiles sweeping, sticky-bit /tmp) is subtle enough that a human should confirm the final state.
Level of scrutiny
High. This is production-critical native-code-loading logic with a documented attack surface, platform-specific branches (POSIX vs Windows openDirAtWindowsA), and a deliberate behavior change (extracted .node files now persist on POSIX rather than being unlinked post-dlopen). Not a candidate for bot-only approval.
Other factors
All 12+ prior inline comments (mine and CodeRabbit's) are resolved, and the bug-hunting pass on the current revision found nothing. The new regression test is Linux-only and isolates its tmpdir via BUN_TMPDIR. The test/expectations.txt addition is unrelated (an ASAN timeout marker).
…test Addresses review feedback on #29587: - Security (CWE-377): the content-hashed tmpfile lived directly in /tmp, where any local user could pre-create `.bun-{hash}.{ext}` at the known name and plant a malicious shared library — the size-only reuse check would then hand that file to dlopen(). Extraction now goes into `{tmpdir}/bun-{uid}/`, which we create with mode 0700 (POSIX) and verify on every use (owner == uid, (mode & 0077) == 0, is-a-directory). Refuses to extract if the subdir can't be trusted. Opened with O_NOFOLLOW so a racing symlink can't redirect us. - Stale cache: the fast path returned the cached path without checking it still existed. After systemd-tmpfiles swept the extracted file, every subsequent dlopen() in that process would fail with ENOENT for the rest of its lifetime. The fast path now stat()s the cached path and, on ENOENT, clears the cache and falls through to re-extract. - Test hermeticity: the test read and rm'd files in the shared /tmp, which could race with concurrent runs or unrelated processes that happened to have a byte-identical .so there. It now points the spawned binary at a per-test `BUN_TMPDIR` so extraction stays sandboxed, and walks one level into that dir to find the `bun-{uid}/` subdir. - Test: drop the brittle `expect(cc.stderr).toBe("")` — gcc/clang/ld can emit benign notes on success. Match the existing precedent in addr32.test.ts / garbage-env.test.ts (exit code only). - Test: add a third run that deletes the extracted file mid-test to prove the cache self-heals.
The `readdirSync(tmpdir).toBeEmpty()` check was verifying that BunProcess.cpp's `deleteAfter` successfully unlinked the extracted .node. #29587 drops `deleteAfter` on POSIX because the file is now content-hashed and cached — so an empty tmpdir isn't the right assertion anymore. My replacement (uniqueness of content hashes per extracted file) was redundant with test/regression/issue/29585.test.ts, which already exercises the dedup logic end-to-end. Just drop the FS-state check here. The existing `stdout === "hello world!"` assertion is enough — it proves the compiled binary loaded its embedded addons successfully. The dedup-correctness is covered by 29585.test.ts.
e24ffb9 to
1a5d561
Compare
There was a problem hiding this comment.
All prior feedback has been addressed and I didn't find anything new — deferring to a human reviewer only because this rewrites the embedded-native-module extraction path with a security model (per-user 0700 subdir, ownership/mode verification, predictable filenames feeding dlopen()) that warrants a second pair of eyes.
Extended reasoning...
Overview
This PR rewrites resolveEmbeddedFile in src/jsc/ModuleLoader.zig (~200 new lines) to deduplicate extracted embedded native modules (.so/.node) in bun build --compile binaries via a content-hashed filename inside a per-user 0700 subdirectory of tmpdir, with an in-process cache on StandaloneModuleGraph.File. It also removes the POSIX post-dlopen unlink in BunProcess.cpp, adds a one-line Windows mkdirA namespace fix in sys.zig, updates test/napi/napi.test.ts to drop the now-invalid "tmpdir is empty" assertion, and adds a new Linux regression test.
Security risks
This is the part that warrants human review. The change moves from unpredictable-per-call tempfile names to deterministic content-hashed names — a classic CWE-377 surface. Over ~15 review iterations the PR added: a per-user 0700 subdir ({tmpdir}/bun-{euid}/) opened with O_NOFOLLOW; ownership + mode verification via fstat; a random-name fallback when the canonical dir is squatted; geteuid() (not getuid()) for setuid binaries; lstat + uid + size revalidation on the fast-path cache; and lax-mode handling for CIFS/vfat/DrvFs/Docker bind-mounts. The final mode check was relaxed from strict == 0o700 to (mode & 0o022) == 0 with st.uid == uid as the load-bearing CWE-377 defence on real POSIX filesystems. I believe the current state is sound, but the threat model is subtle enough (multi-user hosts, sticky-bit /tmp, systemd-tmpfiles sweeps, lax-mode mounts) that a maintainer should sign off on the final security/availability trade-off rather than a bot.
Level of scrutiny
High. The output of this function is handed directly to dlopen(), so a wrong answer is arbitrary code execution. The PR also changes long-standing behavior (POSIX no longer unlinks the extracted .node after dlopen) and removes a test assertion in napi.test.ts.
Other factors
All 15+ inline review comments (mine and CodeRabbit's) are resolved, including the most recent commit 112bbfc which addressed the lax-mode-FS cross-restart dedup gap and the leaked-empty-dir cleanup. The new regression test exercises the main-thread, cross-restart, and tmpfiles-sweep self-heal paths on Linux; the Worker test is gated to debug builds due to a pre-existing unrelated shutdown race. CI status on the latest commit wasn't visible in the timeline yet.
Resolves BunProcess.cpp against #29587 (the extracted addon is no longer deleted after dlopen) and napi.test.ts against its new extraction-count assertions. Also restores bun_runtime's bun_css dependency edge, which #37301 removed after #39229 had started using the crate in pm_diff_normalize.rs; main does not build without it.
OpenCode 1.18.32, the latest stable release, is built with Bun 1.3.14. That runtime copies each bundled native library to a new randomly named file in the temp directory on every load and never deletes the copy (anomalyco/opencode#42700, #49283). The Bun fix (oven-sh/bun#29587) needs Bun 1.4 or later. The 1.18.x upgrade (anomalyco/opencode#44946) is still open. ODS now starts its managed OpenCode with BUN_TMPDIR set to an ODS-owned directory that is emptied on every start: - Linux systemd user unit: ExecStartPre empties it, ExecStopPost removes it - macOS LaunchAgent: a /bin/sh wrapper empties it, then execs OpenCode - Windows scheduled-task launcher: empties it before starting OpenCode TMPDIR for OpenCode's tools is unchanged. New test tests/test-opencode-bun-tmpdir.sh runs in make test and in Test Linux CI. The Hermes Agent (v2026.9.24, 0.21.5) and OpenCode (1.18.32) pins are already the latest upstream releases, so no version changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#29587 removed Bun__unlink together with its last caller. BunDebugger__willReloadProcess calls it to remove the inspector socket files before a reload.
Fixes #29585.
Problem
A
bun build --compilebinary that loads an embedded native library (bun:ffidlopenorprocess.dlopenon a.node) extracted a fresh copy to/tmpon every call and never cleaned it up. Each Worker re-extracted on its firstdlopen, so Worker-heavy servers leaked fast (the reporter measured ~14 GB/hour). The cause:resolve_embedded_file_to_bufnamed each tmpfile withnanoTimestamp()plus a per-VM counter, so no two calls ever produced the same path.Fix
Extraction is now stateless and idempotent. The on-disk file is the cache.
{tmpdir}/.bun-{euid}-{wyhash(contents):x}.{ext}. Same content gives the same path, so all dlopens, Workers, and restarts of the binary share one file. The euid keeps users on a shared/tmpapart.lstatatthe canonical name and reuse it if it is a regular file, owned by our euid, with the expected size.lstatat(notfstatat) makes a planted symlink fail theISREGcheck instead of being followed.rename(2)s it onto the canonical name. Racing Workers converge on one valid file. If the rename fails (a foreign owner squats the name on sticky/tmp), that call uses its scratch file.dlopendeletion inBunProcess.cppis removed on both platforms: deleting the shared file would defeat the dedup, and the Windows delete-on-reboot required admin rights and only ever marked the file in active use. The orphanedBun__unlinkexport is deleted with its last caller.Verification
test/regression/issue/29585.test.ts: one file after 10 dlopens, after a second process run, after deleting the file (re-extracts), and across 5 concurrent Workers. Fails pre-fix withExpected: 1 / Received: 10.test/napi/napi.test.ts(--compile): the.node/process.dlopenpath keeps a stable extracted count across two runs, on all platforms (verified on Windows Server 2019 x64).no test proof · iteration 55 · Platform-specific test(s) that do not run on this machine. Deferring to CI, which covers all platforms: test/napi/napi.test.ts test/regression/issue/29585.test.ts