* [PATCH 0/2] ALSA: pcm: Yet a couple of more fixes for 7.5
@ 2026-11-25 6:50 Takashi Iwai
2026-11-25 6:50 ` [PATCH 1/2] ALSA: pcm: Cover buffer access gate in all read/write ioctls Takashi Iwai
2026-11-25 6:50 ` [PATCH 2/2] ALSA: pcm: Prevent PCM hw_params & co during mmap on OSS, too Takashi Iwai
0 siblings, 2 replies; 4+ messages in thread
From: Takashi Iwai @ 2026-11-25 6:50 UTC (permalink / raw)
To: linux-sound
This is a couple of PCM fixes for coverage of the unexpected state
changes. As they are rather corner case issues, expected to be
included in 7.5.
Takashi
===
Takashi Iwai (2):
ALSA: pcm: Cover buffer access gate in all read/write ioctls
ALSA: pcm: Prevent PCM hw_params & co during mmap on OSS, too
sound/core/pcm_compat.c | 3 +++
sound/core/pcm_lib.c | 6 ------
sound/core/pcm_native.c | 5 ++++-
3 files changed, 7 insertions(+), 7 deletions(-)
base-commit: c3c132d63840093a3ca4bd09e7434614c1bd07e3
--
2.53.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] ALSA: pcm: Cover buffer access gate in all read/write ioctls
2026-11-25 6:50 [PATCH 0/2] ALSA: pcm: Yet a couple of more fixes for 7.5 Takashi Iwai
@ 2026-11-25 6:50 ` Takashi Iwai
2026-11-25 7:16 ` sashiko-bot
2026-11-25 6:50 ` [PATCH 2/2] ALSA: pcm: Prevent PCM hw_params & co during mmap on OSS, too Takashi Iwai
1 sibling, 1 reply; 4+ messages in thread
From: Takashi Iwai @ 2026-11-25 6:50 UTC (permalink / raw)
To: linux-sound
We haven't applied the buffer access gate in interleaved read/write
because it doesn't involve with the runtime attribute access before
the actual snd_pcm_lib_read/write calls. However, there can be a
slight race during those ioctls between the PCM state check and
read/write calls, which may trigger a kernel WARNING() for the NULL
runtime->dma_area check in pcm_sanity_check().
For addressing it, in this patch, all read/write ioctls are assured to
take the buffer access get via SND_PCM_BUFFER_ACQUIRE() call for
avoiding the unexpected PCM state change during the whole operation.
Meanwhile, this change makes the snd_pcm_buffer_access_get() and
_put() calls in __snd_pcm_lib_xfer() (which is the main body of
pcm_lib_read() and pcm_lib_write()) superfluous, so those are dropped,
too.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
sound/core/pcm_compat.c | 3 +++
sound/core/pcm_lib.c | 6 ------
sound/core/pcm_native.c | 3 +++
3 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/sound/core/pcm_compat.c b/sound/core/pcm_compat.c
index c5931fdfe1f5..924fd269dace 100644
--- a/sound/core/pcm_compat.c
+++ b/sound/core/pcm_compat.c
@@ -293,6 +293,9 @@ static int snd_pcm_ioctl_xferi_compat(struct snd_pcm_substream *substream,
return -ENOTTY;
if (substream->stream != dir)
return -EINVAL;
+ SND_PCM_BUFFER_ACQUIRE(substream->runtime, lock);
+ if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
+ return -EBUSY;
if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_OPEN)
return -EBADFD;
diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c
index 3b66677c037a..50bd5f32d2f8 100644
--- a/sound/core/pcm_lib.c
+++ b/sound/core/pcm_lib.c
@@ -2377,9 +2377,6 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
err = -EINVAL;
goto _end_unlock;
}
- err = snd_pcm_buffer_access_get(runtime);
- if (err < 0)
- goto _end_unlock;
snd_pcm_stream_unlock_irq(substream);
if (!is_playback)
snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU);
@@ -2388,9 +2385,6 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
if (is_playback)
snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
snd_pcm_stream_lock_irq(substream);
- snd_pcm_buffer_access_put(runtime);
- if (err < 0)
- goto _end_unlock;
err = pcm_accessible_state(substream);
if (err < 0)
goto _end_unlock;
diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 897975e43a9b..9e4a82d4ee82 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -3418,6 +3418,9 @@ static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
struct snd_xferi xferi;
snd_pcm_sframes_t result;
+ SND_PCM_BUFFER_ACQUIRE(substream->runtime, lock);
+ if (SND_PCM_BUFFER_ACQUIRE_ERR(lock))
+ return -EBUSY;
if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_OPEN)
return -EBADFD;
if (put_user(0, &_xferi->result))
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/2] ALSA: pcm: Prevent PCM hw_params & co during mmap on OSS, too
2026-11-25 6:50 [PATCH 0/2] ALSA: pcm: Yet a couple of more fixes for 7.5 Takashi Iwai
2026-11-25 6:50 ` [PATCH 1/2] ALSA: pcm: Cover buffer access gate in all read/write ioctls Takashi Iwai
@ 2026-11-25 6:50 ` Takashi Iwai
1 sibling, 0 replies; 4+ messages in thread
From: Takashi Iwai @ 2026-11-25 6:50 UTC (permalink / raw)
To: linux-sound
In the OSS emulation mode, we allowed the PCM hw_params, hw_refine and
hw_free even when the buffer is mmapped as an exception. This is
supposed to be a workaround for some old applications, but this
exception is rather harmful as the mmapped pages can be invalidated by
the concurrent hw_free and hw_params.
As the need of OSS emulation with mmap must be very very low (almost
negligible) now, let's remove the exception and prohibits the PCM
parameter changes during the mmap just like the normal ALSA
applications, too.
Link: https://lore.kernel.org/20261118131945.27592-1-Dmitry.Chumachenko@cyberprotect.ru
Cc: Dmitriy Chumachenko <Dmitry.Chumachenko@cyberprotect.ru>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
sound/core/pcm_native.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 9e4a82d4ee82..01abf09ec017 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -784,7 +784,7 @@ static int hw_params_state_check(struct snd_pcm_substream *substream,
case SNDRV_PCM_STATE_OPEN:
case SNDRV_PCM_STATE_SETUP:
case SNDRV_PCM_STATE_PREPARED:
- if (check_mmap && !is_oss_stream(substream) &&
+ if (check_mmap &&
atomic_read(&substream->mmap_count))
return -EBADFD;
return 0;
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] ALSA: pcm: Cover buffer access gate in all read/write ioctls
2026-11-25 6:50 ` [PATCH 1/2] ALSA: pcm: Cover buffer access gate in all read/write ioctls Takashi Iwai
@ 2026-11-25 7:16 ` sashiko-bot
0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-25 7:16 UTC (permalink / raw)
To: Takashi Iwai, linux-sound
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] ALSA: pcm: missing writer() error check in __snd_pcm_lib_xfer()
- [High] ALSA: pcm: missing buffer_accessing gate in
__snd_pcm_lib_xfer() callers
--
commit a0ca0563533dec8be3f120c69e7d751e0b7fb94c
Author: Takashi Iwai <tiwai@suse.de>
ALSA: pcm: Cover buffer access gate in all read/write ioctls
This commit acquires the PCM buffer access gate in the native and compat
interleaved read/write ioctls and removes the inner
snd_pcm_buffer_access_get() and snd_pcm_buffer_access_put() calls from
__snd_pcm_lib_xfer().
> diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c
> index 3b66677c037a6..50bd5f32d2f8d 100644
> --- a/sound/core/pcm_lib.c
> +++ b/sound/core/pcm_lib.c
> @@ -2377,9 +2377,6 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
> err = -EINVAL;
> goto _end_unlock;
> }
> - err = snd_pcm_buffer_access_get(runtime);
> - if (err < 0)
> - goto _end_unlock;
> snd_pcm_stream_unlock_irq(substream);
> if (!is_playback)
> snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU);
> @@ -2388,9 +2385,6 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
> if (is_playback)
> snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
> snd_pcm_stream_lock_irq(substream);
> - snd_pcm_buffer_access_put(runtime);
> - if (err < 0)
> - goto _end_unlock;
> err = pcm_accessible_state(substream);
> if (err < 0)
> goto _end_unlock;
[Severity: High]
Does removing "if (err < 0) goto _end_unlock;" here drop the error check
for writer()? Because err is immediately overwritten by
pcm_accessible_state(substream), a failure from writer() (such as -EFAULT
during user copy) is lost whenever the stream remains in an accessible
state, causing __snd_pcm_lib_xfer() to advance appl_ptr and report a
successful transfer.
[Severity: High]
Also, can dropping snd_pcm_buffer_access_get() and
snd_pcm_buffer_access_put() from __snd_pcm_lib_xfer() leave callers
outside pcm_native.c and pcm_compat.c with runtime->buffer_accessing at 0
while the stream lock is dropped?
Callers such as snd_pcm_oss_write3(), snd_pcm_oss_read3(),
snd_pcm_oss_writev3(), snd_pcm_oss_readv3(), and snd_pcm_oss_sync() in
sound/core/oss/pcm_oss.c, as well as u_audio_playback() ->
snd_pcm_kernel_write() in drivers/usb/gadget/function/u_uac1_legacy.c,
enter __snd_pcm_lib_xfer() without SND_PCM_BUFFER_ACQUIRE():
CPU0 CPU1
u_audio_playback()
snd_pcm_kernel_write()
__snd_pcm_lib_xfer()
snd_pcm_stream_unlock_irq()
snd_pcm_hw_free()
snd_pcm_buffer_access_lock()
snd_pcm_lib_free_pages()
writer()
... <-- use-after-free on runtime->dma_area
For OSS streams, a concurrent SNDCTL_DSP_RESET followed by prepare on the
same descriptor can similarly bypass snd_pcm_buffer_access_lock() while
writer() and snd_pcm_dma_buffer_sync() run unlocked.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125065034.1470697-1-tiwai@suse.de?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-25 7:16 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 6:50 [PATCH 0/2] ALSA: pcm: Yet a couple of more fixes for 7.5 Takashi Iwai
2026-11-25 6:50 ` [PATCH 1/2] ALSA: pcm: Cover buffer access gate in all read/write ioctls Takashi Iwai
2026-11-25 7:16 ` sashiko-bot
2026-11-25 6:50 ` [PATCH 2/2] ALSA: pcm: Prevent PCM hw_params & co during mmap on OSS, too Takashi Iwai
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox