security: replace strcpy with memcpy in legacy fallbacks; add zcalloc overflow guard#1274
Open
MH1983-web wants to merge 1 commit into
Open
security: replace strcpy with memcpy in legacy fallbacks; add zcalloc overflow guard#1274MH1983-web wants to merge 1 commit into
MH1983-web wants to merge 1 commit into
Conversation
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
Two proactive security hardening improvements to the legacy fallback
code paths, both independently confirmed as not addressed by the
1.3.2 / 7ASecurity audit fixes.
Change 1: strcpy → memcpy in legacy fallbacks (gzlib.c)
In gz_open() and gz_error(), the #else fallback blocks (active only
under NO_snprintf/NO_vsnprintf) used strcpy() to copy into buffers
whose sizes were computed immediately before the copy. Replaced with
memcpy(dst, src, strlen(src) + 1) which makes the size relationship
explicit, is not flagged by static analyzers, and produces equivalent
behavior on all platforms.
Affected lines: gz_open() line 222, gz_error() line 584.
Change 2: Integer overflow guard in zcalloc() (zutil.c)
The expression malloc(items * size) where both operands are unsigned
int is vulnerable to silent integer overflow on LLP64 platforms
(Windows x64), where unsigned int is 32-bit but size_t is 64-bit.
Added explicit overflow check before the multiplication:
This uses only integer division (no overflow risk) and returns NULL
for any combination where items * size would overflow.
Testing
New test added: test/hardening_test.c
Build: gcc -I. -o test_hardening test/hardening_test.c libz.a -lm
Notes