Skip to content

fix: document PCRE1 global state thread-safety hazard and add build w… - #380

Open
eilandert wants to merge 1 commit into
owasp-modsecurity:masterfrom
eilandert:fix/pcre1-thread-safety-warning
Open

eilandert wants to merge 1 commit into
owasp-modsecurity:masterfrom
eilandert:fix/pcre1-thread-safety-warning

Conversation

@eilandert

Copy link
Copy Markdown
Contributor

…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.

…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
@sonarqubecloud

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@airween

airween commented Sep 26, 2026

Copy link
Copy Markdown
Member

Hi @eilandert,

thanks for this PR.

I reviewed this with help of Claude Opus 5, and found the following:

The PCRE1 malloc/free workaround saves and restores the global pcre_malloc and pcre_free function pointers around ModSecurity calls.

True,

These are process-global in PCRE1 and not protected by any lock.

True,

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.

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 ngx_thread_task_post() (file I/O for aio threads and similar), and those tasks only run pread(), pwritev() or sendfile(). Currently all of the connector's phase handlers and filters - and therefore every pcre_malloc/pcre_free swap and every PCRE call made by libmodsecurity - run on the worker's event-loop thread, so the globals are never swapped concurrently. (This holds for nginx core and this connector; a third-party module running its own PCRE1 code in thread-pool tasks would be a separate concern.)

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 #warning would break builds. Nginx's configure adds -Werror to CFLAGS by default (auto/cc/gcc, auto/cc/clang) unless CFLAGS is set in the environment, and dynamic modules are compiled with the same flags. So on a PCRE1 build with --with-threads - which is how e.g. the Ubuntu 22.04 and Debian 11 nginx packages are configured - building the module with the arguments from nginx -V would be expected to fail with error: #warning ... [-Werror=cpp]. On PCRE2 builds the block isn't compiled at all, so the change has no effect there.

The suggested configure flag doesn't exist. As Copilot pointed out, Nginx has no --with-pcre2 option; PCRE2 is the default since nginx 1.21.5, and the relevant options are --with-pcre=DIR and --without-pcre2.

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?

@eilandert

Copy link
Copy Markdown
Contributor Author

Sorry this is so long ago i lost my train of thoughts on this :)
Documenting is good, otherwise close.

@fzipi

fzipi commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

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.

@fzipi

fzipi commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

@airween I don't have permission to close PRs in this repository. Could you close this one, per the comment above? Thanks.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants