Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1753621ybl; Sat, 11 Jan 2020 02:08:06 -0800 (PST) X-Google-Smtp-Source: APXvYqxb3eLB4dJQS0pDle6mR9R2zzdvf6mP3AE1jQIbOKUE5HspnHRUina9f5JkLUB6pCfrDqJM X-Received: by 2002:a54:4117:: with SMTP id l23mr5539482oic.140.1578737286281; Sat, 11 Jan 2020 02:08:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578737286; cv=none; d=google.com; s=arc-20160816; b=rE1skwvW59O0A8c/fCLSQy4Es0qiHUWNMWYrSMIKD2r+lYPLuzMP/Vj+L5vLqKkasy AXzJ4GmVJQYQFv8GsndI3ljk76zUX5GFw3m0UsncINUTeRCb+of54ImgABqD5k0thWxJ 2rcfNBp/9DUKQyDvsyz7ZJpQQ5zQOE/C8+mTAfXu0tkq0+mARqUtgb+3i8WTN6ypmYg4 s8Vv/sDNjHKOt2rVy2ZApkY2RzVBpW43TtZcntCJK5s0p6REjYdSdKroLYk8MWUpY3F/ 7Tr04WcMQy36WKlaxHIanYIWSzc0h/SKp6a78i/q+eAij5BFzR9B1S+VpoEd3z2n8B/U gfog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=IiUCHRgI3NJqIUmWPQphLYt/P6PgORO7i3FXKKh6nM4=; b=vMnQCVhDRjaRi/cGnHNSdwHowv9Wzt/RX/5TNVo2CJE0HwtCehLmJKkDDMdR/2u6GR qQu+ucBaXqGhBxmWebEuOR7hrtZrq/NjRyP24upe6Hk1rqM4qniSgsbY8m+w9EXAOokN rUfpynen0fZ8bp13sBGBRO8Shbr5HrHbgODEZpLlTdK0wD9m25B540jefdTq8hDHdA47 c40zA3xVr5GhVVRZqlWlw/+AMsJ/0DizASyQTE+WqnKPykNNp0tcvyRxlwWu1u7mWVFG bnictGfp7FATcoc8qnenuTkOVaKRfiCF8oEtpmDZUFvWffSw8xaxwF2SguCF2ogyw1Xe B4tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=zljjoApY; 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 p11si3211447ota.300.2020.01.11.02.07.55; Sat, 11 Jan 2020 02:08:06 -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; dkim=pass header.i=@kernel.org header.s=default header.b=zljjoApY; 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 S1729473AbgAKKGC (ORCPT + 99 others); Sat, 11 Jan 2020 05:06:02 -0500 Received: from mail.kernel.org ([198.145.29.99]:39544 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729277AbgAKKGB (ORCPT ); Sat, 11 Jan 2020 05:06:01 -0500 Received: from localhost (unknown [62.119.166.9]) (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 32E232064C; Sat, 11 Jan 2020 10:05:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578737160; bh=4QdQTm/Dkx3w4kDSPsXzEvYSoyZn+BOOipbeN+G3DwM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zljjoApYNi8cfpGKrIg7N9pLKB6mQcUuysMFXUYLDBrnKcv2ilIJ7cXMGsQK9SgK1 79yzbePA7KeljPOhJ4BCHgKjuRat+JD2nwV5N2iPk1VCxFlE35AKx6L2rMm0IdNwh5 zVEA6UbwYqxz6yHcwJExrLD8Z6HrM8JZxXp9mSM0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Johan Hovold , Alan Stern Subject: [PATCH 4.9 90/91] USB: core: fix check for duplicate endpoints Date: Sat, 11 Jan 2020 10:50:23 +0100 Message-Id: <20200111094913.478733512@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200111094844.748507863@linuxfoundation.org> References: <20200111094844.748507863@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Johan Hovold commit 3e4f8e21c4f27bcf30a48486b9dcc269512b79ff upstream. Amend the endpoint-descriptor sanity checks to detect all duplicate endpoint addresses in a configuration. Commit 0a8fd1346254 ("USB: fix problems with duplicate endpoint addresses") added a check for duplicate endpoint addresses within a single alternate setting, but did not look for duplicate addresses in other interfaces. The current check would also not detect all duplicate addresses when one endpoint is as a (bi-directional) control endpoint. This specifically avoids overwriting the endpoint entries in struct usb_device when enabling a duplicate endpoint, something which could potentially lead to crashes or leaks, for example, when endpoints are later disabled. Cc: stable Signed-off-by: Johan Hovold Acked-by: Alan Stern Link: https://lore.kernel.org/r/20191219161016.6695-1-johan@kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/usb/core/config.c | 70 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 12 deletions(-) --- a/drivers/usb/core/config.c +++ b/drivers/usb/core/config.c @@ -198,9 +198,58 @@ static const unsigned short super_speed_ [USB_ENDPOINT_XFER_INT] = 1024, }; -static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum, - int asnum, struct usb_host_interface *ifp, int num_ep, - unsigned char *buffer, int size) +static bool endpoint_is_duplicate(struct usb_endpoint_descriptor *e1, + struct usb_endpoint_descriptor *e2) +{ + if (e1->bEndpointAddress == e2->bEndpointAddress) + return true; + + if (usb_endpoint_xfer_control(e1) || usb_endpoint_xfer_control(e2)) { + if (usb_endpoint_num(e1) == usb_endpoint_num(e2)) + return true; + } + + return false; +} + +/* + * Check for duplicate endpoint addresses in other interfaces and in the + * altsetting currently being parsed. + */ +static bool config_endpoint_is_duplicate(struct usb_host_config *config, + int inum, int asnum, struct usb_endpoint_descriptor *d) +{ + struct usb_endpoint_descriptor *epd; + struct usb_interface_cache *intfc; + struct usb_host_interface *alt; + int i, j, k; + + for (i = 0; i < config->desc.bNumInterfaces; ++i) { + intfc = config->intf_cache[i]; + + for (j = 0; j < intfc->num_altsetting; ++j) { + alt = &intfc->altsetting[j]; + + if (alt->desc.bInterfaceNumber == inum && + alt->desc.bAlternateSetting != asnum) + continue; + + for (k = 0; k < alt->desc.bNumEndpoints; ++k) { + epd = &alt->endpoint[k].desc; + + if (endpoint_is_duplicate(epd, d)) + return true; + } + } + } + + return false; +} + +static int usb_parse_endpoint(struct device *ddev, int cfgno, + struct usb_host_config *config, int inum, int asnum, + struct usb_host_interface *ifp, int num_ep, + unsigned char *buffer, int size) { unsigned char *buffer0 = buffer; struct usb_endpoint_descriptor *d; @@ -237,13 +286,10 @@ static int usb_parse_endpoint(struct dev goto skip_to_next_endpoint_or_interface_descriptor; /* Check for duplicate endpoint addresses */ - for (i = 0; i < ifp->desc.bNumEndpoints; ++i) { - if (ifp->endpoint[i].desc.bEndpointAddress == - d->bEndpointAddress) { - dev_warn(ddev, "config %d interface %d altsetting %d has a duplicate endpoint with address 0x%X, skipping\n", - cfgno, inum, asnum, d->bEndpointAddress); - goto skip_to_next_endpoint_or_interface_descriptor; - } + if (config_endpoint_is_duplicate(config, inum, asnum, d)) { + dev_warn(ddev, "config %d interface %d altsetting %d has a duplicate endpoint with address 0x%X, skipping\n", + cfgno, inum, asnum, d->bEndpointAddress); + goto skip_to_next_endpoint_or_interface_descriptor; } endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints]; @@ -517,8 +563,8 @@ static int usb_parse_interface(struct de if (((struct usb_descriptor_header *) buffer)->bDescriptorType == USB_DT_INTERFACE) break; - retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt, - num_ep, buffer, size); + retval = usb_parse_endpoint(ddev, cfgno, config, inum, asnum, + alt, num_ep, buffer, size); if (retval < 0) return retval; ++n;