Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "YuanZhe Liu" <25031212351@stu.xidian.edu.cn>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH] USB: serial: kobil_sct: fix URB submitted while active
Date: Mon, 30 Nov 2026 10:15:25 +0000	[thread overview]
Message-ID: <sashiko-outbox-240835@kernel.org> (raw)
In-Reply-To: <20261130095951.7095-1-25031212351@stu.xidian.edu.cn>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] usb: serial: kobil_sct: sleeping in non-sleeping tty write
  kobil_write()
- [High] usb: serial: kobil_sct: missing locking in kobil_write() across
  sleeps
- [High] usb: serial: kobil_sct: use-after-free of priv in kobil_write()
  on disconnect
- [Medium] usb: serial: kobil_sct: unchecked urb->status and errors in
  kobil_write()
--

commit 4bb7c45b6bb59969ecfdd3b8714328e15e165214
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

USB: serial: kobil_sct: fix URB submitted while active

This commit adds a completion to wait for each interrupt-out URB transfer
to finish before reusing the URB for the next chunk in kobil_write().

> diff --git a/drivers/usb/serial/kobil_sct.c b/drivers/usb/serial/kobil_sct.c
> index 4a622cf8e6aed..a614a5cc02308 100644
> --- a/drivers/usb/serial/kobil_sct.c
> +++ b/drivers/usb/serial/kobil_sct.c
> @@ -105,6 +105,7 @@ struct kobil_private {
>  	int filled;  /* index of the last char in buf */
>  	int cur_pos; /* index of the next char to send in buf */
>  	__u16 device_type;
> +	struct completion write_done;
>  };
>  
>  static int kobil_ctrl_send(struct usb_serial_port *port, u8 req, u16 val)
> @@ -135,6 +136,7 @@ static int kobil_port_probe(struct usb_serial_port *port)
>  	priv->filled = 0;
>  	priv->cur_pos = 0;
>  	priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
> +	init_completion(&priv->write_done);
>  
>  	switch (priv->device_type) {
>  	case KOBIL_ADAPTER_B_PRODUCT_ID:
> @@ -264,6 +266,10 @@ static void kobil_read_int_callback(struct urb *urb)
>  
>  static void kobil_write_int_callback(struct urb *urb)
>  {
> +	struct usb_serial_port *port = urb->context;
> +	struct kobil_private *priv = usb_get_serial_port_data(port);
> +
> +	complete(&priv->write_done);
>  }

[Severity: Medium]
Should kobil_write_int_callback() or kobil_write() check urb->status
before sending the remaining chunks?

Completing priv->write_done unconditionally means that if a transfer fails
or kobil_close() kills port->interrupt_out_urb (completing it with
-ENOENT), kobil_write() treats the chunk as sent and keeps submitting
port->interrupt_out_urb (and then port->interrupt_in_urb) after close.
Also, when usb_submit_urb() fails or wait_for_completion_timeout() times
out, kobil_write() breaks out of the loop, resets priv->filled = 0, and
returns count as if the whole block succeeded.

> @@ -311,9 +317,23 @@ static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
>  			port->interrupt_out_urb->transfer_buffer_length = length;
>  
>  			priv->cur_pos = priv->cur_pos + length;
> +			reinit_completion(&priv->write_done);
>  			result = usb_submit_urb(port->interrupt_out_urb,
>  					GFP_ATOMIC);
> -			dev_dbg(&port->dev, "%s - Send write URB returns: %i\n", __func__, result);
> +			if (result) {
> +				dev_err(&port->dev,
> +					"%s - failed to send write URB: %d\n",
> +					__func__, result);
> +				break;
> +			}
> +
> +			/* Do not reuse the URB before the transfer has completed. */
> +			if (!wait_for_completion_timeout(&priv->write_done,
> +					msecs_to_jiffies(KOBIL_TIMEOUT))) {
> +				usb_kill_urb(port->interrupt_out_urb);
> +				break;
> +			}
> +
>  			todo = priv->filled - priv->cur_pos;
>  
>  			if (todo > 0)

[Severity: High]
Can kobil_write() sleep in wait_for_completion_timeout() and
usb_kill_urb() inside the tty write callback?

tty_operations->write may not sleep (note the GFP_ATOMIC passed to
usb_submit_urb() right above, added in 191252837626 ("USB: kobil_sct: fix
non-atomic allocation in write path")).  Callers such as gsmld_write()
invoke tty->ops->write() under spin_lock_irqsave(&gsm->tx_lock, flags),
and usb_console_write() calls serial->type->write() from atomic printk
context, so blocking on priv->write_done sleeps in atomic context.
Subsequent chunks should be submitted asynchronously from
kobil_write_int_callback().

[Severity: High]
What prevents a concurrent kobil_write() caller from entering this loop
while the first caller is blocked in wait_for_completion_timeout()?

tty_operations->write can run in parallel, for example when
tty_send_xchar() (taking tty->atomic_write_lock) runs concurrently with
N_TTY commit_echoes() -> __process_echoes() -> tty_put_char() (taking
ldata->output_lock).  Because priv->filled and priv->cur_pos are only
reset after the while (todo > 0) loop finishes, a second caller sees the
complete-block threshold already met and enters the loop while the URB is
in flight:

CPU0                                  CPU1
kobil_write()
  reinit_completion(&priv->write_done)
  usb_submit_urb(interrupt_out_urb)
  wait_for_completion_timeout(...)
                                      kobil_write()
                                        memcpy(interrupt_out_buffer, ...)
                                        priv->cur_pos += length
                                        reinit_completion(&priv->write_done)
                                        usb_submit_urb(interrupt_out_urb)
                                          <-- submitted while active

Because todo is local while priv->cur_pos is shared, concurrent iterations
also advance priv->cur_pos past KOBIL_BUF_LENGTH and read out of bounds
from priv->buf[].

[Severity: High]
Can usb_serial_disconnect() free priv while kobil_write() is still blocked
on priv->write_done?

CPU0                                  CPU1
kobil_write()
  wait_for_completion_timeout(
    &priv->write_done, ...)
                                      usb_serial_disconnect()
                                        usb_serial_port_poison_urbs(port)
                                          kobil_write_int_callback()
                                            complete(&priv->write_done)
                                        device_del(&port->dev)
                                          ...
                                            kobil_port_remove()
                                              kfree(priv)
  /* wakes in do_wait_for_common() */
  spin_lock_irq(&priv->write_done.wait.lock)
  todo = priv->filled - priv->cur_pos <-- use-after-free

An active write holds tty->ldisc_sem, which pins struct usb_serial, but it
does not prevent device_del(&port->dev) from unbinding the port device and
freeing priv in kobil_port_remove() as soon as usb_poison_urb() completes
port->interrupt_out_urb.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130095951.7095-1-25031212351@stu.xidian.edu.cn?part=1

      reply	other threads:[~2026-11-30 10:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  9:59 [PATCH] USB: serial: kobil_sct: fix URB submitted while active YuanZhe Liu
2026-11-30 10:15 ` sashiko-bot [this message]

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-240835@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=25031212351@stu.xidian.edu.cn \
    --cc=linux-usb@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox