Received: by 10.223.185.116 with SMTP id b49csp826184wrg; Sat, 10 Feb 2018 21:18:58 -0800 (PST) X-Google-Smtp-Source: AH8x226ygJV63U6DKRQVPYzo1n/4ahymXL4ovJ/BKoBCTZy4h5sZF4J0llVL6Es9K1XzaKivBePN X-Received: by 10.99.36.65 with SMTP id k62mr6510627pgk.345.1518326338598; Sat, 10 Feb 2018 21:18:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518326338; cv=none; d=google.com; s=arc-20160816; b=dkn4+fWGdJPko21+tpCkEJW0Ula1vE23SoIYMyShDajujjcjUQOgUgEjNLhcWFzvnm 64/6fu2+JulIL/y0q5SnhwzHhec4QqmwKXff4erpmNHY5ViDkahiwl+6Iqeg6F27p0A+ ElJlWzxBeWdReLpQKWnXc2zrjFR6Zq+GM5S76eq8CcQ9dg1dSTMCoaoPDK5ARqf7ggnR MrSUdV0apodfGBOmrfbiyszAqU4G3ySrpATnQzTTvT5O/h/z3USGeOUS8WZHFzM9FInF eJ83WOJFdY8ViFYIEyVWxalfI1G0q0Wbz+BGM/cf7Fe+1vdUncfK1RoXGR9lU8urGvzK N+vw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=jNEuAdImRNXMINPc8UWXj1UhsmqtxGHzdq6asQyUDK0=; b=vBypfCrIau4xRfJgj4VtD79T0BiFadVKMAdASusFjxUrnJ43aZ4h1v5iw3Cb598Dsr LReYb61kh9fGCXPjQLmw/tOi1YWp5Em1HNSYfAWq7YRKpm0EnbQT7qIwWVQaT8x0Z5FG MbRHA7+cwNfQwp6I9JPYhLRPSdrn+408KnLQy0KLAj9YPHJK4uv9L7e3grnrjqFHjw+j u6NMN+WtBou4aFrhaIHH8ScQzjCqFzo6/+xXPefSBpXfZ8K8PgaF8uallv36K/LK68xP G4bDMTRLTkIoHp9MVzAEKSsN+E5fcgmEVsgvyVVeOuDaE5Kowyn3IGuMol0bqjmSrCuv it6w== 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 f34-v6si4029427ple.102.2018.02.10.21.18.44; Sat, 10 Feb 2018 21:18:58 -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 S1753397AbeBKFOR (ORCPT + 99 others); Sun, 11 Feb 2018 00:14:17 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:41393 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752621AbeBKEdl (ORCPT ); Sat, 10 Feb 2018 23:33:41 -0500 Received: from [2a02:8011:400e:2:6f00:88c8:c921:d332] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ekjKc-0002hI-Tg; Sun, 11 Feb 2018 04:33:38 +0000 Received: from ben by deadeye with local (Exim 4.90) (envelope-from ) id 1ekjKY-0004XO-Uz; Sun, 11 Feb 2018 04:33:34 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Secunia Research" , "Shuah Khan" , "Greg Kroah-Hartman" Date: Sun, 11 Feb 2018 04:20:06 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.2 74/79] usbip: fix stub_rx: harden CMD_SUBMIT path to handle malicious input In-Reply-To: X-SA-Exim-Connect-IP: 2a02:8011:400e:2:6f00:88c8:c921:d332 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.2.99-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Shuah Khan commit c6688ef9f29762e65bce325ef4acd6c675806366 upstream. Harden CMD_SUBMIT path to handle malicious input that could trigger large memory allocations. Add checks to validate transfer_buffer_length and number_of_packets to protect against bad input requesting for unbounded memory allocations. Validate early in get_pipe() and return failure. Reported-by: Secunia Research Signed-off-by: Shuah Khan Signed-off-by: Greg Kroah-Hartman [bwh: Backported to 3.2: - Device for logging purposes is &sdev->interface->dev - Adjust filename, context] Signed-off-by: Ben Hutchings --- drivers/staging/usbip/stub_rx.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) --- a/drivers/staging/usbip/stub_rx.c +++ b/drivers/staging/usbip/stub_rx.c @@ -350,11 +350,13 @@ static struct stub_priv *stub_priv_alloc return priv; } -static int get_pipe(struct stub_device *sdev, int epnum, int dir) +static int get_pipe(struct stub_device *sdev, struct usbip_header *pdu) { struct usb_device *udev = sdev->udev; struct usb_host_endpoint *ep; struct usb_endpoint_descriptor *epd = NULL; + int epnum = pdu->base.ep; + int dir = pdu->base.direction; if (epnum < 0 || epnum > 15) goto err_ret; @@ -367,6 +369,15 @@ static int get_pipe(struct stub_device * goto err_ret; epd = &ep->desc; + + /* validate transfer_buffer_length */ + if (pdu->u.cmd_submit.transfer_buffer_length > INT_MAX) { + dev_err(&sdev->interface->dev, + "CMD_SUBMIT: -EMSGSIZE transfer_buffer_length %d\n", + pdu->u.cmd_submit.transfer_buffer_length); + return -1; + } + if (usb_endpoint_xfer_control(epd)) { if (dir == USBIP_DIR_OUT) return usb_sndctrlpipe(udev, epnum); @@ -389,6 +400,21 @@ static int get_pipe(struct stub_device * } if (usb_endpoint_xfer_isoc(epd)) { + /* validate packet size and number of packets */ + unsigned int maxp, packets, bytes; + + maxp = usb_endpoint_maxp(epd); + maxp *= usb_endpoint_maxp_mult(epd); + bytes = pdu->u.cmd_submit.transfer_buffer_length; + packets = DIV_ROUND_UP(bytes, maxp); + + if (pdu->u.cmd_submit.number_of_packets < 0 || + pdu->u.cmd_submit.number_of_packets > packets) { + dev_err(&sdev->interface->dev, + "CMD_SUBMIT: isoc invalid num packets %d\n", + pdu->u.cmd_submit.number_of_packets); + return -1; + } if (dir == USBIP_DIR_OUT) return usb_sndisocpipe(udev, epnum); else @@ -397,7 +423,7 @@ static int get_pipe(struct stub_device * err_ret: /* NOT REACHED */ - dev_err(&sdev->interface->dev, "get pipe() invalid epnum %d\n", epnum); + dev_err(&sdev->interface->dev, "CMD_SUBMIT: invalid epnum %d\n", epnum); return -1; } @@ -462,7 +488,7 @@ static void stub_recv_cmd_submit(struct struct stub_priv *priv; struct usbip_device *ud = &sdev->ud; struct usb_device *udev = sdev->udev; - int pipe = get_pipe(sdev, pdu->base.ep, pdu->base.direction); + int pipe = get_pipe(sdev, pdu); if (pipe == -1) return; @@ -485,7 +511,8 @@ static void stub_recv_cmd_submit(struct } /* set priv->urb->transfer_buffer */ - if (pdu->u.cmd_submit.transfer_buffer_length > 0) { + if (pdu->u.cmd_submit.transfer_buffer_length > 0 && + pdu->u.cmd_submit.transfer_buffer_length <= INT_MAX) { priv->urb->transfer_buffer = kzalloc(pdu->u.cmd_submit.transfer_buffer_length, GFP_KERNEL);