From d4e4b5df982ba70a7cd367018d9573a3446bb136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Tue, 9 Nov 2021 23:08:31 +0100 Subject: [PATCH] pipewire: module-profiler: handle global's destroy event Handle if the global is destroyed (e.g. `pw-cli destroy X`) to avoid a use-after-free in `pw_global_destroy()` when it is called with a dangling pointer from `module_destroy()`. --- src/modules/module-profiler.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/modules/module-profiler.c b/src/modules/module-profiler.c index 91fa7b0db..aec9949b8 100644 --- a/src/modules/module-profiler.c +++ b/src/modules/module-profiler.c @@ -77,6 +77,7 @@ struct impl { struct spa_hook module_listener; struct pw_global *global; + struct spa_hook global_listener; int64_t count; uint32_t busy; @@ -338,7 +339,8 @@ static void module_destroy(void *data) { struct impl *impl = data; - pw_global_destroy(impl->global); + if (impl->global != NULL) + pw_global_destroy(impl->global); spa_hook_remove(&impl->module_listener); @@ -352,6 +354,22 @@ static const struct pw_impl_module_events module_events = { .destroy = module_destroy, }; +static void global_destroy(void *data) +{ + struct impl *impl = data; + + stop_listener(impl); + stop_flush(impl); + + spa_hook_remove(&impl->global_listener); + impl->global = NULL; +} + +static const struct pw_global_events global_events = { + PW_VERSION_GLOBAL_EVENTS, + .destroy = global_destroy, +}; + SPA_EXPORT int pipewire__module_init(struct pw_impl_module *module, const char *args) { @@ -398,5 +416,7 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args) pw_global_register(impl->global); + pw_global_add_listener(impl->global, &impl->global_listener, &global_events, impl); + return 0; }