Skip to content

fix(libobs): get_ue_golomb UB on a truncated SPS (1 << 32) - #17

Merged
zackees merged 2 commits into
masterfrom
fix/obs-avc-golomb-ub
Oct 9, 2026
Merged

zackees merged 2 commits into
masterfrom
fix/obs-avc-golomb-ub

Conversation

@zackees

@zackees zackees commented Oct 9, 2026

Copy link
Copy Markdown
Member

Item 2 of #13. Fixes the undefined behavior in get_ue_golomb (libobs/obs-avc.c) on a truncated SPS.

Change

  • get_ue_golomb: a valid ue(v) has at most 31 leading zeros, so the leading-zero count is capped at 31 and the value is computed with 1u << i. Before, a truncated or malformed SPS made the loop count 32 zeros, because the bitstream reader returns 0 past the end, and then evaluated 1 << 32. 1 << 31 also overflowed int. Valid input gives the same result as before.
  • New cmocka test test/cmocka/test_avc.c (existing C tests untouched). It calls obs_parse_avc_header on a High-profile (profile_idc 100) SPS that ends right after the level byte, followed by a PPS, and checks the avcC layout. On non-MSVC toolchains, obs-avc.c is compiled into the test with -fsanitize=undefined -fno-sanitize-recover=undefined, so any UB aborts the test. Its definitions take precedence over libobs's.
  • test_avc is added to the Tier 2 target lists in rust/tools/linux-validate/validate.sh and rust/tools/tier2-validate/validate.{sh,ps1}. ctest runs it with ENABLE_RUST_LIBOBS OFF and ON.

Intentional behavior difference, relevant to a future Rust port's Tier 3 parity exclusion: on 32+ leading zeros (garbage input), the decoded value is now well defined (0xFF after the uint8_t truncation). Before, it was undefined, and on x86 it was typically 0.

RED → GREEN (./unittest, tier2-linux lane)

RED (commit 1, test only):

5/5 Test #5: test_avc .........................***Failed    0.05 sec
../../src/libobs/obs-avc.c:173:48: runtime error: shift exponent 32 is too large for 32-bit type 'int'
== lane tier2-linux: FAILED (8)

GREEN (commit 2, fix):

5/5 Test #5: test_avc .........................   Passed    0.03 sec   (OFF)
5/5 Test #5: test_avc .........................   Passed    0.03 sec   (ON)
IDENTICAL (1752 symbols)
== lane tier2-linux: ok

./lint ok. Stamped with ci-lint local-gate run (lint, rust-test, tier2-linux all passed).

Closes #11
Refs #13

…(RED for #11)

obs_parse_avc_header on an SPS that ends right after the level byte makes
get_ue_golomb count 32 leading zeros and evaluate 1 << 32. The test
compiles obs-avc.c with -fsanitize=undefined -fno-sanitize-recover where
the toolchain supports it, so the UB aborts the test. Added to the Tier 2
target lists (Linux, macOS, Windows).
#11)

On a truncated or malformed SPS the bitstream reader returns 0 past the
end, so get_ue_golomb counted 32 leading zeros and evaluated 1 << 32,
undefined behavior (and 1 << 31 overflowed int). Cap the count at 31,
the most a valid ue(v) has, and compute with an unsigned shift. Valid
input is unaffected; test_avc goes GREEN under UBSan.

Closes #11

Local-Gate: v1 tree=522f3ed0e7d811b504351a747d5939f6e3802cde secs=53 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791509701,"gate":"rust/all/fmt","host":"linux-x86_64","key":"9b8ead564a49f74e170d8f91590327aa355cfb99ab8eaa5a30be2e2950a675a2","lane":"lint","parents":["5b01120fb64be07c7f361d7ae3078ecf43940f54"],"secs":14,"stamp":"66346a49a592ac7012d76c16e9f5d1ee","tree":"522f3ed0e7d811b504351a747d5939f6e3802cde","v":1,"via":"run"}
Ci-Attestation: {"at":1791509701,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"9b8ead564a49f74e170d8f91590327aa355cfb99ab8eaa5a30be2e2950a675a2","lane":"lint","parents":["5b01120fb64be07c7f361d7ae3078ecf43940f54"],"secs":14,"stamp":"c86a616ff8501783665dfb1d6caa259a","tree":"522f3ed0e7d811b504351a747d5939f6e3802cde","v":1,"via":"run"}
Ci-Attestation: {"at":1791509701,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"57d0cdbed6b046cd49e037050c3d98e2cbd30dc535f666b52c360db558094159","lane":"rust-test","parents":["5b01120fb64be07c7f361d7ae3078ecf43940f54"],"secs":4,"stamp":"7acae19b769c260afb6a3df2b8a3be58","tree":"522f3ed0e7d811b504351a747d5939f6e3802cde","v":1,"via":"run"}
Ci-Attestation: {"at":1791509701,"gate":"general/linux-x64/tier2","host":"linux-x86_64","key":"6d6c804fa30509892a9fbe1ec2ddfe1dfb406282db6a4633d838512a274d27c8","lane":"tier2-linux","parents":["5b01120fb64be07c7f361d7ae3078ecf43940f54"],"secs":35,"stamp":"ab7e8bdda5e16251f3ff10c658a70b9c","tree":"522f3ed0e7d811b504351a747d5939f6e3802cde","v":1,"via":"run"}
@zackees
zackees merged commit ad22d25 into master Oct 9, 2026
22 checks passed
@zackees
zackees deleted the fix/obs-avc-golomb-ub branch October 9, 2026 01:51
b7216309-jpg added a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
obs_avc_keyframe, obs_avc_find_startcode, obs_parse_avc_packet,
obs_parse_avc_packet_priority, obs_parse_avc_header and
obs_extract_avc_headers move to Rust. With ENABLE_RUST_LIBOBS=ON libobs
builds without obs-avc.c.

Every function walks the NAL units as the C loop does: skip zeros and
one more byte after each start code, stop when that reaches the end.
Outputs are bmalloc'ed like the C serializer and DARRAY buffers, with
the long reference count in front of a parsed packet. The SPS reader
keeps the C details: emulation-prevention bytes dropped, the
Exp-Golomb leading-zero cap from obs-rust#17, uint8_t results, and SPS/PPS
sizes written as uint16_t.

The one intentional difference is inherited from the obs-nal port: on
NULL data with size 0 these functions give empty results, where C reads
address 0 in the start-code search.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@b7216309-jpg b7216309-jpg mentioned this pull request Oct 10, 2026
6 tasks done
zackees pushed a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
obs_avc_keyframe, obs_avc_find_startcode, obs_parse_avc_packet,
obs_parse_avc_packet_priority, obs_parse_avc_header and
obs_extract_avc_headers move to Rust. With ENABLE_RUST_LIBOBS=ON libobs
builds without obs-avc.c.

Every function walks the NAL units as the C loop does: skip zeros and
one more byte after each start code, stop when that reaches the end.
Outputs are bmalloc'ed like the C serializer and DARRAY buffers, with
the long reference count in front of a parsed packet. The SPS reader
keeps the C details: emulation-prevention bytes dropped, the
Exp-Golomb leading-zero cap from obs-rust#17, uint8_t results, and SPS/PPS
sizes written as uint16_t.

The one intentional difference is inherited from the obs-nal port: on
NULL data with size 0 these functions give empty results, where C reads
address 0 in the start-code search.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zackees pushed a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
obs_avc_keyframe, obs_avc_find_startcode, obs_parse_avc_packet,
obs_parse_avc_packet_priority, obs_parse_avc_header and
obs_extract_avc_headers move to Rust. With ENABLE_RUST_LIBOBS=ON libobs
builds without obs-avc.c.

Every function walks the NAL units as the C loop does: skip zeros and
one more byte after each start code, stop when that reaches the end.
Outputs are bmalloc'ed like the C serializer and DARRAY buffers, with
the long reference count in front of a parsed packet. The SPS reader
keeps the C details: emulation-prevention bytes dropped, the
Exp-Golomb leading-zero cap from obs-rust#17, uint8_t results, and SPS/PPS
sizes written as uint16_t.

The one intentional difference is inherited from the obs-nal port: on
NULL data with size 0 these functions give empty results, where C reads
address 0 in the start-code search.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zackees pushed a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
obs_avc_keyframe, obs_avc_find_startcode, obs_parse_avc_packet,
obs_parse_avc_packet_priority, obs_parse_avc_header and
obs_extract_avc_headers move to Rust. With ENABLE_RUST_LIBOBS=ON libobs
builds without obs-avc.c.

The NAL walk, AVCC conversion, priority scan and header split use the
shared helpers in obs-codec::nal; AVC adds its unit rating (IDR is a
keyframe, priority header >> 5) and the AVCDecoderConfigurationRecord
builder. The SPS reader keeps the C details: emulation-prevention bytes
dropped, the Exp-Golomb leading-zero cap from obs-rust#17, uint8_t results, and
SPS/PPS sizes written as uint16_t.

The one intentional difference is inherited from the obs-nal port: on
NULL data with size 0 these functions give empty results, where C reads
address 0 in the start-code search.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zackees pushed a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
obs_avc_keyframe, obs_avc_find_startcode, obs_parse_avc_packet,
obs_parse_avc_packet_priority, obs_parse_avc_header and
obs_extract_avc_headers move to Rust. With ENABLE_RUST_LIBOBS=ON libobs
builds without obs-avc.c.

The NAL walk, AVCC conversion, priority scan and header split use the
shared helpers in obs-codec::nal; AVC adds its unit rating (IDR is a
keyframe, priority header >> 5) and the AVCDecoderConfigurationRecord
builder. The SPS reader keeps the C details: emulation-prevention bytes
dropped, the Exp-Golomb leading-zero cap from obs-rust#17, uint8_t results, and
SPS/PPS sizes written as uint16_t.

The one intentional difference is inherited from the obs-nal port: on
NULL data with size 0 these functions give empty results, where C reads
address 0 in the start-code search.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
b7216309-jpg added a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
obs_avc_keyframe, obs_avc_find_startcode, obs_parse_avc_packet,
obs_parse_avc_packet_priority, obs_parse_avc_header and
obs_extract_avc_headers move to Rust. With ENABLE_RUST_LIBOBS=ON libobs
builds without obs-avc.c.

The port is AVC's classification on the NAL walk and packet helpers
already shared by the HEVC port: the unit type is the low 5 bits; an
IDR unit makes a keyframe; priority is header >> 5. On top of that, the
SPS reader keeps the C details: emulation-prevention bytes dropped, the
Exp-Golomb leading-zero cap from obs-rust#17, uint8_t results, and SPS/PPS
sizes written as uint16_t.

The one intentional difference is inherited from obs-nal: NULL data
with size 0 gives empty results, where C reads address 0 in the
start-code search.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
b7216309-jpg added a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
obs_avc_keyframe, obs_avc_find_startcode, obs_parse_avc_packet,
obs_parse_avc_packet_priority, obs_parse_avc_header and
obs_extract_avc_headers move to Rust. With ENABLE_RUST_LIBOBS=ON libobs
builds without obs-avc.c.

The port is AVC's classification on the NAL walk and packet helpers
already shared by the HEVC port: the unit type is the low 5 bits; an
IDR unit makes a keyframe; priority is header >> 5. On top of that, the
SPS reader keeps the C details: emulation-prevention bytes dropped, the
Exp-Golomb leading-zero cap from obs-rust#17, uint8_t results, and SPS/PPS
sizes written as uint16_t.

The one intentional difference is inherited from obs-nal: NULL data
with size 0 gives empty results, where C reads address 0 in the
start-code search.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
b7216309-jpg added a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
obs_avc_keyframe, obs_avc_find_startcode, obs_parse_avc_packet,
obs_parse_avc_packet_priority, obs_parse_avc_header and
obs_extract_avc_headers move to Rust. With ENABLE_RUST_LIBOBS=ON libobs
builds without obs-avc.c.

The port is AVC's classification on the NAL walk and packet helpers
already shared by the HEVC port: the unit type is the low 5 bits; an
IDR unit makes a keyframe; priority is header >> 5. On top of that, the
SPS reader keeps the C details: emulation-prevention bytes dropped, the
Exp-Golomb leading-zero cap from obs-rust#17, uint8_t results, and SPS/PPS
sizes written as uint16_t.

The one intentional difference is inherited from obs-nal: NULL data
with size 0 gives empty results, where C reads address 0 in the
start-code search.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zackees pushed a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
obs_avc_keyframe, obs_avc_find_startcode, obs_parse_avc_packet,
obs_parse_avc_packet_priority, obs_parse_avc_header and
obs_extract_avc_headers move to Rust. With ENABLE_RUST_LIBOBS=ON libobs
builds without obs-avc.c.

The NAL walk, AVCC conversion, priority scan and header split use the
shared helpers in obs-codec::nal; AVC adds its unit rating (IDR is a
keyframe, priority header >> 5) and the AVCDecoderConfigurationRecord
builder. The SPS reader keeps the C details: emulation-prevention bytes
dropped, the Exp-Golomb leading-zero cap from obs-rust#17, uint8_t results, and
SPS/PPS sizes written as uint16_t.

The one intentional difference is inherited from the obs-nal port: on
NULL data with size 0 these functions give empty results, where C reads
address 0 in the start-code search.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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-avc.c get_ue_golomb can evaluate 1 << 32 (undefined behavior) on a truncated SPS

1 participant