Repository navigation
Fix use-after-frees, out-of-bounds accesses, races and leaks in the JavaScriptCore bindings #48543
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
base: main
Are you sure you want to change the base?
Changes from all commits
9f849eb
b35c1a2
9eafece
9b6abb5
2930928
e9c733a
ff70857
24e421e
73a5c3c
ed8fe62
46b61d4
004a8b2
2d5f0c2
c794a99
c924d30
de62cfc
dbe0884
17b3de7
5c56433
335eb8c
4319b8f
cd703ca
105d45b
466bfaa
c6aaec1
a8e52d8
b2bc2f9
3d585c0
66331e3
8ed56a4
32580a7
5df639b
b2d51bb
fc50aa5
c9dd893
e290317
580bdeb
8805848
06e7538
e771f04
7c9b284
1aec13e
3e17cd2
acccdfa
88f2684
33d1e00
8304f60
81c3aa4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -432,9 +432,11 @@ struct HttpContext { | |
| ((AsyncSocket<SSL> *) s)->cork(); | ||
|
|
||
| /* Mark that we are inside the parser now */ | ||
| httpContextData->flags.isParsingHttp = true; | ||
| struct us_socket_t *prevParsingSocket = httpContextData->parsingSocket; | ||
| httpContextData->parsingSocket = s; | ||
| typename HttpContextData<SSL>::Parse parse{s, httpContextData->innermostParse}; | ||
| if (parse.outer) [[unlikely]] { | ||
| parse.outer->interrupted = true; | ||
| } | ||
| httpContextData->innermostParse = &parse; | ||
|
Comment on lines
+435
to
+439
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟣 pre-existing, not blocking: pre-existing, left by this hunk: a node:http handler that destroys its own connection and then waits in a nested event loop for its 'close' still hangs, as on the base. HttpContext.h:437 sets Why this was flaggedA node:http request handler calls req.socket.destroy() while uws is still inside the parse of that socket's read, then runs the event loop (Bun.build() with an async plugin setup()) until the socket's 'close' fires, with no other request of that server read during the wait. JSNodeHTTPServerSocket::close at src/jsc/bindings/node/JSNodeHTTPServerSocket.cpp:106-112 asks closeAfterMessageIfParsing(), which at packages/bun-uws/src/HttpResponse.h:149 checks isParsing(); HttpContextData.h:121 returns true because Verification: HttpContextData.h:121 |
||
| const bool wasIdle = httpResponseData->isIdle; | ||
| httpResponseData->isIdle = false; | ||
|
|
||
|
|
@@ -598,7 +600,7 @@ struct HttpContext { | |
| } | ||
|
|
||
| /* First of all we need to check if this socket was deleted due to upgrade */ | ||
| if (httpContextData->upgradedWebSocket) { | ||
| if (httpContextData->innermostParse->upgradedWebSocket) { | ||
| /* We differ between closed and upgraded below */ | ||
| return nullptr; | ||
| } | ||
|
|
@@ -691,7 +693,7 @@ struct HttpContext { | |
| httpResponseData->inStream(static_cast<HttpResponse<SSL>*>(user), data.data(), data.length(), fin, httpResponseData->userData); | ||
|
|
||
| /* The body handler upgraded this socket (ws.handleUpgrade in req 'end'): upgrade() destroyed httpResponseData, so stop here. */ | ||
| if (httpContextData->upgradedWebSocket) { | ||
| if (httpContextData->innermostParse->upgradedWebSocket) { | ||
| return nullptr; | ||
| } | ||
|
|
||
|
|
@@ -740,8 +742,7 @@ struct HttpContext { | |
| auto httpErrorStatusCode = result.httpErrorStatusCode(); | ||
|
|
||
| /* Mark that we are no longer parsing Http */ | ||
| httpContextData->flags.isParsingHttp = false; | ||
| httpContextData->parsingSocket = prevParsingSocket; | ||
| httpContextData->innermostParse = parse.outer; | ||
| /* If we got fullptr that means the parser wants us to close the socket from error (same as calling the errorHandler) */ | ||
| if (httpErrorStatusCode) { | ||
| /* node:http compat: parse errors surface as the server's 'clientError' | ||
|
|
@@ -789,7 +790,7 @@ struct HttpContext { | |
|
|
||
| /* JavaScript destroyed the socket and its message did not complete in this read: close now, unless the read made this socket a WebSocket. */ | ||
| if constexpr (IsNodeHttp) { | ||
| if (httpContextData->upgradedWebSocket != s && !us_socket_is_closed(s) | ||
| if (parse.upgradedWebSocket != s && !us_socket_is_closed(s) | ||
| && (httpResponseData->state & HttpResponseData<SSL>::HTTP_NODE_CLOSE_AFTER_MESSAGE)) { | ||
| closeDestroyedNodeHttpSocket(s, httpResponseData); | ||
| returnedData = nullptr; | ||
|
|
@@ -854,26 +855,20 @@ struct HttpContext { | |
| } | ||
|
|
||
| /* If we upgraded, check here (differ between nullptr close and nullptr upgrade) */ | ||
| if (httpContextData->upgradedWebSocket) { | ||
| if (parse.upgradedWebSocket) { | ||
| /* This path is only for upgraded websockets */ | ||
| AsyncSocket<SSL> *asyncSocket = (AsyncSocket<SSL> *) httpContextData->upgradedWebSocket; | ||
| AsyncSocket<SSL> *asyncSocket = (AsyncSocket<SSL> *) parse.upgradedWebSocket; | ||
|
|
||
| /* Uncork here as well (note: what if we failed to uncork and we then pub/sub before we even upgraded?) */ | ||
| asyncSocket->uncork(); | ||
|
|
||
| /* Reset upgradedWebSocket before we return */ | ||
| httpContextData->upgradedWebSocket = nullptr; | ||
|
|
||
| /* Frames that a client sent without waiting for the 101 (RFC 6455 4.1) follow the | ||
| * request head in this read, and the HTTP parser stopped there. Give them to the | ||
| * WebSocket now, as the loop would have for a read of its own. If the request declared | ||
| * a body, the parser counts the whole read as consumed, so what this read holds of the | ||
| * body is not taken for frames. Nothing remembers the rest: a later read is all frames. | ||
| * Not when upgradedWebSocket names another connection (upgrade() adopts in place, | ||
| * so ours is s): the field is per context, and a microtask of this dispatch, or an | ||
| * earlier upgrade from a request body handler, can set it. */ | ||
| * body is not taken for frames. Nothing remembers the rest: a later read is all frames. */ | ||
| unsigned int consumed = result.consumedBytes(); | ||
| if (consumed < (unsigned int) length && (us_socket_t *) asyncSocket == s | ||
| if (parse.readHoldsUpgrade && consumed < (unsigned int) length && (us_socket_t *) asyncSocket == s /* upgrade() adopts in place */ | ||
| && !us_socket_is_closed(s) && !us_socket_is_shut_down(s)) { | ||
| return us_dispatch_data(s, data + consumed, (int) ((unsigned int) length - consumed)); | ||
| } | ||
|
|
||
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.
🟣 pre-existing, not blocking: Pre-existing, left by this hunk: a poll callback that ticks the loop makes the outer tick drop the rest of its ready batch, so one-shot fds such as Bun.spawn pipes or a macOS child's exit are never serviced again. The nested us_loop_run_bun_tick overwrites loop->ready_polls, num_ready_polls and current_ready_poll at epoll_kqueue.c:536-558, and the outer for loop at :258 (epoll) or :350 (kqueue) then ends. Fix: have us_internal_loop_enter_tick/leave_tick (internal.h:245-251) save and restore the batch count, index and undispatched entries as they now do for recv_buf, for epoll, kqueue and us_internal_drain_ready_polls. EPOLLONESHOT/EV_DISPATCH polls are disarmed by the kernel and have no re-arm path.
A small fix can ride a push you are already making; otherwise a short reply is enough.
Why this was flagged
The outer tick in us_loop_run_bun_tick fetches a batch into loop->ready_polls (epoll_kqueue.c:536-538) and dispatches it with the for loop at epoll_kqueue.c:258 (kqueue: :350). A callback of entry k runs the event loop again. The nested us_loop_run_bun_tick refills loop->ready_polls and num_ready_polls, sets current_ready_poll = 0 (:550) and its dispatch leaves current_ready_poll == its own num_ready_polls. Back in the outer for loop the ++ makes the bound fail, so outer entries k+1.. are never dispatched. us_internal_loop_enter_tick (internal.h:245-251) saves only recv_buf and the SSL read output, not the batch. FilePolls registered with OneShotFlag::Dispatch (io/lib.rs:1927) get EPOLLONESHOT at posix_event_loop.rs:591-595 and EV_DISPATCH on macOS at :645-651, and the EVFILT_PROC exit watch keeps EV_ONESHOT on macOS (process.rs:90): the kernel disarmed them when it reported them, Bun re-arms only on dispatch, so the pipe is never read or drained again and 'exit' never arrives. The base branch behaves the same.
Verification: A nested
us_loop_run_bun_tickoverwritesloop->ready_polls,num_ready_pollsandcurrent_ready_poll(packages/bun-usockets/src/eventing/epoll_kqueue.c:536-538, :550), so every outer entry after the re-entering one is never dispatched.us_internal_loop_enter_tick/leave_tick(internal.h:245-251) only swaprecv_buf/ssl_read_output. The base branch has the identical dispatch loop.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.
Not in this PR. The base loses the same events. Restoring the batch is not a small fix:
us_internal_loop_update_pending_ready_polls()would have to look through the saved batches too, or a poll that the nested tick closed would be dispatched after it was freed. That is worse than the lost event, so it needs its own change.Generated by Claude Code