Skip to content

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

Closed

Description

@zackees

Found by the first CI run of the sanitizer workflow (run, PR #171; meta issue linked as parent). Upstream OBS code, unchanged in this fork.

Report (UBSan, test_rtmp_chunk_size)

plugins/obs-outputs/librtmp/amf.c:59:17: runtime error: left shift of 128 by 24 places cannot be represented in type 'int'
    #0 AMF_DecodeInt32          plugins/obs-outputs/librtmp/amf.c:59
    #1 HandleChangeChunkSize    plugins/obs-outputs/librtmp/rtmp.c:3605
    #2 RTMP_ClientPacket        plugins/obs-outputs/librtmp/rtmp.c:1317
    #3 receive_chunk_size       test/cmocka/test_rtmp_chunk_size.c:37

Class

Signed integer overflow (UB) on server-controlled input.

Cause

AMF_DecodeInt32 does (c[0] << 24) | ... with unsigned char *c. c[0] is promoted to int, so any first byte ≥ 0x80 overflows. It decodes every 32-bit field a server sends: chunk size, window ack size, peer bandwidth, AMF numbers' lengths, and so on.

The same pattern is worse at the timestamp sites in rtmp.c (1446, 4879, 5109, 5117, 5364) and AMF_DecodeInt32LE (rtmp.c:3800). These shift elements of char *m_body, and char is signed on x86/ARM Linux. A byte ≥ 0x80 is first sign-extended to a negative int (UB to shift), and on compilers that don't trap it ORs 0xFFFFFF.. into the timestamp.

Reached by

A server (or MITM on plain RTMP) sends a Set Chunk Size, Window Ack or extended-timestamp message with the high bit set in the first byte.

Fix

Cast before shifting: ((uint32_t)c[0] << 24) | ((uint32_t)c[1] << 16) | ..., and (uint32_t)(uint8_t)body[i] << 24 at the rtmp.c sites.

Failing test sketch

test_rtmp_chunk_size already drives this path. Under -fsanitize=undefined -fno-sanitize-recover=undefined it would abort. Adding the per-test UBSan flags it lacks, as test_avc has (and see #159), makes it RED. An extended-timestamp packet with 0x80 in the top byte covers the rtmp.c sites.

Activity

  1. added
    bug
    on Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions