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: mos7720: fix use-after-free in write paths
Date: Fri, 04 Dec 2026 16:55:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-250244@kernel.org> (raw)
In-Reply-To: <20261204162044.17524-1-25031212351@stu.xidian.edu.cn>

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 *port)
>  	for (j = 0; j < NUM_URBS; ++j)
>  		usb_kill_urb(mos7720_port->write_urb_pool[j]);
>  
> -	/* Freeing Write URBs */
> -	for (j = 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_port *port)
>  
>  	mos7720_port->port = port;
>  
> +	for (i = 0; i < NUM_URBS; ++i) {
> +		urb = usb_alloc_urb(0, GFP_KERNEL);
> +		if (!urb)
> +			goto err_free;
> +
> +		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
> +					       GFP_KERNEL);
> +		if (!urb->transfer_buffer) {
> +			usb_free_urb(urb);
> +			goto err_free;
> +		}
> +
> +		mos7720_port->write_urb_pool[i] = urb;
> +	}
> +
>  	usb_set_serial_port_data(port, mos7720_port);
>  
>  	return 0;
> +
> +err_free:
> +	while (i-- > 0) {
> +		urb = 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 == -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 = usb_submit_urb(urb, GFP_ATOMIC);
	if (status) {
		dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
			"with status = %d\n", __func__, status);
		bytes_sent = 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;
>  
>  	mos7720_port = usb_get_serial_port_data(port);
> +
> +	for (i = 0; i < NUM_URBS; ++i) {
> +		urb = 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.

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

      reply	other threads:[~2026-12-04 16:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 16:20 [PATCH] USB: serial: mos7720: fix use-after-free in write paths YuanZhe Liu
2026-12-04 16:55 ` 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-250244@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