Skip to content

bug: libobs: unregister_hotkey() frees the hotkey before remove_bindings() reads it (UAF; macOS Tier 2 red) #3044

Description

@zackees

What

test/cmocka/test_hotkey_binding_press_steps (from #3032, merged as 93b5e64db) fails on the macOS Tier 2 lane. It is the only failing test in the lane (382/383 pass), it is not flaky-looking, and no PR has run that lane since the test landed, because PR CI only selects macOS Tier 2 when the change is in scope: this was found by a workflow-only PR that selects it.

Run: https://github.com/obs-rust/obs-studio/actions/runs/41036571990/job/123998913951 (PR #3042, head c6a527ee6, master 5fe22f01c)

222/383 Test #224: test_hotkey_binding_press_steps .................***Failed
[ RUN      ] remove_bindings_releases
[  ERROR   ] --- 1 != 2
[   LINE   ] --- test/cmocka/test_hotkey_binding_press_steps.c:317: error: Failure!
[  FAILED  ] remove_bindings_releases

Line 317 is assert_int_equal(num_calls, 2) right after obs_hotkey_unregister(id). The two presses did count (hotkey->pressed == 2 at line 312 passed); the release calls did not happen at unregister.

Root cause

libobs/obs-hotkey.c.h, unregister_hotkey():

HASH_DEL(obs->hotkeys.hotkeys, hotkey);
hotkey_signal("hotkey_unregister", hotkey);
release_registerer(hotkey);
if (hotkey->registerer_type == OBS_HOTKEY_REGISTERER_SOURCE)
	obs_weak_source_release(hotkey->registerer);
bfree(hotkey->name);
bfree(hotkey->description);
bfree(hotkey);          /* hotkey is freed here */
remove_bindings(id);    /* ... and remove_bindings() releases bindings */

remove_bindings() (:798) calls obs_hotkey_binding_release(binding) for every pressed binding it removes, and that wrapper reads binding->hotkey->pressed through obs_hotkey_binding_count_fields(). The hotkey it dereferences was bfreed four lines earlier: a use-after-free.

Linux passes because the freed block still holds pressed = 2, so the release runs the callback; macOS's allocator reuses/poisons the block, the count reads as 0, the release sees "already released" and no callback runs, hence one call short. The test is right and the code is wrong: a binding release after the hotkey is gone is exactly the case remove_bindings() exists to handle, and it currently reads freed memory on every platform (it is only silently benign where the allocator happens not to reuse the block).

Note this ordering matches upstream OBS, so it is an upstream latent defect that surfaced here, not a fork regression.

Why it matters beyond the test

obs_hotkey_unregister() is the path for source, output, encoder and frontend hotkeys going away. Every unregister of a hotkey that has a pressed binding does the same read-after-free; the observable effect is a missing "released" callback (a hotkey that stays down for the app), and under ASan it is a straight use-after-free report.

Fix

Move remove_bindings(id) before the frees in unregister_hotkey(), so bindings are released while the hotkey is still alive:

HASH_DEL(obs->hotkeys.hotkeys, hotkey);
hotkey_signal("hotkey_unregister", hotkey);
release_registerer(hotkey);
if (hotkey->registerer_type == OBS_HOTKEY_REGISTERER_SOURCE)
	obs_weak_source_release(hotkey->registerer);
remove_bindings(id);
bfree(hotkey->name);
bfree(hotkey->description);
bfree(hotkey);

The RED is already recorded above (macOS Tier 2, line 317); the GREEN is the same lane. A Linux asan build of test_hotkey_binding_press_steps should also report the use-after-free before the fix, which is worth recording in the fix PR.

Class: libobs lifetime bug (use-after-free), reachable from every unregister path. Not remote-reachable: it needs a local hotkey registration, so it is a normal public bug per SECURITY.md.

Activity

  1. added
    bug
    P1
    size:M
    on Oct 30, 2026
  2. zackees commented on Oct 30, 2026

    @zackees
    MemberAuthor

    Claiming: #3044 — release the bindings before the hotkey is freed.

    1. RED is already recorded on the issue: test_hotkey_binding_press_steps / remove_bindings_releases fails on macOS Tier 2 (1 != 2 at line 317) because remove_bindings() reads binding->hotkey->pressed from the block unregister_hotkey() freed four lines earlier.
    2. GREEN: move remove_bindings(id) above the frees in unregister_hotkey() (libobs/obs-hotkey.c.h), so every pressed binding of the hotkey is released while the hotkey is alive. No test edits: the new characterization test is the failing test.
    3. Validate on the lane that saw the RED (macOS Tier 2) and, if the asan lane builds this test, on Sanitizers too. One PR.
  3. added
    ready
    on Oct 30, 2026
  4. zackees commented on Oct 30, 2026

    @zackees
    MemberAuthor

    Fixed and merged.

    • PR: #3045, merged 2026-10-30T11:12Z as 908595e00 (libobs: release a hotkey's bindings before the hotkey is freed). Merge-group run 41045378895 is green (CI Gate ✅; macOS Tier 2 skipped there by scope).
    • Change: remove_bindings(id) moved ahead of the bfrees in unregister_hotkey(), so obs_hotkey_binding_release() reads binding->hotkey->pressed while the hotkey is alive instead of after it was freed.

    Evidence:

    • macOS Tier 2 on the branch (the lane that produced the RED): scheduled.yaml run 41039924489, Build 🧱 / Rust Tier 2 (macos-26) passed, test_hotkey_binding_press_steps remove_bindings_releases green.
    • ASan on PR CI: run 41038389267, Build 🧱 / Sanitize 🧪 / asan 🧪 success; test_hotkey_binding_press_steps exit=0 and the sanitizer triage lists no obs-hotkey finding — the use-after-free is gone. App launch app exit=0; the listed leaks/UBSan reports are the lane's existing set, unrelated to this path.

    What is left:

    • The docs/features.md line that this user-visible upstream-bug fix should have carried in the same PR was missed. #3057 adds it under Fixed upstream bugs → Stability (docs only).
    • Nothing else on the fork side. The same ordering exists upstream, so an upstream report/PR is a separate decision, not a fork task.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1bugreadysize:M

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions