[PATCH 2/3] ALSA: pcm: Check mmap_count before freeing buffer in error path
From: Ahmed Abdelmoemen
Date: Wed Dec 02 2026 - 09:01:40 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Ahmed Abdelmoemen <ahmedabdelmoumen05@xxxxxxxxx>
---
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