Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/brpc/rtmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.


#include <limits>
#include <gflags/gflags.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h> // StringOutputStream
#include "bthread/bthread.h" // bthread_id_xx
Expand Down Expand Up @@ -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<int>::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();
Expand Down
21 changes: 21 additions & 0 deletions test/brpc_rtmp_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading