Skip to content

media-playback: Own FFmpeg options - #733

Merged
zackees merged 2 commits into
obs-rust:masterfrom
chriscoveries:fix/media-source-options-lifetime
Oct 14, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
chriscoveries:fix/media-source-options-lifetime

Conversation

@chriscoveries

Copy link
Copy Markdown
Contributor

Draft. Opened so upstream PR CI can run it. Our own CI is down, so this hasn't been through our usual CI runs: see our note on #699.

Status: draft. Opened so upstream PR CI can run it. The branch fix/media-source-options-lifetime is two commits above 5f55dc0df49bad85def95aae9ab82b6e656d9a93. Upstream master at the time of writing is 8af78cc7750f8308b7d1948257bced056ca0532a; the branch merges into it cleanly with no conflicts.

  • RED 5d4025fc67c51fab10c6ba112eb6ca8ff425b2ec — test: Check that media playback owns its FFmpeg options (RED) (tests only)
  • GREEN e412fc8947f6cf0b9c30201fde5035d39928836c — media-playback: Own FFmpeg options (GREEN)

Problem

Updating an FFmpeg source can free the settings string while the old media worker is still parsing it. The worker reads freed memory in FFmpeg's option parser, which is a heap use-after-free under ASan and can crash or read garbage options without it. No issue has been opened yet, so this draft has no issue link.

Root cause

At the base commit, plugins/obs-ffmpeg/obs-ffmpeg-source.c:317 passes s->ffmpeg_options to media playback. ffmpeg_source_update() frees that string at :463-464, before its conditional media destruction at :491-493. Meanwhile shared/media-playback/media-playback/media.c:1008 stores the pointer without copying it; :991 starts the worker, which parses the pointer later at :739-742. The media destroy path joins the worker at :1050, so the source can free the string before that join completes.

Fix

mp_media_init() now copies the options string (bstrdup) and mp_media_free() releases that copy, after mp_kill_thread() has joined the worker. The source's own string can therefore be freed at any time.

The full-decode (cache) path goes through the same mp_media_init(), so its embedded mp_media_t also owns a copy. mp_cache_decode() releases it through mp_media_free() (both on success and at its fail: label). For the cases where decode never runs, mp_cache_free() now calls mp_media_free(&c->m) unconditionally instead of only when c->m.fmt is set. mp_media_free() is safe on an already-freed or zeroed struct (mp_media_init() already relies on that when it fails part-way), so there is no double free on any path.

That cache change also closes a pre-existing leak on mp_cache_init()'s failure path: when mp_media_init2() fails before the file is opened, fmt is NULL, so the old code skipped mp_media_free() and leaked the half-initialised media (the semaphore, a copy of the path and now the options copy). It is the same one-line change that frees the new options copy there, so it is included rather than split out.

mp_cache_t::ffmpeg_options is deliberately left alone. It is assigned in mp_cache_init() and never read anywhere (its only references are that assignment and the field declaration), so copying and freeing it would be dead code with nothing to test. The cache also never had the race: it parses synchronously inside mp_cache_init() on the caller's thread, while the caller's string is still valid.

Options considered:

  • A — copy in mp_media only (implemented): fixes the asynchronous worker path with the smallest change.
  • B — also copy into mp_cache_t::ffmpeg_options: rejected. The field is never read, so the copy cannot be exercised and would only add a double-free surface.
  • C — defer source-side frees until media shutdown: ties source settings lifetime to playback teardown. ffmpeg_source_update() also replaces the settings string when it keeps the existing media object, so this requires retaining old strings or restarting playback for unrelated updates.

Option A is recommended.

