Skip to contentSkip to conversation

usb: core: use sysfs_emit() instead of sprintf()

PATCH Superseded Sergey Shtylyov 2 participants 4 messages Feb 8, 2022 USB

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#1 lore raw
In reply to Sergey Shtylyov
Quoted 2 lines · Sergey Shtylyov

sprintf() (still used in the USB core for the sysfs output) is vulnerable to the buffer overflow.

Really? Where? If we have potential overflows, let's fix them as bug fixes and properly backport the fixes where needed.

If these really are just using the "old-style" functions instead, then that's something totally different and you should not say "vulnerable" if it really is not at all.

Quoted 4 lines

Use the new-fangled sysfs_emit() instead.

Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool.

You mean coccinelle, right? It's been checking for this for a while.

Also properly wrap your changelog at 72 columns please.

thanks,

greg k-h

#0 lore raw

PATCH 1/1 usb: core: use sysfs_emit() instead of sprintf()

sprintf() (still used in the USB core for the sysfs output) is vulnerable to the buffer overflow. Use the new-fangled sysfs_emit() instead.

Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool.

---
This patch is against the 'usb-next' branch of Greg KH's 'usb.git' repo.

 drivers/usb/core/endpoint.c        |   11 ++---
 drivers/usb/core/ledtrig-usbport.c |    3 -
 drivers/usb/core/port.c            |   11 ++---
 drivers/usb/core/sysfs.c           |   77 ++++++++++++++++++-------------------
 4 files changed, 53 insertions(+), 49 deletions(-)
drivers/usb/core/endpoint.c +6 -5
Index: usb/drivers/usb/core/endpoint.c
===================================================================
@@ -14,6 +14,7 @@
 #include <linux/kernel.h>
 #include <linux/spinlock.h>
 #include <linux/slab.h>
+#include <linux/sysfs.h>
 #include <linux/usb.h>
 #include "usb.h"
 
@@ -39,7 +40,7 @@ static ssize_t field##_show(struct devic
 			       char *buf)			\
 {								\
 	struct ep_device *ep = to_ep_device(dev);		\
-	return sprintf(buf, format_string, ep->desc->field);	\
+	return sysfs_emit(buf, format_string, ep->desc->field);	\
 }								\
 static DEVICE_ATTR_RO(field)
 
@@ -52,7 +53,7 @@ static ssize_t wMaxPacketSize_show(struc
 				   struct device_attribute *attr, char *buf)
 {
 	struct ep_device *ep = to_ep_device(dev);
-	return sprintf(buf, "%04x\n", usb_endpoint_maxp(ep->desc));
+	return sysfs_emit(buf, "%04x\n", usb_endpoint_maxp(ep->desc));
 }
 static DEVICE_ATTR_RO(wMaxPacketSize);
 
@@ -76,7 +77,7 @@ static ssize_t type_show(struct device *
 		type = "Interrupt";
 		break;
 	}
-	return sprintf(buf, "%s\n", type);
+	return sysfs_emit(buf, "%s\n", type);
 }
 static DEVICE_ATTR_RO(type);
 
@@ -95,7 +96,7 @@ static ssize_t interval_show(struct devi
 		interval /= 1000;
 	}
 
-	return sprintf(buf, "%d%cs\n", interval, unit);
+	return sysfs_emit(buf, "%d%cs\n", interval, unit);
 }
 static DEVICE_ATTR_RO(interval);
 
@@ -111,7 +112,7 @@ static ssize_t direction_show(struct dev
 		direction = "in";
 	else
 		direction = "out";
-	return sprintf(buf, "%s\n", direction);
+	return sysfs_emit(buf, "%s\n", direction);
 }
 static DEVICE_ATTR_RO(direction);
 
drivers/usb/core/ledtrig-usbport.c +2 -1
Index: usb/drivers/usb/core/ledtrig-usbport.c
===================================================================
@@ -10,6 +10,7 @@
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/slab.h>
+#include <linux/sysfs.h>
 #include <linux/usb.h>
 #include <linux/usb/of.h>
 
@@ -87,7 +88,7 @@ static ssize_t usbport_trig_port_show(st
 						      struct usbport_trig_port,
 						      attr);
 
