Skip to content

callback: Track the running global signal callback by index - #1639

Merged
zackees merged 2 commits into
obs-rust:masterfrom
vpacher:fix/1441-global-callback-index
Oct 21, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
vpacher:fix/1441-global-callback-index

Conversation

@vpacher

@vpacher vpacher commented Oct 21, 2026

Copy link
Copy Markdown
Member

Refs #1441

Follow-up to #1556 for the global signal callbacks. GHSA-rjwf-j53r-47qr is published and #1556 fixes its signal-callback part. This PR fixes the global-callback part, as requested in #1441. All code changes are in libobs/callback/signal.c.h. signal.c has no Rust port on master yet, so there is no Rust side to change. The port (#1441) follows in a separate PR on top of this one.

What and why

signal_handler_signal kept a pointer into handler->global_callbacks.array while a global callback ran. The running callback used it for signal_handler_remove_current() and for the signaling count. Three cases move the entry while the pointer is held:

  1. Connect, then remove itself. The callback connects enough global callbacks to reallocate the array, then calls signal_handler_remove_current(). The pointer refers to freed memory. The write goes there, and the callback stays connected. Without remove_current, cb->signaling-- writes to freed memory too.
  2. Nested signal. An inner pass erases an earlier removed entry while the outer pass still walks the list. The running entry moves down one slot. Its removal and its signaling-- go to the next callback, and the outer pass skips that callback.
  3. Disconnect during the pass. signal_handler_disconnect_global erases an earlier entry at once, because that entry is not running. The result is the same as in case 2.

The fix follows the signal callbacks of #1556:

  • The running global callback is a (handler, index) context.
  • The handler counts the running global passes (global_signalling). Removed entries are erased only when the outermost pass ends.
  • signal_handler_disconnect_global only marks the entry while a global pass runs.
  • The per-entry signaling count is no longer needed and is removed. struct global_callback_info is private to signal.c.h.

test_signal is now built with ASan (obs_test_sanitize(test_signal address)). ASan's allocator moves every reallocated block, so case 1 does not depend on the platform allocator. libobs is not instrumented, so ASan does not report the write itself. The test sees the wrong result.

No existing assertion changes. Three tests are new:

Test Case Expected
global_connect_then_remove_current_test 1 callback connects 64 globals, removes itself: called once over two signals, the 64 run in the same pass
global_nested_pass_test 2 outer a, outer b, inner b, inner c, outer c (5 calls); then c only
global_disconnect_during_signal_test 3 y, x, z (3 calls); then z only

RED → GREEN

Linux x86_64 (Debian 13, glibc), CMake build on 0b10a2f, frontend off, -DOBS_BUILD_C_REFERENCE=ON. test_signal ran against the hybrid libobs and against libobs-c-reference; the loader check confirmed the selected library each time. signal.c is not ported, so both cores run this C.

  • RED 8c7af05: 3 of 19 fail on both cores: global_connect_then_remove_current_test (2 != 1), global_nested_pass_test (4 != 5), global_disconnect_during_signal_test (2 != 3). Before the rebase, the same 3 also failed without ASan (glibc moved the array in 5 of 5 runs).
  • GREEN 89e2cef: 19/19 on both cores. Before the rebase, also 19/19 without ASan.
  • Full build with -k 0: only the 3 test_xcomposite_* targets fail to compile (older Debian cmocka without assert_int_in_range; not related). ctest without xcomposite|xshm: 343/344. The one failure, test_v4l2_busy_signal, needs an X display; under xvfb-run it passes 5/5.
  • ci/local_gate.py --only clang-format and --only gersemi: ok.

Checklist

vpacher and others added 2 commits October 21, 2026 20:08
…st#1441)

signal_handler_signal keeps a pointer into the global callback array
while a global callback runs, and the running callback uses it for
signal_handler_remove_current and the signaling count. Three cases
move that entry while the pointer is held:

- The callback connects enough global callbacks to reallocate the
  array, then removes itself (heap use-after-free, GHSA-rjwf-j53r-47qr).
- A nested signal erases an earlier removed entry, then the outer
  callback removes itself.
- The callback disconnects an earlier global callback, then removes
  itself.

In the last two, the removal marks the next callback instead, and the
pass skips that callback. The signal callbacks handle the same cases
since obs-rust#1556.

Build test_signal with ASan. Its allocator moves every reallocated
block, so the first case does not depend on the platform allocator.
libobs is not instrumented, so ASan does not report the write itself;
the test sees the wrong result. Without ASan, glibc moves the array
here too (5 of 5 runs fail).

Refs obs-rust#1441

