Skip to content

fix(net,rmt_rx): heap-ownership handshake + ISR copy underflow guard (#3588) - #3599

Merged
zackees merged 1 commit into
masterfrom
fix/httpd-ownership-rmtrx-underflow-3588
Jul 5, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/httpd-ownership-rmtrx-underflow-3588

Conversation

@zackees

@zackees zackees commented Jul 5, 2026 •

Copy link
Copy Markdown
Member

Two genuine memory-safety defects found while investigating #3588. Neither fully cures the WROOM corruption (it still reproduces — see issue), but both are real OOB/UAF bugs.

1. Marshaling request-item lifetime (server.cpp.hpp)

The request item the httpd task handed to the main-loop runner was stack-allocated. On a 5 s handler-wait timeout, handle_esp_request returned and its stack frame died while the item pointer was still live in the queue — the runner then wrote *response through freed stack memory. Now heap-allocated with an atomic ownership handshake (CAS 0→1 runner-claims / 0→3 httpd-abandons) that assigns cleanup to exactly one side. No double-free, no dangling pointer on either the timeout or normal path.

Also fixes a NUL-terminator bug from the prior patch: four str[i] = '\0' writes were stored as literal NUL bytes in the source char literals — now proper '\0' escapes.

2. RMT-RX accumulation copy underflow (rmt_rx_channel_5.cpp.hpp)

available_space = buffer.size() - offset underflows to a huge value if a late callback lands while the buffer is cleared (size 0) with a stale-nonzero offset, letting the symbol copy write past the buffer. Guarded with an explicit offset < size check.

Validation

WROOM I2S/RMT/UART/SPI loopback 4/4; dual-device marshaled HTTP serves over the air. Host 349/349, lint clean, esp32dev/c6/s3 compile clean.

Refs #3588.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved request handling on ESP32 to prevent stale pointers and heap corruption under heavy load.
    • Fixed timeout and cleanup paths so queued HTTP requests are safely released without duplicate cleanup.
    • Hardened RMT receive buffering to avoid invalid copy sizes when available space is exhausted, reducing the risk of memory issues during partial/DMA reception.

…R copy underflow guard (#3588)

Two genuine memory-safety defects found while investigating #3588.
Neither fully cures the WROOM corruption (it still reproduces — see the
issue), but both are real out-of-bounds/UAF bugs worth fixing on their
own merits.

1. esp_http_server marshaling (server.cpp.hpp): the request item that
   the httpd task hands to the main-loop runner was STACK-allocated.
   When the 5 s handler wait timed out, handle_esp_request returned and
   its stack frame died while the item pointer was still live in the
   queue — the runner then wrote *response through freed stack memory.
   The item is now heap-allocated with an atomic ownership handshake
   (CAS state 0→1 runner-claims / 0→3 httpd-abandons) that
   deterministically assigns cleanup to exactly one side, with no
   double-free and no dangling pointer on either the timeout or the
   normal path.
   Also fixes a NUL-terminator bug from the prior patch: four
   `str[i] = '\0'` writes had been stored as literal NUL *bytes* in the
   source char literals (functionally correct but tool-hostile) — now
   proper `'\0'` escapes.

2. RMT-RX accumulation copy (rmt_rx_channel_5.cpp.hpp): the
   `available_space = buffer.size() - offset` computation UNDERFLOWS to
   a huge value if a late callback lands while the buffer is cleared
   (size 0) but the offset is still stale-nonzero, causing the symbol
   copy to write far past the buffer. Guarded with an explicit
   `offset < size` check.

Verified on WROOM: I2S/RMT/UART/SPI loopback all 4/4, dual-device
marshaled HTTP serves over the air. Host 349/349, lint clean,
esp32dev/c6/s3 compile clean.

Co-Authored-By: Claude Fable 5 <[email protected]>
@zackees
zackees merged commit 217ca30 into master Jul 5, 2026
10 of 11 checks passed
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Jul 5, 2026
@coderabbitai

coderabbitai Bot commented Jul 5, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 1f842197-e974-4b03-bbb0-ac4935b65838

📥 Commits

Reviewing files that changed from the base of the PR and between 8df8258 and 5c8ec38.

📒 Files selected for processing (2)
  • src/fl/stl/asio/http/server.cpp.hpp
  • src/platforms/esp/32/drivers/rmt_rx/rmt_rx_5/rmt_rx_channel_5.cpp.hpp

📝 Walkthrough

Walkthrough

This PR hardens ESP32 HTTP server request lifecycle management by adding atomic state tracking and centralized cleanup to prevent stale pointer access and heap corruption, and adds a bounds check in the RMT RX ISR callback to prevent buffer size underflow.

Changes

ESP32 HTTP Server Request Lifecycle

Layer / File(s) Summary
Pending request state contract and cleanup helper
src/fl/stl/asio/http/server.cpp.hpp
Adds atomic.h include and an atomic state field to EspPendingRequest; introduces espRequestCleanup to free the Response, body buffer, semaphore, and request object.
Runner queue-drain state transition
src/fl/stl/asio/http/server.cpp.hpp
Runner queue processing now gates on a state transition from queued to processing, discarding abandoned requests via espRequestCleanup instead of running handlers on stale memory.
HTTPD task allocation, handoff, and timeout ownership
src/fl/stl/asio/http/server.cpp.hpp
handle_esp_request heap-allocates EspPendingRequest with initialized state, queues it to the runner, and uses state.compare_exchange on semaphore timeout to decide cleanup ownership versus waiting for runner completion; response sending finishes via espRequestCleanup.

RMT RX ISR Underflow Guard

Layer / File(s) Summary
Accumulation buffer space bounds check
src/platforms/esp/32/drivers/rmt_rx/rmt_rx_5/rmt_rx_channel_5.cpp.hpp
rxDoneCallback computes available_space with a guarded comparison instead of direct subtraction, preventing underflow when the accumulation buffer size is smaller than the offset.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related issues

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/httpd-ownership-rmtrx-underflow-3588

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

Comment @coderabbitai help to get the list of available commands.

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

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant