]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
usb: Add checks for snprintf() calls in usb_alloc_dev()
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Fri, 21 Mar 2025 16:49:49 +0000 (18:49 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sun, 6 Jul 2025 08:57:56 +0000 (10:57 +0200)
[ Upstream commit 82fe5107fa3d21d6c3fba091c9dbc50495588630 ]

When creating a device path in the driver the snprintf() takes
up to 16 characters long argument along with the additional up to
12 characters for the signed integer (as it can't see the actual limits)
and tries to pack this into 16 bytes array. GCC complains about that
when build with `make W=1`:

  drivers/usb/core/usb.c:705:25: note: ‘snprintf’ output between 3 and 28 bytes into a destination of size 16

Since everything works until now, let's just check for the potential
buffer overflow and bail out. It is most likely a never happen situation,
but at least it makes GCC happy.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://lore.kernel.org/r/20250321164949.423957-1-andriy.shevchenko@linux.intel.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/usb/core/usb.c

index 3500e3c94c4b8784265c656dca54f7a7ee37bedb..2215f8ca54170e81394e2286dc7975c50e4275f8 100644 (file)
@@ -704,15 +704,16 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,
                dev_set_name(&dev->dev, "usb%d", bus->busnum);
                root_hub = 1;
        } else {
+               int n;
+
                /* match any labeling on the hubs; it's one-based */
                if (parent->devpath[0] == '0') {
-                       snprintf(dev->devpath, sizeof dev->devpath,
-                               "%d", port1);
+                       n = snprintf(dev->devpath, sizeof(dev->devpath), "%d", port1);
                        /* Root ports are not counted in route string */
                        dev->route = 0;
                } else {
-                       snprintf(dev->devpath, sizeof dev->devpath,
-                               "%s.%d", parent->devpath, port1);
+                       n = snprintf(dev->devpath, sizeof(dev->devpath), "%s.%d",
+                                    parent->devpath, port1);
                        /* Route string assumes hubs have less than 16 ports */
                        if (port1 < 15)
                                dev->route = parent->route +
@@ -721,6 +722,11 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,
                                dev->route = parent->route +
                                        (15 << ((parent->level - 1)*4));
                }
+               if (n >= sizeof(dev->devpath)) {
+                       usb_put_hcd(bus_to_hcd(bus));
+                       usb_put_dev(dev);
+                       return NULL;
+               }
 
                dev->dev.parent = &parent->dev;
                dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);