Received: by 10.223.176.5 with SMTP id f5csp1136979wra; Wed, 7 Feb 2018 13:22:43 -0800 (PST) X-Google-Smtp-Source: AH8x224jX+1VGSsNJ5SBkU5GEUaKzd7wZTcki3GMARshIWJdGVKCes8n+3Uv6c4oxI9sDRp2RWCg X-Received: by 10.99.149.4 with SMTP id p4mr4630980pgd.0.1518038563103; Wed, 07 Feb 2018 13:22:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518038563; cv=none; d=google.com; s=arc-20160816; b=QjtjlNCKSrCWoYKTMQrntl5MD+zZjagYyF4wkFytP0Hflc7Bmg2B0q9x4V5o9MObUI 29jXrfnBrDxwfQMv/Fe8Wndq2SfRQK9z2LZ59BY2zzUhO5ymMvrEEtbo+6vZ5xj0dYU4 d8GcRtEJ6AhBmj7Br4TIFktkDmS8wpLBOXEncn6gjWeQ95X9vrF1ttuzXQQ2igu3qpkq mT4CIEAFF/cSxW+jRbtZPLVUkGIe2TcktyqbtWq1wJQk8ZgFvFi2m9UGJpSxqUKOjiH0 Htd7rksNQAr7x6AW+aKPBy5507wbjkeYeiKnTHIUNe+dOmEV7/5DJ/dY1hZKAUk8V5ap npmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=AWN3makXYS74miFhAUp/vP8hKS6Dzik5VvNDw6MhWHY=; b=vba5YhyWoJmEkml02inaJLrrIlFi1fSPNY9y6kH75HTEHhri5heXrsCxab5in1yX0N oDqHkc5d3CWp+ka90amSIc+qgte/qPfkI8+lawaLtTu+31YzT/vz7+UkaAd39lDMeR62 XXI4ksdutUPB7m6bB2ncg90OpmKyd+Q3fM/5RFKX2EIcr0G7cd+pdhKgg/eJi4B9RRl5 EuLD+nJYpahnxO5+Pdpe6QC3dfymVIA9I1upbt178m7iOoPW+cMDxVAQhgqB85+pD0m0 zryByHa+TP8pAHgVehsgAaFxBxe0+K4WX6hB9ekixo0USP0kkq9kAPjtaBtVRoNAYpKR rsmw== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r13-v6si1652992pls.308.2018.02.07.13.22.29; Wed, 07 Feb 2018 13:22:43 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932336AbeBGVVH (ORCPT + 99 others); Wed, 7 Feb 2018 16:21:07 -0500 Received: from osg.samsung.com ([64.30.133.232]:53567 "EHLO osg.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932276AbeBGVVB (ORCPT ); Wed, 7 Feb 2018 16:21:01 -0500 Received: from localhost (localhost [127.0.0.1]) by osg.samsung.com (Postfix) with ESMTP id 1FE2C1B28E; Wed, 7 Feb 2018 13:21:01 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at dev.s-opensource.com X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from osg.samsung.com ([127.0.0.1]) by localhost (localhost [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id kl1GJgCZXVLY; Wed, 7 Feb 2018 13:20:56 -0800 (PST) Received: from localhost.localdomain (c-24-9-64-241.hsd1.co.comcast.net [24.9.64.241]) by osg.samsung.com (Postfix) with ESMTPSA id 6CF011B269; Wed, 7 Feb 2018 13:20:55 -0800 (PST) From: Shuah Khan To: valentina.manea.m@gmail.com, shuah@kernel.org, gregkh@linuxfoundation.org Cc: Shuah Khan , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH 3.18 2/9] usbip: fix stub_rx: harden CMD_SUBMIT path to handle malicious input Date: Wed, 7 Feb 2018 14:20:25 -0700 Message-Id: <48094d82a68e9bb52f4463d46a8b0aa259c15a1c.1518036867.git.shuahkh@osg.samsung.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Upstream commit c6688ef9f297 ("usbip: fix stub_rx: harden CMD_SUBMIT path to handle malicious input") 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 Cc: stable Signed-off-by: Shuah Khan Signed-off-by: Greg Kroah-Hartman --- drivers/usb/usbip/stub_rx.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/drivers/usb/usbip/stub_rx.c b/drivers/usb/usbip/stub_rx.c index 2e07acda456e..f5533c99cd48 100644 --- a/drivers/usb/usbip/stub_rx.c +++ b/drivers/usb/usbip/stub_rx.c @@ -341,11 +341,13 @@ static struct stub_priv *stub_priv_alloc(struct stub_device *sdev, 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; @@ -358,6 +360,7 @@ static int get_pipe(struct stub_device *sdev, int epnum, int dir) goto err_ret; epd = &ep->desc; + if (usb_endpoint_xfer_control(epd)) { if (dir == USBIP_DIR_OUT) return usb_sndctrlpipe(udev, epnum); @@ -380,6 +383,27 @@ static int get_pipe(struct stub_device *sdev, int epnum, int dir) } if (usb_endpoint_xfer_isoc(epd)) { + /* validate packet size and number of packets */ + unsigned int maxp, packets, bytes; + +#define USB_EP_MAXP_MULT_SHIFT 11 +#define USB_EP_MAXP_MULT_MASK (3 << USB_EP_MAXP_MULT_SHIFT) +#define USB_EP_MAXP_MULT(m) \ + (((m) & USB_EP_MAXP_MULT_MASK) >> USB_EP_MAXP_MULT_SHIFT) + + maxp = usb_endpoint_maxp(epd); + maxp *= (USB_EP_MAXP_MULT( + __le16_to_cpu(epd->wMaxPacketSize)) + 1); + 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->udev->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 @@ -388,7 +412,7 @@ static int get_pipe(struct stub_device *sdev, int epnum, int dir) err_ret: /* NOT REACHED */ - dev_err(&sdev->interface->dev, "get pipe, epnum %d\n", epnum); + dev_err(&sdev->udev->dev, "CMD_SUBMIT: invalid epnum %d\n", epnum); return -1; } @@ -453,7 +477,7 @@ static void stub_recv_cmd_submit(struct stub_device *sdev, 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; -- 2.14.1