diff --git a/webrtc/RtpExt.cpp b/webrtc/RtpExt.cpp
index b9a405d6..b0a4cdc6 100644
--- a/webrtc/RtpExt.cpp
+++ b/webrtc/RtpExt.cpp
@@ -172,13 +172,24 @@ RtpExt::operator std::string() const{
return string(_data, _size);
}
-map RtpExt::getExtValue(const RtpHeader *header) {
+map RtpExt::getExtValue(const RtpHeader *header, size_t len) {
map ret;
assert(header);
+ // 固定头(12字节) + csrc; 读取扩展头前必须先保证其4字节(reserved + length)落在实际数据报内,
+ // 否则包内声明的扩展长度不可信, 会导致 getExtSize() 自身及后续遍历发生堆越界读写
+ size_t offset = RtpPacket::kRtpHeaderSize + header->getCsrcSize();
+ if (!header->ext || offset + 4 > len) {
+ return ret;
+ }
auto ext_size = header->getExtSize();
if (!ext_size) {
return ret;
}
+ // 声明的扩展数据长度必须落在实际数据报内, 防止 appendExt 朝攻击者声明的越界末尾遍历
+ if (offset + 4 + ext_size > len) {
+ WarnL << "drop rtp with invalid ext, declared ext_size:" << ext_size << ", available:" << len - offset - 4;
+ return ret;
+ }
auto reserved = header->getExtReserved();
auto ptr = const_cast(header)->getExtData();
auto end = ptr + ext_size;
@@ -580,10 +591,10 @@ void RtpExtContext::setRid(uint32_t ssrc, const string &rid) {
_ssrc_to_rid[ssrc] = rid;
}
-RtpExt RtpExtContext::changeRtpExtId(const RtpHeader *header, bool is_recv, string *rid_ptr, RtpExtType type) {
+RtpExt RtpExtContext::changeRtpExtId(const RtpHeader *header, size_t len, bool is_recv, string *rid_ptr, RtpExtType type) {
string rid, repaired_rid;
RtpExt ret;
- auto ext_map = RtpExt::getExtValue(header);
+ auto ext_map = RtpExt::getExtValue(header, len);
for (auto &pr : ext_map) {
if (is_recv) {
auto it = _rtp_ext_id_to_type.find(pr.first);
diff --git a/webrtc/RtpExt.h b/webrtc/RtpExt.h
index 438840fe..bea642a6 100644
--- a/webrtc/RtpExt.h
+++ b/webrtc/RtpExt.h
@@ -55,7 +55,8 @@ public:
friend void appendExt(std::map &ret, uint8_t *ptr, const uint8_t *end);
friend class RtpExtContext;
- static std::map getExtValue(const RtpHeader *header);
+ // len为rtp数据报实际长度, 用于校验包内声明的扩展长度, 防止堆越界读写
+ static std::map getExtValue(const RtpHeader *header, size_t len);
static RtpExtType getExtType(const std::string &url);
static const std::string& getExtUrl(RtpExtType type);
static const char *getExtName(RtpExtType type);
@@ -123,7 +124,8 @@ public:
void setOnGetRtp(OnGetRtp cb);
std::string getRid(uint32_t ssrc) const;
void setRid(uint32_t ssrc, const std::string &rid);
- RtpExt changeRtpExtId(const RtpHeader *header, bool is_recv, std::string *rid_ptr = nullptr, RtpExtType type = RtpExtType::padding);
+ // len为rtp数据报实际长度, 用于校验包内声明的扩展长度, 防止堆越界读写
+ RtpExt changeRtpExtId(const RtpHeader *header, size_t len, bool is_recv, std::string *rid_ptr = nullptr, RtpExtType type = RtpExtType::padding);
private:
void onGetRtp(uint8_t pt, uint32_t ssrc, const std::string &rid);
diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp
index f80c1dcf..eb6129e2 100644
--- a/webrtc/WebRtcTransport.cpp
+++ b/webrtc/WebRtcTransport.cpp
@@ -1397,7 +1397,7 @@ void WrappedRtpTrack::inputRtp(const char *buf, size_t len, uint64_t stamp_ms, R
// 修改ext id至统一 [AUTO-TRANSLATED:0769b0ec]
// Modify the ext id to be unified
string rid;
- auto twcc_ext = track->rtp_ext_ctx->changeRtpExtId(rtp, true, &rid, RtpExtType::transport_cc);
+ auto twcc_ext = track->rtp_ext_ctx->changeRtpExtId(rtp, len, true, &rid, RtpExtType::transport_cc);
if (twcc_ext) {
_twcc_ctx.onRtp(ssrc, twcc_ext.getTransportCCSeq(), stamp_ms);
@@ -1417,7 +1417,7 @@ void WrappedRtxTrack::inputRtp(const char *buf, size_t len, uint64_t stamp_ms, R
// 修改ext id至统一 [AUTO-TRANSLATED:0769b0ec]
// Modify the ext id to be unified
string rid;
- track->rtp_ext_ctx->changeRtpExtId(rtp, true, &rid, RtpExtType::transport_cc);
+ track->rtp_ext_ctx->changeRtpExtId(rtp, len, true, &rid, RtpExtType::transport_cc);
auto &ref = track->rtp_channel[rid];
if (!ref) {
@@ -1536,13 +1536,13 @@ void WebRtcTransportImp::onBeforeEncryptRtp(const char *buf, int &len, void *ctx
if (!pr->first || !pr->second->plan_rtx) {
// 普通的rtp,或者不支持rtx, 修改目标pt和ssrc [AUTO-TRANSLATED:e1264971]
// Ordinary RTP, or does not support RTX, modify the target PT and SSRC
- pr->second->rtp_ext_ctx->changeRtpExtId(header, false);
+ pr->second->rtp_ext_ctx->changeRtpExtId(header, len, false);
header->pt = pr->second->plan_rtp->pt;
header->ssrc = htonl(pr->second->answer_ssrc_rtp);
} else {
// 重传的rtp, rtx [AUTO-TRANSLATED:e863a518]
// Retransmitted RTP, RTX
- pr->second->rtp_ext_ctx->changeRtpExtId(header, false);
+ pr->second->rtp_ext_ctx->changeRtpExtId(header, len, false);
header->pt = pr->second->plan_rtx->pt;
if (pr->second->answer_ssrc_rtx) {
// 有rtx单独的ssrc,有些情况下,浏览器支持rtx,但是未指定rtx单独的ssrc [AUTO-TRANSLATED:181cee9a]