From fcb2889ad15ef48aa7c15081cf9f1672cbb7ffad Mon Sep 17 00:00:00 2001 From: Xiaofeng Wang Date: Sun, 25 Oct 2026 21:53:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20makeRtp=E5=A2=9E=E5=8A=A0=E8=B4=9F?= =?UTF-8?q?=E8=BD=BD=E9=95=BF=E5=BA=A6=E6=A0=A1=E9=AA=8C=20(#4857)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit RTP 包的长度字段为 16 位,但 `RtpInfo::makeRtp` 未校验入参长度:分配内存时 长度被截断为 16 位,拷贝数据时却使用原始的 `size_t` 长度,二者并不一致;被 截断的长度字段同样无法正确表示 RTSP interleaved 包长。 ## 改动 - `src/Rtsp/RtpCodec.cpp`:在相加、分配内存和写头部之前,拒绝超过 `UINT16_MAX - kRtpHeaderSize`(65523)的长度,抛出 `std::length_error`。 允许的最大负载恰好填满 RTSP interleaved 长度字段,不会产生被截断的头部。 - `src/Rtsp/RtpCodec.h`:补充接口注释,说明异常行为。 - `src/Rtsp/RtspMuxer.cpp`:在 `inputFrame`/`flush` 中捕获异常,避免 MTU 配置过大时异常扩散到事件循环。 - `webrtc/WebRtcPlayer.cpp`:跳过超长 config 帧,并保持后续包的 seq 序号对齐。 ## 兼容性 65523 字节的上限不会拒绝任何合法负载:其余 `makeRtp` 调用点的长度均由 `getMaxSize()`(MTU - 12)限制,而 UDP 数据报本身最大也只有 65507 字节。 正常 MTU 下的大帧仍按原有逻辑分片,行为不变。 ## 验证 在 `ENABLE_ASAN=ON` + `ENABLE_WEBRTC=ON` 下编译通过,并以 ASan 运行了 长度边界(0 / 4 / 1388 / 65523 接受,65524 及以上拒绝)、muxer 异常边界 与 WebRTC config 帧路径的检查,均无内存错误。 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5 --- src/Rtsp/RtpCodec.cpp | 6 ++++++ src/Rtsp/RtpCodec.h | 1 + src/Rtsp/RtspMuxer.cpp | 14 ++++++++++++-- webrtc/WebRtcPlayer.cpp | 10 +++++++++- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Rtsp/RtpCodec.cpp b/src/Rtsp/RtpCodec.cpp index 0ed1cd2c..2205f39c 100644 --- a/src/Rtsp/RtpCodec.cpp +++ b/src/Rtsp/RtpCodec.cpp @@ -9,10 +9,16 @@ */ #include "RtpCodec.h" +#include +#include namespace mediakit{ RtpPacket::Ptr RtpInfo::makeRtp(TrackType type, const void* data, size_t len, bool mark, uint64_t stamp) { + // Validate before addition, allocation or header writes, including data == nullptr. + if (len > (std::numeric_limits::max)() - RtpPacket::kRtpHeaderSize) { + throw std::length_error("RTP payload exceeds the RTSP interleaved length limit"); + } uint16_t payload_len = (uint16_t) (len + RtpPacket::kRtpHeaderSize); auto rtp = RtpPacket::create(); rtp->setCapacity(payload_len + RtpPacket::kRtpTcpHeaderSize); diff --git a/src/Rtsp/RtpCodec.h b/src/Rtsp/RtpCodec.h index 49e1ddde..dee2edc4 100644 --- a/src/Rtsp/RtpCodec.h +++ b/src/Rtsp/RtpCodec.h @@ -82,6 +82,7 @@ public: return _mtu_size - RtpPacket::kRtpHeaderSize; } + // Throws std::length_error if the RTP packet cannot fit the 16-bit RTSP length field. RtpPacket::Ptr makeRtp(TrackType type,const void *data, size_t len, bool mark, uint64_t stamp); private: diff --git a/src/Rtsp/RtspMuxer.cpp b/src/Rtsp/RtspMuxer.cpp index 684a16f8..4d5230d8 100644 --- a/src/Rtsp/RtspMuxer.cpp +++ b/src/Rtsp/RtspMuxer.cpp @@ -9,6 +9,7 @@ */ #include "RtspMuxer.h" +#include #include "Common/config.h" #include "Extension/Factory.h" @@ -151,13 +152,22 @@ void RtspMuxer::trySyncTrack() { bool RtspMuxer::inputFrame(const Frame::Ptr &frame) { auto &encoder = _tracks[frame->getIndex()].encoder; - return encoder ? encoder->inputFrame(frame) : false; + try { + return encoder ? encoder->inputFrame(frame) : false; + } catch (const std::length_error &ex) { + WarnL << "Skipping oversized RTP frame: " << ex.what(); + return false; + } } void RtspMuxer::flush() { for (auto &pr : _tracks) { if (pr.second.encoder) { - pr.second.encoder->flush(); + try { + pr.second.encoder->flush(); + } catch (const std::length_error &ex) { + WarnL << "Skipping oversized buffered RTP frame: " << ex.what(); + } } } } diff --git a/webrtc/WebRtcPlayer.cpp b/webrtc/WebRtcPlayer.cpp index 964b947b..e90b896d 100644 --- a/webrtc/WebRtcPlayer.cpp +++ b/webrtc/WebRtcPlayer.cpp @@ -9,6 +9,7 @@ */ #include "WebRtcPlayer.h" +#include #include "Common/config.h" #include "Extension/Factory.h" @@ -326,7 +327,14 @@ void WebRtcPlayer::sendConfigFrames(const RtpPacket::Ptr &packet) { auto seq = packet->getSeq() - frames.size(); for (const auto &frame : frames) { - auto rtp = encoder->getRtpInfo().makeRtp(TrackVideo, frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize(), false, 0); + RtpPacket::Ptr rtp; + try { + rtp = encoder->getRtpInfo().makeRtp(TrackVideo, frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize(), false, 0); + } catch (const std::length_error &ex) { + WarnL << "Skipping oversized WebRTC config frame: " << ex.what(); + ++seq; + continue; + } auto header = rtp->getHeader(); header->seq = htons(seq++); header->stamp = htonl(packet->getStamp());