Skip to contentSkip to conversation

USB: make to_usb_driver() use container_of_const()

PATCH In review Greg Kroah-Hartman 2 participants 4 messages Nov 13, 2024 – Nov 14, 2024 USB

lore
Share & tools
lore
author maintainer
#1 lore raw

PATCH v2 2/2 USB: make to_usb_device_driver() use container_of_const()

In reply to Greg Kroah-Hartman

Turns out that we have some const pointers being passed to to_usb_device_driver() but were not catching this. Change the macro to properly propagate the const-ness of the pointer so that we will notice when we try to write to memory that we shouldn't be writing to.

This requires fixing up the usb_driver_applicable() function as well, because it can handle a const * to struct usb_driver.

Cc Johan Hovold <[email protected]>
Cc Alan Stern <[email protected]>
Cc Grant Grundler <[email protected]>
Cc Yajun Deng <[email protected]>
Cc Oliver Neukum <[email protected]>
Cc Douglas Anderson <[email protected]>

---
v2: reordered patches, goes on top of series submitted at https://lore.kernel.org/r/2024111322-kindly-finalist-d247@gregkh

 drivers/usb/core/driver.c | 4 ++--
 drivers/usb/core/usb.h    | 2 +-
 include/linux/usb.h       | 3 +--
 3 files changed, 4 insertions(+), 5 deletions(-)
