Fix OpenSSL 3.x fork() crashes with pkg_malloc double-free protection #3737
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
OpenSSL 3.x crashes with "freeing already freed pointer" when using custom memory allocators with fork() and TLS_VERIFY_CERT=1. This occurs because OpenSSL 3.x stores the same pointer in multiple thread-local storage slots, and when fork() duplicates TLS, child processes try to free the same buffer multiple times during cleanup.
Details
Bug Fix: OpenSSL 3.x Double-Free Crash with TLS Certificate Verification
Problem Description
This PR fixes a critical crash that occurs when using OpenSIPS with OpenSSL 3.x and TLS certificate verification enabled (TLS_VERIFY_CERT=1). The issue manifests as:
CRITICAL:qm_free: freeing already freed pointer (0xffff...), first free:
Followed by immediate process termination.
Root Cause
OpenSSL 3.x introduced extensive use of thread-local storage (TLS) for managing per-thread state (error queues, RNG state, provider dispatch tables). When combined with OpenSIPS's fork-based worker model, this creates a double-free scenario:
pointers
When Does This Occur?
Trigger Condition: The bug is specifically triggered when:
Affected Scenarios:
Not Affected:
Solution
The fix implements a thin tracking layer around pkg_malloc specifically for OpenSSL 3.x allocations.
Each allocation gets a 12-byte header containing:
Key Points:
This is not a hack - it's a defensive wrapper that:
Testing Results
OpenSSL 3.0.17 (Debian Bookworm):
OpenSSL 1.1.1w (Debian Bullseye):
Without this fix, OpenSIPS cannot safely use OpenSSL 3.x with certificate verification enabled. This is a blocker for:
Compatibility
No breaking changes. This fix is fully backward compatible and transparent to existing configurations.
Existing Scenarios - No Migration Required
All existing OpenSIPS configurations work unchanged:
Configuration Changes Required
None. The fix automatically detects the OpenSSL version at compile time and uses the appropriate memory allocator.
Runtime Behavior
Module Dependencies
No changes to module loading order or dependencies. The existing requirement to load tls_mgm before other modules remains unchanged.
API/ABI Compatibility
Upgrade Path
From OpenSSL 1.x → OpenSSL 3.x:
Already running OpenSSL 3.x:
Downgrade/Rollback
Safe to downgrade - the fix is isolated to the OpenSSL memory allocator wrappers. Rolling back to a version without this fix will:
Closing issues