tpm: xen-tpmfront: fix use-after-free in backend_changed()
Subsystems (2)
PATCH v3 1/1 tpm: xen-tpmfront: fix use-after-free in backend_changed()
From: Suyuan Wang <[email protected]>
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.
---
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/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
--- drivers/char/tpm/xen-tpmfront.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/char/tpm/xen-tpmfront.c +3 -0
@@ -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 ;
Jarkko Sakkinen
maintainerQuoted 30 lines · On Fri, Nov 20, 2026 at 04:26:16AM +0000, Suyuan Wang via B4 Relay wrote:
From: Suyuan Wang <[email protected]>
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. Thetpm_chipregistered by xen-tpmfront has dev as its parent and holds a reference to it, buttpmfront_remove()drops that reference when it unregisters the chip. Sodevice_unregister()can release the last reference and free thexenbus_device, andxenbus_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 <[email protected]>Signed-off-by Kaixuan Li <[email protected]>Signed-off-by Suyuan Wang <[email protected]>
Quoted 7 lines
---
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.
There's no use-after-free to fix.
Br, Jarkko
Hi Jarkko,
Quoted 1 line · Jarkko Sakkinen
There's no use-after-free to fix.
OK, with the only backend in use never closing, there is nothing to hit here. I'll drop this patch. Thanks to you and Juergen for looking at it.
Thanks,
Suyuan
Quoted 43 lines · Jarkko Sakkinen <[email protected]> 于2026年11月20日周五 16:58写道:
Quoted 30 lines · On Fri, Nov 20, 2026 at 04:26:16AM +0000, Suyuan Wang via B4 Relay wrote:
From: Suyuan Wang <[email protected]>
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. Thetpm_chipregistered by xen-tpmfront has dev as its parent and holds a reference to it, buttpmfront_remove()drops that reference when it unregisters the chip. Sodevice_unregister()can release the last reference and free thexenbus_device, andxenbus_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 <[email protected]>Signed-off-by Kaixuan Li <[email protected]>Signed-off-by Suyuan Wang <[email protected]>Quoted 7 lines
---
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.There's no use-after-free to fix.
Br, Jarkko