Repository navigation
Fix use-after-free in TLS pending connection iteration - #988
Open
KIMDONGYEON00 wants to merge 1 commit into
Open
KIMDONGYEON00 wants to merge 1 commit into
KIMDONGYEON00 wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a heap-use-after-free vulnerability in
tlsProcessPendingData()caused by unsafe iteration over the TLS pending connection list.The function uses a
listIterto traversepending_list. However,listNext()caches the next node pointer before invoking the TLS event handler.If the event handler processes a command such as
CLIENT KILL, another pending TLS connection can be closed and its list node freed. The iterator may then access the freed node, resulting in a heap-use-after-free.This issue was reproduced in a running KeyDB TLS server using AddressSanitizer (ASan).
Root Cause
The original implementation iterates over
pending_listas follows:The problem occurs because
listNext()stores a pointer to the next list node beforetlsHandleEvent()is called.During event processing, the following sequence can occur:
tlsProcessPendingData()retrieves a pending TLS connection and caches the next node pointer.tlsHandleEvent()invokes the connection's read handler.The read handler processes a
CLIENT KILLcommand targeting another pending TLS connection.freeClient()closes the targeted connection.connTLSCloseCore()removes and frees the targeted connection's node frompending_list.The next call to
listNext()dereferences the previously freed node, triggering a heap-use-after-free.The iterator is therefore unsafe when an event handler removes a different node from the same list.
Changes
Replace the iterator-based traversal with a bounded, head-based iteration.
The updated implementation:
Retrieves the first pending node using
listFirst()on each iteration.Removes the current node from
pending_listbefore invoking the event handler.Clears the connection's
pending_list_nodepointer.Limits processing to the number of pending connections observed at the beginning of the iteration.
Allows connections with remaining TLS data to be added back to the pending list by the existing event-handling logic.
This prevents the iteration from retaining a pointer to a list node that may be freed during event processing.
Validation
The vulnerability was reproduced and the fix was tested using an AddressSanitizer-enabled KeyDB build in an isolated Docker environment.
Test Environment
These results demonstrate that the patch prevents the reproduced use-after-free under the tested conditions while preserving TLS server responsiveness.
Security Impact
The vulnerability affects TLS connection processing when an event handler removes another connection from the pending list during iteration.
An invalidated list iterator may dereference freed memory, potentially resulting in:
Heap-use-after-free
Server crash
Denial of service
The issue was confirmed in an ASan-enabled KeyDB server. The observed impact was a heap-use-after-free and subsequent loss of server responsiveness.
No remote code execution or information disclosure was demonstrated.
References
Related upstream Redis fix: redis/redis@6d088c3
The full AddressSanitizer report and reproduction details can be provided privately to maintainers if needed.