From c48596eef98dd21ce785599071993b76c131efc1 Mon Sep 17 00:00:00 2001 From: robobun Date: Fri, 8 May 2026 23:06:10 +0000 Subject: [PATCH] Fix double-free in S3 static methods when path is passed as string constructS3FileInternalStore passes the PathLike by value to Store.initS3, which calls toThreadSafe() on a local copy. For the encoded_slice variant, toThreadSafe is a no-op, so both the store's copy and the caller's original share the same underlying allocated memory. If a subsequent operation fails, defer blob.deinit() frees the store (and the path data), then the errdefer also tries to free the same path via path_or_blob.path.deinit(), causing a use-after-poison. Fix by setting path_or_blob to the blob variant after constructS3FileInternalStore succeeds, so the errdefer check (path_or_blob == .path) becomes false and the path is only freed once through blob.deinit(). --- src/runtime/webcore/S3File.zig | 6 +++ test/js/bun/s3/s3-presign-double-free.test.ts | 53 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 test/js/bun/s3/s3-presign-double-free.test.ts diff --git a/src/runtime/webcore/S3File.zig b/src/runtime/webcore/S3File.zig index 41e900fb987f..4cb02c158ef2 100644 --- a/src/runtime/webcore/S3File.zig +++ b/src/runtime/webcore/S3File.zig @@ -84,6 +84,7 @@ pub fn presign(globalThis: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.J } const options = args.nextEat(); var blob = try constructS3FileInternalStore(globalThis, path.path, options); + path_or_blob = .{ .blob = blob }; // path ownership transferred to store defer blob.deinit(); return try getPresignUrlFrom(&blob, globalThis, options); }, @@ -114,6 +115,7 @@ pub fn unlink(globalThis: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JS } const options = args.nextEat(); var blob = try constructS3FileInternalStore(globalThis, path.path, options); + path_or_blob = .{ .blob = blob }; // path ownership transferred to store defer blob.deinit(); return try blob.store.?.data.s3.unlink(blob.store.?, globalThis, options); }, @@ -151,6 +153,7 @@ pub fn write(globalThis: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JSE return globalThis.throwInvalidArguments("Expected a S3 or path to upload", .{}); } var blob = try constructS3FileInternalStore(globalThis, path.path, options); + path_or_blob = .{ .blob = blob }; // path ownership transferred to store defer blob.deinit(); var blob_internal: PathOrBlob = .{ .blob = blob }; @@ -190,6 +193,7 @@ pub fn size(globalThis: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JSEr return globalThis.throwInvalidArguments("Expected a S3 or path to get size", .{}); } var blob = try constructS3FileInternalStore(globalThis, path.path, options); + path_or_blob = .{ .blob = blob }; // path ownership transferred to store defer blob.deinit(); return S3BlobStatTask.size(globalThis, &blob); @@ -223,6 +227,7 @@ pub fn exists(globalThis: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JS return globalThis.throwInvalidArguments("Expected a S3 or path to check if it exists", .{}); } var blob = try constructS3FileInternalStore(globalThis, path.path, options); + path_or_blob = .{ .blob = blob }; // path ownership transferred to store defer blob.deinit(); return S3BlobStatTask.exists(globalThis, &blob); @@ -574,6 +579,7 @@ pub fn stat(globalThis: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JSEr return globalThis.throwInvalidArguments("Expected a S3 or path to get size", .{}); } var blob = try constructS3FileInternalStore(globalThis, path.path, options); + path_or_blob = .{ .blob = blob }; // path ownership transferred to store defer blob.deinit(); return S3BlobStatTask.stat(globalThis, &blob); diff --git a/test/js/bun/s3/s3-presign-double-free.test.ts b/test/js/bun/s3/s3-presign-double-free.test.ts new file mode 100644 index 000000000000..4af531e7c862 --- /dev/null +++ b/test/js/bun/s3/s3-presign-double-free.test.ts @@ -0,0 +1,53 @@ +import { expect, test } from "bun:test"; +import { bunEnv, bunExe } from "harness"; + +test("S3Client static methods should not crash with string path arguments", async () => { + await using proc = Bun.spawn({ + cmd: [ + bunExe(), + "-e", + ` + async function main() { + const calls = [ + () => Bun.S3Client.presign(Date()), + () => Bun.S3Client.presign(Date(), Date()), + () => Bun.S3Client.unlink(Date()), + () => Bun.S3Client.size(Date()), + () => Bun.S3Client.exists(Date()), + () => Bun.S3Client.stat(Date()), + () => Bun.S3Client.write(Date(), "data"), + ]; + for (const call of calls) { + try { await call(); } catch(e) {} + } + Bun.gc(true); + console.log("OK"); + } + main(); + `, + ], + env: { + ...bunEnv, + // Strip any ambient S3/AWS credentials so presign fails synchronously + // and exercises the errdefer cleanup path. + AWS_ACCESS_KEY_ID: undefined, + AWS_SECRET_ACCESS_KEY: undefined, + AWS_SESSION_TOKEN: undefined, + AWS_REGION: undefined, + AWS_BUCKET: undefined, + AWS_ENDPOINT: undefined, + S3_ACCESS_KEY_ID: undefined, + S3_SECRET_ACCESS_KEY: undefined, + S3_SESSION_TOKEN: undefined, + S3_REGION: undefined, + S3_BUCKET: undefined, + S3_ENDPOINT: undefined, + }, + stderr: "pipe", + }); + + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + + expect(stdout).toBe("OK\n"); + expect(exitCode).toBe(0); +});