Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2174914imm; Mon, 28 May 2018 03:14:38 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrwvOH7RbLou9LQU6brSq5EZo1fFlpWpVLHK9Sghr+W1w6LUeXOYs8/vTNKOc8Pca1jF1BO X-Received: by 2002:a17:902:8308:: with SMTP id bd8-v6mr13088162plb.195.1527502478772; Mon, 28 May 2018 03:14:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527502478; cv=none; d=google.com; s=arc-20160816; b=VeoPbNcAQGpwCQc9MFF/tReyXipGT9wneZIcxBbLbbV5L2f8WmCU2Tm6NZwG3xTChk Xac6QWmRzRDVjsIJHgIQHFzZ3G8J8w44P2xbXuE5fINoEf9mfAcdgkFCPrTlUL0s8e7X HCiDZQU8BC5ytpAaUzK5beBfEpoH+WUjulln7713HEX/AzSItlRlVZbBdDW+h3nflGld 6ZOd/C9aIR+GTfYl4ciuqWJSKKgKMbM3aIUiRYdl70FhN/TFeKDUdCfopkhC2RtTJ2Zk qBJ5SehnwtQxCyQAJdYy5gboYCK95EgwOjuNsX+IgsMXWxzTEadkMK8PLd/ZUcOFGK+x u/jg== 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:dkim-signature :arc-authentication-results; bh=qMvSp4Tx0s6MdDW9ibwirEUG78SIx3gwb2W70apmtSg=; b=mhxzWMQg3uWZbIFiWypsUWuKKh/gGHuE1Ds8gEf17s9A0uux0uKGdNSbbEEE4gK468 F1fd963YNXezSwxaPxo5U7F4LXgIWkl14okdsoJeMWicyw9qvGp41o9HzmrzpXgpLTzX GMeQgFAUwCC7y2BZhu99skrv7h1eybYnWApy9v7L+FkwK+hv3yly12ntdmcPl4QCLxiH w2lNAqPIySBPeHJ4aJbHJGD0DC5tI6Gb5Q/IdDbGDNDt31JHYBZ4zDecoq4YhBtW1BVb PBLB/0VV2j+MDr/JDchXHU0gF7qISqwLYyfeCPWG7ZiDsL0Gn0Lk64YWUgFQsQALGNjL AR1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=knc2/wK5; 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 d124-v6si30036431pfc.176.2018.05.28.03.14.24; Mon, 28 May 2018 03:14:38 -0700 (PDT) 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; dkim=pass header.i=@kernel.org header.s=default header.b=knc2/wK5; 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 S965272AbeE1KM5 (ORCPT + 99 others); Mon, 28 May 2018 06:12:57 -0400 Received: from mail.kernel.org ([198.145.29.99]:60816 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965045AbeE1KMm (ORCPT ); Mon, 28 May 2018 06:12:42 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8FB832086D; Mon, 28 May 2018 10:12:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527502362; bh=EBNgDWGt/1Pmqt5+GIawo0aCbeJQOKS3RpqaeOi3eXc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=knc2/wK59psciT/ZZ7TtfSM0FFCb4fDdVILkQF/pSG2IDzPpwBnIEY+49vcLZxdcx DGCy0nJuWiuaVt9/d3Pkf6PyoF8EtLlfg+u6TG8bnwHQ3UzvrlZ3/Suy8LtaJwpRzs 8y9lLX7WPOanyc/zN7QYAvStPvjqKnHrmq6yY4+Y= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Chris Dickens , Felipe Balbi , Sasha Levin Subject: [PATCH 3.18 158/185] usb: gadget: composite: fix incorrect handling of OS desc requests Date: Mon, 28 May 2018 12:03:19 +0200 Message-Id: <20180528100111.098562252@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100050.700971285@linuxfoundation.org> References: <20180528100050.700971285@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review 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: Chris Dickens [ Upstream commit 5d6ae4f0da8a64a185074dabb1b2f8c148efa741 ] When handling an OS descriptor request, one of the first operations is to zero out the request buffer using the wLength from the setup packet. There is no bounds checking, so a wLength > 4096 would clobber memory adjacent to the request buffer. Fix this by taking the min of wLength and the request buffer length prior to the memset. While at it, define the buffer length in a header file so that magic numbers don't appear throughout the code. When returning data to the host, the data length should be the min of the wLength and the valid data we have to return. Currently we are returning wLength, thus requests for a wLength greater than the amount of data in the OS descriptor buffer would return invalid (albeit zero'd) data following the valid descriptor data. Fix this by counting the number of bytes when constructing the data and using this when determining the length of the request. Signed-off-by: Chris Dickens Signed-off-by: Felipe Balbi Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- drivers/usb/gadget/composite.c | 40 +++++++++++++++++++--------------------- include/linux/usb/composite.h | 3 +++ 2 files changed, 22 insertions(+), 21 deletions(-) --- a/drivers/usb/gadget/composite.c +++ b/drivers/usb/gadget/composite.c @@ -1279,7 +1279,7 @@ static int count_ext_compat(struct usb_c return res; } -static void fill_ext_compat(struct usb_configuration *c, u8 *buf) +static int fill_ext_compat(struct usb_configuration *c, u8 *buf) { int i, count; @@ -1306,10 +1306,12 @@ static void fill_ext_compat(struct usb_c buf += 23; } count += 24; - if (count >= 4096) - return; + if (count + 24 >= USB_COMP_EP0_OS_DESC_BUFSIZ) + return count; } } + + return count; } static int count_ext_prop(struct usb_configuration *c, int interface) @@ -1354,25 +1356,20 @@ static int fill_ext_prop(struct usb_conf struct usb_os_desc *d; struct usb_os_desc_ext_prop *ext_prop; int j, count, n, ret; - u8 *start = buf; f = c->interface[interface]; + count = 10; /* header length */ for (j = 0; j < f->os_desc_n; ++j) { if (interface != f->os_desc_table[j].if_id) continue; d = f->os_desc_table[j].os_desc; if (d) list_for_each_entry(ext_prop, &d->ext_prop, entry) { - /* 4kB minus header length */ - n = buf - start; - if (n >= 4086) - return 0; - - count = ext_prop->data_len + + n = ext_prop->data_len + ext_prop->name_len + 14; - if (count > 4086 - n) - return -EINVAL; - usb_ext_prop_put_size(buf, count); + if (count + n >= USB_COMP_EP0_OS_DESC_BUFSIZ) + return count; + usb_ext_prop_put_size(buf, n); usb_ext_prop_put_type(buf, ext_prop->type); ret = usb_ext_prop_put_name(buf, ext_prop->name, ext_prop->name_len); @@ -1398,11 +1395,12 @@ static int fill_ext_prop(struct usb_conf default: return -EINVAL; } - buf += count; + buf += n; + count += n; } } - return 0; + return count; } /* @@ -1630,6 +1628,7 @@ unknown: req->complete = composite_setup_complete; buf = req->buf; os_desc_cfg = cdev->os_desc_config; + w_length = min_t(u16, w_length, USB_COMP_EP0_OS_DESC_BUFSIZ); memset(buf, 0, w_length); buf[5] = 0x01; switch (ctrl->bRequestType & USB_RECIP_MASK) { @@ -1653,8 +1652,8 @@ unknown: count += 16; /* header */ put_unaligned_le32(count, buf); buf += 16; - fill_ext_compat(os_desc_cfg, buf); - value = w_length; + value = fill_ext_compat(os_desc_cfg, buf); + value = min_t(u16, w_length, value); } break; case USB_RECIP_INTERFACE: @@ -1683,8 +1682,7 @@ unknown: interface, buf); if (value < 0) return value; - - value = w_length; + value = min_t(u16, w_length, value); } break; } @@ -1938,8 +1936,8 @@ int composite_os_desc_req_prepare(struct goto end; } - /* OS feature descriptor length <= 4kB */ - cdev->os_desc_req->buf = kmalloc(4096, GFP_KERNEL); + cdev->os_desc_req->buf = kmalloc(USB_COMP_EP0_OS_DESC_BUFSIZ, + GFP_KERNEL); if (!cdev->os_desc_req->buf) { ret = PTR_ERR(cdev->os_desc_req->buf); kfree(cdev->os_desc_req); --- a/include/linux/usb/composite.h +++ b/include/linux/usb/composite.h @@ -53,6 +53,9 @@ /* big enough to hold our biggest descriptor */ #define USB_COMP_EP0_BUFSIZ 1024 +/* OS feature descriptor length <= 4kB */ +#define USB_COMP_EP0_OS_DESC_BUFSIZ 4096 + #define USB_MS_TO_HS_INTERVAL(x) (ilog2((x * 1000 / 125)) + 1) struct usb_configuration;