Skip to content

Commit

Permalink
Fix EC Delegate use-after-free in IM (#20141)
Browse files Browse the repository at this point in the history
If an error was encountered parsing the SubscribeResponse message,
ReadClient::OnMessageReceived would just null-out the EC pointer but not
the delegate pointer within the EC. This meant that when we got back to
the exchange management layer after unwinding the stack, it attempted to
call OnExchangeClosing on the delegate that had by then, been free'ed as
part of cleaning up the ReadClient object.
  • Loading branch information
mrjerryjohns authored and pull[bot] committed Nov 9, 2023
1 parent 0090300 commit 285d508
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/app/ReadClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,14 @@ CHIP_ERROR ReadClient::OnMessageReceived(Messaging::ExchangeContext * apExchange
{
VerifyOrExit(apExchangeContext == mpExchangeCtx, err = CHIP_ERROR_INCORRECT_STATE);
err = ProcessSubscribeResponse(std::move(aPayload));
SuccessOrExit(err);

// Forget the context as SUBSCRIBE RESPONSE is the last message in SUBSCRIBE transaction and
// ExchangeContext::HandleMessage automatically closes a context if no other messages need to
// be sent or received.
//
// Null out the delegate and context as SubscribeResponse is the last message the Subscribe transaction and
// the exchange layer will automatically close the exchange.
//
mpExchangeCtx->SetDelegate(nullptr);
mpExchangeCtx = nullptr;
SuccessOrExit(err);
}
else if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::StatusResponse))
{
Expand Down

0 comments on commit 285d508

Please sign in to comment.