Repository navigation
libobs: Free a refused type's data only when its fields are whole - #2202
Merged
Merged
Conversation
When obs_register_source_s(), obs_register_output_s(), obs_register_encoder_s() or obs_register_service_s() refuses a type, HANDLE_ERROR() copies the first `size` bytes of the caller's structure into a zeroed one and calls free_type_data(type_data) when both are set. A size that ends inside free_type_data leaves a partly copied pointer, and HANDLE_ERROR() calls it. test_register_refused_type_data registers each of the four types without get_name through the public API, at sizes that end inside type_data, halfway through free_type_data, one byte short of its end, exactly at its end, and at sizeof. free_type_data must be called only when both fields lie wholly within the size, and then once with the caller's type_data, a byte pattern that a partial copy cannot match. Each registration runs in a forked child, so a jump through a partial pointer fails only its own case. Refs obs-rust#2195
When obs_register_source_s(), obs_register_output_s(), obs_register_encoder_s() or obs_register_service_s() refused a type, HANDLE_ERROR() copied the first `size` bytes of the caller's structure into a zeroed one and called free_type_data(type_data) when both were set. A size that ended inside free_type_data left a partly copied pointer (some low bytes of the caller's, then zeros), and HANDLE_ERROR() called it: a jump to a wrong address, or to the right one by luck when the missing bytes were zero anyway. HANDLE_ERROR() now uses type_data and free_type_data only when each lies wholly within the size (offsetof + sizeof <= size, the rule HAS_VAL() already applies to the callbacks), and reads them from the caller's structure, so the copy is gone. A size of 0 still frees nothing, and every registration that passes sizeof() of its structure, as the obs_register_*() macros do, behaves as before. docs/features.md gets a line under fixed upstream bugs. Fixes obs-rust#2195
This was referenced Oct 24, 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.
Fixes #2195
What and why
When
obs_register_source_s(),obs_register_output_s(),obs_register_encoder_s()orobs_register_service_s()refused a type,HANDLE_ERROR(libobs/obs-module.c.h) copied the firstsizebytes of the caller's structure into a zeroed one, then calledfree_type_data(type_data)when both were non-NULL. A size that ended insidefree_type_dataleft a partly copied pointer (some low bytes of the caller's, then zeros), andHANDLE_ERRORcalled it. With half the pointer copied that is a jump into unmapped memory. With all but the top byte copied it is, on 64-bit Linux, usually the right address by luck, because the top byte of a user-space pointer is 0.HANDLE_ERRORnow usestype_dataandfree_type_dataonly when each lies wholly within the size (offsetof + sizeof <= size, the ruleHAS_VAL()already applies to the callbacks), and reads them from the caller's structure, so the copy is gone. A size of 0 still frees nothing. Every registration that passessizeof()of its structure, as theobs_register_*()macros and every bundled plugin do, behaves exactly as before, and so does every size that ends between fields. Only a plugin that passes a size ending inside one of the two pointers is affected. Upstream has the same code.Two commits:
test_register_refused_type_data, a new real-libobs test, registered intest-graph.jsonin sorted order, underxvfb-runwhere a display is needed (astest_source_registration). It is new rather than an extension:test_source_registrationcovers sources only andtest_module_type_rules_steps(libobs: Port the output, encoder and service registration rules to obs-core #2194) outputs, encoders and services only, and both run every case in one process, where a jump through a partial pointer would take down the other cases. One test covers all four registration functions through the public API.get_name, so it is refused, withtype_dataset to a byte pattern the test never dereferences (0xA1B2C3D4E5F60718), so a partly copiedtype_datacannot match it.type_data, halfway throughfree_type_data, one byte short of the end offree_type_data, exactly at its end, andsizeof. That is 20 cases.free_type_dataran once with the caller'stype_datawhen both fields lie wholly within the size, and that it did not run otherwise.test_output_null_id). A 30 s alarm ends a child that jumps into code that loops. Windows has nofork(), so the cases run in the test process there.HANDLE_ERRORchange, and adocs/features.mdline under fixed upstream bugs (Stability).The Rust models of the registrations already assume whole fields:
rust/obs-core/tests/obs_core/module_source_rules.rsfrees only when both fields areinside()the size, and the model of the output, encoder and service registrations does the same. No Rust changes.Overlap: #2194 (merged while this was prepared) moved the output, encoder and service rules out of
obs-module.c.hand deletedCHECK_REQUIRED_VALandCHECK_REQUIRED_VAL_EITHERjust aboveHANDLE_ERROR;HANDLE_ERRORstayed. The macro's first two lines are kept as they were, so this change merged with the #2194 head without conflicts before it landed, and the branch rebased onto it cleanly.test_module_type_rules_steps(#2194) cuts neithertype_datanorfree_type_datain any of its sizes, and passes unchanged.A related bug, not fixed here (C,
REGISTER_OBS_DEFandobs_register_source_s(), unchanged; upstream has the same code): an accepted registration copies the caller's structure cut atsize, and the cut can fall inside any pointer after the last required callback. That pointer is stored partly copied. Forfree_type_datait is then called when the type is freed at shutdown (obs.c.h, theFREE_REGISTERED_TYPESloop and the source types), and an optional callback cut the same way is called whenever OBS uses it. Again only a plugin passing a size that is notsizeof()of its structure can reach it. A fix would zero a field the size cuts, after thememcpy. That changes what an accepted type looks like, so it is left for its own issue.RED → GREEN
RED commit:
fc6a07dc2test: Expect a refused registration to free only whole type data fields(evidence from27ea42d4d, the same commit before two rebases onto master).test_register_refused_type_datafails 8 of 20 on the C backendlibobs, against the hybridlibobsand againstlibobs-c-reference, the same 8 on each, without taking down the binary:free_type_data(sizes 228 source, 140 output, 132 encoder, 140 service): the child dies with signal 11;free_type_datais called once with0xa1b2c3d4e5f60718, through the partly copied pointer, which is whole by luck;type_datanothing is freed (free_type_datais not copied at all), and at the end offree_type_dataand atsizeofit is freed once with the caller'stype_data.test_source_registrationpasses 10/10 on all three.GREEN:
066992505.test_register_refused_type_data20/20,test_source_registration10/10 andtest_module_type_rules_steps16/16 on the C backendlibobs, the hybridlibobsandlibobs-c-reference(ninja libobs-c-referencerebuilt with each commit before the c-ref runs), on the head and on67ffe5b6c(the GREEN commit on master with libobs: Port the output, encoder and service registration rules to obs-core #2194).obs_register_*(the module and registration tests), on67ffe5b6c: 161/161 on the C backend, 162/162 against the hybridlibobsand 162/162 againstlibobs-c-reference, 0 skipped. On an earlier GREEN (3ec83b8c0, before libobs: Port the output, encoder and service registration rules to obs-core #2194 landed), at a host load of 30 to 48, single runs oftest_media_sync_offset_restart(Flaky: real-time audio cmocka tests lose audio under machine load (test_vlc_playlist_gap, test_group_hide_transition_audio) #838),test_pulse_monitor_deviceandtest_pipewire_screencast_shm(sound server and screencast timing; none registers a type) failed once each againstlibobs-c-referenceor the C backend, and each passed alone (test_media_sync_offset_restartonce, 6/6 cases; the other two twice on both lanes).67ffe5b6c(private Xvfb,dbus-run-session, fresh HOME, TMPDIR unset,ulimit -c 0): 465/465 on the C backend and 482/482 against the hybridlibobs, 0 skipped. The known flakes (test_deinterlace_2x_field_order is timing-sensitive (yadif 2x 60i into 60): startup underrun and load-induced hold #456, Flaky: real-time audio cmocka tests lose audio under machine load (test_vlc_playlist_gap, test_group_hide_transition_audio) #838) andtest_stinger_media_durationdid not fail. On3ec83b8c0it was 476/476 against the hybridlibobsand 460/461 on the C backend, the one failure being thetest_pulse_monitor_devicerun above.libobs-c-reference, identical sets, on the head.ci/local_gate.py --test-diff origin/master: 3634 Rust tests pass (5 skipped) on67ffe5b6c.--lane lint(clang-format 22.1.3 over the two C files) passes on67ffe5b6c,--lane crosson3ec83b8c0. No Rust changed, so no Windows clippy run. The last rebase (to the head) brought no Rust changes.67ffe5b6calso cmocka Windows diagnostics and mbedtls graph.Real app (UI, protocol output or logging changes): not applicable, no UI, protocol or log change.
Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): built with scripting ON (PR CI builds no OBS app: rungh workflow run ci.yml --ref <branch>for the full builds) and cross-checked with clang and MSVC