Repository navigation
Conversation
…arning The PCRE1 malloc/free workaround saves and restores the global pcre_malloc and pcre_free function pointers around ModSecurity calls. These are process-global in PCRE1 and not protected by any lock. In nginx configurations that use thread pools (NGX_THREADS), concurrent requests can race on these globals: one thread may allocate memory from pool A while another swaps the pointer to pool B, causing the subsequent free to operate on the wrong pool and corrupt heap memory. Changes: - Expand the block comment to clearly describe the thread-safety hazard and the conditions under which it can manifest - Add a #warning directive that fires at compile time when building with both PCRE1 (no NGX_PCRE2) and nginx thread pools (NGX_THREADS enabled), prompting the operator to switch to PCRE2 - No logic change; the runtime behaviour is unchanged for the common event-loop (non-threaded) case The correct long-term fix is to build nginx with PCRE2. PCRE2 does not use global allocator pointers and eliminates this issue entirely. Severity: High (thread-unsafe global state under NGX_THREADS builds) Reported-by: Security audit 2026-05-13
|
There was a problem hiding this comment.
Pull request overview
This PR documents the PCRE1 allocator global-state hazard in threaded nginx builds and adds a compile-time warning for PCRE1 plus NGX_THREADS configurations.
Changes:
- Expanded the PCRE workaround comment to describe thread-safety risks.
- Added a preprocessor warning for PCRE1 builds with nginx thread support enabled.
- Clarified that PCRE2 avoids the global allocator pointer issue.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * via another pool, causing heap corruption. | ||
| * | ||
| * This block is compiled only when building against PCRE1 (not PCRE2). | ||
| * Building nginx with PCRE2 (--with-pcre2 or linking against libpcre2) avoids |
|
Hi @eilandert, thanks for this PR. I reviewed this with help of Claude Opus 5, and found the following:
True,
True,
That sounds like true but not. In my opinion, the described race doesn't occur in this connector. Nginx thread pools only execute tasks that nginx itself calls via The one proposal that would change this, #396 (running phase 5 in a thread pool), deliberately restricts itself to PCRE2 builds, where the swap helpers are no-ops, and refuses the directive at configuration time on PCRE1. That is the right place to handle it, rather than a compile-time warning on every threaded PCRE1 build. It looks like the The suggested configure flag doesn't exist. As Copilot pointed out, Nginx has no What is worth documenting is the invariant behind point 1: the PCRE1 allocator swap is safe only because libmodsecurity is called exclusively from the event-loop thread. Do you think we can add more here? |
|
Sorry this is so long ago i lost my train of thoughts on this :) |
|
Closing, as agreed in the thread. The review found that the thread-race scenario does not apply, since nginx does not run ModSecurity calls on thread-pool threads, so the warning would be misleading. Building with PCRE2 remains the recommended configuration. Thanks for the contribution and for your patience. |
|
@airween I don't have permission to close PRs in this repository. Could you close this one, per the comment above? Thanks. |



…arning
The PCRE1 malloc/free workaround saves and restores the global pcre_malloc and pcre_free function pointers around ModSecurity calls. These are process-global in PCRE1 and not protected by any lock.
In nginx configurations that use thread pools (NGX_THREADS), concurrent requests can race on these globals: one thread may allocate memory from pool A while another swaps the pointer to pool B, causing the subsequent free to operate on the wrong pool and corrupt heap memory.
Changes:
The correct long-term fix is to build nginx with PCRE2. PCRE2 does not use global allocator pointers and eliminates this issue entirely.