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=1796387849; b=SeaP5tLOkbCsRA6HyKMYe0MqHVUK1N45EgGKS6d+NZvEKFF+5UYU+dKAWlM6VUscJlNV OtkKF/t+aAJH7pFgKBWGMRlIDR8d5U/Z/aqjxhbqrMTi7i3I+8C9+oVhr2nc9gm57Ox1p qiDBFi7O5cxt8TX6p439fMAAWfBFso8ujDc9Qzp0qv6nT8ks+LZGn87g3cG6TAfxjHZcn UgAEaDCxAhMqIyCqG6289QSE6J7Yp5HvqElrqY5lir7UtzrmXHanRqPhFYOQDO5i9IJej e2VpOvaA80qrxWV+Fhp2tytTMvsrwXGD+76FEBvweeG3lLD9cSQsVdpJQ2vfuMUqKqQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387849; 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=SJy3IDJPs0dkteC4T9sqVEkCYIX+j3TNcuMTQXDwbUQ=; b=jTrsbW4rPjIFHJxocQvrAKfFe5A8O/Ca/CSPts4bq+EEh4l0Ut2oed9dEBakr1N+bubd vRhOAlmCXQUmcuXx9KKcpzspXPqzqFW4FTsG7fJn8A8RXSMpwqzxRk/UaCrEGk4Ntq2XN PglBg5u/+4sA/WuboFONv2GPDXtPV+6wkm7A1wueI/oVcdKIfWmipSOfSLBr6JStd3cxd R6og9ERT6masp3Z7ozz/NIX4A0scH36d02SaHMJU/gwE9nQYH4E5XUSdtgc35M5F1qSaO EkmKHvs7QGPfwQTno+r34DjtHZOeNMKgPb4I8XRmTGRsgd63hSh2BCRRvTowEmS1Lmg== 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=Gcocm1ud; 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 22350606A335; Fri, 4 Dec 2026 12:37: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=Gcocm1ud Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8BB9E1F000CE; Fri, 4 Dec 2026 12:37:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387849; bh=SJy3IDJPs0dkteC4T9sqVEkCYIX+j3TNcuMTQXDwbUQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Gcocm1udUtugfpiy6POfWm2dJGouZNpoqwEcrhOQocrL6chwGJEUWMYohlOf4f3kp PxmSr4xU0C9vU2gfc8Dcw9NkOrZVUMdilaAWSAuswt175gY8wd2Zlnw7Pzwe7G6llx nHR9Z3EPKWK0KImCi7xXE7bXJZ2rLktpYk5L+wgk= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Cen Zhang , Takashi Iwai , Sasha Levin Subject: [PATCH 6.1 0383/1059] ALSA: rawmidi: Drain input work before freeing the runtime Date: Fri, 4 Dec 2026 18:58:34 +0900 Message-ID: <20261204100654.219368225@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Cen Zhang [ Upstream commit 8c3529f0a8faaac7d73333486f69da1617ecc579 ] The input runtime embeds event_work, so the work must be drained before close_substream() frees the runtime. The cancellation in snd_rawmidi_input_trigger() runs before close clears substream->opened, leaving a window for snd_rawmidi_receive() to enqueue more work. With a sequencer subscription to a USB MIDI input port, an in-flight completion can overlap the last subscription's midisynth_unsubscribe(): 1. snd_usbmidi_input_data() observes input_triggered and is delayed before calling snd_rawmidi_receive(). 2. Unsubscribe enters close_substream(), clears input_triggered through the provider trigger callback and returns from cancel_work_sync(). The substream is still opened. 3. The completion resumes. snd_rawmidi_receive() takes substream->lock, sees opened set and queues runtime->event_work before dropping the lock. 4. Close clears opened under that lock, closes the provider and frees the runtime. Stopping USB URBs does not drain the raw-MIDI work. 5. The queued work runs and snd_rawmidi_input_event_work() reads event or substream from the freed runtime, causing a use-after-free. Cancel input event_work again after clearing opened, before closing the provider or releasing the runtime's private data. Receive holds substream->lock through its opened check and work enqueue, so once close has cleared opened under that lock, no further receive can queue work. The final cancellation therefore drains any late work before the runtime is freed. Keep it outside the spinlock so a running callback can finish. Fixes: b3c705aa9e91 ("ALSA: rawmidi - Use workq for event handling") Assisted-by: LLM Signed-off-by: Cen Zhang Link: https://patch.msgid.link/pm-pmb-98489078ab56528d8e4a-v2-9408ce8c8f3c70b5c10d@gmail.com Signed-off-by: Takashi Iwai [ sashal: Context-only adaptation: kept the older spin_lock_irq()/spin_unlock_irq() block instead of scoped_guard(), and added the same cancel_work_sync() lines right after the unlock. ] Signed-off-by: Sasha Levin --- sound/core/rawmidi.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c index a1c4e280e7894..473b41a1a86a4 100644 --- a/sound/core/rawmidi.c +++ b/sound/core/rawmidi.c @@ -564,6 +564,9 @@ static void close_substream(struct snd_rawmidi *rmidi, substream->opened = 0; substream->append = 0; spin_unlock_irq(&substream->lock); + /* Drain input work queued after the earlier cancellation. */ + if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT) + cancel_work_sync(&substream->runtime->event_work); substream->ops->close(substream); if (substream->runtime->private_free) substream->runtime->private_free(substream); -- 2.53.0