Received: by 10.223.185.116 with SMTP id b49csp2454819wrg; Thu, 15 Feb 2018 11:50:27 -0800 (PST) X-Google-Smtp-Source: AH8x224JYBD7oPoRyivYKQZWwG7loPIPc973z1hfN436WxNMz3+fh5rSZMBZc/iZxEzQg3+nnpUB X-Received: by 10.101.97.209 with SMTP id j17mr3050043pgv.266.1518724227806; Thu, 15 Feb 2018 11:50:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518724227; cv=none; d=google.com; s=arc-20160816; b=Nfa9/F7WHjn3JTA6Gu9dlcIjcq6/wqqK4DcKT1ic98gwCy54NoNDumGC8rLDwCHM8H M9x01kBpE/6ujejoeD/MhvR5Ceo5D7tfrDvUyuZMsanIhcLLnGqdSAoRTjLFjish6BZG luJkaByjSNdtDDCjhLBGuOj+tutiPkQ6giOwrRjKKu+HZbbBMysSj4cetEqZMJJC7T/L jgCKiIPZzQ8iP1gSGnxHHyyXtaYD50zr+H7YxQZ8ZKPSPQgOu2nwpURCWhL5+kjSHEpU i29i4zTgTJ46d+O8bBG6SDFeu1okVeaUVWPOgTiTarI2DwVy9SwH87yzR9h65Pj5Vcxb zw5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=JIlB3Nmg79zaUckVekJm8ehOAf3yqqBwMDeNliVestY=; b=zOraB0SyXgwYiaU+EOhZGUSSgTbOBV4Fe7MUpjhMLKJv2RvFYzW6L3sowb+JxIb9YG p8gOquVbvaBTnxL0KH6Z+tvo6jcVwfMQvv7YLGFN4zY/qMCiVHY/uNfriQhnZYAv+gv4 713Crjkn2CnHWbinMpylHJw5xTmOcrI5FeWX60baT38zrmM+Bi2Z7PMOAeuA8Wg8xqaB DHstKAP4vd56QZuDEZVi5iWkG1Sl/JgSqUM0lXuDb0Kdx1gqCAbbc/wsWEJR38A7plLT 0ijvIIbrq/KIZTPsw7gFckbxli/jS1Z4GralX8zemufZ7rdfv7HsMaJeensmfiv1Vbmq 96Sw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k69si4998352pfc.296.2018.02.15.11.50.12; Thu, 15 Feb 2018 11:50:27 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1161762AbeBOPUc (ORCPT + 99 others); Thu, 15 Feb 2018 10:20:32 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:48356 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161735AbeBOPU3 (ORCPT ); Thu, 15 Feb 2018 10:20:29 -0500 Received: from localhost (LFbn-1-12258-90.w90-92.abo.wanadoo.fr [90.92.71.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 0115B1096; Thu, 15 Feb 2018 15:20:28 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Peter Senna Tschudin , Jonathan Dieter , Shuah Khan Subject: [PATCH 3.18 18/45] usbip: Fix potential format overflow in userspace tools Date: Thu, 15 Feb 2018 16:17:09 +0100 Message-Id: <20180215144119.706452573@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180215144115.863307741@linuxfoundation.org> References: <20180215144115.863307741@linuxfoundation.org> User-Agent: quilt/0.65 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jonathan Dieter commit e5dfa3f902b9a642ae8c6997d57d7c41e384a90b upstream. 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 Signed-off-by: Jonathan Dieter Acked-by: Shuah Khan Signed-off-by: Greg Kroah-Hartman --- tools/usb/usbip/libsrc/usbip_common.c | 9 ++++++++- tools/usb/usbip/libsrc/usbip_host_driver.c | 27 ++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) --- a/tools/usb/usbip/libsrc/usbip_common.c +++ b/tools/usb/usbip/libsrc/usbip_common.c @@ -215,9 +215,16 @@ int read_usb_interface(struct usbip_usb_ 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, + (unsigned long)sizeof(busid)); + return -1; + } sif = udev_device_new_from_subsystem_sysname(udev_context, "usb", busid); if (!sif) { --- a/tools/usb/usbip/libsrc/usbip_host_driver.c +++ b/tools/usb/usbip/libsrc/usbip_host_driver.c @@ -39,13 +39,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 < 0 || (unsigned int)size >= sizeof(status_attr_path)) { + err("usbip_status path length %i >= %lu or < 0", size, + (unsigned long)sizeof(status_attr_path)); + return -1; + } fd = open(status_attr_path, O_RDONLY); if (fd < 0) { @@ -225,6 +231,7 @@ int usbip_host_export_device(struct usbi { char attr_name[] = "usbip_sockfd"; char sockfd_attr_path[SYSFS_PATH_MAX]; + int size; char sockfd_buff[30]; int ret; @@ -244,10 +251,20 @@ int usbip_host_export_device(struct usbi } /* 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, + (unsigned long)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, + (unsigned long)sizeof(sockfd_buff)); + return -1; + } ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff, strlen(sockfd_buff));