minizip: null-terminate the filename buffer when the stored name is truncated - #1306
Open
afonsojanu wants to merge 1 commit into
Open
Conversation
… no room for a null When the stored filename is at least as long as the caller's buffer, the code copies fileNameBufferSize bytes and never null-terminates, so any later strlen() on that buffer reads past its end. This is easy to hit in practice: a lot of callers just pass a fixed-size stack buffer sized for the common case and rely on the result being a normal C string. Now that branch backs off by one byte and terminates there instead, same as the branch right above it already does when the name fits. Verified with an ASan build: a 264-byte name into a 256-byte buffer crashes with a stack-buffer-overflow in strlen() before this change and comes back clean afterward, strlen() reporting 255.
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.
There's a stack buffer over-read reachable through
unzGetCurrentFileInfo()/unzGetCurrentFileInfo64()in minizip. When a ZIP entry's stored filename is at least as long as the buffer the caller passed in,unz64local_GetCurrentFileInfoInternal()copies exactlyfileNameBufferSizebytes into it and never writes a terminator, so the buffer comes back completely full with no null anywhere inside it. Whatever the caller does with that buffer next (strlen, string comparisons, logging it,unzLocateFile, ...) walks off the end.The fix mirrors what the branch right above it already does when the name does fit: back off by one byte and terminate there. That means a name that's too long for the buffer gets truncated by one extra character compared to before, but it comes back as an actual C string instead of an unterminated 256-byte blob.
I built this with an ASan program that zips up a file with a 264-byte name, reads it back through
unzGetCurrentFileInfo()into a 256-byte stack buffer, and doesstrlen()on the result. Before the fix that's an immediate stack-buffer-overflow abort; after it,strlen()comes back with 255 and nothing complains. Didn't add this as a repo test since minizip doesn't have a unit test harness wired into CMake/CI for this kind of thing, but happy to add one in whatever shape you'd prefer if that's useful.This affects anything that lets an attacker control ZIP entry names and feeds them to code using minizip with a fixed-size buffer, which is a fairly common pattern (archive tools, upload handlers, etc).