* [PATCH 0/3] ALSA: pcm: Fix OSS mmap buffer-lifetime issues
@ 2026-12-02 14:01 Ahmed Abdelmoemen
2026-12-02 14:01 ` [PATCH 1/3] ALSA: pcm: Remove is_oss_stream() bypass of mmap_count check Ahmed Abdelmoemen
` (2 more replies)
0 siblings, 3 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
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
sound/core/pcm_native.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [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:21 ` sashiko-bot
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, 2 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
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
2026-12-02 14:21 ` sashiko-bot
1 sibling, 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* 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
@ 2026-12-02 14:21 ` sashiko-bot
1 sibling, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-02 14:21 UTC (permalink / raw)
To: Ahmed Abdelmoemen; +Cc: linux-sound
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] ALSA: pcm: removing is_oss_stream() in hw_params_state_check()
wedges OSS mmap
- [Low] ALSA: pcm: Fixes trailer in commit message cites refactoring
commit 92ee3c60ec9f
--
commit 421134d72db5ebc0315596afb99aa7522ec2fe0d
Author: Ahmed Abdelmoemen <ahmedabdelmoumen05@gmail.com>
> ALSA: pcm: Remove is_oss_stream() bypass of mmap_count check
>
> 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>
[Severity: Low]
Does the Fixes: tag point to the commit that introduced this bypass?
92ee3c60ec9f ("ALSA: pcm: Fix races among concurrent hw_params and hw_free
calls") only wrapped the existing !substream->oss.oss check in
is_oss_stream() and moved it inside the stream lock, whereas the check
itself dates back to 1da177e4c3f4 ("Linux-2.6.12-rc2").
> diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
> index 897975e43a9bd..d77045aba17b6 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:
[Severity: High]
Does removing !is_oss_stream(substream) here break OSS parameter
reconfiguration after mmap() and wedge the OSS substream?
The OSS emulation layer supports changing parameters while /dev/dsp is
mapped: snd_pcm_oss_period_size() clamps oss_buffer_size to
runtime->oss.mmap_bytes, and snd_pcm_oss_change_params_locked() forces
direct mode and SNDRV_PCM_ACCESS_MMAP_INTERLEAVED when substream->mmap_count
is non-zero before invoking SNDRV_PCM_IOCTL_HW_PARAMS:
sound/core/oss/pcm_oss.c:snd_pcm_oss_change_params_locked() {
...
if (atomic_read(&substream->mmap_count))
direct = 1;
else
direct = substream->oss.setup.direct;
_snd_pcm_hw_params_any(sparams);
_snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
_snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
snd_mask_none(&mask);
if (atomic_read(&substream->mmap_count))
snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
...
snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams);
if (err < 0) {
pcm_dbg(substream->pcm, "HW_PARAMS failed: %i\n", err);
goto failure;
}
...
failure:
if (err) {
runtime->oss.params = 1;
snd_pcm_oss_release_buffers(substream);
}
...
}
Because the OSS parameter setters (such as snd_pcm_oss_set_rate() or
snd_pcm_oss_set_fragment1()) update runtime->oss and set
runtime->oss.params = 1 without checking mmap_count, rejecting
SNDRV_PCM_IOCTL_HW_PARAMS with -EBADFD sends
snd_pcm_oss_change_params_locked() to the failure path after dropping the
stream. That frees the OSS buffers and leaves runtime->oss.params set to 1
while mmap_count > 0, so every subsequent OSS ioctl that calls
snd_pcm_oss_make_ready() or snd_pcm_oss_get_active_substream() re-enters
snd_pcm_oss_change_params_locked() and fails with -EBADFD.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202140117.1164633-1-ahmedabdelmoumen05@gmail.com?part=1
^ 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:16 ` sashiko-bot
2026-12-02 14:01 ` [PATCH 3/3] ALSA: pcm: Clear mmap tail padding to avoid info leak Ahmed Abdelmoemen
2 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
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* Re: [PATCH 2/3] ALSA: pcm: Check mmap_count before freeing buffer in error path
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:16 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-02 14:16 UTC (permalink / raw)
To: Ahmed Abdelmoemen; +Cc: linux-sound
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ALSA: pcm: DMA buffer leak and incomplete mmap guard in
snd_pcm_hw_params()
--
commit 5fc3b38b4810c777d9255a21426c3dfa8b6fc8a1
Author: Ahmed Abdelmoemen <ahmedabdelmoumen05@gmail.com>
ALSA: pcm: Check mmap_count before freeing buffer in error path
This commit checks substream->mmap_count before calling
snd_pcm_lib_free_pages() in the error path of snd_pcm_hw_params().
> diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
> index d77045aba17b6..f8d9d145bc352 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);
> }
[Severity: Medium]
Does skipping snd_pcm_lib_free_pages() here leak runtime->dma_buffer_p and
its backing DMA pages?
The error path resets runtime->state to SNDRV_PCM_STATE_OPEN before
checking mmap_count, and snd_pcm_release_substream() only calls
do_hw_free() when the state is not SNDRV_PCM_STATE_OPEN:
sound/core/pcm_native.c:snd_pcm_release_substream() {
...
if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
do_hw_free(substream);
...
}
When the substream is later closed without another successful
snd_pcm_hw_params() call, do_hw_free() is skipped and
snd_pcm_detach_substream() frees runtime without releasing the dynamically
allocated buffer.
Also, if snd_pcm_hw_params() is ever entered with mmap_count > 0, guarding
only the error path does not prevent snd_pcm_lib_malloc_pages() from
freeing an existing smaller mapped buffer before reallocating, or prevent
substream->ops->hw_free() from running in _error.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202140117.1164633-1-ahmedabdelmoumen05@gmail.com?part=2
^ permalink raw reply [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
2 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
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
end of thread, other threads:[~2026-12-02 14:21 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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:20 ` Takashi Iwai
2026-12-02 14:21 ` sashiko-bot
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:16 ` sashiko-bot
2026-12-02 14:01 ` [PATCH 3/3] ALSA: pcm: Clear mmap tail padding to avoid info leak Ahmed Abdelmoemen
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.