Message ID | 20231121-guenter-mini-v3-3-d8a5eae2312b@chromium.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2b07:b0:403:3b70:6f57 with SMTP id io7csp878049vqb; Tue, 21 Nov 2023 11:54:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IFXzYlITRysV6Azcu6cAsDQJvo8OlY/ibZx/ePhTnKMm0HZZnmdm9UNvPyR005GepI48jqS X-Received: by 2002:a05:6a00:1910:b0:6cb:68d7:b1bc with SMTP id y16-20020a056a00191000b006cb68d7b1bcmr172477pfi.30.1700596449398; Tue, 21 Nov 2023 11:54:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700596449; cv=none; d=google.com; s=arc-20160816; b=elqyF0fi2CGKNrVenizFlkB4FqPoSE9jTRrny9RUO+QC2tdeoWYEE2ek5pNYhRqKkj NTHuBCigQvzeyRRcMkVAhJn1AS7JY4dUBQllyExDED2fJhj3m2df73EdNZG57oXc+THX RkqiL2BrGl5t9q1nekeUy7MSxNsCzxVr0htNbdkRokb6ryLMEMbMP7sgWwsxdKodxc7t T7Va2Qpkfmjja7NuCsMmo2mqgUaW/87HDZ1yBLlUrY0vt/aQZKtq62elw9wFrJlTvap2 4BS4TBWQpANsnCwFZG49D7Dc98W5ZAYVy16EfOmfNm9NGRWHpqZonyKR84VuK6nkIai8 KC8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=vBBR3Lb8+qLiLMbc/jEnU4Lx/0wS0LihO+fwyf1YUqQ=; fh=Olmnw9EiPOHXl6tXYk5T9PPwqL+0H7V+IoxB1uD1V0I=; b=GgYjN4ORcZ/zhYoTgtGT/A2Y8X0s9mgwTRopcyoTYim3W90UN1ZJU6P6GFA36kHYk+ PDxOqcn4Rlg4QPCKr3FepCkqMKWPkunNRFWo5E04EhhvcP2qwwVvAiRZXRyPuho9v4ya ADjNL7wQKEH6/u0fwlbOylGSwMDSIqE/ILzFGet2fDn8pqAiWxlAanYQ31P4DlRer2SB r5cKrDhxsFpZVWSM7MjGlEYnk4R9Oj9a4zAOm26E1bEc3pY5tRCFN2OhTaunesyuCTvi xlhPV1KSGWSwZebKA2WEXn7pEzw02kasbH8nTUGy8sw1PaeIHOtuLfxHT43D4Aa8kINN XHHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=mVJnvzEg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id s34-20020a056a0017a200b006c6a9c10e15si11148464pfg.46.2023.11.21.11.54.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Nov 2023 11:54:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=mVJnvzEg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 3EC09811F137; Tue, 21 Nov 2023 11:54:08 -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 S233952AbjKUTyB (ORCPT <rfc822;ouuuleilei@gmail.com> + 99 others); Tue, 21 Nov 2023 14:54:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233504AbjKUTx6 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 21 Nov 2023 14:53:58 -0500 Received: from mail-ot1-x32d.google.com (mail-ot1-x32d.google.com [IPv6:2607:f8b0:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 998791A2 for <linux-kernel@vger.kernel.org>; Tue, 21 Nov 2023 11:53:54 -0800 (PST) Received: by mail-ot1-x32d.google.com with SMTP id 46e09a7af769-6d7e3be2614so494746a34.2 for <linux-kernel@vger.kernel.org>; Tue, 21 Nov 2023 11:53:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1700596434; x=1701201234; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=vBBR3Lb8+qLiLMbc/jEnU4Lx/0wS0LihO+fwyf1YUqQ=; b=mVJnvzEgvHgTXvGHTkIA53uO+Ifdo3TgXxNPsUo7sb4BRl2+BghrRSdPx5Itbp8EVF 7FdkBg+7JcCw+Y8PTu4vxCcF5ADjPPRslOEWwgdAcjDBEJBlzSse6WWJPzoIzemABcgC HRuV09zfPV86dCbLDhPqVwHSd5tzUb70pWu4w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700596434; x=1701201234; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vBBR3Lb8+qLiLMbc/jEnU4Lx/0wS0LihO+fwyf1YUqQ=; b=BYJYB4+e2RkHuN0z7nlLwYEiISd2XHM9w2/CsD0LuzPe+tmpNRsLsebxmm3c5RQ+wE 6JVyM2ivK8PHMWLZ7qB5YKXiHH0PVWUqLsrIUJ6Y4giPwzZ9TxsRFX6SEyYOam9q5oM3 TDCUeEagoV9Ywu3C1I8rXF7/9E0IjRUcllbzWrQZjrL5d5hS9rVQXRJjPLzH1/mhDTGY TPMVikNkjoj2NFVtrK/b0DFfCYbTc+xiQidrOdADheImER5F3WTK6imPbiAW+kl2Ftut QY9baKB1mSE73p6fma1bLuTd5e2YYbgpB+NS2RI0uTTYkUn+sll8GLkhwLx8KaOX10bC 0ItQ== X-Gm-Message-State: AOJu0YxXclhUeKUyZ2XCTHENWEIXu1zi2bV6vDqVNa7Afze/8zjtYrmD 1Kf0/ruBc8dSevjx5TeHP/0egA== X-Received: by 2002:a05:6830:39cc:b0:6d6:4c25:5a56 with SMTP id bt12-20020a05683039cc00b006d64c255a56mr325498otb.12.1700596433867; Tue, 21 Nov 2023 11:53:53 -0800 (PST) Received: from denia.c.googlers.com (228.221.150.34.bc.googleusercontent.com. [34.150.221.228]) by smtp.gmail.com with ESMTPSA id ct2-20020a056214178200b0065b0d9b4ee7sm4199409qvb.20.2023.11.21.11.53.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Nov 2023 11:53:53 -0800 (PST) From: Ricardo Ribalda <ribalda@chromium.org> Date: Tue, 21 Nov 2023 19:53:50 +0000 Subject: [PATCH v3 3/3] media: uvcvideo: Lock video streams and queues while unregistering MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20231121-guenter-mini-v3-3-d8a5eae2312b@chromium.org> References: <20231121-guenter-mini-v3-0-d8a5eae2312b@chromium.org> In-Reply-To: <20231121-guenter-mini-v3-0-d8a5eae2312b@chromium.org> To: Mauro Carvalho Chehab <mchehab@kernel.org> Cc: Guenter Roeck <linux@roeck-us.net>, Tomasz Figa <tfiga@chromium.org>, Laurent Pinchart <laurent.pinchart@ideasonboard.com>, Alan Stern <stern@rowland.harvard.edu>, Hans Verkuil <hverkuil-cisco@xs4all.nl>, linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Paul <seanpaul@chromium.org>, Ricardo Ribalda <ribalda@chromium.org>, Sakari Ailus <sakari.ailus@linux.intel.com> X-Mailer: b4 0.12.3 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: <linux-kernel.vger.kernel.org> 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]); Tue, 21 Nov 2023 11:54:08 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783204622535991348 X-GMAIL-MSGID: 1783204622535991348 |
Series |
uvcvideo: Attempt N to land UVC race conditions fixes
|
|
Commit Message
Ricardo Ribalda
Nov. 21, 2023, 7:53 p.m. UTC
From: Guenter Roeck <linux@roeck-us.net> 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 <laurent.pinchart@ideasonboard.com> Cc: Alan Stern <stern@rowland.harvard.edu> Cc: Hans Verkuil <hverkuil-cisco@xs4all.nl> Reviewed-by: Tomasz Figa <tfiga@chromium.org> Reviewed-by: Sean Paul <seanpaul@chromium.org> Signed-off-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org> --- drivers/media/usb/uvc/uvc_driver.c | 9 +++++++++ 1 file changed, 9 insertions(+)
Comments
On (23/11/21 19:53), Ricardo Ribalda wrote: > 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. > [..] > > 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 <laurent.pinchart@ideasonboard.com> > Cc: Alan Stern <stern@rowland.harvard.edu> > Cc: Hans Verkuil <hverkuil-cisco@xs4all.nl> > Reviewed-by: Tomasz Figa <tfiga@chromium.org> > Reviewed-by: Sean Paul <seanpaul@chromium.org> > Signed-off-by: Guenter Roeck <linux@roeck-us.net> > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org> Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Hi Ricardo, On Tue, Nov 21, 2023 at 07:53:50PM +0000, Ricardo Ribalda wrote: > From: Guenter Roeck <linux@roeck-us.net> > > 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 <laurent.pinchart@ideasonboard.com> > Cc: Alan Stern <stern@rowland.harvard.edu> > Cc: Hans Verkuil <hverkuil-cisco@xs4all.nl> > Reviewed-by: Tomasz Figa <tfiga@chromium.org> > Reviewed-by: Sean Paul <seanpaul@chromium.org> > Signed-off-by: Guenter Roeck <linux@roeck-us.net> > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org> > --- > 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. <URL:https://chromium.googlesource.com/chromiumos/third_party/kernel/+/refs/heads/kcam-6.1/drivers/isp/isp-device.c:
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 <linux@roeck-us.net> > > > > 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 <laurent.pinchart@ideasonboard.com> > > Cc: Alan Stern <stern@rowland.harvard.edu> > > Cc: Hans Verkuil <hverkuil-cisco@xs4all.nl> > > Reviewed-by: Tomasz Figa <tfiga@chromium.org> > > Reviewed-by: Sean Paul <seanpaul@chromium.org> > > Signed-off-by: Guenter Roeck <linux@roeck-us.net> > > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org> > > --- > > 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. > <URL:https://chromium.googlesource.com/chromiumos/third_party/kernel/+/refs/heads/kcam-6.1/drivers/isp/isp-device.c: 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/
Hi Laurent On Wed, 22 Nov 2023 at 14:14, Laurent Pinchart <laurent.pinchart@ideasonboard.com> 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 <linux@roeck-us.net> > > > > > > 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 <laurent.pinchart@ideasonboard.com> > > > Cc: Alan Stern <stern@rowland.harvard.edu> > > > Cc: Hans Verkuil <hverkuil-cisco@xs4all.nl> > > > Reviewed-by: Tomasz Figa <tfiga@chromium.org> > > > Reviewed-by: Sean Paul <seanpaul@chromium.org> > > > Signed-off-by: Guenter Roeck <linux@roeck-us.net> > > > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org> > > > --- > > > 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. > > <URL:https://chromium.googlesource.com/chromiumos/third_party/kernel/+/refs/heads/kcam-6.1/drivers/isp/isp-device.c: > > 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
Hi Ricardo, On Wed, Nov 22, 2023 at 02:59:31PM +0100, Ricardo Ribalda wrote: > On Wed, 22 Nov 2023 at 14:14, Laurent Pinchart wrote: > > On Wed, Nov 22, 2023 at 10:21:04AM +0000, Sakari Ailus wrote: > > > On Tue, Nov 21, 2023 at 07:53:50PM +0000, Ricardo Ribalda wrote: > > > > From: Guenter Roeck <linux@roeck-us.net> > > > > > > > > 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 <laurent.pinchart@ideasonboard.com> > > > > Cc: Alan Stern <stern@rowland.harvard.edu> > > > > Cc: Hans Verkuil <hverkuil-cisco@xs4all.nl> > > > > Reviewed-by: Tomasz Figa <tfiga@chromium.org> > > > > Reviewed-by: Sean Paul <seanpaul@chromium.org> > > > > Signed-off-by: Guenter Roeck <linux@roeck-us.net> > > > > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org> > > > > --- > > > > 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. > > > <URL:https://chromium.googlesource.com/chromiumos/third_party/kernel/+/refs/heads/kcam-6.1/drivers/isp/isp-device.c: > > > > 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. What I'm asking is that you, or someone else at your time, take over these patches from Dan and myself and help with the huge backlog we have in V4L2. You can't expect maintainers to fix everything in their spare time. Helping there is a good way to lower the pressure on maintainers and get patches reviewed more quickly. > [1] https://www.kernel.org/doc/html/latest/driver-api/usb/callbacks.html#the-disconnect-callback
Hi Laurent On Wed, 22 Nov 2023 at 15:04, Laurent Pinchart <laurent.pinchart@ideasonboard.com> wrote: > > Hi Ricardo, > > On Wed, Nov 22, 2023 at 02:59:31PM +0100, Ricardo Ribalda wrote: > > On Wed, 22 Nov 2023 at 14:14, Laurent Pinchart wrote: > > > On Wed, Nov 22, 2023 at 10:21:04AM +0000, Sakari Ailus wrote: > > > > On Tue, Nov 21, 2023 at 07:53:50PM +0000, Ricardo Ribalda wrote: > > > > > From: Guenter Roeck <linux@roeck-us.net> > > > > > > > > > > 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 <laurent.pinchart@ideasonboard.com> > > > > > Cc: Alan Stern <stern@rowland.harvard.edu> > > > > > Cc: Hans Verkuil <hverkuil-cisco@xs4all.nl> > > > > > Reviewed-by: Tomasz Figa <tfiga@chromium.org> > > > > > Reviewed-by: Sean Paul <seanpaul@chromium.org> > > > > > Signed-off-by: Guenter Roeck <linux@roeck-us.net> > > > > > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org> > > > > > --- > > > > > 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. > > > > <URL:https://chromium.googlesource.com/chromiumos/third_party/kernel/+/refs/heads/kcam-6.1/drivers/isp/isp-device.c: > > > > > > 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. > > What I'm asking is that you, or someone else at your time, take over > these patches from Dan and myself and help with the huge backlog we have > in V4L2. You can't expect maintainers to fix everything in their spare > time. Helping there is a good way to lower the pressure on maintainers > and get patches reviewed more quickly. I do not think that this is a fair comment. I am already reviewing all the uvc patches in the ML. Most of the time in my spare time. How much more help do you need there? We know for over 4 years that there is a NULL deref issue in the uvc driver. There is a 10 lines fix for it, that has been both reviewed and tested. What else is needed from a driver point of view? It feels that by your position you are just pressuring to get a global solution, that is a noble cause. But that is not the responsibility or the role of a driver maintainer. > > > [1] https://www.kernel.org/doc/html/latest/driver-api/usb/callbacks.html#the-disconnect-callback > > -- > Regards, > > Laurent Pinchart
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,