Skip to content

fix(libobs): obs_shutdown() lets a scene release its items before it destroys what is left (#3441) - #3442

Merged
vpacher merged 2 commits into
masterfrom
fix/3441-shutdown-control-uaf
Nov 1, 2026
Merged

vpacher merged 2 commits into
masterfrom
fix/3441-shutdown-control-uaf

Conversation

@vpacher

@vpacher vpacher commented Nov 1, 2026

Copy link
Copy Markdown
Member

Fixes #3441. Regression from #3431 (#3364), which is in obs-studio-rust-v0.1.0-beta.20261101.

What

obs_free_data() releases the main canvas, which queues the destroy of the scene it held on the destruction task thread. Since #3431 the left-alive loop frees the control block of each source it destroys. The scene's inputs stay in the source table until the scene's deferred destroy releases its items. When the loop reaches them first, the scene's teardown releases freed control blocks: a heap-use-after-free under ASan (test_source_profiler).

The app's own close path is not affected: OBSBasic calls ClearSceneData() before obs_shutdown(), which sets every output channel to nullptr and releases every scene and source. Only code that calls libobs directly and leaves a scene in the canvas at shutdown reaches it (tests, embedders, a plugin with a leak). This is the low-risk tier of SECURITY.md.

Fix

  • obs_free_data() waits until the destruction queue is empty before the left-alive loop. The scene's teardown then releases its items through obs_source_release(), which frees each control block in the normal way. Only the sources the host left alive stay in the tables.
  • The loop is plain obs_source_destroy() again. Before it, obs_free_data() takes a weak reference on the control block of each source left alive and gives the references back after the final queue wait. A deferred destroy that the loop starts (a scene the host left alive releasing its items) can drop a source's last strong reference. The hold keeps the block valid until then, and no block is released twice. The final wait also repeats until the queue is empty.

RED → GREEN

  • RED a8f30ed: a new case test_scene_items_are_released_not_left_alive in test_shutdown_canvas_weak_ref. A scene held only by the main canvas holds two inputs; a 200 ms delay in the scene's first item_remove signal (on the destruction thread, before the items are released) forces the order. It synchronizes nothing. Without ASan the use-after-free is invisible, so the case checks that shutdown logs no "source(s) were remaining".
    • Tier 2 (local gate): only test_shutdown_canvas_weak_ref fails, on left_alive_logged is not false.
    • asan, 30 runs on 4 loaded cores: use-after-free in 30/30 for the new case and for test_source_profiler.
  • GREEN 8c60ad4: local gate passes all lanes (stamped). asan, 30 runs: 0/30 for the new case, test_source_profiler and test_shutdown_type_lifetime. The two fix(libobs): obs_shutdown() frees the control block of a source left alive (#3364) #3431 cases still pass (nothing left after shutdown; a held weak reference frees on release).
  • Tiers: C shutdown code only, no Rust or protocol change; Tier 1, 3 and 4 do not apply. Test cost: the new case adds about 0.2 s.

Not in this PR

Two older hazards in the same loop are independent of #3431 and only reachable when the host leaks a scene at shutdown. They go to a separate low-priority issue: the unlocked iteration of the source tables while the destruction thread removes entries, and a leaked item's deferred destroy freeing the source before a leaked scene's teardown releases it.

🤖 Generated with Claude Code

vpacher and others added 2 commits November 1, 2026 20:42
…e it destroys what is left (#3441)

obs_free_data() releases the main canvas, which queues the destroy of
the scene it held on the destruction task thread. Since #3431 the
left-alive loop frees the control block of each source it destroys, and
the scene's inputs are still in the source table until its deferred
destroy releases its items. When the loop reaches them first, the
scene's teardown then releases freed control blocks: a heap-use-after-
free under ASan (test_source_profiler, 29 of 30 runs on 4 loaded
cores).

The new case holds the destruction thread for 200 ms in the scene's
first item_remove signal, which forces that order, and checks that
shutdown logs no "source(s) were remaining": the scene's inputs are
released by the scene, not destroyed as left alive. Without ASan the
use-after-free itself is invisible, so the log line is the check.

Fails: test_scene_items_are_released_not_left_alive (the left-alive
loop logs "2 source(s) were remaining").

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ore it destroys what is left (#3441)

obs_free_data() now waits until the destruction task queue is empty
before its left-alive loop. The destroy of the scenes the main canvas
held runs first, and their teardown releases their items through
obs_source_release(), which frees each control block in the normal
way. Only the sources the host left alive stay in the tables.

The left-alive loop is plain obs_source_destroy() again. Before it,
obs_free_data() takes a weak reference on the control block of each
source left alive, and gives the references back after the final queue
wait: a deferred destroy the loop starts (a scene the host left alive
releasing its items) can drop a source's last strong reference, and the
hold keeps the block valid until then. If that happened, the source's
own reference is already given back and only the hold goes; otherwise
both go, as #3431 did. The final wait also repeats until the queue is
empty, as obs_shutdown() does at its start.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=c6439dc37a0145fa5bc5028137f2ff1b1b6c424e secs=267 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run,macos-native:n/a,windows-native:n/a
Ci-Attestation: {"at":1793562864,"gate":"rust/all/fmt","host":"linux-x86_64","key":"117417cebcc0a1b4fb051550efdabe0640e2f346e85057d8d3a6aa5d45b65f9d","lane":"lint","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":40,"stamp":"e9ae2c5a591359279ea4d59b59c5b9ca","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}
Ci-Attestation: {"at":1793562864,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"117417cebcc0a1b4fb051550efdabe0640e2f346e85057d8d3a6aa5d45b65f9d","lane":"lint","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":40,"stamp":"daf439d257486fe748af24fb3d71b09d","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}
Ci-Attestation: {"at":1793562864,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"970d1ef2bb26c86fb8b9fa766981824e53d382bd8227ee69ae94ff8523b7272e","lane":"rust-test","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":82,"stamp":"d97c9400078326df9e93273f14b0a30b","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}
Ci-Attestation: {"at":1793562864,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"cb88e9fcab2c55994b3ec27de674531ca9553155a446b5924672d4f18e0552e0","lane":"cross","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":38,"stamp":"cb29f82d2eaa8c53c797e8adf0083bd1","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}
Ci-Attestation: {"at":1793562864,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"cb88e9fcab2c55994b3ec27de674531ca9553155a446b5924672d4f18e0552e0","lane":"cross","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":38,"stamp":"7e0cfdd270b31e2f74b4960f19a95015","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}
Ci-Attestation: {"at":1793562864,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"cb88e9fcab2c55994b3ec27de674531ca9553155a446b5924672d4f18e0552e0","lane":"cross","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":38,"stamp":"d0c4ec32a4c02f59e541dc30345af231","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}
Ci-Attestation: {"at":1793562864,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"cb88e9fcab2c55994b3ec27de674531ca9553155a446b5924672d4f18e0552e0","lane":"cross","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":38,"stamp":"fd6ed26841f820aac6456c6dbe5aab41","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}
Ci-Attestation: {"at":1793562864,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"470964d90453530511fbad59c537a129df93ee9028f232f3691bc0f3201b6e92","lane":"tier2-linux","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":266,"stamp":"ceaaa5181f74c96bf81243c9b5a0190d","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}

@zackees zackees left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed. Draining the destruction queue before the left-alive loop and pinning each remaining control block with a weak ref closes the #3441 use-after-free; the obs_weak_ref_expired check correctly separates sources destroyed directly (their strong-side weak ref still held) from those released by a deferred scene teardown. The RED case extends a repo-added test (08aed4e), so it's within policy. LGTM; I'll queue it once the last check is green.

@vpacher
vpacher added this pull request to the merge queue Nov 1, 2026
Merged via the queue into master with commit 6a679f0 Nov 1, 2026
37 checks passed
@vpacher
vpacher deleted the fix/3441-shutdown-control-uaf branch November 1, 2026 20:44
@obs-rust-project-manager obs-rust-project-manager Bot moved this from In review to Done in obs-rust Multiview Nov 1, 2026
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: obs_shutdown() frees a source's control block while a scene's deferred destroy still releases it (use-after-free, regression from #3431)

2 participants