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.

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Jan 12, 2025
## 1.12.2 - 2025-01-10

* `MMDB_get_entry_data_list()` now always sets the passed `entry_data_list`
  parameter to either `NULL` or valid memory. This makes it safe for
  callers to use `MMDB_free_entry_data_list()` on it even in case of error.
  In 1.12.0 `MMDB_get_entry_data_list()` was changed to not set this
  parameter to valid memory in additional error cases. That change caused
  segfaults for certain libraries that assumed it was safe to free memory
  on error. Doing so was never safe, but worked in some cases. This change
  makes such calls safe. Reported by Petr Pisar. GitHub
  maxmind/MaxMind-DB-Reader-XS#39.

## 1.12.1 - 2025-01-08

* Added missing `cmake_uninstall.cmake.in` to the source distribution. This
  was missing from 1.12.0, causing CMake builds to fail. Reported by Marcel
  Raad. GitHub #367.

## 1.12.0 - 2025-01-07

* Fixed memory leaks in `MMDB_open()`. These could happen with invalid
  databases or in error situations such as failing to allocate memory. As
  part of the fix, `MMDB_get_entry_data_list()` now frees memory it
  allocates on additional errors. Previously it failed to clean up when
  certain errors occurred. Pull request by pkillarjun. GitHub #356.
* There is now a build target to fuzz the library. Pull request by
  pkillarjun. GitHub #357.
* Updated `cmake_minimum_required` to a version range to quiet deprecation
  warnings on new CMake versions. Reported by gmou3. GitHub #359.
* The script for generating man pages no longer uses `autodie`. This
  eliminates the dependency on `IPC::System::Simple`. Reported by gmou3.
  GitHub #359.
* An uninstall target is now included for CMake. Pull request by gmou3.
  GitHub #362.
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