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=1796406666; b=AxYio5da9+Rp/QGnwxMdSHNkxekfNEH3xDKggHbHGPIihYpsWfX9sIzRvSejhuv5Aysd g+9wBxALvwki4QuCGQWehaFKO/ia4yhh98i/lL5ipFSCH/PA9UEYdrSvpJRCtqLHgHVaM 12VALUXCgas0roZtkcB4nCM4X7TtS2SwMdh388T+w3s1q/4Va7dcWsolOsX4HHtkvIf/Y v5u0ZLas8SrswSpABvtHpSa/10EsZP9VhjxjSGGP5rPVC7T+RQGPKtMkiB/tZASbP0az5 1i1q3H0HmsGKIg0WLi1NFoGex5uSVcVSMblyor0DkNJJsDDXEUhZQqFbB93DfgEw7pg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796406666; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=kSB6balxTxsm/4OEovzXk+f/kF42dsRTVBngqQdwxg8=; b=v1CmVN+Jy31nioFJuRb2E9Xjt2cQ2g2J8m1NZhAA8biGcrZ/MTUqhwZATQbVBQVjKoHp 9yFEegFYMSzojDMNetjof0B4jcHxaB8QKK9llhU5nqgzxuHR0Xar0AX19M8qbqxiwgbor p2S6yALb+9jUrLdRrJWvWnRi13ZKz1oOLjMDm+VgTIA29Lu5qHqvrv4JUevxeMmk0C3wK Ae+37A2s96zH47e2AREyfHn9Ylw3vbBaXT39GYoJAGuwMjLMq++yiIHageldltPN6JO69 Ywe953+mEGkCZy5YrZc0XQbZ5fojmdlszAQCxWoAC5kAE3Jwd1pHY5EyV7PrOSMfwhg== 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=RLzli9GH; 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 AE466606A322 for ; Fri, 4 Dec 2026 17:51:06 +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=RLzli9GH Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3AD9B1F0093B; Fri, 4 Dec 2026 17:51:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796406666; bh=kSB6balxTxsm/4OEovzXk+f/kF42dsRTVBngqQdwxg8=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=RLzli9GHWPbqVwGl8cPdfsRdtc8cLuY/Dvyo0JR37lKLC9V3XxcZw6gqc6jcJ7HKR fb4n6pEIJge4cJmZDda9NYhJPbBrG9j0FeVb37xdlh/Ikxg+03AUgLe+82b0tFMyUm eHVb3LZTbhz/bInddajTbonGXwpfv9iLpEPl5Q8NfAkafcM+jPQasBm2jnF3zSrsFa 1EKBqAnRdyCL9A/OW9yN5HwmU4jFyaIm2niWUhL5JVMh1ho5Jgcd73apkY0uI4sxvM 8X5W6x6U93qi5o7osqFVLv5sAElm1h7X+Gvs6L+lZid6yaDv1Dmb0omevaVOPuzW7D SwmwyElI5KmFA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v2] USB: serial: mos7840: fix use-after-free in write path Reply-To: sashiko-reviews@lists.linux.dev To: "YuanZhe Liu" <25031212351@stu.xidian.edu.cn> Cc: linux-usb@vger.kernel.org In-Reply-To: <20261204172007.5822-1-25031212351@stu.xidian.edu.cn> References: <20261204172007.5822-1-25031212351@stu.xidian.edu.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 17:51:05 +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 3 potential issues to consider: - [Medium] USB: serial: mos7840: mos7840_write() triggers LED URB before poison check - [Medium] usb: serial: mos7840: unpoisoning write URBs too early in mos7840_open() - [Low] USB: serial: mos7840: commit message misstates usb-serial core URB lifetime -- commit fa8687ff7e2ac29a01ae071fbfa4d9d49b895178 Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn> USB: serial: mos7840: fix use-after-free in write path This patch moves allocation and freeing of the write URB pool from mos7840_open() and mos7840_close() to mos7840_port_probe() and mos7840_port_remove(). It also poisons the write URB pool on close and unpoisons it on open to reject post-close writes. > Give the write URB pool the same lifetime as the port instead: allocate > it at port probe and free it at port removal, after the URBs have been > killed. This matches how usb-serial core manages its own read and write > URBs. [Severity: Low] Does freeing the pool in mos7840_port_remove() really match usb-serial core's URB lifetime? usb-serial core poisons port->read_urbs[] and port->write_urbs[] in usb_serial_disconnect(), but keeps them allocated until usb_serial_port_release() runs after the last serial->kref is dropped. mos7840_port_remove() runs earlier during device_del(), so the commit message overstates the lifetime guarantee. > diff --git a/drivers/usb/serial/mos7840.c b/drivers/usb/serial/mos7840.c > index ef803e74ab4bc..fa4516775a332 100644 > --- a/drivers/usb/serial/mos7840.c > +++ b/drivers/usb/serial/mos7840.c [ ... ] > @@ -501,27 +502,16 @@ static int mos7840_open(struct tty_struct *tty, str= uct usb_serial_port *port) > struct usb_serial *serial =3D port->serial; > int response; > int j; > - struct urb *urb; > __u16 Data; > int status; > =20 > usb_clear_halt(serial->dev, port->write_urb->pipe); > usb_clear_halt(serial->dev, port->read_urb->pipe); > =20 > - /* Initialising the write urb pool */ > - for (j =3D 0; j < NUM_URBS; ++j) { > - urb =3D usb_alloc_urb(0, GFP_KERNEL); > - mos7840_port->write_urb_pool[j] =3D urb; > - if (!urb) > - continue; > - > - urb->transfer_buffer =3D kmalloc(URB_TRANSFER_BUFFER_SIZE, > - GFP_KERNEL); > - if (!urb->transfer_buffer) { > - usb_free_urb(urb); > - mos7840_port->write_urb_pool[j] =3D NULL; > - continue; > - } > + if (mos7840_port->write_urbs_poisoned) { > + for (j =3D 0; j < NUM_URBS; ++j) > + usb_unpoison_urb(mos7840_port->write_urb_pool[j]); > + mos7840_port->write_urbs_poisoned =3D false; > } > @@ -699,13 +689,6 @@ static int mos7840_open(struct tty_struct *tty, stru= ct usb_serial_port *port) > =20 > return 0; > err: > - for (j =3D 0; j < NUM_URBS; ++j) { > - urb =3D mos7840_port->write_urb_pool[j]; > - if (!urb) > - continue; > - kfree(urb->transfer_buffer); > - usb_free_urb(urb); > - } > return status; > } [Severity: Medium] Can unpoisoning write_urb_pool[] at the start of mos7840_open() allow a write URB to be submitted during hardware setup or after open fails? If a port is hung up while another fd keeps tty->count > 0, incoming data can queue flush_to_ldisc() before mos7840_close() kills read_urb. When a reopen then calls tty_reopen(), N_TTY is reattached before mos7840_open() runs, and err: returns without re-poisoning write_urb_pool[]: CPU0 kworker (flush_to_ldisc) ---- ------------------------ tty_reopen() tty_ldisc_reinit() mos7840_open() usb_unpoison_urb(...) mos7840_get_reg_sync() fails goto err; (stays unpoisoned) usb_autopm_put_interface() n_tty_receive_buf2() ... -> mos7840_write() usb_submit_urb() <-- on closed port Because tty_port_open() leaves tty_port_initialized false when open fails, mos7840_close() and mos7840_suspend() skip the port, leaving an active bulk-out URB on a closed, autosuspend-eligible port. Deferring usb_unpoison_urb() until after all register setup and goto err exits succeed, just before submitting read_urb, avoids both the setup race and leaving the pool unpoisoned on failure. > @@ -748,20 +731,13 @@ static void mos7840_close(struct usb_serial_port *p= ort) > int j; > __u16 Data; > =20 > - for (j =3D 0; j < NUM_URBS; ++j) > - usb_kill_urb(mos7840_port->write_urb_pool[j]); > - > - /* Freeing Write URBs */ > - for (j =3D 0; j < NUM_URBS; ++j) { > - if (mos7840_port->write_urb_pool[j]) { > - kfree(mos7840_port->write_urb_pool[j]->transfer_buffer); > - usb_free_urb(mos7840_port->write_urb_pool[j]); > - } > - } > - > usb_kill_urb(mos7840_port->read_urb); > mos7840_port->read_urb_busy =3D false; > =20 > + for (j =3D 0; j < NUM_URBS; ++j) > + usb_poison_urb(mos7840_port->write_urb_pool[j]); > + mos7840_port->write_urbs_poisoned =3D true; > + > Data =3D 0x0; > mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); [Severity: Medium] Can a post-close mos7840_write() still trigger LED control transfers and timers on MCS7810 devices before usb_submit_urb() sees the poisoned write URB? drivers/usb/serial/mos7840.c:mos7840_write() { ... if (mos7840_port->has_led) mos7840_led_activity(port); /* send it down the pipe */ status =3D usb_submit_urb(urb, GFP_ATOMIC); if (status) { mos7840_port->busy[i] =3D 0; dev_err_console(port, "%s - usb_submit_urb(write bulk) failed " "with status =3D %d\n", __func__, status); bytes_sent =3D status; goto exit; } ... } mos7840_close() poisons write_urb_pool[] but not led_urb or the LED timers. A post-close write therefore submits led_urb (writing MODEM_CONTROL_REGISTER in parallel with mos7840_close() clearing it) and arms led_timer1 on a closed port after runtime PM is dropped. In addition, every expected -EPERM rejection from the poisoned write URB will log a spurious dev_err_console() error. Moving mos7840_led_activity() after the status check and skipping the error log for -EPERM would address both. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204172007.5822= -1-25031212351@stu.xidian.edu.cn?part=3D1