Unverified 提交 fab4891c authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

Worker: Fix possible double free when ICE consent check fails (#1393)

上级 ea40829a
# Changelog # Changelog
### NEXT
- Worker: Fix possible double free when ICE consent check fails ([PR #1393](https://github.com/versatica/mediasoup/pull/1393)).
### 3.14.5 ### 3.14.5
- Worker: Fix memory leak when using `WebRtcServer` with TCP enabled ([PR #1389](https://github.com/versatica/mediasoup/pull/1389)). - Worker: Fix memory leak when using `WebRtcServer` with TCP enabled ([PR #1389](https://github.com/versatica/mediasoup/pull/1389)).
......
...@@ -131,7 +131,6 @@ namespace RTC ...@@ -131,7 +131,6 @@ namespace RTC
std::string password; std::string password;
uint16_t consentTimeoutMs{ 30000u }; uint16_t consentTimeoutMs{ 30000u };
// Others. // Others.
bool destroying{ false };
std::string oldUsernameFragment; std::string oldUsernameFragment;
std::string oldPassword; std::string oldPassword;
IceState state{ IceState::NEW }; IceState state{ IceState::NEW };
...@@ -140,6 +139,7 @@ namespace RTC ...@@ -140,6 +139,7 @@ namespace RTC
RTC::TransportTuple* selectedTuple{ nullptr }; RTC::TransportTuple* selectedTuple{ nullptr };
TimerHandle* consentCheckTimer{ nullptr }; TimerHandle* consentCheckTimer{ nullptr };
uint64_t lastConsentRequestReceivedAtMs{ 0u }; uint64_t lastConsentRequestReceivedAtMs{ 0u };
bool isRemovingTuples{ false };
}; };
} // namespace RTC } // namespace RTC
......
...@@ -112,8 +112,6 @@ namespace RTC ...@@ -112,8 +112,6 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
this->destroying = true;
// Here we must notify the listener about the removal of current // Here we must notify the listener about the removal of current
// usernameFragments (and also the old one if any) and all tuples. // usernameFragments (and also the old one if any) and all tuples.
...@@ -125,6 +123,8 @@ namespace RTC ...@@ -125,6 +123,8 @@ namespace RTC
} }
// Clear all tuples. // Clear all tuples.
this->isRemovingTuples = true;
for (const auto& it : this->tuples) for (const auto& it : this->tuples)
{ {
auto* storedTuple = const_cast<RTC::TransportTuple*>(std::addressof(it)); auto* storedTuple = const_cast<RTC::TransportTuple*>(std::addressof(it));
...@@ -133,6 +133,8 @@ namespace RTC ...@@ -133,6 +133,8 @@ namespace RTC
this->listener->OnIceServerTupleRemoved(this, storedTuple); this->listener->OnIceServerTupleRemoved(this, storedTuple);
} }
this->isRemovingTuples = false;
// Clear all tuples. // Clear all tuples.
// NOTE: Do it after notifying the listener since the listener may need to // NOTE: Do it after notifying the listener since the listener may need to
// use/read the tuple being removed so we cannot free it yet. // use/read the tuple being removed so we cannot free it yet.
...@@ -225,9 +227,10 @@ namespace RTC ...@@ -225,9 +227,10 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// While IceServer is being destroyed, it may call listener methods that may // If IceServer is removing a tuple or all tuples (for instance in the
// end calling RemoveTuple(). We must ignore it to avoid double-free issues. // destructor), the OnIceServerTupleRemoved() callback may end triggering
if (this->destroying) // new calls to RemoveTuple(). We must ignore it to avoid double-free issues.
if (this->isRemovingTuples)
{ {
return; return;
} }
...@@ -256,7 +259,9 @@ namespace RTC ...@@ -256,7 +259,9 @@ namespace RTC
} }
// Notify the listener. // Notify the listener.
this->isRemovingTuples = true;
this->listener->OnIceServerTupleRemoved(this, removedTuple); this->listener->OnIceServerTupleRemoved(this, removedTuple);
this->isRemovingTuples = false;
// Remove it from the list of tuples. // Remove it from the list of tuples.
// NOTE: Do it after notifying the listener since the listener may need to // NOTE: Do it after notifying the listener since the listener may need to
...@@ -810,7 +815,9 @@ namespace RTC ...@@ -810,7 +815,9 @@ namespace RTC
MS_ASSERT(removedTuple, "couldn't find any tuple to be removed"); MS_ASSERT(removedTuple, "couldn't find any tuple to be removed");
// Notify the listener. // Notify the listener.
this->isRemovingTuples = true;
this->listener->OnIceServerTupleRemoved(this, removedTuple); this->listener->OnIceServerTupleRemoved(this, removedTuple);
this->isRemovingTuples = false;
// Remove it from the list of tuples. // Remove it from the list of tuples.
// NOTE: Do it after notifying the listener since the listener may need to // NOTE: Do it after notifying the listener since the listener may need to
...@@ -927,6 +934,8 @@ namespace RTC ...@@ -927,6 +934,8 @@ namespace RTC
this->remoteNomination = 0u; this->remoteNomination = 0u;
// Clear all tuples. // Clear all tuples.
this->isRemovingTuples = true;
for (const auto& it : this->tuples) for (const auto& it : this->tuples)
{ {
auto* storedTuple = const_cast<RTC::TransportTuple*>(std::addressof(it)); auto* storedTuple = const_cast<RTC::TransportTuple*>(std::addressof(it));
...@@ -935,6 +944,8 @@ namespace RTC ...@@ -935,6 +944,8 @@ namespace RTC
this->listener->OnIceServerTupleRemoved(this, storedTuple); this->listener->OnIceServerTupleRemoved(this, storedTuple);
} }
this->isRemovingTuples = false;
// Clear all tuples. // Clear all tuples.
// NOTE: Do it after notifying the listener since the listener may need to // NOTE: Do it after notifying the listener since the listener may need to
// use/read the tuple being removed so we cannot free it yet. // use/read the tuple being removed so we cannot free it yet.
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论