From cf56c4a25f7d67d1fd93cad39fa24752010caa6f Mon Sep 17 00:00:00 2001 From: Minh Vu Date: Mon, 27 Jul 2026 20:38:31 +0200 Subject: [PATCH 1/5] fix: close fd zero when mmap fails --- src/modulefinder/sentry_modulefinder_linux.c | 2 +- tests/unit/test_modulefinder.c | 30 ++++++++++++++++++++ tests/unit/tests.inc | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/modulefinder/sentry_modulefinder_linux.c b/src/modulefinder/sentry_modulefinder_linux.c index b2f9ddd9de..7dc6884b20 100644 --- a/src/modulefinder/sentry_modulefinder_linux.c +++ b/src/modulefinder/sentry_modulefinder_linux.c @@ -74,7 +74,7 @@ sentry__mmap_file(sentry_mmap_t *rv, const char *path) return true; fail: - if (fd > 0) { + if (fd >= 0) { close(fd); } rv->ptr = NULL; diff --git a/tests/unit/test_modulefinder.c b/tests/unit/test_modulefinder.c index 563f49baeb..d19c1f7601 100644 --- a/tests/unit/test_modulefinder.c +++ b/tests/unit/test_modulefinder.c @@ -4,6 +4,9 @@ #ifdef SENTRY_PLATFORM_LINUX # include "modulefinder/sentry_modulefinder_linux.h" +# include +# include +# include #endif SENTRY_TEST(module_finder) @@ -74,6 +77,33 @@ SENTRY_TEST(module_addr) #endif } +SENTRY_TEST(mmap_file_closes_fd_zero_on_failure) +{ +#if !defined(SENTRY_PLATFORM_LINUX) + SKIP_TEST(); +#else + char tmp_path[] = "/tmp/sentry-native-mmap-XXXXXX"; + int tmp_fd = mkstemp(tmp_path); + TEST_ASSERT(tmp_fd >= 0); + close(tmp_fd); + + int saved_stdin = dup(STDIN_FILENO); + TEST_ASSERT(saved_stdin >= 0); + close(STDIN_FILENO); + + sentry_mmap_t mmap = { 0 }; + bool mapped = sentry__mmap_file(&mmap, tmp_path); + int fd_flags = fcntl(STDIN_FILENO, F_GETFD); + + dup2(saved_stdin, STDIN_FILENO); + close(saved_stdin); + unlink(tmp_path); + + TEST_CHECK(!mapped); + TEST_CHECK(fd_flags == -1); +#endif +} + SENTRY_TEST(procmaps_parser) { #if !defined(SENTRY_PLATFORM_LINUX) || __SIZEOF_POINTER__ != 8 diff --git a/tests/unit/tests.inc b/tests/unit/tests.inc index 6789e1d926..e845f07e37 100644 --- a/tests/unit/tests.inc +++ b/tests/unit/tests.inc @@ -222,6 +222,7 @@ XX(minidump_stream_types) XX(minidump_structures_packed) XX(minidump_system_info) XX(minidump_thread_structure) +XX(mmap_file_closes_fd_zero_on_failure) XX(module_addr) XX(module_finder) XX(mpack_newlines) From 0bcad1039f6f8b62e755e774e5ff84cc6df0116f Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Tue, 4 Aug 2026 10:43:14 +0200 Subject: [PATCH 2/5] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a960cc7b1c..4b36fc5e98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ **Fixes**: - Name `sentry-logs` and `sentry-metrics` telemetry batcher threads so they can be identified in debuggers, profilers, and crash reports. ([#1937](https://github.com/getsentry/sentry-native/pull/1937)) +- Linux: close descriptor `0` on failed module file mapping when that descriptor was returned by `open`, avoiding a file descriptor leak. ([#1924](https://github.com/getsentry/sentry-native/pull/1924)) **Fixes**: From 369565ba56b81796c951ff9ec1adbfdea97a75b8 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Tue, 4 Aug 2026 10:50:25 +0200 Subject: [PATCH 3/5] skip test on android --- tests/unit/test_modulefinder.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_modulefinder.c b/tests/unit/test_modulefinder.c index d19c1f7601..3173c3ba1d 100644 --- a/tests/unit/test_modulefinder.c +++ b/tests/unit/test_modulefinder.c @@ -79,7 +79,7 @@ SENTRY_TEST(module_addr) SENTRY_TEST(mmap_file_closes_fd_zero_on_failure) { -#if !defined(SENTRY_PLATFORM_LINUX) +#if !defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID) SKIP_TEST(); #else char tmp_path[] = "/tmp/sentry-native-mmap-XXXXXX"; From cf43bd2b3c5f9eeaab4f559e8c9af41b398fce38 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Tue, 4 Aug 2026 11:04:04 +0200 Subject: [PATCH 4/5] declare sentry__mmap_file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` tests/unit/test_modulefinder.c: In function ‘test_sentry_mmap_file_closes_fd_zero_on_failure’: tests/unit/test_modulefinder.c:95:19: error: implicit declaration of function ‘sentry__mmap_file’; did you mean ‘sentry_attach_file’? [-Wimplicit-function-declaration] 95 | bool mapped = sentry__mmap_file(&mmap, tmp_path); | ^~~~~~~~~~~~~~~~~ ``` --- src/modulefinder/sentry_modulefinder_linux.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/modulefinder/sentry_modulefinder_linux.h b/src/modulefinder/sentry_modulefinder_linux.h index c063825a3b..90c58c3975 100644 --- a/src/modulefinder/sentry_modulefinder_linux.h +++ b/src/modulefinder/sentry_modulefinder_linux.h @@ -52,6 +52,8 @@ void sentry__module_mapping_push( sentry_module_t *module, const sentry_parsed_module_t *parsed); #ifdef SENTRY_UNITTEST +bool sentry__mmap_file(sentry_mmap_t *rv, const char *path); + bool sentry__procmaps_read_ids_from_elf( sentry_value_t value, const sentry_module_t *module); From 4cc38437e4473d013bb2881b5fdaf6d5d2c3c1cd Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Tue, 4 Aug 2026 12:04:39 +0200 Subject: [PATCH 5/5] Fixup CHANGELOG.md --- CHANGELOG.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b36fc5e98..aa66734714 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,13 +11,7 @@ - Name `sentry-logs` and `sentry-metrics` telemetry batcher threads so they can be identified in debuggers, profilers, and crash reports. ([#1937](https://github.com/getsentry/sentry-native/pull/1937)) - Linux: close descriptor `0` on failed module file mapping when that descriptor was returned by `open`, avoiding a file descriptor leak. ([#1924](https://github.com/getsentry/sentry-native/pull/1924)) - -**Fixes**: - - `sentry_attachment_set_filename`, `sentry_attachment_set_type`, and `sentry_attachment_set_content_type` now flush the scope, so changes applied after `sentry_attach_file`/`sentry_attach_bytes` also apply to hard-crash events instead of only to normal events. ([#1934](https://github.com/getsentry/sentry-native/pull/1934)) - -**Fixes**: - - Crashpad: fix a crash when calling `sentry_init` before C++ dynamic initializers have run. ([#1930](https://github.com/getsentry/sentry-native/issues/1930), [mini_chromium#8](https://github.com/getsentry/mini_chromium/pull/8)) ## 0.16.1