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=1796403302; b=np46Bct0nJgU0+t3X2caWCZfLkdlXx9jMxuoIAf5ioxBUK+qO/lfj3arYO/YKBimWWSE oUcBUv85rNCuq1Ikj7hCTZfCqXu1/hKZx6wBoguOBU7qMRLkammkAZRWFh9A+xbB1vUp0 iKhfvoUCWD+EaRR5SNrLEyYq9PItY03YT87LUIOxZL0u3iG4npaFoTI5XonCOFJtE/KqZ O4/ovALuffC1rqPJ9VGuPOM2nWb2PEr44mkxf+nMI/H/JtL8aHaopfGjZ5Pxm8FjYDAcY M+XiPUiFjdNoQVf34dR6Q+C//z40wbh639wKGW/gF5HdF4TOHHc6gZ1STphXc++gCkg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796403302; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=kY3hXVmwmDiv34VQGKSW8YXNvjuWaovoc+SiY/h3LSg=; b=LlugZHFRESSLS20Fsi2Qp5txAPSUh1H88JoqQKOMD90mxJXM51Hy4sCFoggQL/1LOXHp 8LCB6PgpQ+VHHgsAh4LkCSszq4g1gweM0QBJY7hr8JvJl/uWSeO9Fz36RUuEb6sFYuCcs zIJod9N/wEkFrdkkFrFNsZqRuWZFboctesM5K+TQko8jIbRjL1Vu+vNOcFuawjr+kLTPF HyTHKbQ7YkLPrhNYLbTpvX4D57EmJ9gu2BaVZEWxYm6VBL7VFoENsXWJUSVXCLiH3hEwU ZoTb5kIHSTtN/kIMfLHA3pCFkinncBBqLLOLevbzPDDd+D4+Xc4iM0W9s6HxbPf3Xgg== 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=TJi2pMju; 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 0C256606A322 for ; Fri, 4 Dec 2026 16:55:02 +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=TJi2pMju Received: by smtp.kernel.org (Postfix) with ESMTPSA id C8FE11F000C2; Fri, 4 Dec 2026 16:55:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796403301; bh=kY3hXVmwmDiv34VQGKSW8YXNvjuWaovoc+SiY/h3LSg=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=TJi2pMjuoPwq3zID5L/3VCNxvJoRzeey5IIk/7IocJlcXYHcxUWpU6mHw8BoR+daC 3KW0xbJUdqykb8IUxl2EGPDHX0b7l4isiGrvdcy6A6nx81mAAuM59AGK9d+PFaFj9B WW8hLjv5H+aLwnWTKtDf1LR/J3HyIIimPzgdWa90IYe4yZK531u+i+AEYhM/HL3koa 2coC1bMoBiftOluZK50JJogCbNL9XOuFoYybx03m5QLDgZX9V/8xfUbcqUVAA2cGb7 6PWImX2AK700eZILyUWNqoOfC1R3krsBe+pcrK0JGvjT8TBaYy0++w6i2Q2lryI4B2 VACiqgFacS1uQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] USB: serial: mos7720: fix use-after-free in write paths Reply-To: sashiko-reviews@lists.linux.dev To: "YuanZhe Liu" <25031212351@stu.xidian.edu.cn> Cc: linux-usb@vger.kernel.org In-Reply-To: <20261204162044.17524-1-25031212351@stu.xidian.edu.cn> References: <20261204162044.17524-1-25031212351@stu.xidian.edu.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 16:55:01 +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: - [High] usb: serial: mos7720: failed write URB submit wedges pool slot permanently - [Medium] usb: serial: mos7720: post-close write submits live URBs across suspend/open - [Medium] USB: serial: mos7720: write URBs freed too early in mos7720_port_remove() -- commit a57251408ca39e2c92809e7b23430a70e2f2694b Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn> USB: serial: mos7720: fix use-after-free in write paths This commit moves allocation and freeing of mos7720's write_urb_pool[] from mos7720_open() and mos7720_close() to mos7720_port_probe() and mos7720_port_remove(). > diff --git a/drivers/usb/serial/mos7720.c b/drivers/usb/serial/mos7720.c > index 312fa11cea952..0606de2e9c7a0 100644 > --- a/drivers/usb/serial/mos7720.c > +++ b/drivers/usb/serial/mos7720.c [ ... ] > @@ -978,14 +955,6 @@ static void mos7720_close(struct usb_serial_port *po= rt) > for (j =3D 0; j < NUM_URBS; ++j) > usb_kill_urb(mos7720_port->write_urb_pool[j]); > =20 > - /* Freeing Write URBs */ > - for (j =3D 0; j < NUM_URBS; ++j) { > - if (mos7720_port->write_urb_pool[j]) { > - kfree(mos7720_port->write_urb_pool[j]->transfer_buffer); > - usb_free_urb(mos7720_port->write_urb_pool[j]); > - } > - } > - > /* While closing port, shutdown all bulk read, write * > * and interrupt read if they exists, otherwise nop */ > usb_kill_urb(port->write_urb); [Severity: Medium] Can a racing echo write still submit live URBs from write_urb_pool[] after mos7720_close() returns? mos7720_close() kills write_urb_pool[] with usb_kill_urb() before killing port->read_urb, and mos7720_write() does not check mos7720_port->open. A racing flush_to_ldisc echo can therefore submit a bulk-out URB after serial_port_shutdown() calls usb_autopm_put_interface(). Because moschip7720_2port_driver has no .suspend callback and usb_serial_port_poison_urbs() only poisons usb-serial core URBs, that URB can stay in flight across USB autosuspend or collide with usb_clear_halt() when the port is reopened. Switching mos7720 to usb-serial core's generic write callbacks would avoid managing a private write_urb_pool[] altogether. [ ... ] > @@ -1710,16 +1681,53 @@ static int mos7720_port_probe(struct usb_serial_p= ort *port) > =20 > mos7720_port->port =3D port; > =20 > + for (i =3D 0; i < NUM_URBS; ++i) { > + urb =3D usb_alloc_urb(0, GFP_KERNEL); > + if (!urb) > + goto err_free; > + > + urb->transfer_buffer =3D kmalloc(URB_TRANSFER_BUFFER_SIZE, > + GFP_KERNEL); > + if (!urb->transfer_buffer) { > + usb_free_urb(urb); > + goto err_free; > + } > + > + mos7720_port->write_urb_pool[i] =3D urb; > + } > + > usb_set_serial_port_data(port, mos7720_port); > =20 > return 0; > + > +err_free: > + while (i-- > 0) { > + urb =3D mos7720_port->write_urb_pool[i]; > + kfree(urb->transfer_buffer); > + usb_free_urb(urb); > + } > + kfree(mos7720_port); > + > + return -ENOMEM; > } [Severity: High] Does reusing write_urb_pool[] across open/close cycles permanently wedge a pool slot whenever usb_submit_urb() fails? mos7720 tracks busy slots solely via urb->status =3D=3D -EINPROGRESS, which usb_submit_urb() sets before calling the HCD. When a racing mos7720_write() hits usb_kill_urb() in mos7720_close(), submission fails with -EPERM without clearing urb->status or calling urb->complete(): drivers/usb/serial/mos7720.c:mos7720_write() { ... status =3D usb_submit_urb(urb, GFP_ATOMIC); if (status) { dev_err_console(port, "%s - usb_submit_urb(write bulk) failed " "with status =3D %d\n", __func__, status); bytes_sent =3D status; goto exit; } ... } Previously, reallocating the pool in mos7720_open() reset urb->status to 0. Now a failed submit permanently leaks that 32-byte slot and keeps mos7720_chars_in_buffer() non-zero, making every later close() stall in tty_wait_until_sent() for the 30-second closing_wait. Resetting urb->status on usb_submit_urb() failure in mos7720_write() would prevent the slot from staying wedged. > static void mos7720_port_remove(struct usb_serial_port *port) > { > struct moschip_port *mos7720_port; > + struct urb *urb; > + int i; > =20 > mos7720_port =3D usb_get_serial_port_data(port); > + > + for (i =3D 0; i < NUM_URBS; ++i) { > + urb =3D mos7720_port->write_urb_pool[i]; > + if (!urb) > + continue; > + usb_kill_urb(urb); > + kfree(urb->transfer_buffer); > + usb_free_urb(urb); > + } > + > kfree(mos7720_port); > } [Severity: Medium] Are write_urb_pool[] and their transfer buffers freed too early here if a line discipline is still tearing down? tty_port_close() clears port->tty before tty_ldisc_release() runs. If usb_serial_disconnect() races in between, tty_port_tty_vhangup() skips hanging up the ldisc and mos7720_port_remove() frees the pool while an attached ldisc (such as N_GSM0710) still has async TX work running: CPU0 CPU1 kworker tty_release() tty_port_close() tty_port_tty_set(port, NULL) usb_serial_disconnect() tty_port_tty_vhangup() (port->tty is NULL) mos7720_port_remove() usb_kill_urb(urb) kfree(urb->transfer_buffer) usb_free_urb(urb) kfree(mos7720_port) gsmld_write_task() gsmld_output() mos7720_write_room() <-- use-after-free tty_ldisc_release() gsmld_close() serial_write_room() does not check for disconnect, so mos7720_write_room() dereferences the freed mos7720_port and URBs even on physical unplug. On sysfs unbind or module unload, serial_write() can also call mos7720_write(), writing into a freed transfer_buffer or resubmitting an URB between usb_kill_urb() and usb_free_urb() because usb_kill_urb() does not poison the URB. Note also that the commit message says this matches how usb-serial core manages its URBs, but usb-serial core poisons its URBs in usb_serial_disconnect() and defers freeing them until usb_serial_port_release() after serial_cleanup() drops the final kref. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204162044.1752= 4-1-25031212351@stu.xidian.edu.cn?part=3D1