Skip to content

librtmp: Bound AMF/AMF3 decode recursion depth - #116

Merged
zackees merged 4 commits into
obs-rust:masterfrom
chriscoveries:fix/102-amf-depth
Oct 11, 2026
Merged

zackees merged 4 commits into
obs-rust:masterfrom
chriscoveries:fix/102-amf-depth

Conversation

@chriscoveries

Copy link
Copy Markdown
Contributor

AMF_Decode, AMFProp_Decode, AMF_DecodeArray, AMF3_Decode and AMF3Prop_Decode recurse once per nesting level of remote-supplied AMF data with no limit, so a server (or MITM on plaintext RTMP) can overflow the thread stack with a deeply nested object/array payload.

The five recursive routines are renamed to *Depth variants carrying a depth counter and the decode fails with -1 past AMF_MAX_DEPTH (128). Public entry points keep their signatures and start at depth 0, so valid input decodes exactly as before.

Fixes #102

@b7216309-jpg

Copy link
Copy Markdown
Contributor

I opened #118 for the same issue a few minutes after this one, with the same depth design. I'm closing it in favor of this PR. Two pieces from it may be useful here:

1. A test: test/cmocka/test_amf.c (b7216309-jpg@02ddbcc). It builds only amf.c and log.c with NO_CRYPTO, like test_flv_mux, and its CMake block is in the same commit.

  • A 3-level object decodes to the expected structure.
  • 128 levels of objects, strict arrays and ECMA arrays decode, and 129 return -1. 129 AMF3 objects (via AMF_AVMPLUS) also return -1.
  • 200000 levels of each kind return -1.

I ran it locally with MSVC and a minimal cmocka stand-in:

  • against master's amf.c, 129 levels decode (900 != -1) and the deep cases crash with a stack overflow;
  • against this PR's amf.c, all three tests pass.

2. The AMF3 error path: b7216309-jpg@e465b53. AMF3_Decode ignores nRes == -1 from AMF3Prop_Decode:

  • In the sealed-member loop it logs the error, then does pBuffer += nRes; nSize -= nRes;, moving one byte back.
  • In the dynamic loop it doesn't check at all.
  • Both then AMF_AddProp a property whose p_type was never set, so AMF_Reset can later act on garbage.

With a depth limit, every deeply nested AMF3 message reaches that path. With this PR, the 129-level AMF3 case logs a run of AMF3 Object encapsulated in AMF stream does not start with AMF3_OBJECT! while re-parsing from shifted offsets. It does terminate. In #118 the externalizable, sealed and dynamic paths each return -1 on a failed property.

Feel free to cherry-pick either commit.

@zackees
zackees marked this pull request as ready for review October 10, 2026 22:16
@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Took the offer from #118 (old head 7925b80, replaced with --force-with-lease):

  1. RED: b7216309-jpg's test_amf (02ddbcc, authorship kept). On master it fails test_depth_limit, and the deep cases segfault from unbounded recursion.
  2. GREEN: this PR's depth bound, unchanged. test_amf passes with the Rust port OFF and ON.

The local gate passes and is stamped. Marked ready.

I left out #118's AMF3 error-path change (a failed AMF3Prop_Decode shifts the buffer back and adds a property with unset p_type). It's a separate bug, so it should come as its own PR with a RED test first. @b7216309-jpg, want to take it?

@chriscoveries
chriscoveries marked this pull request as draft October 10, 2026 22:22
@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

@chriscoveries I see you moved this back to draft after my update. Is something blocking, or did you want to change the approach? The current head is green locally (RED→GREEN in Tier 2). Mark it ready again when you're happy and I'll merge once CI passes.

