Skip to content

Fix use-after-free in TLS pending connection iteration - #988

Open
KIMDONGYEON00 wants to merge 1 commit into
Snapchat:mainfrom
KIMDONGYEON00:fix/tls-pending-list-uaf
Open

KIMDONGYEON00 wants to merge 1 commit into
Snapchat:mainfrom
KIMDONGYEON00:fix/tls-pending-list-uaf

Conversation

@KIMDONGYEON00

Copy link
Copy Markdown

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 listIter to traverse pending_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_list as follows:

int tlsProcessPendingData() {
    listIter li;
    listNode *ln;
    serverAssert(!GlobalLocksAcquired());
int processed = listLength(pending_list);
listRewind(pending_list, &li);

while ((ln = listNext(&li))) {
    tls_connection *conn = (tls_connection*)listNodeValue(ln);
    tlsHandleEvent(conn, AE_READABLE);
}

return processed;

}

The problem occurs because listNext() stores a pointer to the next list node before tlsHandleEvent() is called.

During event processing, the following sequence can occur:

  1. tlsProcessPendingData() retrieves a pending TLS connection and caches the next node pointer.

  2. tlsHandleEvent() invokes the connection's read handler.

  3. The read handler processes a CLIENT KILL command targeting another pending TLS connection.

  4. freeClient() closes the targeted connection.

  5. connTLSCloseCore() removes and frees the targeted connection's node from pending_list.

  6. 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_list before invoking the event handler.

  • Clears the connection's pending_list_node pointer.

  • 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

Item Details
Project KeyDB
Original commit 603ebb2
Operating system Linux x86_64 (Docker)
Sanitizers AddressSanitizer and UndefinedBehaviorSanitizer
TLS Enabled
Affected file src/tls.cpp
Affected function tlsProcessPendingData()

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

The full AddressSanitizer report and reproduction details can be provided privately to maintainers if needed.

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