Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH 1/3] ALSA: pcm: Remove is_oss_stream() bypass of mmap_count check
       [not found] <20261202140117.1164633-1-ahmedabdelmoumen05@gmail.com>
@ 2026-12-02 14:01 ` Ahmed Abdelmoemen
  2026-12-02 14:20   ` Takashi Iwai
  2026-12-02 14:01 ` [PATCH 2/3] ALSA: pcm: Check mmap_count before freeing buffer in error path Ahmed Abdelmoemen
  2026-12-02 14:01 ` [PATCH 3/3] ALSA: pcm: Clear mmap tail padding to avoid info leak Ahmed Abdelmoemen
  2 siblings, 1 reply; 4+ messages in thread
From: Ahmed Abdelmoemen @ 2026-12-02 14:01 UTC (permalink / raw)
  To: tiwai, perex; +Cc: linux-sound, linux-kernel, Ahmed Abdelmoemen, stable

hw_params_state_check() skips the mmap_count guard for OSS streams via
the is_oss_stream() condition. This lets hw_params run while userspace
holds a live VM_PFNMAP mapping obtained through /dev/dsp.

remap_pfn_range() mappings (VM_PFNMAP) carry no page reference counts,
so the kernel cannot revoke them when hw_params reconfigures or
reallocates the DMA buffer. The buffer can be cleared or freed while
userspace retains read/write access to it.

Removing the bypass restores the intended safety invariant: an OSS
caller holding a mapping gets -EBADFD on reconfiguration, the same as a
native PCM stream. Reconfiguring the buffer under an active mapping is
never safe regardless of stream type.

Fixes: 92ee3c60ec9f ("ALSA: pcm: Fix races among concurrent hw_params and hw_free calls")
Cc: stable@vger.kernel.org
Signed-off-by: Ahmed Abdelmoemen <ahmedabdelmoumen05@gmail.com>
---
 sound/core/pcm_native.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 897975e43..d77045aba 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -784,8 +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) &&
-		    atomic_read(&substream->mmap_count))
+		if (check_mmap && atomic_read(&substream->mmap_count))
 			return -EBADFD;
 		return 0;
 	default:
-- 
2.43.0


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

* [PATCH 2/3] ALSA: pcm: Check mmap_count before freeing buffer in error path
       [not found] <20261202140117.1164633-1-ahmedabdelmoumen05@gmail.com>
  2026-12-02 14:01 ` [PATCH 1/3] ALSA: pcm: Remove is_oss_stream() bypass of mmap_count check Ahmed Abdelmoemen
@ 2026-12-02 14:01 ` Ahmed Abdelmoemen
  2026-12-02 14:01 ` [PATCH 3/3] ALSA: pcm: Clear mmap tail padding to avoid info leak Ahmed Abdelmoemen
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmed Abdelmoemen @ 2026-12-02 14:01 UTC (permalink / raw)
  To: tiwai, perex; +Cc: linux-sound, linux-kernel, Ahmed Abdelmoemen, stable

The error cleanup in snd_pcm_hw_params() frees a managed buffer with
snd_pcm_lib_free_pages() without checking mmap_count. With
preallocate_dma=0 the pages are returned to the page allocator while a
VM_PFNMAP mapping may still reference them (no refcount), yielding a
use-after-free: userspace retains access to pages that can be
reallocated to security-sensitive objects such as struct cred.

The previous patch closes the primary route here (the is_oss_stream
bypass), but the free should not depend on an upstream guard remaining
in place. Guard it directly: if a mapping is still held, skip the free.
The buffer is freed on the normal teardown path (release -> do_hw_free)
once the mapping is gone and mmap_count has dropped to zero, so no leak
results.

Fixes: 0dba808eae26 ("ALSA: pcm: Introduce managed buffer allocation mode")
Cc: stable@vger.kernel.org
Signed-off-by: Ahmed Abdelmoemen <ahmedabdelmoumen05@gmail.com>
---
 sound/core/pcm_native.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index d77045aba..f8d9d145b 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -900,7 +900,8 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
 		snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
 		if (substream->ops->hw_free != NULL)
 			substream->ops->hw_free(substream);
-		if (substream->managed_buffer_alloc)
+		if (substream->managed_buffer_alloc &&
+		    !atomic_read(&substream->mmap_count))
 			snd_pcm_lib_free_pages(substream);
 	}
  unlock:
-- 
2.43.0


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

* [PATCH 3/3] ALSA: pcm: Clear mmap tail padding to avoid info leak
       [not found] <20261202140117.1164633-1-ahmedabdelmoumen05@gmail.com>
  2026-12-02 14:01 ` [PATCH 1/3] ALSA: pcm: Remove is_oss_stream() bypass of mmap_count check Ahmed Abdelmoemen
  2026-12-02 14:01 ` [PATCH 2/3] ALSA: pcm: Check mmap_count before freeing buffer in error path Ahmed Abdelmoemen
@ 2026-12-02 14:01 ` Ahmed Abdelmoemen
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmed Abdelmoemen @ 2026-12-02 14:01 UTC (permalink / raw)
  To: tiwai, perex; +Cc: linux-sound, linux-kernel, Ahmed Abdelmoemen, stable

snd_pcm_mmap_data() validates the request against
PAGE_ALIGN(runtime->dma_bytes), so when the logical buffer is smaller
than a page (e.g. 512 bytes via OSS SETFRAGMENT) userspace may map the
whole page. The tail padding is backed by the same page and may hold
data from prior use of the buffer. hw_params zeroes
PAGE_ALIGN(dma_bytes) for MMAP-capable streams, but a stream mapped
through the OSS path can be exposed without that memset having run.

Zero the padding in the mmap path, immediately before the mapping is
established, so bytes beyond the logical buffer are always zero.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Ahmed Abdelmoemen <ahmedabdelmoumen05@gmail.com>
---
 sound/core/pcm_native.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index f8d9d145b..605e596db 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -4136,6 +4136,12 @@ int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
 		return -EINVAL;
 	if (offset > dma_bytes - size)
 		return -EINVAL;
+	/* Zero the page tail beyond the logical buffer so the mapping
+	 * cannot expose stale contents (DMA buffers are page-granular).
+	 */
+	if (runtime->dma_area && runtime->dma_bytes < dma_bytes)
+		memset(runtime->dma_area + runtime->dma_bytes, 0,
+		       dma_bytes - runtime->dma_bytes);
 
 	area->vm_ops = &snd_pcm_vm_ops_data;
 	area->vm_private_data = substream;
-- 
2.43.0


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

* Re: [PATCH 1/3] ALSA: pcm: Remove is_oss_stream() bypass of mmap_count check
  2026-12-02 14:01 ` [PATCH 1/3] ALSA: pcm: Remove is_oss_stream() bypass of mmap_count check Ahmed Abdelmoemen
@ 2026-12-02 14:20   ` Takashi Iwai
  0 siblings, 0 replies; 4+ messages in thread
From: Takashi Iwai @ 2026-12-02 14:20 UTC (permalink / raw)
  To: Ahmed Abdelmoemen; +Cc: tiwai, perex, linux-sound, linux-kernel, stable

On Wed, 02 Dec 2026 15:01:15 +0100,
Ahmed Abdelmoemen wrote:
> 
> hw_params_state_check() skips the mmap_count guard for OSS streams via
> the is_oss_stream() condition. This lets hw_params run while userspace
> holds a live VM_PFNMAP mapping obtained through /dev/dsp.
> 
> remap_pfn_range() mappings (VM_PFNMAP) carry no page reference counts,
> so the kernel cannot revoke them when hw_params reconfigures or
> reallocates the DMA buffer. The buffer can be cleared or freed while
> userspace retains read/write access to it.
> 
> Removing the bypass restores the intended safety invariant: an OSS
> caller holding a mapping gets -EBADFD on reconfiguration, the same as a
> native PCM stream. Reconfiguring the buffer under an active mapping is
> never safe regardless of stream type.
> 
> Fixes: 92ee3c60ec9f ("ALSA: pcm: Fix races among concurrent hw_params and hw_free calls")
> Cc: stable@vger.kernel.org
> Signed-off-by: Ahmed Abdelmoemen <ahmedabdelmoumen05@gmail.com>

The same fix is already on sound.git tree for-next branch.


thanks,

Takashi

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

end of thread, other threads:[~2026-12-02 14:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261202140117.1164633-1-ahmedabdelmoumen05@gmail.com>
2026-12-02 14:01 ` [PATCH 1/3] ALSA: pcm: Remove is_oss_stream() bypass of mmap_count check Ahmed Abdelmoemen
2026-12-02 14:20   ` Takashi Iwai
2026-12-02 14:01 ` [PATCH 2/3] ALSA: pcm: Check mmap_count before freeing buffer in error path Ahmed Abdelmoemen
2026-12-02 14:01 ` [PATCH 3/3] ALSA: pcm: Clear mmap tail padding to avoid info leak Ahmed Abdelmoemen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox