Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp1107301rdf; Wed, 22 Nov 2023 05:59:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IHeYtDVDg+bocMtLoUTOM6h3UUCjLVXpZO6CSbvXfcY5CjD3MemLRAswwQMQuv6obp9FqPO X-Received: by 2002:a17:90b:1a8c:b0:283:2a1b:b18a with SMTP id ng12-20020a17090b1a8c00b002832a1bb18amr2328366pjb.26.1700661596464; Wed, 22 Nov 2023 05:59:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700661596; cv=none; d=google.com; s=arc-20160816; b=nIaCu2gm8AXl37aNPVkh+cyMPj1JATYjZ0I/yVCpIIhsGdjHrugb8UYWTJ5nNVL2gu hq7krIpYhI+fQCWLKq0uid0i4ORJPMp0VJhtdQuGUmL0wAJl1RmDs/cFFO0r+2jarQ8F cVhlbprERHGQMpN4986l0QbebAdEK7E0jcOnvsnQvLNTDdi0UCTcDV9au+Wa+pofsPhD 2YzIeAWNx8velP6v2Z+80ReADKu9on+ODJwk6rsWKZRARChVQAWzYjbB+XuwoXqjAQKF MsCx1A6EA+Pj9j0sxeRrcGoPculQPDceA4sAmVzTxdfnAs9pwCGmYceFTiMixqVQeKrO aiVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=D6DKXVEGFtP618IWdtKwLgwiHtnsTox1dLjqRW4wwcY=; fh=GTz2sTf+JCTxGBgUtyEbeTp+rvSsWze2P9q/3BnRb9c=; b=tE8BNAbh8SjNUGz++cPNFvkO4HJoqfcsccKhBwimC/i7FxJn1uz2OkVimDPadi5Qj0 p6NsQ2ITGbPtPVrsqetJR9swfnq0KBtQwFPvEKgwnCsE3nSZWGGsSdaADIpE2ijL/Jf5 F5XMeJmCgKFTUFoPhGPDzFID/6/mQmMEcDhORL2dIwHIUvw6QlNvM66OCrt1Q51Y/6vf 8TsxXzzj/WRqBmTV1ihdT1gR6dbvuFAK3zTZSNsDNIzzTBcqQK3BpFvPWi5asrCK+hVh XvYXsMOx+lRx+PIkCmAPiPtdC5nvRpEe38XmjLu+WjvxtC9EOstPrS8Q1YXr0eb/D90w 6l3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Lo9EZXeN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id p11-20020a170902e74b00b001c62cfff795si10419054plf.429.2023.11.22.05.59.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 05:59:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Lo9EZXeN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 1AD1F820DA1A; Wed, 22 Nov 2023 05:59:55 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343970AbjKVN7z (ORCPT + 99 others); Wed, 22 Nov 2023 08:59:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231418AbjKVN7y (ORCPT ); Wed, 22 Nov 2023 08:59:54 -0500 Received: from mail-oo1-xc34.google.com (mail-oo1-xc34.google.com [IPv6:2607:f8b0:4864:20::c34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ADF22101 for ; Wed, 22 Nov 2023 05:59:49 -0800 (PST) Received: by mail-oo1-xc34.google.com with SMTP id 006d021491bc7-5842c251d7cso3742118eaf.1 for ; Wed, 22 Nov 2023 05:59:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1700661588; x=1701266388; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=D6DKXVEGFtP618IWdtKwLgwiHtnsTox1dLjqRW4wwcY=; b=Lo9EZXeNSzLqA4Di37WMHUE84LYjntTKML2DaOoNZZnUnPiLvVs0c/67ritmLayzXI rDaOcS2cSsStbXdoAYF3uszcIx5zgguxpXt3ZvnZ0gpcYlO0eDOhXW8C2AmQUObLyJz7 /yXfS8xoPoUWxtnw7whTwQXn1LlS/CTKQu42U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700661588; x=1701266388; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=D6DKXVEGFtP618IWdtKwLgwiHtnsTox1dLjqRW4wwcY=; b=s8x/YqRthdaUNu1xhVKFxT+XxdZFv2KkDe2p8q7XwqJanqb2+0GaJrdMWW+pB772PX OkpF0ffP2+C4srd1Lisjq3Q/VYH/IisZH5vPRGhncoGsylAY5VZB2nf0krbo2k4jnmDH pDwoCOj04OiGZ1nlIu6ilPfUugIsv1X1TQZbL0TgTkVb3zPbPfNSsRTeXVEsdGehBcs8 GHf+qNxRICryP4+UxrwCUjgA85xAXumCI9Y2zpCK9N15n0BeacyL4SOWFOxfYD+ezidy 5CrKOUfa3kLVXnIg1aaHBs0lmahHna06SrEH/Md+3oVtlfrBnaSKLHJvz75hHztXwcI7 KhvA== X-Gm-Message-State: AOJu0Yy7ynZzYFPL0uWP6TPlaBO7UyqeXqMMp55p3IDNWCT9EkLnu1s2 frGZs1XDwkCPeD0TMFld6bOdUjB4THzLxnnLooyuNg== X-Received: by 2002:a05:6358:91a0:b0:16d:bd05:d0f1 with SMTP id j32-20020a05635891a000b0016dbd05d0f1mr3036505rwa.5.1700661588412; Wed, 22 Nov 2023 05:59:48 -0800 (PST) Received: from mail-qv1-f47.google.com (mail-qv1-f47.google.com. [209.85.219.47]) by smtp.gmail.com with ESMTPSA id v7-20020ac87487000000b0041950c7f6d8sm4408237qtq.60.2023.11.22.05.59.47 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Nov 2023 05:59:47 -0800 (PST) Received: by mail-qv1-f47.google.com with SMTP id 6a1803df08f44-66fa16092c0so25998446d6.0 for ; Wed, 22 Nov 2023 05:59:47 -0800 (PST) X-Received: by 2002:a05:6214:5193:b0:671:dac1:c856 with SMTP id kl19-20020a056214519300b00671dac1c856mr2106687qvb.53.1700661587263; Wed, 22 Nov 2023 05:59:47 -0800 (PST) MIME-Version: 1.0 References: <20231121-guenter-mini-v3-0-d8a5eae2312b@chromium.org> <20231121-guenter-mini-v3-3-d8a5eae2312b@chromium.org> <20231122131455.GC3909@pendragon.ideasonboard.com> In-Reply-To: <20231122131455.GC3909@pendragon.ideasonboard.com> From: Ricardo Ribalda Date: Wed, 22 Nov 2023 14:59:31 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3 3/3] media: uvcvideo: Lock video streams and queues while unregistering To: Laurent Pinchart Cc: Sakari Ailus , Mauro Carvalho Chehab , Guenter Roeck , Tomasz Figa , Alan Stern , Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Paul , Sakari Ailus , Dan Williams , Sergey Senozhatsky Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 22 Nov 2023 05:59:55 -0800 (PST) Hi Laurent On Wed, 22 Nov 2023 at 14:14, Laurent Pinchart wrote: > > CC'ing Dan Williams. > > On Wed, Nov 22, 2023 at 10:21:04AM +0000, Sakari Ailus wrote: > > Hi Ricardo, > > > > On Tue, Nov 21, 2023 at 07:53:50PM +0000, Ricardo Ribalda wrote: > > > From: Guenter Roeck > > > > > > The call to uvc_disconnect() is not protected by any mutex. > > > This means it can and will be called while other accesses to the video > > > device are in progress. This can cause all kinds of race conditions, > > > including crashes such as the following. > > > > > > usb 1-4: USB disconnect, device number 3 > > > BUG: unable to handle kernel NULL pointer dereference at 0000000000000000 > > > PGD 0 P4D 0 > > > Oops: 0000 [#1] PREEMPT SMP PTI > > > CPU: 0 PID: 5633 Comm: V4L2CaptureThre Not tainted 4.19.113-08536-g5d29ca36db06 #1 > > > Hardware name: GOOGLE Edgar, BIOS Google_Edgar.7287.167.156 03/25/2019 > > > RIP: 0010:usb_ifnum_to_if+0x29/0x40 > > > Code: <...> > > > RSP: 0018:ffffa46f42a47a80 EFLAGS: 00010246 > > > RAX: 0000000000000000 RBX: 0000000000000000 RCX: ffff904a396c9000 > > > RDX: ffff904a39641320 RSI: 0000000000000001 RDI: 0000000000000000 > > > RBP: ffffa46f42a47a80 R08: 0000000000000002 R09: 0000000000000000 > > > R10: 0000000000009975 R11: 0000000000000009 R12: 0000000000000000 > > > R13: ffff904a396b3800 R14: ffff904a39e88000 R15: 0000000000000000 > > > FS: 00007f396448e700(0000) GS:ffff904a3ba00000(0000) knlGS:0000000000000000 > > > CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > > > CR2: 0000000000000000 CR3: 000000016cb46000 CR4: 00000000001006f0 > > > Call Trace: > > > usb_hcd_alloc_bandwidth+0x1ee/0x30f > > > usb_set_interface+0x1a3/0x2b7 > > > uvc_video_start_transfer+0x29b/0x4b8 [uvcvideo] > > > uvc_video_start_streaming+0x91/0xdd [uvcvideo] > > > uvc_start_streaming+0x28/0x5d [uvcvideo] > > > vb2_start_streaming+0x61/0x143 [videobuf2_common] > > > vb2_core_streamon+0xf7/0x10f [videobuf2_common] > > > uvc_queue_streamon+0x2e/0x41 [uvcvideo] > > > uvc_ioctl_streamon+0x42/0x5c [uvcvideo] > > > __video_do_ioctl+0x33d/0x42a > > > video_usercopy+0x34e/0x5ff > > > ? video_ioctl2+0x16/0x16 > > > v4l2_ioctl+0x46/0x53 > > > do_vfs_ioctl+0x50a/0x76f > > > ksys_ioctl+0x58/0x83 > > > __x64_sys_ioctl+0x1a/0x1e > > > do_syscall_64+0x54/0xde > > > > > > usb_set_interface() should not be called after the USB device has been > > > unregistered. However, in the above case the disconnect happened after > > > v4l2_ioctl() was called, but before the call to usb_ifnum_to_if(). > > > > > > Acquire various mutexes in uvc_unregister_video() to fix the majority > > > (maybe all) of the observed race conditions. > > > > > > The uvc_device lock prevents races against suspend and resume calls > > > and the poll function. > > > > > > The uvc_streaming lock prevents races against stream related functions; > > > for the most part, those are ioctls. This lock also requires other > > > functions using this lock to check if a video device is still registered > > > after acquiring it. For example, it was observed that the video device > > > was already unregistered by the time the stream lock was acquired in > > > uvc_ioctl_streamon(). > > > > > > The uvc_queue lock prevents races against queue functions, Most of > > > those are already protected by the uvc_streaming lock, but some > > > are called directly. This is done as added protection; an actual race > > > was not (yet) observed. > > > > > > Cc: Laurent Pinchart > > > Cc: Alan Stern > > > Cc: Hans Verkuil > > > Reviewed-by: Tomasz Figa > > > Reviewed-by: Sean Paul > > > Signed-off-by: Guenter Roeck > > > Signed-off-by: Ricardo Ribalda > > > --- > > > drivers/media/usb/uvc/uvc_driver.c | 9 +++++++++ > > > 1 file changed, 9 insertions(+) > > > > > > diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c > > > index 413c32867617..3408b865d346 100644 > > > --- a/drivers/media/usb/uvc/uvc_driver.c > > > +++ b/drivers/media/usb/uvc/uvc_driver.c > > > @@ -1907,14 +1907,22 @@ static void uvc_unregister_video(struct uvc_device *dev) > > > { > > > struct uvc_streaming *stream; > > > > > > + mutex_lock(&dev->lock); > > > + > > > list_for_each_entry(stream, &dev->streams, list) { > > > if (!video_is_registered(&stream->vdev)) > > > continue; > > > > > > + mutex_lock(&stream->mutex); > > > + mutex_lock(&stream->queue.mutex); > > > + > > > video_unregister_device(&stream->vdev); > > > video_unregister_device(&stream->meta.vdev); > > > > > > uvc_debugfs_cleanup_stream(stream); > > > + > > > + mutex_unlock(&stream->queue.mutex); > > > + mutex_unlock(&stream->mutex); > > > } > > > > > > uvc_status_unregister(dev); > > > @@ -1925,6 +1933,7 @@ static void uvc_unregister_video(struct uvc_device *dev) > > > if (media_devnode_is_registered(dev->mdev.devnode)) > > > media_device_unregister(&dev->mdev); > > > #endif > > > + mutex_unlock(&dev->lock); > > > } > > > > > > int uvc_register_video_device(struct uvc_device *dev, > > > > > > > Instead of acquiring all the possible locks, could you instead take the > > same approach as you do with ISP? It should use refcount_t btw. > > > The right approach, as I've said countless of times, is to fix this at > the cdev and V4L2 level. Dan Williams has done some ground work on this > a while ago ([1]), and before that I posted an RFC series that > overlapped with Dan's work (with a more naive and less efficient > implementation of refcounting, see [2]). > > [1] https://lore.kernel.org/all/161117153776.2853729.6944617921517514510.stgit@dwillia2-desk3.amr.corp.intel.com/ > [2] https://lore.kernel.org/linux-media/20171116003349.19235-2-laurent.pinchart+renesas@ideasonboard.com/ The UVC driver is violating the USB driver API [1] causing crashes and probably security vulnerabilities. It has to be fixed. If there was a different way TODAY to do this, I would very happily implement it differently. But your patchset is 6 years old and Dan's 2 and they have not landed. How many kernel versions is ok to put our users willingly at risk? This patch simply serialises unregister_video? How can this patch affect the viability of your patchset or Dan's patch? If this patch is not needed in the future we can simply revert it. When/If there is a better way to implement this, I would very happily send a follow-up patch to use that framework. [1] https://www.kernel.org/doc/html/latest/driver-api/usb/callbacks.html#the-disconnect-callback > > -- > Regards, > > Laurent Pinchart -- Ricardo Ribalda