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=1796381429; b=Bhrh5CKdV5ihmYKJzER+l0U03KHzZsxQGk3oIMMb1/Wk81QLDWOmsgtikFTgi8JNElNn HBg8DtCaUSlEa2Tqqu0zJrk5pxnHCnB9Q8G3AjK+4j1gk3/pGwN8DaCYQf9otW2x7BiPC 2wn4pJfD+HVObO7NJealDxyakPzIWK0sizibW4zRXtlRmBFT/cdX6xHGU6OChjU4un7nz dFC1ZIRNNp41PskBWimefxx25ZIZ2v0fzXFjQ8qn22G1cJjn0MePkIlaLREsZDluTdLZB 2D00iWeb5wHPCBQdEam0cArZ3oMHGANmv7QqijnQQRZnDKQQVhM8k8B/N0zy2opwH+w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381429; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=Jn+7FKOizKjJYEVbeEuo0bhH/OOSaph61jeq9D2xcAA=; b=GvdZYj7Kw3LoivhyxFks+sIErw/O+WLnGtGh0OxZi2eW0HlFzYMWFEqDd25CW8vW5cGg isd/4yBC5llswHDt53FAjrOBq1eS9zt7D/weXRk9uTu/pFhzDb3dt3V3Fbqx79OmzKNIG sFXNxro/FKvreFTLgJHxRVgIoX4aN/beVHIFNWj9nv/LpevCj4mi9gn4o2E5ma2QxxeF4 EWdjZU9i4u8nvnt79uldiOAICozss6DQFYns7UMj4oJe7OlRDJ4C3b6aJOQeQ84zm99NY +bTi/jcRyNf0mWW2V3j3TOgkwxgHKE9LQiuF22lEBE5jlFBPMw40vPn73YgGHoJhvtA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=ri6QwzLq; 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 A3931606A322; Fri, 4 Dec 2026 10:50:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=ri6QwzLq Received: by smtp.kernel.org (Postfix) with ESMTPSA id 177F11F000C2; Fri, 4 Dec 2026 10:50:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381429; bh=Jn+7FKOizKjJYEVbeEuo0bhH/OOSaph61jeq9D2xcAA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=ri6QwzLqRhnCDFYgC3w/q+QwTS8zNR18lkW23LXEJQQ86wZ25kWzVtAYRM+E75/vj WXLzKLXRj9rfPG487ZFwOl8pMewzw5Vd7XeIux5ysiycUgRi+JQkne2ApiRvVICGR9 A6spc2khVoXlgixN2KdO5Ll7PmegNo6zEmjixHDE= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Rongen Wang , Takashi Iwai Subject: [PATCH 7.3 698/742] ALSA: timer: Protect the slave instance flags with the timer lock Date: Fri, 4 Dec 2026 19:09:22 +0900 Message-ID: <20261204100653.594309080@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Rongen Wang commit 8e324d767bdc5ab38b0e1e921e786ff9ea514188 upstream. snd_timer_start_slave() and snd_timer_stop_slave() set and clear SNDRV_TIMER_IFLG_RUNNING in timeri->flags while holding only timeri_lock. For a slave that is linked to a master, the interrupt path updates the same word under timer->lock: snd_timer_process_callbacks() sets SNDRV_TIMER_IFLG_CALLBACK before it calls the instance callback and clears it afterwards. Both sides do a plain read-modify-write, so when they overlap one of the two updates is lost. If the set of SNDRV_TIMER_IFLG_CALLBACK is lost, snd_timer_close_locked() does not wait for the callback that is still running, the owner frees the instance, and the interrupt path clears the flag in the freed object: BUG: KASAN: slab-use-after-free in snd_timer_process_callbacks+0x269/0x2b0 Read of size 4 at addr ffff888007e5cc10 by task poc/90 snd_timer_process_callbacks+0x269/0x2b0 snd_timer_interrupt+0x7b8/0x1100 snd_utimer_ioctl+0x7c/0xb0 Freed by task 91: kfree+0x127/0x3b0 __snd_timer_user_ioctl.isra.0+0xe93/0x28f0 If the clear is lost instead, the flag stays set: the instance gets no further callbacks, and closing it spins in the drain loop of snd_timer_close_locked() with register_mutex held, which blocks every other open and close of an ALSA timer. Take timer->lock around the whole flag update of a linked slave, as snd_timer_start1() and snd_timer_stop1() do for master instances. The callers hold timeri_lock, so timeri->timer is stable, and the lock order is the one these functions already use. A slave that is not linked to a master cannot be reached from any timer and needs no additional protection. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Signed-off-by: Rongen Wang Link: https://patch.msgid.link/20261126-alsa-timer-slave-flags-v2-1-8e07a536fea3@nitrohex.com Signed-off-by: Takashi Iwai Signed-off-by: Greg Kroah-Hartman --- sound/core/timer.c | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) --- a/sound/core/timer.c +++ b/sound/core/timer.c @@ -657,9 +657,12 @@ static int snd_timer_start1(struct snd_t return result; } -/* start/continue a slave timer */ -static int snd_timer_start_slave(struct snd_timer_instance *timeri, - bool start) +/* + * start/continue a slave timer; + * called with timer->lock held when the slave is linked to a master + */ +static int __snd_timer_start_slave(struct snd_timer_instance *timeri, + bool start) { if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) return -EINVAL; @@ -667,7 +670,6 @@ static int snd_timer_start_slave(struct return -EBUSY; timeri->flags |= SNDRV_TIMER_IFLG_RUNNING; if (timeri->master && timeri->timer) { - guard(spinlock)(&timeri->timer->lock); list_add_tail(&timeri->active_list, &timeri->master->slave_active_head); snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START : @@ -676,6 +678,27 @@ static int snd_timer_start_slave(struct return 1; /* delayed start */ } +/* + * start/continue a slave timer + * + * The flags of a slave linked to a master are updated also from the timer + * interrupt (SNDRV_TIMER_IFLG_CALLBACK in snd_timer_process_callbacks()) + * under timer->lock. Change them only under the same lock, otherwise one + * of the two read-modify-write updates may get lost. The caller holds + * timeri_lock, hence timeri->timer can't change here; an unlinked slave is + * not reachable from any timer and needs no further protection. + */ +static int snd_timer_start_slave(struct snd_timer_instance *timeri, + bool start) +{ + struct snd_timer *timer = timeri->timer; + + if (!timer) + return __snd_timer_start_slave(timeri, start); + guard(spinlock)(&timer->lock); + return __snd_timer_start_slave(timeri, start); +} + /* stop/pause a master timer */ static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop) { @@ -718,15 +741,17 @@ static int snd_timer_stop1(struct snd_ti return 0; } -/* stop/pause a slave timer */ -static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop) +/* + * stop/pause a slave timer; + * called with timer->lock held when the slave is linked to a master + */ +static int __snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop) { bool running; running = timeri->flags & SNDRV_TIMER_IFLG_RUNNING; timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING; if (timeri->timer) { - guard(spinlock)(&timeri->timer->lock); list_del_init(&timeri->ack_list); list_del_init(&timeri->active_list); if (running) @@ -736,6 +761,17 @@ static int snd_timer_stop_slave(struct s return running ? 0 : -EBUSY; } +/* stop/pause a slave timer; see snd_timer_start_slave() for the locking */ +static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop) +{ + struct snd_timer *timer = timeri->timer; + + if (!timer) + return __snd_timer_stop_slave(timeri, stop); + guard(spinlock)(&timer->lock); + return __snd_timer_stop_slave(timeri, stop); +} + /* * start the timer instance */