* [PATCH] tpm: xen-tpmfront: fix use-after-free in backend_changed()
@ 2026-11-19 11:20 Suyuan Wang via B4 Relay
2026-11-19 15:20 ` Jarkko Sakkinen
0 siblings, 1 reply; 5+ messages in thread
From: Suyuan Wang via B4 Relay @ 2026-11-19 11:20 UTC (permalink / raw)
To: jgg, jarkko; +Cc: linux-kernel, jgross, sstabellini, linux-integrity, xen-devel
From: Suyuan Wang <stevewong@hust.edu.cn>
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.
Fixes: e2683957fb26 ("drivers/tpm: add xen tpmfront interface")
Cc: stable@vger.kernel.org
Co-developed-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Suyuan Wang <stevewong@hust.edu.cn>
---
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(+)
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;
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] tpm: xen-tpmfront: fix use-after-free in backend_changed()
2026-11-19 11:20 [PATCH] tpm: xen-tpmfront: fix use-after-free in backend_changed() Suyuan Wang via B4 Relay
@ 2026-11-19 15:20 ` Jarkko Sakkinen
2026-11-19 15:35 ` Jürgen Groß
0 siblings, 1 reply; 5+ messages in thread
From: Jarkko Sakkinen @ 2026-11-19 15:20 UTC (permalink / raw)
To: shyckless
Cc: jgg, linux-kernel, jgross, sstabellini, linux-integrity, xen-devel
On Thu, Nov 19, 2026 at 11:20:33AM +0000, Suyuan Wang via B4 Relay wrote:
> From: Suyuan Wang <stevewong@hust.edu.cn>
>
> 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.
>
> Fixes: e2683957fb26 ("drivers/tpm: add xen tpmfront interface")
> Cc: stable@vger.kernel.org
> Co-developed-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
> Signed-off-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
> Signed-off-by: Suyuan Wang <stevewong@hust.edu.cn>
> ---
> 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(+)
>
> 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);
Not a NAK but makes me wonder why this has not hit the wall ever.
I.e. "feeling confused".
> break;
> default:
> break;
>
>
>
>
Br, Jarkko
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] tpm: xen-tpmfront: fix use-after-free in backend_changed()
2026-11-19 15:20 ` Jarkko Sakkinen
@ 2026-11-19 15:35 ` Jürgen Groß
2026-11-19 16:30 ` Jarkko Sakkinen
0 siblings, 1 reply; 5+ messages in thread
From: Jürgen Groß @ 2026-11-19 15:35 UTC (permalink / raw)
To: Jarkko Sakkinen, shyckless
Cc: jgg, linux-kernel, sstabellini, linux-integrity, xen-devel
[-- Attachment #1.1: Type: text/plain, Size: 2430 bytes --]
On 19.11.26 16:20, Jarkko Sakkinen wrote:
> On Thu, Nov 19, 2026 at 11:20:33AM +0000, Suyuan Wang via B4 Relay wrote:
>> From: Suyuan Wang <stevewong@hust.edu.cn>
>>
>> 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.
>>
>> Fixes: e2683957fb26 ("drivers/tpm: add xen tpmfront interface")
>> Cc: stable@vger.kernel.org
>> Co-developed-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
>> Signed-off-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
>> Signed-off-by: Suyuan Wang <stevewong@hust.edu.cn>
>> ---
>> 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(+)
>>
>> 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);
>
> Not a NAK but makes me wonder why this has not hit the wall ever.
>
> 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
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] tpm: xen-tpmfront: fix use-after-free in backend_changed()
2026-11-19 15:35 ` Jürgen Groß
@ 2026-11-19 16:30 ` Jarkko Sakkinen
2026-11-19 16:32 ` Jürgen Groß
0 siblings, 1 reply; 5+ messages in thread
From: Jarkko Sakkinen @ 2026-11-19 16:30 UTC (permalink / raw)
To: Jürgen Groß
Cc: shyckless, jgg, linux-kernel, sstabellini, linux-integrity, xen-devel
On Thu, Nov 19, 2026 at 04:35:02PM +0100, Jürgen Groß wrote:
> On 19.11.26 16:20, Jarkko Sakkinen wrote:
> > On Thu, Nov 19, 2026 at 11:20:33AM +0000, Suyuan Wang via B4 Relay wrote:
> > > From: Suyuan Wang <stevewong@hust.edu.cn>
> > >
> > > 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.
> > >
> > > Fixes: e2683957fb26 ("drivers/tpm: add xen tpmfront interface")
> > > Cc: stable@vger.kernel.org
> > > Co-developed-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
> > > Signed-off-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
> > > Signed-off-by: Suyuan Wang <stevewong@hust.edu.cn>
> > > ---
> > > 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(+)
> > >
> > > 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);
> >
> > Not a NAK but makes me wonder why this has not hit the wall ever.
> >
> > 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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] tpm: xen-tpmfront: fix use-after-free in backend_changed()
2026-11-19 16:30 ` Jarkko Sakkinen
@ 2026-11-19 16:32 ` Jürgen Groß
0 siblings, 0 replies; 5+ messages in thread
From: Jürgen Groß @ 2026-11-19 16:32 UTC (permalink / raw)
To: Jarkko Sakkinen
Cc: shyckless, jgg, linux-kernel, sstabellini, linux-integrity, xen-devel
[-- Attachment #1.1: Type: text/plain, Size: 2858 bytes --]
On 19.11.26 17:30, Jarkko Sakkinen wrote:
> On Thu, Nov 19, 2026 at 04:35:02PM +0100, Jürgen Groß wrote:
>> On 19.11.26 16:20, Jarkko Sakkinen wrote:
>>> On Thu, Nov 19, 2026 at 11:20:33AM +0000, Suyuan Wang via B4 Relay wrote:
>>>> From: Suyuan Wang <stevewong@hust.edu.cn>
>>>>
>>>> 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.
>>>>
>>>> Fixes: e2683957fb26 ("drivers/tpm: add xen tpmfront interface")
>>>> Cc: stable@vger.kernel.org
>>>> Co-developed-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
>>>> Signed-off-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
>>>> Signed-off-by: Suyuan Wang <stevewong@hust.edu.cn>
>>>> ---
>>>> 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(+)
>>>>
>>>> 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);
>>>
>>> Not a NAK but makes me wonder why this has not hit the wall ever.
>>>
>>> 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?
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
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-19 16:32 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 11:20 [PATCH] tpm: xen-tpmfront: fix use-after-free in backend_changed() Suyuan Wang via B4 Relay
2026-11-19 15:20 ` Jarkko Sakkinen
2026-11-19 15:35 ` Jürgen Groß
2026-11-19 16:30 ` Jarkko Sakkinen
2026-11-19 16:32 ` Jürgen Groß
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®