From nobody Mon Apr 6 09:11:36 2026 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6866A355F59 for ; Fri, 20 Mar 2026 06:54:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773989673; cv=none; b=qIE9Z6oXHngQYpCvFPLpFQkjw2GhYGWf1dY+aLDORJCtZBYMvEsBK9o0LuzGmWI4SLMWwhhracc7wzUjd6N0ZuhNk9F2MGEYcA5y8Dr59muku8ttOjnwJIuH5Gj/rKvIfPrO2hQZqtb65xskBhwKbJkqRlAqV6uXlnloA6LvkYA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773989673; c=relaxed/simple; bh=KE7ryRI217ghJKTIFCU76Brwb6dBcjnOO8TMElWQeSM=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=kFyR1waFNk9AhsFbSrGn9Q0DnxmPy5hs/2EjAEcjS9XAQVOao9vCKLXTDoOEIwwNMj1xHNccR3olLaY25LrFLOoVgBny598Vbm1gtiKRL4TxCl/wM+EySCWdPGM7/bTCK0J7bgn2yYNezp37PsdJzekRqZrVBKlBnFs/Fv1EU48= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--hhhuuu.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jv3PRAzz; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--hhhuuu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jv3PRAzz" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-829b7ed8964so306650b3a.2 for ; Thu, 19 Mar 2026 23:54:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773989672; x=1774594472; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=/wPANvJo1MTxq8jSz8DIizqvGS0ZDwyTVHtzAj8yEPA=; b=jv3PRAzz8TYT75aWjbFwnhs/F3nN1/Y+7++ugH8332iHEvkidLTGjRJc1erYs9DKRT NEqHlw5Qb/mZNXsqGhlVtqPt9ROw7eA5PqLVTiyqNJKTFEZ7Y3egMRyyhMqorK5a/Af0 3cE2B38vbrd5dtTt1HFr/wX9KJFRlU75QfLqDEK91hLZ4rdTtHZrpdghHtucaAA2hPqg ryDzx20b9BtJcDuEmdSwaQvgh5P1IwKypZ1lUOfPaVigwVl3XXN33RMXBzq+S0z5oPR4 l8Ze9OqlH16bNiUmLaj8SkJulJnVjYizQOHm7ONiK5gvutst0M9mm8ppetkWzK/D2vZz ciNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773989672; x=1774594472; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=/wPANvJo1MTxq8jSz8DIizqvGS0ZDwyTVHtzAj8yEPA=; b=rzm9hSxvTfPcZO19kpPOqQIlOIl1jPDCuShfGXFkWhXwhAiqyFJTUS7gvS3I6N3aIz 7MN5vgXYG7cPyo8jP2+7L1ksYrmj9Vv/xkzW5HDR6JN7DLP/aGg5jA8vvssOipDy4gWT h1sA1OCK+FyCh6V4MxfGwlQBvlb/ffx/VX8XHdaJOIfA+n4VQgOUvV/CiQxHGKkYVZRn 9zBsh17DRElJeTAVOLjATawp/x4zEsOuYH8yjz4Xm8u8X8nJ/7HzAndQ8IQgVeU967ai 0gty4cqEXg16toKg9gy72LCR0wjy4nPzfUse908F8tipKZnhGiC3geNrcP/qrlJ3NQDM P2GQ== X-Forwarded-Encrypted: i=1; AJvYcCUiA9E9evROxIMSo8MIo1UhjAqPD0WcScFHFojACWG2iUCpqJyNhfQY0bpa1be4cKp+HaCcZZ12queZ5Po=@vger.kernel.org X-Gm-Message-State: AOJu0YzPwXbJU/8CG6Mg4QGYWFQTxSnjFX4zNhQsx2bwX3IWw9m96Vs9 6z84682uJl0ewMBDT1ulx5fmsdMbsnXzrFYTQDZ6ifFBSlYOXWD29m+k/UlPUxTsOqJL4jWHtC1 6yiWqKA== X-Received: from pfbhh2.prod.google.com ([2002:a05:6a00:8682:b0:824:1dc9:90db]) (user=hhhuuu job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1392:b0:827:2995:3b99 with SMTP id d2e1a72fcca58-82a8c324815mr1582927b3a.31.1773989671540; Thu, 19 Mar 2026 23:54:31 -0700 (PDT) Date: Fri, 20 Mar 2026 14:54:27 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.53.0.959.g497ff81fa9-goog Message-ID: <20260320065427.1374555-1-hhhuuu@google.com> Subject: [PATCH v4] usb: gadget: uvc: fix NULL pointer dereference during unbind race From: Jimmy Hu To: Greg Kroah-Hartman Cc: Dan Vacura , Alan Stern , Laurent Pinchart , Hans Verkuil , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Jimmy Hu Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit b81ac4395bbe ("usb: gadget: uvc: allow for application to cleanly shutdown") introduced two stages of synchronization waits totaling 1500ms in uvc_function_unbind() to prevent several types of kernel panics. However, this timing-based approach is insufficient during power management (PM) transitions. When the PM subsystem starts freezing user space processes, the wait_event_interruptible_timeout() is aborted early, which allows the unbind thread to proceed and nullify the gadget pointer (cdev->gadget =3D NULL): [ 814.123447][ T947] configfs-gadget.g1 gadget.0: uvc: uvc_function_unbin= d() [ 814.178583][ T3173] PM: suspend entry (deep) [ 814.192487][ T3173] Freezing user space processes [ 814.197668][ T947] configfs-gadget.g1 gadget.0: uvc: uvc_function_unbin= d no clean disconnect, wait for release When the PM subsystem resumes or aborts the suspend and tasks are restarted, the V4L2 release path is executed and attempts to access the already nullified gadget pointer, triggering a kernel panic: [ 814.292597][ C0] PM: pm_system_irq_wakeup: 479 triggered dhdpcie_host= _wake [ 814.386727][ T3173] Restarting tasks ... [ 814.403522][ T4558] Unable to handle kernel NULL pointer dereference at = virtual address 0000000000000030 [ 814.404021][ T4558] pc : usb_gadget_deactivate+0x14/0xf4 [ 814.404031][ T4558] lr : usb_function_deactivate+0x54/0x94 [ 814.404078][ T4558] Call trace: [ 814.404080][ T4558] usb_gadget_deactivate+0x14/0xf4 [ 814.404083][ T4558] usb_function_deactivate+0x54/0x94 [ 814.404087][ T4558] uvc_function_disconnect+0x1c/0x5c [ 814.404092][ T4558] uvc_v4l2_release+0x44/0xac [ 814.404095][ T4558] v4l2_release+0xcc/0x130 Address the race condition and NULL pointer dereference by: 1. State Synchronization (flag + mutex) Introduce a 'func_unbound' flag in struct uvc_device. This allows uvc_function_disconnect() to safely skip accessing the nullified cdev->gadget pointer. As suggested by Alan Stern, this flag is protected by a new mutex (uvc->lock) to ensure proper memory ordering and prevent instruction reordering or speculative loads. This mutex is also used to protect 'func_connected' for consistent state management. 2. Explicit Synchronization (completion) Use a completion to synchronize uvc_function_unbind() with the uvc_vdev_release() callback. This prevents Use-After-Free (UAF) by ensuring struct uvc_device is freed after all video device resources are released. Fixes: b81ac4395bbe ("usb: gadget: uvc: allow for application to cleanly sh= utdown") Cc: Suggested-by: Alan Stern Suggested-by: Greg Kroah-Hartman Signed-off-by: Jimmy Hu --- v3 -> v4: - Replaced pr_debug() with dev_dbg(), as suggested by Greg KH. - Used guard() and scoped_guard(), as suggested by Greg KH. - Expanded 'uvc->lock' to protect 'func_unbound' and 'func_connected',=20 using a local copy in unbind() to avoid wait_event() deadlocks. v2 -> v3: - Replaced pr_info() with pr_debug() instead of uvcg_info() to stay quiet=20 and avoided potential NULL pointer dereferences on cdev->gadget, as=20 suggested by Greg KH. - Replaced kref-based lifecycle management with a completion to synchronize=20 uvc_function_unbind() with the video device release callback, avoiding=20 redundant reference counting, as suggested by Greg KH. - Added a proper comment for 'lock' in struct uvc_device to describe=20 what it protects, as suggested by Greg KH. v1 -> v2: - Renamed 'func_unbinding' to 'func_unbound' for clearer state semantics. - Added a mutex (uvc->lock) to protect the 'func_unbound' flag and ensure proper memory ordering, as suggested by Alan Stern. - Integrated kref to manage the struct uvc_device lifecycle, allowing the=20 removal of redundant buffer cleanup skip logic in uvc_v4l2_disable(). v3: https://lore.kernel.org/all/20260319084640.478695-1-hhhuuu@google.com/ v2: https://lore.kernel.org/all/20260309053107.2591494-1-hhhuuu@google.com/ v1: https://lore.kernel.org/all/20260224083955.1375032-1-hhhuuu@google.com/ drivers/usb/gadget/function/f_uvc.c | 39 ++++++++++++++++++++++++-- drivers/usb/gadget/function/uvc.h | 3 ++ drivers/usb/gadget/function/uvc_v4l2.c | 5 +++- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/drivers/usb/gadget/function/f_uvc.c b/drivers/usb/gadget/funct= ion/f_uvc.c index 494fdbc4e85b..8d404d88391c 100644 --- a/drivers/usb/gadget/function/f_uvc.c +++ b/drivers/usb/gadget/function/f_uvc.c @@ -413,6 +413,12 @@ uvc_function_disconnect(struct uvc_device *uvc) { int ret; =20 + guard(mutex)(&uvc->lock); + if (uvc->func_unbound) { + dev_dbg(&uvc->vdev.dev, "skipping function deactivate (unbound)\n"); + return; + } + if ((ret =3D usb_function_deactivate(&uvc->func)) < 0) uvcg_info(&uvc->func, "UVC disconnect failed with %d\n", ret); } @@ -431,6 +437,15 @@ static ssize_t function_name_show(struct device *dev, =20 static DEVICE_ATTR_RO(function_name); =20 +static void uvc_vdev_release(struct video_device *vdev) +{ + struct uvc_device *uvc =3D video_get_drvdata(vdev); + + /* Signal uvc_function_unbind() that the video device has been released */ + if (uvc->vdev_release_done) + complete(uvc->vdev_release_done); +} + static int uvc_register_video(struct uvc_device *uvc) { @@ -443,7 +458,7 @@ uvc_register_video(struct uvc_device *uvc) uvc->vdev.v4l2_dev->dev =3D &cdev->gadget->dev; uvc->vdev.fops =3D &uvc_v4l2_fops; uvc->vdev.ioctl_ops =3D &uvc_v4l2_ioctl_ops; - uvc->vdev.release =3D video_device_release_empty; + uvc->vdev.release =3D uvc_vdev_release; uvc->vdev.vfl_dir =3D VFL_DIR_TX; uvc->vdev.lock =3D &uvc->video.mutex; uvc->vdev.device_caps =3D V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING; @@ -659,6 +674,8 @@ uvc_function_bind(struct usb_configuration *c, struct u= sb_function *f) int ret =3D -EINVAL; =20 uvcg_info(f, "%s()\n", __func__); + scoped_guard(mutex, &uvc->lock) + uvc->func_unbound =3D false; =20 opts =3D fi_to_f_uvc_opts(f->fi); /* Sanity check the streaming endpoint module parameters. */ @@ -988,12 +1005,19 @@ static void uvc_free(struct usb_function *f) static void uvc_function_unbind(struct usb_configuration *c, struct usb_function *f) { + DECLARE_COMPLETION_ONSTACK(vdev_release_done); struct usb_composite_dev *cdev =3D c->cdev; struct uvc_device *uvc =3D to_uvc(f); struct uvc_video *video =3D &uvc->video; long wait_ret =3D 1; + bool connected; =20 uvcg_info(f, "%s()\n", __func__); + scoped_guard(mutex, &uvc->lock) { + uvc->func_unbound =3D true; + uvc->vdev_release_done =3D &vdev_release_done; + connected =3D uvc->func_connected; + } =20 kthread_cancel_work_sync(&video->hw_submit); =20 @@ -1006,7 +1030,7 @@ static void uvc_function_unbind(struct usb_configurat= ion *c, * though the video device removal uevent. Allow some time for the * application to close out before things get deleted. */ - if (uvc->func_connected) { + if (connected) { uvcg_dbg(f, "waiting for clean disconnect\n"); wait_ret =3D wait_event_interruptible_timeout(uvc->func_connected_queue, uvc->func_connected =3D=3D false, msecs_to_jiffies(500)); @@ -1017,7 +1041,10 @@ static void uvc_function_unbind(struct usb_configura= tion *c, video_unregister_device(&uvc->vdev); v4l2_device_unregister(&uvc->v4l2_dev); =20 - if (uvc->func_connected) { + scoped_guard(mutex, &uvc->lock) + connected =3D uvc->func_connected; + + if (connected) { /* * Wait for the release to occur to ensure there are no longer any * pending operations that may cause panics when resources are cleaned @@ -1029,6 +1056,10 @@ static void uvc_function_unbind(struct usb_configura= tion *c, uvcg_dbg(f, "done waiting for release with ret: %ld\n", wait_ret); } =20 + /* Wait for the video device to be released */ + wait_for_completion(&vdev_release_done); + uvc->vdev_release_done =3D NULL; + usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); kfree(uvc->control_buf); =20 @@ -1047,6 +1078,8 @@ static struct usb_function *uvc_alloc(struct usb_func= tion_instance *fi) return ERR_PTR(-ENOMEM); =20 mutex_init(&uvc->video.mutex); + mutex_init(&uvc->lock); + uvc->func_unbound =3D true; uvc->state =3D UVC_STATE_DISCONNECTED; init_waitqueue_head(&uvc->func_connected_queue); opts =3D fi_to_f_uvc_opts(fi); diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/functio= n/uvc.h index 676419a04976..7abfdd5e1eef 100644 --- a/drivers/usb/gadget/function/uvc.h +++ b/drivers/usb/gadget/function/uvc.h @@ -155,6 +155,9 @@ struct uvc_device { enum uvc_state state; struct usb_function func; struct uvc_video video; + struct completion *vdev_release_done; + struct mutex lock; /* protects func_unbound and func_connected */ + bool func_unbound; bool func_connected; wait_queue_head_t func_connected_queue; =20 diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/fu= nction/uvc_v4l2.c index ed48d38498fb..514e5930b9ca 100644 --- a/drivers/usb/gadget/function/uvc_v4l2.c +++ b/drivers/usb/gadget/function/uvc_v4l2.c @@ -574,6 +574,8 @@ uvc_v4l2_subscribe_event(struct v4l2_fh *fh, if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST) return -EINVAL; =20 + guard(mutex)(&uvc->lock); + if (sub->type =3D=3D UVC_EVENT_SETUP && uvc->func_connected) return -EBUSY; =20 @@ -595,7 +597,8 @@ static void uvc_v4l2_disable(struct uvc_device *uvc) uvc_function_disconnect(uvc); uvcg_video_disable(&uvc->video); uvcg_free_buffers(&uvc->video.queue); - uvc->func_connected =3D false; + scoped_guard(mutex, &uvc->lock) + uvc->func_connected =3D false; wake_up_interruptible(&uvc->func_connected_queue); } =20 base-commit: f338e77383789c0cae23ca3d48adcc5e9e137e3c --=20 2.53.0.959.g497ff81fa9-goog