Skip to contentSkip to conversation

ALSA: pcm: Don't release the DMA buffer while it is mmapped

PATCH New bo cao 1 participant 1 message Nov 3 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

bo cao

author
#1 lore raw

PATCH 1/1 ALSA: pcm: Don't release the DMA buffer while it is mmapped

From: Bo Cao <[email protected]>

The hw_params state check refuses a re-configuration while the data buffer is mmapped, but it carries an exemption for OSS emulation streams. The OSS flag is kept on the shared substream, and a native ALSA fd attaching to an already-open substream through an O_APPEND open inherits that flag, so the exemption can be reached without ever touching the OSS emulation layer itself.

An unprivileged user can exploit this combination by opening /dev/dsp to mark the substream as OSS, attaching to the same substream with a native O_APPEND open, running hw_params with a small buffer size and mmapping it, and then re-running hw_params: either with a larger size (making snd_pcm_lib_malloc_pages() release the old DMA buffer pages) or with parameters that fail refinement (making the hw_params error path release them). In both cases the pages are freed while the userspace PTEs remain valid and writable.

The stale mapping of the freed page never expires and can be turned into arbitrary physical memory read/write (e.g. by reclaiming the page as a page table). On arm64 this was demonstrated to escalate an audio-group user to full root via a modprobe_path overwrite.

Refuse the buffer release while the buffer is still mmapped:

- snd_pcm_lib_malloc_pages() returns -EBADFD instead of freeing the old buffer when the new size does not fit it and the buffer is still mmapped (same-size re-configuration, which is what the OSS emulation layer itself does while mmapped, keeps working),

- snd_pcm_lib_free_pages() refuses to release a buffer that is still mmapped, which also covers the hw_params error path and drivers freeing through it,

- a buffer retained this way by a failed hw_params is released at the final close of the substream instead of leaking.

Fixes 0df63e44c3e3 ("[ALSA] Add O_APPEND flag support to PCM")

Hi Johan,

thanks for the quick response. Below is a proposed fix as discussed. It enforces a single invariant at the release sites -- the DMA buffer is never released while it is still mmapped -- which covers both the buffer-growth path my PoC uses and the hw_params error path that frees the buffer on failed refinement while the OSS exemption lets the re-configuration through. The close path releases a buffer that was retained by a failed hw_params, so the fix does not trade the UAF for a leak.

One caveat: I am not a professional kernel developer -- the analysis and PoCs come from an independent security research effort -- so please treat the patch as a starting point rather than a finished fix. In particular I would appreciate review of the snd_pcm_release_substream() hunk (needed so that a buffer retained by a failed hw_params does not leak) and of any OSS emulation behavior I may have missed. I am happy to iterate on a v2 with whatever approach the maintainers prefer.

I verified on the affected setup (7.4-rc1, QEMU arm64, snd-intel8x0) that the PoC now fails cleanly: the second hw_params returns -EBADFD and the mmapped pages are not freed.

Best regards,
Bo Cao

---
 sound/core/pcm_memory.c | 8 ++++++++
 sound/core/pcm_native.c | 6 ++++++
 2 files changed, 14 insertions(+)
sound/core/pcm_memory.c +8 -0
index cfe9f10e7359..ed3da74e2cf5 100644
@@ -439,6 +439,11 @@ int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)
 			runtime->dma_bytes = size;
 			return 0;	/* ok, do not change */
 		}
+		/* the old buffer may still be mmapped: refuse instead of
+		 * freeing it behind the userspace mapping
+		 */
+		if (atomic_read(&substream->mmap_count))
+			return -EBADFD;
 		snd_pcm_lib_free_pages(substream);
 	}
 	if (substream->dma_buffer.area != NULL &&
@@ -488,6 +493,9 @@ int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
 	runtime = substream->runtime;
 	if (runtime->dma_area == NULL)
 		return 0;
+	/* the buffer may still be mmapped: refuse to release it */
+	if (atomic_read(&substream->mmap_count))
+		return -EBADFD;
 	if (runtime->dma_buffer_p != &substream->dma_buffer) {
 		struct snd_card *card = substream->pcm->card;
 
sound/core/pcm_native.c +6 -0
index 5cbf88da7626..8e337983f2ec 100644
@@ -2851,6 +2851,12 @@ void snd_pcm_release_substream(struct snd_pcm_substream *substream)
 		snd_refcount_sync(&substream->runtime->refcnt);
 		if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
 			do_hw_free(substream);
+		else if (substream->managed_buffer_alloc)
+			/* a failed hw_params may have left the buffer
+			 * allocated (it was still mmapped at the time);
+			 * release it now that the mapping is gone
+			 */
+			snd_pcm_lib_free_pages(substream);
 		substream->ops->close(substream);
 		substream->hw_opened = 0;
 	}
Signature · 1 line
2.39.5