From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=132.226.202.154 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796181319; b=KSz897AmIsQ4ve0M1PoBu1Z5kXJpN7crRrRkUzxKIpPgU03111T3U7N0Ub2rGbHgIu9b T5qPe9ZLo3EpVN3koKT3znh+sU3Wljd9cxlHh2QasP0cwDKoZcROaAUsssh8QdGJM0TIC b3brZcVBxlanjeO9VjVeBINk0og68JO1Iljx6ibOV4xsd4Y/p8t1Pwvhpevrmyb00Hihb hAN7eOlCfjVdO3QSXrjN5tsJuYfzrvR95C8vlyAOtVwWqwJCzg5+qCmOzCI+U9LNLKMfk 8UELoZQMyPGVnopoOSC89CcFGLuL8xJb3o7TEzIcbLbu6YBtjeXqltjeHfrXsFUNJcw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796181319; h=DMARC-Filter:Received:Received:From:To:Cc:Subject:Date:Message-ID: MIME-Version:Content-Type:Content-Transfer-Encoding:X-CM-TRANSID: X-Coremail-Antispam:X-CM-SenderInfo; bh=eQekr1fBditgQ/G18Hz7/RWxJCEIZvljNvm9VSnbmyg=; b=GV45PPmJJqLKnZfpjY4tK0EzDk9a7F5CyjMXZllWhQdYYuPi5DvspG7I4wpPjgr8kuZ4 Y3uomuuUHPgYQBobEWIm5k74t45eUx8U+Ffcx7EF83X2s6+A6XNbw4ssyApBeq84fc+v6 fZ/MW8/u01D+nOrkpUqhv1oUUOa6SJuTI7S1OQbCQq+LVcPlgbEF/IzKPzkK21qOXoiza ErzM1oJfLc2ljEbQl5HqGOZFibCqvnMjzL7M6kr8o5ug+lEXy5cJzqKJPh+uk/HQWBEi6 vpwwE5xQzI+2z+9H12YjLSq1Za1krutR0wxo8VaQUN0cZYl8PFXMHs6p8duSqQASsZg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=hust.edu.cn; spf=pass smtp.mailfrom=hust.edu.cn; arc=none smtp.remote-ip=132.226.202.154 Received: from fraori-sdnproxy-3.icoremail.net (fraori-sdnproxy-3.icoremail.net [132.226.202.154]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EC873606A337; Wed, 2 Dec 2026 03:15:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EC873606A337 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=hust.edu.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=hust.edu.cn Received: from hust.edu.cn (unknown [172.16.0.52]) by app1 (Coremail) with UTF8SMTP id IAEQrMCm6stCjQ9rj2w4AA--.7686S2; Wed, 02 Dec 2026 11:15:14 +0800 (CST) Received: from [222.20.126.109] (unknown [222.20.126.109]) by gateway (Coremail) with SMTP id _____wBX_BU3jQ9rGpg4AA--.26695S3; Wed, 02 Dec 2026 11:15:12 +0800 (CST) From: Hanxiang Xu To: Takashi Iwai , Jaroslav Kysela Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] ALSA: emux: drain open hwdep users before freeing the emux structures Date: Tue, 2 Dec 2026 10:12:31 +0800 Message-ID: <20261202101231.1-1-xuhx@hust.edu.cn> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CM-TRANSID:IAEQrMCm6stCjQ9rj2w4AA--.7686S2 Authentication-Results: app1; spf=neutral smtp.mail=xuhx@hust.edu.cn; X-Coremail-Antispam: 1UD129KBjvJXoWxJFWUJw1xXw1ruw1rJFWUurg_yoWrJw43pF yDGrW3Zr4xXr12vw4YyF4DXFyvyF40kFW3KwnxAa4qvF9xArWrW3WUtr1qyF47KF4kWFWr Wry0gwn5GFZ3Cr7anT9S1TB71UUUUUJqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBlb7Iv0xC_Kw4lb4IE77IF4wAFc2x0x2IEx4CE42xK8VAvwI8I cIk0rVWrJVCq3wA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK021l84ACjcxK6xIIjx v20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4UJVWxJr1l84ACjcxK 6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_GcCE3s1ln4kS14v26r 1Y6r17M2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx1l5I8C rVACY4xI64kE6c02F40Ex7xfMcIj64x0Y40En7xvr7AKxVW8Jr0_Cr1UMcIj6x8ErcxFaV Av8VW8uFyUJr1UMcIj6xkF7I0En7xvr7AKxVW8Jr0_Cr1UMcvjeVCFs4IE7xkEbVWUJVW8 JwACjcxG0xvEwIxGrwCY02Avz4vE-syl42xK82IYc2Ij64vIr41l42xK82IY6x8ErcxFaV Av8VW8uFyUJr1UMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4l x2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrw CI42IY6xIIjxv20xvE14v26F1j6w1UMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWxJVW8Jr1l IxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26F4j6r4UJwCI42IY6I 8E87Iv6xkF7I0E14v26r4UJVWxJrUvcSsGvfC2KfnxnUUI43ZEXa7IUY7WrtUUUUU== X-CM-SenderInfo: bgsqjkayqyjmo6kx23oohg3hdfq/ Opening a hwdep device of the emux wavetable pins only the base-card module; the emux hwdep registers no open/release hooks, so an open fd keeps file->private_data pointing at the struct snd_hwdep and in-flight ioctls keep running against the emux structures while the sub-driver removal (rmmod snd_emu10k1_synth, or unbind which bypasses module refcounts) kfrees the hwdep, the soundfont list and the struct snd_emux itself -- snd_hwdep_ioctl() holds no open_mutex and snd_hwdep_dev_disconnect() neither checks hwdep->used nor revokes f_op (dynamically confirmed on v7.4-rc5, 113 UAF reports). Register hwdep open/release hooks that account for the fd with snd_emux_inc_count()/snd_emux_dec_count() -- which also pins the sub-driver module like the sequencer and OSS-sequencer paths -- and make snd_emux_free() wait until all hwdep users are gone before tearing anything down, mirroring the drain snd_card_disconnect() performs for whole-card removal. Cc: Signed-off-by: Hanxiang Xu --- include/sound/emux_synth.h | 2 ++ sound/synth/emux/emux.c | 3 +++ sound/synth/emux/emux_hwdep.c | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/include/sound/emux_synth.h b/include/sound/emux_synth.h index 2c0a976..46fe2bb 100644 --- a/include/sound/emux_synth.h +++ b/include/sound/emux_synth.h @@ -99,6 +99,8 @@ struct snd_emux { int use_time; /* allocation counter */ spinlock_t voice_lock; /* Lock for voice access */ struct mutex register_mutex; + atomic_t hwdep_users; + wait_queue_head_t free_wait; int client; /* For the sequencer client */ int ports[SNDRV_EMUX_MAX_PORTS]; /* The ports for this device */ struct snd_emux_port *portptrs[SNDRV_EMUX_MAX_PORTS]; diff --git a/sound/synth/emux/emux.c b/sound/synth/emux/emux.c index 2a42d04..4352c57 100644 --- a/sound/synth/emux/emux.c +++ b/sound/synth/emux/emux.c @@ -32,6 +32,8 @@ int snd_emux_new(struct snd_emux **remu) spin_lock_init(&emu->voice_lock); mutex_init(&emu->register_mutex); + atomic_set(&emu->hwdep_users, 0); + init_waitqueue_head(&emu->free_wait); emu->client = -1; #if IS_ENABLED(CONFIG_SND_SEQUENCER_OSS) @@ -131,6 +133,7 @@ int snd_emux_free(struct snd_emux *emu) return -EINVAL; timer_shutdown_sync(&emu->tlist); + wait_event(emu->free_wait, !atomic_read(&emu->hwdep_users)); snd_emux_proc_free(emu); snd_emux_delete_virmidi(emu); diff --git a/sound/synth/emux/emux_hwdep.c b/sound/synth/emux/emux_hwdep.c index 14b990c..3240cbd 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) } +static int snd_emux_hwdep_open(struct snd_hwdep *hw, struct file *file) +{ + struct snd_emux *emu = hw->private_data; + + if (!snd_emux_inc_count(emu)) + return -EFAULT; + atomic_inc(&emu->hwdep_users); + return 0; +} + +static int snd_emux_hwdep_release(struct snd_hwdep *hw, struct file *file) +{ + struct snd_emux *emu = hw->private_data; + + atomic_dec(&emu->hwdep_users); + wake_up(&emu->free_wait); + snd_emux_dec_count(emu); + return 0; +} + /* * ioctl */ @@ -127,6 +147,8 @@ snd_emux_init_hwdep(struct snd_emux *emu) /* The ioctl parameter types are compatible between 32- and * 64-bit architectures, so use the same function. */ hw->ops.ioctl_compat = snd_emux_hwdep_ioctl; + hw->ops.open = snd_emux_hwdep_open; + hw->ops.release = snd_emux_hwdep_release; hw->exclusive = 1; hw->private_data = emu; err = snd_card_register(emu->card); -- 2.43.0