usbip: usbip_host: remove legacy rebind_store in favor of drivers_probe
Subsystems (3)
v3 cover usbip: usbip_host: remove legacy rebind_store in favor of drivers_probe
do_rebind, which sleeps normally gets a mutex lock. However, it does not or should I say, cannot check for null udev between spin lock dropped in rebind_store and entering do_rebind. This is a potential race window already. So, even if we check for null udev under spinlock, we cannot do it outside. Regarding do_rebind, it is called during stub_device_rebind, but that function is called during module exit when all files are removed. So, do_rebind is not designed to work in a concurrent environment in the first place.
We have a safer function that can already do what do_rebind does, drivers_probe. So, we use drivers_probe to rebind the device rather than use rebind_store.
usbip tool references this function immediately after the device is unbound, which is safe for the tool itself but since we opted for drivers_probe, fix it by using drivers_probe rather than rebind_store after unbinding device which is more safer.
Tested and working in both userspace via the tool and manually echoing the busid in the related nodes. rebind node is still left active with a warning to use drivers_probe upon encountering rebind_store.
Thanks,
Jeffin.
---
Changes in v3:
- Removed rebind_store in favor of drivers_probe to eliminate race condition
Changes in v2:
- Addressed concerns raised by the Greg KH in v1 discussion
- Added usb_get_dev() to get a reference to udev preventing it from becoming null after the null check. Drop the reference after using it in do_rebind(). Did not fix the race.
v1:
- Initial patch with a udev null check that returns -ENODEV if udev is null.
---
Jeffin Philip (2):
usbip: usbip_host: fix null pointer dereference in rebind_store
usbip: tools: replace faulty rebind_store with drivers_probe
drivers/usb/usbip/stub_main.c | 27 +-------------------------- tools/usb/usbip/src/usbip_unbind.c | 7 +++---- 2 files changed, 4 insertions(+), 30 deletions(-)
--
2.55.0
PATCH v3 1/2 usbip: usbip_host: fix null pointer dereference in rebind_store
rebind_store drops locks to execute do_rebind which sleeps during which time udev may become NULL due to physical disconnect. Since this cannot be prevented and spinlocks cannot be obtained in do_rebind, we turn towards the function that performs the same action, drivers_probe, safely. Remove rebind_store and print a warning to the user to use drivers_probe instead as a safer alternative.
---
Changes in v3:
- Addressed the race condition proposed by Greg KH in v2 discussion.
Changes in v2:
- Addressed concerns raised by the Greg KH in v1 discussion
- Added usb_get_dev() to get a reference to udev preventing it from becoming null after the null check. Drop the reference after using it in do_rebind()
v1:
- Initial patch with a udev null check that returns -ENODEV if udev is null.
--- drivers/usb/usbip/stub_main.c | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-)
drivers/usb/usbip/stub_main.c +1 -26
@@ -242,32 +242,7 @@ static void stub_device_rebind(void)static ssize_t rebind_store (struct device_driver *dev,const char *buf,size_t count) { -int ret; -int len; -struct bus_id_priv *bid; - -/* buf length should be less that BUSID_SIZE */ - len =strnlen (buf,BUSID_SIZE ); - -if (!(len <BUSID_SIZE )) -return -EINVAL ; - - bid =get_busid_priv (buf); -if (!bid) -return -ENODEV ; - -/* mark the device for deletion so probe ignores it during rescan */ - bid->status =STUB_BUSID_OTHER ; -/* release the busid lock */ -put_busid_priv (bid); - - ret =do_rebind ((char *) buf, bid); -if (ret <0 ) -return ret; - -/* delete device from busid_table */ -del_match_busid ((char *) buf); - +pr_warn ("rebind node is deprecated, consider using drivers_probe instead\n" );return count; }
--
2.55.0
Greg KH
maintainerRe: [PATCH v3 1/2] usbip: usbip_host: fix null pointer dereference in rebind_store
Quoted 54 lines · Jeffin Philip
rebind_storedrops locks to executedo_rebindwhich sleeps during which time udev may become NULL due to physical disconnect. Since this cannot be prevented and spinlocks cannot be obtained indo_rebind, we turn towards the function that performs the same action,drivers_probe, safely. Removerebind_storeand print a warning to the user to usedrivers_probeinstead as a safer alternative.Reported-by [email protected]Fixes 4bfb141bc013 ("usbip: usbip_host: fix to hold parent lock for device_attach() calls")Signed-off-by Jeffin Philip <[email protected]>---
Changes in v3:
- Addressed the race condition proposed by Greg KH in v2 discussion.
Changes in v2:
- Addressed concerns raised by the Greg KH in v1 discussion
- Addedusb_get_dev()to get a reference to udev preventing it from becoming null after the null check. Drop the reference after using it indo_rebind()
v1:
- Initial patch with a udev null check that returns-ENODEVif udev is null.--- drivers/usb/usbip/stub_main.c | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-)drivers/usb/usbip/stub_main.c +0
-17@@ -242,32 +242,7 @@ static void stub_device_rebind(void)static ssize_t rebind_store (struct device_driver *dev,const char *buf,size_t count) { -int ret; -int len; -struct bus_id_priv *bid; - -/* buf length should be less that BUSID_SIZE */ - len =strnlen (buf,BUSID_SIZE ); - -if (!(len <BUSID_SIZE )) -return -EINVAL ; - - bid =get_busid_priv (buf); -if (!bid) -return -ENODEV ; - -/* mark the device for deletion so probe ignores it during rescan */ - bid->status =STUB_BUSID_OTHER ; -/* release the busid lock */
drivers/usb/usbip/stub_main.c static void stub_device_rebind(void) · line 245
Quoted 10 lines
drivers/usb/usbip/stub_main.c +1
-9-put_busid_priv (bid); - - ret =do_rebind ((char *) buf, bid); -if (ret <0 ) -return ret; - -/* delete device from busid_table */ -del_match_busid ((char *) buf); - +pr_warn ("rebind node is deprecated, consider using drivers_probe instead\n" );
No one will ever notice this, just remove the sysfs file entirely if you really think it is ok to remove the functionality. That way userspace will notice and handle it properly (hopefully...)
But, are you sure it is ok to remove this user/kernel api? What about workflows that are using it?
thanks,
greg k-h
Re: [PATCH v3 1/2] usbip: usbip_host: fix null pointer dereference in rebind_store
Quoted 3 lines · Greg KH
No one will ever notice this, just remove the sysfs file entirely if you really think it is ok to remove the functionality. That way userspace will notice and handle it properly (hopefully...)
userspace tool atp handles this properly as it unbinds and rebinds sequentially. The problem is when any script tries to do this without a particular order in
sysfs: bind, rebind then unbind causes an invalid opcode and attempting to directly rebind immediately after writing to match_busid causes the dereference. I think it is best we drop the rebind_store function and use drivers_probe(). We add del_match_busid() alone to rebind_store() to stop tracking.
Quoted 2 lines
But, are you sure it is ok to remove this user/kernel api? What about workflows that are using it?
Any scripts/user that don't use the sysfs properly can keep the broken pieces themselves, no? We have the newer drivers_probe() that can perform this much more cleaner.
Ultimately we can keep the rebind_store() function if you choose. But this is just my opinion.
Thanks,
Jeffin.
PATCH v3 2/2 usbip: tools: replace faulty rebind_store with drivers_probe
In reply to Jeffin Philipunbind_device() currently unbinds and then uses rebind_store to rebind the device back to the usb driver. This is already possible with the safer drivers_probe rather than the faulty rebind_store. Fix this by referencing drivers_probe rather than rebind_store.
--- tools/usb/usbip/src/usbip_unbind.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
tools/usb/usbip/src/usbip_unbind.c +3 -4
@@ -34,7 +34,7 @@ static int unbind_device(char *busid)char unbind_attr_name[] ="unbind" ;char unbind_attr_path[SYSFS_PATH_MAX ]; -char rebind_attr_name[] ="rebind" ; +char rebind_attr_name[] ="drivers_probe" ;char rebind_attr_path[SYSFS_PATH_MAX ];struct udev *udev;
@@ -77,9 +77,8 @@ static int unbind_device(char *busid) }/* Trigger new probing. */ -snprintf (rebind_attr_path,sizeof (unbind_attr_path),"%s/%s/%s/%s/%s/%s" , -SYSFS_MNT_PATH ,SYSFS_BUS_NAME , bus_type,SYSFS_DRIVERS_NAME , -USBIP_HOST_DRV_NAME , rebind_attr_name); +snprintf (rebind_attr_path,sizeof (rebind_attr_path),"%s/%s/%s/%s" , +SYSFS_MNT_PATH ,SYSFS_BUS_NAME , bus_type, rebind_attr_name); rc =write_sysfs_attribute (rebind_attr_path, busid,strlen (busid));if (rc <0 ) {
--
2.55.0
Shuah Khan
maintainerQuoted 22 lines · Jeffin Philip
do_rebind, which sleeps normally gets a mutex lock. However, it does not or should I say, cannot check for null udev between spin lock dropped inrebind_storeand enteringdo_rebind. This is a potential race window already. So, even if we check for null udev under spinlock, we cannot do it outside. Regardingdo_rebind, it is called duringstub_device_rebind, but that function is called during module exit when all files are removed. So,do_rebindis not designed to work in a concurrent environment in the first place.We have a safer function that can already do what
do_rebinddoes,drivers_probe. So, we usedrivers_probeto rebind the device rather than userebind_store.usbip tool references this function immediately after the device is unbound, which is safe for the tool itself but since we opted for
drivers_probe, fix it by usingdrivers_proberather thanrebind_storeafter unbinding device which is more safer.Tested and working in both userspace via the tool and manually echoing the busid in the related nodes. rebind node is still left active with a warning to use
drivers_probeupon encounteringrebind_store.
Quoted 8 lines
Thanks,
Jeffin.Signed-off-by Jeffin Philip <[email protected]>---
Changes in v3:
- Removedrebind_storein favor ofdrivers_probeto eliminate race condition
How did you find this problem? Is this generated code or did you write it?
Also, the first patch removes code in rebind_store(), replacing it with a pr_warn()? The second patch points it driver_probe() - what happens with just the first patch?
Did you run tests to see if you can bind and unbind devices - does the driver work correctly?
thanks,
-- Shuah
Quoted 26 lines · Shuah Khan
Quoted 25 lines · Jeffin Philip
do_rebind, which sleeps normally gets a mutex lock. However, it does not or should I say, cannot check for null udev between spin lock dropped inrebind_storeand enteringdo_rebind. This is a potential race window already. So, even if we check for null udev under spinlock, we cannot do it outside. Regardingdo_rebind, it is called duringstub_device_rebind, but that function is called during module exit when all files are removed. So,do_rebindis not designed to work in a concurrent environment in the first place.We have a safer function that can already do what
do_rebinddoes,drivers_probe. So, we usedrivers_probeto rebind the device rather than userebind_store.usbip tool references this function immediately after the device is unbound, which is safe for the tool itself but since we opted for
drivers_probe, fix it by usingdrivers_proberather thanrebind_storeafter unbinding device which is more safer.Tested and working in both userspace via the tool and manually echoing the busid in the related nodes. rebind node is still left active with a warning to use
drivers_probeupon encounteringrebind_store.Thanks,
Jeffin.
Quoted 7 lines
Quoted 5 lines
Signed-off-by Jeffin Philip <[email protected]>---
Changes in v3:
- Removedrebind_storein favor ofdrivers_probeto eliminate race conditionHow did you find this problem?
I found the problem on syzbot and had to reproduce it using the following commands:
link to issue: https://syzkaller.appspot.com/bug?extid=af76b01c9a0f0ab60fb0
echo 'add 1-1' > /sys/bus/usb/drivers/ubsip-host/match_busid echo '1-1' > /sys/bus/usb/drivers/usbip-host/rebind
Quoted 1 line
Is this generated code or did you write it?
No, I wrote the code myself.
Quoted 3 lines
Also, the first patch removes code in
rebind_store(), replacing it with apr_warn()? The second patch points itdriver_probe()- what happens with just the first patch?
It just prints out a warning. I did get a -Wunused function warning while building the kernel for testing and considered removing it. I ultimately didn't as scripts running on newer kernels(if this was merged) would break as there is no rebind node. Should I remove it? and is pr_warn not the right way to deal with this? If so, please advise.
Quoted 2 lines
Did you run tests to see if you can bind and unbind devices - does the driver work correctly?
Yes the tool works correctly after switching to drivers_probe. Devices are bound and unbound correctly.
The function also caused an invalid opcode while testing in an unpatched kernel with a different sequence: write to match_busid, then write to bind and rebind, followed by writing to unbind.
Thanks,
Jeffin.
Shuah Khan
maintainerQuoted 50 lines · Jeffin Philip
Quoted 26 lines · Shuah Khan
Quoted 25 lines · Jeffin Philip
do_rebind, which sleeps normally gets a mutex lock. However, it does not or should I say, cannot check for null udev between spin lock dropped inrebind_storeand enteringdo_rebind. This is a potential race window already. So, even if we check for null udev under spinlock, we cannot do it outside. Regardingdo_rebind, it is called duringstub_device_rebind, but that function is called during module exit when all files are removed. So,do_rebindis not designed to work in a concurrent environment in the first place.We have a safer function that can already do what
do_rebinddoes,drivers_probe. So, we usedrivers_probeto rebind the device rather than userebind_store.usbip tool references this function immediately after the device is unbound, which is safe for the tool itself but since we opted for
drivers_probe, fix it by usingdrivers_proberather thanrebind_storeafter unbinding device which is more safer.Tested and working in both userspace via the tool and manually echoing the busid in the related nodes. rebind node is still left active with a warning to use
drivers_probeupon encounteringrebind_store.Thanks,
Jeffin.Quoted 7 lines
Quoted 5 lines
Signed-off-by Jeffin Philip <[email protected]>---
Changes in v3:
- Removedrebind_storein favor ofdrivers_probeto eliminate race conditionHow did you find this problem?
I found the problem on syzbot and had to reproduce it using the following commands:
link to issue: https://syzkaller.appspot.com/bug?extid=af76b01c9a0f0ab60fb0
echo 'add 1-1' >/sys/bus/usb/drivers/ubsip-host/match_busidecho '1-1' >/sys/bus/usb/drivers/usbip-host/rebindQuoted 1 line
Is this generated code or did you write it?
No, I wrote the code myself.
Quoted 3 lines
Also, the first patch removes code in
rebind_store(), replacing it with apr_warn()? The second patch points itdriver_probe()- what happens with just the first patch?
Quoted 5 lines
It just prints out a warning. I did get a -Wunused function warning while building the kernel for testing and considered removing it. I ultimately didn't as scripts running on newer kernels(if this was merged) would break as there is no rebind node. Should I remove it? and is
pr_warnnot the right way to deal with this? If so, please advise.
Okay. But why is this change split into two patches? Does just the first patch work correctly?
Quoted 10 lines
Quoted 3 lines
Did you run tests to see if you can bind and unbind devices - does the driver work correctly?
Yes the tool works correctly after switching to
drivers_probe. Devices are bound and unbound correctly.The function also caused an invalid opcode while testing in an unpatched kernel with a different sequence: write to
match_busid, then write to bind and rebind, followed by writing to unbind.
thanks,
-- Shuah
Quoted 51 lines · Shuah Khan
Quoted 50 lines · Jeffin Philip
Quoted 26 lines · Shuah Khan
Quoted 25 lines · Jeffin Philip
do_rebind, which sleeps normally gets a mutex lock. However, it does not or should I say, cannot check for null udev between spin lock dropped inrebind_storeand enteringdo_rebind. This is a potential race window already. So, even if we check for null udev under spinlock, we cannot do it outside. Regardingdo_rebind, it is called duringstub_device_rebind, but that function is called during module exit when all files are removed. So,do_rebindis not designed to work in a concurrent environment in the first place.We have a safer function that can already do what
do_rebinddoes,drivers_probe. So, we usedrivers_probeto rebind the device rather than userebind_store.usbip tool references this function immediately after the device is unbound, which is safe for the tool itself but since we opted for
drivers_probe, fix it by usingdrivers_proberather thanrebind_storeafter unbinding device which is more safer.Tested and working in both userspace via the tool and manually echoing the busid in the related nodes. rebind node is still left active with a warning to use
drivers_probeupon encounteringrebind_store.Thanks,
Jeffin.Quoted 7 lines
Quoted 5 lines
Signed-off-by Jeffin Philip <[email protected]>---
Changes in v3:
- Removedrebind_storein favor ofdrivers_probeto eliminate race conditionHow did you find this problem?
I found the problem on syzbot and had to reproduce it using the following commands:
link to issue: https://syzkaller.appspot.com/bug?extid=af76b01c9a0f0ab60fb0
echo 'add 1-1' >/sys/bus/usb/drivers/ubsip-host/match_busidecho '1-1' >/sys/bus/usb/drivers/usbip-host/rebindQuoted 1 line
Is this generated code or did you write it?
No, I wrote the code myself.
Quoted 3 lines
Also, the first patch removes code in
rebind_store(), replacing it with apr_warn()? The second patch points itdriver_probe()- what happens with just the first patch?
Quoted 8 lines
Quoted 5 lines
It just prints out a warning. I did get a -Wunused function warning while building the kernel for testing and considered removing it. I ultimately didn't as scripts running on newer kernels(if this was merged) would break as there is no rebind node. Should I remove it? and is
pr_warnnot the right way to deal with this? If so, please advise.Okay. But why is this change split into two patches? Does just the first patch work correctly?
The first patch is for people echoing the busid directly to rebind node, so it just prints out a warning asking them to use drivers_probe. The second patch is for the usbip tool, as the old tool used rebind store for rebinding, we replaced it already in the first patch and since 'usbip unbind' rebinds the device back to the usb driver after unbinding it, we cannot use rebind_store like the old tool. That is why I replaced rebind with drivers_probe in the second patch.
Quoted 10 lines
Quoted 10 lines
Quoted 3 lines
Did you run tests to see if you can bind and unbind devices - does the driver work correctly?
Yes the tool works correctly after switching to
drivers_probe. Devices are bound and unbound correctly.The function also caused an invalid opcode while testing in an unpatched kernel with a different sequence: write to
match_busid, then write to bind and rebind, followed by writing to unbind.
Thanks,
Jeffin.
v3 cover usbip: usbip_host: remove legacy rebind_store in favor of drivers_probe
In reply to Jeffin PhilipQuoted 13 lines · Jeffin Philip
do_rebind, which sleeps normally gets a mutex lock. However, it does not or should I say, cannot check for null udev between spin lock dropped inrebind_storeand enteringdo_rebind. This is a potential race window already. So, even if we check for null udev under spinlock, we cannot do it outside. Regardingdo_rebind, it is called duringstub_device_rebind, but that function is called during module exit when all files are removed. So,do_rebindis not designed to work in a concurrent environment in the first place.We have a safer function that can already do what
do_rebinddoes,drivers_probe. So, we usedrivers_probeto rebind the device rather than userebind_store.
Quoted 8 lines
usbip tool references this function immediately after the device is unbound, which is safe for the tool itself but since we opted for
drivers_probe, fix it by usingdrivers_proberather thanrebind_storeafter unbinding device which is more safer.Tested and working in both userspace via the tool and manually echoing the busid in the related nodes. rebind node is still left active with a warning to use
drivers_probeupon encounteringrebind_store.
Friendly ping. Let me know if you want me to change anything.
Thanks,
Jeffin.
Shuah Khan
maintainerQuoted 14 lines · Jeffin Philip
Quoted 13 lines · Jeffin Philip
do_rebind, which sleeps normally gets a mutex lock. However, it does not or should I say, cannot check for null udev between spin lock dropped inrebind_storeand enteringdo_rebind. This is a potential race window already. So, even if we check for null udev under spinlock, we cannot do it outside. Regardingdo_rebind, it is called duringstub_device_rebind, but that function is called during module exit when all files are removed. So,do_rebindis not designed to work in a concurrent environment in the first place.We have a safer function that can already do what
do_rebinddoes,drivers_probe. So, we usedrivers_probeto rebind the device rather than userebind_store.
Quoted 8 lines
Quoted 8 lines
usbip tool references this function immediately after the device is unbound, which is safe for the tool itself but since we opted for
drivers_probe, fix it by usingdrivers_proberather thanrebind_storeafter unbinding device which is more safer.Tested and working in both userspace via the tool and manually echoing the busid in the related nodes. rebind node is still left active with a warning to use
drivers_probeupon encounteringrebind_store.
I want to know your test setup. Can you reproduce this with usbip host and running bind command from the tool?
Also send me the error messages.
thanks,
-- Shuah
Quoted 4 lines · Shuah Khan
I want to know your test setup. Can you reproduce this with usbip host and running bind command from the tool?
Also send me the error messages.
Hi Shuah, Jeffin,
We hit a crash in do_rebind() on production servers, with the usbip tool and no fuzzing involved. In our case the pointer is not NULL but
dangling: stub_probe() saves the usb_device in busid_priv->udev without a reference, and nothing clears it when the device is disconnected.
Our servers export USB debug probes with usbip, each probe connected through external powered USB hubs. When a probe stops responding while exported, closing its usbip connection makes usbip-host reset it; the reset fails and the hub driver disconnects it. The "usbip unbind" that follows writes the busid to "rebind", and do_rebind() passes the freed usb_device to device_attach().
Kernel log, trimmed ("usbip detach", then "usbip unbind -b 1-3.3.4" started at 08:24:36.9; 3-2 is the vhci_hcd side, on the same host):
08:24:36 usbip-host 1-3.3.4: recv a header, 0
08:24:36 vhci_hcd: connection closed
08:24:36 usb 3-2: USB disconnect, device number 36
08:24:37 usbip-host 1-3.3.4: reset full-speed USB device number 127 using xhci_hcd
08:24:37 usbip-host 1-3.3.4: Device not responding to setup address.
08:24:37 usbip-host 1-3.3.4: device descriptor read/all, error -32
[... three more resets, same errors]
08:24:38 usbip-host 1-3.3.4: device reset
08:24:38 usb 1-3.3.4: USB disconnect, device number 127
08:24:38 usb 1-3.3.4: new full-speed USB device number 25 using xhci_hcd
08:24:38 usb 1-3.3.4: Device not responding to setup address.
[... devices 26 to 28 and a port power cycle, same errors]
08:24:41 usb 1-3.3-port4: unable to enumerate USB device
08:24:41 BUG: kernel NULL pointer dereference, address: 00000000000000e0
08:24:41 #PF: supervisor read access in kernel mode
08:24:41 #PF: error_code(0x0000) - not-present page
08:24:41 Oops: Oops: 0000 [#1] SMP NOPTI
08:24:41 CPU: 1 UID: 0 PID: 3667297 Comm: usbip Not tainted 7.0.0-42-generic #42~24.04.1-Ubuntu PREEMPT(lazy) 08:24:41 RIP: 0010:__device_attach+0x3b/0x1e0
08:24:41 Call Trace:
08:24:41 device_attach+0x10/0x20 08:24:41 do_rebind.isra.0+0x3b/0xa0 [usbip_host] 08:24:41 rebind_store+0x99/0x140 [usbip_host] 08:24:41 drv_attr_store+0x24/0x50 08:24:41 sysfs_kf_write+0x72/0x90 08:24:41 kernfs_fop_write_iter+0x161/0x1f0 08:24:41 vfs_write+0x29e/0x480 08:24:41 ksys_write+0x71/0xf0
The usb_device is freed at the "USB disconnect" of 08:24:38. The rebind seems to wait for the hub's device lock, held by the hub driver while it retries the enumeration, and crashes as soon as the hub driver gives up, at 08:24:41.
This happened twice so far, on v7.0 distribution kernels. The task dies holding the parent hub's device lock, so everything that touches that hub blocks afterwards, including the other probes behind it, and only a SysRq reboot recovers. The same use after free happens at module
removal: stub_device_rebind() calls do_rebind() for every device bound since the module was loaded, which v3 doesn't change.
Test setup: a QEMU VM with KASAN, v7.4-rc4, a g_zero gadget on dummy_hcd as the device, unplugged by unloading g_zero:
- bind, unbind, unplug, "match_busid del", rebind (the writes of "usbip unbind", with the device unplugged in between):
slab-use-after-free in do_rebind();
- bind, unplug, rmmod usbip_host: the same;
- "match_busid add" then rebind, never bound: the syzbot report, null-ptr-deref in range [0xf0-0xf7] in do_rebind().
The first case, trimmed: the usb_device was freed by hub_event() when the device was unplugged, and do_rebind() reads its dev.parent (240 = 0xf0, the offset in the syzbot report):
BUG: KASAN: slab-use-after-free in do_rebind.isra.0+0x14d/0x180 [usbip_host] Read of size 8 at addr ffff888004d0e0f0 by task sh/129 CPU: 1 UID: 0 PID: 129 Comm: sh Not tainted 7.4.0-rc4 #1 PREEMPT(lazy) Call Trace: <TASK> dump_stack_lvl+0x53/0x70 print_report+0xd0/0x630 kasan_report+0xce/0x100 do_rebind.isra.0+0x14d/0x180 [usbip_host] rebind_store+0xae/0x110 [usbip_host] kernfs_fop_write_iter+0x329/0x4c0 vfs_write+0x5d2/0xcd0 ksys_write+0xf7/0x1c0 do_syscall_64+0xdf/0x4c0 entry_SYSCALL_64_after_hwframe+0x77/0x7f </TASK> Allocated by task 59: kasan_save_stack+0x33/0x60 kasan_save_track+0x14/0x40 __kasan_kmalloc+0x8f/0xa0 __kmalloc_cache_noprof+0x148/0x360 usb_alloc_dev+0x50/0xef0 hub_event+0x1d46/0x41d0 process_one_work+0x6bb/0x1090 worker_thread+0x45b/0xd10 kthread+0x2c8/0x3b0 Freed by task 10: kasan_save_stack+0x33/0x60 kasan_save_track+0x14/0x40 kasan_save_free_info+0x3b/0x60 __kasan_slab_free+0x57/0x80 kfree+0x1a5/0x370 device_release+0xc8/0x240 kobject_put+0x14d/0x280 hub_event+0x2519/0x41d0 process_one_work+0x6bb/0x1090 worker_thread+0x45b/0xd10 kthread+0x2c8/0x3b0 The buggy address belongs to the object at ffff888004d0e000 which belongs to the cache kmalloc-2k of size 2048 The buggy address is located 240 bytes inside of freed 2048-byte region [ffff888004d0e000, ffff888004d0e800)
The second case, at module removal:
BUG: KASAN: slab-use-after-free in do_rebind.isra.0+0x14d/0x180 [usbip_host] Read of size 8 at addr ffff888004ecb0f0 by task rmmod/123 Call Trace: <TASK> dump_stack_lvl+0x53/0x70 print_report+0xd0/0x630 kasan_report+0xce/0x100 do_rebind.isra.0+0x14d/0x180 [usbip_host] usbip_host_exit+0x123/0x4d0 [usbip_host] __do_sys_delete_module+0x346/0x510 do_syscall_64+0xdf/0x4c0 entry_SYSCALL_64_after_hwframe+0x77/0x7f </TASK>
I sent a driver-only fix: do_rebind() looks the device up by name with bus_find_device_by_name(), which holds a reference, and the saved pointer is removed. That is the lookup drivers_probe itself does, as do the driver core's bind and unbind attributes. It fixes the three cases above, and keeps "rebind" working for the usbip binaries and scripts already in use:
https://lore.kernel.org/all/[email protected]/
It doesn't cover everything raised in this thread. The invalid opcode Jeffin saw (bind, rebind, unbind) is the BUG() in stub_disconnect():rebind_store() removes the busid entry of a device still bound to usbip-host, and the unbind no longer finds it. I could reproduce it, with or without the patch:
kernel BUG at drivers/usb/usbip/stub_dev.c:439!
Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI RIP: 0010:stub_disconnect+0x2ae/0x300 [usbip_host] Call Trace: <TASK> usb_unbind_device+0x6b/0x1b0 device_release_driver_internal+0x32e/0x520 unbind_store+0xd1/0xf0 kernfs_fop_write_iter+0x329/0x4c0 vfs_write+0x5d2/0xcd0 ksys_write+0xf7/0x1c0 do_syscall_64+0xdf/0x4c0 entry_SYSCALL_64_after_hwframe+0x77/0x7f </TASK>
Refusing "rebind" while the device is bound could be a follow-up. We haven't hit that one ourselves.
This analysis, the fix and the reproducer were developed with an AI coding assistant (Claude Code), and I reviewed and tested them; the first oops above is from our own servers, the others from the test VM. I can share the test script and the full logs, or send the script as a selftest if that is useful.
Thanks,
Lucas