All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 4.19 1/5] ALSA: pcm: Fix races among concurrent hw_params and hw_free calls
@ 2022-05-13  8:00 Ovidiu Panait
  2022-05-13  8:00 ` [PATCH 4.19 2/5] ALSA: pcm: Fix races among concurrent read/write and buffer changes Ovidiu Panait
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Ovidiu Panait @ 2022-05-13  8:00 UTC (permalink / raw)
  To: stable; +Cc: Takashi Iwai, Hu Jiahui, Jaroslav Kysela, Ovidiu Panait

From: Takashi Iwai <tiwai@suse.de>

commit 92ee3c60ec9fe64404dc035e7c41277d74aa26cb upstream.

Currently we have neither proper check nor protection against the
concurrent calls of PCM hw_params and hw_free ioctls, which may result
in a UAF.  Since the existing PCM stream lock can't be used for
protecting the whole ioctl operations, we need a new mutex to protect
those racy calls.

This patch introduced a new mutex, runtime->buffer_mutex, and applies
it to both hw_params and hw_free ioctl code paths.  Along with it, the
both functions are slightly modified (the mmap_count check is moved
into the state-check block) for code simplicity.

Reported-by: Hu Jiahui <kirin.say@gmail.com>
Cc: <stable@vger.kernel.org>
Reviewed-by: Jaroslav Kysela <perex@perex.cz>
Link: https://lore.kernel.org/r/20220322170720.3529-2-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
[OP: backport to 4.19: adjusted context]
Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
---
 include/sound/pcm.h     |  1 +
 sound/core/pcm.c        |  2 ++
 sound/core/pcm_native.c | 55 +++++++++++++++++++++++++++--------------
 3 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/include/sound/pcm.h b/include/sound/pcm.h
index d6bd3caf6878..0871e16cd04b 100644
--- a/include/sound/pcm.h
+++ b/include/sound/pcm.h
@@ -404,6 +404,7 @@ struct snd_pcm_runtime {
 	wait_queue_head_t sleep;	/* poll sleep */
 	wait_queue_head_t tsleep;	/* transfer sleep */
 	struct fasync_struct *fasync;
+	struct mutex buffer_mutex;	/* protect for buffer changes */
 
 	/* -- private section -- */
 	void *private_data;
diff --git a/sound/core/pcm.c b/sound/core/pcm.c
index b6ed38dec435..a11365dc5349 100644
--- a/sound/core/pcm.c
+++ b/sound/core/pcm.c
@@ -1031,6 +1031,7 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
 	init_waitqueue_head(&runtime->tsleep);
 
 	runtime->status->state = SNDRV_PCM_STATE_OPEN;
+	mutex_init(&runtime->buffer_mutex);
 
 	substream->runtime = runtime;
 	substream->private_data = pcm->private_data;
@@ -1062,6 +1063,7 @@ void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
 	substream->runtime = NULL;
 	if (substream->timer)
 		spin_unlock_irq(&substream->timer->lock);
+	mutex_destroy(&runtime->buffer_mutex);
 	kfree(runtime);
 	put_pid(substream->pid);
 	substream->pid = NULL;
diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index c92eca627840..2b46a2ebefe0 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -666,33 +666,40 @@ static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
 	return 0;
 }
 
+#if IS_ENABLED(CONFIG_SND_PCM_OSS)
+#define is_oss_stream(substream)	((substream)->oss.oss)
+#else
+#define is_oss_stream(substream)	false
+#endif
+
 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
 			     struct snd_pcm_hw_params *params)
 {
 	struct snd_pcm_runtime *runtime;
-	int err, usecs;
+	int err = 0, usecs;
 	unsigned int bits;
 	snd_pcm_uframes_t frames;
 
 	if (PCM_RUNTIME_CHECK(substream))
 		return -ENXIO;
 	runtime = substream->runtime;
+	mutex_lock(&runtime->buffer_mutex);
 	snd_pcm_stream_lock_irq(substream);
 	switch (runtime->status->state) {
 	case SNDRV_PCM_STATE_OPEN:
 	case SNDRV_PCM_STATE_SETUP:
 	case SNDRV_PCM_STATE_PREPARED:
+		if (!is_oss_stream(substream) &&
+		    atomic_read(&substream->mmap_count))
+			err = -EBADFD;
 		break;
 	default:
-		snd_pcm_stream_unlock_irq(substream);
-		return -EBADFD;
+		err = -EBADFD;
+		break;
 	}
 	snd_pcm_stream_unlock_irq(substream);
-#if IS_ENABLED(CONFIG_SND_PCM_OSS)
-	if (!substream->oss.oss)
-#endif
-		if (atomic_read(&substream->mmap_count))
-			return -EBADFD;
+	if (err)
+		goto unlock;
 
 	params->rmask = ~0U;
 	err = snd_pcm_hw_refine(substream, params);
@@ -769,14 +776,19 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
 	if ((usecs = period_to_usecs(runtime)) >= 0)
 		pm_qos_add_request(&substream->latency_pm_qos_req,
 				   PM_QOS_CPU_DMA_LATENCY, usecs);
-	return 0;
+	err = 0;
  _error:
-	/* hardware might be unusable from this time,
-	   so we force application to retry to set
-	   the correct hardware parameter settings */
-	snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
-	if (substream->ops->hw_free != NULL)
-		substream->ops->hw_free(substream);
+	if (err) {
+		/* hardware might be unusable from this time,
+		 * so we force application to retry to set
+		 * the correct hardware parameter settings
+		 */
+		snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
+		if (substream->ops->hw_free != NULL)
+			substream->ops->hw_free(substream);
+	}
+ unlock:
+	mutex_unlock(&runtime->buffer_mutex);
 	return err;
 }
 
@@ -809,22 +821,27 @@ static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
 	if (PCM_RUNTIME_CHECK(substream))
 		return -ENXIO;
 	runtime = substream->runtime;
+	mutex_lock(&runtime->buffer_mutex);
 	snd_pcm_stream_lock_irq(substream);
 	switch (runtime->status->state) {
 	case SNDRV_PCM_STATE_SETUP:
 	case SNDRV_PCM_STATE_PREPARED:
+		if (atomic_read(&substream->mmap_count))
+			result = -EBADFD;
 		break;
 	default:
-		snd_pcm_stream_unlock_irq(substream);
-		return -EBADFD;
+		result = -EBADFD;
+		break;
 	}
 	snd_pcm_stream_unlock_irq(substream);
-	if (atomic_read(&substream->mmap_count))
-		return -EBADFD;
+	if (result)
+		goto unlock;
 	if (substream->ops->hw_free)
 		result = substream->ops->hw_free(substream);
 	snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
 	pm_qos_remove_request(&substream->latency_pm_qos_req);
+ unlock:
+	mutex_unlock(&runtime->buffer_mutex);
 	return result;
 }
 
-- 
2.36.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4.19 2/5] ALSA: pcm: Fix races among concurrent read/write and buffer changes
  2022-05-13  8:00 [PATCH 4.19 1/5] ALSA: pcm: Fix races among concurrent hw_params and hw_free calls Ovidiu Panait
@ 2022-05-13  8:00 ` Ovidiu Panait
  2022-05-13  8:00 ` [PATCH 4.19 3/5] ALSA: pcm: Fix races among concurrent prepare and hw_params/hw_free calls Ovidiu Panait
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Ovidiu Panait @ 2022-05-13  8:00 UTC (permalink / raw)
  To: stable; +Cc: Takashi Iwai, Jaroslav Kysela, Ovidiu Panait

From: Takashi Iwai <tiwai@suse.de>

commit dca947d4d26dbf925a64a6cfb2ddbc035e831a3d upstream.

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>
Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
---
 sound/core/pcm_lib.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c
index da454eeee5c9..552c642ee53a 100644
--- a/sound/core/pcm_lib.c
+++ b/sound/core/pcm_lib.c
@@ -1876,9 +1876,11 @@ static int wait_for_avail(struct snd_pcm_substream *substream,
 		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) {
@@ -2172,6 +2174,7 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
 
 	nonblock = !!(substream->f_flags & O_NONBLOCK);
 
+	mutex_lock(&runtime->buffer_mutex);
 	snd_pcm_stream_lock_irq(substream);
 	err = pcm_accessible_state(runtime);
 	if (err < 0)
@@ -2254,6 +2257,7 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
 	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;
 }
 EXPORT_SYMBOL(__snd_pcm_lib_xfer);
-- 
2.36.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4.19 3/5] ALSA: pcm: Fix races among concurrent prepare and hw_params/hw_free calls
  2022-05-13  8:00 [PATCH 4.19 1/5] ALSA: pcm: Fix races among concurrent hw_params and hw_free calls Ovidiu Panait
  2022-05-13  8:00 ` [PATCH 4.19 2/5] ALSA: pcm: Fix races among concurrent read/write and buffer changes Ovidiu Panait