drivers/usb/core/driver.c +2 -2
index bc5c561bdbd5..f203fdbfb6f6 100644
@@ -850,7 +850,7 @@ const struct usb_device_id *usb_device_match_id(struct usb_device *udev,
 EXPORT_SYMBOL_GPL(usb_device_match_id);
 
 bool usb_driver_applicable(struct usb_device *udev,
-			   struct usb_device_driver *udrv)
+			   const struct usb_device_driver *udrv)
 {
 	if (udrv->id_table && udrv->match)
 		return usb_device_match_id(udev, udrv->id_table) != NULL &&
@@ -870,7 +870,7 @@ static int usb_device_match(struct device *dev, const struct device_driver *drv)
 	/* devices and interfaces are handled separately */
 	if (is_usb_device(dev)) {
 		struct usb_device *udev;
-		struct usb_device_driver *udrv;
+		const struct usb_device_driver *udrv;
 
 		/* interface drivers never match devices */
 		if (!is_usb_device_driver(drv))
drivers/usb/core/usb.h +1 -1
index b8324ea05b20..a9b37aeb515b 100644
@@ -75,7 +75,7 @@ extern int usb_match_device(struct usb_device *dev,
 extern const struct usb_device_id *usb_device_match_id(struct usb_device *udev,
 				const struct usb_device_id *id);
 extern bool usb_driver_applicable(struct usb_device *udev,
-				  struct usb_device_driver *udrv);
+				  const struct usb_device_driver *udrv);
 extern void usb_forced_unbind_intf(struct usb_interface *intf);
 extern void usb_unbind_and_rebind_marked_interfaces(struct usb_device *udev);
 
include/linux/usb.h +1 -2
index 7a9e96f9d886..cfa8005e24f9 100644
@@ -1294,8 +1294,7 @@ struct usb_device_driver {
 	unsigned int supports_autosuspend:1;
 	unsigned int generic_subclass:1;
 };
-#define	to_usb_device_driver(d) container_of(d, struct usb_device_driver, \
-		driver)
+#define	to_usb_device_driver(d) container_of_const(d, struct usb_device_driver, driver)
 
 /**
  * struct usb_class_driver - identifies a USB driver that wants to use the USB major number
Signature · 1 line
2.47.0

Greg Kroah-Hartman

author maintainer
#0 lore raw

PATCH v2 1/2 USB: make to_usb_driver() use container_of_const()

Turns out that we have some const pointers being passed to to_usb_driver() but were not catching this. Change the macro to properly propagate the const-ness of the pointer so that we will notice when we try to write to memory that we shouldn't be writing to.

This requires fixing up the usb_match_dynamic_id() function as well, because it can handle a const * to struct usb_driver.

Cc Johan Hovold <[email protected]>
Cc Alan Stern <[email protected]>
Cc Grant Grundler <[email protected]>
Cc Yajun Deng <[email protected]>
Cc Oliver Neukum <[email protected]>
Cc Douglas Anderson <[email protected]>

---
v2: reordered patches, goes on top of series submitted at https://lore.kernel.org/r/2024111322-kindly-finalist-d247@gregkh

 drivers/usb/core/driver.c | 4 ++--
 include/linux/usb.h       | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/usb/core/driver.c +2 -2
index 9ea955a3d115..bc5c561bdbd5 100644
@@ -228,7 +228,7 @@ static void usb_free_dynids(struct usb_driver *usb_drv)
 }
 
 static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
-							struct usb_driver *drv)
+							const struct usb_driver *drv)
 {
 	struct usb_dynid *dynid;
 
@@ -890,7 +890,7 @@ static int usb_device_match(struct device *dev, const struct device_driver *drv)
 
 	} else if (is_usb_interface(dev)) {
 		struct usb_interface *intf;
-		struct usb_driver *usb_drv;
+		const struct usb_driver *usb_drv;
 		const struct usb_device_id *id;
 
 		/* device drivers never match interfaces */
include/linux/usb.h +1 -1
index b66b1af3e439..7a9e96f9d886 100644
@@ -1243,7 +1243,7 @@ struct usb_driver {
 	unsigned int disable_hub_initiated_lpm:1;
 	unsigned int soft_unbind:1;
 };
-#define	to_usb_driver(d) container_of(d, struct usb_driver, driver)
+#define	to_usb_driver(d) container_of_const(d, struct usb_driver, driver)
 
 /**
  * struct usb_device_driver - identifies USB device driver to usbcore
Signature · 1 line
2.47.0
Reviewed-by on v2 2/2
#2 lore raw

Re: [PATCH v2 2/2] USB: make to_usb_device_driver() use container_of_const()

In reply to Greg Kroah-Hartman

Hi,

Quoted 17 lines · Greg Kroah-Hartman

Turns out that we have some const pointers being passed to to_usb_device_driver() but were not catching this. Change the macro to properly propagate the const-ness of the pointer so that we will notice when we try to write to memory that we shouldn't be writing to.

This requires fixing up the usb_driver_applicable() function as well, because it can handle a const * to struct usb_driver.

Cc Johan Hovold <[email protected]>
Cc Alan Stern <[email protected]>
Cc Grant Grundler <[email protected]>
Cc Yajun Deng <[email protected]>
Cc Oliver Neukum <[email protected]>
Cc Douglas Anderson <[email protected]>
Quoted 7 lines

---
v2: reordered patches, goes on top of series submitted at https://lore.kernel.org/r/2024111322-kindly-finalist-d247@gregkh

 drivers/usb/core/driver.c | 4 ++--
 drivers/usb/core/usb.h    | 2 +-
 include/linux/usb.h       | 3 +--
 3 files changed, 4 insertions(+), 5 deletions(-)

This looks reasonable to me. Assuming that there are no compiler errors reported:

Reviewed-by on v2 1/2
#3 lore raw
In reply to Greg Kroah-Hartman

Hi,

Quoted 17 lines · Greg Kroah-Hartman

Turns out that we have some const pointers being passed to to_usb_driver() but were not catching this. Change the macro to properly propagate the const-ness of the pointer so that we will notice when we try to write to memory that we shouldn't be writing to.

This requires fixing up the usb_match_dynamic_id() function as well, because it can handle a const * to struct usb_driver.

Cc Johan Hovold <[email protected]>
Cc Alan Stern <[email protected]>
Cc Grant Grundler <[email protected]>
Cc Yajun Deng <[email protected]>
Cc Oliver Neukum <[email protected]>
Cc Douglas Anderson <[email protected]>
Quoted 6 lines

---
v2: reordered patches, goes on top of series submitted at https://lore.kernel.org/r/2024111322-kindly-finalist-d247@gregkh

 drivers/usb/core/driver.c | 4 ++--
 include/linux/usb.h       | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

This looks reasonable to me. Assuming that there are no compiler errors reported: