sahvx655-wq opened a new pull request, #3747:
URL: https://github.com/apache/brpc/pull/3747

   ### What problem does this PR solve?
   
   Issue Number: none
   
   Problem Summary:
   
   `AVCDecoderConfigurationRecord::ParseSPS` (src/brpc/rtmp.cpp) reads 
`pic_width_in_mbs_minus1` and `pic_height_in_map_units_minus1` with 
`avc_nalu_read_uev`, which accepts up to 30 leading zero bits and so returns 
values up to 2^31-2. It then computes `(x + 1) * 16` in `int` with no bound, so 
any value of 134217727 (`INT_MAX / 16`) or more overflows a signed int. The SPS 
comes straight from the AVC sequence header an RTMP publisher sends, and 
`TsWriter::Write` passes it to `AVCDecoderConfigurationRecord::Create` when 
muxing RTMP to HLS/TS.
   
   I found it by following where the ue(v) results end up after the earlier 
`avc_nalu_read_uev` fixes; this is the only place a decoded value feeds a 
multiplication. A 12-byte SPS encoding `pic_width_in_mbs_minus1 = 
ue(268435454)` is enough. Built with `-fsanitize=signed-integer-overflow 
-fsanitize-trap`, the harness dies with an illegal-instruction trap on that 
multiplication. In a normal build `Create()` returns OK with `width = -16`. 
Signed overflow is undefined behaviour, so what you get depends on the 
compiler. Today that is a negative width accepted as valid and reported to the 
caller, and a crash from a single publisher packet in sanitizer-instrumented 
builds.
   
   ### What is changed and the side effects?
   
   Changed:
   - `ParseSPS` rejects `pic_width_in_mbs_minus1` or 
`pic_height_in_map_units_minus1` >= `INT_MAX / 16` with EINVAL before computing 
`width`/`height`. The bound sits next to the multiplication, so callers of 
`Create()` need no checks of their own. Real H.264 streams are nowhere near it 
(level 6.2 tops out around 1055 macroblocks per side).
   - Regression test `RtmpTest.avc_seq_header_sps_oversized_picture` in 
test/brpc_rtmp_unittest.cpp. It uses the SPS from 
`avc_seq_header_sps_exp_golomb` with only the width field changed. It fails on 
master (`Create()` returns OK) and passes with the fix.
   
   Testing: I could not build the full brpc tree on the machine this was 
prepared on (Windows, no Linux toolchain). Instead, a standalone harness 
compiled `BitStream`/`avc_nalu_read_uev` and the `ParseSPS` body verbatim from 
the source with clang and `-fsanitize=signed-integer-overflow -fsanitize-trap`. 
Unpatched, it traps on the oversized SPS. Patched, it rejects that SPS and 
still decodes the existing 80x80 test SPS as 80x80. The new gtest has not yet 
run against a full build, so CI will be its first full run.
   
   Side effects:
   - Performance effects: none, two integer comparisons per sequence header.
   
   - Breaking backward compatibility: none. Only SPS values that previously 
overflowed are rejected.
   
   ---
   ### Check List:
   - Please make sure your changes are compilable.
   - When providing us with a new feature, it is best to add related tests.
   - Please follow [Contributor Covenant Code of 
Conduct](https://github.com/apache/brpc/blob/master/CODE_OF_CONDUCT.md).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to