]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
hwmon: (powerz) Fix use-after-free on USB disconnect
authorSanman Pradhan <psanman@juniper.net>
Fri, 10 Apr 2026 00:25:35 +0000 (00:25 +0000)
committerGuenter Roeck <linux@roeck-us.net>
Fri, 10 Apr 2026 15:28:18 +0000 (08:28 -0700)
After powerz_disconnect() frees the URB and releases the mutex, a
subsequent powerz_read() call can acquire the mutex and call
powerz_read_data(), which dereferences the freed URB pointer.

Fix by:
 - Setting priv->urb to NULL in powerz_disconnect() so that
   powerz_read_data() can detect the disconnected state.
 - Adding a !priv->urb check at the start of powerz_read_data()
   to return -ENODEV on a disconnected device.
 - Moving usb_set_intfdata() before hwmon registration so the
   disconnect handler can always find the priv pointer.

Fixes: 4381a36abdf1c ("hwmon: add POWER-Z driver")
Cc: stable@vger.kernel.org
Signed-off-by: Sanman Pradhan <psanman@juniper.net>
Link: https://lore.kernel.org/r/20260410002521.422645-2-sanman.pradhan@hpe.com
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
drivers/hwmon/powerz.c

index 4e663d5b4e330b540b1bead34fa5bf57c607e244..a75b941bd6e2fc2fb302607b506294c29b6e6feb 100644 (file)
@@ -108,6 +108,9 @@ static int powerz_read_data(struct usb_device *udev, struct powerz_priv *priv)
 {
        int ret;
 
+       if (!priv->urb)
+               return -ENODEV;
+
        priv->status = -ETIMEDOUT;
        reinit_completion(&priv->completion);
 
@@ -224,6 +227,8 @@ static int powerz_probe(struct usb_interface *intf,
        mutex_init(&priv->mutex);
        init_completion(&priv->completion);
 
+       usb_set_intfdata(intf, priv);
+
        hwmon_dev =
            devm_hwmon_device_register_with_info(parent, DRIVER_NAME, priv,
                                                 &powerz_chip_info, NULL);
@@ -232,8 +237,6 @@ static int powerz_probe(struct usb_interface *intf,
                return PTR_ERR(hwmon_dev);
        }
 
-       usb_set_intfdata(intf, priv);
-
        return 0;
 }
 
@@ -244,6 +247,7 @@ static void powerz_disconnect(struct usb_interface *intf)
        mutex_lock(&priv->mutex);
        usb_kill_urb(priv->urb);
        usb_free_urb(priv->urb);
+       priv->urb = NULL;
        mutex_unlock(&priv->mutex);
 }