Evidence

  • RED → GREEN: test_media_source_options_lifetime, a Linux-only cmocka test (it uses GNU ld --wrap, so it is skipped on macOS and Windows). It needs no sanitizer. The test executable compiles media-playback itself and wraps three calls inside it: FFmpeg's av_dict_parse_string (records the pointer and text the parser receives), bfree (counts frees of the pointers under watch) and pthread_join (releases a held parser only when the worker is joined). No libobs test hook or export is added. The four cases:

    1. media_worker_keeps_options_until_parse_finishes: holds the worker inside the parser, frees the caller's string the way ffmpeg_source_update() does, then destroys the media object. The parser is only released when pthread_join runs, so the test asserts the copy was not freed while the worker was still in the parser (this is the free-before-join ordering), that the parser was given a different pointer from the caller's buffer with the same text, that the real parse succeeds, and that the copy is freed exactly once.
    2. cache_init_failure_releases_options_copy: full_decode on /dev/null, so mp_cache_init() fails at open (fmt NULL). Asserts the owned copy, freed exactly once, and that the caller's string is never freed by the library.
    3. cache_decode_releases_options_copy: full_decode on a small generated WAV file, so the cache initialises and decodes. Same assertions.
    4. cache_open_then_init_failure_releases_options_copy: full_decode on a WAV with an unknown format tag, so the file opens (fmt is set) and then init fails. Same assertions; this is the case that would catch a double free of the copy in mp_cache_free().

    RED (tests only, on the base source), Linux GCC 14, RelWithDebInfo, -Werror, no sanitizer, ENABLE_RUST_LIBOBS=OFF: [ PASSED ] 0 test(s), all four cases fail (3 of 3 runs). Case 1 fails with "media playback parsed the caller's options buffer" and the others with the parser pointer equal to the caller's pointer.

    GREEN, same build: [ PASSED ] 4 test(s), 20 of 20 repeated runs and through ctest.

    Mutants run against the final test in the plain build (each killed, 3 of 3 runs):

    Mutant Killed by
    borrow the caller's pointer in mp_media_init() all four cases
    drop bfree(media->ffmpeg_options) in mp_media_free() all four cases
    revert mp_cache_free() to freeing only when fmt is set case 2
    free the copy before mp_kill_thread() (before the join) case 1
    free the wrong cache field (c->ffmpeg_options) in mp_cache_free() case 3
    extra bfree(c->m.ffmpeg_options) before mp_media_free(&c->m) double free abort
    bfree(c->m.ffmpeg_options) before if (c->m.fmt) mp_media_free(...) case 4 (double free abort)
    borrow only when full_decode is set cases 2, 3 and 4

    An earlier ASan run of the original version of this test showed the real use-after-free (heap-use-after-free READ of size 13 ... av_get_token ... av_dict_parse_string).

  • Sanitizers: under GCC ASan+UBSan with ASAN_OPTIONS=detect_leaks=1 all four cases pass and the only report is one 104-byte AVPacket leak allocated in mp_media_reset() and reached from mp_cache_decode() (case 3). That leak is pre-existing and unrelated to the options string: mp_media_next_packet() does not return the packet on a read error or EOF. It is fixed by fix/media-packet-eof-leak. With that branch's one-line fix applied locally on top of this branch, the test exits 0 under ASan+LSan (5 of 5 runs). This PR should land after the EOF-leak PR, otherwise the Sanitizers workflow reports this test as failing for that unrelated leak; the dependency is also noted in the test's tests.d fragment. No LSan suppressions are added.

  • Fork CI: not run. Our self-hosted CI is down (see our note on #699), so this relies on upstream PR CI. The test is Linux-only, so only the Linux jobs run it.

  • Real runs: not run. The headless test synchronizes the media worker at the parser and checks the pointer lifetime directly; a full OBS/websocket run is not done yet.

Compatibility & risk

No public function signature or option parsing behavior changes. Every media object makes one small extra allocation (the options copy), including the cache's embedded mp_media_t, and it is released on every path (normal free, init failure, and the cache failure cases above). mp_cache_free() now calls mp_media_free() on a half-initialised cache, which also frees state that used to leak. Tested configuration: Linux GCC 14 with ENABLE_RUST_LIBOBS=OFF and =ON (the test target, RED and GREEN), plus the ASan+UBSan+LSan build above. macOS and Windows do not run this test and were not tested.

Status

Status: draft. Opened so upstream PR CI can run it. It should land after the EOF-leak PR (for a sanitizer-clean run).


How this was tested

  • RED → GREEN: test_media_source_options_lifetime fails 4 of 4 cases on the base source and passes 4 of 4 on GREEN, in a plain Linux GCC 14 RelWithDebInfo -Werror build with no sanitizer (ENABLE_UNIT_TESTS=ON), in both ENABLE_RUST_LIBOBS=OFF and =ON. The mutants listed under Evidence were each killed.
  • ASan+UBSan+LSan: GREEN passes 4 of 4 cases; the process exits 1 only because of the pre-existing 104-byte AVPacket leak described above. With the EOF-leak fix applied locally it exits 0.
  • Format: clang-format 22.1.3 --dry-run --Werror on the changed C files and gersemi 0.25.0 --check with the repo .gersemirc on the new tests.d fragment are clean.
  • Our own CI: not run. Our self-hosted CI is down (see our note on #699), so this relies on upstream PR CI.
  • Real runs: not run. The headless cmocka test exercises the exact ownership interval; a full OBS/websocket run remains pending.

Thank you to the obs-rust maintainers and contributors for building this project in the open. In the American spirit of free expression and individual independence, and with the principle of judging contributions on their merit alone, this PR is a small piece of what independent people can build when they come together.

@zackees
zackees marked this pull request as ready for review October 14, 2026 23:09
@zackees
zackees self-requested a review as a code owner October 14, 2026 23:09
@zackees
zackees merged commit 28f6b75 into obs-rust:master Oct 14, 2026
22 checks passed
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.

2 participants