Repository navigation
obs-outputs: Decode 32-bit RTMP/AMF fields without signed shifts - #188
Merged
Merged
Conversation
AMF_DecodeInt32 and rtmp.c's DecodeInt32LE shift a byte promoted to int by 24, which is undefined for bytes >= 0x80. RED on master under -fsanitize=undefined. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01XFvTpgDN6Zh3USjgworNmT
Widen each byte to unsigned int before shifting in AMF_DecodeInt32 and DecodeInt32LE. Results are unchanged; the shift is now well-defined. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01XFvTpgDN6Zh3USjgworNmT
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01XFvTpgDN6Zh3USjgworNmT
Adds client-packet, RTMP_Read and RTMP_Write cases whose top timestamp byte is >= 0x80, which trip UBSan on the signed shifts in rtmp.c. Also applies gersemi to test/cmocka/CMakeLists.txt. Refs obs-rust#173 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01XFvTpgDN6Zh3USjgworNmT
Cast each timestamp byte through uint8_t to uint32_t before shifting, as already done for AMF_DecodeInt32. Values below 0x80 are unchanged. Refs obs-rust#173 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01XFvTpgDN6Zh3USjgworNmT Local-Gate: v1 tree=4f5dc3d36c1e2167fa00daa96c3c11414d5bb62c secs=264 lanes=lint:run,rust-test:run,tier2-linux:run Ci-Attestation: {"at":1791689796,"gate":"rust/all/fmt","host":"linux-x86_64","key":"f2006272d469ddbe7ca850e33cce75b64e93aa9bffa6f1ccb5bcc9926b3f341e","lane":"lint","parents":["2a1fb712d9a6d5393c6ae4d325760c4458baa2c4"],"secs":64,"stamp":"dc0737b2ecef1f574286613ac02cf6ab","tree":"4f5dc3d36c1e2167fa00daa96c3c11414d5bb62c","v":1,"via":"run"} Ci-Attestation: {"at":1791689796,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"f2006272d469ddbe7ca850e33cce75b64e93aa9bffa6f1ccb5bcc9926b3f341e","lane":"lint","parents":["2a1fb712d9a6d5393c6ae4d325760c4458baa2c4"],"secs":64,"stamp":"0bb7ff551509a311a45ea9f28649d655","tree":"4f5dc3d36c1e2167fa00daa96c3c11414d5bb62c","v":1,"via":"run"} Ci-Attestation: {"at":1791689796,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"eb5393252d18ee07c5be455eb2eb2494b69101642bce715c6821cb4cfc0e24b4","lane":"rust-test","parents":["2a1fb712d9a6d5393c6ae4d325760c4458baa2c4"],"secs":92,"stamp":"f96f06508c9165ee7276a5b1bdcf283e","tree":"4f5dc3d36c1e2167fa00daa96c3c11414d5bb62c","v":1,"via":"run"} Ci-Attestation: {"at":1791689796,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"8ec1e7666f9206f523e8b4f3bd95ae0593b924784575c0e15b8b26083cd55632","lane":"tier2-linux","parents":["2a1fb712d9a6d5393c6ae4d325760c4458baa2c4"],"secs":107,"stamp":"9acf1b175b7327a7ce46d2ff0eda1383","tree":"4f5dc3d36c1e2167fa00daa96c3c11414d5bb62c","v":1,"via":"run"}
Member
|
Verified locally, rebased onto master and stamped. The new head is
Option A (cast in place) is fine and keeps the diff against upstream librtmp small. Adding |
zackees
force-pushed
the
fix/173-amf-shift
branch
from
October 11, 2026 03:36
f0a93a1 to
ba53ff8
Compare
This was referenced Oct 11, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A server (or anyone on a plain-RTMP path) that sends a 32-bit field whose top byte is ≥ 0x80 (chunk size, window ack size, peer bandwidth, or an extended/FLV timestamp) makes librtmp shift a negative or overflowing
int. That is undefined behaviour; UBSan aborts on it, and compilers that don't trap OR0xFFFFFF..into timestamps.Fixes #173.
Root cause
All in
plugins/obs-outputs/librtmp/, as #173 describes. Line numbers are for this branch (current master); #173's are from an earlier revision, before #116 added declarations toamf.c, soamf.c:77here is #173'samf.c:59, and four of the timestamp lines are one higher.amf.c:77AMF_DecodeInt32andrtmp.c:3800DecodeInt32LE:unsigned charpromoted toint, then<< 24overflows for bytes ≥ 0x80.rtmp.c:1446, 4880, 5110, 5118, 5365(timestamp sites): elements ofchar *buffers are shifted directly.charis signed on x86 and ARM Linux, so a byte ≥ 0x80 becomes a negativeintbefore the shift.Fix
Widen each byte to an unsigned 32-bit value before shifting:
(unsigned int)c[i]in the two decoders, and(uint32_t)(uint8_t)p[i]at the five timestamp sites. This is the fix #173 proposes. Results are identical for every byte below 0x80, and become the intended big-endian value for bytes ≥ 0x80. No signature, header or behaviour change beyond that.Options considered:
rtmp_be32()helper used by every site. It's tidier, but touches more lines in vendored librtmp code and makes the diff against upstream librtmp harder to follow. Happy to switch if you prefer it.-fwrapvor-funsigned-char. That hides the UB instead of fixing it, and is platform-specific.Evidence
test/cmocka/test_amf_decode.c, built with-fsigned-char -fsanitize=undefined -fno-sanitize-recover=undefined(so x86charbehaviour is modelled on every host):decode_int32_bytes,decode_int32le_bytes(plus Int16/Int24 for completeness): 0x00000000, 0x7FFFFFFF, 0x80000000, 0xFFFFFFFF, 0x12345678, and 0x80/0xFF in every byte position.client_packet_decodes_flv_timestamp,read_decodes_flv_timestampsandwrite_decodes_flv_timestamp: a top timestamp byte of 0x80, throughRTMP_ClientPacket,RTMP_ReadandRTMP_Write.rtmp.c:3800:17: runtime error: left shift of 128 by 24 places cannot be represented in type 'int'(decoders), andrtmp.c:1446:56: runtime error: left shift of negative value -128(timestamps). UBSan aborts at the first hit; bug: obs-outputs/librtmp: signed shift overflow decoding 32-bit AMF/RTMP fields (amf.c:59, rtmp.c timestamps) #173's sanitizer run reports the same class of error atamf.c:59.Compatibility & risk
rtmp.c:4614has the same pattern,((HEX2BIN(..) << 4) + HEX2BIN(..)) << 24, which overflows for a hex byte ≥ 0x80. It parses a hex string rather than a server field, so I left it out to keep this PR to bug: obs-outputs/librtmp: signed shift overflow decoding 32-bit AMF/RTMP fields (amf.c:59, rtmp.c timestamps) #173's scope. Happy to add it here or in a follow-up.-fsigned-char) and on x86-64 Linux in CI.Status
Ready for review.
Thank you to the obs-rust maintainers and contributors for building this project in the open. In the American spirit of free expression and individual independence, and with the principle of judging contributions on their merit alone, this PR is a small piece of what independent people can build when they come together.