From 101a61881e6ff9ef9635d20f437531292878e1f9 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 30 Oct 2026 20:29:09 +0000 Subject: [PATCH] Reject invalid extended type bytes in decoder decode_one added 7 to the extended type byte without a check. A byte of 0 decoded as a map, and a byte greater than 8 decoded as a value whose type matches no MMDB_DATA_TYPE_* constant. The decoder now returns MMDB_INVALID_DATA_ERROR for any extended type byte outside 1 through 8. Co-Authored-By: Claude Opus 5.5 --- Changes.md | 8 +++ src/maxminddb.c | 4 ++ t/CMakeLists.txt | 1 + t/Makefile.am | 2 +- t/bad_extended_type_t.c | 113 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 t/bad_extended_type_t.c diff --git a/Changes.md b/Changes.md index d766fb5d..63d73343 100644 --- a/Changes.md +++ b/Changes.md @@ -1,3 +1,11 @@ +## next release + +- The decoder now returns `MMDB_INVALID_DATA_ERROR` for a value whose extended + type byte is 0 or greater than 8. Previously, it decoded an extended type byte + of 0 as a map, and it decoded a byte greater than 8 as a value whose `type` + matches no `MMDB_DATA_TYPE_*` constant. This also applies to the metadata, so + `MMDB_open()` can now fail on a database whose metadata contains such a value. + ## 1.14.1 - 2026-09-17 - Fixed a `-Wsign-compare` warning in `MMDB_open()` that broke builds with diff --git a/src/maxminddb.c b/src/maxminddb.c index 95ca7a7d..bef77373 100644 --- a/src/maxminddb.c +++ b/src/maxminddb.c @@ -1537,6 +1537,10 @@ static int decode_one(const MMDB_s *const mmdb, return MMDB_INVALID_DATA_ERROR; } type = get_ext_type(mem[offset++]); + if (type < MMDB_DATA_TYPE_INT32 || type > MMDB_DATA_TYPE_FLOAT) { + DEBUG_MSGF("Invalid extended type: %i", type); + return MMDB_INVALID_DATA_ERROR; + } DEBUG_MSGF("Extended type: %i (%s)", type, type_num_to_name(type)); } diff --git a/t/CMakeLists.txt b/t/CMakeLists.txt index bb9c23c7..20ba5ae7 100644 --- a/t/CMakeLists.txt +++ b/t/CMakeLists.txt @@ -4,6 +4,7 @@ add_library(tap # test programs set(TEST_TARGET_NAMES + bad_extended_type_t bad_pointers_t bad_search_tree_t basic_lookup_t diff --git a/t/Makefile.am b/t/Makefile.am index f2c01aca..a2a948d3 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -18,7 +18,7 @@ EXTRA_DIST = compile_c++_t.pl decoder_limits_t.pl external_symbols_t.pl \ check_PROGRAMS = \ bad_pointers_t bad_databases_t bad_data_size_t bad_epoch_t bad_indent_t \ - bad_search_tree_t \ + bad_extended_type_t bad_search_tree_t \ basic_lookup_t data_entry_list_t \ data-pool-t data_types_t double_close_t dump_t empty_container_metadata_t \ gai_error_t get_value_t \ diff --git a/t/bad_extended_type_t.c b/t/bad_extended_type_t.c new file mode 100644 index 00000000..0784e72f --- /dev/null +++ b/t/bad_extended_type_t.c @@ -0,0 +1,113 @@ +#include "maxminddb_test_helper.h" + +// Copies section into a heap buffer of the exact size, so a sanitizer reports +// any read past its end. +static uint8_t *make_section(const char *section, size_t size) { + uint8_t *data = malloc(size); + if (NULL == data) { + BAIL_OUT("could not allocate section"); + } + memcpy(data, section, size); + return data; +} + +static void check_entry_data_list(const char *section, + size_t size, + int expected_status, + int expected_type, + const char *message) { + uint8_t *data = make_section(section, size); + MMDB_s mmdb = {.data_section = data, .data_section_size = (uint32_t)size}; + MMDB_entry_s entry = {.mmdb = &mmdb, .offset = 0}; + MMDB_entry_data_list_s *entry_data_list = NULL; + + int status = MMDB_get_entry_data_list(&entry, &entry_data_list); + cmp_ok(status, "==", expected_status, "status: %s", message); + if (MMDB_SUCCESS == status) { + cmp_ok(entry_data_list->entry_data.type, + "==", + expected_type, + "type: %s", + message); + } + + MMDB_free_entry_data_list(entry_data_list); + free(data); +} + +#define CHECK_ENTRY_DATA_LIST(section, status, type, message) \ + check_entry_data_list(section, sizeof(section) - 1, status, type, message) + +void test_extended_type_range(void) { + // The control byte 0x00 is an extended type with a size of 0. + CHECK_ENTRY_DATA_LIST("\x00\x00", + MMDB_INVALID_DATA_ERROR, + 0, + "extended type byte 0 is rejected"); + CHECK_ENTRY_DATA_LIST("\x00\x09", + MMDB_INVALID_DATA_ERROR, + 0, + "extended type byte 9 is rejected"); + CHECK_ENTRY_DATA_LIST("\x00\xFF", + MMDB_INVALID_DATA_ERROR, + 0, + "extended type byte 255 is rejected"); + + CHECK_ENTRY_DATA_LIST("\x00\x01", + MMDB_SUCCESS, + MMDB_DATA_TYPE_INT32, + "extended type byte 1 is an int32"); + CHECK_ENTRY_DATA_LIST("\x04\x08\x00\x00\x00\x00", + MMDB_SUCCESS, + MMDB_DATA_TYPE_FLOAT, + "extended type byte 8 is a float"); +} + +static void +check_skipped_value(const char *section, size_t size, const char *message) { + uint8_t *data = make_section(section, size); + MMDB_s mmdb = {.data_section = data, .data_section_size = (uint32_t)size}; + MMDB_entry_s entry = {.mmdb = &mmdb, .offset = 0}; + MMDB_entry_data_s entry_data; + + int status = MMDB_get_value(&entry, &entry_data, "b", NULL); + cmp_ok(status, "==", MMDB_INVALID_DATA_ERROR, "%s", message); + + free(data); +} + +void test_extended_type_in_skipped_value(void) { + // A map of {"a": , "b": 5}. MMDB_get_value skips + // the value of "a" on its way to "b". Byte 0 is an empty map if it is + // not rejected, so a skip would silently succeed. + const char section_9[] = "\xE2" + "\x41" + "a" + "\x00\x09" + "\x41" + "b" + "\xA1\x05"; + check_skipped_value( + section_9, + sizeof(section_9) - 1, + "a skipped value with extended type byte 9 is rejected"); + + const char section_0[] = "\xE2" + "\x41" + "a" + "\x00\x00" + "\x41" + "b" + "\xA1\x05"; + check_skipped_value( + section_0, + sizeof(section_0) - 1, + "a skipped value with extended type byte 0 is rejected"); +} + +int main(void) { + plan(NO_PLAN); + test_extended_type_range(); + test_extended_type_in_skipped_value(); + done_testing(); +}