mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Michael Grzeschik <grzeschik@puzzle.ch>
To: Shuah Khan <skhan@linuxfoundation.org>,
	Valentina Manea <valentina.manea.m@gmail.com>,
	Shuah Khan <shuah@kernel.org>, Hongren Zheng <i@zenithal.me>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-usb <linux-usb@vger.kernel.org>,
	linux-kernel <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 1/2] usbip: stub: reference count struct stub_device
Date: Wed, 18 Nov 2026 11:35:12 +0100	[thread overview]
Message-ID: <f0a598b9-3160-437c-9ed1-8529cbf07cff@puzzle.ch> (raw)
In-Reply-To: <24cb7bb7-100b-45ad-b1ea-afbb6bf41976@linuxfoundation.org>


[-- Attachment #1.1: Type: text/plain, Size: 3873 bytes --]

On 2026-11-17 12:15 AM, Shuah Khan wrote:
> On 11/11/26 04:34, Michael Grzeschik wrote:
>> A stub_device is freed by stub_disconnect() right after usbip_stop_eh()
>> reported that the event handler is done with it. The rx and tx threads
>> and the event handler can still use it at that point:
>>
>>   - The event handler clears an event bit after it ran the handler for
>>     it. A SHUTDOWN requested while a previous shutdown is running is lost,
>>     so threads started after that shutdown are not stopped before the
>>     stub_device is freed.
>>   - An event queued for the device while another one is handled leaves an
>>     entry in the event list. Once the event bits are clear,
>>     usbip_stop_eh() returns, the stub_device is freed, and the handler
>>     then locks the sysfs_lock of the freed device.
>>   - The stub_device no longer holds a reference to the usb_device, so a
>>     thread outliving the disconnect touches a freed usb_device too.
>>
>> This was seen exporting the serial port of an ST-Link from an
>> STM32MP1-DK2 on a Kirkwood (ARMv5) host. After repeated "endpoint 5 is
>> stalled" / usb_clear_halt cycles while a client opened and closed the
>> port, stub_rx crashed in dev_err() with the device pointer computed from
>> a NULL sdev->udev of freed memory:
>>
>>    Unable to handle kernel NULL pointer dereference at virtual address 000000b8 when read
>>    PC is at dev_driver_string+0x0/0x38
>>    LR is at __dev_printk+0x20/0x6c
>>    Call trace:
>>     dev_driver_string from __dev_printk+0x20/0x6c
>>     __dev_printk from _dev_err+0x40/0x74
>>     _dev_err from stub_rx_loop+0x5d8/0xd68 [usbip_host]
>>     stub_rx_loop [usbip_host] from kthread+0x100/0x120
>>
>> Reference count the stub_device instead of relying on the event handler
>> for its lifetime:
>>
>>   - the binding (probe until disconnect) holds the initial reference,
>>     stub_disconnect() drops it instead of freeing the device
>>   - the rx and tx thread hold one each while they run, taken before they
>>     are woken and dropped when they end. If kthread_stop() reports that a
>>     thread never ran, stub_shutdown_connection() drops it for the thread.
>>   - the event handler holds one per queued event until it is completely
>>     done with the device, through new optional get/put event handler
>>     operations, which vhci and vudc do not set
>>   - the stub_device holds a reference to its usb_device again, dropped
>>     with the last reference
>>
>> Threads that miss their shutdown now end when the connection goes away
>> instead of using freed memory.
> 
> I really need a simpler change log that explains the problem and the fix.

Sorry, that I took the sloptrain here. As you see, the AI was suggesting
some complex kref mechanism, for a obvious use after free.

The main takeway of the issue here is:

    When a stub device is unbound, stub_disconnect() asks the usbip event handler
    to shut the connection down and frees the device as soon as usbip_stop_eh() returns.
    But usbip_stop_eh() only checks whether the device’s event flags are cleared,
    while event_handler() may still have a second event for the same device queued.
    The handler then picks up that leftover event and runs stub_shutdown_connection()
    on the already freed device, which crashes in dev_printk().

However, after taking the sashiko suggestions into account and understanding
the over all picture, I spin another patch with hopefully less complexity.
Adding reference handling seemed like the obvious choice here, but it should
also be possible with proper locking and some checks in the usbip_stop_eh().

Sorry for this again. Please ignore this thread and take Johns version of my 2/2.

Thanks,
Michael


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

  reply	other threads:[~2026-11-18 10:35 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11 11:34 [PATCH 0/2] usbip: stability changes Michael Grzeschik
2026-11-11 11:34 ` [PATCH 1/2] usbip: stub: reference count struct stub_device Michael Grzeschik
2026-11-16 23:15   ` Shuah Khan
2026-11-18 10:35     ` Michael Grzeschik [this message]
2026-11-11 11:34 ` [PATCH 2/2] usbip: identify the event handler with current_work() Michael Grzeschik
2026-11-11 15:35   ` John Ernberg
2026-11-11 21:58     ` Michael Grzeschik
2026-11-12 22:46 ` [PATCH 0/2] usbip: stability changes Shuah Khan
2026-11-13 11:34   ` John Ernberg
2026-11-16 23:16     ` Shuah Khan
2026-11-17 12:33       ` John Ernberg

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=f0a598b9-3160-437c-9ed1-8529cbf07cff@puzzle.ch \
    --to=grzeschik@puzzle.ch \
    --cc=gregkh@linuxfoundation.org \
    --cc=i@zenithal.me \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=valentina.manea.m@gmail.com \
    /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

all inboxes | Powered by JetHome®