Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BD92613C3F2 for ; Tue, 2 Dec 2025 01:59:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764640794; cv=none; b=HP2Y4LX9wEkYPXSxIHKLiPmamDqvuZuQlMpj/iS/Y4aX61xnDIKPg4eGFfrADRyI+T+6vy2AZQCaVoenB0q1CXfwSxAi2mMsgcJWJ9G1NAyDX7+hpQZH4umURijRXq0g8jLSf6rsYLTJxhD2mJhtahP862xE3ckqbf9YEAaz1s4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764640794; c=relaxed/simple; bh=xRegb8BsyteZArSdrUpaAS53LPPnipAf0oz0vXwLuU0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bX2XnyA8BCxpa7/K6CiSP1yjXSt2d0nnTyVZ3Sm/eSXYifHd4exHYyjTlpKrvzsB/jxymmOYOzYuBplnLglW97RzhBYd/q7MygHRsPHjKnhX00h2TFjxjw9sZABn/fXpZDwPPtLQc5Jjsf24OdaShgHahe0f6YK3PaO09UjuXrw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=H7UVz5zI; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="H7UVz5zI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 63E2AC19421; Tue, 2 Dec 2025 01:59:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1764640794; bh=xRegb8BsyteZArSdrUpaAS53LPPnipAf0oz0vXwLuU0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=H7UVz5zIwEEo6tWtQsZv6oPfi0O7CuYl9dctYUU591drBFRcb47iITONJ1xcKs0Rx PM/RTzvscsYP8wOxulm9bU62mUQ0JuFmomhVamiRpyhl1ZnYgDf+VfRBIQhD9M5sjY UQEmsx7/leqwh44Wl3VYigS2nS5xILIXZaGdRCoMr9Saxsm3FfeT5pSOwDtojmlLuN MRaZ7YGgAIXzuQr8js4jLgnVrpbxBmU+Vw6MOQY32bPdXmSdNvLo5/XvPUyFnZlzrB do+sLvZEZ7vEnlarTh/WkLFZaY+LHKmzXp4/d/pwpIfpk8v4sPdyDSM+JsQQDTYosK Rd2fSNIUvRtlg== From: Sasha Levin To: stable@vger.kernel.org Cc: Jimmy Hu , stable , Greg Kroah-Hartman , Sasha Levin Subject: [PATCH 6.6.y 2/2] usb: gadget: udc: fix use-after-free in usb_gadget_state_work Date: Mon, 1 Dec 2025 20:59:49 -0500 Message-ID: <20251202015949.1613366-2-sashal@kernel.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251202015949.1613366-1-sashal@kernel.org> References: <2025120122-shelter-unlit-af6a@gregkh> <20251202015949.1613366-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Jimmy Hu [ Upstream commit baeb66fbd4201d1c4325074e78b1f557dff89b5b ] A race condition during gadget teardown can lead to a use-after-free in usb_gadget_state_work(), as reported by KASAN: BUG: KASAN: invalid-access in sysfs_notify+0x2c/0xd0 Workqueue: events usb_gadget_state_work The fundamental race occurs because a concurrent event (e.g., an interrupt) can call usb_gadget_set_state() and schedule gadget->work at any time during the cleanup process in usb_del_gadget(). Commit 399a45e5237c ("usb: gadget: core: flush gadget workqueue after device removal") attempted to fix this by moving flush_work() to after device_del(). However, this does not fully solve the race, as a new work item can still be scheduled *after* flush_work() completes but before the gadget's memory is freed, leading to the same use-after-free. This patch fixes the race condition robustly by introducing a 'teardown' flag and a 'state_lock' spinlock to the usb_gadget struct. The flag is set during cleanup in usb_del_gadget() *before* calling flush_work() to prevent any new work from being scheduled once cleanup has commenced. The scheduling site, usb_gadget_set_state(), now checks this flag under the lock before queueing the work, thus safely closing the race window. Fixes: 5702f75375aa9 ("usb: gadget: udc-core: move sysfs_notify() to a workqueue") Cc: stable Signed-off-by: Jimmy Hu Link: https://patch.msgid.link/20251023054945.233861-1-hhhuuu@google.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/usb/gadget/udc/core.c | 17 ++++++++++++++++- include/linux/usb/gadget.h | 5 +++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c index a2a760e716ecf..0cad5cf195431 100644 --- a/drivers/usb/gadget/udc/core.c +++ b/drivers/usb/gadget/udc/core.c @@ -1121,8 +1121,13 @@ static void usb_gadget_state_work(struct work_struct *work) void usb_gadget_set_state(struct usb_gadget *gadget, enum usb_device_state state) { + unsigned long flags; + + spin_lock_irqsave(&gadget->state_lock, flags); gadget->state = state; - schedule_work(&gadget->work); + if (!gadget->teardown) + schedule_work(&gadget->work); + spin_unlock_irqrestore(&gadget->state_lock, flags); trace_usb_gadget_set_state(gadget, 0); } EXPORT_SYMBOL_GPL(usb_gadget_set_state); @@ -1356,6 +1361,8 @@ static void usb_udc_nop_release(struct device *dev) void usb_initialize_gadget(struct device *parent, struct usb_gadget *gadget, void (*release)(struct device *dev)) { + spin_lock_init(&gadget->state_lock); + gadget->teardown = false; INIT_WORK(&gadget->work, usb_gadget_state_work); gadget->dev.parent = parent; @@ -1530,6 +1537,7 @@ EXPORT_SYMBOL_GPL(usb_add_gadget_udc); void usb_del_gadget(struct usb_gadget *gadget) { struct usb_udc *udc = gadget->udc; + unsigned long flags; if (!udc) return; @@ -1543,6 +1551,13 @@ void usb_del_gadget(struct usb_gadget *gadget) kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE); sysfs_remove_link(&udc->dev.kobj, "gadget"); device_del(&gadget->dev); + /* + * Set the teardown flag before flushing the work to prevent new work + * from being scheduled while we are cleaning up. + */ + spin_lock_irqsave(&gadget->state_lock, flags); + gadget->teardown = true; + spin_unlock_irqrestore(&gadget->state_lock, flags); flush_work(&gadget->work); ida_free(&gadget_id_numbers, gadget->id_number); cancel_work_sync(&udc->vbus_work); diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h index aa831e16c3d39..1a13d0c01b0aa 100644 --- a/include/linux/usb/gadget.h +++ b/include/linux/usb/gadget.h @@ -374,6 +374,9 @@ struct usb_gadget_ops { * can handle. The UDC must support this and all slower speeds and lower * number of lanes. * @state: the state we are now (attached, suspended, configured, etc) + * @state_lock: Spinlock protecting the `state` and `teardown` members. + * @teardown: True if the device is undergoing teardown, used to prevent + * new work from being scheduled during cleanup. * @name: Identifies the controller hardware type. Used in diagnostics * and sometimes configuration. * @dev: Driver model state for this abstract device. @@ -449,6 +452,8 @@ struct usb_gadget { enum usb_ssp_rate max_ssp_rate; enum usb_device_state state; + spinlock_t state_lock; + bool teardown; const char *name; struct device dev; unsigned isoch_delay; -- 2.51.0