From: Jason Gunthorpe <jgg@nvidia.com>
To: "Aneesh Kumar K.V" <aneesh.kumar@kernel.org>
Cc: sashiko-reviews@lists.linux.dev, linux-coco@lists.linux.dev,
kvm@vger.kernel.org, linux-pci@vger.kernel.org
Subject: Re: [PATCH v7 09/16] tsm: Remove the device from lookup before PCI teardown
Date: Mon, 12 Oct 2026 10:01:09 -0300 [thread overview]
Message-ID: <20261012130109.GK13920@nvidia.com> (raw)
In-Reply-To: <yq5a33ub5lgt.fsf@kernel.org>
On Mon, Oct 12, 2026 at 02:44:58PM +0530, Aneesh Kumar K.V wrote:
> sashiko-bot@kernel.org writes:
>
> > Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> > - [Medium] tsm: race in tsm_register() error path leads to UAF and NULL pointer deref
> > --
> >
> > commit f9b77b7469552ae6078c3a47333908a88eec2474
> > Author: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
> >
> > tsm: Remove the device from lookup before PCI teardown
> >
> > This commit modifies the teardown flow to remove the TSM device from
> > lookup before tearing down the PCI state. It also adjusts the setup
> > flow in tsm_register() to handle PCI registration failures differently.
> >
> >> diff --git a/drivers/virt/coco/tsm-core.c b/drivers/virt/coco/tsm-core.c
> >> --- a/drivers/virt/coco/tsm-core.c
> >> +++ b/drivers/virt/coco/tsm-core.c
> >> @@ -56,26 +56,25 @@ static struct tsm_dev *alloc_tsm_dev(struct device *parent)
> > [ ... ]
> >> -static struct tsm_dev *tsm_register_pci_or_reset(struct tsm_dev *tsm_dev,
> >> - struct pci_tsm_ops *pci_ops)
> >> +static int tsm_register_pci(struct tsm_dev *tsm_dev, struct pci_tsm_ops *pci_ops)
> >> {
> > [ ... ]
> >> tsm_dev->pci_ops = pci_ops;
> >> rc = pci_tsm_register(tsm_dev);
> >> if (rc) {
> >> + tsm_dev->pci_ops = NULL;
> >
> > [Severity: Medium]
> > Does this unprotected write to tsm_dev->pci_ops introduce a race condition?
> >
> > If pci_tsm_register() returns an error early (e.g. if a driver incorrectly
> > provides both link_ops and devsec_ops, returning -EINVAL before acquiring
> > pci_tsm_rwsem), a concurrent userspace write to the connect sysfs attribute
> > might trigger connect_store().
> >
> > If connect_store() evaluates is_link_tsm() concurrently, this lockless
> > assignment to NULL on the error path could cause the compiler to reload the
> > pointer after the NULL check. Could this lead to a NULL pointer dereference
> > when accessing ->link_ops.probe?
> >
> >> dev_err(tsm_dev->dev.parent,
> >> "PCI/TSM registration failure: %d\n", rc);
> >> - device_unregister(&tsm_dev->dev);
> >> - return ERR_PTR(rc);
> >> + return rc;
> >> }
> > [ ... ]
> >> @@ -96,15 +95,22 @@ struct tsm_dev *tsm_register(struct device *parent, struct pci_tsm_ops *pci_ops)
> >> if (rc)
> >> return ERR_PTR(rc);
> >>
> >> - return tsm_register_pci_or_reset(no_free_ptr(tsm_dev), pci_ops);
> >> + rc = tsm_register_pci(tsm_dev, pci_ops);
> >> + if (rc) {
> >> + device_del(dev);
> >
> > [Severity: Medium]
> > Can this error path lead to a use-after-free if PCI registration fails?
> >
> > Since device_add() is called prior to tsm_register_pci() in tsm_register(),
> > the device is already exposed via class_find_device(). If a concurrent
> > connect_store() finds the device and proceeds to pci_tsm_connect() before
> > this error path cleans it up, it will attach the PCI context to tsm_dev.
> >
> > When this error path calls device_del() and returns, the __free(put_tsm_dev)
> > guard at the top of tsm_register() will drop the primary reference to
> > tsm_dev, freeing its memory.
> >
> > Because the attached PCI context stores the pointer without taking an
> > additional device reference, wouldn't this leave a dangling pointer in the
> > PCI context, resulting in a use-after-free on subsequent accesses?
> >
> >> + return ERR_PTR(rc);
> >> + }
> >> + return no_free_ptr(tsm_dev);
> >> }
>
>
> I think this problem already exists upstream. We should delay
> device_add() until PCI/TSM registration succeeds. The kobject event
> should also be emitted after device_add(). I will add the fix as a
> separate patch.
Sigh, trying to do all this stuff through sysfs makes such an
mess.. Nothing gets to be atomic in sysfs land so..
It looks like this ordering is to ensure that when connect appears on
the pci device it can immediately be used with the tsm that triggered
it's creation. I'm not sure this is valuable but I haven't worked out
how a userspace is even supposed to exist with all these troublesome
races.
If you swap the order and remove the extra events then userspace can
see a connect sysfs but no tsm0 device. I think this is possibly
better as userspace will want to obtain the tsm handle then process
all the devices affiliated with it. So waiting around for a tsm and
then knowning that every connect is already there strikes me as easier
to work with. IDK
The bigger issue here is I have no idea how the userspace is supposed
to work. When my imagined 'systemd device accept d' starts it will see
a sytem with no TSM and no connect sysfs.
A tsm may eventually appear when the TSM module gets loaded, eg it
loads by udev through the smccc bus on CCA.
Or it might need manual module loading because it is a SW DOE based
tsm.
Or it might never ever appear.
So what is the device-accept-d logic supposed to even look like?
Before touching this we need to come up with a draft
systemd-device-accept-d shaped thing that does the TSM startup before
drivers bind. Then we can see fully how it can work. I asked someone
to start working on this, but I expected there to be a ready story on
how it should work, not more design work.
One possible answer is more TSM stuff has to be built in so we don't
hotplug the default TSM. Then the userspace simply checks for a TSM,
if present it connects it, otherwise maybe it thinks about using the
DOE one just to get an attestation log.
Module auto loading looks troublesome too, if we want to use the SW
based DOE TSM then how does device-accept-d trigger the module load
when there isn't even any sysfs?
Once again I'm thinking if the sysfs as a mistake and wondering if it
works better using a /dev/tsm.. :(
In any event, lets not get everything tangled into everything
else. This series is about viommu, lets keep track of these other
issues and keep focusing on viommu..
Jason
next prev parent reply other threads:[~2026-10-12 13:01 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-08 5:59 [PATCH v7 00/16] iommufd: vIOMMUs and TSM guest requests for confidential guests Aneesh Kumar K.V (Arm)
2026-10-08 5:59 ` [PATCH v7 01/16] KVM: Introduce file_to_kvm_<arch>() infrastructure Aneesh Kumar K.V (Arm)
2026-10-08 6:11 ` sashiko-bot
2026-10-08 5:59 ` [PATCH v7 02/16] KVM: Add file back-pointer to struct kvm Aneesh Kumar K.V (Arm)
2026-10-08 6:16 ` sashiko-bot
2026-10-08 5:59 ` [PATCH v7 03/16] KVM: x86: Use file_to_kvm_x86() in SEV Aneesh Kumar K.V (Arm)
2026-10-08 6:07 ` sashiko-bot
2026-10-08 5:59 ` [PATCH v7 04/16] KVM/vfio: Use file-based reference counting for KVM Aneesh Kumar K.V (Arm)
2026-10-08 6:27 ` sashiko-bot
2026-10-08 5:59 ` [PATCH v7 05/16] KVM: Restrict kvm_get_kvm/kvm_put_kvm export to internal KVM modules Aneesh Kumar K.V (Arm)
2026-10-08 6:08 ` sashiko-bot
2026-10-08 5:59 ` [PATCH v7 06/16] KVM: Remove unused file_is_kvm Aneesh Kumar K.V (Arm)
2026-10-08 6:06 ` sashiko-bot
2026-10-08 5:59 ` [PATCH v7 07/16] iommufd/device: Associate KVM file pointer with iommufd_device Aneesh Kumar K.V (Arm)
2026-10-08 6:20 ` sashiko-bot
2026-10-08 5:59 ` [PATCH v7 08/16] iommufd/viommu: Keep a reference to the KVM file Aneesh Kumar K.V (Arm)
2026-10-08 6:10 ` sashiko-bot
2026-10-08 5:59 ` [PATCH v7 09/16] tsm: Remove the device from lookup before PCI teardown Aneesh Kumar K.V (Arm)
2026-10-08 6:15 ` sashiko-bot
2026-10-12 9:14 ` Aneesh Kumar K.V
2026-10-12 13:01 ` Jason Gunthorpe [this message]
2026-10-13 8:49 ` Aneesh Kumar K.V
2026-10-13 11:57 ` Jason Gunthorpe
2026-10-08 5:59 ` [PATCH v7 10/16] iommufd: Add the vdevice TSM request ioctl Aneesh Kumar K.V (Arm)
2026-10-08 6:10 ` sashiko-bot
2026-10-08 5:59 ` [PATCH v7 11/16] PCI/TSM: Remove the legacy guest request interface Aneesh Kumar K.V (Arm)
2026-10-08 6:11 ` sashiko-bot
2026-10-08 5:59 ` [PATCH v7 12/16] PCI/TSM: Add vIOMMU-bound contexts for vdevices Aneesh Kumar K.V (Arm)
2026-10-08 6:18 ` sashiko-bot
2026-10-08 5:59 ` [PATCH v7 13/16] iommufd/viommu: Select vIOMMU operations before allocation Aneesh Kumar K.V (Arm)
2026-10-08 6:27 ` sashiko-bot
2026-10-12 9:37 ` Aneesh Kumar K.V
2026-10-12 13:05 ` Jason Gunthorpe
2026-10-13 9:11 ` Aneesh Kumar K.V
2026-10-13 12:33 ` Jason Gunthorpe
2026-10-08 5:59 ` [PATCH v7 14/16] iommufd/viommu: Allow PCI TSM backends to provide vIOMMU operations Aneesh Kumar K.V (Arm)
2026-10-08 6:19 ` sashiko-bot
2026-10-12 9:55 ` Aneesh Kumar K.V
2026-10-08 5:59 ` [PATCH v7 15/16] iommufd: Allow vIOMMUs without a parent HWPT Aneesh Kumar K.V (Arm)
2026-10-08 6:24 ` sashiko-bot
2026-10-08 5:59 ` [PATCH v7 16/16] PCI/TSM: wait for vdevice contexts before removing a DSM Aneesh Kumar K.V (Arm)
2026-10-08 6:25 ` sashiko-bot
2026-10-12 20:34 ` Bjorn Helgaas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261012130109.GK13920@nvidia.com \
--to=jgg@nvidia.com \
--cc=aneesh.kumar@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=linux-coco@lists.linux.dev \
--cc=linux-pci@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox