libmaxminddb/backport-CVE-2020-28241.patch
2021-07-24 09:59:19 +08:00

135 lines
5.4 KiB
Diff

From eac45e29196bcde1d123a6035c15d30356bed248 Mon Sep 17 00:00:00 2001
From: Gregory Oschwald <goschwald@maxmind.com>
Date: Wed, 5 Aug 2020 14:16:17 -0700
Subject: [PATCH] Replace most malloc uses with calloc
Closes #236.
---
bin/mmdblookup.c | 2 +-
doc/libmaxminddb.md | 2 +-
src/maxminddb.c | 19 +++++++++++--------
3 files changed, 13 insertions(+), 10 deletions(-)
diff --git a/bin/mmdblookup.c b/bin/mmdblookup.c
index 26267c4..7f78773 100644
--- a/bin/mmdblookup.c
+++ b/bin/mmdblookup.c
@@ -184,7 +184,7 @@ LOCAL const char **get_options(int argc, char **argv, char **mmdb_file,
}
const char **lookup_path =
- malloc(sizeof(const char *) * ((argc - optind) + 1));
+ calloc((argc - optind) + 1, sizeof(const char *));
int i;
for (i = 0; i < argc - optind; i++) {
lookup_path[i] = argv[i + optind];
diff --git a/doc/libmaxminddb.md b/doc/libmaxminddb.md
index ebeb7c5..a8385e0 100644
--- a/doc/libmaxminddb.md
+++ b/doc/libmaxminddb.md
@@ -302,7 +302,7 @@ libmaxminddb code.
The `utf8_string`, `bytes`, and (maybe) the `uint128` members of this structure
are all pointers directly into the database's data section. This can either be
-a `malloc`'d or `mmap`'d block of memory. In either case, these pointers will
+a `calloc`'d or `mmap`'d block of memory. In either case, these pointers will
become invalid after `MMDB_close()` is called.
If you need to refer to this data after that time you should copy the data
diff --git a/src/maxminddb.c b/src/maxminddb.c
index 1cf2cbc..f8bbecb 100644
--- a/src/maxminddb.c
+++ b/src/maxminddb.c
@@ -34,7 +34,7 @@
do { \
char *binary = byte_to_binary(byte); \
if (NULL == binary) { \
- fprintf(stderr, "Malloc failed in DEBUG_BINARY\n"); \
+ fprintf(stderr, "Calloc failed in DEBUG_BINARY\n"); \
abort(); \
} \
fprintf(stderr, fmt "\n", binary); \
@@ -53,7 +53,7 @@
#ifdef MMDB_DEBUG
DEBUG_FUNC char *byte_to_binary(uint8_t byte)
{
- char *bits = malloc(sizeof(char) * 9);
+ char *bits = calloc(9, sizeof(char));
if (NULL == bits) {
return bits;
}
@@ -658,7 +658,7 @@ LOCAL int populate_languages_metadata(MMDB_s *mmdb, MMDB_s *metadata_db,
MMDB_INVALID_METADATA_ERROR);
mmdb->metadata.languages.count = 0;
- mmdb->metadata.languages.names = malloc(array_size * sizeof(char *));
+ mmdb->metadata.languages.names = calloc(array_size, sizeof(char *));
if (NULL == mmdb->metadata.languages.names) {
return MMDB_OUT_OF_MEMORY_ERROR;
}
@@ -676,7 +676,7 @@ LOCAL int populate_languages_metadata(MMDB_s *mmdb, MMDB_s *metadata_db,
if (NULL == mmdb->metadata.languages.names[i]) {
return MMDB_OUT_OF_MEMORY_ERROR;
}
- // We assign this as we go so that if we fail a malloc and need to
+ // We assign this as we go so that if we fail a calloc and need to
// free it, the count is right.
mmdb->metadata.languages.count = i + 1;
}
@@ -728,7 +728,7 @@ LOCAL int populate_description_metadata(MMDB_s *mmdb, MMDB_s *metadata_db,
MMDB_INVALID_METADATA_ERROR);
mmdb->metadata.description.descriptions =
- malloc(map_size * sizeof(MMDB_description_s *));
+ calloc(map_size, sizeof(MMDB_description_s *));
if (NULL == mmdb->metadata.description.descriptions) {
status = MMDB_OUT_OF_MEMORY_ERROR;
goto cleanup;
@@ -736,7 +736,7 @@ LOCAL int populate_description_metadata(MMDB_s *mmdb, MMDB_s *metadata_db,
for (uint32_t i = 0; i < map_size; i++) {
mmdb->metadata.description.descriptions[i] =
- malloc(sizeof(MMDB_description_s));
+ calloc(1, sizeof(MMDB_description_s));
if (NULL == mmdb->metadata.description.descriptions[i]) {
status = MMDB_OUT_OF_MEMORY_ERROR;
goto cleanup;
@@ -1140,7 +1140,7 @@ int MMDB_vget_value(MMDB_entry_s *const start,
MAYBE_CHECK_SIZE_OVERFLOW(length, SIZE_MAX / sizeof(const char *) - 1,
MMDB_INVALID_METADATA_ERROR);
- const char **path = malloc((length + 1) * sizeof(const char *));
+ const char **path = calloc(length + 1, sizeof(const char *));
if (NULL == path) {
return MMDB_OUT_OF_MEMORY_ERROR;
}
@@ -2000,6 +2000,7 @@ LOCAL MMDB_entry_data_list_s *dump_entry_data_list(
char *hex_string =
bytes_to_hex((uint8_t *)entry_data_list->entry_data.bytes,
entry_data_list->entry_data.data_size);
+
if (NULL == hex_string) {
*status = MMDB_OUT_OF_MEMORY_ERROR;
return NULL;
@@ -2093,7 +2094,7 @@ LOCAL char *bytes_to_hex(uint8_t *bytes, uint32_t size)
char *hex_string;
MAYBE_CHECK_SIZE_OVERFLOW(size, SIZE_MAX / 2 - 1, NULL);
- hex_string = malloc((size * 2) + 1);
+ hex_string = calloc((size * 2) + 1, sizeof(char));
if (NULL == hex_string) {
return NULL;
}
@@ -2102,6 +2103,8 @@ LOCAL char *bytes_to_hex(uint8_t *bytes, uint32_t size)
sprintf(hex_string + (2 * i), "%02X", bytes[i]);
}
+
+
return hex_string;
}
--
1.8.3.1