Repository navigation
linux-pipewire: Fix use-after-free in log message - #393
Merged
Merged
Conversation
Collaborator
Author
|
Merge-order note: #391 (NV12 camera) makes |
Member
|
Verified locally (Tier 2 image + pipewire, wireplumber, dbus, under xvfb-run), 2 runs per commit: |
zackees
force-pushed
the
fix/pipewire-camera-unplug-uaf
branch
from
October 12, 2026 16:59
1a22bfc to
a8ee5bf
Compare
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]>
Collaborator
Author
|
#391 merged first, so this is rebased on it with |
rdoupe
force-pushed
the
fix/pipewire-camera-unplug-uaf
branch
from
October 12, 2026 17:05
a8ee5bf to
60b2b83
Compare
This was referenced Oct 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.clines 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.bfreeas its key destructor (line 1141), sog_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.
test/cmocka: Add a failing test for unplugging a PipeWire camera: addstest_pipewire_camera_unplug(Linux; private PipeWire daemon throughtest_pipewire_cursor_hdr.sh, private D-Bus bus, X display forobs_startup(); skips without them). The real camera portal code runs against a fakeorg.freedesktop.portal.Cameraand 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--wrapto 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 asRemoved 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).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_LIBOBSOFF 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 withheap-use-after-freeatcamera-portal.c:1033and GREEN passes cleanly.ctest -Vshows both cases run, with no skips. 20 of 20 repeated runs pass at GREEN.pipewire(77). CI's image has nopipewirebinary, so there it builds and skips.ci/test_registration.pyare clean.The test holds the camera portal's registry events until its first roundtrip, because
on_registry_global_cb()can otherwise run beforeconnection->obs_pwis assigned (obsproject/obs-studio#13971, a separate startup race not fixed here) and crash the test.Not tested:
Not fixed here (separate causes):
pipewire_camera_destroy()(libobs's destruction thread) andaccess_camera()changeconnection->sourceswithout the PipeWire loop lock while the registry callbacks iterate it on the PipeWire thread, and the device table is read from the UI thread while the PipeWire thread changes it. On module unload the device proxies are destroyed without the loop lock; PipeWire warnsimpl_ext_end_proxy called from wrong contextat the end of the test, before and after the fix.Types of changes
Checklist:
🤖 Generated with Claude Code