Avoid retaining caller input after gzwrite - #1286
Open
soul-sol wants to merge 1 commit into
Open
Conversation
Large writes compress directly from caller-owned memory. Do not retain that external pointer after returning: a partial non-blocking write leaves the unreported tail with the caller, while a completed write leaves a one-past pointer. A later gzip operation must not access or compare either stale address.\n\nClear the external input reference on both stalled and completed direct writes, and add a deterministic non-blocking regression test.
soul-sol
force-pushed
the
fix-nonblocking-gzwrite-input-lifetime
branch
from
July 28, 2026 18:36
595bd45 to
4d03c63
Compare
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
next_inbefore returning from a completed directgzwritenext_inandavail_inbefore returning from a stalled directgzwriteRoot cause
For a large input,
gz_write()compresses directly from the caller buffer. On return it retained an address into that external allocation.When a non-blocking destination stalls,
gzwrite()returns the number of bytes consumed but leaves the unconsumed portion instrm->next_inandstrm->avail_in. The API reports those bytes as unconsumed, so the caller may release or reuse them before retrying. A latergzprintf()can then reachgz_vacate()and move the stale external input into the much smaller internal buffer.After a completed direct write,
next_insimilarly remains one past the caller allocation. A latergz_vacate()performs a relational comparison between that stale external pointer and the internal input allocation, which is undefined. Clang AddressSanitizer with invalid-pointer-pair detection reproduces this after the caller releases the completed input.The fix drops the external input reference before every return from the direct-write path. The reported byte count is unchanged, so callers can retry an unconsumed tail as documented.
This path is independent of #1281: applying that change alone still reproduces the stale caller-buffer access before its new return path is reached.
Validation
develop: deterministic ASan heap-use-after-free, reading 983040 bytes after a partial-write caller frees the unconsumed inputdevelop: deterministic ASan invalid-pointer-pair after a completed direct write and caller free-Wall -Wextra -Werrorbuild: 16/16 CTest tests pass