Unverified 提交 56b953dc authored 作者: José Luis Millán's avatar José Luis Millán 提交者: GitHub

SeqManager: fix crash and add fuzzer (#1045)

* SeqManager: fix crash and add fuzzer

Fixes #1042
上级 b6d23b72
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
* `RtpStreamSend`: Reset RTP retransmission buffer upon RTP sequence number reset ([PR #1041](https://github.com/versatica/mediasoup/pull/1041)). * `RtpStreamSend`: Reset RTP retransmission buffer upon RTP sequence number reset ([PR #1041](https://github.com/versatica/mediasoup/pull/1041)).
* `RtpRetransmissionBuffer`: Handle corner case in which received packet has lower seq than newest packet in the buffer but higher timestamp ([PR #1044](https://github.com/versatica/mediasoup/pull/1044)). * `RtpRetransmissionBuffer`: Handle corner case in which received packet has lower seq than newest packet in the buffer but higher timestamp ([PR #1044](https://github.com/versatica/mediasoup/pull/1044)).
* `SeqManager`: Fix crash and add fuzzer ([PR #1045](https://github.com/versatica/mediasoup/pull/1045)).
* Node: Make `appData` TS typed and writable ([PR #1046](https://github.com/versatica/mediasoup/pull/1046), credits to @mango-martin). * Node: Make `appData` TS typed and writable ([PR #1046](https://github.com/versatica/mediasoup/pull/1046), credits to @mango-martin).
......
#ifndef MS_FUZZER_RTC_SEQ_MANAGER_HPP
#define MS_FUZZER_RTC_SEQ_MANAGER_HPP
#include "common.hpp"
namespace Fuzzer
{
namespace RTC
{
namespace SeqManager
{
void Fuzz(const uint8_t* data, size_t len);
}
} // namespace RTC
} // namespace Fuzzer
#endif
#include "RTC/FuzzerSeqManager.hpp"
#include "Utils.hpp"
#include "RTC/SeqManager.hpp"
#include <iostream>
void Fuzzer::RTC::SeqManager::Fuzz(const uint8_t* data, size_t len)
{
if (len < 10)
{
return;
}
::RTC::SeqManager<uint16_t> seqManager;
uint16_t output;
for (size_t count = 0; count < 7; count++)
{
seqManager.Input(Utils::Byte::Get2Bytes(data, count), output);
seqManager.Drop(Utils::Byte::Get2Bytes(data, count + 2));
}
}
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "RTC/FuzzerRtpPacket.hpp" #include "RTC/FuzzerRtpPacket.hpp"
#include "RTC/FuzzerRtpRetransmissionBuffer.hpp" #include "RTC/FuzzerRtpRetransmissionBuffer.hpp"
#include "RTC/FuzzerRtpStreamSend.hpp" #include "RTC/FuzzerRtpStreamSend.hpp"
#include "RTC/FuzzerSeqManager.hpp"
#include "RTC/FuzzerStunPacket.hpp" #include "RTC/FuzzerStunPacket.hpp"
#include "RTC/FuzzerTrendCalculator.hpp" #include "RTC/FuzzerTrendCalculator.hpp"
#include "RTC/RTCP/FuzzerPacket.hpp" #include "RTC/RTCP/FuzzerPacket.hpp"
...@@ -45,6 +46,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t len) ...@@ -45,6 +46,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t len)
Fuzzer::RTC::RtpPacket::Fuzz(data, len); Fuzzer::RTC::RtpPacket::Fuzz(data, len);
Fuzzer::RTC::RtpStreamSend::Fuzz(data, len); Fuzzer::RTC::RtpStreamSend::Fuzz(data, len);
Fuzzer::RTC::RtpRetransmissionBuffer::Fuzz(data, len); Fuzzer::RTC::RtpRetransmissionBuffer::Fuzz(data, len);
Fuzzer::RTC::SeqManager::Fuzz(data, len);
} }
if (fuzzRtcp) if (fuzzRtcp)
......
...@@ -49,6 +49,8 @@ namespace RTC ...@@ -49,6 +49,8 @@ namespace RTC
void ClearDropped(); void ClearDropped();
private: private:
// Whether at least a sequence number has been inserted.
bool started{ false };
T base{ 0 }; T base{ 0 };
T maxOutput{ 0 }; T maxOutput{ 0 };
T maxInput{ 0 }; T maxInput{ 0 };
......
...@@ -356,6 +356,7 @@ executable( ...@@ -356,6 +356,7 @@ executable(
'fuzzer/src/RTC/FuzzerRtpPacket.cpp', 'fuzzer/src/RTC/FuzzerRtpPacket.cpp',
'fuzzer/src/RTC/FuzzerRtpRetransmissionBuffer.cpp', 'fuzzer/src/RTC/FuzzerRtpRetransmissionBuffer.cpp',
'fuzzer/src/RTC/FuzzerRtpStreamSend.cpp', 'fuzzer/src/RTC/FuzzerRtpStreamSend.cpp',
'fuzzer/src/RTC/FuzzerSeqManager.cpp',
'fuzzer/src/RTC/FuzzerTrendCalculator.cpp', 'fuzzer/src/RTC/FuzzerTrendCalculator.cpp',
'fuzzer/src/RTC/RTCP/FuzzerBye.cpp', 'fuzzer/src/RTC/RTCP/FuzzerBye.cpp',
'fuzzer/src/RTC/RTCP/FuzzerFeedbackPs.cpp', 'fuzzer/src/RTC/RTCP/FuzzerFeedbackPs.cpp',
......
...@@ -113,15 +113,29 @@ namespace RTC ...@@ -113,15 +113,29 @@ namespace RTC
output = (input + base) & MaxValue; output = (input + base) & MaxValue;
if (!this->started)
{
this->started = true;
this->maxInput = input;
this->maxOutput = output;
}
else
{
// New input is higher than the maximum seen. But less than acceptable units higher. // New input is higher than the maximum seen. But less than acceptable units higher.
// Keep it as the maximum seen. See Drop(). // Keep it as the maximum seen. See Drop().
if (IsSeqHigherThan(input, this->maxInput)) if (IsSeqHigherThan(input, this->maxInput))
{
this->maxInput = input; this->maxInput = input;
}
// New output is higher than the maximum seen. But less than acceptable units higher. // New output is higher than the maximum seen. But less than acceptable units higher.
// Keep it as the maximum seen. See Sync(). // Keep it as the maximum seen. See Sync().
if (IsSeqHigherThan(output, this->maxOutput)) if (IsSeqHigherThan(output, this->maxOutput))
{
this->maxOutput = output; this->maxOutput = output;
}
}
ClearDropped(); ClearDropped();
...@@ -171,9 +185,21 @@ namespace RTC ...@@ -171,9 +185,21 @@ namespace RTC
} }
// There are many values in the range. // There are many values in the range.
else else
{
// Measure the distance of it1 and it2 to the beggining of dropped.
auto distanceIt1 = std::distance(this->dropped.begin(), it1);
auto distanceIt2 = std::distance(this->dropped.begin(), it2);
// it2 goes out of range, only it1 is within the range.
if (distanceIt2 < distanceIt1)
{
this->dropped.erase(it1);
}
else
{ {
this->dropped.erase(it1, it2); this->dropped.erase(it1, it2);
} }
}
// Adapt base. // Adapt base.
this->base = (this->base - (previousDroppedSize - this->dropped.size())) & MaxValue; this->base = (this->base - (previousDroppedSize - this->dropped.size())) & MaxValue;
......
...@@ -649,4 +649,83 @@ SCENARIO("SeqManager", "[rtc][SeqMananger]") ...@@ -649,4 +649,83 @@ SCENARIO("SeqManager", "[rtc][SeqMananger]")
SeqManager<uint8_t, 3> seqManager; SeqManager<uint8_t, 3> seqManager;
validate(seqManager, inputs); validate(seqManager, inputs);
} }
SECTION("dropped inputs to be removed going out of range, 1.")
{
// clang-format off
std::vector<TestSeqManagerInput<uint16_t>> inputs =
{
{ 36964, 36964, false, false, 0 },
{ 25923, 0, false, true, 0 }, // Drop.
{ 25701, 25701, false, false, 0 },
{ 17170, 0, false, true, 0 }, // Drop.
{ 25923, 25923, false, false, 0 },
{ 4728, 0, false, true, 0 }, // Drop.
{ 17170, 17170, false, false, 0 },
{ 30738, 0, false, true, 0 }, // Drop.
{ 4728, 4728, false, false, 0 },
{ 4806, 0, false, true, 0 }, // Drop.
{ 30738, 30738, false, false, 0 },
{ 50886, 0, false, true, 0 }, // Drop.
{ 4806, 4805, false, false, 0 }, // Previously dropped.
{ 50774, 0, false, true, 0 }, // Drop.
{ 50886, 50884, false, false, 0 }, // Previously dropped.
{ 22136, 0, false, true, 0 }, // Drop.
{ 50774, 50884, false, false, 0 }, // Previously dropped.
{ 30910, 0, false, true, 0 }, // Drop.
{ 22136, 22134, false, false, 0 },
{ 48862, 0, false, true, 0 }, // Drop.
{ 30910, 30909, false, false, 0 },
{ 56832, 0, false, true, 0 }, // Drop.
{ 48862, 48861, false, false, 0 },
{ 2, 0, false, true, 0 }, // Drop.
{ 56832, 56828, false, false, 0 },
{ 530, 0, false, true, 0 }, // Drop.
{ 2, 65534, false, false, 0 },
};
// clang-format on
SeqManager<uint16_t> seqManager;
validate(seqManager, inputs);
}
SECTION("dropped inputs to be removed go out of range, 2.")
{
// clang-format off
std::vector<TestSeqManagerInput<uint16_t>> inputs =
{
{ 36960, 36960, false, false, 0 },
{ 3328, 0, false, true, 0 }, // Drop.
{ 24589, 24588, false, false, 0 },
{ 120, 0, false, true, 0 }, // Drop.
{ 3328, 3326, false, false, 0 },
{ 30848, 0, false, true, 0 }, // Drop.
{ 120, 3326, false, false, 0 }, // Previously dropped.
};
// clang-format on
SeqManager<uint16_t> seqManager;
validate(seqManager, inputs);
}
SECTION("dropped inputs to be removed go out of range, 3.")
{
// clang-format off
std::vector<TestSeqManagerInput<uint16_t>> inputs =
{
{ 36964, 36964, false, false, 0 },
{ 65396 , 0, false, true, 0 }, // Drop.
{ 25855, 25854, false, false, 0 },
{ 29793 , 0, false, true, 0 }, // Drop.
{ 65396, 65395, false, false, 0 },
{ 25087, 0, false, true, 0 }, // Drop.
{ 29793, 29791, false, false, 0 },
{ 65535 , 0, false, true, 0 }, // Drop.
{ 25087, 29791, false, false, 0 }, // Previously dropped.
};
// clang-format on
SeqManager<uint16_t> seqManager;
validate(seqManager, inputs);
}
} }
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论