-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Heap-Use-After-Free in TCPBase::CloseActiveConnections During Shutdown #36731
Labels
Comments
pidarped
added a commit
to pidarped/connectedhomeip
that referenced
this issue
Dec 18, 2024
Add CloseActiveConnections() call in TCPBase::Close(), which is called as part of Server::Shutdown(). Active connections should be closed as part of Server shutdown. This allows the TCPConnectionState to also close the associated TCPEndpoint object as part of this shutdown flow. Previously, the CloseActiveConnections() call was present in the TCPBase destructor alone.
pidarped
added a commit
to pidarped/connectedhomeip
that referenced
this issue
Dec 18, 2024
Add CloseActiveConnections() call in TCPBase::Close(), which is called as part of Server::Shutdown(). Active connections should be closed as part of Server shutdown. This allows the TCPConnectionState to also close the associated TCPEndpoint object as part of this shutdown flow. Previously, the CloseActiveConnections() call was present in the TCPBase destructor alone. Add test for Connection Close() and checking for TCPEndPoint.
pidarped
added a commit
to pidarped/connectedhomeip
that referenced
this issue
Dec 19, 2024
Add CloseActiveConnections() call in TCPBase::Close(), which is called as part of Server::Shutdown(). Active connections should be closed as part of Server shutdown. This allows the TCPConnectionState to also close the associated TCPEndpoint object as part of this shutdown flow. Previously, the CloseActiveConnections() call was present in the TCPBase destructor alone. Add test for Connection Close() and checking for TCPEndPoint.
mergify bot
pushed a commit
that referenced
this issue
Dec 19, 2024
Add CloseActiveConnections() call in TCPBase::Close(), which is called as part of Server::Shutdown(). Active connections should be closed as part of Server shutdown. This allows the TCPConnectionState to also close the associated TCPEndpoint object as part of this shutdown flow. Previously, the CloseActiveConnections() call was present in the TCPBase destructor alone. Add test for Connection Close() and checking for TCPEndPoint.
Merged
pidarped
added a commit
to pidarped/connectedhomeip
that referenced
this issue
Jan 30, 2025
Add CloseActiveConnections() call in TCPBase::Close(), which is called as part of Server::Shutdown(). Active connections should be closed as part of Server shutdown. This allows the TCPConnectionState to also close the associated TCPEndpoint object as part of this shutdown flow. Previously, the CloseActiveConnections() call was present in the TCPBase destructor alone. Add test for Connection Close() and checking for TCPEndPoint.
andy31415
pushed a commit
that referenced
this issue
Jan 31, 2025
* TCP tests: TC-SC-8.x - Use ArmFailsafe as cmd (#37313) * TCP tests: TC-SC-8.x - Use ArmFailsafe as cmd Also add top-level pics * Fix payload capability * Fix for Bug #36732 (#36879) Set the app_state callback object in the Connection state to null when the CASE session object is being cleared, on top of setting the inner callback methods to null. This prevents the callback object from being accessed later, when the connection is getting closed(after the CASE session has been set up and the session object no longer exists). * Fix for Bug #36731. (#36880) Add CloseActiveConnections() call in TCPBase::Close(), which is called as part of Server::Shutdown(). Active connections should be closed as part of Server shutdown. This allows the TCPConnectionState to also close the associated TCPEndpoint object as part of this shutdown flow. Previously, the CloseActiveConnections() call was present in the TCPBase destructor alone. Add test for Connection Close() and checking for TCPEndPoint. --------- Co-authored-by: C Freeman <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reproduction steps
Build
chip-all-clusters-app
with AddressSanitizer (ASAN) enabled.Open Terminal 1 and start the
chip-all-clusters-app
:Open Terminal 2 and run the following commands in
chip-tool
:$ ./chip-tool interactive start $ pairing onnetwork-long 1 20202021 3840 $ basicinformation read vendor-name 1 0 --allow-large-payload 1
After the TCP session is established, terminate
chip-all-clusters-app
in Terminal 1 withCtrl+C
.Observe the ASAN output of chip-all-clusters-app indicating a heap-use-after-free issue.
ASAN Log.txt
Summary
A heap-use-after-free (UAF) issue occurs during the cleanup phase in the Matter protocol's TCP transport layer. When a
TCPEndPoint
object is deallocated, its reference remains in theActiveTCPConnectionState
object. This dangling pointer leads to undefined behavior when the TCP connection termination logic accesses the invalid memory.Analysis and Description
The root cause of this issue lies in the mismatch between the lifecycle management of
TCPEndPoint
objects and their references inActiveTCPConnectionState
. The following sequence of events explains the issue:Allocation and Initialization:
TCPEndPoint
object is created and assigned to themEndPoint
member of anActiveTCPConnectionState
object during the TCP connection establishment process.Deallocation:
HeapObjectPool
destructor deallocates allTCPEndPoint
objects viaReleaseAll()
. However, theActiveTCPConnectionState
objects retain dangling pointers to these deallocatedTCPEndPoint
objects.Access to Freed Memory:
TCPBase::CloseActiveConnections()
is called, it iterates over theActiveTCPConnectionState
array. ThemEndPoint
pointer in these objects is used to call theClose()
method, resulting in a UAF error.ASAN Error:
TCPEndPoint::Close()
method when the code tries to clear the receive queue (mRcvQueue
), as shown in the ASAN log:Proposed Solution
The following solutions are proposed to address this issue:
Synchronize Lifecycles:
TCPEndPoint
objects are not deallocated whileActiveTCPConnectionState
references them.Use Smart Pointers:
std::shared_ptr
) inActiveTCPConnectionState
. This would allow the object to manage the lifetime of theTCPEndPoint
safely:std::shared_ptr<Inet::TCPEndPoint> mEndPoint;
Refactor Object Management:
ActiveTCPConnectionState
fromTCPEndPoint
objects. Use proxy objects or implement weak references to manage the dependency.By addressing this issue, the framework can prevent undefined behavior and potential security vulnerabilities. The proposed solutions not only resolve the immediate problem but also enhance the robustness and maintainability of the code.
If further information is needed, please do not hesitate to reach out.
Bug prevalence
always
GitHub hash of the SDK that was being used
ffbc362
Platform
core
Platform Version(s)
all versions with TCP support
Anything else?
No response
The text was updated successfully, but these errors were encountered: