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

Fix the usrsctp vulnerability by using a global map for SctpAssociations (#439)

Fix the usrsctp vulnerability by using a global map for SctpAssociations instead of passing this pointer to usrsctp API

- Related commit in libwebrtc: https://webrtc-review.googlesource.com/c/src/+/176422/5/media/sctp/sctp_transport.cc
- Related commit in Janus: https://github.com/meetecho/janus-gateway/pull/2302/
上级 fa726bb9
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
#define MS_DEP_USRSCTP_HPP #define MS_DEP_USRSCTP_HPP
#include "common.hpp" #include "common.hpp"
#include "RTC/SctpAssociation.hpp"
#include "handles/Timer.hpp" #include "handles/Timer.hpp"
#include <unordered_map>
class DepUsrSCTP class DepUsrSCTP
{ {
...@@ -29,12 +31,16 @@ private: ...@@ -29,12 +31,16 @@ private:
public: public:
static void ClassInit(); static void ClassInit();
static void ClassDestroy(); static void ClassDestroy();
static void IncreaseSctpAssociations(); static uintptr_t GetNextSctpAssociationId();
static void DecreaseSctpAssociations(); static void RegisterSctpAssociation(RTC::SctpAssociation* sctpAssociation);
static void DeregisterSctpAssociation(RTC::SctpAssociation* sctpAssociation);
static RTC::SctpAssociation* RetrieveSctpAssociation(uintptr_t id);
private: private:
static Checker* checker; static Checker* checker;
static uint64_t numSctpAssociations; static uint64_t numSctpAssociations;
static uintptr_t nextSctpAssociationId;
static std::unordered_map<uintptr_t, RTC::SctpAssociation*> mapIdSctpAssociation;
}; };
#endif #endif
...@@ -96,6 +96,9 @@ namespace RTC ...@@ -96,6 +96,9 @@ namespace RTC
uint16_t streamId, uint16_t ssn, uint32_t ppid, int flags, const uint8_t* data, size_t len); uint16_t streamId, uint16_t ssn, uint32_t ppid, int flags, const uint8_t* data, size_t len);
void OnUsrSctpReceiveSctpNotification(union sctp_notification* notification, size_t len); void OnUsrSctpReceiveSctpNotification(union sctp_notification* notification, size_t len);
public:
uintptr_t id{ 0u };
private: private:
// Passed by argument. // Passed by argument.
Listener* listener{ nullptr }; Listener* listener{ nullptr };
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#include "DepUsrSCTP.hpp" #include "DepUsrSCTP.hpp"
#include "DepLibUV.hpp" #include "DepLibUV.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include "RTC/SctpAssociation.hpp"
#include <usrsctp.h> #include <usrsctp.h>
/* Static. */ /* Static. */
...@@ -15,10 +14,14 @@ static constexpr size_t CheckerInterval{ 10u }; // In ms. ...@@ -15,10 +14,14 @@ static constexpr size_t CheckerInterval{ 10u }; // In ms.
inline static int onSendSctpData(void* addr, void* data, size_t len, uint8_t /*tos*/, uint8_t /*setDf*/) inline static int onSendSctpData(void* addr, void* data, size_t len, uint8_t /*tos*/, uint8_t /*setDf*/)
{ {
auto* sctpAssociation = static_cast<RTC::SctpAssociation*>(addr); auto* sctpAssociation = DepUsrSCTP::RetrieveSctpAssociation(reinterpret_cast<uintptr_t>(addr));
if (!sctpAssociation) if (!sctpAssociation)
{
MS_WARN_TAG(sctp, "no SctpAssociation found");
return -1; return -1;
}
sctpAssociation->OnUsrSctpSendSctpData(data, len); sctpAssociation->OnUsrSctpSendSctpData(data, len);
...@@ -48,6 +51,8 @@ inline static void sctpDebug(const char* format, ...) ...@@ -48,6 +51,8 @@ inline static void sctpDebug(const char* format, ...)
DepUsrSCTP::Checker* DepUsrSCTP::checker{ nullptr }; DepUsrSCTP::Checker* DepUsrSCTP::checker{ nullptr };
uint64_t DepUsrSCTP::numSctpAssociations{ 0u }; uint64_t DepUsrSCTP::numSctpAssociations{ 0u };
uintptr_t DepUsrSCTP::nextSctpAssociationId{ 0u };
std::unordered_map<uintptr_t, RTC::SctpAssociation*> DepUsrSCTP::mapIdSctpAssociation;
/* Static methods. */ /* Static methods. */
...@@ -78,24 +83,69 @@ void DepUsrSCTP::ClassDestroy() ...@@ -78,24 +83,69 @@ void DepUsrSCTP::ClassDestroy()
delete DepUsrSCTP::checker; delete DepUsrSCTP::checker;
} }
void DepUsrSCTP::IncreaseSctpAssociations() uintptr_t DepUsrSCTP::GetNextSctpAssociationId()
{
MS_TRACE();
// NOTE: usrsctp_connect() fails with a value of 0.
if (DepUsrSCTP::nextSctpAssociationId == 0u)
++DepUsrSCTP::nextSctpAssociationId;
// In case we've wrapped around and need to find an empty spot from a removed
// SctpAssociation. Assumes we'll never be full.
while (DepUsrSCTP::mapIdSctpAssociation.find(DepUsrSCTP::nextSctpAssociationId) !=
DepUsrSCTP::mapIdSctpAssociation.end())
{
++DepUsrSCTP::nextSctpAssociationId;
if (DepUsrSCTP::nextSctpAssociationId == 0u)
++DepUsrSCTP::nextSctpAssociationId;
}
return DepUsrSCTP::nextSctpAssociationId++;
}
void DepUsrSCTP::RegisterSctpAssociation(RTC::SctpAssociation* sctpAssociation)
{ {
MS_TRACE(); MS_TRACE();
auto it = DepUsrSCTP::mapIdSctpAssociation.find(sctpAssociation->id);
MS_ASSERT(
it == DepUsrSCTP::mapIdSctpAssociation.end(),
"the id of the SctpAssociation is already in the map");
DepUsrSCTP::mapIdSctpAssociation[sctpAssociation->id] = sctpAssociation;
if (++DepUsrSCTP::numSctpAssociations == 1u) if (++DepUsrSCTP::numSctpAssociations == 1u)
DepUsrSCTP::checker->Start(); DepUsrSCTP::checker->Start();
} }
void DepUsrSCTP::DecreaseSctpAssociations() void DepUsrSCTP::DeregisterSctpAssociation(RTC::SctpAssociation* sctpAssociation)
{ {
MS_TRACE(); MS_TRACE();
auto found = DepUsrSCTP::mapIdSctpAssociation.erase(sctpAssociation->id);
MS_ASSERT(found > 0, "SctpAssociation not found");
MS_ASSERT(DepUsrSCTP::numSctpAssociations > 0u, "numSctpAssociations was not higher than 0"); MS_ASSERT(DepUsrSCTP::numSctpAssociations > 0u, "numSctpAssociations was not higher than 0");
if (--DepUsrSCTP::numSctpAssociations == 0u) if (--DepUsrSCTP::numSctpAssociations == 0u)
DepUsrSCTP::checker->Stop(); DepUsrSCTP::checker->Stop();
} }
RTC::SctpAssociation* DepUsrSCTP::RetrieveSctpAssociation(uintptr_t id)
{
MS_TRACE();
auto it = DepUsrSCTP::mapIdSctpAssociation.find(id);
if (it == DepUsrSCTP::mapIdSctpAssociation.end())
return nullptr;
return it->second;
}
/* DepUsrSCTP::Checker instance methods. */ /* DepUsrSCTP::Checker instance methods. */
DepUsrSCTP::Checker::Checker() DepUsrSCTP::Checker::Checker()
......
...@@ -37,10 +37,12 @@ inline static int onRecvSctpData( ...@@ -37,10 +37,12 @@ inline static int onRecvSctpData(
int flags, int flags,
void* ulpInfo) void* ulpInfo)
{ {
auto* sctpAssociation = static_cast<RTC::SctpAssociation*>(ulpInfo); auto* sctpAssociation = DepUsrSCTP::RetrieveSctpAssociation(reinterpret_cast<uintptr_t>(ulpInfo));
if (!sctpAssociation) if (!sctpAssociation)
{ {
MS_WARN_TAG(sctp, "no SctpAssociation found");
std::free(data); std::free(data);
return 0; return 0;
...@@ -99,24 +101,34 @@ namespace RTC ...@@ -99,24 +101,34 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// Get a id for this SctpAssociation.
this->id = DepUsrSCTP::GetNextSctpAssociationId();
// Register ourselves in usrsctp. // Register ourselves in usrsctp.
usrsctp_register_address(static_cast<void*>(this)); // NOTE: This must be done before calling usrsctp_bind().
usrsctp_register_address(reinterpret_cast<void*>(this->id));
int ret; int ret;
this->socket = usrsctp_socket( this->socket = usrsctp_socket(
AF_CONN, SOCK_STREAM, IPPROTO_SCTP, onRecvSctpData, nullptr, 0, static_cast<void*>(this)); AF_CONN, SOCK_STREAM, IPPROTO_SCTP, onRecvSctpData, nullptr, 0, reinterpret_cast<void*>(this->id));
if (!this->socket) if (!this->socket)
{
usrsctp_deregister_address(reinterpret_cast<void*>(this->id));
MS_THROW_ERROR("usrsctp_socket() failed: %s", std::strerror(errno)); MS_THROW_ERROR("usrsctp_socket() failed: %s", std::strerror(errno));
}
usrsctp_set_ulpinfo(this->socket, static_cast<void*>(this)); usrsctp_set_ulpinfo(this->socket, reinterpret_cast<void*>(this->id));
// Make the socket non-blocking. // Make the socket non-blocking.
ret = usrsctp_set_non_blocking(this->socket, 1); ret = usrsctp_set_non_blocking(this->socket, 1);
if (ret < 0) if (ret < 0)
{ {
usrsctp_deregister_address(reinterpret_cast<void*>(this->id));
MS_THROW_ERROR("usrsctp_set_non_blocking() failed: %s", std::strerror(errno)); MS_THROW_ERROR("usrsctp_set_non_blocking() failed: %s", std::strerror(errno));
} }
...@@ -133,6 +145,8 @@ namespace RTC ...@@ -133,6 +145,8 @@ namespace RTC
if (ret < 0) if (ret < 0)
{ {
usrsctp_deregister_address(reinterpret_cast<void*>(this->id));
MS_THROW_ERROR("usrsctp_setsockopt(SO_LINGER) failed: %s", std::strerror(errno)); MS_THROW_ERROR("usrsctp_setsockopt(SO_LINGER) failed: %s", std::strerror(errno));
} }
...@@ -146,6 +160,8 @@ namespace RTC ...@@ -146,6 +160,8 @@ namespace RTC
if (ret < 0) if (ret < 0)
{ {
usrsctp_deregister_address(reinterpret_cast<void*>(this->id));
MS_THROW_ERROR("usrsctp_setsockopt(SCTP_ENABLE_STREAM_RESET) failed: %s", std::strerror(errno)); MS_THROW_ERROR("usrsctp_setsockopt(SCTP_ENABLE_STREAM_RESET) failed: %s", std::strerror(errno));
} }
...@@ -156,6 +172,8 @@ namespace RTC ...@@ -156,6 +172,8 @@ namespace RTC
if (ret < 0) if (ret < 0)
{ {
usrsctp_deregister_address(reinterpret_cast<void*>(this->id));
MS_THROW_ERROR("usrsctp_setsockopt(SCTP_NODELAY) failed: %s", std::strerror(errno)); MS_THROW_ERROR("usrsctp_setsockopt(SCTP_NODELAY) failed: %s", std::strerror(errno));
} }
...@@ -173,6 +191,8 @@ namespace RTC ...@@ -173,6 +191,8 @@ namespace RTC
if (ret < 0) if (ret < 0)
{ {
usrsctp_deregister_address(reinterpret_cast<void*>(this->id));
MS_THROW_ERROR("usrsctp_setsockopt(SCTP_EVENT) failed: %s", std::strerror(errno)); MS_THROW_ERROR("usrsctp_setsockopt(SCTP_EVENT) failed: %s", std::strerror(errno));
} }
} }
...@@ -188,6 +208,8 @@ namespace RTC ...@@ -188,6 +208,8 @@ namespace RTC
if (ret < 0) if (ret < 0)
{ {
usrsctp_deregister_address(reinterpret_cast<void*>(this->id));
MS_THROW_ERROR("usrsctp_setsockopt(SCTP_INITMSG) failed: %s", std::strerror(errno)); MS_THROW_ERROR("usrsctp_setsockopt(SCTP_INITMSG) failed: %s", std::strerror(errno));
} }
...@@ -197,26 +219,31 @@ namespace RTC ...@@ -197,26 +219,31 @@ namespace RTC
std::memset(&sconn, 0, sizeof(sconn)); std::memset(&sconn, 0, sizeof(sconn));
sconn.sconn_family = AF_CONN; sconn.sconn_family = AF_CONN;
sconn.sconn_port = htons(5000); sconn.sconn_port = htons(5000);
sconn.sconn_addr = static_cast<void*>(this); sconn.sconn_addr = reinterpret_cast<void*>(this->id);
#ifdef HAVE_SCONN_LEN #ifdef HAVE_SCONN_LEN
rconn.sconn_len = sizeof(sconn); sconn.sconn_len = sizeof(sconn);
#endif #endif
ret = usrsctp_bind(this->socket, reinterpret_cast<struct sockaddr*>(&sconn), sizeof(sconn)); ret = usrsctp_bind(this->socket, reinterpret_cast<struct sockaddr*>(&sconn), sizeof(sconn));
if (ret < 0) if (ret < 0)
{ {
usrsctp_deregister_address(reinterpret_cast<void*>(this->id));
MS_THROW_ERROR("usrsctp_bind() failed: %s", std::strerror(errno)); MS_THROW_ERROR("usrsctp_bind() failed: %s", std::strerror(errno));
} }
DepUsrSCTP::IncreaseSctpAssociations();
auto bufferSize = static_cast<int>(sctpSendBufferSize); auto bufferSize = static_cast<int>(sctpSendBufferSize);
if (usrsctp_setsockopt(this->socket, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(int)) < 0) if (usrsctp_setsockopt(this->socket, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(int)) < 0)
{ {
usrsctp_deregister_address(reinterpret_cast<void*>(this->id));
MS_THROW_ERROR("usrsctp_setsockopt(SO_SNDBUF) failed: %s", std::strerror(errno)); MS_THROW_ERROR("usrsctp_setsockopt(SO_SNDBUF) failed: %s", std::strerror(errno));
} }
// Register the SctpAssociation into the global map.
DepUsrSCTP::RegisterSctpAssociation(this);
} }
SctpAssociation::~SctpAssociation() SctpAssociation::~SctpAssociation()
...@@ -227,9 +254,10 @@ namespace RTC ...@@ -227,9 +254,10 @@ namespace RTC
usrsctp_close(this->socket); usrsctp_close(this->socket);
// Deregister ourselves from usrsctp. // Deregister ourselves from usrsctp.
usrsctp_deregister_address(static_cast<void*>(this)); usrsctp_deregister_address(reinterpret_cast<void*>(this->id));
DepUsrSCTP::DecreaseSctpAssociations(); // Register the SctpAssociation from the global map.
DepUsrSCTP::DeregisterSctpAssociation(this);
delete[] this->messageBuffer; delete[] this->messageBuffer;
} }
...@@ -250,7 +278,7 @@ namespace RTC ...@@ -250,7 +278,7 @@ namespace RTC
std::memset(&rconn, 0, sizeof(rconn)); std::memset(&rconn, 0, sizeof(rconn));
rconn.sconn_family = AF_CONN; rconn.sconn_family = AF_CONN;
rconn.sconn_port = htons(5000); rconn.sconn_port = htons(5000);
rconn.sconn_addr = static_cast<void*>(this); rconn.sconn_addr = reinterpret_cast<void*>(this->id);
#ifdef HAVE_SCONN_LEN #ifdef HAVE_SCONN_LEN
rconn.sconn_len = sizeof(rconn); rconn.sconn_len = sizeof(rconn);
#endif #endif
...@@ -316,7 +344,7 @@ namespace RTC ...@@ -316,7 +344,7 @@ namespace RTC
MS_DUMP_DATA(data, len); MS_DUMP_DATA(data, len);
#endif #endif
usrsctp_conninput(static_cast<void*>(this), data, len, 0); usrsctp_conninput(reinterpret_cast<void*>(this->id), data, len, 0);
} }
void SctpAssociation::SendSctpMessage( void SctpAssociation::SendSctpMessage(
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论