@ 2022-05-13  8:00 ` Ovidiu Panait
  2022-05-13  8:00 ` [PATCH 4.19 4/5] ALSA: pcm: Fix races among concurrent prealloc proc writes Ovidiu Panait
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Ovidiu Panait @ 2022-05-13  8:00 UTC (permalink / raw)
  To: stable; +Cc: Takashi Iwai, Jaroslav Kysela, Ovidiu Panait

From: Takashi Iwai <tiwai@suse.de>

commit 3c3201f8c7bb77eb53b08a3ca8d9a4ddc500b4c0 upstream.

Like the previous fixes to hw_params and hw_free ioctl races, we need
to paper over the concurrent prepare ioctl calls against hw_params and
hw_free, too.

This patch implements the locking with the existing
runtime->buffer_mutex for prepare ioctls.  Unlike the previous case
for snd_pcm_hw_hw_params() and snd_pcm_hw_free(), snd_pcm_prepare() is
performed to the linked streams, hence the lock can't be applied
simply on the top.  For tracking the lock in each linked substream, we
modify snd_pcm_action_group() slightly and apply the buffer_mutex for
the case stream_lock=false (formerly there was no lock applied)
there.

Cc: <stable@vger.kernel.org>
Reviewed-by: Jaroslav Kysela <perex@perex.cz>
Link: https://lore.kernel.org/r/20220322170720.3529-4-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
[OP: backport to 4.19: adjusted context]
Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
---
 sound/core/pcm_native.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)

diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 2b46a2ebefe0..f2508f1f13a3 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -1078,15 +1078,17 @@ struct action_ops {
  */
 static int snd_pcm_action_group(const struct action_ops *ops,
 				struct snd_pcm_substream *substream,
-				int state, int do_lock)
+				int state, int stream_lock)
 {
 	struct snd_pcm_substream *s = NULL;
 	struct snd_pcm_substream *s1;
 	int res = 0, depth = 1;
 
 	snd_pcm_group_for_each_entry(s, substream) {
-		if (do_lock && s != substream) {
-			if (s->pcm->nonatomic)
+		if (s != substream) {
+			if (!stream_lock)
+				mutex_lock_nested(&s->runtime->buffer_mutex, depth);
+			else if (s->pcm->nonatomic)
 				mutex_lock_nested(&s->self_group.mutex, depth);
 			else
 				spin_lock_nested(&s->self_group.lock, depth);
@@ -1114,18 +1116,18 @@ static int snd_pcm_action_group(const struct action_ops *ops,
 		ops->post_action(s, state);
 	}
  _unlock:
-	if (do_lock) {
-		/* unlock streams */
-		snd_pcm_group_for_each_entry(s1, substream) {
-			if (s1 != substream) {
-				if (s1->pcm->nonatomic)
-					mutex_unlock(&s1->self_group.mutex);
-				else
-					spin_unlock(&s1->self_group.lock);
-			}
-			if (s1 == s)	/* end */
-				break;
+	/* unlock streams */
+	snd_pcm_group_for_each_entry(s1, substream) {
+		if (s1 != substream) {
+			if (!stream_lock)
+				mutex_unlock(&s1->runtime->buffer_mutex);
+			else if (s1->pcm->nonatomic)
+				mutex_unlock(&s1->self_group.mutex);
+			else
+				spin_unlock(&s1->self_group.lock);
 		}
+		if (s1 == s)	/* end */
+			break;
 	}
 	return res;
 }
@@ -1206,10 +1208,12 @@ static int snd_pcm_action_nonatomic(const struct action_ops *ops,
 	int res;
 
 	down_read(&snd_pcm_link_rwsem);
+	mutex_lock(&substream->runtime->buffer_mutex);
 	if (snd_pcm_stream_linked(substream))
 		res = snd_pcm_action_group(ops, substream, state, 0);
 	else
 		res = snd_pcm_action_single(ops, substream, state);
+	mutex_unlock(&substream->runtime->buffer_mutex);
 	up_read(&snd_pcm_link_rwsem);
 	return res;
 }
-- 
2.36.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4.19 4/5] ALSA: pcm: Fix races among concurrent prealloc proc writes
  2022-05-13  8:00 [PATCH 4.19 1/5] ALSA: pcm: Fix races among concurrent hw_params and hw_free calls Ovidiu Panait
  2022-05-13  8:00 ` [PATCH 4.19 2/5] ALSA: pcm: Fix races among concurrent read/write and buffer changes Ovidiu Panait
  2022-05-13  8:00 ` [PATCH 4.19 3/5] ALSA: pcm: Fix races among concurrent prepare and hw_params/hw_free calls Ovidiu Panait
@ 2022-05-13  8:00 ` Ovidiu Panait
  2022-05-13  8:00 ` [PATCH 4.19 5/5] ALSA: pcm: Fix potential AB/BA lock with buffer_mutex and mmap_lock Ovidiu Panait
  2022-05-13  8:31 ` [PATCH 4.19 1/5] ALSA: pcm: Fix races among concurrent hw_params and hw_free calls Greg KH
  4 siblings, 0 replies; 6+ messages in thread
From: Ovidiu Panait @ 2022-05-13  8:00 UTC (permalink / raw)
  To: stable; +Cc: Takashi Iwai, Jaroslav Kysela, Ovidiu Panait

From: Takashi Iwai <tiwai@suse.de>

commit 69534c48ba8ce552ce383b3dfdb271ffe51820c3 upstream.

We have no protection against concurrent PCM buffer preallocation
changes via proc files, and it may potentially lead to UAF or some
weird problem.  This patch applies the PCM open_mutex to the proc
write operation for avoiding the racy proc writes and the PCM stream
open (and further operations).

Cc: <stable@vger.kernel.org>
Reviewed-by: Jaroslav Kysela <perex@perex.cz>
Link: https://lore.kernel.org/r/20220322170720.3529-5-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
[OP: backport to 4.19: adjusted context]
Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
---
 sound/core/pcm_memory.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/sound/core/pcm_memory.c b/sound/core/pcm_memory.c
index 4b5356a10315..48e5f0091ce4 100644
--- a/sound/core/pcm_memory.c
+++ b/sound/core/pcm_memory.c
@@ -160,19 +160,20 @@ static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
 	size_t size;
 	struct snd_dma_buffer new_dmab;
 
+	mutex_lock(&substream->pcm->open_mutex);
 	if (substream->runtime) {
 		buffer->error = -EBUSY;
-		return;
+		goto unlock;
 	}
 	if (!snd_info_get_line(buffer, line, sizeof(line))) {
 		snd_info_get_str(str, line, sizeof(str));
 		size = simple_strtoul(str, NULL, 10) * 1024;
 		if ((size != 0 && size < 8192) || size > substream->dma_max) {
 			buffer->error = -EINVAL;
-			return;
+			goto unlock;
 		}
 		if (substream->dma_buffer.bytes == size)
-			return;
+			goto unlock;
 		memset(&new_dmab, 0, sizeof(new_dmab));
 		new_dmab.dev = substream->dma_buffer.dev;
 		if (size > 0) {
@@ -180,7 +181,7 @@ static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
 						substream->dma_buffer.dev.dev,
 						size, &new_dmab) < 0) {
 				buffer->error = -ENOMEM;
-				return;
+				goto unlock;
 			}
 			substream->buffer_bytes_max = size;
 		} else {
@@ -192,6 +193,8 @@ static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry,
 	} else {
 		buffer->error = -EINVAL;
 	}
+ unlock:
+	mutex_unlock(&substream->pcm->open_mutex);
 }
 
 static inline void preallocate_info_init(struct snd_pcm_substream *substream)
-- 
2.36.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4.19 5/5] ALSA: pcm: Fix potential AB/BA lock with buffer_mutex and mmap_lock
  2022-05-13  8:00 [PATCH 4.19 1/5] ALSA: pcm: Fix races among concurrent hw_params and hw_free calls Ovidiu Panait
                   ` (2 preceding siblings ...)
  2022-05-13  8:00 ` [PATCH 4.19 4/5] ALSA: pcm: Fix races among concurrent prealloc proc writes Ovidiu Panait
@ 2022-05-13  8:00 ` Ovidiu Panait
  2022-05-13  8:31 ` [PATCH 4.19 1/5] ALSA: pcm: Fix races among concurrent hw_params and hw_free calls Greg KH
  4 siblings, 0 replies; 6+ messages in thread
From: Ovidiu Panait @ 2022-05-13  8:00 UTC (permalink / raw)
  To: stable; +Cc: Takashi Iwai, syzbot+6e5c88838328e99c7e1c, Ovidiu Panait

From: Takashi Iwai <tiwai@suse.de>

commit bc55cfd5718c7c23e5524582e9fa70b4d10f2433 upstream.

syzbot caught a potential deadlock between the PCM
runtime->buffer_mutex and the mm->mmap_lock.  It was brought by the
recent fix to cover the racy read/write and other ioctls, and in that
commit, I overlooked a (hopefully only) corner case that may take the
revert lock, namely, the OSS mmap.  The OSS mmap operation
exceptionally allows to re-configure the parameters inside the OSS
mmap syscall, where mm->mmap_mutex is already held.  Meanwhile, the
copy_from/to_user calls at read/write operations also take the
mm->mmap_lock internally, hence it may lead to a AB/BA deadlock.

A similar problem was already seen in the past and we fixed it with a
refcount (in commit b248371628aa).  The former fix covered only the
call paths with OSS read/write and OSS ioctls, while we need to cover
the concurrent access via both ALSA and OSS APIs now.

This patch addresses the problem above by replacing the buffer_mutex
lock in the read/write operations with a refcount similar as we've
used for OSS.  The new field, runtime->buffer_accessing, keeps the
number of concurrent read/write operations.  Unlike the former
buffer_mutex protection, this protects only around the
copy_from/to_user() calls; the other codes are basically protected by
the PCM stream lock.  The refcount can be a negative, meaning blocked
by the ioctls.  If a negative value is seen, the read/write aborts
with -EBUSY.  In the ioctl side, OTOH, they check this refcount, too,
and set to a negative value for blocking unless it's already being
accessed.

Reported-by: syzbot+6e5c88838328e99c7e1c@syzkaller.appspotmail.com
Fixes: dca947d4d26d ("ALSA: pcm: Fix races among concurrent read/write and buffer changes")
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/000000000000381a0d05db622a81@google.com
Link: https://lore.kernel.org/r/20220330120903.4738-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
[OP: backport to 4.19: adjusted context]
Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
---
 include/sound/pcm.h     |  1 +
 sound/core/pcm.c        |  1 +
 sound/core/pcm_lib.c    |  9 +++++----
 sound/core/pcm_native.c | 39 ++++++++++++++++++++++++++++++++-------
 4 files changed, 39 insertions(+), 11 deletions(-)

diff --git a/include/sound/pcm.h b/include/sound/pcm.h
index 0871e16cd04b..cd7874535a4b 100644
--- a/include/sound/pcm.h
+++ b/include/sound/pcm.h
@@ -405,6 +405,7 @@ struct snd_pcm_runtime {
 	wait_queue_head_t tsleep;	/* transfer sleep */
 	struct fasync_struct *fasync;
 	struct mutex buffer_mutex;	/* protect for buffer changes */
+	atomic_t buffer_accessing;	/* >0: in r/w operation, <0: blocked */
 
 	/* -- private section -- */
 	void *private_data;
diff --git a/sound/core/pcm.c b/sound/core/pcm.c
index a11365dc5349..8eed6244b832 100644
--- a/sound/core/pcm.c
+++ b/sound/core/pcm.c
@@ -1032,6 +1032,7 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
 
 	runtime->status->state = SNDRV_PCM_STATE_OPEN;
 	mutex_init(&runtime->buffer_mutex);
+	atomic_set(&runtime->buffer_accessing, 0);
 
 	substream->runtime = runtime;
 	substream->private_data = pcm->private_data;
diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c
index 552c642ee53a..c376471cf760 100644
--- a/sound/core/pcm_lib.c
+++ b/sound/core/pcm_lib.c
@@ -1876,11 +1876,9 @@ static int wait_for_avail(struct snd_pcm_substream *substream,
 		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) {
@@ -2174,7 +2172,6 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
 
 	nonblock = !!(substream->f_flags & O_NONBLOCK);
 
-	mutex_lock(&runtime->buffer_mutex);
 	snd_pcm_stream_lock_irq(substream);
 	err = pcm_accessible_state(runtime);
 	if (err < 0)
@@ -2224,10 +2221,15 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
 			snd_pcm_stream_unlock_irq(substream);
 			return -EINVAL;
 		}
+		if (!atomic_inc_unless_negative(&runtime->buffer_accessing)) {
+			err = -EBUSY;
+			goto _end_unlock;
+		}
 		snd_pcm_stream_unlock_irq(substream);
 		err = writer(substream, appl_ofs, data, offset, frames,
 			     transfer);
 		snd_pcm_stream_lock_irq(substream);
+		atomic_dec(&runtime->buffer_accessing);
 		if (err < 0)
 			goto _end_unlock;
 		err = pcm_accessible_state(runtime);
@@ -2257,7 +2259,6 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
 	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;
 }
 EXPORT_SYMBOL(__snd_pcm_lib_xfer);
diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index f2508f1f13a3..9862b60bfa06 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -666,6 +666,24 @@ static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
 	return 0;
 }
 
+/* acquire buffer_mutex; if it's in r/w operation, return -EBUSY, otherwise
+ * block the further r/w operations
+ */
+static int snd_pcm_buffer_access_lock(struct snd_pcm_runtime *runtime)
+{
+	if (!atomic_dec_unless_positive(&runtime->buffer_accessing))
+		return -EBUSY;
+	mutex_lock(&runtime->buffer_mutex);
+	return 0; /* keep buffer_mutex, unlocked by below */
+}
+
+/* release buffer_mutex and clear r/w access flag */
+static void snd_pcm_buffer_access_unlock(struct snd_pcm_runtime *runtime)
+{
+	mutex_unlock(&runtime->buffer_mutex);
+	atomic_inc(&runtime->buffer_accessing);
+}
+
 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
 #define is_oss_stream(substream)	((substream)->oss.oss)
 #else
@@ -676,14 +694,16 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
 			     struct snd_pcm_hw_params *params)
 {
 	struct snd_pcm_runtime *runtime;
-	int err = 0, usecs;
+	int err, usecs;
 	unsigned int bits;
 	snd_pcm_uframes_t frames;
 
 	if (PCM_RUNTIME_CHECK(substream))
 		return -ENXIO;
 	runtime = substream->runtime;
-	mutex_lock(&runtime->buffer_mutex);
+	err = snd_pcm_buffer_access_lock(runtime);
+	if (err < 0)
+		return err;
 	snd_pcm_stream_lock_irq(substream);
 	switch (runtime->status->state) {
 	case SNDRV_PCM_STATE_OPEN:
@@ -788,7 +808,7 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
 			substream->ops->hw_free(substream);
 	}
  unlock:
-	mutex_unlock(&runtime->buffer_mutex);
+	snd_pcm_buffer_access_unlock(runtime);
 	return err;
 }
 
@@ -821,7 +841,9 @@ static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
 	if (PCM_RUNTIME_CHECK(substream))
 		return -ENXIO;
 	runtime = substream->runtime;
-	mutex_lock(&runtime->buffer_mutex);
+	result = snd_pcm_buffer_access_lock(runtime);
+	if (result < 0)
+		return result;
 	snd_pcm_stream_lock_irq(substream);
 	switch (runtime->status->state) {
 	case SNDRV_PCM_STATE_SETUP:
@@ -841,7 +863,7 @@ static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
 	snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
 	pm_qos_remove_request(&substream->latency_pm_qos_req);
  unlock:
-	mutex_unlock(&runtime->buffer_mutex);
+	snd_pcm_buffer_access_unlock(runtime);
 	return result;
 }
 
@@ -1208,12 +1230,15 @@ static int snd_pcm_action_nonatomic(const struct action_ops *ops,
 	int res;
 
 	down_read(&snd_pcm_link_rwsem);
-	mutex_lock(&substream->runtime->buffer_mutex);
+	res = snd_pcm_buffer_access_lock(substream->runtime);
+	if (res < 0)
+		goto unlock;
 	if (snd_pcm_stream_linked(substream))
 		res = snd_pcm_action_group(ops, substream, state, 0);
 	else
 		res = snd_pcm_action_single(ops, substream, state);
-	mutex_unlock(&substream->runtime->buffer_mutex);
+	snd_pcm_buffer_access_unlock(substream->runtime);
+ unlock:
 	up_read(&snd_pcm_link_rwsem);
 	return res;
 }
-- 
2.36.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 4.19 1/5] ALSA: pcm: Fix races among concurrent hw_params and hw_free calls
  2022-05-13  8:00 [PATCH 4.19 1/5] ALSA: pcm: Fix races among concurrent hw_params and hw_free calls Ovidiu Panait
                   ` (3 preceding siblings ...)
  2022-05-13  8:00 ` [PATCH 4.19 5/5] ALSA: pcm: Fix potential AB/BA lock with buffer_mutex and mmap_lock Ovidiu Panait
@ 2022-05-13  8:31 ` Greg KH
  4 siblings, 0 replies; 6+ messages in thread
From: Greg KH @ 2022-05-13  8:31 UTC (permalink / raw)
  To: Ovidiu Panait; +Cc: stable, Takashi Iwai, Hu Jiahui, Jaroslav Kysela

On Fri, May 13, 2022 at 11:00:14AM +0300, Ovidiu Panait wrote:
> From: Takashi Iwai <tiwai@suse.de>
> 
> commit 92ee3c60ec9fe64404dc035e7c41277d74aa26cb upstream.
> 
> Currently we have neither proper check nor protection against the
> concurrent calls of PCM hw_params and hw_free ioctls, which may result
> in a UAF.  Since the existing PCM stream lock can't be used for
> protecting the whole ioctl operations, we need a new mutex to protect
> those racy calls.
> 
> This patch introduced a new mutex, runtime->buffer_mutex, and applies
> it to both hw_params and hw_free ioctl code paths.  Along with it, the
> both functions are slightly modified (the mmap_count check is moved
> into the state-check block) for code simplicity.
> 
> Reported-by: Hu Jiahui <kirin.say@gmail.com>
> Cc: <stable@vger.kernel.org>
> Reviewed-by: Jaroslav Kysela <perex@perex.cz>
> Link: https://lore.kernel.org/r/20220322170720.3529-2-tiwai@suse.de
> Signed-off-by: Takashi Iwai <tiwai@suse.de>
> [OP: backport to 4.19: adjusted context]
> Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
> ---
>  include/sound/pcm.h     |  1 +
>  sound/core/pcm.c        |  2 ++
>  sound/core/pcm_native.c | 55 +++++++++++++++++++++++++++--------------
>  3 files changed, 39 insertions(+), 19 deletions(-)
> 
> diff --git a/include/sound/pcm.h b/include/sound/pcm.h
> index d6bd3caf6878..0871e16cd04b 100644
> --- a/include/sound/pcm.h
> +++ b/include/sound/pcm.h
> @@ -404,6 +404,7 @@ struct snd_pcm_runtime {
>  	wait_queue_head_t sleep;	/* poll sleep */
>  	wait_queue_head_t tsleep;	/* transfer sleep */
>  	struct fasync_struct *fasync;
> +	struct mutex buffer_mutex;	/* protect for buffer changes */
>  
>  	/* -- private section -- */
>  	void *private_data;
> diff --git a/sound/core/pcm.c b/sound/core/pcm.c
> index b6ed38dec435..a11365dc5349 100644
> --- a/sound/core/pcm.c
> +++ b/sound/core/pcm.c
> @@ -1031,6 +1031,7 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
>  	init_waitqueue_head(&runtime->tsleep);
>  
>  	runtime->status->state = SNDRV_PCM_STATE_OPEN;
> +	mutex_init(&runtime->buffer_mutex);
>  
>  	substream->runtime = runtime;
>  	substream->private_data = pcm->private_data;
> @@ -1062,6 +1063,7 @@ void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
>  	substream->runtime = NULL;
>  	if (substream->timer)
>  		spin_unlock_irq(&substream->timer->lock);
> +	mutex_destroy(&runtime->buffer_mutex);
>  	kfree(runtime);
>  	put_pid(substream->pid);
>  	substream->pid = NULL;
> diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
> index c92eca627840..2b46a2ebefe0 100644
> --- a/sound/core/pcm_native.c
> +++ b/sound/core/pcm_native.c
> @@ -666,33 +666,40 @@ static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
>  	return 0;
>  }
>  
> +#if IS_ENABLED(CONFIG_SND_PCM_OSS)
> +#define is_oss_stream(substream)	((substream)->oss.oss)
> +#else
> +#define is_oss_stream(substream)	false
> +#endif
> +
>  static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
>  			     struct snd_pcm_hw_params *params)
>  {
>  	struct snd_pcm_runtime *runtime;
> -	int err, usecs;
> +	int err = 0, usecs;
>  	unsigned int bits;
>  	snd_pcm_uframes_t frames;
>  
>  	if (PCM_RUNTIME_CHECK(substream))
>  		return -ENXIO;
>  	runtime = substream->runtime;
> +	mutex_lock(&runtime->buffer_mutex);
>  	snd_pcm_stream_lock_irq(substream);
>  	switch (runtime->status->state) {
>  	case SNDRV_PCM_STATE_OPEN:
>  	case SNDRV_PCM_STATE_SETUP:
>  	case SNDRV_PCM_STATE_PREPARED:
> +		if (!is_oss_stream(substream) &&
> +		    atomic_read(&substream->mmap_count))
> +			err = -EBADFD;
>  		break;
>  	default:
> -		snd_pcm_stream_unlock_irq(substream);
> -		return -EBADFD;
> +		err = -EBADFD;
> +		break;
>  	}
>  	snd_pcm_stream_unlock_irq(substream);
> -#if IS_ENABLED(CONFIG_SND_PCM_OSS)
> -	if (!substream->oss.oss)
> -#endif
> -		if (atomic_read(&substream->mmap_count))
> -			return -EBADFD;
> +	if (err)
> +		goto unlock;
>  
>  	params->rmask = ~0U;
>  	err = snd_pcm_hw_refine(substream, params);
> @@ -769,14 +776,19 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
>  	if ((usecs = period_to_usecs(runtime)) >= 0)
>  		pm_qos_add_request(&substream->latency_pm_qos_req,
>  				   PM_QOS_CPU_DMA_LATENCY, usecs);
> -	return 0;
> +	err = 0;
>   _error:
> -	/* hardware might be unusable from this time,
> -	   so we force application to retry to set
> -	   the correct hardware parameter settings */
> -	snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
> -	if (substream->ops->hw_free != NULL)
> -		substream->ops->hw_free(substream);
> +	if (err) {
> +		/* hardware might be unusable from this time,
> +		 * so we force application to retry to set
> +		 * the correct hardware parameter settings
> +		 */
> +		snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
> +		if (substream->ops->hw_free != NULL)
> +			substream->ops->hw_free(substream);
> +	}
> + unlock:
> +	mutex_unlock(&runtime->buffer_mutex);
>  	return err;
>  }
>  
> @@ -809,22 +821,27 @@ static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
>  	if (PCM_RUNTIME_CHECK(substream))
>  		return -ENXIO;
>  	runtime = substream->runtime;
> +	mutex_lock(&runtime->buffer_mutex);
>  	snd_pcm_stream_lock_irq(substream);
>  	switch (runtime->status->state) {
>  	case SNDRV_PCM_STATE_SETUP:
>  	case SNDRV_PCM_STATE_PREPARED:
> +		if (atomic_read(&substream->mmap_count))
> +			result = -EBADFD;
>  		break;
>  	default:
> -		snd_pcm_stream_unlock_irq(substream);
> -		return -EBADFD;
> +		result = -EBADFD;
> +		break;
>  	}
>  	snd_pcm_stream_unlock_irq(substream);
> -	if (atomic_read(&substream->mmap_count))
> -		return -EBADFD;
> +	if (result)
> +		goto unlock;
>  	if (substream->ops->hw_free)
>  		result = substream->ops->hw_free(substream);
>  	snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
>  	pm_qos_remove_request(&substream->latency_pm_qos_req);
> + unlock:
> +	mutex_unlock(&runtime->buffer_mutex);
>  	return result;
>  }
>  
> -- 
> 2.36.0
> 

All now queued up, thanks!

greg k-h

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-05-13  8:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-13  8:00 [PATCH 4.19 1/5] ALSA: pcm: Fix races among concurrent hw_params and hw_free calls Ovidiu Panait
2022-05-13  8:00 ` [PATCH 4.19 2/5] ALSA: pcm: Fix races among concurrent read/write and buffer changes Ovidiu Panait
2022-05-13  8:00 ` [PATCH 4.19 3/5] ALSA: pcm: Fix races among concurrent prepare and hw_params/hw_free calls Ovidiu Panait
2022-05-13  8:00 ` [PATCH 4.19 4/5] ALSA: pcm: Fix races among concurrent prealloc proc writes Ovidiu Panait
2022-05-13  8:00 ` [PATCH 4.19 5/5] ALSA: pcm: Fix potential AB/BA lock with buffer_mutex and mmap_lock Ovidiu Panait
2022-05-13  8:31 ` [PATCH 4.19 1/5] ALSA: pcm: Fix races among concurrent hw_params and hw_free calls Greg KH

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.