Skip to content

bug: libobs: obs_scene_enum_items walks a released item when its callback removes a later item #490

Closed

Description

@chriscoveries

Summary

obs_scene_enum_items remembers the next item before it calls the callback, and only holds a reference on the current one. If the callback removes a later item with obs_sceneitem_remove, the walk either visits an item that was just removed or steps onto one that has been freed. Severity: high for code that removes items during an enumeration, because the result is a use-after-free; code that only removes the current item is not affected.

Steps / trigger

Public API only, on a scene with four items:

  1. Call obs_scene_enum_items(scene, cb, param).
  2. In cb, on the first item, call obs_sceneitem_remove on the second (or the second and third).
  3. If someone else still holds the second item, the callback is called with the removed item. If the scene owned the only reference, the walk reaches a freed item (a crash in testing).

Removing the current item in the callback is fine. obs_sceneitem_group_enum_items calls this function, so it has the same problem.

Cause

Line numbers are against current master.

  • libobs/obs-scene.c:2111: next = item->next is read before the callback.
  • libobs/obs-scene.c:2113: only the current item gets obs_sceneitem_addref.
  • libobs/obs-scene.c:2115-2122: after the callback the loop does item = next, whatever the callback did.
  • libobs/obs-scene.c:2405, :2410, :2433: obs_sceneitem_remove marks the item removed, detaches it from the list and drops the scene's reference, which frees an item nobody else holds.

The scene code is C only, so ON and OFF behave the same.

Proposed fix

While holding the scene lock, take a reference on every item into a snapshot array, walk the snapshot, and release the references at the end. Skip any snapshot item that is marked removed or is no longer in this scene's list. Membership is checked against scene->first_item and its links, which the held scene lock protects, rather than by reading fields of an item that may have moved into a group under a different lock. One behaviour change: items added by the callback during the walk are no longer visited by that walk. The regression test uses the public scene API and covers removal of the next item, of two adjacent items, items moved into or out of a group, early exit on a false return, signal re-entry and reference release. It also runs clean under ThreadSanitizer.

I'd like to work on this; a fix with a regression test is ready.

Activity

  1. zackees commented on Oct 13, 2026

    @zackees
    Member

    @chriscoveries go ahead: please open the PR with the RED test commit first, then the fix (one PR per issue, targeting master). Marking it claimed for you so nobody duplicates it.

  2. added
    claimed
    on Oct 13, 2026
  3. chriscoveries commented on Oct 14, 2026

    @chriscoveries
    ContributorAuthor

    Releasing this one. We're backed up on CI and won't get it landed soon, so anyone who wants it is welcome to take it.

    If it helps, our RED test and fix are on the fork branch fix/scene-enum-items-removal. They passed review on our side but haven't been through CI, so reuse them or start fresh, whichever you prefer.

  4. zackees commented on Oct 14, 2026

    @zackees
    Member

    Claiming.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    claimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions