* [PATCH 1/3] ALSA: pcm: Remove is_oss_stream() bypass of mmap_count check
2026-12-02 14:01 [PATCH 0/3] ALSA: pcm: Fix OSS mmap buffer-lifetime issues Ahmed Abdelmoemen
@ 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
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ 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] 7+ 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; 7+ 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] 7+ messages in thread
* [PATCH 2/3] ALSA: pcm: Check mmap_count before freeing buffer in error path
2026-12-02 14:01 [PATCH 0/3] ALSA: pcm: Fix OSS mmap buffer-lifetime issues Ahmed Abdelmoemen
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
2026-12-02 15:47 ` [PATCH 0/3] ALSA: pcm: Fix OSS mmap buffer-lifetime issues Takashi Iwai
3 siblings, 0 replies; 7+ 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] 7+ messages in thread* [PATCH 3/3] ALSA: pcm: Clear mmap tail padding to avoid info leak
2026-12-02 14:01 [PATCH 0/3] ALSA: pcm: Fix OSS mmap buffer-lifetime issues Ahmed Abdelmoemen
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
2026-12-02 15:31 ` Takashi Iwai
2026-12-02 15:47 ` [PATCH 0/3] ALSA: pcm: Fix OSS mmap buffer-lifetime issues Takashi Iwai
3 siblings, 1 reply; 7+ 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] 7+ messages in thread* Re: [PATCH 3/3] ALSA: pcm: Clear mmap tail padding to avoid info leak
2026-12-02 14:01 ` [PATCH 3/3] ALSA: pcm: Clear mmap tail padding to avoid info leak Ahmed Abdelmoemen
@ 2026-12-02 15:31 ` Takashi Iwai
0 siblings, 0 replies; 7+ messages in thread
From: Takashi Iwai @ 2026-12-02 15:31 UTC (permalink / raw)
To: Ahmed Abdelmoemen; +Cc: tiwai, perex, linux-sound, linux-kernel, stable
On Wed, 02 Dec 2026 15:01:17 +0100,
Ahmed Abdelmoemen wrote:
>
> 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.
Hmm, how does it happen?
Takashi
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 0/3] ALSA: pcm: Fix OSS mmap buffer-lifetime issues
2026-12-02 14:01 [PATCH 0/3] ALSA: pcm: Fix OSS mmap buffer-lifetime issues Ahmed Abdelmoemen
` (2 preceding siblings ...)
2026-12-02 14:01 ` [PATCH 3/3] ALSA: pcm: Clear mmap tail padding to avoid info leak Ahmed Abdelmoemen
@ 2026-12-02 15:47 ` Takashi Iwai
3 siblings, 0 replies; 7+ messages in thread
From: Takashi Iwai @ 2026-12-02 15:47 UTC (permalink / raw)
To: Ahmed Abdelmoemen; +Cc: tiwai, perex, linux-sound, linux-kernel
On Wed, 02 Dec 2026 15:01:14 +0100,
Ahmed Abdelmoemen wrote:
>
> These three patches fix memory-safety issues in the ALSA PCM OSS
> compatibility path (sound/core/pcm_native.c, CONFIG_SND_PCM_OSS=y).
> I reported them earlier; as Greg noted that went to a public list, so
> here are the patches.
>
> Common root cause: the DMA buffer is mapped to userspace with
> remap_pfn_range() (VM_PFNMAP), which carries no page refcount. The
> kernel therefore cannot revoke the mapping when the buffer is cleared,
> reallocated, or freed. Each patch removes one way to reach that state.
>
> Patch 1 - hw_params_state_check() skips the mmap_count guard for OSS
> streams via is_oss_stream(). That lets hw_params reconfigure (clear or
> reallocate) the buffer while userspace still holds a live mapping.
> Removing the exception makes the guard apply to all stream types.
>
> Patch 2 - the hw_params error path frees a managed buffer without
> checking mmap_count. With preallocate_dma=0 the pages go back to the
> allocator while a VM_PFNMAP mapping may still reference them - a
> use-after-free. The free is guarded directly, so it holds even
> independent of patch 1.
>
> Patch 3 - snd_pcm_mmap_data() allows mapping PAGE_ALIGN(dma_bytes); when
> the logical buffer is sub-page (e.g. 512 bytes via OSS SETFRAGMENT) the
> page tail can be exposed with stale contents. The tail is now zeroed
> before the mapping is established.
>
> Reproduction: on an unprivileged /dev/dsp with preallocate_dma=0, a
> mapping that outlives the buffer free (patches 1+2) can be used to read
> and write pages after they are reallocated to other kernel objects. PoC
> and a QEMU harness (x86-64, intel8x0/AC97, kernel 6.8.12):
>
> https://github.com/bohmiiidd/linux-UAF
>
> The code paths are present in current mainline. Built and checkpatch-
> clean against 7.4.0-rc with CONFIG_SND_PCM_OSS=m.
>
> Feedback welcome - happy to respin.
>
> Ahmed Abdelmoemen (3):
> ALSA: pcm: Remove is_oss_stream() bypass of mmap_count check
> ALSA: pcm: Check mmap_count before freeing buffer in error path
> ALSA: pcm: Clear mmap tail padding to avoid info leak
I had the same fix for the first patch (disallowing OSS mmap), and
cooked a more fix series without submission. Since this series seems
still requiring more fixes, I'll revisit mine and try submitting.
thanks,
Takashi
^ permalink raw reply [flat|nested] 7+ messages in thread