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
8 changes: 8 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/maxminddb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
1 change: 1 addition & 0 deletions t/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion t/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
113 changes: 113 additions & 0 deletions t/bad_extended_type_t.c
Original file line number Diff line number Diff line change
@@ -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": <bad extended type byte>, "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();
}
Loading