bugfix: prevent SIGSEGV in receiveuntil __gc on aborted multipart upload#2503
Closed
climagabriel wants to merge 1 commit into
Closed
bugfix: prevent SIGSEGV in receiveuntil __gc on aborted multipart upload#2503climagabriel wants to merge 1 commit into
climagabriel wants to merge 1 commit into
Conversation
6335e76 to
1245fa0
Compare
read_error_retval_handler calls finalize_read_part directly when the receiveuntil iterator's recv errors. That clears u->buf_in but leaves cp->upstream live with cp->state > 0. Later GC fires cleanup_compiled_pattern -> read_prepare, which derefs the now-NULL u->buf_in. Mirror tcp_finalize's cp->upstream = NULL detach so __gc's existing `if (u != NULL)` guard short-circuits.
1245fa0 to
ebf9719
Compare
Contributor
Author
|
will re-open after coverage and mutation testing of the reproducer pytest script |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
A client that POSTs a multipart body and aborts the connection mid-pattern against
ngx.req.socket():receiveuntil(boundary)can SIGSEGV the worker from the LuaJIT GC's__gcfinalizer on the compiled-pattern userdata.ngx_http_lua_socket_read_error_retval_handlercallsngx_http_lua_socket_tcp_finalize_read_partdirectly when the iterator's recv returns an error (e.g. client RST). That clearsu->buf_inbut leavescp->upstreampointing atuwithcp->state > 0. When LuaJIT GC later sweepscp,ngx_http_lua_socket_cleanup_compiled_patterncallsngx_http_lua_socket_tcp_read_prepare, thecp->state > 0path derefsu->buf_in— NULL deref.Fix
In
ngx_http_lua_socket_tcp_finalize_read_part, clearcp->upstream— mirroring the same detach thatngx_http_lua_socket_tcp_finalizealready performs. The__gchandler then short-circuits at its existingif (u != NULL)guard.Reproducer
POST a multipart body that ends mid-boundary, then close the socket with
SO_LINGER {1, 0}so the server reads RST while the DFA is mid-match.Six leading dashes against the 12-dash boundary leader land
cp->stateat 6 with no DFA fallback; the synchronouscollectgarbagerunscp's__gcinside the request. 100% crash rate.Crash signature
Observed in the wild across multiple PoPs against SWFUpload-style boundary scanners hitting WordPress upload endpoints behind a Lua front layer.
Test plan
upstream/master(41ed26b6).--count=100, 8 aborts per iteration).t/test suite.