Skip to content

obs-outputs: Decode 32-bit RTMP/AMF fields without signed shifts - #188

Merged
zackees merged 5 commits into
obs-rust:masterfrom
chriscoveries:fix/173-amf-shift
Oct 11, 2026
Merged

zackees merged 5 commits into
obs-rust:masterfrom
chriscoveries:fix/173-amf-shift

Conversation

@chriscoveries

@chriscoveries chriscoveries commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

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 OR 0xFFFFFF.. 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 to amf.c, so amf.c:77 here is #173's amf.c:59, and four of the timestamp lines are one higher.

  • amf.c:77 AMF_DecodeInt32 and rtmp.c:3800 DecodeInt32LE: unsigned char promoted to int, then << 24 overflows for bytes ≥ 0x80.
  • rtmp.c:1446, 4880, 5110, 5118, 5365 (timestamp sites): elements of char * buffers are shifted directly. char is signed on x86 and ARM Linux, so a byte ≥ 0x80 becomes a negative int before 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:

  • A, this PR: cast in place at the 7 sites (+/- 1 line each).
  • B: a shared 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.
  • Rejected: compiling librtmp with -fwrapv or -funsigned-char. That hides the UB instead of fixing it, and is platform-specific.

Evidence

  • New test/cmocka/test_amf_decode.c, built with -fsigned-char -fsanitize=undefined -fno-sanitize-recover=undefined (so x86 char behaviour 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_timestamps and write_decodes_flv_timestamp: a top timestamp byte of 0x80, through RTMP_ClientPacket, RTMP_Read and RTMP_Write.
  • RED before each fix commit: rtmp.c:3800:17: runtime error: left shift of 128 by 24 places cannot be represented in type 'int' (decoders), and rtmp.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 at amf.c:59.
  • GREEN: 7/7 pass. Instrumented counters confirm every timestamp site is executed by the tests (1446 twice, the other four once each).
  • Fork CI (self-hosted x86-64 Linux: lint, Rust Tiers 1–3, Docker Tier 2 OFF/ON): passed at f0a93a1: https://github.com/chriscoveries/obsrust-fork/actions/runs/38105331192

Compatibility & risk

  • C API/ABI: unchanged.
  • Wire protocol: unchanged for all values a well-behaved server sends today. High-bit values now decode to the correct unsigned value instead of UB.
  • Not changed here: rtmp.c:4614 has 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.
  • Platforms: tested on macOS arm64 (with -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.

chriscoveries and others added 5 commits October 10, 2026 20:32
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
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"}
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Verified locally, rebased onto master and stamped. The new head is ba53ff8b2.

  • RED 1 (9e4f018d8): Tier 2 passed 50 of 51. test_amf_decode aborts with rtmp.c:3800:17: runtime error: left shift of 128 by 24 places cannot be represented in type 'int'.
  • RED 2 (532d06425): Tier 2 passed 50 of 51. rtmp.c:1446:56: runtime error: left shift of negative value -128.
  • GREEN (head): Tier 2 passed 57 of 57 with ENABLE_RUST_LIBOBS OFF and 57 of 57 with it ON. Local gate: lint, rust-test and tier2-linux all passed.

Option A (cast in place) is fine and keeps the diff against upstream librtmp small. Adding test_amf_decode to the existing OBS::outputs foreach is OK too, since a tests.d fragment would have to duplicate that whole block. Merging once CI is green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: obs-outputs/librtmp: signed shift overflow decoding 32-bit AMF/RTMP fields (amf.c:59, rtmp.c timestamps)

2 participants