Co-Authored-By: Claude Opus 5.5 <[email protected]>
signal_handler_signal kept a pointer into the global callback array
while a global callback ran. A connect from the callback could
reallocate the array, and a nested signal or a disconnect could erase
an earlier entry and move the running one. signal_handler_remove_current
then wrote to freed memory or marked the next callback, and the
signaling count went to the wrong entry.

Follow the signal callbacks (obs-rust#1556):
- The running global callback is a (handler, index) context.
- The handler counts the running global passes. Removed entries are
  erased only when the outermost pass ends.
- signal_handler_disconnect_global only marks the entry while a global
  pass runs.

The per-entry signaling count is no longer needed and is removed.

Refs obs-rust#1441

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vpacher
vpacher requested a review from zackees as a code owner October 21, 2026 18:20
@zackees
zackees enabled auto-merge October 21, 2026 18:21
@zackees
zackees added this pull request to the merge queue Oct 21, 2026
Merged via the queue into obs-rust:master with commit 722d4c9 Oct 21, 2026
25 checks passed
@vpacher vpacher mentioned this pull request Oct 21, 2026
5 tasks done
@vpacher

vpacher commented Oct 21, 2026

Copy link
Copy Markdown
Member Author

Confirmed: both reproductions from GHSA-rjwf-j53r-47qr pass on master 01561bf.

Setup: libobs built with ASan (C backend, Linux x86_64, Debian 13). The PoCs are built with -fsanitize=address and linked against that libobs. The signal PoC is the one in the advisory, unchanged. The global PoC is the variant from the advisory comment: a global callback calls signal_handler_connect_global 8 times, once without and once with signal_handler_remove_current().

libobs signal PoC global, connect only global + remove_current
4f270ee (before #1556) heap-use-after-free, WRITE of size 1 in signal_handler_remove_current heap-use-after-free, READ of size 8 in signal_handler_signal heap-use-after-free, WRITE of size 1 in signal_handler_remove_current
d412d8b (after #1556, before #1639) clean heap-use-after-free, READ of size 8 in signal_handler_signal (signal.c.h:345) heap-use-after-free, WRITE of size 1 in signal_handler_remove_current (signal.c.h:288)
01561bf (with #1639) clean, exit 0 clean, exit 0 clean, exit 0

The loader check confirmed the ASan libobs in each run. The "partly fixed" note can be removed from my side.

vpacher added a commit to vpacher/obs-studio that referenced this pull request Oct 21, 2026
The ten signal_handler_* exports move into obs-callback.
signal_handler_add_array stays inline in signal.h. The handle is one
bmalloc block that holds an Arc, as in the audio-controls port, so
bnum_allocs sees the handler until its last reference goes
(keep_ref_remove_current_last_ref_test reads it). Both are freed when
the C reference count reaches zero. signal_handler_signal keeps its
own clone while callbacks run and frees the handler after the pass
when the pass gave back the last reference. The log texts are the C
ones.

callback/signal.c moves into the ported group libobs.ported-callback,
on its header.

The audio-controls oracle defined signal_handler_connect and
signal_handler_disconnect as stubs. A test binary that also links
obs-callback, such as obs-tests-core, would define them twice (GNU ld
stops). Rename the stubs to oracle_audio_controls_* with #define, as
the oracle already does for its own exports.

Tier 3 runs the original signal.c.h, as fixed by obs-rust#1639, on the decl
oracle (oracle/signal.c). Random scripts of add, connect, connect_ref,
disconnect, connect_global, disconnect_global and signal run with
re-entrant actions. Signal callbacks remove the current one, disconnect
or connect another, connect then remove, or start a nested signal.
Global callbacks remove the current one, disconnect themselves,
connect or disconnect another global callback, connect then remove, or
start a nested signal. The test compares every callback call in order
and every logged line. Excluded: a destroy from a callback (C frees the
handler under the running pass). Against the core without the obs-rust#1639
rule, Tier 3 finds a failing script.

Checks on Linux x86_64 (Debian 13), CMake with OBS_BUILD_C_REFERENCE:
test_signal.c passes 19/19 against the hybrid libobs and against
libobs-c-reference (loader checked). ctest against the hybrid passes
344/344 under xvfb-run, without the xcomposite/xshm tests that do not
build on Debian's cmocka. obs-tests-core passes 422/422 with GNU ld.

Refs obs-rust#1441

Co-Authored-By: Claude Opus 5.5 <[email protected]>
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 21, 2026
* test: add failing obs-callback signal tier 1 (Refs obs-rust#1441)

Mirror test/cmocka/test_signal.c, as fixed by obs-rust#1556 and obs-rust#1639, against
todo stubs in obs-callback. Add: global connect and disconnect matched by
callback and data, a connect of many callbacks then remove_current
from the running callback, a global signal in a nested pass, and a
disconnect from another thread that waits for the running pass.

From obs-rust#1639: a global callback that connects more global callbacks
then removes itself, a nested pass that must not erase an earlier
removed global callback, and a global disconnect during the pass.

Refs obs-rust#1441

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

* obs-callback: signal handler core in Rust (Refs obs-rust#1441)

The safe core follows signal.c.h as fixed by obs-rust#1556 and obs-rust#1639: per-signal and
global re-entrant locks held while callbacks run, deferred removal
while signalling, keep_ref references, and the thread-local running
callback. The running callback is a (signal, index) pair instead of a
pointer into the callback array, so a connect from the callback cannot
leave it dangling.

As in the fixed C:
- Passes of one signal nest by count, and so do the global passes.
  Removed callbacks are erased only when the outermost pass ends. A
  disconnect while a pass runs only marks the callback. The running
  global callback is an index, as the running signal callback is.
- Each callback call saves and restores both running callbacks. A
  signal callback clears the global one, a global callback clears the
  signal one. remove_current marks the signal one first.
- Global callbacks are found by callback and data only.
- A keep_ref reference given back after a pass destroys the handler
  when it was the last one. signal() reports it to the owner.

Refs obs-rust#1441

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

* obs-callback: swap signal_handler to Rust (Refs obs-rust#1441)

The ten signal_handler_* exports move into obs-callback.
signal_handler_add_array stays inline in signal.h. The handle is one
bmalloc block that holds an Arc, as in the audio-controls port, so
bnum_allocs sees the handler until its last reference goes
(keep_ref_remove_current_last_ref_test reads it). Both are freed when
the C reference count reaches zero. signal_handler_signal keeps its
own clone while callbacks run and frees the handler after the pass
when the pass gave back the last reference. The log texts are the C
ones.

callback/signal.c moves into the ported group libobs.ported-callback,
on its header.

The audio-controls oracle defined signal_handler_connect and
signal_handler_disconnect as stubs. A test binary that also links
obs-callback, such as obs-tests-core, would define them twice (GNU ld
stops). Rename the stubs to oracle_audio_controls_* with #define, as
the oracle already does for its own exports.

Tier 3 runs the original signal.c.h, as fixed by obs-rust#1639, on the decl
oracle (oracle/signal.c). Random scripts of add, connect, connect_ref,
disconnect, connect_global, disconnect_global and signal run with
re-entrant actions. Signal callbacks remove the current one, disconnect
or connect another, connect then remove, or start a nested signal.
Global callbacks remove the current one, disconnect themselves,
connect or disconnect another global callback, connect then remove, or
start a nested signal. The test compares every callback call in order
and every logged line. Excluded: a destroy from a callback (C frees the
handler under the running pass). Against the core without the obs-rust#1639
rule, Tier 3 finds a failing script.

Checks on Linux x86_64 (Debian 13), CMake with OBS_BUILD_C_REFERENCE:
test_signal.c passes 19/19 against the hybrid libobs and against
libobs-c-reference (loader checked). ctest against the hybrid passes
344/344 under xvfb-run, without the xcomposite/xshm tests that do not
build on Debian's cmocka. obs-tests-core passes 422/422 with GNU ld.

Refs obs-rust#1441

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@vpacher
vpacher deleted the fix/1441-global-callback-index branch October 22, 2026 07:36
@zackees

zackees commented Oct 28, 2026

Copy link
Copy Markdown
Member

This is also the fix for the second half of GHSA-rjwf-j53r-47qr, and I want the record on the PR that closed it.

@vpacher reported on that advisory (before it was published) that #1556 fixed only the signal-callback path: the global-callback path still held current_global_cb and the cb used for cb->signaling-- as pointers into global_callbacks.array, so a global callback that calls signal_handler_connect_global() reallocated the array under it — ASan heap-use-after-free at the old signal.c.h:345, plus a 1-byte write at line 288. He asked for the running callback to be tracked by index and held his C fix back pending that decision.

This PR is exactly that change (current_global_cb → {handler, index}, per-entry writes through the index, nesting counted on the handler, erasure only at the outermost pass), and it merged 2026-10-21T18:53Z — 1h24m after the advisory was published, which is why the published text names only the #1556 half.

Follow-up, so it does not sit in the same state as before: the advisory is published, so its comment thread is closed and his reply cannot be posted there. I have asked whether #1639 alone closes his repro. If it reproduces on current_global_cb-free master, the stack belongs here rather than on the advisory.

@vpacher

vpacher commented Oct 28, 2026

Copy link
Copy Markdown
Member Author

The repro check is above (my comment of 2026-10-21, after the merge): with #1639 on master 01561bf, the signal PoC and both global variants are clean under ASan, and the two builds before it show the use-after-free. So #1639 closes it. Nothing is open on my side for GHSA-rjwf-j53r-47qr.

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