[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v3] tpm: xen-tpmfront: fix use-after-free in backend_changed()


  • To: jgg@xxxxxxxx, jarkko@xxxxxxxxxx
  • From: Suyuan Wang via B4 Relay <devnull+shyckless.gmail.com@xxxxxxxxxx>
  • Date: Fri, 20 Nov 2026 04:26:16 +0000
  • Arc-authentication-results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77
  • Arc-message-signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795148777; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:X-B4-Tracking:X-Change-ID:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To; bh=qGC3Y4r7T20cQuoO4kDMAGiRID8rez9I459XavcFz0Y=; b=KBdHxITGeT/rH2lX2xEroxM+gSZ3kt7GpvvCwpPjedp9MCRvdcXVgC1HEU6dJfJe+Htr RZrfBa4m61HZJux+H1Q1jX5KM03GbmFgrCCioXq4A/HXMlGir87QyT/wpqRURN6QZ8Ndx +kKHdlps4kuLQOXQTqNpcbsHp1lPzw9DzgZtLkV2uJwI7E7RU2ywvUyo0k8t5hM3DJmYf RekJ43CFVLw9DNhXfOVVGFR2IFAvOqWF1Ot+ofg8FK2NNSnY5CW6y7KhRhhFeoG3do9m9 cGMJSH+rYqlJVRckWciXvX7TY4uaGBXYD4ikGdvaQp+HqkSGQ4FR1w8shrF1rLdUrfg==
  • Arc-seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795148777; b=KrVB86G+VLdQ5lvzKGgZ7mRSE7rBgjZJG3FD2Yayw+ObSZcjpALDLm9nWy6SqYSnd4yR kDTXG/0vU2lYcNH7AB6pwfD05yVA0Z1vW4zuvMraGV9sjQwvQXkz010iuNrnCLrvLrFUH qO2UNdbfIEX14j31eBOsx5xIKwQL/l8BlBq/EX4BYrq6DttldvRksKC51kqbtdioGwaNh b/EiraX4L5mgRkzSlhk+U7Yb0W4tvmD4Dbw0CJkHbpWboolwqARKmrNZ2fuOWCuYyivgv 2JYbRo9etajRRZnRuATlmJIuRpCRh22Yk60orvc6OWE7hW9AmZhaVVSfEHUH3AADSsw==
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=k20260515 header.d=kernel.org header.i="@kernel.org" header.h="From:Date:Subject:To:Cc:Reply-To"
  • Authentication-results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77
  • Cc: sstabellini@xxxxxxxxxx, jgross@xxxxxxxx, xen-devel@xxxxxxxxxxxxxxxxxxxx, linux-kernel@xxxxxxxxxxxxxxx, linux-integrity@xxxxxxxxxxxxxxx
  • Delivery-date: Fri, 20 Nov 2026 04:26:38 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

From: Suyuan Wang <stevewong@xxxxxxxxxxx>

When the backend moves to Closing or Closed, backend_changed()
unregisters the device and then keeps using it:

        device_unregister(&dev->dev);
        xenbus_frontend_closed(dev);

The xenbus watch callback that calls backend_changed() does not hold a
reference to the device. The tpm_chip registered by xen-tpmfront has dev
as its parent and holds a reference to it, but tpmfront_remove() drops
that reference when it unregisters the chip. So device_unregister() can
release the last reference and free the xenbus_device, and
xenbus_frontend_closed(dev) then accesses freed memory.

The stubdom based vTPM backend, the only one in use today, never
switches to Closing or Closed, so this path is not reached with it. It
is reached with any backend that follows the normal xenbus state
machine on shutdown.

Take a temporary reference before unregistering and drop it after
xenbus_frontend_closed(). This keeps dev alive for the last access and
keeps the existing order, in which TPM operations are stopped before
the frontend is marked Closed.

Found with a static analysis tool and checked by hand.

Co-developed-by: Kaixuan Li <kaixuan.li@xxxxxxxxxx>
Signed-off-by: Kaixuan Li <kaixuan.li@xxxxxxxxxx>
Signed-off-by: Suyuan Wang <stevewong@xxxxxxxxxxx>
---
Changes in v3:
- Drop the Fixes tag (Jarkko). No code change.

Changes in v2:
- Say that the stubdom vTPM backend never reaches this path, and don't
  ask for a stable backport (Juergen, Jarkko). No code change.

Compile tested on x86_64 (W=1, no new warnings). Not run with a Xen vTPM
backend.

v2: 
https://lore.kernel.org/all/20261119-xen-tpmfront-v2-1-e0e51c36360d@xxxxxxxxx/
v1: 
https://lore.kernel.org/all/20261119-xen-tpmfront-v1-1-bfabbbed462a@xxxxxxxxx/
---
 drivers/char/tpm/xen-tpmfront.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/char/tpm/xen-tpmfront.c b/drivers/char/tpm/xen-tpmfront.c
index 3c0cce57c4ae..1be925cca535 100644
--- a/drivers/char/tpm/xen-tpmfront.c
+++ b/drivers/char/tpm/xen-tpmfront.c
@@ -397,8 +397,11 @@ static void backend_changed(struct xenbus_device *dev,
 
        case XenbusStateClosing:
        case XenbusStateClosed:
+               /* device_unregister() can drop the last reference to dev */
+               get_device(&dev->dev);
                device_unregister(&dev->dev);
                xenbus_frontend_closed(dev);
+               put_device(&dev->dev);
                break;
        default:
                break;







 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.