-
Notifications
You must be signed in to change notification settings - Fork 619
Add UAF regression test for Response body with transferred ArrayBuffer #6579
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
Merged
+70
−3
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,44 @@ | ||
| // Copyright (c) 2026 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| // Regression test for a use-after-free: creating a Response from an ArrayBuffer, | ||
|
erikcorry marked this conversation as resolved.
|
||
| // then transferring the buffer via structuredClone and collecting the clone, | ||
| // must not cause the Response body read to access freed memory. | ||
| // | ||
| // Without the fix, this crashes under ASAN with: | ||
| // heap-use-after-free READ of size 1024 | ||
|
|
||
| export default { | ||
| async test() { | ||
| // Fixed-size ArrayBuffer (non-resizable). The UAF is not specific to | ||
| // resizable buffers — it affects any ArrayBuffer whose backing store can | ||
| // be transferred away. | ||
| const buffer = new ArrayBuffer(1024); | ||
| new Uint8Array(buffer).fill(0x42); | ||
|
|
||
| const res = new Response(buffer); | ||
|
|
||
| // Transfer detaches the original buffer. The clone becomes the sole | ||
| // JS-visible owner of the backing store. | ||
| structuredClone(buffer, { transfer: [buffer] }); | ||
|
|
||
| // Collect the clone (which was not assigned to a variable). This frees | ||
| // the backing store memory if nothing else prevents it. | ||
| gc(); | ||
|
|
||
| // Reading the body must not touch freed memory. | ||
| const result = new Uint8Array(await res.arrayBuffer()); | ||
| if (result.length !== 1024) { | ||
| throw new Error(`Expected 1024 bytes, got ${result.length}`); | ||
| } | ||
| // Verify the data is intact (not garbage from freed memory). | ||
| for (let i = 0; i < result.length; i++) { | ||
| if (result[i] !== 0x42) { | ||
| throw new Error( | ||
| `Byte ${i}: expected 0x42, got 0x${result[i].toString(16)}` | ||
| ); | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
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,15 @@ | ||
| using Workerd = import "/workerd/workerd.capnp"; | ||
|
|
||
| const unitTests :Workerd.Config = ( | ||
| v8Flags = ["--expose-gc"], | ||
| services = [ | ||
| ( name = "response-uaf-test", | ||
| worker = ( | ||
| modules = [ | ||
| (name = "worker", esModule = embed "response-uaf-test.js") | ||
| ], | ||
| compatibilityFlags = ["nodejs_compat"], | ||
| ) | ||
| ), | ||
| ], | ||
| ); |
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.