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: mos7840: fix use-after-free in write path
Date: Fri, 04 Dec 2026 17:02:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-250253@kernel.org> (raw)
In-Reply-To: <20261204162226.17678-1-25031212351@stu.xidian.edu.cn>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] USB: serial: mos7840: uninitialized status return in
  mos7840_port_probe()
- [Medium] usb: serial: mos7840: mos7840_close() leaves post-close
  writes unblocked
- [Low] USB: serial: mos7840: inaccurate Fixes tag and URB lifetime
  claim in commit log
--

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

> USB: serial: mos7840: fix use-after-free in write path
>
> The write URB pool is allocated at open() and freed at close(), but the
> TTY line discipline can still invoke the driver write() callback after
> close() has returned.  tty_release() calls the driver close() before it
> cancels the flip-buffer work, so a port that is still processing
> received data can flush pending echo characters to the driver and
> dereference the freed URBs.
>
> A KASAN slab-use-after-free was observed in mos7840_write() while
> reading the transfer buffer of a freed urb, followed by an "urb
> submitted while active" warning when the same freed urb was resubmitted.
>
> 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 and makes the write callback safe against a racing close().
>
> The issue was found by exercising the driver with a virtual USB device
> during automated testing.  The change has only been compile tested; no
> physical device was available for runtime testing.
>
> Fixes: 0de9a7024e7a ("USB: overhaul of mos7840 driver")
> Cc: stable@vger.kernel.org
> Co-developed-by: Bin Yu <byu@xidian.edu.cn>
> Signed-off-by: Bin Yu <byu@xidian.edu.cn>
> Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

[Severity: Low]
Should the Fixes tag point to 3f5429746d91 ("USB: Moschip 7840 USB-Serial
Driver") instead of 0de9a7024e7a ("USB: overhaul of mos7840 driver")?  The
initial driver commit already allocated write_urb_pool[] in mos7840_open()
and freed it in mos7840_close() without clearing the pointers.

Also, usb-serial core poisons its read and write URBs in
usb_serial_disconnect() and frees them in usb_serial_port_release(), not
during port_remove().

> diff --git a/drivers/usb/serial/mos7840.c b/drivers/usb/serial/mos7840.c
> index ef803e74ab4bc..f4eda9bc7b236 100644
> --- a/drivers/usb/serial/mos7840.c
> +++ b/drivers/usb/serial/mos7840.c

[ ... ]

> @@ -751,14 +726,6 @@ static void mos7840_close(struct usb_serial_port *port)
>  	for (j = 0; j < NUM_URBS; ++j)
>  		usb_kill_urb(mos7840_port->write_urb_pool[j]);
>  
> -	/* Freeing Write URBs */
> -	for (j = 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 = false;
>  

[Severity: Medium]
Can a racing post-close mos7840_write() still submit write URBs and trigger
LED activity after mos7840_close() returns?

CPU0                              CPU1
tty_port_close_start()
  ...
                                  mos7840_bulk_in_callback()
                                    tty_flip_buffer_push()
mos7840_close()
  usb_kill_urb(write_urb_pool[])
  usb_kill_urb(read_urb)
serial_port_shutdown()
  usb_autopm_put_interface()
                                  flush_to_ldisc()
                                    ...
                                      mos7840_write()
                                        usb_submit_urb(urb)  <-- post-close

Because mos7840_close() kills write_urb_pool[] before read_urb and does not
poison the pool, a queued ldisc write can still submit a bulk-out URB and
arm led_urb/led_timer1 after the runtime PM reference is dropped.

If the port is reopened while that write URB is still queued, mos7840_open()
calls usb_clear_halt() on the busy bulk-out endpoint, triggering an EHCI
warning or xHCI reset refusal and leaving host and device data toggles out
of sync.  On suspend, mos7840_suspend() skips closed ports and never poisons
write_urb_pool[] or stops the LED timers and URB, leaving them active on a
suspended device.

Killing read_urb first and poisoning write_urb_pool[] in mos7840_close()
(unpoisoning it in mos7840_open()) would prevent post-close submissions.

[ ... ]

> @@ -1521,7 +1488,9 @@ static int mos7840_port_probe(struct usb_serial_port *port)
>  	struct usb_serial *serial = port->serial;
>  	unsigned long device_flags = (unsigned long)usb_get_serial_data(serial);
>  	struct moschip_port *mos7840_port;
> +	struct urb *urb;
>  	int status;
> +	int j;
>  	int pnum;
>  	__u16 Data;
>  
> @@ -1542,6 +1511,19 @@ static int mos7840_port_probe(struct usb_serial_port *port)
>  	mos7840_port->port = port;
>  	spin_lock_init(&mos7840_port->pool_lock);
>  
> +	/* Initializing the write urb pool */
> +	for (j = 0; j < NUM_URBS; ++j) {
> +		urb = usb_alloc_urb(0, GFP_KERNEL);
> +		if (!urb)
> +			goto error;
> +		mos7840_port->write_urb_pool[j] = urb;
> +
> +		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
> +					       GFP_KERNEL);
> +		if (!urb->transfer_buffer)
> +			goto error;
> +	}
> +
>  	/* minor is not initialised until later by
>  	 * usb-serial.c:get_free_serial() and cannot therefore be used
>  	 * to index device instances */

[Severity: Medium]
Does mos7840_port_probe() return an uninitialized status if usb_alloc_urb()
or kmalloc() fails here?

Both error branches jump to error before status is assigned, so the function
frees mos7840_port and returns uninitialized status.  If status happens to
be zero (for example under CONFIG_INIT_STACK_ALL_ZERO=y),
usb_serial_device_probe() proceeds to register the tty device with NULL port
driver data, causing a NULL pointer dereference on subsequent open or
removal.  Setting status = -ENOMEM before jumping to error would fix this.

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

      reply	other threads:[~2026-12-04 17:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 16:22 [PATCH] USB: serial: mos7840: fix use-after-free in write path YuanZhe Liu
2026-12-04 17:02 ` 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-250253@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