Skip to content

Commit 9b1dbd6

Browse files
mehulraotiwai
authored andcommitted
ALSA: pcm: fix use-after-free on linked stream runtime in snd_pcm_drain()
In the drain loop, the local variable 'runtime' is reassigned to a linked stream's runtime (runtime = s->runtime at line 2157). After releasing the stream lock at line 2169, the code accesses runtime->no_period_wakeup, runtime->rate, and runtime->buffer_size (lines 2170-2178) — all referencing the linked stream's runtime without any lock or refcount protecting its lifetime. A concurrent close() on the linked stream's fd triggers snd_pcm_release_substream() → snd_pcm_drop() → pcm_release_private() → snd_pcm_unlink() → snd_pcm_detach_substream() → kfree(runtime). No synchronization prevents kfree(runtime) from completing while the drain path dereferences the stale pointer. Fix by caching the needed runtime fields (no_period_wakeup, rate, buffer_size) into local variables while still holding the stream lock, and using the cached values after the lock is released. Fixes: f2b3614 ("ALSA: PCM - Don't check DMA time-out too shortly") Cc: stable@vger.kernel.org Signed-off-by: Mehul Rao <mehulrao@gmail.com> Link: https://patch.msgid.link/20260305193508.311096-1-mehulrao@gmail.com Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 56fbbe0 commit 9b1dbd6

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

sound/core/pcm_native.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,10 @@ static int snd_pcm_drain(struct snd_pcm_substream *substream,
21442144
for (;;) {
21452145
long tout;
21462146
struct snd_pcm_runtime *to_check;
2147+
unsigned int drain_rate;
2148+
snd_pcm_uframes_t drain_bufsz;
2149+
bool drain_no_period_wakeup;
2150+
21472151
if (signal_pending(current)) {
21482152
result = -ERESTARTSYS;
21492153
break;
@@ -2163,16 +2167,25 @@ static int snd_pcm_drain(struct snd_pcm_substream *substream,
21632167
snd_pcm_group_unref(group, substream);
21642168
if (!to_check)
21652169
break; /* all drained */
2170+
/*
2171+
* Cache the runtime fields needed after unlock.
2172+
* A concurrent close() on the linked stream may free
2173+
* its runtime via snd_pcm_detach_substream() once we
2174+
* release the stream lock below.
2175+
*/
2176+
drain_no_period_wakeup = to_check->no_period_wakeup;
2177+
drain_rate = to_check->rate;
2178+
drain_bufsz = to_check->buffer_size;
21662179
init_waitqueue_entry(&wait, current);
21672180
set_current_state(TASK_INTERRUPTIBLE);
21682181
add_wait_queue(&to_check->sleep, &wait);
21692182
snd_pcm_stream_unlock_irq(substream);
2170-
if (runtime->no_period_wakeup)
2183+
if (drain_no_period_wakeup)
21712184
tout = MAX_SCHEDULE_TIMEOUT;
21722185
else {
21732186
tout = 100;
2174-
if (runtime->rate) {
2175-
long t = runtime->buffer_size * 1100 / runtime->rate;
2187+
if (drain_rate) {
2188+
long t = drain_bufsz * 1100 / drain_rate;
21762189
tout = max(t, tout);
21772190
}
21782191
tout = msecs_to_jiffies(tout);

0 commit comments

Comments
 (0)