Re: [PATCH 1/2] Fix format overflows
From: Jonathan Dieter
Date: Tue Feb 21 2017 - 01:50:41 EST
Thanks for looking at this. One quick question before I put out
version two with your corrections:
On Tue, 2017-02-21 at 07:12 +0100, Krzysztof Opasiak wrote:
> Hi,
>Â
> W dniu 2017-02-20 o 21:51, Jonathan Dieter pisze:
> > 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@xxxxxxxxx>
> > Signed-off-by: Jonathan Dieter <jdieter@xxxxxxxxx>
> > ---
> >ÂÂtools/usb/usbip/libsrc/usbip_common.cÂÂÂÂÂÂ|ÂÂ8 +++++++-
> >ÂÂtools/usb/usbip/libsrc/usbip_host_common.c | 25
> > ++++++++++++++++++++-----
> >ÂÂ2 files changed, 27 insertions(+), 6 deletions(-)
> >Â
> > diff --git a/tools/usb/usbip/libsrc/usbip_common.c
> > b/tools/usb/usbip/libsrc/usbip_common.c
> > index ac73710..fc875ee 100644
> > --- a/tools/usb/usbip/libsrc/usbip_common.c
> > +++ b/tools/usb/usbip/libsrc/usbip_common.c
> > @@ -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)?
>Â
> > + udev->busid, udev->bConfigurationValue,
> > i);
> > + if (size >= SYSFS_BUS_ID_SIZE) {
>Â
> As above.
>Â
> > + 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));
> > + return -1;
> > + }
> >Â
> >ÂÂ sif = udev_device_new_from_subsystem_sysname(udev_context,
> > "usb", busid);
> >ÂÂ if (!sif) {
> > diff --git a/tools/usb/usbip/libsrc/usbip_host_common.c
> > b/tools/usb/usbip/libsrc/usbip_host_common.c
> > index 9d41522..690cd49 100644
> > --- a/tools/usb/usbip/libsrc/usbip_host_common.c
> > +++ b/tools/usb/usbip/libsrc/usbip_host_common.c
> > @@ -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",
>Â
> The same here.
>Â
> > + 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) {
>Â
> hmmm this should be sizeof(sockfd_attr_path) not 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) {
>Â
> Please don't hardcode such values in if. use sizeof() like one line
> above
>Â
> > + err("socket length %i >= 30", size);
> > + return -1;
> > + }
> >Â
> >ÂÂ ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
> >ÂÂ ÂÂÂÂstrlen(sockfd_buff));
> >Â
>Â
> Best regards,