-
Notifications
You must be signed in to change notification settings - Fork 5k
fix(s3): double free of path when S3Client operation throws #29656
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
+127
−10
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6faef3c
fix(s3): double free of path when S3Client operation throws
robobun 834876b
test: cover exists/size/stat/unlink error paths
robobun 4f10b4c
[autofix.ci] apply automated fixes
autofix-ci[bot] 988f170
ci: retrigger after buildkite agent outage
robobun 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,110 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
|
|
||
| // A non-ASCII character forces the PathLike to be an allocated encoded_slice. | ||
| // When the operation fails after the path has been placed in the blob store, | ||
| // it must be freed exactly once; previously both the blob store and the | ||
| // caller's errdefer would free it, causing an ASAN use-after-poison. | ||
| const nonAsciiPath = "bucket/key-ü.txt"; | ||
|
|
||
| describe("S3Client error paths do not double-free the path", () => { | ||
| test("instance presign() throwing after blob creation", () => { | ||
| const client = new Bun.S3Client({ accessKeyId: "x", secretAccessKey: "y", endpoint: "http://example.com" }); | ||
| expect(() => client.presign(nonAsciiPath, { expiresIn: -1 })).toThrow(); | ||
| expect(() => | ||
| client.presign(nonAsciiPath, { | ||
| get method() { | ||
| throw new Error("boom"); | ||
| }, | ||
| }), | ||
| ).toThrow("boom"); | ||
| }); | ||
|
|
||
| test("instance presign() throwing before blob creation", () => { | ||
| const client = new Bun.S3Client(); | ||
| expect(() => | ||
| client.presign(nonAsciiPath, { | ||
| get type() { | ||
| throw new Error("boom"); | ||
| }, | ||
| }), | ||
| ).toThrow("boom"); | ||
| }); | ||
|
|
||
| test("instance file() throwing before blob creation", () => { | ||
| const client = new Bun.S3Client(); | ||
| expect(() => | ||
| client.file(nonAsciiPath, { | ||
| get type() { | ||
| throw new Error("boom"); | ||
| }, | ||
| }), | ||
| ).toThrow("boom"); | ||
| }); | ||
|
|
||
| test("instance write() with non-ASCII path and missing data", () => { | ||
| const client = new Bun.S3Client(); | ||
| expect(() => client.write(nonAsciiPath)).toThrow(); | ||
| }); | ||
|
|
||
| test.each(["exists", "size", "stat", "unlink"] as const)("instance %s() throwing before blob creation", method => { | ||
| const client = new Bun.S3Client(); | ||
| expect(() => | ||
| client[method](nonAsciiPath, { | ||
| get type() { | ||
| throw new Error("boom"); | ||
| }, | ||
| }), | ||
| ).toThrow("boom"); | ||
| }); | ||
|
|
||
| test("static presign() throwing after blob creation", () => { | ||
| expect(() => | ||
| Bun.S3Client.presign(nonAsciiPath, { | ||
| accessKeyId: "x", | ||
| secretAccessKey: "y", | ||
| endpoint: "http://example.com", | ||
| expiresIn: -1, | ||
| }), | ||
| ).toThrow(); | ||
| expect(() => | ||
| Bun.S3Client.presign(nonAsciiPath, { | ||
| accessKeyId: "x", | ||
| secretAccessKey: "y", | ||
| endpoint: "http://example.com", | ||
| get method() { | ||
| throw new Error("boom"); | ||
| }, | ||
| }), | ||
| ).toThrow("boom"); | ||
| }); | ||
|
|
||
| test("static presign() throwing before blob creation", () => { | ||
| expect(() => | ||
| Bun.S3Client.presign(nonAsciiPath, { | ||
| get type() { | ||
| throw new Error("boom"); | ||
| }, | ||
| }), | ||
| ).toThrow("boom"); | ||
| }); | ||
|
|
||
| test("static file() throwing before blob creation", () => { | ||
| expect(() => | ||
| Bun.S3Client.file(nonAsciiPath, { | ||
| get type() { | ||
| throw new Error("boom"); | ||
| }, | ||
| }), | ||
| ).toThrow("boom"); | ||
| }); | ||
|
|
||
| test.each(["exists", "size", "stat", "unlink"] as const)("static %s() throwing before blob creation", method => { | ||
| expect(() => | ||
| Bun.S3Client[method](nonAsciiPath, { | ||
| get type() { | ||
| throw new Error("boom"); | ||
| }, | ||
| }), | ||
| ).toThrow("boom"); | ||
| }); | ||
| }); | ||
|
coderabbitai[bot] 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this create a memory leak right here if it throws?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No —
constructS3FileWithS3CredentialsAndOptionsnow unconditionally takes ownership ofpath(see the other hunk inS3File.zig). Inside it:So every error path inside the constructor frees
pathexactly once, and on success the returned blob owns it (freed bydefer blob.detach()).Between
fromJSand the constructor call there is onlyargs.nextEat(), which returns?jsc.JSValueand cannot throw.Keeping the old
errdefer path.deinit()here is what caused the double free: whens3.unlink(blob.store.?, globalThis, options)(orgetPresignUrlFrominpresign) threw after the blob existed, bothdefer blob.detach()(store → path) anderrdefer path.deinit()fired on the same allocation.