You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After the TCP session is established, terminate chip-all-clusters-app in Terminal 1 with Ctrl+C.
Observe that chip-tool receives a FIN packet, triggering heap-use-after-free behavior. ASAN Log.txt
Summary
A heap-use-after-free (UAF) issue occurs when a CASESession object is deallocated but its callback remains referenced by the ActiveTCPConnectionState object. This issue introduces undefined behavior, potentially leading to security vulnerabilities under specific conditions.
Analysis and Description
The issue arises due to a mismatch in the lifecycle management of the CASESession and ActiveTCPConnectionState objects. The following sequence highlights the root cause:
Creation and Callback Setup:
During CASESession::EstablishSession, the AppTCPConnectionCallbackCtxt structure is initialized, and its connClosedCb member is set to CASESession::HandleConnectionClosed:
The ActiveTCPConnectionState object is initialized during TCPBase::StartConnect, and it retains a reference to the AppTCPConnectionCallbackCtxt structure via its mAppState member:
activeConnection->mAppState = appState;
CASESession Deallocation:
After the CASE session is successfully established, the CASESession object is deallocated. However, ActiveTCPConnectionState still holds a dangling reference to the AppTCPConnectionCallbackCtxt structure.
TCP Connection Termination:
When the TCP connection is terminated, the ActiveTCPConnectionState invokes the connClosedCb callback stored in its mAppState. Since the callback references the already-freed CASESession object, a heap-use-after-free occurs:
if (appTCPConnCbCtxt->connClosedCb != nullptr) {
appTCPConnCbCtxt->connClosedCb(conn, conErr);
}
This UAF condition results from the ActiveTCPConnectionState lifecycle extending beyond the CASESession lifecycle, causing undefined behavior. If the memory previously occupied by the CASESession is overwritten with controlled data, this could lead to arbitrary code execution.
Proposed Solution
Refactor Callback Handling:
Decouple the lifecycle of CASESession from ActiveTCPConnectionState using proxy objects or weak references.
By resolving this issue, the framework can eliminate the risk of undefined behavior and proactively mitigate potential security vulnerabilities, ensuring the robustness of the system.
If further information is needed, please do not hesitate to reach out.
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).
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).
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).
Reproduction steps
Build the
chip-tool
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 that
chip-tool
receives a FIN packet, triggering heap-use-after-free behavior.ASAN Log.txt
Summary
A heap-use-after-free (UAF) issue occurs when a
CASESession
object is deallocated but its callback remains referenced by theActiveTCPConnectionState
object. This issue introduces undefined behavior, potentially leading to security vulnerabilities under specific conditions.Analysis and Description
The issue arises due to a mismatch in the lifecycle management of the
CASESession
andActiveTCPConnectionState
objects. The following sequence highlights the root cause:Creation and Callback Setup:
During
CASESession::EstablishSession
, theAppTCPConnectionCallbackCtxt
structure is initialized, and itsconnClosedCb
member is set toCASESession::HandleConnectionClosed
:ActiveTCPConnectionState References CASESession:
The
ActiveTCPConnectionState
object is initialized duringTCPBase::StartConnect
, and it retains a reference to theAppTCPConnectionCallbackCtxt
structure via itsmAppState
member:activeConnection->mAppState = appState;
CASESession Deallocation:
After the CASE session is successfully established, the
CASESession
object is deallocated. However,ActiveTCPConnectionState
still holds a dangling reference to theAppTCPConnectionCallbackCtxt
structure.TCP Connection Termination:
When the TCP connection is terminated, the
ActiveTCPConnectionState
invokes theconnClosedCb
callback stored in itsmAppState
. Since the callback references the already-freedCASESession
object, a heap-use-after-free occurs:This UAF condition results from the
ActiveTCPConnectionState
lifecycle extending beyond theCASESession
lifecycle, causing undefined behavior. If the memory previously occupied by theCASESession
is overwritten with controlled data, this could lead to arbitrary code execution.Proposed Solution
CASESession
fromActiveTCPConnectionState
using proxy objects or weak references.By resolving this issue, the framework can eliminate the risk of undefined behavior and proactively mitigate potential security vulnerabilities, ensuring the robustness of the system.
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: