Skip to content

obs-outputs: Make DecodeTEA's shifts well-defined - #208

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

zackees merged 4 commits into
obs-rust:masterfrom
chriscoveries:fix/173-tea-shift

Conversation

@chriscoveries

@chriscoveries chriscoveries commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A follow-up to #188 for the two remaining #173-pattern shifts, both in DecodeTEA, and a correction. In #188 I said rtmp.c:4614 only parses a local hex string and is not a server field. That was wrong. It unpacks the secureToken a server sends in its connect _result, so a token byte ≥ 0x80 makes it shift an int into the sign bit (UB). The TEA key packing just above it has the same pattern for a configured token with a byte ≥ 0x80.

Refs #173, #188.

Root cause

plugins/obs-outputs/librtmp/rtmp.c, DecodeTEA (line 4572):

  • :4588 key packing: u |= ptr[i] << (n * 8);. ptr[i] is an unsigned char promoted to int, so << 24 overflows for bytes ≥ 0x80.
  • :4614 hex unpack of the server's token: u |= ((HEX2BIN(ptr[6]) << 4) + HEX2BIN(ptr[7])) << 24;. The shifted operand is a whole byte (up to 0xff) held in an int.

The << 8 and << 16 lines can't overflow on OBS's 32-bit-int targets.

Fix

Widen to uint32_t before shifting at both lines, the same pattern #188 used. Two one-line changes. The intended 32-bit bit pattern is unchanged; the cast makes the shift defined.

Evidence

Two tests in test/cmocka/test_amf_decode.c drive the real path, RTMP_ClientPacket → HandleInvoke → DecodeTEA, with an in-memory connect _result carrying a secureToken. No server is needed.

  • secure_token_decode_uses_unsigned_shift: the server token has a high top byte. RED at 3a1a5979e: rtmp.c:4614:57: runtime error: left shift of 128 by 24 places cannot be represented in type 'int'.
  • secure_token_key_uses_unsigned_shift: the configured token has 0x80 in a word's top byte. RED at 1c2b54560: rtmp.c:4588:21: runtime error: left shift of 128 by 24 places cannot be represented in type 'int'.
  • GREEN at 3ad80e240: 9/9 pass.

How this was tested

  • RED → GREEN: each new test fails before its fix and passes after, built with ASan/UBSan.
  • Our own CI: self-hosted x86-64 Linux runners on our fork run lint (clang-format 22.1.3, gersemi), Rust Tiers 1–3, and the unchanged cmocka suite in Docker with ENABLE_RUST_LIBOBS OFF and ON, including export parity. The run is pinned to this PR's exact head commit: https://github.com/chriscoveries/obsrust-fork/actions/runs/38117176938
  • Real runs: not applicable. These are one-line arithmetic fixes, and the tests exercise the real decode path.
  • Review before posting: two independent reviewers checked the claims, file:line references and numbers in this description against the code at that commit, and a person approved posting it.

Out of scope (separate issue to follow)

DecodeTEA has two more problems on the same server-reachable path, which this PR doesn't touch:

  • :4605 rounds the length up to 8-character blocks, and the loop then reads 8 characters per block regardless of text->av_len, so it reads past the token.
  • An empty token gives n == 0, which leads to v[n - 1] at :4622 and a division by zero at :4624.

We'll open these as their own issue with options rather than widen this PR.

Compatibility & risk

No API, ABI or wire change.

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 3 commits October 11, 2026 17:01
A connect _result carrying a secureToken reaches DecodeTEA, whose hex
decode shifts an int byte by 24. RED under UBSan.

Refs obs-rust#173

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XFvTpgDN6Zh3USjgworNmT
Widen the top byte to uint32_t before shifting by 24 in DecodeTEA, as
done for the other obs-rust#173 sites. Values are unchanged.

Refs obs-rust#173

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XFvTpgDN6Zh3USjgworNmT
The configured token is packed into the TEA key with ptr[i] << (n * 8),
which shifts an int into the sign bit for bytes >= 0x80. RED under UBSan.

Refs obs-rust#173

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XFvTpgDN6Zh3USjgworNmT
@chriscoveries

Copy link
Copy Markdown
Contributor Author

@zackees, a short note on how we work now, since some of our earlier PRs cost you time.

Some of our earlier PRs were unclear, a few went up red, and some were posted before they were ready. That came from our agents posting without enough gating. We've changed the flow:

  1. Issue first, with options. For anything non-trivial we propose the change as an issue with the smallest option first, and follow your choice.
  2. Test first. Every fix comes with a test that fails on master and passes on the branch.
  3. Our own CI server. We run self-hosted x86-64 Linux runners on our fork: lint, Rust Tiers 1–3, and the cmocka suite in Docker with the Rust port OFF and ON. Every PR links the run for its exact head commit, so nothing reaches you red.
  4. Real runs. Runtime changes are exercised in OBS itself on macOS and x86-64 Linux, and we say plainly what we couldn't test.
  5. Two reviewers, then a person. Two independent reviewers verify every claim and line reference in the description against the code, and a person (me) approves every post. Nothing goes up automatically.

Each PR from us now ends with a short "How this was tested" section. If you'd rather we did anything differently, tell us and we'll adjust.

Widen each key byte to uint32_t before shifting in DecodeTEA, matching
the hex-unpack fix. Values 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=8929cc7344900e31f348c1624a0f3662303fff18 secs=123 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791699852,"gate":"rust/all/fmt","host":"linux-x86_64","key":"e05236c8a40b522c388f6ef66eb04e6fcef40af433fcc35e68fc143e38519180","lane":"lint","parents":["1c2b5456089ba937ce9b552965ea30c936860225"],"secs":50,"stamp":"ff6fa3eb4d295fef70f8a8966142512e","tree":"8929cc7344900e31f348c1624a0f3662303fff18","v":1,"via":"run"}
Ci-Attestation: {"at":1791699852,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"e05236c8a40b522c388f6ef66eb04e6fcef40af433fcc35e68fc143e38519180","lane":"lint","parents":["1c2b5456089ba937ce9b552965ea30c936860225"],"secs":50,"stamp":"a4f37700690fb2c5598176ef14c82175","tree":"8929cc7344900e31f348c1624a0f3662303fff18","v":1,"via":"run"}
Ci-Attestation: {"at":1791699852,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"4fef475e41107961f42cff4729113a9c20bb2f1743b13afdfad3b91183bee815","lane":"rust-test","parents":["1c2b5456089ba937ce9b552965ea30c936860225"],"secs":32,"stamp":"6e4f6809ea264a315f1fc25815d34cee","tree":"8929cc7344900e31f348c1624a0f3662303fff18","v":1,"via":"run"}
Ci-Attestation: {"at":1791699852,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"5a69f2850c9c47c48ad95f66c68c9e09bd4ac812cefdc322679988361c2cb974","lane":"tier2-linux","parents":["1c2b5456089ba937ce9b552965ea30c936860225"],"secs":42,"stamp":"17521b8d604685b67c0aeff3ad20c22f","tree":"8929cc7344900e31f348c1624a0f3662303fff18","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 a8f526d7d.

  • RED 1 (3a1a5979e): Tier 2 passed 63 of 64. rtmp.c:4614:57: runtime error: left shift of 128 by 24 places cannot be represented in type 'int'.
  • RED 2 (1c2b54560): Tier 2 passed 63 of 64. rtmp.c:4588:21: runtime error: left shift of 128 by 24 places ....
  • GREEN (3ad80e240): Tier 2 passed 64 of 64. Local gate on the rebased head: lint, rust-test and tier2-linux all passed.

Thanks for the correction about :4614 being server-controlled. Merging once CI is green.

@zackees
zackees merged commit ce08e85 into obs-rust:master Oct 11, 2026
23 checks passed
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.

2 participants