From nobody Wed Jun 17 05:13:08 2026 Received: from mail-dl1-f50.google.com (mail-dl1-f50.google.com [74.125.82.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4C6A93815DB for ; Fri, 24 Apr 2026 12:49:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777034976; cv=none; b=atc3r6/0g7RJmyxx4ewvnBvgXdA/aJS/ynoqUlGAfK+wBjnLEE3JGz5lWhdVcPbeIE05Nynvenf/a6mKh6Ivz319nOf1JtXT4/e7lQN8aiKlhKexfReqLLODOmI+z0+oEfxT68hruHUFoiDlJ4qLN5coh3d/HE4XYZlzcgw7Bxc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777034976; c=relaxed/simple; bh=8DfXZXq4mVPMtrbEJ3azRH0nkf22Z7tVppnYqNc9f54=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=csGwVzb37t0ERTvseo53H5wBKGF5tPugl/K9WUeOaonVztLsH5VxPEK1f6P8faoYc+vD6iRYYCD9ynbqhD6TkQ6zetUgXIoE6MKn/eix83RHgL3hcApduirma66dYdTPyM4gxvg5POH6bdAQJ/SdV+coqDNndDanvIN2WiqnIFw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=GrIJ/z3Q; arc=none smtp.client-ip=74.125.82.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GrIJ/z3Q" Received: by mail-dl1-f50.google.com with SMTP id a92af1059eb24-12c637089ccso8002731c88.1 for ; Fri, 24 Apr 2026 05:49:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777034974; x=1777639774; darn=vger.kernel.org; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:from:to:cc:subject:date:message-id:reply-to; bh=5sDFo3WV98TxsiaFURx28Dc3liN7VLrYqdD8rKeIweI=; b=GrIJ/z3QWD7AZozMM+bPkLhs5omgGnw0D8xtOEkzBpycKwapo/xGLEjALMOLD9qlAk i1ahj/lZwRiF+yN54vy7//WxdEffYFcnRGf03xjvMujrT2rRvqXU5KpcB59X8UowlsbG yHfV+RmUOH15A2HNzboPUnEwqOWw/QiM8i3bOQ3dON5BK5xcg2so8hp10Q34wYLNsjbs Kfvrvu1WSpLPSg0oxY9WMr/TjydI6zfI5FdyYO4NTtaUgf5B1U1X4uCStgBMr6o39zrv JgSiZ0GBWQ4pN75XhOQRT9qGqQ4sqov9xrrNQPYSFH7y2IqFg4FpzraUJ6zKWua2wR5N mBvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777034974; x=1777639774; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=5sDFo3WV98TxsiaFURx28Dc3liN7VLrYqdD8rKeIweI=; b=mcZ0A7FRFpT85Q3VwgGgIx2Zxig9dfNUbDAAlW993CSdGXQPrszTAVsuqATJiY6Xxx TfMJb0sq5rUlAR+qF3LgJcfUBApnjWjjF7FJSSBqbIY6sKrD0CMkAjLfUEpCmvZ87P+n 0+baxIYym+XoLXn0iPnMdbN338YCtMeo2oAtwQ6iggWyR2QQ++WiU5m0E86TQ0hINh7F tM+wrOGJDNMjMAMnPXsonpB3CCKzA+8VgJiG/RUgLE1OxB/fsgCBX7206QdKuEmsc+RE Wmnj43P7voelPBWO3eHyBAi7B/NbaASKmm86KeUtEn6NY0rKY4E0bJwRsucohN7LHTvy P+Qw== X-Forwarded-Encrypted: i=1; AFNElJ/P0p4MRaf8cIUQW23ARX7i3XaY6TUxpNMrklI/str3jdA8hvQMONJIws5TDYSQF1+1LShTMYU+L4c7iW8=@vger.kernel.org X-Gm-Message-State: AOJu0Yz9WfgPHWPgdiNh2DHBkA0YIKKihXuO76hNeEpgE1zre+6jJQQR Zx0M8wbcDRlxOoQyK4NjHh89XpFfuIeTMPDmPV9eHdyvmhLBMSaOp5lI X-Gm-Gg: AeBDieuPtih+TaVKb2OfINwslTRIcp4d9qYN9+10H+ZBi+wqKkWccqYpkE91zkBCTXc wRjl1fk2utxLwyN8ZfhhAIQ0OfiyIYuH6+nq42yu7HqAeZV24Uo8Ft4o3tOzNRwz0JeacES7ZKV L+sc41N+vf1iEZVeuCrzBLYNorurQE+rO2kdMZXbYwJZaOaOv8w6Emp0ZgiKQZY3cw+76a039r1 E4BlR4JXyagYUY7snYsNnTnKlYGjcP+K7mdFwuEBh3zz7PcvEkwKsYtWNcWtiN2epclCBV5XA4C fAUC0s+ZKqZJjq/+8e/4WIMbNSeDH9l4g+gY8Cn4JLJ0IbHO7zrpLVRpbdf6AEcrQzJNmgY+g2/ KVQW24kRyDiFw4zd+rct1oNhN5CRkS+pOQztKxYp/zdhqyFrs9DVYc0Wi+qnaaro7MC28LhR3ZK 3helrIiCViYvgv3AfiUZUkOqXJ4PPAHc344fRq+fqX6jCDjoYMMgwqlt2qnAslCWGVYVuNqJoaf 6Gj4gnN1qYc X-Received: by 2002:a05:7022:418e:b0:12c:87f1:f41a with SMTP id a92af1059eb24-12c87f1f772mr14051063c88.21.1777034974262; Fri, 24 Apr 2026 05:49:34 -0700 (PDT) Received: from [192.168.1.18] (177-4-161-87.user3p.v-tal.net.br. [177.4.161.87]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-12c74a20eb5sm40524524c88.14.2026.04.24.05.49.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Apr 2026 05:49:33 -0700 (PDT) From: =?utf-8?q?C=C3=A1ssio_Gabriel?= Date: Fri, 24 Apr 2026 09:48:41 -0300 Subject: [PATCH v2] ALSA: aloop: Fix peer runtime UAF during format-change stop Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260424-alsa-aloop-peer-stop-uaf-v2-1-94e68101db8a@gmail.com> X-B4-Tracking: v=1; b=H4sIAAAAAAAC/3WNyw6CMBBFf4XM2jHtCPhY+R+GRSkD1ABtWiAaw r9bMC7dTOYk996zQGBvOMAtWcDzbIKxQwQ6JKBbNTSMpooMJCgXKRGqLqh4rHXomD2GMX6TqlG ItGKSJMqMINad59q89ulH8eUwlU/W47a3JVoTy/69u2e55X6a03/NLFEiZdVFXc9pVuf63vTKd EdteyjWdf0ANcApM9IAAAA= X-Change-ID: 20260422-alsa-aloop-peer-stop-uaf-004de2120b52 To: Takashi Iwai , Jaroslav Kysela Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+8fa95c41eafbc9d2ff6f@syzkaller.appspotmail.com, stable@vger.kernel.org, =?utf-8?q?C=C3=A1ssio_Gabriel?= X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4746; i=cassiogabrielcontato@gmail.com; h=from:subject:message-id; bh=8DfXZXq4mVPMtrbEJ3azRH0nkf22Z7tVppnYqNc9f54=; b=owGbwMvMwCV2IdZeKur/u2bG02pJDJmv026ZFe6/UGn8a6p9X3f5tG1dvM9r7hm6BPryTr3Hk r6J/VJTRykLgxgXg6yYIsvqpEWWe7oeXK2PW+EBM4eVCWQIAxenAExEYTPD/+wjbo4NDyKuGDFG 3VtTtWxbNEcn41Rxy5tzfX05g5bbKjMyvCmsbtx3t//9Be55giv2/5ptlyTwINf9jN2Pre7hEi8 TGQA= X-Developer-Key: i=cassiogabrielcontato@gmail.com; a=openpgp; fpr=AB62A239BC8AE0D57F5EA848D05D3F1A5AFFEE83 loopback_check_format() may stop the capture side when playback starts with parameters that no longer match a running capture stream. Commit 826af7fa62e3 ("ALSA: aloop: Fix racy access at PCM trigger") moved the peer lookup under cable->lock, but the actual snd_pcm_stop() still runs after dropping that lock. A concurrent close can clear the capture entry from cable->streams[] and detach or free its runtime while the playback trigger path still holds a stale peer substream pointer. Keep a per-cable count of in-flight peer stops before dropping cable->lock, and make free_cable() wait for those stops before detaching the runtime. This preserves the existing behavior while making the peer runtime lifetime explicit. Reported-by: syzbot+8fa95c41eafbc9d2ff6f@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=3D8fa95c41eafbc9d2ff6f Fixes: 597603d615d2 ("ALSA: introduce the snd-aloop module for the PCM loop= back") Cc: stable@vger.kernel.org Suggested-by: Takashi Iwai Signed-off-by: C=C3=A1ssio Gabriel --- Changes in v2: - simplify the peer-stop path in loopback_check_format() - drop the extra stop_dpcm indirection and runtime/running recheck - keep only the stop_count/stop_wait lifetime serialization - Link to v1: https://patch.msgid.link/20260423-alsa-aloop-peer-stop-uaf-v1= -1-25d8a9745f6c@gmail.com --- sound/drivers/aloop.c | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/sound/drivers/aloop.c b/sound/drivers/aloop.c index aa0d2fcb1a18..a37a1695f51c 100644 --- a/sound/drivers/aloop.c +++ b/sound/drivers/aloop.c @@ -99,6 +99,9 @@ struct loopback_ops { struct loopback_cable { spinlock_t lock; struct loopback_pcm *streams[2]; + /* in-flight peer stops running outside cable->lock */ + atomic_t stop_count; + wait_queue_head_t stop_wait; struct snd_pcm_hardware hw; /* flags */ unsigned int valid; @@ -366,8 +369,11 @@ static int loopback_check_format(struct loopback_cable= *cable, int stream) return 0; if (stream =3D=3D SNDRV_PCM_STREAM_CAPTURE) return -EIO; - else if (cruntime->state =3D=3D SNDRV_PCM_STATE_RUNNING) + else if (cruntime->state =3D=3D SNDRV_PCM_STATE_RUNNING) { + /* close must not free the peer runtime below */ + atomic_inc(&cable->stop_count); stop_capture =3D true; + } } =20 setup =3D get_setup(dpcm_play); @@ -396,8 +402,11 @@ static int loopback_check_format(struct loopback_cable= *cable, int stream) } } =20 - if (stop_capture) + if (stop_capture) { snd_pcm_stop(dpcm_capt->substream, SNDRV_PCM_STATE_DRAINING); + if (atomic_dec_and_test(&cable->stop_count)) + wake_up(&cable->stop_wait); + } =20 return 0; } @@ -1049,23 +1058,29 @@ static void free_cable(struct snd_pcm_substream *su= bstream) struct loopback *loopback =3D substream->private_data; int dev =3D get_cable_index(substream); struct loopback_cable *cable; + struct loopback_pcm *dpcm; + bool other_alive; =20 cable =3D loopback->cables[substream->number][dev]; if (!cable) return; - if (cable->streams[!substream->stream]) { - /* other stream is still alive */ - guard(spinlock_irq)(&cable->lock); - cable->streams[substream->stream] =3D NULL; - } else { - struct loopback_pcm *dpcm =3D substream->runtime->private_data; =20 - if (cable->ops && cable->ops->close_cable && dpcm) - cable->ops->close_cable(dpcm); - /* free the cable */ - loopback->cables[substream->number][dev] =3D NULL; - kfree(cable); + scoped_guard(spinlock_irq, &cable->lock) { + cable->streams[substream->stream] =3D NULL; + other_alive =3D cable->streams[!substream->stream]; } + + /* Pair with the stop_count increment in loopback_check_format(). */ + wait_event(cable->stop_wait, !atomic_read(&cable->stop_count)); + if (other_alive) + return; + + dpcm =3D substream->runtime->private_data; + if (cable->ops && cable->ops->close_cable && dpcm) + cable->ops->close_cable(dpcm); + /* free the cable */ + loopback->cables[substream->number][dev] =3D NULL; + kfree(cable); } =20 static int loopback_jiffies_timer_open(struct loopback_pcm *dpcm) @@ -1260,6 +1275,8 @@ static int loopback_open(struct snd_pcm_substream *su= bstream) goto unlock; } spin_lock_init(&cable->lock); + atomic_set(&cable->stop_count, 0); + init_waitqueue_head(&cable->stop_wait); cable->hw =3D loopback_pcm_hardware; if (loopback->timer_source) cable->ops =3D &loopback_snd_timer_ops; --- base-commit: 876c495d412ef67bd4d0bdc4b74b0bd3d9f4e890 change-id: 20260422-alsa-aloop-peer-stop-uaf-004de2120b52 Best regards, -- =20 C=C3=A1ssio Gabriel