提交 e5dfa3f9 编写于 作者: J Jonathan Dieter 提交者: Greg Kroah-Hartman

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: NPeter Senna Tschudin <peter.senna@gmail.com>
Signed-off-by: NJonathan Dieter <jdieter@lesbg.com>
Acked-by: NShuah Khan <shuahkh@osg.samsung.com>
Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
上级 2c93e790
...@@ -215,9 +215,16 @@ int read_usb_interface(struct usbip_usb_device *udev, int i, ...@@ -215,9 +215,16 @@ int read_usb_interface(struct usbip_usb_device *udev, int i,
struct usbip_usb_interface *uinf) struct usbip_usb_interface *uinf)
{ {
char busid[SYSFS_BUS_ID_SIZE]; char busid[SYSFS_BUS_ID_SIZE];
int size;
struct udev_device *sif; 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); sif = udev_device_new_from_subsystem_sysname(udev_context, "usb", busid);
if (!sif) { if (!sif) {
......
...@@ -40,13 +40,20 @@ struct udev *udev_context; ...@@ -40,13 +40,20 @@ struct udev *udev_context;
static int32_t read_attr_usbip_status(struct usbip_usb_device *udev) static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
{ {
char status_attr_path[SYSFS_PATH_MAX]; char status_attr_path[SYSFS_PATH_MAX];
int size;
int fd; int fd;
int length; int length;
char status; char status;
int value = 0; int value = 0;
snprintf(status_attr_path, SYSFS_PATH_MAX, "%s/usbip_status", size = snprintf(status_attr_path, sizeof(status_attr_path),
udev->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); fd = open(status_attr_path, O_RDONLY);
if (fd < 0) { if (fd < 0) {
...@@ -218,6 +225,7 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd) ...@@ -218,6 +225,7 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
{ {
char attr_name[] = "usbip_sockfd"; char attr_name[] = "usbip_sockfd";
char sockfd_attr_path[SYSFS_PATH_MAX]; char sockfd_attr_path[SYSFS_PATH_MAX];
int size;
char sockfd_buff[30]; char sockfd_buff[30];
int ret; int ret;
...@@ -237,10 +245,20 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd) ...@@ -237,10 +245,20 @@ int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
} }
/* only the first interface is true */ /* only the first interface is true */
snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s", size = snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
edev->udev.path, attr_name); 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, ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
strlen(sockfd_buff)); strlen(sockfd_buff));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册