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=1796037874; b=lXa5R6l3YKTl6geg6Il9ISdA+bAHFMAFnsI2kz6sZHak8YDtsiO4kdUX8QaVH5uQvtPJ VZ8gq54IxyYeXwlLS9OvrU6Vi+F0E2wpp3Jt0jOvkAD0WPEJxVBPeSPi4asdGkZmgmuEn tkAAIt9nW/GK3NKOzDXTl8QscsWnn7+YmEh06g24ZNDRYEG/LE2KDtQvYqoW6KVC/6Hm5 bWpbQBzxVXZ/SgpUOEWQukIdsu8z4ri9upnGlCZrWHINdBW3XrzFLvxElMLF9PPkmsT54 MT9jalvTYdmpq5T2TEccTOTcJGN5NhwxidDR7t7vfKWFmSyhOiM+kOPcA5sg0Fz5CZg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796037874; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=IrtfKdZfT3GB1Z9rughShGi0BIcajbXHVidZ0DiSdgk=; b=stfteB5soIk4qC90sP113pI5pghKW+pWHzzrSRC7t6zvR771qlpmq8zTPj9pQHHnKgpC oLuNkOw9XlDS6QMDUGCI076WCG0Su3TQlWap1ZbdbwKLVnMsKfOHdQdrkx8/KNwdnSVQi 0J2XcTgdWzPeAg2EVopym2NuWlATEJx7ksl/6hUmA3NblNRZfQbYO+qM1idrYxate64vd xotsBVo74x9G4Pnd0mJzvY9wNjlOk4//m4nGFMTp7JHgG4fNIBjiI+lq0MKgqC9kJSjA8 Jbtt2sQt2zbXozPIuVCvWiyGZqYLbCuANgOvhoSIF65UucQT0Z/geA/sujIEApqql6Q== 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=TBKzWARl; 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 C23DA606A31C for ; Mon, 30 Nov 2026 11:24:34 +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=TBKzWARl Received: by smtp.kernel.org (Postfix) with ESMTPSA id 08CAA1F0092B; Mon, 30 Nov 2026 11:24:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796037874; bh=IrtfKdZfT3GB1Z9rughShGi0BIcajbXHVidZ0DiSdgk=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=TBKzWARlYk0AF7ZCszsaP5AQtdAYl6jV3pA20ztUNsVceNcJXsDnXu508n1kbuREk jSjELs8T6+6Xxcx+mjiYYdxfYjW8iZuhj9YhWUKIDbaAE+3l7eiS4zY9rXwDghup/v j5OQQ3qHK6gKmovsvqMBr3GadkYdxlg2jcZHIq7MoCg+5esC5riqNwTUdEKmoZ2xhw GcOrwLXbngg1NVrqXNw0nsUzD1PGP1REkrfF6OEt3HaaH0PFvCWHxTvUgbME72AZYq Kn5mFrArnt/81yyiUyktKZ+EenusiCZTdO1pNei4Mz1IbamjVRB9Q59mM75wTGNsu8 cqPcZDFHukPJA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v2] 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: <20261130105735.26286-1-25031212351@stu.xidian.edu.cn> References: <20261130105735.26286-1-25031212351@stu.xidian.edu.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Mon, 30 Nov 2026 11:24:33 +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 on disconnect in kobil_write() - [Medium] usb: serial: kobil_sct: wrong error check in kobil_write() -- commit 89c1739659514782646132d4f7382206a453d236 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 chunk to finish in kobil_write() before submitting the next chunk or restarting the interrupt-in URB. > diff --git a/drivers/usb/serial/kobil_sct.c b/drivers/usb/serial/kobil_sc= t.c > index 4a622cf8e6aed..59fa951e34b8e 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; > }; [ ... ] > @@ -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); > } > =20 > =20 > @@ -301,6 +307,7 @@ static int kobil_write(struct tty_struct *tty, struct= usb_serial_port *port, > } > =20 > todo =3D priv->filled - priv->cur_pos; > + result =3D 0; > =20 > while (todo > 0) { > /* max 8 byte in one urb (endpoint size) */ > @@ -311,9 +318,39 @@ 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); [Severity: High] Can concurrent kobil_write() callers race while one thread sleeps in wait_for_completion_timeout() or msleep()? In N_TTY, tty_send_xchar() (via TCXONC ioctl) invokes tty->ops->write() under tty->atomic_write_lock without taking ldata->output_lock, while echo output in commit_echoes() -> __process_echoes() -> tty_put_char() invokes tty->ops->write() under ldata->output_lock without taking tty->atomic_write_lock: CPU0 CPU1 kobil_write() priv->filled +=3D count while (todo > 0) priv->cur_pos +=3D length reinit_completion(&priv->write_done) usb_submit_urb(interrupt_out_urb) wait_for_completion_timeout() kobil_write() priv->filled +=3D count while (todo > 0) memcpy(interrupt_out_buffer, priv->buf + priv->cur_pos, ...) priv->cur_pos +=3D length reinit_completion(&priv->write_done) usb_submit_urb(interrupt_out_urb) <-- URB submitted while active Because priv->filled and priv->cur_pos are not reset until the loop ends and no lock serializes access to priv or port->interrupt_out_urb, a second caller sees the complete-block threshold already met and enters the send loop. It overwrites port->interrupt_out_buffer, reinitializes priv->write_done, resubmits port->interrupt_out_urb while it is still active, and advances priv->cur_pos past priv->filled and KOBIL_BUF_LENGTH, causing an out-of-bounds read from priv->buf[]. > - 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); > + result =3D -ETIMEDOUT; > + break; > + } [Severity: High] Can waiting for URB completion here sleep in atomic context? struct tty_operations documents that write() must not sleep, and commit 191252837626 ("USB: kobil_sct: fix non-atomic allocation in write path") switched usb_submit_urb() in kobil_write() to GFP_ATOMIC because tty write paths can be called from atomic context (for example, line disciplines such as gsmld_write() calling tty->ops->write() under spin_lock_irqsave(), or usb_console_write()). Previously, single-chunk writes on USBTWIN and KAAN SIM returned without sleeping, whereas kobil_write() now blocks in wait_for_completion_timeout() and usb_kill_urb() on every chunk and still calls msleep(24) after the URB has completed. Should subsequent chunks be submitted asynchronously from kobil_write_int_callback() instead? > + > + /* > + * The port may have been disconnected while waiting, in > + * which case the private data may already have been freed. > + */ > + if (port->serial->disconnected) > + return count; [Severity: High] Can kobil_write() still access priv after kobil_port_remove() has freed it on disconnect? When a caller invokes tty->ops->write() without holding tty->ldisc_sem (such as spk_ttyio_out() or serport_serio_write(), or a line-discipline write work item after tty_port_close() clears port->port.tty), tty_port_tty_vhangup() does not block usb_serial_disconnect(): 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 up in __wait_for_common() */ spin_lock_irq(&priv->write_done.wait.lock) <-- use-after-free Because write_done is embedded in struct kobil_private, wait_for_completion_timeout() touches priv->write_done after complete() allows disconnect to free priv, before reaching the port->serial->disconnected check. In addition, if disconnect happens while wait_for_completion_timeout() times out or during msleep(24), execution either breaks past the port->serial->disconnected check or loops back and dereferences priv->buf, priv->filled, priv->cur_pos, and priv->device_type after priv has been freed. > + > + result =3D port->interrupt_out_urb->status; > + if (result) { > + dev_dbg(&port->dev, > + "%s - write URB status: %d\n", > + __func__, result); > + break; > + } > + > todo =3D priv->filled - priv->cur_pos; > =20 > if (todo > 0) > @@ -324,8 +361,9 @@ static int kobil_write(struct tty_struct *tty, struct= usb_serial_port *port, > priv->cur_pos =3D 0; > =20 > /* start reading (except TWIN and KAAN SIM) */ > - if (priv->device_type =3D=3D KOBIL_ADAPTER_B_PRODUCT_ID || > - priv->device_type =3D=3D KOBIL_ADAPTER_K_PRODUCT_ID) { > + if (result !=3D -ENOENT && > + (priv->device_type =3D=3D KOBIL_ADAPTER_B_PRODUCT_ID || > + priv->device_type =3D=3D KOBIL_ADAPTER_K_PRODUCT_ID)) { > result =3D usb_submit_urb(port->interrupt_in_urb, > GFP_ATOMIC); > dev_dbg(&port->dev, "%s - Send read URB returns: %i\n", __func__, res= ult); [Severity: Medium] Should this check !result instead of result !=3D -ENOENT before restarting port->interrupt_in_urb? When usb_submit_urb() fails, wait_for_completion_timeout() sets result =3D -ETIMEDOUT, or port->interrupt_out_urb->status reports a transfer error other than -ENOENT (such as -EPROTO, -EPIPE, or -ECONNRESET), result !=3D -ENOENT is still true. kobil_write() then submits port->interrupt_in_urb even though sending the block failed, and if kobil_close() ran while kobil_write() was in msleep(24) or timed out, this restarts the self-resubmitting interrupt_in_urb on a closed port. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261130105735.2628= 6-1-25031212351@stu.xidian.edu.cn?part=3D1