control: remap - fix numid table corruption on element removal
PATCH 1/1 control: remap - fix numid table corruption on element removal
Removing a control element from a card that is opened through the remap plugin corrupts the heap of every process holding the remapped handle. UCM profiles that use MixerRemap or CtlRemap put this plugin between PipeWire's ACP and the card, so on such a card WirePlumber crashes some time after a driver takes controls away -- inside an unrelated free(), far from the cause. Found with a USB interface whose vendor controls come from a separate HID driver: unloading it removes nine elements from the live card, and WirePlumber later died with SIGSEGV or SIGABRT in free().
remap_forget_numid_child() drops the element's entry by moving the rest of the numid table down one slot, then increments numid_items instead of decrementing it. Every removal therefore grows the table past its
allocation: the loop goes on comparing beyond the end, and the next removal's memcpy() writes there. Valgrind reports the invalid read at the comparison and invalid writes in memcpy(), 0 and 8 bytes after the block allocated in remap_numid_new().
With the fix, the same sequence -- the driver unloaded ten times, nine elements removed each time, under a UCM profile that uses remap -- runs clean under valgrind.
Decrement the count, use memmove() since source and destination overlap, and stop at the first match: an entry for a real child numid is unique, as remap_find_numid_child() adds one only when none is found.
--- src/control/control_remap.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
src/control/control_remap.c +3 -2
@@ -219,9 +219,10 @@ static void remap_forget_numid_child(snd_ctl_remap_t *priv, unsigned int numid_cfor (index =0 ; index < priv->numid_items; index++) {if (numid[index].numid_child != numid_child)continue ; -memcpy (&priv->numid[index], &priv->numid[index +1 ], +memmove (&priv->numid[index], &priv->numid[index +1 ], (priv->numid_items -1 - index) *sizeof (*numid)); - priv->numid_items++; + priv->numid_items--; +return ; } }
Signature · 1 line
2.43.0