From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796354660; b=PHN9oTZwASTYXt8LoSpwJZ2/CKBM4xK8aGKjbQwydck6la6HFFeci9Xs5/nT3jL7+jSW /U8T3nX+1fxG6h365w0rfP/VfxPTNC2DNEtI1p+gE/cIxsJ1bgozTqkcWED/1/4Eu34W2 8vTw+9JN8FDEZSVRGvv1b0dpfzVa+sqbS5nYzxDAoI57791VxYtQKgvCrHF2ZqNJG7u+T M8YML29hGmmt71qXji9omM0QSwzAyY+6cTkGBonHo15wKKFW2QPkFOjfnJOYkgGpZd6Vq qAbAvBZsfiCsV6YbNcuvx+PHQYTiRRPj+8waTyd9vgaSKzei/P4O7+m5ipDY8NzxTFg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796354660; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition: Content-Transfer-Encoding:In-Reply-To; bh=Gre501+DtnleR5rG7VnIJKL6kJaQgzO59SlgDZHSZsw=; b=2Q4H/jGbg0tEV83MBiHTOn0IEESP3WXo3Zbwz3QdcjoeBx/Ec9Uv9P36UoQY1ZjVbs02 vk9nhk/PXWSjRl+KStOhxSI7lOszHbIs1kLjBf/x7L080mgoUr7RiwG90/P0xxAnhXiU7 zCNXTs0CRuDyBpi8kr3zqCHweo+IKnzmzBN7XXZlVqrAwRoQZjQb6knhaH7e4Q8/EGn5s +edkbpeF0vtxTE8zfkXhkufts6ISDi//Cqf43swPn0SgI4gYoH0nF1ziIudhUFR3EidWv C34mC1EjxfTMJ7yb1cEfHRZZor4R7TV/pm8/qYjvBTdDjj2KP6PgFfIsqGo3eU8Z43w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=k3yvO6O0; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3763C606A322; Fri, 4 Dec 2026 03:24:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=k3yvO6O0 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E7B431F000C2; Fri, 4 Dec 2026 03:24:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796354660; bh=Gre501+DtnleR5rG7VnIJKL6kJaQgzO59SlgDZHSZsw=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=k3yvO6O00kUBr4Q0lbdX97ECrwHJzTI2AIdwbhOmghOaODF5wbVPpC4Xdlaxde33z ObqP4Q3h9dfCPqFw4mYyqL3N+z/3X77BuFZUTRic5W3MvHJJPZ94h9Rl15jkh4oNZT 5dwRvvi7TvdSzYXiGPFEeVJYcemk6zP9x2LfNPdk= Date: Thu, 3 Dec 2026 19:15:41 +0100 From: Greg Kroah-Hartman To: =?iso-8859-1?Q?Iv=E1n?= Ezequiel Rodriguez Cc: Heikki Krogerus , Fan Wu , Huang Wei , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [RESEND PATCH] usb: typec: ucsi: acpi: fix use-after-free on driver removal Message-ID: <2026120324-obsessed-frightful-8078@gregkh> References: <20261026125350.816530-1-ivanrwcm25@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20261026125350.816530-1-ivanrwcm25@gmail.com> On Mon, Oct 26, 2026 at 09:53:50AM -0300, Iván Ezequiel Rodriguez wrote: > ucsi_acpi_remove() destroys the UCSI instance before removing the ACPI > notify handler: > > ucsi_unregister(ua->ucsi); > ucsi_destroy(ua->ucsi); > > acpi_remove_notify_handler(...); > > ucsi_acpi_notify() dereferences ua->ucsi, so a notify arriving after > ucsi_destroy() uses freed memory: > > CPU0 CPU1 > ---- ---- > ucsi_acpi_remove() > ucsi_unregister() > ucsi_destroy() > kfree(ucsi) > ucsi_acpi_notify() > ua->ucsi->ops->read_cci() <-- UAF > > Simply removing the handler before ucsi_unregister() is not correct > either. ucsi_unregister() drains work that needs the notify path to make > progress: ucsi_handle_connector_change() issues GET_CONNECTOR_STATUS and > ucsi_unregister_port() drains and destroys con->wq, and those commands > block in wait_for_completion_timeout() on ucsi->complete for up to > UCSI_TIMEOUT_MS. That completion is signalled only from > ucsi_notify_common(), i.e. from the notify handler. Tearing the handler > down first would leave cancel_work_sync() and destroy_workqueue() > waiting the full timeout for a completion that can no longer arrive. > > Moving the removal between ucsi_unregister() and ucsi_destroy() is not > sufficient on its own: at that point the connector array has already > been freed, so a late notify reaching ucsi_connector_change() would > queue work on a freed connector. > > Teardown therefore needs two properties at the same time: no new > connector changes once connectors start going away, but the notify path > still available for command and acknowledge completions until that work > has been drained. Whether the PPM actually produces those completions is > a firmware matter; what changes here is that the path able to deliver > them is no longer torn down first. > > Introduce a quiescing state, local to the ACPI backend, that provides > both. ucsi_acpi_remove() sets ua->quiescing under ua->notify_lock before > calling ucsi_unregister(); ucsi_acpi_notify() takes the same lock and, > when quiescing, reduces the CCI to the bits that ucsi_notify_common() > consumes for completions. ucsi_notify_common() looks at exactly > UCSI_CCI_BUSY, the connector number, UCSI_CCI_ACK_COMPLETE and > UCSI_CCI_COMMAND_COMPLETE; keeping the first and the last two preserves > the completion and bogus-data behaviour unchanged, while clearing the > connector number makes ucsi_connector_change() unreachable. The CCI that > the command path inspects is unaffected, because > ucsi_sync_control_common() re-reads it from the interface after the > completion. > > The resulting order is: > > mutex_lock(&ua->notify_lock); > ua->quiescing = true; -- no new connector work > mutex_unlock(&ua->notify_lock); > > ucsi_unregister(); -- drains work, notify path still > available for completions > > acpi_remove_notify_handler(); -- unlinks, then flushes > kacpi_notify_wq > > ucsi_destroy(); -- no notify can be in flight > > which gives the following happens-before chain: > > - A notify that acquires notify_lock before ucsi_acpi_remove() runs to > completion while remove() waits on the lock, so any schedule_work() it > performs happens before ucsi_unregister() starts cancelling. > - A notify that acquires notify_lock after remove() released it observes > quiescing == true, so it cannot reach ucsi_connector_change() and > cannot touch ucsi->connector. > - acpi_remove_notify_handler() unlinks the handler and then calls > acpi_os_wait_events_complete(), which flushes kacpi_notify_wq, so a > notify already dispatched on another CPU has returned before > ucsi_destroy() frees the instance. > > notify_lock is never held across ucsi_unregister() or > acpi_remove_notify_handler(); holding it there would deadlock against > the notify work those calls wait for. It is only ever taken as a leaf: > ucsi_notify_common() and ucsi_connector_change() take no locks, so it > cannot invert against ucsi->ppm_lock or con->lock, which the drained > work holds while waiting for the completion. The handler runs from > kacpi_notify_wq via acpi_os_execute(OSL_NOTIFY_HANDLER, ...), i.e. in > process context, so sleeping on the mutex is allowed. > > The probe error path already removes the handler before ucsi_destroy() > and is left unchanged. that's a horrid wall-of-text :( > > Fixes: f56de278e8ec ("usb: typec: ucsi: acpi: Move to the new API") > Cc: stable@vger.kernel.org > Reported-by: Fan Wu > Link: https://lore.kernel.org/all/20260718021142.3146566-1-fanwu01@zju.edu.cn/ > Signed-off-by: Iván Ezequiel Rodriguez > Reviewed-by: Huang Wei > Reviewed-by: Heikki Krogerus > --- > Resend as a new thread per Heikki's request. Same code as v2 > <20260903232121.271776-1-ivanrwcm25@gmail.com>, with collected > Reviewed-by tags. > > drivers/usb/typec/ucsi/ucsi_acpi.c | 53 ++++++++++++++++++++++++++++-- > 1 file changed, 51 insertions(+), 2 deletions(-) > > diff --git a/drivers/usb/typec/ucsi/ucsi_acpi.c b/drivers/usb/typec/ucsi/ucsi_acpi.c > index 5697ccc24ec3..2dd8e6d00c73 100644 > --- a/drivers/usb/typec/ucsi/ucsi_acpi.c > +++ b/drivers/usb/typec/ucsi/ucsi_acpi.c > @@ -24,6 +24,15 @@ struct ucsi_acpi { > bool check_bogus_event; > guid_t guid; > u64 cmd; > + /* > + * notify_lock serialises ucsi_acpi_notify() against the start of > + * teardown, so that @quiescing is observed by every notify that has > + * not yet run. It must not be held across ucsi_unregister(), whose > + * drained work may depend on the notify path, nor across > + * acpi_remove_notify_handler(), which flushes notify work. > + */ > + struct mutex notify_lock; > + bool quiescing; llms love to add boolean flags as it's "simple" for them to figure out. You have a lock, why do you need yet-another-state flag here? There's no other way to do this without that boolean? > }; > > static int ucsi_acpi_dsm(struct ucsi_acpi *ua, int func) > @@ -213,11 +222,31 @@ static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data) > u32 cci; > int ret; > > + mutex_lock(&ua->notify_lock); guard(). llms don't know about modern kernel development, but that doesn't mean that you should let them do your thinking for you. > + > ret = ua->ucsi->ops->read_cci(ua->ucsi, &cci); > if (ret) > - return; > + goto out_unlock; > + > + /* > + * Once teardown has started the connectors are being unregistered and > + * freed, so a connector change must not be reported any more. Command > + * and acknowledge completions must still be able to reach the core: > + * ucsi_unregister() drains connector and partner work that can be > + * blocked in wait_for_completion_timeout() on ucsi->complete, and that > + * completion is only signalled from here. Keep exactly the bits that > + * ucsi_notify_common() needs for that, which drops the connector > + * number and with it the path to ucsi_connector_change(). The busy > + * indicator is kept so that bogus CCI data is still ignored. > + */ why the huge comment? Is that really needed? > + if (ua->quiescing) > + cci &= UCSI_CCI_BUSY | UCSI_CCI_ACK_COMPLETE | > + UCSI_CCI_COMMAND_COMPLETE; > > ucsi_notify_common(ua->ucsi, cci); > + > +out_unlock: > + mutex_unlock(&ua->notify_lock); > } > > static int ucsi_acpi_probe(struct platform_device *pdev) > @@ -253,6 +282,8 @@ static int ucsi_acpi_probe(struct platform_device *pdev) > > ua->dev = &pdev->dev; > > + mutex_init(&ua->notify_lock); > + > id = dmi_first_match(ucsi_acpi_quirks); > if (id) > ops = id->driver_data; > @@ -290,11 +321,29 @@ static void ucsi_acpi_remove(struct platform_device *pdev) > { > struct ucsi_acpi *ua = platform_get_drvdata(pdev); > > + /* > + * Stop reporting connector changes, but keep the notify handler > + * installed so that the work ucsi_unregister() drains can still be > + * reached by the command completions it may be waiting for. Any notify > + * that already passed this point runs to completion first, so no > + * connector work can be queued once ucsi_unregister() starts. > + */ > + mutex_lock(&ua->notify_lock); > + ua->quiescing = true; > + mutex_unlock(&ua->notify_lock); guard. and really, a whole lock for a boolean? this feels wrong... greg k-h