Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usbip: Fix potential format overflow in userspace tools

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().

Reviewed-by: Peter Senna Tschudin <peter.senna@gmail.com>
Signed-off-by: Jonathan Dieter <jdieter@lesbg.com>
Acked-by: Shuah Khan <shuahkh@osg.samsung.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Jonathan Dieter and committed by
Greg Kroah-Hartman
e5dfa3f9 2c93e790

+31 -6
+8 -1
tools/usb/usbip/libsrc/usbip_common.c
··· 215 215 struct usbip_usb_interface *uinf) 216 216 { 217 217 char busid[SYSFS_BUS_ID_SIZE]; 218 + int size; 218 219 struct udev_device *sif; 219 220 220 - sprintf(busid, "%s:%d.%d", udev->busid, udev->bConfigurationValue, i); 221 + size = snprintf(busid, sizeof(busid), "%s:%d.%d", 222 + udev->busid, udev->bConfigurationValue, i); 223 + if (size < 0 || (unsigned int)size >= sizeof(busid)) { 224 + err("busid length %i >= %lu or < 0", size, 225 + (long unsigned)sizeof(busid)); 226 + return -1; 227 + } 221 228 222 229 sif = udev_device_new_from_subsystem_sysname(udev_context, "usb", busid); 223 230 if (!sif) {
+23 -5
tools/usb/usbip/libsrc/usbip_host_common.c
··· 40 40 static int32_t read_attr_usbip_status(struct usbip_usb_device *udev) 41 41 { 42 42 char status_attr_path[SYSFS_PATH_MAX]; 43 + int size; 43 44 int fd; 44 45 int length; 45 46 char status; 46 47 int value = 0; 47 48 48 - snprintf(status_attr_path, SYSFS_PATH_MAX, "%s/usbip_status", 49 - udev->path); 49 + size = snprintf(status_attr_path, sizeof(status_attr_path), 50 + "%s/usbip_status", udev->path); 51 + if (size < 0 || (unsigned int)size >= sizeof(status_attr_path)) { 52 + err("usbip_status path length %i >= %lu or < 0", size, 53 + (long unsigned)sizeof(status_attr_path)); 54 + return -1; 55 + } 56 + 50 57 51 58 fd = open(status_attr_path, O_RDONLY); 52 59 if (fd < 0) { ··· 225 218 { 226 219 char attr_name[] = "usbip_sockfd"; 227 220 char sockfd_attr_path[SYSFS_PATH_MAX]; 221 + int size; 228 222 char sockfd_buff[30]; 229 223 int ret; 230 224 ··· 245 237 } 246 238 247 239 /* only the first interface is true */ 248 - snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s", 249 - edev->udev.path, attr_name); 240 + size = snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s", 241 + edev->udev.path, attr_name); 242 + if (size < 0 || (unsigned int)size >= sizeof(sockfd_attr_path)) { 243 + err("exported device path length %i >= %lu or < 0", size, 244 + (long unsigned)sizeof(sockfd_attr_path)); 245 + return -1; 246 + } 250 247 251 - snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd); 248 + size = snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd); 249 + if (size < 0 || (unsigned int)size >= sizeof(sockfd_buff)) { 250 + err("socket length %i >= %lu or < 0", size, 251 + (long unsigned)sizeof(sockfd_buff)); 252 + return -1; 253 + } 252 254 253 255 ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff, 254 256 strlen(sockfd_buff));