Linux PCI subsystem development
 help / color / mirror / Atom feed
From: Aneesh Kumar K.V <aneesh.kumar@kernel.org>
To: sashiko-reviews@lists.linux.dev, linux-coco@lists.linux.dev,
	Jason Gunthorpe <jgg@nvidia.com>
Cc: 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 14:44:58 +0530	[thread overview]
Message-ID: <yq5a33ub5lgt.fsf@kernel.org> (raw)
In-Reply-To: <sashiko-outbox-163673@kernel.org>

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.

-aneesh

  reply	other threads:[~2026-10-12  9:15 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 [this message]
2026-10-12 13:01       ` Jason Gunthorpe
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=yq5a33ub5lgt.fsf@kernel.org \
    --to=aneesh.kumar@kernel.org \
    --cc=jgg@nvidia.com \
    --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