From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796181986; b=hRYwO2fiXlxCMUbSNjkWgIVy8PbCPH6h3UUTIkjWTYtHK2EjXCzs5rQ7c7XsymRFCofY Ucz5Bjrc05HM48kzsowWCr9LtM5i7PLM9sqosgNjJwNa7NJ5G/RFJXQvft03eXvOtHLsF xJ56Q39PvnuXR64Zpuxz9aXcNU/X9i0eLXLn43hju893PhEhdtoV3q0G+cfmuVBRBUCKU 8CkApkByfE/HXFjfxLRmjwf7UMaGEeKpYRnMd4LZ0Xu6ktvQKhYpScuX2NF48oXexxD+p fjonOcG5PL3n0S1hqVuSeQVEFgRtcD2I72oxjkUeOrsQ4/GhXxMpaXNOo1kJ/Cj7Piw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796181986; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=epVPGlAZqwYu+IRdaiqPUL8q7Er2MaEOIPjuyRGVEmw=; b=gmZOscyC0NU8bjVpN21DiOyr5IC0LW06PqFowvM/rTdrOEvhqGiaY3sdQcUyTVWhu9s8 Pf4A5SGtY21k17galvyG8KbbEgqwQ5LjesVeJd4N3TyCIN9Gx3DKHXvWcizxUIHiOE6RP wbnVBx4aLLV+sPgGmiSWDfJ5cU+T2x4nmS5KnmG0wqkCGu3mHAIxFIEowwTZz/QGH1wXB IyAAGYIiglzbPjBW9a3fwQtIi69nltf5EuNGy/EVIhmyEEv7NPb9krlmtM5An9zn6TG13 U6bk6DcyxaxYBSJ4fD+FL8xFFMuZq5ShBaT7cvF/is5VS6psmmse4o3ZFUVbpWo8NLg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=e7ECOHd7; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D953606A33F for ; Wed, 2 Dec 2026 03:26:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=e7ECOHd7 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B4E71F000C1; Wed, 2 Dec 2026 03:26:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796181986; bh=epVPGlAZqwYu+IRdaiqPUL8q7Er2MaEOIPjuyRGVEmw=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=e7ECOHd7Fj5aoWLMzjHeti+8Usyz7j95ThuB7zI0Muttv3JaigRShhQhY/j/DQPUQ 9hQUb6T5rHW2SmgXz332f7Mo9rblJKNaiD8MtBV3GAOerq+KMK1pou6QtfmB8y6kKZ EOTK/HUruEKg3ZXI9XSSiB/dgEDHrRVrF7cqHomm0E+rvY/mpfcCYtmXRgR5siuCTM 3sdmBppH4gzLNuc9uCVDi+xyRJ3bHGFuqEgIZDa/SoCJeAuUie7mE6NE0GtnPqJkPo 8P9A2DGfwOoKArVL5YG/CZ+yLwAqJHwEaDlD/tTY3Y5IG7pVZRu+842jRMApOrfOqg Pm2VMieRuEZQA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] ALSA: emux: drain open hwdep users before freeing the emux structures Reply-To: sashiko-reviews@lists.linux.dev To: "Hanxiang Xu" Cc: linux-sound@vger.kernel.org In-Reply-To: <20261202101231.1-1-xuhx@hust.edu.cn> References: <20261202101231.1-1-xuhx@hust.edu.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 03:26:25 +0000 X-Mailing-List: linux-sound@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] ALSA: emux: use-after-free in snd_emux_free() due to waiting before disconnect - [High] ALSA: emux: use-after-free in snd_emux_hwdep_release() on premature wakeup - [Medium] ALSA: emux: blocking wait in snd_emux_free() stalls snd_card_disconnect() -- commit fccddfea9a552c936c62cedc5b7a1b89754d19a2 Author: Hanxiang Xu ALSA: emux: drain open hwdep users before freeing the emux structures This commit registers open and release callbacks on the emux hwdep device to track active users and waits in snd_emux_free() for hwdep_users to drop to zero before tearing down the emux structures. > diff --git a/sound/synth/emux/emux.c b/sound/synth/emux/emux.c > index 2a42d041e1da0..4352c57aa9586 100644 > --- a/sound/synth/emux/emux.c > +++ b/sound/synth/emux/emux.c [ ... ] > @@ -131,6 +133,7 @@ int snd_emux_free(struct snd_emux *emu) > return -EINVAL; > =20 > timer_shutdown_sync(&emu->tlist); > + wait_event(emu->free_wait, !atomic_read(&emu->hwdep_users)); > =20 > snd_emux_proc_free(emu); > snd_emux_delete_virmidi(emu); [Severity: Medium] Does waiting unconditionally in snd_emux_free() stall snd_card_disconnect() when a process holds the emux hwdep device open? During whole-card teardown, snd_card_disconnect() calls snd_device_disconnect_all(), which invokes snd_seq_device_dev_disconnect() = -> device_del() -> snd_emux_free() for the synth device before disconnecting lower-ordered devices on the card. Unlike snd_card_disconnect_sync(), snd_card_disconnect() and snd_card_free_when_closed() are meant to revoke f= ile operations and disconnect devices without waiting for open descriptors to close, so blocking here prevents remaining card devices (such as rawmidi, P= CM, and lowlevel devices) from disconnecting until userspace closes the hwdep f= d. > diff --git a/sound/synth/emux/emux_hwdep.c b/sound/synth/emux/emux_hwdep.c > index 14b990cf3b223..3240cbd58f05a 100644 > --- a/sound/synth/emux/emux_hwdep.c > +++ b/sound/synth/emux/emux_hwdep.c > @@ -73,6 +73,26 @@ snd_emux_hwdep_misc_mode(struct snd_emux *emu, void __= user *arg) > } > =20 > =20 > +static int snd_emux_hwdep_open(struct snd_hwdep *hw, struct file *file) > +{ > + struct snd_emux *emu =3D hw->private_data; > + > + if (!snd_emux_inc_count(emu)) > + return -EFAULT; > + atomic_inc(&emu->hwdep_users); > + return 0; > +} [Severity: High] Can a concurrent open succeed after wait_event() in snd_emux_free() returns, leading to a use-after-free of emu and emu->hwdep? snd_emux_free() waits for emu->hwdep_users to reach zero before calling snd_emux_delete_hwdep(emu), while emu->hwdep is still registered and snd_emux_hwdep_open() does not reject new opens once teardown begins. When snd_emux_free() runs via sysfs driver unbind (or with a built-in synth driver), try_module_get() inside snd_emux_inc_count() still succeeds: CPU0 CPU1 snd_emux_free(emu) wait_event(emu->free_wait, !atomic_read(&emu->hwdep_users)) snd_hwdep_open() hw->ops.open(hw, file) snd_emux_hwdep_open() snd_emux_inc_count(emu) atomic_inc(&emu->hwdep_users) snd_emux_proc_free(emu) ... snd_emux_delete_hwdep(emu) snd_sf_free(emu->sflist) kfree(emu) snd_emux_hwdep_ioctl() / release() dereferences emu <-- use-after-free > + > +static int snd_emux_hwdep_release(struct snd_hwdep *hw, struct file *fil= e) > +{ > + struct snd_emux *emu =3D hw->private_data; > + > + atomic_dec(&emu->hwdep_users); > + wake_up(&emu->free_wait); > + snd_emux_dec_count(emu); > + return 0; > +} [Severity: High] Does waking emu->free_wait here allow snd_emux_free() to free emu and hw before snd_emux_dec_count() and snd_hwdep_release() finish using them? CPU0 CPU1 snd_hwdep_release() mutex_lock(&hw->open_mutex) snd_emux_hwdep_release() atomic_dec(&emu->hwdep_users) wake_up(&emu->free_wait) snd_emux_free() /* wait_event() wakes */ ... snd_emux_delete_hwdep(emu) snd_device_free(card, hw) snd_emux_dec_count(emu) mutex_unlock(&hw->open_mutex) snd_hwdep_dev_disconnect() snd_hwdep_dev_free() kfree(hw) kfree(emu) wake_up(&hw->open_wait) <-- use-after-free snd_card_file_remove(hw->card, file) <-- use-after-free Because snd_emux_delete_hwdep() calls snd_device_free() on a live card rath= er than deferring snd_hwdep_dev_free() until card->card_dev release, freeing starts as soon as emu->free_wait wakes and hw->open_mutex is dropped. If hwdep was already disconnected (for example during snd_card_disconnect()), snd_device_free() does not take hw->open_mutex at all and kfree(emu) can al= so race directly with snd_emux_dec_count(emu). --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202101231.1-1-= xuhx@hust.edu.cn?part=3D1