@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Rebased onto master (old head d09a5fa, replaced with --force-with-lease). I also added the AMF3 error-path fix as a RED→GREEN pair, because this PR's depth limit makes it easy to reach (a review flagged it):

  • RED (test/cmocka: RED AMF3 member decode error test): a sealed or externalizable AMF3 member that fails to decode must fail the object. On the current code, AMF3_Decode returns 4 instead of -1 for the sealed case: it keeps the half-decoded prop (unset p_type) and does pBuffer += -1.
  • GREEN (librtmp: Fail AMF3 objects whose member does not decode): reset the object and return -1 in the externalizable, sealed and dynamic branches. The dynamic branch had no check at all; its RED case isn't deterministic (it re-parses backwards), so the fix covers it without a dedicated test.

test_amf passes with the Rust port OFF and ON. Its run time also dropped from about 11 s to 0.04 s: the 129-level AMF3 case used to re-parse from shifted offsets, and now fails at once. The local gate passes and is stamped. Credit to b7216309-jpg, who described this on #118. @chriscoveries, mark this ready when you're happy.

@chriscoveries

Copy link
Copy Markdown
Contributor Author

Thank you @zackees, and @b7216309-jpg for the depth test. Nothing is blocking, and sorry for the unexplained re-draft (context in the #115 comment). Your head 8c2f0d159 also passes on our side: lint, Rust Tiers 1–3 and Docker Tier 2 with the port OFF and ON (run), plus sanitizer runs on arm64 and x86. Marking it ready.

Two follow-ups, each as its own PR with a RED test first, unless you'd rather we skip them:

  • A positive test showing a valid AMF3 document at exactly depth 128 still decodes.
  • A pre-existing over-read that ASan found while exercising this path: AMF3_Decode logs the class name with %s (amf.c ~L1179), but cd_name.av_val isn't NUL-terminated. The fix is %.*s with av_len. It dates from 2014 and wasn't introduced by this PR.

b7216309-jpg and others added 4 commits October 10, 2026 17:12
Decodes nested AMF0 objects, strict arrays and ECMA arrays and AMF3
objects with librtmp's AMF_Decode, as for an RTMP server's command or
metadata message:

- 128 levels decode, 129 are refused
- 200000 levels fail cleanly instead of overflowing the stack

Both regressions fail until the next commit: 129 levels decode, and
the deep cases crash with a stack overflow.

Refs obs-rust#102

Co-Authored-By: Claude Opus 5.5 <[email protected]>
AMF_Decode, AMFProp_Decode, AMF_DecodeArray, AMF3_Decode and
AMF3Prop_Decode recurse once per nesting level of remote-supplied AMF
data with no limit, so a server (or MITM on plaintext RTMP) can
overflow the thread stack with a deeply nested object/array payload.

Rename the five recursive routines to *Depth variants carrying a depth
counter and fail the decode with -1 past AMF_MAX_DEPTH (128). The
public entry points keep their signatures and start at depth 0, so
valid input decodes exactly as before.

Fixes obs-rust#102

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Local-Gate: v1 tree=cbbf43405c0102f24c4d6e725931c2b3252cb0aa secs=323 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791670604,"gate":"rust/all/fmt","host":"linux-x86_64","key":"bf2730b1d4f44164686f48e55a2086784ac47009bec506b58941c3552845e023","lane":"lint","parents":["dd156060aa3761b627d9a586924e61f58529efa3"],"secs":146,"stamp":"f23ffcd4f572e9f47ee15b75c1755858","tree":"cbbf43405c0102f24c4d6e725931c2b3252cb0aa","v":1,"via":"run"}
Ci-Attestation: {"at":1791670604,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"bf2730b1d4f44164686f48e55a2086784ac47009bec506b58941c3552845e023","lane":"lint","parents":["dd156060aa3761b627d9a586924e61f58529efa3"],"secs":146,"stamp":"c3cf26593eb4cdbb4b4ca81912f578f6","tree":"cbbf43405c0102f24c4d6e725931c2b3252cb0aa","v":1,"via":"run"}
Ci-Attestation: {"at":1791670604,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"f785492681703c97bd84aacbe60603c5f1d5b2bdd9c04b3d9eb21f3b72718060","lane":"rust-test","parents":["dd156060aa3761b627d9a586924e61f58529efa3"],"secs":79,"stamp":"816d97669a90ec8e46f97237eef34f7b","tree":"cbbf43405c0102f24c4d6e725931c2b3252cb0aa","v":1,"via":"run"}
Ci-Attestation: {"at":1791670604,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"b5c5e63e1c049e3c75571f9f36da9a9ac3712432ecfd915708627fa73895a07c","lane":"tier2-linux","parents":["dd156060aa3761b627d9a586924e61f58529efa3"],"secs":96,"stamp":"5e92a41bd6d68fe7e33f23bfb76ff084","tree":"cbbf43405c0102f24c4d6e725931c2b3252cb0aa","v":1,"via":"run"}
A sealed or externalizable AMF3 member that fails to decode must fail the
whole object. AMF3_Decode instead keeps the half-decoded property (with
an unset p_type) and, for sealed members, steps the buffer back by the
-1 it got. The depth limit in this PR returns -1 deep in the nesting,
which makes that path easy to reach.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
AMF3_Decode logged a failed AMF3Prop_Decode and kept going: it added the
property with an unset p_type (which AMF_Reset could later act on), and
for sealed members added the -1 result to pBuffer, re-parsing from one
byte back. The dynamic loop did not check at all. Reset the object and
return -1 in all three branches, as AMF_Decode does for AMF0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=2675e34ba05c5319570b7eb37afe23f73b09cba1 secs=270 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791677832,"gate":"rust/all/fmt","host":"linux-x86_64","key":"5f3dbc9d36a60c29b6af45ecf530d9ac20b140a5b4fce26c51bbf9f97af84647","lane":"lint","parents":["92a86cd6728ae4f699e99e2262861d0521e5e5de"],"secs":123,"stamp":"ff4ddf4ac40a955ff34950cf518b9ed3","tree":"2675e34ba05c5319570b7eb37afe23f73b09cba1","v":1,"via":"run"}
Ci-Attestation: {"at":1791677832,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"5f3dbc9d36a60c29b6af45ecf530d9ac20b140a5b4fce26c51bbf9f97af84647","lane":"lint","parents":["92a86cd6728ae4f699e99e2262861d0521e5e5de"],"secs":123,"stamp":"eb148c0718dd406ac9492d96e11a61a4","tree":"2675e34ba05c5319570b7eb37afe23f73b09cba1","v":1,"via":"run"}
Ci-Attestation: {"at":1791677832,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"c264e2969035808f5e43e7bf0708865984296186c9479db69e6848fa2b20b571","lane":"rust-test","parents":["92a86cd6728ae4f699e99e2262861d0521e5e5de"],"secs":57,"stamp":"05b04fb0404a9cd24b06792a2d0755a9","tree":"2675e34ba05c5319570b7eb37afe23f73b09cba1","v":1,"via":"run"}
Ci-Attestation: {"at":1791677832,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"bdeb7c993afd034bc1e848ebaef44b54e09f6acecd6d045f45cd40e7bab782d6","lane":"tier2-linux","parents":["92a86cd6728ae4f699e99e2262861d0521e5e5de"],"secs":90,"stamp":"1ce35f82d9c67ad91b1bee3001699e11","tree":"2675e34ba05c5319570b7eb37afe23f73b09cba1","v":1,"via":"run"}
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Rebased onto master (old head 8c2f0d1, replaced with --force-with-lease). Since #168, cmocka tests live in test/cmocka/tests.d/, so test_amf's block moved to tests.d/test_amf.cmake (folded into the test commit). The earlier Windows x64 failure was an MSBuild file-lock flake (MSB3374), not this code. test_amf passes with the Rust port OFF and ON, and the local gate passes and is stamped. I'll merge once CI is green.

@zackees
zackees merged commit b0630e8 into obs-rust:master Oct 11, 2026
23 checks passed
@chriscoveries
chriscoveries deleted the fix/102-amf-depth branch October 11, 2026 02:18
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.

[crash] librtmp AMF_Decode: unbounded recursion on server data -> stack overflow

3 participants