Skip to content

libobs: Free a refused type's data only when its fields are whole - #2202

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/handle-error-partial
Oct 24, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/handle-error-partial

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator

Fixes #2195

What and why

When obs_register_source_s(), obs_register_output_s(), obs_register_encoder_s() or obs_register_service_s() refused a type, HANDLE_ERROR (libobs/obs-module.c.h) copied the first size bytes of the caller's structure into a zeroed one, then called free_type_data(type_data) when both were non-NULL. A size that ended inside free_type_data left a partly copied pointer (some low bytes of the caller's, then zeros), and HANDLE_ERROR called it. With half the pointer copied that is a jump into unmapped memory. With all but the top byte copied it is, on 64-bit Linux, usually the right address by luck, because the top byte of a user-space pointer is 0.

HANDLE_ERROR now uses type_data and free_type_data only when each lies wholly within the size (offsetof + sizeof <= size, the rule HAS_VAL() already applies to the callbacks), and reads them from the caller's structure, so the copy is gone. A size of 0 still frees nothing. Every registration that passes sizeof() of its structure, as the obs_register_*() macros and every bundled plugin do, behaves exactly as before, and so does every size that ends between fields. Only a plugin that passes a size ending inside one of the two pointers is affected. Upstream has the same code.

Two commits:

  1. RED: test_register_refused_type_data, a new real-libobs test, registered in test-graph.json in sorted order, under xvfb-run where a display is needed (as test_source_registration). It is new rather than an extension: test_source_registration covers sources only and test_module_type_rules_steps (libobs: Port the output, encoder and service registration rules to obs-core #2194) outputs, encoders and services only, and both run every case in one process, where a jump through a partial pointer would take down the other cases. One test covers all four registration functions through the public API.
    • Each type is registered without get_name, so it is refused, with type_data set to a byte pattern the test never dereferences (0xA1B2C3D4E5F60718), so a partly copied type_data cannot match it.
    • Five sizes per type: ending halfway through type_data, halfway through free_type_data, one byte short of the end of free_type_data, exactly at its end, and sizeof. That is 20 cases.
    • Each case checks that the type is not registered, that free_type_data ran once with the caller's type_data when both fields lie wholly within the size, and that it did not run otherwise.
    • Each registration runs in a forked child that reports back through a pipe, so a jump into garbage fails only its own case (the pattern of test_output_null_id). A 30 s alarm ends a child that jumps into code that loops. Windows has no fork(), so the cases run in the test process there.
  2. GREEN: the HANDLE_ERROR change, and a docs/features.md line under fixed upstream bugs (Stability).

The Rust models of the registrations already assume whole fields: rust/obs-core/tests/obs_core/module_source_rules.rs frees only when both fields are inside() the size, and the model of the output, encoder and service registrations does the same. No Rust changes.

Overlap: #2194 (merged while this was prepared) moved the output, encoder and service rules out of obs-module.c.h and deleted CHECK_REQUIRED_VAL and CHECK_REQUIRED_VAL_EITHER just above HANDLE_ERROR; HANDLE_ERROR stayed. The macro's first two lines are kept as they were, so this change merged with the #2194 head without conflicts before it landed, and the branch rebased onto it cleanly. test_module_type_rules_steps (#2194) cuts neither type_data nor free_type_data in any of its sizes, and passes unchanged.

A related bug, not fixed here (C, REGISTER_OBS_DEF and obs_register_source_s(), unchanged; upstream has the same code): an accepted registration copies the caller's structure cut at size, and the cut can fall inside any pointer after the last required callback. That pointer is stored partly copied. For free_type_data it is then called when the type is freed at shutdown (obs.c.h, the FREE_REGISTERED_TYPES loop and the source types), and an optional callback cut the same way is called whenever OBS uses it. Again only a plugin passing a size that is not sizeof() of its structure can reach it. A fix would zero a field the size cuts, after the memcpy. That changes what an accepted type looks like, so it is left for its own issue.

RED → GREEN

  • RED commit: fc6a07dc2 test: Expect a refused registration to free only whole type data fields (evidence from 27ea42d4d, the same commit before two rebases onto master). test_register_refused_type_data fails 8 of 20 on the C backend libobs, against the hybrid libobs and against libobs-c-reference, the same 8 on each, without taking down the binary:

    • halfway through free_type_data (sizes 228 source, 140 output, 132 encoder, 140 service): the child dies with signal 11;
    • one byte short of the end (231, 143, 135, 143): free_type_data is called once with 0xa1b2c3d4e5f60718, through the partly copied pointer, which is whole by luck;
    • the 12 others pass: halfway through type_data nothing is freed (free_type_data is not copied at all), and at the end of free_type_data and at sizeof it is freed once with the caller's type_data.

    test_source_registration passes 10/10 on all three.

  • GREEN: 066992505.

  • Real app (UI, protocol output or logging changes): not applicable, no UI, protocol or log change.

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits (N/A for docs-only; a focused CI fix may push RED and GREEN together)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

When obs_register_source_s(), obs_register_output_s(),
obs_register_encoder_s() or obs_register_service_s() refuses a type,
HANDLE_ERROR() copies the first `size` bytes of the caller's structure into
a zeroed one and calls free_type_data(type_data) when both are set. A size
that ends inside free_type_data leaves a partly copied pointer, and
HANDLE_ERROR() calls it.

test_register_refused_type_data registers each of the four types without
get_name through the public API, at sizes that end inside type_data, halfway
through free_type_data, one byte short of its end, exactly at its end, and
at sizeof. free_type_data must be called only when both fields lie wholly
within the size, and then once with the caller's type_data, a byte pattern
that a partial copy cannot match. Each registration runs in a forked child,
so a jump through a partial pointer fails only its own case.

Refs obs-rust#2195
When obs_register_source_s(), obs_register_output_s(),
obs_register_encoder_s() or obs_register_service_s() refused a type,
HANDLE_ERROR() copied the first `size` bytes of the caller's structure into
a zeroed one and called free_type_data(type_data) when both were set. A
size that ended inside free_type_data left a partly copied pointer (some
low bytes of the caller's, then zeros), and HANDLE_ERROR() called it: a
jump to a wrong address, or to the right one by luck when the missing bytes
were zero anyway.

HANDLE_ERROR() now uses type_data and free_type_data only when each lies
wholly within the size (offsetof + sizeof <= size, the rule HAS_VAL()
already applies to the callbacks), and reads them from the caller's
structure, so the copy is gone. A size of 0 still frees nothing, and every
registration that passes sizeof() of its structure, as the obs_register_*()
macros do, behaves as before.

docs/features.md gets a line under fixed upstream bugs.

Fixes obs-rust#2195
@rdoupe
rdoupe requested a review from zackees as a code owner October 24, 2026 19:26
@zackees
zackees added this pull request to the merge queue Oct 24, 2026
Merged via the queue into obs-rust:master with commit 6b6d3ca Oct 24, 2026
35 checks passed
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.

libobs: A refused registration can call a partly copied free_type_data pointer

2 participants