Skip to contentSkip to conversation

Fix format overflows

PATCH In review Jonathan Dieter 4 participants 16 messages Feb 20, 2017 – Mar 17, 2017 USB over IP driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Fix format overflows

The usbip userspace tools call sprintf()/snprintf() and don't check for the return value which can lead the paths to overflow, truncating the final file in the path.

More urgently, GCC 7 now warns that these aren't checked with -Wformat-overflow, and with -Werror enabled in configure.ac, that makes these tools unbuildable.

This patch fixes these problems by replacing sprintf() with snprintf() in one place and adding checks for the return value of snprintf().

---
 tools/usb/usbip/libsrc/usbip_common.c      |  8 +++++++-
 tools/usb/usbip/libsrc/usbip_host_common.c | 25 ++++++++++++++++++++-----
 2 files changed, 27 insertions(+), 6 deletions(-)
tools/usb/usbip/libsrc/usbip_common.c +7 -1
index ac73710..fc875ee 100644
@@ -215,9 +215,15 @@ int read_usb_interface(struct usbip_usb_device *udev, int i,
 		       struct usbip_usb_interface *uinf)
 {
 	char busid[SYSFS_BUS_ID_SIZE];
+	int size;
 	struct udev_device *sif;
 
-	sprintf(busid, "%s:%d.%d", udev->busid, udev->bConfigurationValue, i);
+	size = snprintf(busid, SYSFS_BUS_ID_SIZE, "%s:%d.%d",
+			udev->busid, udev->bConfigurationValue, i);
+	if (size >= SYSFS_BUS_ID_SIZE) {
+		err("busid length %i >= SYSFS_BUS_ID_SIZE", size);
+		return -1;
+	}
 
 	sif = udev_device_new_from_subsystem_sysname(udev_context, "usb", busid);
 	if (!sif) {
tools/usb/usbip/libsrc/usbip_host_common.c +20 -5
index 9d41522..690cd49 100644
@@ -40,13 +40,19 @@ struct udev *udev_context;
 static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
 {
 	char status_attr_path[SYSFS_PATH_MAX];
+	int size;
 	int fd;
 	int length;
 	char status;
 	int value = 0;
 
-	snprintf(status_attr_path, SYSFS_PATH_MAX, "%s/usbip_status",
-		 udev->path);
+	size = snprintf(status_attr_path, SYSFS_PATH_MAX, "%s/usbip_status",
+			udev->path);
+	if (size >= SYSFS_PATH_MAX) {
+		err("usbip_status path length %i >= SYSFS_PATH_MAX", size);
+		return -1;
+	}
+
 
 	fd = open(status_attr_path, O_RDONLY);
 	if (fd < 0) {
@@ -218,6 +224,7 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
 {
 	char attr_name[] = "usbip_sockfd";
 	char sockfd_attr_path[SYSFS_PATH_MAX];
+	int size;
 	char sockfd_buff[30];
 	int ret;
 
@@ -237,10 +244,18 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
 	}
 
 	/* only the first interface is true */
-	snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
-		 edev->udev.path, attr_name);
+	size = snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
+			edev->udev.path, attr_name);
+	if (size >= SYSFS_PATH_MAX) {
+		err("exported device path length %i >= SYSFS_PATH_MAX", size);
+		return -1;
+	}
 
-	snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
+	size = snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
+	if (size >= 30) {
+		err("socket length %i >= 30", size);
+		return -1;
+	}
 
 	ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
 				    strlen(sockfd_buff));
Signature · 1 line
2.9.3
#1 lore raw

PATCH 2/2 Fix implicit fallthrough warning

GCC 7 now warns when switch statements fall through implicitly, and with -Werror enabled in configure.ac, that makes these tools unbuildable.

We fix this by notifying the compiler that this particular case statement is meant to fall through.

---
 tools/usb/usbip/src/usbip.c | 2 ++
 1 file changed, 2 insertions(+)
tools/usb/usbip/src/usbip.c +2 -0
index d7599d9..73d8eee 100644
@@ -176,6 +176,8 @@ int main(int argc, char *argv[])
 			break;
 		case '?':
 			printf("usbip: invalid option\n");
+			/* Terminate after printing error */
+			/* FALLTHRU */
 		default:
 			usbip_usage();
 			goto out;
Signature · 1 line
2.9.3
#2 lore raw
In reply to Jonathan Dieter

Hi,

W dniu 2017-02-20 o 21:51, Jonathan Dieter pisze:

Quoted 23 lines

The usbip userspace tools call sprintf()/snprintf() and don't check for the return value which can lead the paths to overflow, truncating the final file in the path.

More urgently, GCC 7 now warns that these aren't checked with -Wformat-overflow, and with -Werror enabled in configure.ac, that makes these tools unbuildable.

This patch fixes these problems by replacing sprintf() with snprintf() in one place and adding checks for the return value of snprintf().

---
 tools/usb/usbip/libsrc/usbip_common.c      |  8 +++++++-
 tools/usb/usbip/libsrc/usbip_host_common.c | 25 ++++++++++++++++++++-----
 2 files changed, 27 insertions(+), 6 deletions(-)
tools/usb/usbip/libsrc/usbip_common.c +0 -0
index ac73710..fc875ee 100644

tools/usb/usbip/libsrc/usbip_common.c int read_usb_interface(struct usbip_usb_device *udev, int i, · line 221

Quoted 8 lines
tools/usb/usbip/libsrc/usbip_common.c +2 -1
@@ -215,9 +215,15 @@ int read_usb_interface(struct usbip_usb_device *udev, int i,
 		       struct usbip_usb_interface *uinf)
 {
 	char busid[SYSFS_BUS_ID_SIZE];
+	int size;
 	struct udev_device *sif;
 
-	sprintf(busid, "%s:%d.%d", udev->busid, udev->bConfigurationValue, i);
+	size = snprintf(busid, SYSFS_BUS_ID_SIZE, "%s:%d.%d",

why not sizeof(busid)?

tools/usb/usbip/libsrc/usbip_common.c int read_usb_interface(struct usbip_usb_device *udev, int i, · line 223

Quoted 2 lines
tools/usb/usbip/libsrc/usbip_common.c +2 -0
+			udev->busid, udev->bConfigurationValue, i);
+	if (size >= SYSFS_BUS_ID_SIZE) {

As above.

Quoted 13 lines
tools/usb/usbip/libsrc/usbip_common.c +3 -0
+		err("busid length %i >= SYSFS_BUS_ID_SIZE", size);
+		return -1;
+	}
 
 	sif = udev_device_new_from_subsystem_sysname(udev_context, "usb", busid);
 	if (!sif) {
tools/usb/usbip/libsrc/usbip_host_common.c +0 -0
index 9d41522..690cd49 100644
@@ -40,13 +40,19 @@ struct udev *udev_context;
 static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
 {

tools/usb/usbip/libsrc/usbip_host_common.c struct udev *udev_context; · line 49

Quoted 10 lines
tools/usb/usbip/libsrc/usbip_host_common.c +2 -2
 	char status_attr_path[SYSFS_PATH_MAX];
+	int size;
 	int fd;
 	int length;
 	char status;
 	int value = 0;
 
-	snprintf(status_attr_path, SYSFS_PATH_MAX, "%s/usbip_status",
-		 udev->path);
+	size = snprintf(status_attr_path, SYSFS_PATH_MAX, "%s/usbip_status",

The same here.

Quoted 18 lines
tools/usb/usbip/libsrc/usbip_host_common.c +7 -0
+			udev->path);
+	if (size >= SYSFS_PATH_MAX) {
+		err("usbip_status path length %i >= SYSFS_PATH_MAX", size);
+		return -1;
+	}
+
 
 	fd = open(status_attr_path, O_RDONLY);
 	if (fd < 0) {
@@ -218,6 +224,7 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
 {
 	char attr_name[] = "usbip_sockfd";
 	char sockfd_attr_path[SYSFS_PATH_MAX];
+	int size;
 	char sockfd_buff[30];
 	int ret;
 

tools/usb/usbip/libsrc/usbip_host_common.c int usbip_export_device(struct usbip_exported_device *edev, int sockfd) · line 249

Quoted 8 lines
tools/usb/usbip/libsrc/usbip_host_common.c +3 -2
@@ -237,10 +244,18 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
 	}
 
 	/* only the first interface is true */
-	snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
-		 edev->udev.path, attr_name);
+	size = snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
+			edev->udev.path, attr_name);
+	if (size >= SYSFS_PATH_MAX) {

hmmm this should be sizeof(sockfd_attr_path) not SYSFS_PATH_MAX

tools/usb/usbip/libsrc/usbip_host_common.c int usbip_export_device(struct usbip_exported_device *edev, int sockfd) · line 255

Quoted 7 lines
tools/usb/usbip/libsrc/usbip_host_common.c +5 -1
+		err("exported device path length %i >= SYSFS_PATH_MAX", size);
+		return -1;
+	}
 
-	snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
+	size = snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
+	if (size >= 30) {

Please don't hardcode such values in if. use sizeof() like one line above

tools/usb/usbip/libsrc/usbip_host_common.c int usbip_export_device(struct usbip_exported_device *edev, int sockfd) · line 261

Quoted 7 lines
tools/usb/usbip/libsrc/usbip_host_common.c +3 -0
+		err("socket length %i >= 30", size);
+		return -1;
+	}
 
 	ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
 				    strlen(sockfd_buff));

Best regards,

Signature · 3 lines
Krzysztof Opasiak
Samsung R&D Institute Poland
Samsung Electronics
#3 lore raw

Thanks for looking at this. One quick question before I put out version two with your corrections:

Quoted 45 lines · Krzysztof Opasiak

Hi,

W dniu 2017-02-20 o 21:51, Jonathan Dieter pisze:

Quoted 38 lines

The usbip userspace tools call sprintf()/snprintf() and don't check for
the return value which can lead the paths to overflow, truncating the
final file in the path.

More urgently, GCC 7 now warns that these aren't checked with -Wformat-overflow, and with -Werror enabled in configure.ac, that makes
these tools unbuildable.

This patch fixes these problems by replacing sprintf() with snprintf() in
one place and adding checks for the return value of snprintf().

---
  tools/usb/usbip/libsrc/usbip_common.c      |  8 +++++++- tools/usb/usbip/libsrc/usbip_host_common.c | 25
++++++++++++++++++++-----

  2 files changed, 27 insertions(+), 6 deletions(-)

tools/usb/usbip/libsrc/usbip_common.c +1 -0
index ac73710..fc875ee 100644
@@ -215,9 +215,15 @@ int read_usb_interface(struct usbip_usb_device
 *udev, int i,
   		       struct usbip_usb_interface *uinf)
   {
   	char busid[SYSFS_BUS_ID_SIZE];
+	int size;
   	struct udev_device *sif;

tools/usb/usbip/libsrc/usbip_common.c +0 -1
-	sprintf(busid, "%s:%d.%d", udev->busid, udev-
Quoted 1 line

bConfigurationValue, i);

tools/usb/usbip/libsrc/usbip_common.c int read_usb_interface(struct usbip_usb_device *udev, int i, · line 221

Quoted 1 line
tools/usb/usbip/libsrc/usbip_common.c +1 -0
+	size = snprintf(busid, SYSFS_BUS_ID_SIZE, "%s:%d.%d",


why not sizeof(busid)?

Quoted 7 lines

tools/usb/usbip/libsrc/usbip_common.c int read_usb_interface(struct usbip_usb_device *udev, int i, · line 223

Quoted 3 lines
tools/usb/usbip/libsrc/usbip_common.c +1 -0
+			udev->busid, udev->bConfigurationValue,

i);

tools/usb/usbip/libsrc/usbip_common.c +1 -0
+	if (size >= SYSFS_BUS_ID_SIZE) {


As above.

tools/usb/usbip/libsrc/usbip_common.c int read_usb_interface(struct usbip_usb_device *udev, int i, · line 224

Quoted 1 line
tools/usb/usbip/libsrc/usbip_common.c +1 -0
+		err("busid length %i >= SYSFS_BUS_ID_SIZE", size);

Should I also change the error messages to use real values? Ex:

		err("busid length %i >= %i", size, sizeof(busid));
Quoted 87 lines
Quoted 26 lines
tools/usb/usbip/libsrc/usbip_common.c +2 -0
+		return -1;
+	}


   sif = udev_device_new_from_subsystem_sysname(udev_context, "usb", busid);
   if (!sif) {

tools/usb/usbip/libsrc/usbip_host_common.c +0 -0
index 9d41522..690cd49 100644
@@ -40,13 +40,19 @@ struct udev *udev_context;

  static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
  {
   char status_attr_path[SYSFS_PATH_MAX];

tools/usb/usbip/libsrc/usbip_host_common.c +1 -0
+	int size;

   int fd;
   int length;
   char status;
   int value = 0;

tools/usb/usbip/libsrc/usbip_host_common.c +0 -1
-	snprintf(status_attr_path, SYSFS_PATH_MAX,

"%s/usbip_status",

tools/usb/usbip/libsrc/usbip_host_common.c +1 -1
-		 udev->path);
+	size = snprintf(status_attr_path, SYSFS_PATH_MAX,
Quoted 1 line

"%s/usbip_status",


The same here.

Quoted 29 lines
tools/usb/usbip/libsrc/usbip_host_common.c +3 -0
+			udev->path);
+	if (size >= SYSFS_PATH_MAX) {
+		err("usbip_status path length %i >=

SYSFS_PATH_MAX", size);

tools/usb/usbip/libsrc/usbip_host_common.c +3 -0
+		return -1;
+	}
+


   fd = open(status_attr_path, O_RDONLY);
   if (fd < 0) {

tools/usb/usbip/libsrc/usbip_host_common.c +1 -0
@@ -218,6 +224,7 @@ int usbip_export_device(struct
 usbip_exported_device *edev, int sockfd)
   {
   	char attr_name[] = "usbip_sockfd";
   	char sockfd_attr_path[SYSFS_PATH_MAX];
+	int size;
   	char sockfd_buff[30];
   	int ret;

tools/usb/usbip/libsrc/usbip_host_common.c +0 -0
@@ -237,10 +244,18 @@ int usbip_export_device(struct
 usbip_exported_device *edev, int sockfd)
   	}


   /* only the first interface is true */

tools/usb/usbip/libsrc/usbip_host_common.c +0 -1
-	snprintf(sockfd_attr_path, sizeof(sockfd_attr_path),

"%s/%s",

tools/usb/usbip/libsrc/usbip_host_common.c +1 -1
-		 edev->udev.path, attr_name);
+	size = snprintf(sockfd_attr_path,

sizeof(sockfd_attr_path), "%s/%s",

tools/usb/usbip/libsrc/usbip_host_common.c int usbip_export_device(struct usbip_exported_device *edev, int sockfd) · line 249

Quoted 2 lines
tools/usb/usbip/libsrc/usbip_host_common.c +2 -0
+			edev->udev.path, attr_name);
+	if (size >= SYSFS_PATH_MAX) {


hmmm this should be sizeof(sockfd_attr_path) not SYSFS_PATH_MAX

tools/usb/usbip/libsrc/usbip_host_common.c int usbip_export_device(struct usbip_exported_device *edev, int sockfd) · line 255

Quoted 10 lines
tools/usb/usbip/libsrc/usbip_host_common.c +1 -0
+		err("exported device path length %i >=

SYSFS_PATH_MAX", size);

tools/usb/usbip/libsrc/usbip_host_common.c +2 -0
+		return -1;
+	}

tools/usb/usbip/libsrc/usbip_host_common.c +0 -1
-	snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n",

sockfd);

tools/usb/usbip/libsrc/usbip_host_common.c +1 -0
+	size = snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n",

sockfd);

tools/usb/usbip/libsrc/usbip_host_common.c +1 -0
+	if (size >= 30) {


Please don't hardcode such values in if. use sizeof() like one line above

Quoted 7 lines
tools/usb/usbip/libsrc/usbip_host_common.c +3 -0
+		err("socket length %i >= 30", size);
+		return -1;
+	}


   ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff, strlen(sockfd_buff));


Best regards,

#4 lore raw
Quoted 10 lines · Jonathan Dieter
Quoted 5 lines · Krzysztof Opasiak

Hi,

W dniu 2017-02-20 o 21:51, Jonathan Dieter pisze:

Quoted 2 lines
(quoted diff) +1 -0
+		err("busid length %i >= SYSFS_BUS_ID_SIZE",

size);

Should I also change the error messages to use real values? Ex:

		err("busid length %i >= %i", size, sizeof(busid));

On further reflection, I do think changing the error messages to use sizeof() is cleaner.

If you disagree, please let me know. I don't have a strong feeling about it one way or the other.

The next revision uses sizeof() in the places you indicated and changes size to an unsigned int since we're now comparing it to the result of sizeof().

Thanks again,
Jonathan

#5 lore raw

PATCH v2 1/2 usbip: Fix-format-overflow

The usbip userspace tools call sprintf()/snprintf() and don't check for the return value which can lead the paths to overflow, truncating the final file in the path.

More urgently, GCC 7 now warns that these aren't checked with -Wformat-overflow, and with -Werror enabled in configure.ac, that makes these tools unbuildable.

This patch fixes these problems by replacing sprintf() with snprintf() in one place and adding checks for the return value of snprintf().

---
 tools/usb/usbip/libsrc/usbip_common.c      |  8 +++++++-
 tools/usb/usbip/libsrc/usbip_host_common.c | 25 ++++++++++++++++++++-----
 2 files changed, 27 insertions(+), 6 deletions(-)
tools/usb/usbip/libsrc/usbip_common.c +7 -1
index ac73710..01dd4b2 100644
@@ -215,9 +215,15 @@ int read_usb_interface(struct usbip_usb_device *udev, int i,
 		       struct usbip_usb_interface *uinf)
 {
 	char busid[SYSFS_BUS_ID_SIZE];
+	unsigned int size;
 	struct udev_device *sif;
 
-	sprintf(busid, "%s:%d.%d", udev->busid, udev->bConfigurationValue, i);
+	size = snprintf(busid, sizeof(busid), "%s:%d.%d",
+			udev->busid, udev->bConfigurationValue, i);
+	if (size >= sizeof(busid)) {
+		err("busid length %u >= %lu", size, sizeof(busid));
+		return -1;
+	}
 
 	sif = udev_device_new_from_subsystem_sysname(udev_context, "usb", busid);
 	if (!sif) {
tools/usb/usbip/libsrc/usbip_host_common.c +20 -5
index 9d41522..398c1fa 100644
@@ -40,13 +40,19 @@ struct udev *udev_context;
 static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
 {
 	char status_attr_path[SYSFS_PATH_MAX];
+	unsigned int size;
 	int fd;
 	int length;
 	char status;
 	int value = 0;
 
-	snprintf(status_attr_path, SYSFS_PATH_MAX, "%s/usbip_status",
-		 udev->path);
+	size = snprintf(status_attr_path, sizeof(status_attr_path), "%s/usbip_status",
+			udev->path);
+	if (size >= sizeof(status_attr_path)) {
+		err("usbip_status path length %u >= %lu", size, sizeof(status_attr_path));
+		return -1;
+	}
+
 
 	fd = open(status_attr_path, O_RDONLY);
 	if (fd < 0) {
@@ -218,6 +224,7 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
 {
 	char attr_name[] = "usbip_sockfd";
 	char sockfd_attr_path[SYSFS_PATH_MAX];
+	unsigned int size;
 	char sockfd_buff[30];
 	int ret;
 
@@ -237,10 +244,18 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
 	}
 
 	/* only the first interface is true */
-	snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
-		 edev->udev.path, attr_name);
+	size = snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
+			edev->udev.path, attr_name);
+	if (size >= sizeof(sockfd_attr_path)) {
+		err("exported device path length %u >= %lu", size, sizeof(sockfd_attr_path));
+		return -1;
+	}
 
-	snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
+	size = snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
+	if (size >= sizeof(sockfd_buff)) {
+		err("socket length %u >= %lu", size, sizeof(sockfd_buff));
+		return -1;
+	}
 
 	ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
 				    strlen(sockfd_buff));
Signature · 1 line
2.9.3
#6 lore raw

PATCH v2 2/2 usbip: Fix implicit fallthrough warning

GCC 7 now warns when switch statements fall through implicitly, and with -Werror enabled in configure.ac, that makes these tools unbuildable.

We fix this by notifying the compiler that this particular case statement is meant to fall through.

---
 tools/usb/usbip/src/usbip.c | 2 ++
 1 file changed, 2 insertions(+)
tools/usb/usbip/src/usbip.c +2 -0
index d7599d9..73d8eee 100644
@@ -176,6 +176,8 @@ int main(int argc, char *argv[])
 			break;
 		case '?':
 			printf("usbip: invalid option\n");
+			/* Terminate after printing error */
+			/* FALLTHRU */
 		default:
 			usbip_usage();
 			goto out;
Signature · 1 line
2.9.3
#7 lore raw

Re: [PATCH v2 1/2] usbip: Fix-format-overflow

In reply to Jonathan Dieter

Hi,

W dniu 2017-02-21 o 18:57, Jonathan Dieter pisze:

Quoted 23 lines

The usbip userspace tools call sprintf()/snprintf() and don't check for the return value which can lead the paths to overflow, truncating the final file in the path.

More urgently, GCC 7 now warns that these aren't checked with -Wformat-overflow, and with -Werror enabled in configure.ac, that makes these tools unbuildable.

This patch fixes these problems by replacing sprintf() with snprintf() in one place and adding checks for the return value of snprintf().

---
 tools/usb/usbip/libsrc/usbip_common.c      |  8 +++++++-
 tools/usb/usbip/libsrc/usbip_host_common.c | 25 ++++++++++++++++++++-----
 2 files changed, 27 insertions(+), 6 deletions(-)
tools/usb/usbip/libsrc/usbip_common.c +0 -0
index ac73710..01dd4b2 100644

tools/usb/usbip/libsrc/usbip_common.c int read_usb_interface(struct usbip_usb_device *udev, int i, · line 218

Quoted 4 lines
tools/usb/usbip/libsrc/usbip_common.c +1 -0
@@ -215,9 +215,15 @@ int read_usb_interface(struct usbip_usb_device *udev, int i,
 		       struct usbip_usb_interface *uinf)
 {
 	char busid[SYSFS_BUS_ID_SIZE];
+	unsigned int size;

I'm not really convinced to use unsigned here. snprintf() is declared to return signed integer so we should assume that some of its implementation may return negative error code. Any rationale to this instead of just doing a cast for comparsion but signed value to print error?

Best regards

Signature · 3 lines
Krzysztof Opasiak
Samsung R&D Institute Poland
Samsung Electronics
#8 lore raw

Re: [PATCH v2 1/2] usbip: Fix-format-overflow

Quoted 3 lines · Krzysztof Opasiak

Hi,

W dniu 2017-02-21 o 18:57, Jonathan Dieter pisze:

<snip>

Quoted 9 lines

Review

Quoted 2 lines

  char busid[SYSFS_BUS_ID_SIZE];

(quoted diff) +1 -0
+	unsigned int size;

I'm not really convinced to use unsigned here. snprintf() is declared to
return signed integer so we should assume that some of its implementation may return negative error code. Any rationale to this instead of just doing a cast for comparsion but signed value to print error?

No, you're absolutely right. I've fixed this and am sending the next revision now.

Jonathan

#9 lore raw

PATCH v3 1/2 usbip: Fix-format-overflow

In reply to Krzysztof Opasiak

The usbip userspace tools call sprintf()/snprintf() and don't check for the return value which can lead the paths to overflow, truncating the final file in the path.

More urgently, GCC 7 now warns that these aren't checked with -Wformat-overflow, and with -Werror enabled in configure.ac, that makes these tools unbuildable.

This patch fixes these problems by replacing sprintf() with snprintf() in one place and adding checks for the return value of snprintf().

---
Changes since v2
* Converted size back to int because snprintf may return < 0 if there's an error
* Expanded error messages to indicate failure may be because size < 0

 tools/usb/usbip/libsrc/usbip_common.c      |  8 +++++++-
 tools/usb/usbip/libsrc/usbip_host_common.c | 28 +++++++++++++++++++++++-----
 2 files changed, 30 insertions(+), 6 deletions(-)
tools/usb/usbip/libsrc/usbip_common.c +7 -1
index ac73710..66017d7 100644
@@ -215,9 +215,15 @@ int read_usb_interface(struct usbip_usb_device *udev, int i,
 		       struct usbip_usb_interface *uinf)
 {
 	char busid[SYSFS_BUS_ID_SIZE];
+	int size;
 	struct udev_device *sif;
 
-	sprintf(busid, "%s:%d.%d", udev->busid, udev->bConfigurationValue, i);
+	size = snprintf(busid, sizeof(busid), "%s:%d.%d",
+			udev->busid, udev->bConfigurationValue, i);
+	if (size < 0 || (unsigned int)size >= sizeof(busid)) {
+		err("busid length %i >= %lu or < 0", size, sizeof(busid));
+		return -1;
+	}
 
 	sif = udev_device_new_from_subsystem_sysname(udev_context, "usb", busid);
 	if (!sif) {
tools/usb/usbip/libsrc/usbip_host_common.c +23 -5
index 9d41522..6fb91d9 100644
@@ -40,13 +40,20 @@ struct udev *udev_context;
 static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
 {
 	char status_attr_path[SYSFS_PATH_MAX];
+	int size;
 	int fd;
 	int length;
 	char status;
 	int value = 0;
 
-	snprintf(status_attr_path, SYSFS_PATH_MAX, "%s/usbip_status",
-		 udev->path);
+	size = snprintf(status_attr_path, sizeof(status_attr_path),
+			"%s/usbip_status", udev->path);
+	if (size < 0 || (unsigned int)size >= sizeof(status_attr_path)) {
+		err("usbip_status path length %i >= %lu or < 0", size,
+		    sizeof(status_attr_path));
+		return -1;
+	}
+
 
 	fd = open(status_attr_path, O_RDONLY);
 	if (fd < 0) {
@@ -218,6 +225,7 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
 {
 	char attr_name[] = "usbip_sockfd";
 	char sockfd_attr_path[SYSFS_PATH_MAX];
+	int size;
 	char sockfd_buff[30];
 	int ret;
 
@@ -237,10 +245,20 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
 	}
 
 	/* only the first interface is true */
-	snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
-		 edev->udev.path, attr_name);
+	size = snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
+			edev->udev.path, attr_name);
+	if (size < 0 || (unsigned int)size >= sizeof(sockfd_attr_path)) {
+		err("exported device path length %i >= %lu or < 0", size,
+		    sizeof(sockfd_attr_path));
+		return -1;
+	}
 
-	snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
+	size = snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
+	if (size < 0 || (unsigned int)size >= sizeof(sockfd_buff)) {
+		err("socket length %i >= %lu or < 0", size,
+		    sizeof(sockfd_buff));
+		return -1;
+	}
 
 	ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
 				    strlen(sockfd_buff));
Signature · 1 line
2.9.3
#10 lore raw

PATCH v3 2/2 usbip: Fix implicit fallthrough warning

GCC 7 now warns when switch statements fall through implicitly, and with -Werror enabled in configure.ac, that makes these tools unbuildable.

We fix this by notifying the compiler that this particular case statement is meant to fall through.

---
 tools/usb/usbip/src/usbip.c | 2 ++
 1 file changed, 2 insertions(+)
tools/usb/usbip/src/usbip.c +2 -0
index d7599d9..73d8eee 100644
@@ -176,6 +176,8 @@ int main(int argc, char *argv[])
 			break;
 		case '?':
 			printf("usbip: invalid option\n");
+			/* Terminate after printing error */
+			/* FALLTHRU */
 		default:
 			usbip_usage();
 			goto out;
Signature · 1 line
2.9.3
#11 lore raw

PATCH v4 1/2 usbip: Fix-format-overflow

In reply to Jonathan Dieter

The usbip userspace tools call sprintf()/snprintf() and don't check for the return value which can lead the paths to overflow, truncating the final file in the path.

More urgently, GCC 7 now warns that these aren't checked with -Wformat-overflow, and with -Werror enabled in configure.ac, that makes these tools unbuildable.

This patch fixes these problems by replacing sprintf() with snprintf() in one place and adding checks for the return value of snprintf().

---
Changes since v3
* Cast sizeof to long unsigned when printing errors to fix building for 32-bit architectures

 tools/usb/usbip/libsrc/usbip_common.c      |  9 ++++++++-
 tools/usb/usbip/libsrc/usbip_host_common.c | 28 +++++++++++++++++++++++-----
 2 files changed, 31 insertions(+), 6 deletions(-)
tools/usb/usbip/libsrc/usbip_common.c +8 -1
index ac73710..1517a23 100644
@@ -215,9 +215,16 @@ int read_usb_interface(struct usbip_usb_device *udev, int i,
 		       struct usbip_usb_interface *uinf)
 {
 	char busid[SYSFS_BUS_ID_SIZE];
+	int size;
 	struct udev_device *sif;
 
-	sprintf(busid, "%s:%d.%d", udev->busid, udev->bConfigurationValue, i);
+	size = snprintf(busid, sizeof(busid), "%s:%d.%d",
+			udev->busid, udev->bConfigurationValue, i);
+	if (size < 0 || (unsigned int)size >= sizeof(busid)) {
+		err("busid length %i >= %lu or < 0", size,
+		    (long unsigned)sizeof(busid));
+		return -1;
+	}
 
 	sif = udev_device_new_from_subsystem_sysname(udev_context, "usb", busid);
 	if (!sif) {
tools/usb/usbip/libsrc/usbip_host_common.c +23 -5
index 9d41522..6ff7b60 100644
@@ -40,13 +40,20 @@ struct udev *udev_context;
 static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
 {
 	char status_attr_path[SYSFS_PATH_MAX];
+	int size;
 	int fd;
 	int length;
 	char status;
 	int value = 0;
 
-	snprintf(status_attr_path, SYSFS_PATH_MAX, "%s/usbip_status",
-		 udev->path);
+	size = snprintf(status_attr_path, sizeof(status_attr_path),
+			"%s/usbip_status", udev->path);
+	if (size < 0 || (unsigned int)size >= sizeof(status_attr_path)) {
+		err("usbip_status path length %i >= %lu or < 0", size,
+		    (long unsigned)sizeof(status_attr_path));
+		return -1;
+	}
+
 
 	fd = open(status_attr_path, O_RDONLY);
 	if (fd < 0) {
@@ -218,6 +225,7 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
 {
 	char attr_name[] = "usbip_sockfd";
 	char sockfd_attr_path[SYSFS_PATH_MAX];
+	int size;
 	char sockfd_buff[30];
 	int ret;
 
@@ -237,10 +245,20 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
 	}
 
 	/* only the first interface is true */
-	snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
-		 edev->udev.path, attr_name);
+	size = snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
+			edev->udev.path, attr_name);
+	if (size < 0 || (unsigned int)size >= sizeof(sockfd_attr_path)) {
+		err("exported device path length %i >= %lu or < 0", size,
+		    (long unsigned)sizeof(sockfd_attr_path));
+		return -1;
+	}
 
-	snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
+	size = snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
+	if (size < 0 || (unsigned int)size >= sizeof(sockfd_buff)) {
+		err("socket length %i >= %lu or < 0", size,
+		    (long unsigned)sizeof(sockfd_buff));
+		return -1;
+	}
 
 	ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
 				    strlen(sockfd_buff));
Signature · 1 line
2.9.3
#12 lore raw

PATCH v4 2/2 usbip: Fix implicit fallthrough warning

GCC 7 now warns when switch statements fall through implicitly, and with -Werror enabled in configure.ac, that makes these tools unbuildable.

We fix this by notifying the compiler that this particular case statement is meant to fall through.

---
 tools/usb/usbip/src/usbip.c | 2 ++
 1 file changed, 2 insertions(+)
tools/usb/usbip/src/usbip.c +2 -0
index d7599d9..73d8eee 100644
@@ -176,6 +176,8 @@ int main(int argc, char *argv[])
 			break;
 		case '?':
 			printf("usbip: invalid option\n");
+			/* Terminate after printing error */
+			/* FALLTHRU */
 		default:
 			usbip_usage();
 			goto out;
Signature · 1 line
2.9.3
#13 lore raw

Re: [PATCH v4 1/2] usbip: Fix-format-overflow

In reply to Jonathan Dieter
Quoted 11 lines · Jonathan Dieter

The usbip userspace tools call sprintf()/snprintf() and don't check for the return value which can lead the paths to overflow, truncating the final file in the path.

More urgently, GCC 7 now warns that these aren't checked with -Wformat-overflow, and with -Werror enabled in configure.ac, that makes these tools unbuildable.

This patch fixes these problems by replacing sprintf() with snprintf() in one place and adding checks for the return value of snprintf().

Quoted 6 lines
Quoted 2 lines

---
Changes since v3
 * Cast sizeof to long unsigned when printing errors to fix building for 32-bit architectures

Just wanted to check if there's anything else I need to do to make this ready?

Thanks,
Jonathan

maintainer Acked-by on v4 1/2
#14 lore raw

Re: [PATCH v4 1/2] usbip: Fix-format-overflow

In reply to Jonathan Dieter
Quoted 8 lines · Jonathan Dieter

The usbip userspace tools call sprintf()/snprintf() and don't check for the return value which can lead the paths to overflow, truncating the final file in the path.

More urgently, GCC 7 now warns that these aren't checked with -Wformat-overflow, and with -Werror enabled in configure.ac, that makes these tools unbuildable.

Quoted 5 lines

This patch fixes these problems by replacing sprintf() with snprintf() in one place and adding checks for the return value of snprintf().

Greg,

Please pick this up.

thanks,
-- Shuah

Quoted 91 lines

---
Changes since v3
* Cast sizeof to long unsigned when printing errors to fix building for 32-bit architectures

 tools/usb/usbip/libsrc/usbip_common.c      |  9 ++++++++-
 tools/usb/usbip/libsrc/usbip_host_common.c | 28 +++++++++++++++++++++++-----
 2 files changed, 31 insertions(+), 6 deletions(-)
tools/usb/usbip/libsrc/usbip_common.c +8 -1
index ac73710..1517a23 100644
@@ -215,9 +215,16 @@ int read_usb_interface(struct usbip_usb_device *udev, int i,
 		       struct usbip_usb_interface *uinf)
 {
 	char busid[SYSFS_BUS_ID_SIZE];
+	int size;
 	struct udev_device *sif;
 
-	sprintf(busid, "%s:%d.%d", udev->busid, udev->bConfigurationValue, i);
+	size = snprintf(busid, sizeof(busid), "%s:%d.%d",
+			udev->busid, udev->bConfigurationValue, i);
+	if (size < 0 || (unsigned int)size >= sizeof(busid)) {
+		err("busid length %i >= %lu or < 0", size,
+		    (long unsigned)sizeof(busid));
+		return -1;
+	}
 
 	sif = udev_device_new_from_subsystem_sysname(udev_context, "usb", busid);
 	if (!sif) {
tools/usb/usbip/libsrc/usbip_host_common.c +23 -5
index 9d41522..6ff7b60 100644
@@ -40,13 +40,20 @@ struct udev *udev_context;
 static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
 {
 	char status_attr_path[SYSFS_PATH_MAX];
+	int size;
 	int fd;
 	int length;
 	char status;
 	int value = 0;
 
-	snprintf(status_attr_path, SYSFS_PATH_MAX, "%s/usbip_status",
-		 udev->path);
+	size = snprintf(status_attr_path, sizeof(status_attr_path),
+			"%s/usbip_status", udev->path);
+	if (size < 0 || (unsigned int)size >= sizeof(status_attr_path)) {
+		err("usbip_status path length %i >= %lu or < 0", size,
+		    (long unsigned)sizeof(status_attr_path));
+		return -1;
+	}
+
 
 	fd = open(status_attr_path, O_RDONLY);
 	if (fd < 0) {
@@ -218,6 +225,7 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
 {
 	char attr_name[] = "usbip_sockfd";
 	char sockfd_attr_path[SYSFS_PATH_MAX];
+	int size;
 	char sockfd_buff[30];
 	int ret;
 
@@ -237,10 +245,20 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
 	}
 
 	/* only the first interface is true */
-	snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
-		 edev->udev.path, attr_name);
+	size = snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
+			edev->udev.path, attr_name);
+	if (size < 0 || (unsigned int)size >= sizeof(sockfd_attr_path)) {
+		err("exported device path length %i >= %lu or < 0", size,
+		    (long unsigned)sizeof(sockfd_attr_path));
+		return -1;
+	}
 
-	snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
+	size = snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
+	if (size < 0 || (unsigned int)size >= sizeof(sockfd_buff)) {
+		err("socket length %i >= %lu or < 0", size,
+		    (long unsigned)sizeof(sockfd_buff));
+		return -1;
+	}
 
 	ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
 				    strlen(sockfd_buff));
#15 lore raw

Re: [PATCH v4 1/2] usbip: Fix-format-overflow

Quoted 12 lines · Shuah Khan
Quoted 11 lines · Jonathan Dieter

The usbip userspace tools call sprintf()/snprintf() and don't check for the return value which can lead the paths to overflow, truncating the final file in the path.

More urgently, GCC 7 now warns that these aren't checked with -Wformat-overflow, and with -Werror enabled in configure.ac, that makes these tools unbuildable.

This patch fixes these problems by replacing sprintf() with snprintf() in one place and adding checks for the return value of snprintf().

Quoted 8 lines
Quoted 2 lines

Greg,

Please pick this up.

Thanks, still digging through USB patches...