[PATCH] gpib: ni_usb: fix use-after-free in ni_usb_line_status()
From: YuanZhe Liu
Date: Mon Nov 16 2026 - 02:49:10 EST
ni_usb_line_status() caches the usb_device derived from
ni_priv->bus_interface and uses it for the two error messages that
follow the register read. The bus_interface check at the top of the
function only proves that the interface was still valid when the
pointer was taken: the bulk transfer can take up to a second, and
longer when the endpoint stalls, so the dongle can be disconnected
while the transfer is in flight. The USB core then frees the
usb_device and dev_err() reads freed memory:
BUG: KASAN: slab-use-after-free in dev_name include/linux/device.h:934
Read of size 8 at addr ffff8880088df100
Freed by task 11:
device_release
kobject_put
put_device
hub_port_connect
Call Trace:
_dev_err
ni_usb_line_status
iblines
general_ibstatus
ibwait
ibioctl
The gpib board and its device outlive the USB interface, so report the
transfer failure through board->gpib_dev and drop the local usb_device
pointer.
Fixes: 4934b98bb243 ("staging: gpib: Update messaging and usb_device refs in ni_usb")
Cc: stable@xxxxxxxxxxxxxxx
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Co-developed-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>
---
drivers/gpib/ni_usb/ni_usb_gpib.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/drivers/gpib/ni_usb/ni_usb_gpib.c b/drivers/gpib/ni_usb/ni_usb_gpib.c
index 28de1d543ad6..8e688509fdff 100644
--- a/drivers/gpib/ni_usb/ni_usb_gpib.c
+++ b/drivers/gpib/ni_usb/ni_usb_gpib.c
@@ -1516,7 +1516,6 @@ static int ni_usb_line_status(const struct gpib_board *board)
{
int retval;
struct ni_usb_priv *ni_priv = board->private_data;
- struct usb_device *usb_dev;
u8 *out_data, *in_data;
static const int out_data_length = 0x20;
static const int in_data_length = 0x20;
@@ -1528,7 +1527,6 @@ static int ni_usb_line_status(const struct gpib_board *board)
if (!ni_priv->bus_interface)
return -ENODEV;
- usb_dev = interface_to_usbdev(ni_priv->bus_interface);
out_data = kmalloc(out_data_length, GFP_KERNEL);
if (!out_data)
return -ENOMEM;
@@ -1550,7 +1548,7 @@ static int ni_usb_line_status(const struct gpib_board *board)
if (retval || bytes_written != i) {
mutex_unlock(&ni_priv->addressed_transfer_lock);
if (retval != -EAGAIN)
- dev_err(&usb_dev->dev, "send_bulk_msg returned %i, bytes_written=%i, i=%i\n",
+ dev_err(board->gpib_dev, "send_bulk_msg returned %i, bytes_written=%i, i=%i\n",
retval, bytes_written, i);
return retval;
}
@@ -1567,7 +1565,7 @@ static int ni_usb_line_status(const struct gpib_board *board)
if (retval) {
if (retval != -EAGAIN)
- dev_err(&usb_dev->dev, "receive_bulk_msg returned %i, bytes_read=%i\n",
+ dev_err(board->gpib_dev, "receive_bulk_msg returned %i, bytes_read=%i\n",
retval, bytes_read);
kfree(in_data);
return retval;
--
2.34.1