Skip to content

librtmp: Bound AMF decoding depth - #118

Closed
b7216309-jpg wants to merge 2 commits into
obs-rust:masterfrom
b7216309-jpg:fix/amf-decode-depth
Closed

b7216309-jpg wants to merge 2 commits into
obs-rust:masterfrom
b7216309-jpg:fix/amf-decode-depth

Conversation

@b7216309-jpg

Copy link
Copy Markdown
Contributor

Fixes #102.

AMF_Decode, AMF_DecodeArray, AMFProp_Decode, AMF3_Decode and AMF3Prop_Decode recurse once per nested object or array, as deep as the data says. The data comes from the RTMP server (connect result, onMetaData, ...), so one message with a few bytes per level overflows the send thread's stack.

Change

  • Each decoder gets a static *Depth variant that carries the nesting depth and returns -1 past AMF_MAX_DEPTH (128). The public functions keep their signatures and start at depth 0, so no caller changes.
  • AMF3_Decode now fails when a property fails. Before, it logged and continued: with nRes == -1 it moved its buffer pointer back one byte, and it added the property with an unset p_type. With the depth limit that path would be hit on every deeply nested AMF3 message.

Tests

New test/cmocka/test_amf.c (builds only amf.c and log.c, NO_CRYPTO, like test_flv_mux):

  • a 3-level object decodes to the expected structure
  • 128 levels of objects / strict arrays / ECMA arrays decode, 129 return -1; 129 AMF3 objects return -1
  • 200000 levels of each return -1

The test commit comes first. I ran it locally (MSVC, with a minimal cmocka stand-in): against the old amf.c, 129 levels decode (900 != -1) and the deep cases crash with a stack overflow (0xC00000FD); with the fix all three tests pass. CI is the first build with the real cmocka and CMake.

🤖 Generated with Claude Code

b7216309-jpg and others added 2 commits October 10, 2026 23:19
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, AMF_DecodeArray, AMFProp_Decode, AMF3_Decode and
AMF3Prop_Decode recurse once per nested object or array, as deep as
the data says. That data comes from the RTMP server (connect result,
onMetaData, ...), so a few bytes per level in one message overflow the
send thread's stack.

Each decoder now has a static *Depth variant that carries the nesting
depth and fails with -1 past AMF_MAX_DEPTH (128). The public functions
keep their signatures and start at depth 0.

AMF3_Decode now also fails when a property fails to decode. It logged
the error and went on, moving its buffer pointer back by one byte
(nRes == -1) and adding the property with an unset type; with the
depth limit that would be hit on every deep AMF3 message.

Fixes obs-rust#102

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@b7216309-jpg

Copy link
Copy Markdown
Contributor Author

Closing in favor of #116, which was opened first with the same fix. I offered the test and the AMF3 error-path change there.

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

1 participant