Message ID | 20231122-guenter-mini-v4-1-3d94e1e34dc1@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 io7csp1221442vqb; Wed, 22 Nov 2023 02:20:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IGOMKDGzAB7clIc0WUtTBUegPzH36isg3zG9yzkF8zOWU6cbqzvxSXjqlo2B7ZBZCdeR3M2 X-Received: by 2002:a54:468c:0:b0:3a9:bb08:d468 with SMTP id k12-20020a54468c000000b003a9bb08d468mr1833958oic.55.1700648409273; Wed, 22 Nov 2023 02:20:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700648409; cv=none; d=google.com; s=arc-20160816; b=XuWK07PmpVpCPz6eznmKydKSoIUKiTy7cs8IAuzn0Op1ifREn32h1sR5rLisa1CtSd eTZJTEXm53rEdZ9PDVpZ19Sr2iHnhBE50ASEpMCoaJEf6EOpcemLXUisl9AR0AaKjm66 5XBH1BauRyMSMg0D4iNbyO+LtN7CdOB8c4GRxDJ2fGkFeGhi8tUFgfAgcp0C63zvZnkm YnmHPdpJmaccYpzow1ERcIelfif+zpp6ZWvutfWzP6Iid9XXmVT02I6pcFHF0ap/I8n7 ovrHQvx6ENLNrwyiYAwPsmcG+o4j9Nyesu6KEgb5R2msYIOq7xSvu/PRpYer7y+ciwyr 1k4g== 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=3jbUgexWSE+UkwxAykTeDpdOwZGPMwaBInU8mlrkF50=; fh=m7dgJSqrUWbEiCvuETYoo3Fvna8nCiBibY2ykXjL+q8=; b=gCZ5tD+R0yZx1MdUqhP2pw0VA/2k2YFuPDUexg2NuYOyqUGETl2JLkULKrIkjOYOpY Xa5gg2MVaNCPOwhcsYfN7l3YW4G0Tj0yAGzUypOo8qrAkaDKTAu0xd3r1SuJO2FEsKJp na4FvD3Qw5+TLp0EGsOUBTsBtbpydGkn2z6JKTckmB4pLOHM8Ljn7vnznkYwHJTZ1Wuv WIWZOCT8z+DsUXFUZTSF03bNwHVz7L0HaSh1eZgYToM/xnkA3HMs2h7jtLsvGZWNv8E7 G5HDF6S/cP/ZfG7zWOqv69jtmmDzn+9NYm4MDl5CivCg6K9r4xsa2a72hc88fXacKQJR 7Bzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=F7wxorbO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id g124-20020a636b82000000b005ac8d44bad5si11880616pgc.7.2023.11.22.02.20.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 02:20:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=F7wxorbO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id A7E03811F574; Wed, 22 Nov 2023 02:19:57 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235342AbjKVKTy (ORCPT <rfc822;ouuuleilei@gmail.com> + 99 others); Wed, 22 Nov 2023 05:19:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235204AbjKVKTv (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 22 Nov 2023 05:19:51 -0500 Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E8AE93 for <linux-kernel@vger.kernel.org>; Wed, 22 Nov 2023 02:19:47 -0800 (PST) Received: by mail-qk1-x72b.google.com with SMTP id af79cd13be357-77d55e986ecso114531885a.2 for <linux-kernel@vger.kernel.org>; Wed, 22 Nov 2023 02:19:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1700648386; x=1701253186; 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=3jbUgexWSE+UkwxAykTeDpdOwZGPMwaBInU8mlrkF50=; b=F7wxorbOiTE0uDAQtEfKGrBeMd2wEhVSErzly03FhyI05NagpfT2xSZQR6TxDtE/Lj qznHJm6IDGkXuGUm7Ngi9LiI+IBp34S+NLGP7+5s5V/GjE8vhzY5rDHPzOwDpDi0F4o/ fsCzeUjHjTRdfe8mYVqQ3ALUCZj4d/Cs/oLhU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700648386; x=1701253186; 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=3jbUgexWSE+UkwxAykTeDpdOwZGPMwaBInU8mlrkF50=; b=kzop6X31wcz1upWT7ffEiUAKbyt/JdkXsaCViJtayTIgxu2y6PziDWHubdaK41A2ce UmlMHM0KQ3hB3HLKeH3IjDaC6I/G3KWS5/56L6QKKvn3AgT1m18SP8tNLqXFoq4yl1lW hIhrE3ZU9fqQSJIdkhXc0Ri+4/Q5ynunofAHFO6e4t36ejY2/yuYg5QplFj1VWhRbHTQ XxM1ZgIVLPkbFdgaPwZrDRGwe/1+Nponr3fr6AHhutkiWhbXxZ3Qeu+rn4WrHuhd5sk8 X1cD/A5ACp8y29MqSN80N91ZX4Hclal23yP42gJv6nJnHHtI4BS2P97cKfl4LVj045o2 VVtg== X-Gm-Message-State: AOJu0Yxj8kY1rTtPUfPUH8pu/RFpyE3HEiazRwe0/Iy5/AOe/jT3v/Wb /QfKATou4Ilmd1mdkCesjdRKLw== X-Received: by 2002:a05:6214:250d:b0:679:f6e4:5ed1 with SMTP id gf13-20020a056214250d00b00679f6e45ed1mr1464786qvb.60.1700648386567; Wed, 22 Nov 2023 02:19:46 -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 di6-20020ad458e6000000b0066d1d2242desm4739352qvb.120.2023.11.22.02.19.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 02:19:45 -0800 (PST) From: Ricardo Ribalda <ribalda@chromium.org> Date: Wed, 22 Nov 2023 10:19:34 +0000 Subject: [PATCH v4 1/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: <20231122-guenter-mini-v4-1-3d94e1e34dc1@chromium.org> References: <20231122-guenter-mini-v4-0-3d94e1e34dc1@chromium.org> In-Reply-To: <20231122-guenter-mini-v4-0-3d94e1e34dc1@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>, Sergey Senozhatsky <senozhatsky@chromium.org> X-Mailer: b4 0.12.3 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,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 agentk.vger.email 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 (agentk.vger.email [0.0.0.0]); Wed, 22 Nov 2023 02:19:57 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783259106469001031 X-GMAIL-MSGID: 1783259106469001031 |
Series |
uvcvideo: Attempt N to land UVC race conditions fixes
|
|
Commit Message
Ricardo Ribalda
Nov. 22, 2023, 10:19 a.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> Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org> 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 08fcd2ffa727..ded2cb6ce14f 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,