Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp924641pxu; Mon, 23 Nov 2020 07:29:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJy2isRHL9i/SDVJ7STO8VjhX0rurgH+ff0m9a5OljVuwdtHBGGeetICxD5qXiEt7KKjoUmR X-Received: by 2002:a17:906:17d1:: with SMTP id u17mr106559eje.229.1606145342423; Mon, 23 Nov 2020 07:29:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606145342; cv=none; d=google.com; s=arc-20160816; b=p2519ftTDvG8iTOQxjliozvBMdQFIQlqNydQDCsvy0Vz6xNMokoIhFv5wMW/NQ8bAr X/oUiLX/ex9B9B8TOvNZryrx5A9KNBztXNUQ9gqnMNhdiiCQsaPI3HNO7nw0pwH7KQ9W BkBdWm1XP/roGJifRsAKwMgC2KoIa1p2fTtFIhmy2MsnYOu2v2knLnxj+4gEJQYQt2aM hSmBhX75WRLnocaFwBjEC+3BYvtl4mmeTr2WWrgjenwPC/lKjaofhqk9eh4Fep+4qum2 e7PTzbj2iAJO2Infnzxkf+Z7VPw0KVgDK/4NgzPWH3Pi0yjQ8KEgYEApcN4U5GjTLe9Z Iegw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=ltKvl8pr3hJaZt4L3NIrlg2t/PAaNaeZBB3s/KG/v4Y=; b=w5mn24ErdFT0Tr7h019mBxKfFe8mEveUltOlQcUkNJmNGd7DqQu6+TuxvU4p3STkug N8aQxqa9T+qiB1ObED1LTH9mnfRQbSAESVC/1g7j+pZY3vahmswA9RQofiDpiD2oba0k qUr/Dh34uhRpAk+QMpTeT30goNxK4IvIQAivj6aD7ze2vrzPzt+Q4wm9mW5YveLzlIqz JcXYI3udV3qp/qHm92bSn0SZVWN2pnqOyZjuXngB7IeDQ42ys9QltgqXOFTxxUVL2lUv gavKzxKYgVXCZvYJEgcWMKeGLu1dgyWUUPjNYu/bPr27ELLdgtcUtl3/QeKVyo0JHy2a rExQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i20si4278652ejx.153.2020.11.23.07.28.38; Mon, 23 Nov 2020 07:29:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389505AbgKWP0z (ORCPT + 99 others); Mon, 23 Nov 2020 10:26:55 -0500 Received: from netrider.rowland.org ([192.131.102.5]:44181 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S2389490AbgKWP0z (ORCPT ); Mon, 23 Nov 2020 10:26:55 -0500 Received: (qmail 710166 invoked by uid 1000); 23 Nov 2020 10:26:54 -0500 Date: Mon, 23 Nov 2020 10:26:54 -0500 From: Alan Stern To: John Boero , Laurent Pinchart Cc: Greg Kroah-Hartman , Felipe Balbi , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] usb: core: Null deref in kernel with USB webcams. Message-ID: <20201123152654.GB708586@rowland.harvard.edu> References: <20201112192524.GB287229@rowland.harvard.edu> <20201113163449.GB322940@rowland.harvard.edu> <20201113171658.GF322940@rowland.harvard.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org To recap: The problem is that uvcvideo tries to change an interface altsetting (from within uvc_video_start_streaming -> uvc_video_start_transfer) after the driver has been unbound from the device. This triggers an invalid memory reference. On Sun, Nov 22, 2020 at 08:03:43PM +0000, John Boero wrote: > Thanks Alan > I just spent some more time investigating and playing with different patches, > locks, mutexes, and sleeps, and I think I see exactly what's happening now. > I now understand why it: > A) seems to happen randomly during uvc start_stream > B) affects multiple device vendors > C) has been reported in RaspberryPi and IoT threads > > I put a trace on usb/core/hub.c:usb_disconnect to identify why the device was > disconnecting and it seems this is a low power issue. An idle webcam appears > fine to usbcore but as soon as you initialize it or uvc starts a stream, it > consumes more power, might find the cable can't supply it, and then disconnects > via interrupt. In my case I can reproduce this consistently with a cheap USB > extension cable, but this issue appears common to passive hubs, and IoT or SBCs > that don't always supply clean power over USB. My simplified patch can at least > protect usbcore from crashing on a bad device: > > From 73019d79fe4fd8b2c945005f8a067f528d8056fd Mon Sep 17 00:00:00 2001 > From: jboero > Date: Sun, 22 Nov 2020 19:30:41 +0000 > Subject: [PATCH] USBCore NULL interface deref fix > > --- > drivers/usb/core/usb.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c > index bafc113f2b3e..da46c84c87ce 100644 > --- a/drivers/usb/core/usb.c > +++ b/drivers/usb/core/usb.c > @@ -278,7 +278,7 @@ struct usb_interface *usb_ifnum_to_if(const struct > usb_device *dev, > if (!config) > return NULL; > for (i = 0; i < config->desc.bNumInterfaces; i++) > - if (config->interface[i]->altsetting[0] > + if (config->interface[i] && config->interface[i]->altsetting[0] > .desc.bInterfaceNumber == ifnum) > return config->interface[i]; I really dislike the idea of papering over a problem instead of fixing it properly. > This protects ongoing USB functionality including lsusb, and prevents a hang on > reboot after error. It doesn't help a user diagnose the error on the UVC side. > A fix from the uvc side is a little trickier and I'd like an opinion on how > best to handle locks in uvc_video_start_transfer. I've tried a few options > around uvcvideo.c:1874 > > ret = usb_set_interface(stream->dev->udev, intfnum, altsetting); > > I've even used multiple msleeps and checked for NULL interfaces but that feels > like a cheap trick and I was wondering what lock solution might help best here? Laurent Pinchart is the person to ask. He is the maintainer of the uvcvideo driver. I know very little about it. Alan Stern