-
Notifications
You must be signed in to change notification settings - Fork 4.9k
node:zlib: copy dictionary into owned buffer to prevent use-after-free on detach #30120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| // Regression test: node:zlib stored a raw pointer into the user-supplied | ||
| // dictionary ArrayBuffer and read it lazily from the threadpool | ||
| // (inflateSetDictionary on Z_NEED_DICT, and on reset()). Caching the JS view | ||
| // does not prevent the underlying ArrayBuffer from being detached — | ||
| // ArrayBuffer.prototype.transfer(newLength) with a different length | ||
| // synchronously frees the old backing store, leaving the native handle with | ||
| // a dangling pointer. Under ASAN this is a heap-use-after-free in | ||
| // adler32()/inflateSetDictionary() on the worker thread. | ||
| // | ||
| // The fix copies the dictionary into an owned buffer in Context.init(), | ||
| // matching Node.js (ZlibContext::dictionary_ is a std::vector). | ||
|
|
||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, isWindows } from "harness"; | ||
|
|
||
| // Malloc=1 routes JSC ArrayBuffer allocations through system malloc instead | ||
| // of bmalloc/libpas so that ASAN poisons freed ArrayBuffer backing stores. | ||
| // Without it bmalloc keeps the freed region in its own free list and ASAN | ||
| // never sees the UAF. bmalloc's SystemHeap is unimplemented on Windows | ||
| // (RELEASE_BASSERT_NOT_REACHED), so skip it there — Windows CI isn't ASAN | ||
| // anyway, and the test still verifies correctness. | ||
| // | ||
| // detect_leaks=0: Malloc=1 also exposes pre-existing small runtime leaks | ||
| // (parser/transpiler allocations normally hidden behind bmalloc) to | ||
| // LeakSanitizer, which would print them to stderr at exit. We only care | ||
| // about the heap-use-after-free here. | ||
| // symbolize=0: when this test is run against an unfixed build, ASAN aborts | ||
| // and symbolizing the debug binary takes longer than the default test | ||
| // timeout. We only care that the subprocess prints "OK" and exits 0. | ||
| // allow_user_segv_handler=1 suppresses JSC's "ASAN interferes with JSC | ||
| // signal handlers" stderr banner on ASAN builds where bunEnv didn't set it. | ||
| const asanOptions = [bunEnv.ASAN_OPTIONS, "allow_user_segv_handler=1", "symbolize=0", "detect_leaks=0"] | ||
| .filter(Boolean) | ||
| .join(":"); | ||
| const env = { ...bunEnv, ...(isWindows ? {} : { Malloc: "1" }), ASAN_OPTIONS: asanOptions }; | ||
|
|
||
| const inflateFixture = /* js */ ` | ||
| const zlib = require("zlib"); | ||
|
|
||
| const expected = Buffer.alloc(64, "a").toString(); | ||
| const ab = new ArrayBuffer(4096); | ||
| const dict = Buffer.from(ab); | ||
| dict.fill("a"); | ||
|
|
||
| // Deflate with a dictionary so the stream sets FDICT and inflate() will | ||
| // return Z_NEED_DICT, which triggers inflateSetDictionary() on the | ||
| // threadpool with the stored dictionary pointer. | ||
| const payload = zlib.deflateSync(Buffer.alloc(64, "a"), { dictionary: dict }); | ||
|
|
||
| const inf = zlib.createInflate({ dictionary: dict }); | ||
| inf.on("error", err => { | ||
| console.error("error:", err.message); | ||
| process.exitCode = 1; | ||
| }); | ||
| let out = Buffer.alloc(0); | ||
| inf.on("data", chunk => { | ||
| out = Buffer.concat([out, chunk]); | ||
| }); | ||
| inf.on("end", () => { | ||
| console.log(out.toString() === expected ? "OK" : "WRONG: " + out.toString()); | ||
| }); | ||
|
|
||
| // transfer() with a different length allocates a new backing, memcpy's, and | ||
| // synchronously frees the old backing (Gigacage::free -> system free under | ||
| // Malloc=1). The native zlib handle still holds a pointer into it. | ||
| ab.transfer(1); | ||
|
|
||
| inf.write(payload, () => inf.end()); | ||
| `; | ||
|
|
||
| const resetFixture = /* js */ ` | ||
| const zlib = require("zlib"); | ||
|
|
||
| const expected = Buffer.alloc(64, "a").toString(); | ||
| const ab = new ArrayBuffer(4096); | ||
| const dict = Buffer.from(ab); | ||
| dict.fill("a"); | ||
|
|
||
| const payload = zlib.deflateRawSync(Buffer.alloc(64, "a"), { dictionary: dict }); | ||
|
|
||
| // INFLATERAW applies the dictionary synchronously in init(), and reset() | ||
| // re-applies it via setDictionary() — both read the stored pointer. | ||
| const inf = zlib.createInflateRaw({ dictionary: dict }); | ||
| inf.on("error", err => { | ||
| console.error("error:", err.message); | ||
| process.exitCode = 1; | ||
| }); | ||
| let out = Buffer.alloc(0); | ||
| inf.on("data", chunk => { | ||
| out = Buffer.concat([out, chunk]); | ||
| }); | ||
| inf.on("end", () => { | ||
| console.log(out.toString() === expected ? "OK" : "WRONG: " + out.toString()); | ||
| }); | ||
|
|
||
| ab.transfer(1); | ||
|
|
||
| // reset() re-applies the dictionary from the stored (now stale) pointer. | ||
| inf.reset(); | ||
| inf.write(payload, () => inf.end()); | ||
| `; | ||
|
|
||
| const deflateResetFixture = /* js */ ` | ||
| const zlib = require("zlib"); | ||
|
|
||
| const expected = Buffer.alloc(64, "a").toString(); | ||
| const ab = new ArrayBuffer(4096); | ||
| const dict = Buffer.from(ab); | ||
| dict.fill("a"); | ||
| const dictCopy = Buffer.from(dict); | ||
|
|
||
| const def = zlib.createDeflate({ dictionary: dict }); | ||
| def.on("error", err => { | ||
| console.error("error:", err.message); | ||
| process.exitCode = 1; | ||
| }); | ||
| let out = Buffer.alloc(0); | ||
| def.on("data", chunk => { | ||
| out = Buffer.concat([out, chunk]); | ||
| }); | ||
| def.on("end", () => { | ||
| const result = zlib.inflateSync(out, { dictionary: dictCopy }).toString(); | ||
| console.log(result === expected ? "OK" : "WRONG: " + result); | ||
| }); | ||
|
|
||
| ab.transfer(1); | ||
|
|
||
| // reset() calls deflateReset() then deflateSetDictionary() with the stored | ||
| // pointer on the JS thread. | ||
| def.reset(); | ||
| def.end(Buffer.alloc(64, "a")); | ||
| `; | ||
|
|
||
| async function run(fixture: string) { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", fixture], | ||
| env, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| return { stdout, stderr, exitCode }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| test.concurrent( | ||
| "inflate: detaching the dictionary ArrayBuffer after createInflate does not use-after-free", | ||
| async () => { | ||
| const { stdout, stderr, exitCode } = await run(inflateFixture); | ||
| expect(stderr).toBe(""); | ||
| expect(stdout.trim()).toBe("OK"); | ||
| expect(exitCode).toBe(0); | ||
| }, | ||
| ); | ||
|
|
||
| test.concurrent("inflateRaw: reset() after detaching the dictionary ArrayBuffer does not use-after-free", async () => { | ||
| const { stdout, stderr, exitCode } = await run(resetFixture); | ||
| expect(stderr).toBe(""); | ||
| expect(stdout.trim()).toBe("OK"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test.concurrent("deflate: reset() after detaching the dictionary ArrayBuffer does not use-after-free", async () => { | ||
| const { stdout, stderr, exitCode } = await run(deflateResetFixture); | ||
| expect(stderr).toBe(""); | ||
| expect(stdout.trim()).toBe("OK"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
robobun marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.