Skip to content

Commit a820ccb

Browse files
committed
ALSA: pcm: Fix UAF at PCM release via PCM timer access
The PCM runtime object is created and freed dynamically at PCM stream open / close time. This is tracked via substream->runtime, and it's cleared at snd_pcm_detach_substream(). The runtime object assignment is protected by PCM open_mutex, so for all PCM operations, it's safely handled. However, each PCM substream provides also an ALSA timer interface, and user-space can access to this while closing a PCM substream. This may eventually lead to a UAF, as snd_pcm_timer_resolution() tries to access the runtime while clearing it in other side. Fortunately, it's the only concurrent access from the PCM timer, and it merely reads runtime->timer_resolution field. So, we can avoid the race by reordering kfree() and wrapping the substream->runtime clearance with the corresponding timer lock. Reported-by: [email protected] Cc: <[email protected]> Signed-off-by: Takashi Iwai <[email protected]>
1 parent 903d271 commit a820ccb

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

sound/core/pcm.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <sound/core.h>
2929
#include <sound/minors.h>
3030
#include <sound/pcm.h>
31+
#include <sound/timer.h>
3132
#include <sound/control.h>
3233
#include <sound/info.h>
3334

@@ -1054,8 +1055,13 @@ void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
10541055
snd_free_pages((void*)runtime->control,
10551056
PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
10561057
kfree(runtime->hw_constraints.rules);
1057-
kfree(runtime);
1058+
/* Avoid concurrent access to runtime via PCM timer interface */
1059+
if (substream->timer)
1060+
spin_lock_irq(&substream->timer->lock);
10581061
substream->runtime = NULL;
1062+
if (substream->timer)
1063+
spin_unlock_irq(&substream->timer->lock);
1064+
kfree(runtime);
10591065
put_pid(substream->pid);
10601066
substream->pid = NULL;
10611067
substream->pstr->substream_opened--;

0 commit comments

Comments
 (0)