Skip to content

fix(mbedtls): preserve partial headers with dynamic RX buffers - #132

Merged
floitsch merged 2 commits into
patch-head-5.4.2from
floitsch/patch-head-5.4.2.dynamic-rx-headers
Sep 8, 2026
Merged

floitsch merged 2 commits into
patch-head-5.4.2from
floitsch/patch-head-5.4.2.dynamic-rx-headers

Conversation

@floitsch

@floitsch floitsch commented Sep 8, 2026

Copy link
Copy Markdown
Member

Nonblocking receives can return WANT_READ after only part of a TLS record header. The dynamic RX wrapper retains the received-byte count but stores the header bytes on the stack, so a later call can process a corrupted header and fail with MBEDTLS_ERR_SSL_INVALID_RECORD.

Preserve partial headers in the connection's idle RX buffer, adding five bytes after the counter/IV cache. Allocate the full record buffer before releasing that cache so allocation failure also preserves the input and cryptographic state for a retry.

Add Unity regression tests and a dynamic-buffer CI configuration covering all header split positions, repeated WANT_READ, interleaved connections, buffer reuse, counter/IV preservation, and close/reset buffer paths after partial reads, timeout, or EOF.

Validation:

  • The regression source fails with the original wrapper and passes with the fix in the host harness.
  • 54 verified TLS 1.2 transfer cases pass across AES-CBC, AES-GCM, and ChaCha20-Poly1305, including 1–4-byte receives, zero-length read probes, and peer-certificate retention on/off.
  • Host allocation-failure injection and AddressSanitizer memory-access checks pass; explicit allocation tracking returns to zero after the successful tests.
  • ESP32 regression-test firmware builds with dynamic buffers and comprehensive heap poisoning. Physical-board execution remains outstanding.

Based on the current patch-head-5.4.2 head. Its mbedTLS files match the version used for validation.

@floitsch
floitsch merged commit 1488abf into patch-head-5.4.2 Sep 8, 2026
2 checks passed
@floitsch
floitsch deleted the floitsch/patch-head-5.4.2.dynamic-rx-headers branch September 8, 2026 15:10
floitsch added a commit to toitlang/toit that referenced this pull request Sep 9, 2026
Reduce TLS heap use on ESP32 by enabling dynamic mbedTLS buffers and
disabling retention of the peer certificate after verification. Apply
both settings to all six supported targets (ESP32, C3, C6, P4, S2, and
S3); certificate verification remains enabled.

The required partial-header fix for nonblocking dynamic TLS receives
([toitware/esp-idf#132](toitware/esp-idf#132))
is available on master through #3216.

Generate `sdkconfig.defaults` with `idf.py save-defconfig`, then
recreate `sdkconfig` from those defaults for each target. Keep the
existing 7800/3700-byte record limits and leave dynamic freeing of
configuration/CA data disabled. TLS 1.3 remains disabled; these settings
would need revisiting when enabling it.
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