-	return sprintf(buf, "%d\n", port->observed) + 1;
+	return sysfs_emit(buf, "%d\n", port->observed) + 1;
 }
 
 static ssize_t usbport_trig_port_store(struct device *dev,
drivers/usb/core/port.c +6 -5
Index: usb/drivers/usb/core/port.c
===================================================================
@@ -8,6 +8,7 @@
  */
 
 #include <linux/slab.h>
+#include <linux/sysfs.h>
 #include <linux/pm_qos.h>
 #include <linux/component.h>
 
@@ -22,7 +23,7 @@ static ssize_t location_show(struct devi
 {
 	struct usb_port *port_dev = to_usb_port(dev);
 
-	return sprintf(buf, "0x%08x\n", port_dev->location);
+	return sysfs_emit(buf, "0x%08x\n", port_dev->location);
 }
 static DEVICE_ATTR_RO(location);
 
@@ -47,7 +48,7 @@ static ssize_t connect_type_show(struct
 		break;
 	}
 
-	return sprintf(buf, "%s\n", result);
+	return sysfs_emit(buf, "%s\n", result);
 }
 static DEVICE_ATTR_RO(connect_type);
 
@@ -56,7 +57,7 @@ static ssize_t over_current_count_show(s
 {
 	struct usb_port *port_dev = to_usb_port(dev);
 
-	return sprintf(buf, "%u\n", port_dev->over_current_count);
+	return sysfs_emit(buf, "%u\n", port_dev->over_current_count);
 }
 static DEVICE_ATTR_RO(over_current_count);
 
@@ -65,7 +66,7 @@ static ssize_t quirks_show(struct device
 {
 	struct usb_port *port_dev = to_usb_port(dev);
 
-	return sprintf(buf, "%08x\n", port_dev->quirks);
+	return sysfs_emit(buf, "%08x\n", port_dev->quirks);
 }
 
 static ssize_t quirks_store(struct device *dev, struct device_attribute *attr,
@@ -100,7 +101,7 @@ static ssize_t usb3_lpm_permit_show(stru
 			p = "0";
 	}
 
-	return sprintf(buf, "%s\n", p);
+	return sysfs_emit(buf, "%s\n", p);
 }
 
 static ssize_t usb3_lpm_permit_store(struct device *dev,
drivers/usb/core/sysfs.c +39 -38

299 lines not on this page · raw message

#2 lore raw
In reply to Greg Kroah-Hartman
Quoted 5 lines · Greg Kroah-Hartman
Quoted 2 lines

sprintf() (still used in the USB core for the sysfs output) is vulnerable to the buffer overflow.

Really? Where? If we have potential overflows, let's fix them as bug fixes and properly backport the fixes where needed.

I must admit I didn't found any real overflows in my quick triage...

Quoted 3 lines

If these really are just using the "old-style" functions instead, then that's something totally different and you should not say "vulnerable" if it really is not at all.

Isn't sprint() generally considered harmful? :-)

Quoted 6 lines
Quoted 4 lines

Use the new-fangled sysfs_emit() instead.

Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool.

You mean coccinelle, right?

Do you think coccinelle is the only code analyzer in this world? :-) I told you I was using SVACE (made by Russian Institute of the System Programming).

Quoted 3 lines

It's been checking for this for a while.

Also properly wrap your changelog at 72 columns please.

Well, checkpatch.pl was content. :-)

Quoted 3 lines

thanks,

greg k-h

MBR, Sergey

#3 lore raw
Quoted 9 lines · Sergey Shtylyov
Quoted 5 lines · Greg Kroah-Hartman
Quoted 2 lines

sprintf() (still used in the USB core for the sysfs output) is vulnerable to the buffer overflow.

Really? Where? If we have potential overflows, let's fix them as bug fixes and properly backport the fixes where needed.

I must admit I didn't found any real overflows in my quick triage...

Then please do not scare people by saying otherwise.

Quoted 6 lines
Quoted 4 lines

If these really are just using the "old-style" functions instead, then that's something totally different and you should not say "vulnerable" if it really is not at all.

Isn't sprint() generally considered harmful? :-)

For sysfs files that have a known size (PAGE_SIZE) with a single value like this, no, it's not harmful.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Use the new-fangled sysfs_emit() instead.

Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool.

You mean coccinelle, right?

Do you think coccinelle is the only code analyzer in this world? :-)

No, but it has a built-in rule for this already, why not just use that to find these types of things?

Quoted 1 line

I told you I was using SVACE (made by Russian Institute of the System Programming).

Nice, where is the rule for this with that tool?

thanks,

greg k-h