From dca947d4d26dbf925a64a6cfb2ddbc035e831a3d Mon Sep 17 00:00:00 2001
From: Takashi Iwai <tiwai@suse.de>
Date: Tue, 22 Mar 2022 18:07:18 +0100
Subject: [PATCH] ALSA: pcm: Fix races among concurrent read/write and buffer
changes
Git-commit: dca947d4d26dbf925a64a6cfb2ddbc035e831a3d
Patch-mainline: v5.18-rc1
References: CVE-2022-1048 bsc#1197331
In the current PCM design, the read/write syscalls (as well as the
equivalent ioctls) are allowed before the PCM stream is running, that
is, at PCM PREPARED state. Meanwhile, we also allow to re-issue
hw_params and hw_free ioctl calls at the PREPARED state that may
change or free the buffers, too. The problem is that there is no
protection against those mix-ups.
This patch applies the previously introduced runtime->buffer_mutex to
the read/write operations so that the concurrent hw_params or hw_free
call can no longer interfere during the operation. The mutex is
unlocked before scheduling, so we don't take it too long.
Cc: <stable@vger.kernel.org>
Reviewed-by: Jaroslav Kysela <perex@perex.cz>
Link: https://lore.kernel.org/r/20220322170720.3529-3-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
sound/core/pcm_lib.c | 8 ++++++++
1 file changed, 8 insertions(+)
--- a/sound/core/pcm_lib.c
+++ b/sound/core/pcm_lib.c
@@ -1956,9 +1956,11 @@ static int wait_for_avail(struct snd_pcm
if (avail >= runtime->twake)
break;
snd_pcm_stream_unlock_irq(substream);
+ mutex_unlock(&runtime->buffer_mutex);
tout = schedule_timeout(wait_time);
+ mutex_lock(&runtime->buffer_mutex);
snd_pcm_stream_lock_irq(substream);
set_current_state(TASK_INTERRUPTIBLE);
switch (runtime->status->state) {
@@ -2035,6 +2037,7 @@ static snd_pcm_sframes_t snd_pcm_lib_wri
if (size == 0)
return 0;
+ mutex_lock(&runtime->buffer_mutex);
snd_pcm_stream_lock_irq(substream);
switch (runtime->status->state) {
case SNDRV_PCM_STATE_PREPARED:
@@ -2077,6 +2080,7 @@ static snd_pcm_sframes_t snd_pcm_lib_wri
if (snd_BUG_ON(!frames)) {
runtime->twake = 0;
snd_pcm_stream_unlock_irq(substream);
+ mutex_unlock(&runtime->buffer_mutex);
return -EINVAL;
}
appl_ptr = runtime->control->appl_ptr;
@@ -2119,6 +2123,7 @@ static snd_pcm_sframes_t snd_pcm_lib_wri
if (xfer > 0 && err >= 0)
snd_pcm_update_state(substream, runtime);
snd_pcm_stream_unlock_irq(substream);
+ mutex_unlock(&runtime->buffer_mutex);
return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
}
@@ -2253,6 +2258,7 @@ static snd_pcm_sframes_t snd_pcm_lib_rea
if (size == 0)
return 0;
+ mutex_lock(&runtime->buffer_mutex);
snd_pcm_stream_lock_irq(substream);
switch (runtime->status->state) {
case SNDRV_PCM_STATE_PREPARED:
@@ -2311,6 +2317,7 @@ static snd_pcm_sframes_t snd_pcm_lib_rea
if (snd_BUG_ON(!frames)) {
runtime->twake = 0;
snd_pcm_stream_unlock_irq(substream);
+ mutex_unlock(&runtime->buffer_mutex);
return -EINVAL;
}
snd_pcm_stream_unlock_irq(substream);
@@ -2345,6 +2352,7 @@ static snd_pcm_sframes_t snd_pcm_lib_rea
if (xfer > 0 && err >= 0)
snd_pcm_update_state(substream, runtime);
snd_pcm_stream_unlock_irq(substream);
+ mutex_unlock(&runtime->buffer_mutex);
return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
}