Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: 徐晗翔 <xuhx@hust.edu.cn>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH] Re: ALSA: pcm: link-group walk in timer callback can use a closed substream's freed runtime (UAF)
Date: Wed, 25 Nov 2026 13:12:59 +0000	[thread overview]
Message-ID: <sashiko-outbox-233469@kernel.org> (raw)
In-Reply-To: <49f1dede.596.1a212c69040.Coremail.xuhx@hust.edu.cn>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Critical] ALSA: asihpi: unlocked group list walk in
  snd_card_asihpi_timer_function()
- [Critical] ALSA: asihpi: deferred elapsed/xrun calls race with
  snd_pcm_detach_substream()
- [Critical] ALSA: asihpi: unsynchronized timer stop and missing runtime
  checks on close
- [High] ALSA: asihpi: shared timer_walked race in
  snd_card_asihpi_timer_function()
- [High] ALSA: asihpi: spin_lock_irqsave held across DSP polling and
  MMIO transfers
- [High] ALSA: pcm: missing Signed-off-by trailer in commit message
- [Low] ALSA: pcm: missing Fixes trailer in commit message
--

commit 7d06715d1a9473c30893b41b4784ed5585d479ae
Author: 徐晗翔 <xuhx@hust.edu.cn>

> ALSA: pcm: link-group walk in timer callback can use a closed substream's freed runtime (UAF)
>
> snd_pcm_group_for_each_entry() is a plain list_for_each_entry() over
> substream->group->substreams.  A driver timer callback (asihpi's
> snd_card_asihpi_timer_function()) walked that list from softirq with
> no group reference and no link rwsem, while the close of another
> linked substream could unlink the members, reassign the survivors
> and kfree() the group object -- the callback kept dereferencing a
> stale s/runtime pointer and freed group memory (KASAN UAF).
>
> Core: free the group object only after an RCU grace period (new
> rcu_head in struct snd_pcm_group, kfree_rcu() at both free sites),
> so a group pointer read without the link rwsem stays memory-safe
> while a concurrent unlink tears the group down.
>
> Driver: walk the group one member at a time under the group lock
> with rcu_read_lock() held -- the lock makes membership (and each
> member's runtime) stable while the member is processed, and RCU
> keeps the group object alive.  snd_pcm_stop_xrun()/
> snd_pcm_period_elapsed() are deferred out of the locked section:
> they enter snd_pcm_action(), which re-takes the group lock.  A
> substream object outlives its fd's close and both helpers recheck
> substream->runtime under the stream lock, so the deferred calls are
> safe against a concurrent close of the member.

[Severity: High]
The commit message is missing a Signed-off-by: trailer.

[Severity: Low]
Since this fixes a bug in the asihpi timer callback, should it include a
Fixes: tag?

  Fixes: 719f82d3987a ("ALSA: Add support of AudioScience ASI boards")

> diff --git a/sound/pci/asihpi/asihpi.c b/sound/pci/asihpi/asihpi.c
> index b78e96caef9d0..768f2969004fb 100644
> --- a/sound/pci/asihpi/asihpi.c
> +++ b/sound/pci/asihpi/asihpi.c
> @@ -136,6 +136,8 @@ struct snd_card_asihpi_pcm {
>  	unsigned int pcm_buf_dma_ofs;	/* DMA R/W offset in buffer */
>  	unsigned int pcm_buf_elapsed_dma_ofs;	/* DMA R/W offset in buffer */
>  	unsigned int drained_count;
> +	bool timer_walked;		/* one-shot mark for the timer
> +					 * callback's group walk */
>  	struct snd_pcm_substream *substream;
>  	u32 h_stream;
>  	struct hpi_format format;
> @@ -705,6 +707,31 @@ static inline unsigned int modulo_min(unsigned int a, unsigned int b,
>  	return result;
>  }
>  
> +/* Pick the next group member this timer callback has not processed yet.
> + * Must be called with the group lock held and under rcu_read_lock(): the
> + * group list cannot change under us, so every member's runtime is still
> + * alive. */
> +static struct snd_pcm_substream *
> +asihpi_timer_next_member(struct snd_pcm_substream *substream,
> +			 struct snd_card_asihpi *card)
> +{
> +	struct snd_pcm_substream *s;
> +
> +	snd_pcm_group_for_each_entry(s, substream) {
> +		struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
> +
> +		if (snd_pcm_substream_chip(s) != card)
> +			continue;
> +		/* don't link Cap and Play */
> +		if (substream->stream != s->stream)
> +			continue;
> +		if (ds->timer_walked)
> +			continue;
> +		return s;
> +	}
> +	return NULL;
> +}
> +
>  /** Timer function, equivalent to interrupt service routine for cards
>  */
>  static void snd_card_asihpi_timer_function(struct timer_list *t)
> @@ -713,10 +740,12 @@ static void snd_card_asihpi_timer_function(struct timer_list *t)
>  	struct snd_pcm_substream *substream = dpcm->substream;
>  	struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
>  	struct snd_pcm_runtime *runtime;
> +	struct snd_pcm_group *group;
>  	struct snd_pcm_substream *s;
>  	unsigned int newdata = 0;
>  	unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
>  	unsigned int remdata, xfercount, next_jiffies;
> +	unsigned long flags;
>  	int first = 1;
>  	u16 state;
>  	u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
> @@ -725,17 +754,58 @@ static void snd_card_asihpi_timer_function(struct timer_list *t)
>  
>  	snd_pcm_debug_name(substream, name, sizeof(name));
>  
> -	/* find minimum newdata and buffer pos in group */
> +	/*
> +	 * Walk the PCM link group one member at a time, under the group
> +	 * lock and an RCU read lock.  This callback runs from a timer
> +	 * (softirq) with no group reference and no link rwsem, so a close
> +	 * of another linked substream could previously unlink the members,
> +	 * free the group object and free that member's runtime while this
> +	 * walk was holding a stale s/runtime pointer.
> +	 *
> +	 * The group lock keeps the membership (and each member's runtime)
> +	 * stable while the member is processed; the RCU read lock keeps
> +	 * the group object itself alive (snd_pcm_unlink() frees it only
> +	 * after a grace period).  snd_pcm_stop_xrun() and
> +	 * snd_pcm_period_elapsed() must run WITHOUT the group lock -- they
> +	 * enter snd_pcm_action(), which re-takes the group lock -- so they
> +	 * are deferred to after the unlocked section.  A substream object
> +	 * outlives the close of its fd, and both helpers recheck
> +	 * substream->runtime under the stream lock.
> +	 */
> +
> +	/* clear the one-shot walk marks of the previous invocation */
> +	rcu_read_lock();
> +	group = READ_ONCE(substream->group);	/* kept alive by RCU */
> +	spin_lock_irqsave(&group->lock, flags);
>  	snd_pcm_group_for_each_entry(s, substream) {
> -		struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
> -		runtime = s->runtime;
> +		if (snd_pcm_substream_chip(s) == card) {
> +			struct snd_card_asihpi_pcm *ds =
> +				s->runtime->private_data;
>  
> -		if (snd_pcm_substream_chip(s) != card)
> -			continue;
> +			ds->timer_walked = false;
> +		}
> +	}
> +	spin_unlock_irqrestore(&group->lock, flags);
> +	rcu_read_unlock();
>  
> -		/* don't link Cap and Play */
> -		if (substream->stream != s->stream)
> -			continue;
> +	/* find minimum newdata and buffer pos in group */
> +	for (;;) {
> +		struct snd_card_asihpi_pcm *ds;
> +		struct snd_pcm_substream *xrun = NULL;
> +
> +		rcu_read_lock();
> +		group = READ_ONCE(substream->group);	/* kept alive by RCU */
> +		spin_lock_irqsave(&group->lock, flags);
> +
> +		s = asihpi_timer_next_member(substream, card);
> +		if (!s) {
> +			spin_unlock_irqrestore(&group->lock, flags);
> +			rcu_read_unlock();
> +			break;
> +		}
> +		ds = s->runtime->private_data;
> +		runtime = s->runtime;
> +		ds->timer_walked = true;

[Severity: Critical]
Can snd_card_asihpi_timer_function() lock a stale group and then walk
substream->group->substreams without holding the current group's lock?

All four lock sites read group = READ_ONCE(substream->group) and acquire
group->lock without rechecking that substream->group == group (the way
snd_pcm_stream_group_ref() does), while snd_pcm_group_for_each_entry()
expands to list_for_each_entry(s, &substream->group->substreams, link_list)
and re-reads substream->group:

CPU0 (<softirq>)                  CPU1
snd_card_asihpi_timer_function()
  group = READ_ONCE(substream->group)
    /* group == G1 */
                                  snd_pcm_unlink(peer)
                                    spin_lock_irq(&G1->lock)
                                    relink_to_local(substream)
                                      substream->group = &substream->self_group
                                    spin_unlock_irq(&G1->lock)
                                  snd_pcm_link(other, substream)
                                    /* moves substream to G2 */
  spin_lock_irqsave(&G1->lock, ...)
  asihpi_timer_next_member()
    snd_pcm_group_for_each_entry()
      /* walks G2->substreams under G1->lock */
                                  close(member_in_G2)
                                    snd_pcm_unlink(member_in_G2)
                                    snd_pcm_detach_substream(member_in_G2)
                                      kfree(runtime)
      s->runtime->private_data    <-- use-after-free

In addition, snd_pcm_link() calls snd_pcm_group_assign(substream, group)
under substream->self_group.lock before acquiring target_group->lock, so
reading substream->group without substream->self_group.lock can also observe
the new group while snd_pcm_group_assign() is still mutating its substreams
list.  If substream->group changes mid-loop, the list_entry_is_head() check
in snd_pcm_group_for_each_entry() also compares against the new list head
and walks past the old list head.

[Severity: High]
Can two timers running in the same link group corrupt each other's walk
state through ds->timer_walked and use on_card_bytes uninitialized?

Multiple members of the same group can have active timers at once: for
instance, when a playback and a capture substream on the same card are
linked (snd_card_asihpi_trigger() starts a timer per direction, whereas the
two mark-clearing loops clear ds->timer_walked for all group members on the
card regardless of stream direction), or when two already-running streams
are linked via SNDRV_PCM_IOCTL_LINK.  Because group->lock is dropped after
clearing ds->timer_walked and after each member:

CPU0 (timer A)                    CPU1 (timer B)
snd_card_asihpi_timer_function()
  /* clear timer_walked = false */
  spin_unlock_irqrestore(&group->lock)
                                  snd_card_asihpi_timer_function()
                                    /* Pass 1 marks all members
                                     * ds->timer_walked = true */
  spin_lock_irqsave(&group->lock)
  asihpi_timer_next_member()
    /* returns NULL on 1st iter */
  break;
  if (xfercount &&
      on_card_bytes > ...)        <-- uninitialized on_card_bytes

When Pass 1 exits on its first iteration like this, on_card_bytes has never
been written by hpi_stream_get_info_ex() and Pass 2 overwrites each member's
ds->pcm_buf_dma_ofs with min_buf_pos (0).  Conversely, if timer B runs its
mark-clearing loop while timer A is partway through Pass 2, timer A visits
already-processed members a second time, performing duplicate
hpi_outstream_write_buf() / hpi_instream_read_buf() transfers and advancing
pcm_buf_host_rw_ofs and pcm_buf_elapsed_dma_ofs twice in one pass.  Also,
because Pass 2 leaves ds->timer_walked set to true, a stream linked into the
group after the first mark-clearing loop is skipped in Pass 1.

[ ... ]

> @@ -799,6 +870,13 @@ static void snd_card_asihpi_timer_function(struct timer_list *t)
>  			(unsigned long)frames_to_bytes(runtime,
>  						runtime->control->appl_ptr)
>  		);
> +
> +member_done:
> +		spin_unlock_irqrestore(&group->lock, flags);
> +		rcu_read_unlock();
> +
> +		if (xrun)
> +			snd_pcm_stop_xrun(xrun);
>  	}
>  	pcm_buf_dma_ofs = min_buf_pos;

[ ... ]

> @@ -887,8 +994,15 @@ static void snd_card_asihpi_timer_function(struct timer_list *t)
>  			/* ? host_rw_ofs always ahead of elapsed_dma_ofs by preload size? */
>  			ds->pcm_buf_host_rw_ofs += xfercount;
>  			ds->pcm_buf_elapsed_dma_ofs += xfercount;
> -			snd_pcm_period_elapsed(s);
> +			/* defer to after the unlock */
> +			elapsed = s;
>  		}
> +
> +		spin_unlock_irqrestore(&group->lock, flags);
> +		rcu_read_unlock();
> +
> +		if (elapsed)
> +			snd_pcm_period_elapsed(elapsed);
>  	}
>  
>  	if (!card->hpi->interrupt_mode && dpcm->respawn_timer)

