Skip to content

Fix UCRT invalid-fd crash when request body temp file creation fails (#3397) - #3796

Open
A13501350 wants to merge 1 commit into
owasp-modsecurity:v2/masterfrom
A13501350:fix/issue-3397-reqbody-fd
Open

A13501350 wants to merge 1 commit into
owasp-modsecurity:v2/masterfrom
A13501350:fix/issue-3397-reqbody-fd

Conversation

@A13501350

Copy link
Copy Markdown

Summary

Fixes #3397 (re-verified on current v2/master, where #3624 did not address this crash because #3624 was in response-side ReadFileChunk(), whereas #3397 occurs during request body ingestion).

Root Cause

On Windows (observed on Windows Server 2025 / IIS 10.0), when a request body exceeds SecRequestBodyInMemoryLimit (131072 bytes out of the box) and temporary file creation fails (for example, SecTmpDir c:\inetpub\temp\ is not writable by the IIS AppPool identity, the directory does not exist, or SecTmpDir none is configured):

  1. modsecurity_request_body_store_memory() switched msr->msc_reqbody_storage to MSC_REQBODY_DISK before modsecurity_request_body_start_init() succeeded, leaving msr->msc_reqbody_fd = -1 (and msr->msc_reqbody_filename populated even though no file was created).
  2. In read_request_body() (apache2/apache2_io.c), internal storage errors (rcbs < 0 with rcbs != -5) were only aborted when is_enabled == MODSEC_ENABLED && if_limit_action == REQUEST_BODY_LIMIT_ACTION_REJECT. Under out-of-the-box SecRuleEngine DetectionOnly (or ProcessPartial), the loop swallowed -1 and continued reading subsequent chunks.
  3. On the next chunk, modsecurity_request_body_store_disk() called CRT write(-1, data, length). Unlike POSIX (which returns EBADF), Windows UCRT invokes the invalid-parameter handler (ucrtbase!invoke_watson / FAST_FAIL_INVALID_ARG, exception 0xc0000409 in ucrtbase.dll), crashing w3wp.exe.
Reproduced WER / debugger stack trace on v2/master prior to this fix
  • Windows Event ID 1000: w3wp.exe (10.0.26100.1882), faulting module ucrtbase.dll (10.0.26100.33648), exception 0xc0000409, offset 0x000000000001cba8.
  • cdb.exe .ecxr; k (Subcode: 0x5 FAST_FAIL_INVALID_ARG):
    ucrtbase!invoke_watson+0x18
    ucrtbase!invalid_parameter_internal+0xd1
    ucrtbase!write_internal+0x11b
    ucrtbase!write+0x41
    ModSecurityIIS!modsecurity_request_body_store_disk+0x2b
    ModSecurityIIS!modsecurity_request_body_store+0x2c9
    ModSecurityIIS!read_request_body+0x22c
    ModSecurityIIS!hook_request_late+0x26f
    ModSecurityIIS!modsecProcessRequestBody+0x2d
    ModSecurityIIS!CMyHttpModule::OnBeginRequest+0x1014
    

Changes

  • apache2/msc_reqbody.c:
    • Guard msr->txcfg->tmp_dir == NULL in modsecurity_request_body_start_init(), set msr->msc_reqbody_fd = -1, and only assign msr->msc_reqbody_filename once msc_mkstemp() succeeds (avoiding spurious "Failed to delete temporary file" errors in modsecurity_request_body_clear()).
    • Restore msr->msc_reqbody_storage = MSC_REQBODY_MEMORY if modsecurity_request_body_start_init() fails during spill-to-disk in modsecurity_request_body_store_memory().
    • Add defensive checks before CRT calls: msr->msc_reqbody_fd < 0 in modsecurity_request_body_store_disk() and modsecurity_request_body_retrieve(), and msr->msc_reqbody_filename == NULL in modsecurity_request_body_retrieve_start().
  • apache2/apache2_io.c:
    • In read_request_body(), return HTTP_INTERNAL_SERVER_ERROR for non-limit (rcbs != -5) modsecurity_request_body_store() failures regardless of SecRuleEngine DetectionOnly or SecRequestBodyLimitAction ProcessPartial (consistent with modsecurity_request_body_start(), modsecurity_request_body_to_stream(), and modsecurity_request_body_end()).
  • apache2/msc_multipart.c:
    • Guard msr->txcfg->tmp_dir == NULL and only assign msr->mpd->mpp->tmp_file_name after msc_mkstemp_ex() succeeds.
    • Change msr->mpd->mpp->tmp_file_fd != 0 to > 0 in multipart_process_boundary() so close(-1) cannot be called on UCRT.

Note for Windows / IIS deployments

Out of the box on Windows Server, C:\inetpub\temp\ is not writable by IIS_IUSRS / AppPool identities, so uploads exceeding SecRequestBodyInMemoryLimit (131072) require SecTmpDir to point to a directory writable by the AppPool identity. With this fix, if SecTmpDir is missing or not writable, ModSecurity cleanly logs the temporary file creation error and returns HTTP 500 instead of crashing w3wp.exe.

Verification

Tested on GitHub Actions (windows-latest / Windows Server 2025, RelWithDebInfo):

  • go-ftw CRS regression suite passed (exit code 0).
  • Verified 7 multipart upload scenarios (64 KB, 4.5 MB, 12 MB, 14 MB across DetectionOnly and On, Reject and ProcessPartial, default c:\inetpub\temp\, writable SecTmpDir, missing SecTmpDir, and SecTmpDir none): zero w3wp.exe crashes, zero WER dumps, and zero Event ID 1000 entries (actions/runs/46908964436).

@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 619ea22f-c99b-4b50-855f-85265c036aea

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@A13501350
A13501350 force-pushed the fix/issue-3397-reqbody-fd branch from 85f2e2b to b13765c Compare December 2, 2026 15:30
@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
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.

1 participant