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=1796390601; b=46yBQuadL0yjYrJz0Kgo754WAjJGOWiSbFosKgS4vLoFtnyBgmvPvPKBiEhlPMEA6INu UcPCuvHrF+L2SEyyvmlffG4OVtcamFQ/G1PAibWlVZKHT1S9sHJd4mEA4EJVxY+JPRqpO 8cXs+pQSL+rx1UFznco6dnOsds/upPRcDDAQN65EDg9DW8b3bKRFlszat6r6jsHY1bzOQ W4aqCfWyP/1vOU+pSqtSSWUp4I1BNS6GILtw/D9bljk0KRgsqRH36a1cHR95tsIWqA9lI Rs6bkMHYNGHnArk9xkNwxJg2DW1HwpHj8ZBU75BmbK32zuTs6iRfU/M2UVjS5CQ2SHg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796390601; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=ZKRxEE/TpqlnakHrwuMok2ObnC/auViQMEg0Vt17AdY=; b=1GDTWK0aRF292H+DxhYFn6CAuI1Yw/GqyWj/UJesWf8cJ2OlKJyNUZsu65hRDah/KnNU US8IAk4fW6g3ShcQetnoSPzP/t2p8AzGhUGSfSBf37KG+zCUnKp0538dhk6htpl2iA6f8 UdwRXohKwbnHmROzKekgW/S++/LoYHgBkLbyQ0YvoGy3rtFG5sPa0iJDBlDfS11Y4SPWC ToSJEXWWn/qh0yhnR2XBb4UPxN1SuDzXQegMdxxeCX0ne1zdq+LtS2Fd+jpTyOJzUEOP1 kSGEKBaBEAdlWkRQqpFyDcfPmG+U7zSDOic57jV2PU4aNaH0jSItU1TEqEMOFKmwQjw== 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=QgGG9vUv; 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 52407606A334 for ; Fri, 4 Dec 2026 13:23:21 +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=QgGG9vUv Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6171B1F000C2; Fri, 4 Dec 2026 13:23:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796390601; bh=ZKRxEE/TpqlnakHrwuMok2ObnC/auViQMEg0Vt17AdY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=QgGG9vUvLokFks66Vpj2OTbCkwiLoi14hQjdTGtJbhfzx2CWwI795Xz/l0IMHW3Y4 fMI6ACD1hGsmEXmpXBnXRBY12Et7qD43jCiArlDLw86m3EP7zMRS2n5LVZ6isMHNvW JUVVJRrOriBKvlxt2eZOLKhazNC+sYYQXo/woNLuIkfaTOuagDDqzenphif7WX1ekm ukncud0W9v03kCTPxN6IwCOkwJ/AlnVKs6VSJOeRK2X97vp1IjjLlEzcezHTiEsshV JebgbBYJqgXpgTQ31ldc3Pqs6AXigEL7YkpQeljFPgqFQ4P0f3lWKif1E0SpYHyf4l QdWGhScx9jWYA== From: Sasha Levin To: stable@vger.kernel.org Cc: Rongen Wang , Takashi Iwai , Sasha Levin Subject: [PATCH 6.6.y] ALSA: timer: Protect the slave instance flags with the timer lock Date: Fri, 4 Dec 2026 08:23:19 -0500 Message-ID: <20261204132309.3141827-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120318-sushi-relax-0fc7@gregkh> References: <2026120318-sushi-relax-0fc7@gregkh> 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 From: Rongen Wang [ Upstream commit 8e324d767bdc5ab38b0e1e921e786ff9ea514188 ] 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 [ sashal: Inlined the slave locking in the start and stop paths, taking slave_active_lock then timer->lock, because this tree has no guard() helpers or timeri_lock for upstream's wrappers. ] Signed-off-by: Sasha Levin --- sound/core/timer.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/sound/core/timer.c b/sound/core/timer.c index e7cf47104257f..fc208d2bce178 100644 --- a/sound/core/timer.c +++ b/sound/core/timer.c @@ -596,10 +596,14 @@ static int snd_timer_start1(struct snd_timer_instance *timeri, static int snd_timer_start_slave(struct snd_timer_instance *timeri, bool start) { + struct snd_timer *timer; unsigned long flags; int err; spin_lock_irqsave(&slave_active_lock, flags); + timer = timeri->timer; + if (timer) + spin_lock(&timer->lock); if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) { err = -EINVAL; goto unlock; @@ -609,16 +613,16 @@ static int snd_timer_start_slave(struct snd_timer_instance *timeri, goto unlock; } timeri->flags |= SNDRV_TIMER_IFLG_RUNNING; - if (timeri->master && timeri->timer) { - spin_lock(&timeri->timer->lock); + if (timeri->master && timer) { list_add_tail(&timeri->active_list, &timeri->master->slave_active_head); snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START : SNDRV_TIMER_EVENT_CONTINUE); - spin_unlock(&timeri->timer->lock); } err = 1; /* delayed start */ unlock: + if (timer) + spin_unlock(&timer->lock); spin_unlock_irqrestore(&slave_active_lock, flags); return err; } @@ -674,20 +678,23 @@ static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop) /* stop/pause a slave timer */ static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop) { + struct snd_timer *timer; unsigned long flags; bool running; spin_lock_irqsave(&slave_active_lock, flags); + timer = timeri->timer; + if (timer) + spin_lock(&timer->lock); running = timeri->flags & SNDRV_TIMER_IFLG_RUNNING; timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING; - if (timeri->timer) { - spin_lock(&timeri->timer->lock); + if (timer) { list_del_init(&timeri->ack_list); list_del_init(&timeri->active_list); if (running) snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP : SNDRV_TIMER_EVENT_PAUSE); - spin_unlock(&timeri->timer->lock); + spin_unlock(&timer->lock); } spin_unlock_irqrestore(&slave_active_lock, flags); return running ? 0 : -EBUSY; -- 2.53.0