tpm: xen-tpmfront: fix use-after-free in backend_changed()
Subsystems (2)
PATCH 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.
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.
---
Compile tested on x86_64 (W=1, no new warnings). Not run with a Xen vTPM backend.
--- 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 39 lines · On Thu, Nov 19, 2026 at 11:20:33AM +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.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.
Fixes e2683957fb26 ("drivers/tpm: add xen tpmfront interface")Co-developed-by Kaixuan Li <[email protected]>Signed-off-by Kaixuan Li <[email protected]>Signed-off-by Suyuan Wang <[email protected]>---
Compile tested onx86_64(W=1, no new warnings). Not run with a Xen vTPM backend.--- drivers/char/tpm/xen-tpmfront.c | 3 +++ 1 file changed, 3 insertions(+)drivers/char/tpm/xen-tpmfront.c +0
-0
drivers/char/tpm/xen-tpmfront.c static void backend_changed(struct xenbus_device *dev, · line 404
Quoted 8 lines
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);
Not a NAK but makes me wonder why this has not hit the wall ever.
I.e. "feeling confused".
drivers/char/tpm/xen-tpmfront.c static void backend_changed(struct xenbus_device *dev, · line 407
Quoted 7 lines
drivers/char/tpm/xen-tpmfront.c +0
-0break ;default :break ;
Br, Jarkko
Quoted 51 lines · Jarkko Sakkinen
Quoted 39 lines · On Thu, Nov 19, 2026 at 11:20:33AM +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.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.
Fixes e2683957fb26 ("drivers/tpm: add xen tpmfront interface")Co-developed-by Kaixuan Li <[email protected]>Signed-off-by Kaixuan Li <[email protected]>Signed-off-by Suyuan Wang <[email protected]>---
Compile tested onx86_64(W=1, no new warnings). Not run with a Xen vTPM backend.--- drivers/char/tpm/xen-tpmfront.c | 3 +++ 1 file changed, 3 insertions(+)drivers/char/tpm/xen-tpmfront.c +0
-0drivers/char/tpm/xen-tpmfront.c static void backend_changed(struct xenbus_device *dev, · line 404
Quoted 8 lines
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);Not a NAK but makes me wonder why this has not hit the wall ever.
Quoted 1 line
I.e. "feeling confused".
Is there any vtpm backend other than the stubdom based one?
If not, then it is quite easy: this backend never switches state to XenbusStateClosing or XenbusStateClosed.
Juergen
Jarkko Sakkinen
maintainerQuoted 51 lines · Jürgen Groß
Quoted 50 lines · Jarkko Sakkinen
Quoted 39 lines · On Thu, Nov 19, 2026 at 11:20:33AM +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.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.
Fixes e2683957fb26 ("drivers/tpm: add xen tpmfront interface")Co-developed-by Kaixuan Li <[email protected]>Signed-off-by Kaixuan Li <[email protected]>Signed-off-by Suyuan Wang <[email protected]>---
Compile tested onx86_64(W=1, no new warnings). Not run with a Xen vTPM backend.--- drivers/char/tpm/xen-tpmfront.c | 3 +++ 1 file changed, 3 insertions(+)drivers/char/tpm/xen-tpmfront.c +0
-0drivers/char/tpm/xen-tpmfront.c static void backend_changed(struct xenbus_device *dev, · line 403
Quoted 7 lines
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);Not a NAK but makes me wonder why this has not hit the wall ever.
Quoted 8 lines
Quoted 1 line
I.e. "feeling confused".
Is there any vtpm backend other than the stubdom based one?
If not, then it is quite easy: this backend never switches state to XenbusStateClosing or XenbusStateClosed.
Juergen
Should it be removed use the default-fallback?
Br, Jarkko
Quoted 55 lines · Jarkko Sakkinen
Quoted 54 lines · Jürgen Groß
Quoted 50 lines · Jarkko Sakkinen
Quoted 39 lines · On Thu, Nov 19, 2026 at 11:20:33AM +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.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.
Fixes e2683957fb26 ("drivers/tpm: add xen tpmfront interface")Co-developed-by Kaixuan Li <[email protected]>Signed-off-by Kaixuan Li <[email protected]>Signed-off-by Suyuan Wang <[email protected]>---
Compile tested onx86_64(W=1, no new warnings). Not run with a Xen vTPM backend.--- drivers/char/tpm/xen-tpmfront.c | 3 +++ 1 file changed, 3 insertions(+)drivers/char/tpm/xen-tpmfront.c +0
-0drivers/char/tpm/xen-tpmfront.c static void backend_changed(struct xenbus_device *dev, · line 403
Quoted 7 lines
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);Not a NAK but makes me wonder why this has not hit the wall ever.
Quoted 1 line
I.e. "feeling confused".
Is there any vtpm backend other than the stubdom based one?
Quoted 7 lines
Quoted 5 lines
If not, then it is quite easy: this backend never switches state to XenbusStateClosing or XenbusStateClosed.
Juergen
Should it be removed use the default-fallback?
I don't think so.
There might be other backends in future, so following the normal Xen PV-device state machine should be kept.
Juergen
Hi Juergen, Jarkko,
Thanks Juergen, that explains why nobody has run into it: with the stubdom backend the frontend never sees Closing or Closed, so this path is only reached with a backend that does close. I agree the normal state machine should stay.
So with today's backend this is latent. Jarkko, if you prefer, I can send a v2 that says so in the commit message and drops the
Cc: stable.
Thanks,
Suyuan
Quoted 71 lines · Jürgen Groß <[email protected]> 于2026年11月20日周五 00:32写道:
Quoted 55 lines · Jarkko Sakkinen
Quoted 54 lines · Jürgen Groß
Quoted 50 lines · Jarkko Sakkinen
Quoted 39 lines · On Thu, Nov 19, 2026 at 11:20:33AM +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.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.
Fixes e2683957fb26 ("drivers/tpm: add xen tpmfront interface")Co-developed-by Kaixuan Li <[email protected]>Signed-off-by Kaixuan Li <[email protected]>Signed-off-by Suyuan Wang <[email protected]>---
Compile tested onx86_64(W=1, no new warnings). Not run with a Xen vTPM backend.--- drivers/char/tpm/xen-tpmfront.c | 3 +++ 1 file changed, 3 insertions(+)drivers/char/tpm/xen-tpmfront.c +0
-0drivers/char/tpm/xen-tpmfront.c static void backend_changed(struct xenbus_device *dev, · line 403
Quoted 7 lines
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);Not a NAK but makes me wonder why this has not hit the wall ever.
Quoted 1 line
I.e. "feeling confused".
Is there any vtpm backend other than the stubdom based one?
Quoted 7 lines
Quoted 5 lines
If not, then it is quite easy: this backend never switches state to XenbusStateClosing or XenbusStateClosed.
Juergen
Should it be removed use the default-fallback?
I don't think so.
There might be other backends in future, so following the normal Xen PV-device state machine should be kept.
Juergen
Jarkko Sakkinen
maintainerQuoted 7 lines · Wong Steve
Hi Juergen, Jarkko,
Thanks Juergen, that explains why nobody has run into it: with the stubdom backend the frontend never sees Closing or Closed, so this path is only reached with a backend that does close. I agree the normal state machine should stay.
Quoted 6 lines
So with today's backend this is latent. Jarkko, if you prefer, I can send a v2 that says so in the commit message and drops the
Cc: stable.Thanks,
Suyuan
Given the feedback, go ahead.
Br, Jarkko