All of lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: Hillf Danton <hdanton@sina.com>
Cc: alsa-devel@alsa-project.org,
	Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 2/9] ALSA: usb-audio: Fix possible race at sync of urb completions
Date: Tue, 22 Apr 2025 09:03:20 +0200	[thread overview]
Message-ID: <875xiwu18n.wl-tiwai@suse.de> (raw)
In-Reply-To: <20250421233900.4221-1-hdanton@sina.com>

On Tue, 22 Apr 2025 01:38:59 +0200,
Hillf Danton wrote:
> 
> On Mon, 21 Apr 2025 16:36:30 +0200 Takashi Iwai wrote:
> > On Mon, 21 Apr 2025 12:43:42 +0200, Hillf Danton wrote:
> > > I misread "Which reads and writes are you trying to solve?" though I
> > > showed the read/write, but it is a bad case particulay with UAF.
> > > 
> > > Could you tell us what will happen if the race is not fixed? Could ep
> > > be freed with in-flight urbs for example?
> > 
> > Before the patch, wait_clear_urbs() might return earlier than actually
> > all pending eps are finished, so it can be UAF.
> > 
> Got it.
> 
> > > Is it still race if the wait loop in wait_clear_urbs() ends before the
> > > urb complete callbace completes, given the last sentence in your commit
> > > message? If nope, igore my noise please.
> > 
> > Well, your concern about the missing barrier -- that would
> > wait_clear_urbs() missing the refcount decrement, hence it would be
> > rather to make the return delayed.  So it shouldn't lead to further
> > UAF, but at most it might lead to an unnecessary delay.
> > 
> > That said, I'm willing to take a fix even for a theoretical issue if
> > it clarifies what it really fixes.  But scratching a random surface
> > isn't what we want.
> > 
> Thank you for shedding light on the race, given
> a) the mb in 26fbe9772b8c ("USB: core: Fix hang in usb_kill_urb by adding memory barriers")
> b) the urb complete callback is invoked in giveback, see __usb_hcd_giveback_urb()
> 
> use the urb routines instead to close the race.

I'm afraid that it can break things as of this form; the stopped
stream might be restarted without reinitializing URBs.  That is, this
isn't called only from disconnect or close, but also just for stopping
the stream in the middle, too.


Takashi

> 
> Note the one second timeout is also cut.
> 
> 	Hillf
> 
> --- x/sound/usb/endpoint.c
> +++ y/sound/usb/endpoint.c
> @@ -996,24 +996,14 @@ void snd_usb_endpoint_suspend(struct snd
>   */
>  static int wait_clear_urbs(struct snd_usb_endpoint *ep)
>  {
> -	unsigned long end_time = jiffies + msecs_to_jiffies(1000);
> -	int alive;
> -
>  	if (atomic_read(&ep->state) != EP_STATE_STOPPING)
>  		return 0;
>  
> -	do {
> -		alive = atomic_read(&ep->submitted_urbs);
> -		if (!alive)
> -			break;
> -
> -		schedule_timeout_uninterruptible(1);
> -	} while (time_before(jiffies, end_time));
> +	for (int i = 0; i < ep->nurbs; i++) {
> +		struct snd_urb_ctx *u = &ep->urb[i];
>  
> -	if (alive)
> -		usb_audio_err(ep->chip,
> -			"timeout: still %d active urbs on EP #%x\n",
> -			alive, ep->ep_num);
> +		usb_kill_urb(u->urb);
> +	}
>  
>  	if (ep_state_update(ep, EP_STATE_STOPPING, EP_STATE_STOPPED)) {
>  		ep->sync_sink = NULL;

  parent reply	other threads:[~2025-04-22  7:03 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-29  8:08 [PATCH 0/9] ALSA: usb-audio: More fixes / improvements on PCM Takashi Iwai
2021-09-29  8:08 ` [PATCH 1/9] ALSA: usb-audio: Restrict rates for the shared clocks Takashi Iwai
2021-09-29  8:08 ` [PATCH 2/9] ALSA: usb-audio: Fix possible race at sync of urb completions Takashi Iwai
     [not found]   ` <20250418103533.4078-1-hdanton@sina.com>
2025-04-18 11:08     ` Takashi Iwai
     [not found]       ` <20250418144518.4097-1-hdanton@sina.com>
2025-04-19  6:50         ` Takashi Iwai
     [not found]           ` <20250419080410.4148-1-hdanton@sina.com>
2025-04-20  7:49             ` Takashi Iwai
     [not found]               ` <20250421051832.4179-1-hdanton@sina.com>
2025-04-21  7:23                 ` Takashi Iwai
     [not found]                   ` <20250421104343.4197-1-hdanton@sina.com>
2025-04-21 14:36                     ` Takashi Iwai
     [not found]                       ` <20250421233900.4221-1-hdanton@sina.com>
2025-04-22  7:03                         ` Takashi Iwai [this message]
     [not found]                           ` <20250422102933.4239-1-hdanton@sina.com>
2025-04-22 11:03                             ` Takashi Iwai
2021-09-29  8:08 ` [PATCH 3/9] ALSA: usb-audio: Rename early_playback_start flag with lowlatency_playback Takashi Iwai
2021-09-29  8:08 ` [PATCH 4/9] ALSA: usb-audio: Disable low-latency playback for free-wheel mode Takashi Iwai
2021-09-29  8:08 ` [PATCH 5/9] ALSA: usb-audio: Disable low-latency mode for implicit feedback sync Takashi Iwai
2021-09-29  8:08 ` [PATCH 6/9] ALSA: usb-audio: Check available frames for the next packet size Takashi Iwai
2021-09-29  8:08 ` [PATCH 7/9] ALSA: usb-audio: Add spinlock to stop_urbs() Takashi Iwai
2021-09-29  8:08 ` [PATCH 8/9] ALSA: usb-audio: Improved lowlatency playback support Takashi Iwai
2021-09-29  8:08 ` [PATCH 9/9] ALSA: usb-audio: Avoid killing in-flight URBs during draining Takashi Iwai

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=875xiwu18n.wl-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=alsa-devel@alsa-project.org \
    --cc=hdanton@sina.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mikhail.v.gavrilov@gmail.com \
    /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.