[Severity: Critical]
Does deferring snd_pcm_stop_xrun() and snd_pcm_period_elapsed() outside
group->lock race with snd_pcm_detach_substream() on a closing member?

The comment notes that both helpers recheck substream->runtime under the
stream lock, but snd_pcm_detach_substream() does not acquire
pcm_stream_lock:

sound/core/pcm.c:snd_pcm_detach_substream() {
    ...
	if (substream->timer) {
		scoped_guard(spinlock_irq, &substream->timer->lock)
			substream->runtime = NULL;
	} else {
		substream->runtime = NULL;
	}
	mutex_destroy(&runtime->buffer_mutex);
	snd_fasync_free(runtime->fasync);
	kfree(runtime);
    ...
}

Once group->lock is dropped, a concurrent close() on elapsed (or xrun) can
finish snd_pcm_unlink() and enter snd_pcm_detach_substream():

CPU0 (<softirq>)                  CPU1
snd_card_asihpi_timer_function()
  elapsed = s;
  spin_unlock_irqrestore(&group->lock)
                                  snd_pcm_release_substream(s)
                                    snd_pcm_unlink(s)
                                    ...
                                    snd_pcm_detach_substream(s)
  snd_pcm_period_elapsed(elapsed)
    guard(pcm_stream_lock_irqsave)(s)
    snd_pcm_period_elapsed_under_stream_lock(s)
      if (PCM_RUNTIME_CHECK(s))
      runtime = s->runtime;
                                      s->runtime = NULL;
                                      snd_fasync_free(runtime->fasync);
                                      kfree(runtime);
      if (!snd_pcm_running(s) || ...)
        /* dereferences s->runtime->state */
      snd_kill_fasync(runtime->fasync, ...)  <-- use-after-free

Even if snd_pcm_detach_substream() finishes setting elapsed->runtime = NULL
before snd_pcm_period_elapsed() takes the stream lock,
PCM_RUNTIME_CHECK(substream) triggers snd_BUG_ON(!substream->runtime) when
CONFIG_SND_DEBUG is enabled.

[Severity: Critical]
What happens if the timer-owning substream itself is closed, or a member
runs hw_free, while snd_card_asihpi_timer_function() is between its locked
sections?

A substream stays on its own substream->self_group.substreams list even
after unlink and close, and snd_card_asihpi_pcm_timer_stop() only calls
timer_delete(&dpcm->timer) (not timer_delete_sync(), with no .sync_stop
callback):

CPU0 (<softirq>)                  CPU1
snd_card_asihpi_timer_function()
  /* finishes Pass 1, unlocks */
                                  snd_pcm_release_substream(substream)
                                    snd_pcm_unlink(substream)
                                      /* moves to self_group.substreams */
                                    snd_pcm_drop(substream)
                                      timer_delete(&dpcm->timer)
                                    do_hw_free(substream)
                                    snd_pcm_detach_substream(substream)
                                      /* frees dpcm, sets runtime = NULL */
  spin_lock_irqsave(&group->lock)
  snd_pcm_group_for_each_entry()
    s->runtime->private_data      <-- NULL pointer dereference
  ...
  if (... && dpcm->respawn_timer)
    add_timer(&dpcm->timer)       <-- use-after-free on dpcm

Similarly, in Pass 2, a member stopped by the deferred snd_pcm_stop_xrun()
at the end of Pass 1 (or by a concurrent SNDRV_PCM_IOCTL_DROP) can undergo
SNDRV_PCM_IOCTL_HW_FREE before Pass 2 acquires group->lock; do_hw_free()
clears s->runtime->dma_area = NULL without unlinking s, and Pass 2 does not
check snd_pcm_running(s) or s->runtime->dma_area before reading
&s->runtime->dma_area[buf_ofs] and passing it to hpi_outstream_write_buf()
or hpi_instream_read_buf().

[Severity: High]
Does holding spin_lock_irqsave(&group->lock, flags) across the bodies of
Pass 1 and Pass 2 disable local hardware interrupts during DSP polling and
multi-kilobyte PCI MMIO transfers?

Previously snd_card_asihpi_timer_function() ran with hardirqs enabled (from
TIMER_SOFTIRQ or the threaded ISR asihpi_isr_thread), and
hpios_dsplock_lock() used spin_lock_bh().  With spin_lock_irqsave() held
around each member, hpi_stream_get_info_ex(), hpi_stream_start(),
hpi_outstream_write_buf(), and hpi_instream_read_buf() now execute with local
IRQs disabled while busy-waiting in wait_dsp_ack() (udelay(4) polling loops
in sound/pci/asihpi/hpi6205.c) and transferring period buffers over PCI MMIO
in sound/pci/asihpi/hpi6000.c.  In addition, rescanning from the list head in
asihpi_timer_next_member() on every iteration makes both passes O(N^2) with
2N + 4 lock acquisitions per callback.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/49f1dede.596.1a212c69040.Coremail.xuhx@hust.edu.cn?part=1

  reply	other threads:[~2026-11-25 13:13 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <4c7f9772.192e1.1a17d5c2ed5.Coremail.xuhx@hust.edu.cn>
     [not found] ` <2026102727-data-sequel-ce41@gregkh>
2026-11-13 11:58   ` ALSA: ctxfi: free atimer only after the timer IRQ is quiesced 徐晗翔
2026-11-13 16:30     ` Takashi Iwai
2026-11-25 12:51       ` [PATCH] Re: " 徐晗翔
2026-11-25 13:00         ` sashiko-bot
2026-11-13 12:03   ` ALSA: ctxfi: do not free a running system timer when timer_delete_sync_try() returns -1 (UAF) 徐晗翔
2026-11-25 12:51     ` [PATCH] " 徐晗翔
2026-11-25 13:05       ` sashiko-bot
2026-11-13 12:04   ` ALSA: asihpi: free IRQ before snd_card_free()/HPI delete on HPI6205 removal (UAF) 徐晗翔
2026-11-25 12:50     ` [PATCH] " 徐晗翔
2026-11-25 13:03       ` sashiko-bot
2026-11-13 12:06   ` ALSA: asihpi: quiesce PCM timer and IRQ before freeing dpcm on close (UAF) 徐晗翔
2026-11-25 12:50     ` [PATCH] " 徐晗翔
2026-11-25 13:06       ` sashiko-bot
2026-11-13 12:15   ` ALSA: pcm: link-group walk in timer callback can use a closed substream's freed runtime (UAF) 徐晗翔
2026-11-25 12:55     ` [PATCH] " 徐晗翔
2026-11-25 13:12       ` sashiko-bot [this message]
2026-11-13 12:28   ` ALSA: usb-audio: us144mkii: PCM close frees runtime while playback/feedback URB completion and capture work still use it (UAF) 徐晗翔
2026-11-25 12:48     ` [PATCH] " 徐晗翔
2026-11-25 13:13       ` sashiko-bot
2026-11-13 12:29   ` ALSA: usb-audio: us144mkii: queued stop_pcm_work calls snd_pcm_stop() after PCM close and dereferences NULL runtime 徐晗翔
2026-11-25 12:49     ` [PATCH] " 徐晗翔
2026-11-25 13:03       ` sashiko-bot
2026-11-22  7:43   ` sound: core: compress offload fd does not pin card or module, so card removal leaves a live fd pointing at freed snd_compr (KASAN UAF) 徐晗翔
2026-11-22  8:07     ` Takashi Iwai
2026-11-25 12:44       ` [PATCH] Re: " 徐晗翔
2026-11-22  7:48   ` sound: core: jack kcontrol is published before jack_kctl allocation, and the failure path frees it without removing it from card->controls (null-ptr-deref) 徐晗翔
2026-11-24  9:39     ` Sreeraj S Kurup
2026-11-25 12:43       ` 徐晗翔
2026-11-22  7:50   ` sound: core: rawmidi input close can re-queue event_work after cancel_work_sync() (UAF) 徐晗翔
2026-11-24 14:52     ` Krzysztof Kozlowski
2026-11-25 12:45       ` [PATCH] Re: " 徐晗翔
2026-11-22  7:52   ` sound: core: hwdep DSP_LOAD check-then-act is unlocked, allowing concurrent DSP_LOAD callbacks (UAF) 徐晗翔
2026-11-25 12:46     ` [PATCH] " 徐晗翔
2026-11-22  7:55   ` sound: core: get_available_index() calls snd_ctl_find_id() without controls_rwsem (UAF risk) 徐晗翔
2026-11-25 12:46     ` [PATCH] " 徐晗翔
2026-11-25 12:51       ` sashiko-bot
2026-11-22  7:56   ` sound: core: OSS mixer notify callback is invoked without module pin or mutex, racing with rmmod 徐晗翔
2026-11-25 12:47     ` [PATCH] " 徐晗翔
2026-11-22  7:57   ` sound: timer: snd_utimer_fops lacks .owner, so an open user timer fd does not pin snd-timer 徐晗翔
2026-11-25 12:48     ` [PATCH] " 徐晗翔

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-233469@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=xuhx@hust.edu.cn \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox