Fix use-after-free on re-entrant ftp_close() during a transfer#118
Closed
iliaal wants to merge 1 commit into
Closed
Fix use-after-free on re-entrant ftp_close() during a transfer#118iliaal wants to merge 1 commit into
iliaal wants to merge 1 commit into
Conversation
A user stream wrapper passed to ftp_get(), ftp_put(), ftp_append() or the ftp_nb_* variants can call ftp_close() from its stream_read or stream_write handler while the engine still holds the freed ftpbuf_t and databuf_t on the C stack, so the transfer resumes on freed memory. Guard each engine transfer with an in_use flag and make ftp_close() throw while it is set. Closes phpGH-22400
7102ae6 to
b758b53
Compare
Owner
Author
|
Superseded by the upstream PR php#22400. |
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.
ftp_get(),ftp_put(),ftp_append()and theftp_nb_*variants cache theftpbuf_ton the C stack and stream the received or sent bytes through a caller-suppliedphp_stream. When that stream is a user-registered wrapper, itsstream_read/stream_writehandler can callftp_close()on the same connection mid-transfer, freeing the ftpbuf_t anddatabuf_twhile the engine still holds them, so the transfer resumes on freed memory. Valgrind confirms invalid reads in the ftp_get() receive loop, freed by the re-entrant ftp_close(). The fix sets anin_useflag around each engine transfer (includingftp_nb_continue_read/ftp_nb_continue_write, which the non-blocking entry points run through) and makes ftp_close() throw while it is set. Smallest trigger: a wrapper whose stream_write callsftp_close($conn)duringftp_get($conn, 'wrapper://x', ...). Reported privately as GHSA-vr2x-x6ff-4586 and rejected per the project security policy (the trigger is PHP code, not the FTP server), so filing as a normal bug.