Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp138153rwl; Thu, 30 Mar 2023 13:25:31 -0700 (PDT) X-Google-Smtp-Source: AKy350aE8F14jnIsX1ERmL4Sfev/5Grtuq6DKUt8Cgc5Ktd9lqaYSVlg3xnNARumWuHYZRDL32Co X-Received: by 2002:a17:906:7390:b0:947:8734:a058 with SMTP id f16-20020a170906739000b009478734a058mr1473515ejl.4.1680207931508; Thu, 30 Mar 2023 13:25:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680207931; cv=none; d=google.com; s=arc-20160816; b=u7Sy9cRYPQR3fuvCYGsgGXgYKRflb/2tBU8nVxcSl5CZwPdtvbOBa0FI73nzYy0Vx5 YjZ2kCUs+QDsyNxN9yVWsWq6fLY+IVVcWdiRl+aOGRzSbKj704IGRydrgfx2BtNLqa8C JkKFFTCJ0oOM1hLOfIMmd5B067LIXG9AAsoa2efQ5prxtLkc3hXhzSZwX+g+h2FNagqK FqxIVPaDOrd4iKraeZJfTgrflEDoq3iMwSP9xnnVqmVvFTVy8uKvMbUhX1+q4efOke1q qelozAduxQfm+jzGzNN+yh3W830X860mcaHihXSzYWDUv6EjLRUg96FdvGJgYZwP1tJU G+PQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=WvZRQ5S/1RoeLie2AtZ6ZELwylsx0UCFWzW74Vbp7QY=; b=Iv+39NLXJTORXcFQbSugAlWSNW3DV2KPGdqtzP0Fymh212QZ0D2XjuJpXmoAHYm2Jp OXN9HEU6eb74SBpSATHyQa4wEkiKB64hmT99PHJre0B/EQufRTnyscqIhVRRS+QHkMg1 egseekMpnkRrgwbVeAf7iUeM+paSA+hQFulE5l9lmVHovG3mbXhbGOsgGfZSiZsik/kK LHmoP8rogxuvj4VU6L1KxKit/FXW108jLyMIIcRpbtfrek2aqw8tpNCJ8BLnpFwC8nX0 nYH8o7XkDnSMDTxfQbLS612cKBsqNDUhjxE+q7LU1RPWhw8bR4YkQq4n/avRkzuY4X35 kekA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lc12-20020a170906f90c00b0092b5384d67dsi425100ejb.613.2023.03.30.13.25.05; Thu, 30 Mar 2023 13:25:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229644AbjC3ULD (ORCPT + 99 others); Thu, 30 Mar 2023 16:11:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229436AbjC3ULB (ORCPT ); Thu, 30 Mar 2023 16:11:01 -0400 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 560459EF3 for ; Thu, 30 Mar 2023 13:11:00 -0700 (PDT) Received: (qmail 212186 invoked by uid 1000); 30 Mar 2023 16:10:59 -0400 Date: Thu, 30 Mar 2023 16:10:59 -0400 From: Alan Stern To: syzbot , syzbot , Hans de Goede Cc: linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, syzkaller-bugs@googlegroups.com Subject: Re: [syzbot] WARNING in shark_write_reg/usb_submit_urb, WARNING in shark_write_val/usb_submit_urb Message-ID: References: <00000000000096e4f905f81b2702@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <00000000000096e4f905f81b2702@google.com> X-Spam-Status: No, score=0.2 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Reference: https://syzkaller.appspot.com/bug?extid=4b3f8190f6e13b3efd74 Reference: https://syzkaller.appspot.com/bug?extid=1cb937c125adb93fad2d The radio-shark driver just assumes that the endpoints it uses will be present, without checking. This adds an appropriate check. Alan Stern #syz test: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/ v6.2 drivers/usb/core/usb.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/usb.h | 7 ++++ 2 files changed, 77 insertions(+) Index: usb-devel/drivers/usb/core/usb.c =================================================================== --- usb-devel.orig/drivers/usb/core/usb.c +++ usb-devel/drivers/usb/core/usb.c @@ -207,6 +207,76 @@ int usb_find_common_endpoints_reverse(st EXPORT_SYMBOL_GPL(usb_find_common_endpoints_reverse); /** + * usb_find_endpoint() - Given an endpoint address, search for the endpoint's + * usb_host_endpoint structure in an interface's current altsetting. + * @intf: the interface whose current altsetting should be searched + * @ep_addr: the endpoint address (number and direction) to find + * + * Search the altsetting's list of endpoints for one with the specified address. + * + * Return: Pointer to the usb_host_endpoint if found, %NULL otherwise. + */ +struct usb_host_endpoint __must_check *usb_find_endpoint( + const struct usb_interface *intf, unsigned int ep_addr) +{ + int n; + struct usb_host_endpoint *ep; + + n = intf->cur_altsetting->desc.bNumEndpoints; + ep = intf->cur_altsetting->endpoint; + for (; n > 0; (--n, ++ep)) { + if (ep->desc.bEndpointAddress == ep_addr) + return ep; + } + return NULL; +} +EXPORT_SYMBOL_GPL(usb_find_endpoint); + +/** + * usb_check_bulk_endpoint - Check whether an interface's current altsetting + * contains a bulk endpoint with the given address. + * @intf: the interface whose current altsetting should be searched + * @ep_addr: the endpoint address (number and direction) to look for + * + * Search for an endpoint with the specified address and check its type. + * + * Return: %true if the endpoint is found and is bulk, %false otherwise. + */ +bool usb_check_bulk_endpoint( + const struct usb_interface *intf, unsigned int ep_addr) +{ + const struct usb_host_endpoint *ep; + + ep = usb_find_endpoint(intf, ep_addr); + if (!ep) + return false; + return usb_endpoint_xfer_bulk(&ep->desc); +} +EXPORT_SYMBOL_GPL(usb_check_bulk_endpoint); + +/** + * usb_check_int_endpoint - Check whether an interface's current altsetting + * contains an interrupt endpoint with the given address. + * @intf: the interface whose current altsetting should be searched + * @ep_addr: the endpoint address (number and direction) to look for + * + * Search for an endpoint with the specified address and check its type. + * + * Return: %true if the endpoint is found and is interrupt, %false otherwise. + */ +bool usb_check_int_endpoint( + const struct usb_interface *intf, unsigned int ep_addr) +{ + const struct usb_host_endpoint *ep; + + ep = usb_find_endpoint(intf, ep_addr); + if (!ep) + return false; + return usb_endpoint_xfer_int(&ep->desc); +} +EXPORT_SYMBOL_GPL(usb_check_int_endpoint); + +/** * usb_find_alt_setting() - Given a configuration, find the alternate setting * for the given interface. * @config: the configuration to search (not necessarily the current config). Index: usb-devel/include/linux/usb.h =================================================================== --- usb-devel.orig/include/linux/usb.h +++ usb-devel/include/linux/usb.h @@ -292,6 +292,13 @@ void usb_put_intf(struct usb_interface * #define USB_MAXINTERFACES 32 #define USB_MAXIADS (USB_MAXINTERFACES/2) +struct usb_host_endpoint __must_check *usb_find_endpoint( + const struct usb_interface *intf, unsigned int ep_addr); +bool usb_check_bulk_endpoint( + const struct usb_interface *intf, unsigned int ep_addr); +bool usb_check_int_endpoint( + const struct usb_interface *intf, unsigned int ep_addr); + /* * USB Resume Timer: Every Host controller driver should drive the resume * signalling on the bus for the amount of time defined by this macro. drivers/media/radio/radio-shark.c | 7 +++++++ drivers/media/radio/radio-shark2.c | 7 +++++++ 2 files changed, 14 insertions(+) Index: usb-devel/drivers/media/radio/radio-shark.c =================================================================== --- usb-devel.orig/drivers/media/radio/radio-shark.c +++ usb-devel/drivers/media/radio/radio-shark.c @@ -317,6 +317,13 @@ static int usb_shark_probe(struct usb_in struct shark_device *shark; int retval = -ENOMEM; + /* Are the expected endpoints present? */ + if (!usb_check_int_endpoint(intf, SHARK_IN_EP | USB_DIR_IN) || + !usb_check_int_endpoint(intf, SHARK_OUT_EP | USB_DIR_OUT)) { + dev_err(&intf->dev, "Invalid radioSHARK device\n"); + return -EINVAL; + } + shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL); if (!shark) return retval; Index: usb-devel/drivers/media/radio/radio-shark2.c =================================================================== --- usb-devel.orig/drivers/media/radio/radio-shark2.c +++ usb-devel/drivers/media/radio/radio-shark2.c @@ -283,6 +283,13 @@ static int usb_shark_probe(struct usb_in struct shark_device *shark; int retval = -ENOMEM; + /* Are the expected endpoints present? */ + if (!usb_check_int_endpoint(intf, SHARK_IN_EP | USB_DIR_IN) || + !usb_check_int_endpoint(intf, SHARK_OUT_EP | USB_DIR_OUT)) { + dev_err(&intf->dev, "Invalid radioSHARK2 device\n"); + return -EINVAL; + } + shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL); if (!shark) return retval;