Repository navigation
Fix VLA stack overflow in TLS writev after write failure - #4307
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThe server adds a debug command and state field for forced TLS write errors. TLS vector writes now use bounded buffering and validate retry lengths. A regression test exercises small and large pipelined TLS responses. ChangesTLS write-error handling
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant TLSClient
participant DebugCommand
participant connTLSWritev
participant connTLSWrite
TLSClient->>DebugCommand: Set FORCE-TLS-WRITE-ERROR
DebugCommand->>connTLSWritev: Enable forced write error
connTLSWritev->>connTLSWrite: Submit vector data
connTLSWrite-->>connTLSWritev: Return EAGAIN and retry length
connTLSWritev->>connTLSWrite: Retry bounded data
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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. Comment |
e11bf1b to
371517f
Compare
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (1)
src/tls.c (1)
1716-1743: 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy liftConsider a GoogleTest for the buffer-coalescing and retry-length logic.
The buffer-fill loop and the
to_write < conn->last_failed_write_data_lenretry check are pure, TLS-independent logic. Extracting this into a small testable helper and adding a GoogleTest undersrc/unit/would let boundary cases (to_writeexactly atNET_MAX_WRITES_PER_EVENT, multi-iovec coalescing, retry-length exactly satisfied/unsatisfied) be verified without a live 10 MB TLS round trip.As per path instructions: "Place data-structure and low-level logic tests in
src/unit/as C++ GoogleTest tests."🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/tls.c` around lines 1716 - 1743, Extract the buffer-coalescing and retry-length validation from the connTLSWritev flow into a small TLS-independent helper, preserving the NET_MAX_WRITES_PER_EVENT cap and last_failed_write_data_len check. Add a C++ GoogleTest under src/unit/ covering exact-capacity writes, multi-iovec concatenation, and retry lengths that are exactly satisfied or unsatisfied.Source: Path instructions
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/debug.c`:
- Around line 1097-1099: Add a usage and description entry for DEBUG
FORCE-TLS-WRITE-ERROR to the help[] array in the DEBUG HELP definitions,
matching the formatting and wording style of the existing DEBUG subcommand
entries and reflecting its required argument.
In `@src/tls.c`:
- Around line 1716-1743: Update createIOThread() so its pthread attributes
configure the stack size using the VALKEY_THREAD_STACK_SIZE guard, matching
bioInit() before creating I/O threads; preserve the existing thread creation
behavior while ensuring connTLSWritev() has sufficient stack space for its fixed
buffer.
In `@tests/unit/tls.tcl`:
- Around line 806-859: Add a bounded timeout to the raw socket read in the TLS
regression test, replacing the blocking read in the read_failed catch block with
nonblocking/fileevent handling and an after-based timeout. Ensure a stalled
connection sets the failure condition and allows the test to continue to the
existing crash/EOF validation instead of hanging indefinitely.
---
Nitpick comments:
In `@src/tls.c`:
- Around line 1716-1743: Extract the buffer-coalescing and retry-length
validation from the connTLSWritev flow into a small TLS-independent helper,
preserving the NET_MAX_WRITES_PER_EVENT cap and last_failed_write_data_len
check. Add a C++ GoogleTest under src/unit/ covering exact-capacity writes,
multi-iovec concatenation, and retry lengths that are exactly satisfied or
unsatisfied.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 660b8ccf-8ce5-4ae9-af20-7f1dcc12db98
📒 Files selected for processing (5)
src/debug.csrc/server.csrc/server.hsrc/tls.ctests/unit/tls.tcl
371517f to
2a77f81
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/debug.c`:
- Around line 1097-1099: Update the DEBUG FORCE-TLS-WRITE-ERROR branch in the
command handler to parse c->argv[2] with getLongFromObjectOrReply, reject values
other than 0 or 1 with an error reply, and only then assign
server.debug_force_tls_write_error and return success.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 95a972ec-1776-4919-b471-a0cefc9ca402
📒 Files selected for processing (5)
src/debug.csrc/server.csrc/server.hsrc/tls.ctests/unit/tls.tcl
🚧 Files skipped from review as they are similar to previous changes (4)
- src/server.c
- src/server.h
- tests/unit/tls.tcl
- src/tls.c
2a77f81 to
cc07c94
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/debug.c`:
- Around line 1097-1099: Update the DEBUG HELP command listing to include
force-tls-write-error, documenting its argument syntax and purpose consistently
with the existing entries. Locate the help array or DEBUG HELP response
definition near the other debug command descriptions, and update related command
documentation if that listing is maintained separately.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: b8072338-00b1-468f-a89c-653b212423ac
📒 Files selected for processing (5)
src/debug.csrc/server.csrc/server.hsrc/tls.ctests/unit/tls.tcl
🚧 Files skipped from review as they are similar to previous changes (4)
- src/server.h
- src/server.c
- src/tls.c
- tests/unit/tls.tcl
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/debug.c`:
- Around line 1099-1101: Update the FORCE-TLS-WRITE-ERROR branch in the debug
command handler to parse the argument with the bounded 64-bit reply parser
instead of atoi, reject parse failures and values other than 0 or 1, and only
then assign server.debug_force_tls_write_error and return the success reply.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: dc21ce05-7be7-4c36-bf4f-36a6bb30eeb2
📒 Files selected for processing (5)
src/debug.csrc/server.csrc/server.hsrc/tls.ctests/unit/tls.tcl
🚧 Files skipped from review as they are similar to previous changes (4)
- src/server.h
- src/server.c
- tests/unit/tls.tcl
- src/tls.c
There was a problem hiding this comment.
The bounded-copy approach looks right: consolidated writes are now capped at 64 KB and the leftover is handled as a partial write by writevToClient, and the "retry must be at least as large as the last failed write" invariant still holds because any last_failed_write_data_len above 64 KB can only come from a single-iovec write in the one-by-one path, which is still iov[0] on the retry.
Two things to address: the new fallback guard's failure mode, and the fact that the regression test can go green without ever reaching the fixed path.
b4b40d3 to
b789a2e
Compare
b789a2e to
dc0fa68
Compare
Problem
The bug was originally introduced by #1737
In tls.c, we normally avoid consolidating IO vectors into a buffer if they exceed a capped amount (
NET_MAX_WRITES_PER_EVENT, which is 64KB). However, on OpenSSL write errors, we must never trigger a subsequent write that is smaller than the previous failed write. In this path, we must consolidate the IO vectors to ensure the write is at least as large as the last write we made on the socket:However,
iov_bytes_lencan easily exceed the stack boundaries if one of the IO vectors is large (e.g. a large key read), leading to a crash:Fix
To fix this, we simply avoid allocating large buffers on the stack by using a fixed-size stack buffer
char buf[NET_MAX_WRITES_PER_EVENT](64KB) and performing partial copies:iovblocks to decide if we can use one-by-one writing.NET_MAX_WRITES_PER_EVENT(64KB) andiov[0]is large enough to satisfylast_failed_write_data_len, write blocks sequentially one-by-one.iovinto a fixed-size stack buffer ofNET_MAX_WRITES_PER_EVENT(64KB), capping the copy at 64KB (partial copy if it exceeds).last_failed_write_data_lento satisfy OpenSSL retry constraint.Important: this should keep the existing "write must be larger than last failed write" invariant, considering: 1) if the last write went to the consolidated buffer path, it would be at most
NET_MAX_WRITES_PER_EVENT2) if the last write did not go through the consolidated buffer path, it will be at the front of theiov, and therefore ouriov[0] >= last_failed_write_data_lencheck will always pass (it is the same write).Finally, since we added IO threaded TLS writes, the stack limit may be even smaller for the thread (depending on the system). We work around this in bio threads, and I am copying that workaround to IO threads to ensure the stack is not smaller than
NET_MAX_WRITES_PER_EVENT.Testing
A regression test was appended to the existing TLS test suite in tests/unit/tls.tcl. We need to add a new debug subcommand
DEBUG FORCE-TLS-WRITE-ERROR <0 or 1>to deterministically trigger OpenSSL write failures. With this flow, we write a command with a small response and force it to trigger an IO error, then we write a command with a larger response and resolve the IO errors. The response to the large command will accumulate in the buffer, and we will attempt to write out the full output buffer for the client, which now includes a massive final IO vector. In the old code, this would trigger a stack overflow as we try to allocate O(megabytes) on the stack.