Skip to content

minizip: null-terminate the filename buffer when the stored name is truncated - #1306

Open
afonsojanu wants to merge 1 commit into
madler:developfrom
afonsojanu:fix/minizip-unzGetCurrentFileInfo-truncation-null-term
Open

minizip: null-terminate the filename buffer when the stored name is truncated#1306
afonsojanu wants to merge 1 commit into
madler:developfrom
afonsojanu:fix/minizip-unzGetCurrentFileInfo-truncation-null-term

Conversation

@afonsojanu

Copy link
Copy Markdown

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 exactly fileNameBufferSize bytes 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 does strlen() 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).

… 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants