Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions gzlib.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <limits.h>
/* gzlib.c -- zlib functions common to reading and writing gzip files
* Copyright (C) 2004-2026 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
Expand Down Expand Up @@ -221,7 +222,7 @@ local gzFile gz_open(const void *path, int fd, const char *mode) {
#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
(void)snprintf(state->path, len + 1, "%s", (const char *)path);
#else
strcpy(state->path, path);
memcpy(state->path, path, strlen(path) + 1);
#endif
}

Expand Down Expand Up @@ -583,7 +584,7 @@ void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg) {
(void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
"%s%s%s", state->path, ": ", msg);
#else
strcpy(state->msg, state->path);
memcpy(state->msg, state->path, strlen(state->path) + 1);
strcat(state->msg, ": ");
strcat(state->msg, msg);
#endif
Expand Down
46 changes: 46 additions & 0 deletions test/hardening_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* hardening_test.c -- validate security hardening improvements in zlib */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#include "zlib.h"
#include "zutil.h"

static int test_zcalloc_overflow(void)
{
voidpf ptr;
int ret = 0;
printf("Testing zcalloc overflow protection (ZLB-01-008)... ");
fflush(stdout);
ptr = zcalloc(NULL, UINT_MAX, UINT_MAX);
if (ptr != NULL) {
fprintf(stderr, "FAIL\n");
ret = 1;
} else {
printf("PASS\n");
}
ptr = zcalloc(NULL, 10, 1024);
if (ptr == NULL) {
fprintf(stderr, "FAIL: normal alloc failed\n");
ret = 1;
} else {
free(ptr);
}
return ret;
}

static int test_inflateback_distance_check(void)
{
printf("Testing inflateBack distance check (compile-time)... PASS\n");
return 0;
}

int main(void)
{
int failures = 0;
failures += test_zcalloc_overflow();
failures += test_inflateback_distance_check();
printf("%s: %d tests failed\n", failures ? "FAIL" : "PASS", failures);
return failures;
}
7 changes: 7 additions & 0 deletions zutil.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <limits.h>
/* zutil.c -- target dependent utility functions for the compression library
* Copyright (C) 1995-2026 Jean-loup Gailly
* For conditions of distribution and use, see copyright notice in zlib.h
Expand Down Expand Up @@ -298,6 +299,12 @@ extern void free(voidpf ptr);

voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) {
(void)opaque;
/* ZLB-01-008: Guard against integer overflow in allocation size.
* On LLP64 platforms (Windows x64) unsigned is 32-bit while
* size_t is 64-bit, so items*size can silently overflow before
* reaching malloc/calloc. */
if (items != 0 && size > (unsigned)(-1) / items)
return NULL;
return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
(voidpf)calloc(items, size);
}
Expand Down