Skip to content

linux-pipewire: Fix use-after-free in log message - #393

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/pipewire-camera-unplug-uaf
Oct 12, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/pipewire-camera-unplug-uaf

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

Description

Unplugging a camera while a "Video Capture Device (PipeWire)" source exists no longer reads freed memory.

  • on_registry_global_remove_cb() (plugins/linux-pipewire/camera-portal.c lines 1032-1033) removed the unplugged device from the camera portal's device table and then logged its device id, which is the table's key.
  • The table is created with bfree as its key destructor (line 1141), so g_hash_table_iter_remove() had already freed the id: every unplug logged a freed string on the PipeWire thread. Under AddressSanitizer OBS aborts there.

The fix logs the device before removing it. It is pobrn's patch from obsproject/obs-studio#13698, unchanged, with pobrn as the commit author.

  1. test/cmocka: Add a failing test for unplugging a PipeWire camera: adds test_pipewire_camera_unplug (Linux; private PipeWire daemon through test_pipewire_cursor_hdr.sh, private D-Bus bus, X display for obs_startup(); skips without them). The real camera portal code runs against a fake org.freedesktop.portal.Camera and a video source node in the test plays the camera: it is plugged in before the source is created, listed by the camera portal, then unplugged by destroying it. The test links with --wrap to see the camera portal's device ids: an id is overwritten with 'X' when it is freed and, without AddressSanitizer, kept allocated until the end, so the failure is deterministic in a normal build. 1 of 2 cases fails: the removal is logged as Removed device XXXXXXXXXXXXXXX. The guard, that the plugged-in camera is listed, passes. Under AddressSanitizer the process aborts with the trace from the upstream PR (freed at line 1032, read at line 1033).
  2. linux-pipewire: Fix use-after-free in log message: 2 of 2 pass, Removed device obs-test-camera, and AddressSanitizer reports nothing.

Motivation and Context

Fixes #392 (upstream obsproject/obs-studio#13698, pobrn's PR; there is no upstream issue).

Why this one: it happens on every camera unplug, for anyone with a PipeWire camera source, and it is a memory-safety bug on the PipeWire thread. pobrn found it with AddressSanitizer and a removable USB camera and posted the fix on 2026-07-25; tytan652 approved it the same day, but it is still not merged, and the code is unchanged upstream and in this fork. The fix is a two-line swap. There is no user report beyond pobrn's, so the visible demand is low; the cost is low too.

How Has This Been Tested?

Automated, in both libobs lanes (ENABLE_RUST_LIBOBS OFF and ON), on current master:

  • test_pipewire_camera_unplug: 1 of 2 cases fails at the RED commit and 2 of 2 pass at GREEN, in both lanes, without AddressSanitizer. In a separate AddressSanitizer build, RED aborts with heap-use-after-free at camera-portal.c:1033 and GREEN passes cleanly. ctest -V shows both cases run, with no skips. 20 of 20 repeated runs pass at GREEN.
  • It skips without a display (both cases skip), without the wrapper (77) and without pipewire (77). CI's image has no pipewire binary, so there it builds and skips.
  • Full ctest: 128 of 129 at RED (only the new test fails) and 129 of 129 at GREEN, in each lane.
  • clang-format, gersemi and ci/test_registration.py are clean.

The test holds the camera portal's registry events until its first roundtrip, because on_registry_global_cb() can otherwise run before connection->obs_pw is assigned (obsproject/obs-studio#13971, a separate startup race not fixed here) and crash the test.

Not tested:

  • A physical camera. The test uses a real PipeWire daemon and the camera portal's own code; only the portal (which hands over the PipeWire connection) is faked.

Not fixed here (separate causes):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the test, the commit messages and this text under the submitter's direction, ported pobrn's fix, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

@rdoupe
rdoupe requested a review from zackees as a code owner October 12, 2026 16:51
@rdoupe

rdoupe commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator Author

Merge-order note: #391 (NV12 camera) makes pipewire.c depend on a new plugins/linux-pipewire/frame-layout.c. If #391 merges first, this PR's test_pipewire_camera_unplug.cmake needs that file added to its sources or the test won't link. I'll rebase and add it as soon as #391 lands; if this one lands first, I'll add it to #391 instead.

@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Verified locally (Tier 2 image + pipewire, wireplumber, dbus, under xvfb-run), 2 runs per commit:

  • RED 4131224: unplugged_camera_is_logged_by_name fails (1 of 2)
  • GREEN 1a22bfc: 2/2 pass
  • The test needs PipeWire, D-Bus and a display, so plain CI skips it. Rebased on master, local gate passed; pushed as a8ee5bf.

@zackees
zackees force-pushed the fix/pipewire-camera-unplug-uaf branch from 1a22bfc to a8ee5bf Compare October 12, 2026 16:59
rdoupe and others added 2 commits October 12, 2026 13:03
Unplugging a camera that the "Video Capture Device (PipeWire)" source
lists reads freed memory (obsproject/obs-studio#13698, found by pobrn
with AddressSanitizer and a removable USB camera).

on_registry_global_remove_cb() removes the device from the camera
portal's device table, whose key destructor is bfree(), and then logs
the device id, which is that key: the log line reads the freed string.
It happens on every unplug.

test_pipewire_camera_unplug runs the real camera portal against a fake
org.freedesktop.portal.Camera on a private D-Bus bus (GTestDBus) and a
private PipeWire daemon (test_pipewire_cursor_hdr.sh: own runtime dir,
no session manager, no devices). A video source node in the test plays
the camera; it is plugged in before the source is created and unplugged
by destroying it. The test links with --wrap to see the device ids: a
key is recorded when it is inserted and overwritten with 'X' when it is
freed, and without ASan the freed key stays allocated until the end, so
the failure is deterministic in a normal build: the removal is logged
as "Removed device XXXXXXXXXXXXXXX". With ASan the key is freed at once
and ASan reports the read, with the trace in the upstream report.

The registry events are held until the camera portal's first roundtrip,
so that the separate startup race in obsproject/obs-studio#13971
(connection->obs_pw read before it is assigned) cannot crash the test.

Needs an X display for obs_startup(), dbus-daemon and pipewire; skips
without them. plugged_camera_is_found passes (guard);
unplugged_camera_is_logged_by_name fails.

Refs obs-rust#392

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
When a device disappears, it is removed from the hash table and
a message is logged. However, this log message uses the device's
id, which is freed when it is removed from the hash table.

So swap the order to fix that.

Ported unchanged from pobrn's obsproject/obs-studio#13698 (open
upstream, approved by tytan652), found with AddressSanitizer and a
removable USB camera.

test_pipewire_camera_unplug: 2 of 2 cases pass (the unplug case failed
before, with the freed id logged), and AddressSanitizer reports nothing.

Fixes obs-rust#392

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@rdoupe

rdoupe commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator Author

#391 merged first, so this is rebased on it with plugins/linux-pipewire/frame-layout.c added to test_pipewire_camera_unplug.cmake (folded into the test commit; pobrn's fix commit is unchanged). Full ctest 131/131 in both lanes, the unplug test 2/2.

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.

bug: linux-pipewire: unplugging a PipeWire camera logs a freed string (use-after-free) (upstream PR #13698)

3 participants