Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent out of boundary access when using offset #103

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 16 additions & 1 deletion src/maxminddb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ LOCAL int decode_one(MMDB_s *mmdb, uint32_t offset,
{
const uint8_t *mem = mmdb->data_section;

if (offset > mmdb->data_section_size) {
if (offset + 1 > mmdb->data_section_size) {
return MMDB_INVALID_DATA_ERROR;
}

Expand All @@ -1307,6 +1307,9 @@ LOCAL int decode_one(MMDB_s *mmdb, uint32_t offset,
DEBUG_MSGF("Type: %i (%s)", type, type_num_to_name(type));

if (type == MMDB_DATA_TYPE_EXTENDED) {
if (offset + 1 > mmdb->data_section_size) {
return MMDB_INVALID_DATA_ERROR;
}
type = get_ext_type(mem[offset++]);
DEBUG_MSGF("Extended type: %i (%s)", type, type_num_to_name(type));
}
Expand All @@ -1317,6 +1320,9 @@ LOCAL int decode_one(MMDB_s *mmdb, uint32_t offset,
int psize = (ctrl >> 3) & 3;
DEBUG_MSGF("Pointer size: %i", psize);

if (offset + psize + 1 > mmdb->data_section_size) {
return MMDB_INVALID_DATA_ERROR;
}
entry_data->pointer = get_ptr_from(ctrl, &mem[offset], psize);
DEBUG_MSGF("Pointer to: %i", entry_data->pointer);

Expand All @@ -1328,13 +1334,22 @@ LOCAL int decode_one(MMDB_s *mmdb, uint32_t offset,
uint32_t size = ctrl & 31;
switch (size) {
case 29:
if (offset + 1 > mmdb->data_section_size) {
return MMDB_INVALID_DATA_ERROR;
}
size = 29 + mem[offset++];
break;
case 30:
if (offset + 2 > mmdb->data_section_size) {
return MMDB_INVALID_DATA_ERROR;
}
size = 285 + get_uint16(&mem[offset]);
offset += 2;
break;
case 31:
if (offset + 3 > mmdb->data_section_size) {
return MMDB_INVALID_DATA_ERROR;
}
size = 65821 + get_uint24(&mem[offset]);
offset += 3;
default:
Expand Down