All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ahmed Abdelmoemen" <ahmedabdelmoumen05@gmail.com>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH 2/3] ALSA: pcm: Check mmap_count before freeing buffer in error path
Date: Wed, 02 Dec 2026 14:16:48 +0000	[thread overview]
Message-ID: <sashiko-outbox-245931@kernel.org> (raw)
In-Reply-To: <20261202140117.1164633-3-ahmedabdelmoumen05@gmail.com>

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

  reply	other threads:[~2026-12-02 14:16 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-12-02 14:01 ` [PATCH 3/3] ALSA: pcm: Clear mmap tail padding to avoid info leak Ahmed Abdelmoemen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-245931@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=ahmedabdelmoumen05@gmail.com \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.