From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796033727; b=umCS1/pL1rpPqbWSiAZaNkIlFCNSyHTLmY+wdJM1qA/K0dk0HUXFBHwlSmNxBR7J6Bmq qTzcW0ptT0bFCFs0K/hMMEIIRxSNejk9mmJss7yGODyqUzQM6OOmq6dRXNV4wB45MYhQo 6VmFMjCLJvbZ7LsxL/DrDlS/TLIjDDmNjmYMxGW+d5e1Pw/rDMVhPkVPz5R+1xcbB9jpA 3tvY4xABiBeDhLuQ/jNsY8vI8psuW1udJXOt5ZW9UGwXT9vR7EzGU4WY0kR2XnuY6oAa0 8IJ9dR0kbiZs+gdbND8Q8dUincP9/tH3IbL9l+6DdK3mQWEeadbxPC34cOsk7l7+P7g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796033727; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=podDcgyf5QLrhlz/zCKHvTEUR8VwRhcO4rhlAxKhVnM=; b=hLBpPF9XagZeUkuCdXZ4Wbr1r0Ng1dHQA16Ffga81uKMXVolDOkWEoG3BQdGB8sgB4uD x6ZlR3V+11g4Jm5Wn5h8okUgS0Te8M5D99vL6VG0ylS8gZ6Ph/SXEj0QXb2ytDT8ZH9B9 44BXCOlCl9/DTvRyMA9pR7TktNIXPJiEUUWOV/6b9xU2IGx3n07s6QUdt/8vtM1x4kaMr h/9WW4rWsR79kZ9m+M5pCMHW9F2QrMuMr+/FhZd1sju1eyxwBsk/o2HZjcF4WnmcIMq0F q5aG4FesbGfvxj422wWRSl9Sxuf6THMdhxP1QJhNhlsK+AgIbOr/gjNm3VFJ5ZpS7JA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=PLo6styM; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5BA74606A320 for ; Mon, 30 Nov 2026 10:15:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=PLo6styM Received: by smtp.kernel.org (Postfix) with ESMTPSA id 47A791F000C1; Mon, 30 Nov 2026 10:15:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796033726; bh=podDcgyf5QLrhlz/zCKHvTEUR8VwRhcO4rhlAxKhVnM=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=PLo6styMNWSlYRhffBaMrXATVUwkFPET53ohK5sp5QoxbjyoDNZpfjVYUqbuLbEzm YeEPq3EPUVfCBspyZW3cVzCDr1XH7S03dA73fVgoF5RMnMNtzbHfzbrdWMXowEjSJB m3cQOozvFFSROPc1V5sz5qZSzqY8DiSmAI7X/WoLMElFFXTLROuiCRpZSwKIIMbMxM WpU7CDnbKNdvZ1m5B7sKSYRCnUoHP1D0bmc4oDYYIYMVZIIxof7nh9XW/P/XWL5lNo B4/U/xE39rPLcvD5uj8a7SjlZjbFx3rM2/w9u6hbFgwJQ8JcG7xzrET4yFaa6ULFYo 0yE6gdOmwOq5w== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] USB: serial: kobil_sct: fix URB submitted while active Reply-To: sashiko-reviews@lists.linux.dev To: "YuanZhe Liu" <25031212351@stu.xidian.edu.cn> Cc: linux-usb@vger.kernel.org In-Reply-To: <20261130095951.7095-1-25031212351@stu.xidian.edu.cn> References: <20261130095951.7095-1-25031212351@stu.xidian.edu.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Mon, 30 Nov 2026 10:15:25 +0000 X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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_sc= t.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; > }; > =20 > 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 *p= ort) > priv->filled =3D 0; > priv->cur_pos =3D 0; > priv->device_type =3D le16_to_cpu(serial->dev->descriptor.idProduct); > + init_completion(&priv->write_done); > =20 > switch (priv->device_type) { > case KOBIL_ADAPTER_B_PRODUCT_ID: > @@ -264,6 +266,10 @@ static void kobil_read_int_callback(struct urb *urb) > =20 > static void kobil_write_int_callback(struct urb *urb) > { > + struct usb_serial_port *port =3D urb->context; > + struct kobil_private *priv =3D 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 =3D 0, and returns count as if the whole block succeeded. > @@ -311,9 +317,23 @@ static int kobil_write(struct tty_struct *tty, struc= t usb_serial_port *port, > port->interrupt_out_urb->transfer_buffer_length =3D length; > =20 > priv->cur_pos =3D priv->cur_pos + length; > + reinit_completion(&priv->write_done); > result =3D usb_submit_urb(port->interrupt_out_urb, > GFP_ATOMIC); > - dev_dbg(&port->dev, "%s - Send write URB returns: %i\n", __func__, re= sult); > + 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 =3D priv->filled - priv->cur_pos; > =20 > 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 +=3D 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 =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261130095951.7095= -1-25031212351@stu.xidian.edu.cn?part=3D1