Skip to content

libobs: Keep AV1 OBU parsing inside the buffer - #103

Merged
zackees merged 3 commits into
obs-rust:masterfrom
b7216309-jpg:fix/av1-obu-bounds
Oct 10, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
b7216309-jpg:fix/av1-obu-bounds

Conversation

@b7216309-jpg

Copy link
Copy Markdown
Contributor

Description

parse_obu_header in libobs/obs-av1.c trusts the stream, which leads to out-of-bounds reads and undefined behavior on malformed or merely unusual AV1 packets:

  1. Heap over-read. An OBU's size field is never checked against what is left. obs_extract_av1_headers then copies obu_start + obu_size bytes, so an OBU that claims 16 bytes in a 4-byte packet copies 18. Checked against the C compiled as the Rust test oracle: {0x32, 16, 0x10, 0x11} gives a packet of 18 bytes, 14 of them read past the end.
  2. Undefined shift. leb128 computes (leb128_byte & 0x7f) << (i * 7) in int. From the 5th byte on it overflows or shifts past 31 bits, and the negative result sign-extends into the 64-bit size.
  3. Off by one without a size field. The size is size - 1, but AV1 5.3.1 says sz - 1 - obu_extension_flag. An OBU with an extension byte and no size field is therefore sized one byte past the end, even in a well-formed stream.
  4. Underflow. A 1-byte OBU announcing an extension byte makes size - *obu_start underflow, and leb128 then reads past the buffer.

The fix:

  • shifts in 64 bits;
  • caps the header and extension byte at the buffer;
  • uses the spec's size when there is no size field;
  • cuts the OBU size to what is left.

With that, start always advances by at least one byte and never passes end. Well-formed streams parse as before, except case 3, which now follows the spec.

Motivation and Context

Found while porting obs-av1.c to Rust (#45). The port matches the fixed C, and its parity test can then run on arbitrary bytes. Same approach as #53 and #54: fix the C first, then port.

Refs #45

How Has This Been Tested?

  • test/cmocka/test_av1.c is new: keyframe detection (key, inter, show_existing_frame, frame header OBU, none), the header/packet split, metadata_obu and the T.35 wrapper byte for byte (including a two-byte leb128 size), plus one regression per bug above. The test is committed first, so it is RED on current master.
  • I can't run cmocka here (Windows), so I checked the same expectations against obs-av1.c compiled as the Rust oracle. With master's file the oversized-OBU case reports a packet of 18 bytes for a 4-byte input. With this fix every case passes, and 4096 arbitrary and structured inputs match the Rust port, which is stacked on rust: Port obs-hevc.c to obs-codec #98.
  • python ci/local_gate.py --lane lint passes.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.

🤖 Generated with Claude Code

b7216309-jpg and others added 3 commits October 10, 2026 16:47
Pins obs-av1.c on well-formed streams:
- keyframes come from the first frame (header) OBU: show_existing_frame
  is never a key frame, frame_type 0 is;
- sequence header and metadata OBUs are copied to the header while
  every OBU stays in the packet;
- metadata_obu and the ITU-T T.35 wrapper build the OBU byte for byte,
  with a two-byte leb128 size above 127.

Adds regressions that fail on the current code:
- an OBU whose size field claims more than the buffer holds is copied
  past the end;
- a 5-byte leb128 size is shifted in int, which overflows;
- without a size field, an OBU with an extension byte is sized
  size - 1, one byte past the end;
- a lone header announcing an extension byte and a size field
  underflows size - obu_start.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
parse_obu_header trusted the stream:
- leb128 shifted each byte as an int, which overflows (undefined
  behavior) from the 5th byte on and sign-extends into the 64-bit size;
- an OBU's size was never checked against what was left, so
  obs_extract_av1_headers copied past the end of the packet;
- without a size field the size was size - 1, one byte too many when
  an extension byte is present (AV1 spec 5.3.1: sz - 1 -
  obu_extension_flag);
- a 1-byte OBU announcing an extension byte made size - obu_start
  underflow.

Now the shift is done in 64 bits, the header and extension byte are
capped at the buffer, the no-size-field case follows the spec, and the
size is cut to what is left. Well-formed streams parse as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@b7216309-jpg b7216309-jpg mentioned this pull request Oct 10, 2026
6 tasks done
@zackees
zackees merged commit 604692f into obs-rust:master Oct 10, 2026
22 checks passed
@zackees zackees 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_av1_keyframe, obs_extract_av1_headers, metadata_obu and
metadata_obu_itu_t35 move to Rust. With ENABLE_RUST_LIBOBS=ON libobs
builds without obs-av1.c.

The OBU walk follows obs-av1.c with the bounds fix from obs-rust#103:
- leb128 reads at most 8 bytes into 64 bits;
- a missing extension byte ends the OBU;
- without a size field the OBU is what follows the header;
- the size is cut to what is left, cast as size_t first, as in C.

Every OBU is at least its header byte, so the walk always advances and
never leaves the buffer. Keyframes come from the first frame or frame
header OBU with a payload. metadata_obu builds the header, leb128 size,
type, payload and 0x80 trailing bits. No intentional differences.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zackees added a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
Feeds arbitrary bytes to the Rust obs-av1 shims and the C oracle
(obs-av1.c with the obs-rust#103 bounds fix) and asserts identical results, per
the bitstream-parser fuzz rule in docs/rust-port/testing-policy.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=7dae7da263cc7ee9bac137fc2b28f7de1a108c2f secs=40 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791649335,"gate":"rust/all/fmt","host":"linux-x86_64","key":"8bd2d3327e9fbbc6d26786ce830b37d486281c5f4e9b1152cbeea81fd90b0552","lane":"lint","parents":["f4d405b67706bb2234e7f426c04336a2b2d2137a"],"secs":11,"stamp":"34d359ec7f4783b18908b20d686a10f6","tree":"7dae7da263cc7ee9bac137fc2b28f7de1a108c2f","v":1,"via":"run"}
Ci-Attestation: {"at":1791649335,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"8bd2d3327e9fbbc6d26786ce830b37d486281c5f4e9b1152cbeea81fd90b0552","lane":"lint","parents":["f4d405b67706bb2234e7f426c04336a2b2d2137a"],"secs":11,"stamp":"9e5e97e6eaf5f09d898741f70a0f0353","tree":"7dae7da263cc7ee9bac137fc2b28f7de1a108c2f","v":1,"via":"run"}
Ci-Attestation: {"at":1791649335,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"9481ccf926ba37f3f220810f23196fbb2d3a86735d80c56b7c7b037b72c56f15","lane":"rust-test","parents":["f4d405b67706bb2234e7f426c04336a2b2d2137a"],"secs":10,"stamp":"a5312266717d36f6c303aa18fd24e713","tree":"7dae7da263cc7ee9bac137fc2b28f7de1a108c2f","v":1,"via":"run"}
Ci-Attestation: {"at":1791649335,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"c45067f21626755b51b0add2d47df4b2a1d1e572595e4b6ef79afc270bccf461","lane":"tier2-linux","parents":["f4d405b67706bb2234e7f426c04336a2b2d2137a"],"secs":19,"stamp":"a3b1a7be55d4b308708556430a892608","tree":"7dae7da263cc7ee9bac137fc2b28f7de1a108c2f","v":1,"via":"run"}
zackees pushed a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
obs_av1_keyframe, obs_extract_av1_headers, metadata_obu and
metadata_obu_itu_t35 move to Rust. With ENABLE_RUST_LIBOBS=ON libobs
builds without obs-av1.c.

The OBU walk follows obs-av1.c with the bounds fix from obs-rust#103:
- leb128 reads at most 8 bytes into 64 bits;
- a missing extension byte ends the OBU;
- without a size field the OBU is what follows the header;
- the size is cut to what is left, cast as size_t first, as in C.

Every OBU is at least its header byte, so the walk always advances and
never leaves the buffer. Keyframes come from the first frame or frame
header OBU with a payload. metadata_obu builds the header, leb128 size,
type, payload and 0x80 trailing bits. No intentional differences.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zackees added a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
Feeds arbitrary bytes to the Rust obs-av1 shims and the C oracle
(obs-av1.c with the obs-rust#103 bounds fix) and asserts identical results, per
the bitstream-parser fuzz rule in docs/rust-port/testing-policy.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=2ef89b983446d354ce88b2a54222cc8a4ddb9397 secs=51 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791655036,"gate":"rust/all/fmt","host":"linux-x86_64","key":"1380afbee8635b904ffd8c1c128277080a1641d6e28908d0b5c36118716e34d3","lane":"lint","parents":["d4c6fa63737da50e03e91f42e3d4a3c2d2a245fc"],"secs":16,"stamp":"7312fa19791786bba2b1ca041c0e9754","tree":"2ef89b983446d354ce88b2a54222cc8a4ddb9397","v":1,"via":"run"}
Ci-Attestation: {"at":1791655036,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"1380afbee8635b904ffd8c1c128277080a1641d6e28908d0b5c36118716e34d3","lane":"lint","parents":["d4c6fa63737da50e03e91f42e3d4a3c2d2a245fc"],"secs":16,"stamp":"bd48d2920de5fb57e2615829a50a8137","tree":"2ef89b983446d354ce88b2a54222cc8a4ddb9397","v":1,"via":"run"}
Ci-Attestation: {"at":1791655036,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"c128be0d5735dac595bd6fc9a1777919eef205c61b3279a0ac497d649a7b97ea","lane":"rust-test","parents":["d4c6fa63737da50e03e91f42e3d4a3c2d2a245fc"],"secs":17,"stamp":"eba6d95b736c9c648ca4d63f99f474f5","tree":"2ef89b983446d354ce88b2a54222cc8a4ddb9397","v":1,"via":"run"}
Ci-Attestation: {"at":1791655036,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"ef3f06be9dbcf9f1be0afe563b807b85b9e38e4a32dc36ca1d5451e50ad7c116","lane":"tier2-linux","parents":["d4c6fa63737da50e03e91f42e3d4a3c2d2a245fc"],"secs":18,"stamp":"6614963acde57d1c87968b31b7c1d42a","tree":"2ef89b983446d354ce88b2a54222cc8a4ddb9397","v":1,"via":"run"}
zackees pushed a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
obs_av1_keyframe, obs_extract_av1_headers, metadata_obu and
metadata_obu_itu_t35 move to Rust. With ENABLE_RUST_LIBOBS=ON libobs
builds without obs-av1.c.

The OBU walk follows obs-av1.c with the bounds fix from obs-rust#103:
- leb128 reads at most 8 bytes into 64 bits;
- a missing extension byte ends the OBU;
- without a size field the OBU is what follows the header;
- the size is cut to what is left, cast as size_t first, as in C.

Every OBU is at least its header byte, so the walk always advances and
never leaves the buffer. Keyframes come from the first frame or frame
header OBU with a payload. metadata_obu builds the header, leb128 size,
type, payload and 0x80 trailing bits. No intentional differences.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zackees added a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
Feeds arbitrary bytes to the Rust obs-av1 shims and the C oracle
(obs-av1.c with the obs-rust#103 bounds fix) and asserts identical results, per
the bitstream-parser fuzz rule in docs/rust-port/testing-policy.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=9ce794709fd0a6ffd4dbdbb73593a7ea5dd67be4 secs=56 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791656801,"gate":"rust/all/fmt","host":"linux-x86_64","key":"a462edd75576cc3f895aa589808b34f03df55969110a0b83c50da8acba70e0bd","lane":"lint","parents":["fe63e450129c1eeb4b1e1dc1272b678b7305f7d6"],"secs":13,"stamp":"3b9d19058a18740a8119265176b3d64b","tree":"9ce794709fd0a6ffd4dbdbb73593a7ea5dd67be4","v":1,"via":"run"}
Ci-Attestation: {"at":1791656801,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"a462edd75576cc3f895aa589808b34f03df55969110a0b83c50da8acba70e0bd","lane":"lint","parents":["fe63e450129c1eeb4b1e1dc1272b678b7305f7d6"],"secs":13,"stamp":"2e06ead6cace504173cfde20d9d37f9c","tree":"9ce794709fd0a6ffd4dbdbb73593a7ea5dd67be4","v":1,"via":"run"}
Ci-Attestation: {"at":1791656801,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"d9f9eb5090c7a44b3f2f23547f7d5ca46a72d7107ef48180b3cd47a692710a8f","lane":"rust-test","parents":["fe63e450129c1eeb4b1e1dc1272b678b7305f7d6"],"secs":19,"stamp":"bdf6d4d57719dca99c2e4fdc58f985c0","tree":"9ce794709fd0a6ffd4dbdbb73593a7ea5dd67be4","v":1,"via":"run"}
Ci-Attestation: {"at":1791656801,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"8168c5812db09dd001ecdd524685622f754df9c62269d3d1f0d76dccc610456d","lane":"tier2-linux","parents":["fe63e450129c1eeb4b1e1dc1272b678b7305f7d6"],"secs":24,"stamp":"f4a4bb74e724e8dc8cb2544f8b9651d7","tree":"9ce794709fd0a6ffd4dbdbb73593a7ea5dd67be4","v":1,"via":"run"}
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