diff --git a/src/brpc/rtmp.cpp b/src/brpc/rtmp.cpp index b0a5f04a6e..3c9ce8d527 100644 --- a/src/brpc/rtmp.cpp +++ b/src/brpc/rtmp.cpp @@ -16,6 +16,7 @@ // under the License. +#include #include #include // StringOutputStream #include "bthread/bthread.h" // bthread_id_xx @@ -832,6 +833,15 @@ butil::Status AVCDecoderConfigurationRecord::ParseSPS( if (avc_nalu_read_uev(&bs, &pic_height_in_map_units_minus1) != 0) { return butil::Status(EINVAL, "Fail to read pic_height_in_map_units_minus1"); } + // ue(v) decodes up to 2^31-2, so (x + 1) * 16 must be bounded to fit int. + const int32_t max_size_in_mbs = std::numeric_limits::max() / 16; + if (pic_width_in_mbs_minus1 >= max_size_in_mbs || + pic_height_in_map_units_minus1 >= max_size_in_mbs) { + return butil::Status(EINVAL, "Invalid pic_width_in_mbs_minus1=%d " + "pic_height_in_map_units_minus1=%d", + (int)pic_width_in_mbs_minus1, + (int)pic_height_in_map_units_minus1); + } width = (int)(pic_width_in_mbs_minus1 + 1) * 16; height = (int)(pic_height_in_map_units_minus1 + 1) * 16; return butil::Status::OK(); diff --git a/test/brpc_rtmp_unittest.cpp b/test/brpc_rtmp_unittest.cpp index bbfaba4467..67fb31c8f1 100644 --- a/test/brpc_rtmp_unittest.cpp +++ b/test/brpc_rtmp_unittest.cpp @@ -1142,6 +1142,27 @@ TEST(RtmpTest, avc_seq_header_rejected_length_size_keeps_record) { ASSERT_EQ(std::string(pps, sizeof(pps)), avc.pps_list[0]); } +// pic_width_in_mbs_minus1 is a ue(v) that decodes up to 2^31-2, and ParseSPS +// computed (pic_width_in_mbs_minus1 + 1) * 16 in int. Here it is +// ue(268435454), so the product overflowed (UB, width came out as -16) and the +// record was accepted. It must be rejected instead. +TEST(RtmpTest, avc_seq_header_sps_oversized_picture) { + butil::IOBuf buf; + const char head[6] = { 0x01, 0x42, 0x00, 0x1E, (char)0xff, (char)0xe1 }; + buf.append(head, sizeof(head)); + // Same fields as avc_seq_header_sps_exp_golomb, except that + // pic_width_in_mbs_minus1 = ue(2^28-2) and pic_height_in_map_units_minus1 = ue(0). + const char sps[12] = { 0x67, 0x42, 0x00, 0x1E, (char)0xdc, 0x00, 0x00, 0x00, + 0x3f, (char)0xff, (char)0xff, (char)0xff }; + const char len_be[2] = { 0x00, (char)sizeof(sps) }; + buf.append(len_be, sizeof(len_be)); + buf.append(sps, sizeof(sps)); + buf.push_back('\0'); // numPPS=0 + + brpc::AVCDecoderConfigurationRecord avc; + ASSERT_FALSE(avc.Create(buf).ok()); +} + static void AppendBigEndian3Bytes(std::string* s, uint32_t v) { s->push_back((char)((v >> 16) & 0xFF)); s->push_back((char)((v >> 8) & 0xFF));