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

Do not call MMDB_free_entry_data_list() on libmaxminddb failures #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ppisar
Copy link

@ppisar ppisar commented Jan 10, 2025

After upgrading libmaxminddb from 1.11.0 to 1.12.0, t/MaxMind/DB/Reader-broken-databases.t started to segfault. A minimal reproducer is:

use MaxMind::DB::Reader;
# Test broken doubles
my $reader = MaxMind::DB::Reader->new( file => 'maxmind-db/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb');
$reader->record_for_address('2001:220::');

The cause was that MaxMind-DB-Reader-XS called
MMDB_free_entry_data_list(entry_data_list) when handling a MMDB_get_entry_data_list(, &entry_data_list) failure. But MMDB_get_entry_data_list() did not initialize the pointer in case of error.

That was not true even before libmaxminddb 1.12.0, but because previous libmaxminddb versions did not free the memory itself, the erroneous MMDB_free_entry_data_list() call actually operated on valid data and no crash occured. When libmaxminddb 1.12.0 started correctly to clean up on the error, MaxMind-DB-Reader-XS started to double-free entry_data_list leading to a crash.

This bug was introduced with e5322e5 commit ("free entry data list before croaking on failed calls to get entry data list").

This patch removes all MMDB_free_entry_data_list() calls when handling libmaxminddb failures as the pointer is supposed to be undefined in that case. This patch does not conditionalize this change on libmaxminddb version because it's better to leak a memory than to crash after updating libmaxminddb.

After upgrading libmaxminddb from 1.11.0 to 1.12.0,
t/MaxMind/DB/Reader-broken-databases.t started to segfault. A minimal
reproducer is:

    use MaxMind::DB::Reader;
    # Test broken doubles
    my $reader = MaxMind::DB::Reader->new( file => 'maxmind-db/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb');
    $reader->record_for_address('2001:220::');

The cause was that MaxMind-DB-Reader-XS called
MMDB_free_entry_data_list(entry_data_list) when handling
a MMDB_get_entry_data_list(, &entry_data_list) failure. But
MMDB_get_entry_data_list() did not initialize the pointer in case of
error.

That was not true even before libmaxminddb 1.12.0, but because
previous libmaxminddb versions did not free the memory itself, the
erroneous MMDB_free_entry_data_list() call actually operated on valid
data and no crash occured. When libmaxminddb 1.12.0 started correctly
to clean up on the error, MaxMind-DB-Reader-XS started to double-free
entry_data_list leading to a crash.

This bug was introduced with e5322e5
commit ("free entry data list before croaking on failed calls to get
entry data list").

This patch removes all MMDB_free_entry_data_list() calls when handling
libmaxminddb failures as the pointer is supposed to be undefined in
that case. This patch does not conditionalize this change on
libmaxminddb version because it's better to leak a memory than to
crash after updating libmaxminddb.
@oschwald
Copy link
Member

Thanks for the analysis and the PR. We decided to resolve the issue in libmaxminddb. See maxmind/libmaxminddb#368. This should make calling MMDB_free_entry_data_list safe in these cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants