Fix dangling pointer in mz_zip_set_comment() to prevent double-free/UAF (#1031)
Some checks failed
Build / Ubuntu Clang No Bzip2 (push) Failing after 5s
Build / Ubuntu Clang Compress Only (push) Failing after 4s
Build / Ubuntu Clang Decompress Only (push) Failing after 3s
Build / Ubuntu Clang ICU (push) Failing after 2s
Build / Ubuntu Clang No LZMA (push) Failing after 2s
Build / Ubuntu Clang OpenSSL (push) Failing after 2s
Build / Ubuntu Clang No Encryption (push) Failing after 2s
Build / Ubuntu Clang No Pkcrypt (push) Failing after 2s
Build / Ubuntu Clang No Ppmd (push) Failing after 2s
Build / Ubuntu Clang No Winzip AES (push) Failing after 2s
Build / Ubuntu Clang No Zlib (push) Failing after 2s
Build / Ubuntu Clang No Zstd (push) Failing after 2s
Build / Ubuntu Clang (push) Failing after 2s
Build / Ubuntu GCC (push) Failing after 2s
Build / Ubuntu GCC ASAN (push) Failing after 2s
Build / Ubuntu GCC UBSAN (push) Failing after 2s
Build / Ubuntu GCC OSB (push) Failing after 2s
Build / Ubuntu Clang MSAN (push) Failing after 2s
CodeQL / Analyze (push) Failing after 2s
Build / Windows GCC Code Coverage (push) Has been cancelled
Build / macOS Xcode LibCompression (push) Has been cancelled
Build / macOS Xcode OpenSSL (push) Has been cancelled
Build / macOS Xcode Code Coverage (push) Has been cancelled
Build / Windows MSVC (push) Has been cancelled
Build / Ubuntu 22 Clang (push) Has been cancelled
Build / Ubuntu 22 Clang 11 (push) Has been cancelled
Build / macOS Xcode (push) Has been cancelled
Build / Upload Coverage Reports (push) Has been cancelled

zip->comment was freed before validating the new comment length.
When the new comment exceeds UINT16_MAX, the function returned early
without resetting zip->comment to NULL, leaving a dangling pointer
that mz_zip_close() would later free again (double-free / UAF).
This commit is contained in:
krleejihyeong 2026-08-23 12:53:20 +09:00 committed by GitHub
parent 13fee4ade0
commit 7d2917b47f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1571,17 +1571,19 @@ int32_t mz_zip_get_comment(void *handle, const char **comment) {
int32_t mz_zip_set_comment(void *handle, const char *comment) { int32_t mz_zip_set_comment(void *handle, const char *comment) {
mz_zip *zip = (mz_zip *)handle; mz_zip *zip = (mz_zip *)handle;
int32_t comment_size = 0; size_t comment_size = 0;
char *new_comment = NULL;
if (!zip || !comment) if (!zip || !comment)
return MZ_PARAM_ERROR; return MZ_PARAM_ERROR;
free(zip->comment); comment_size = strlen(comment);
comment_size = (int32_t)strlen(comment);
if (comment_size > UINT16_MAX) if (comment_size > UINT16_MAX)
return MZ_PARAM_ERROR; return MZ_PARAM_ERROR;
zip->comment = (char *)calloc(comment_size + 1, sizeof(char)); new_comment = (char *)calloc(comment_size + 1, sizeof(char));
if (!zip->comment) if (!new_comment)
return MZ_MEM_ERROR; return MZ_MEM_ERROR;
strncpy(zip->comment, comment, comment_size); strncpy(new_comment, comment, comment_size);
free(zip->comment);
zip->comment = new_comment;
return MZ_OK; return MZ_OK;
} }