Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Aneesh Kumar K.V <aneesh.kumar@kernel.org>
To: sashiko-reviews@lists.linux.dev
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v5 2/5] iommufd/device: Associate KVM file pointer with iommufd_device
Date: Tue, 26 May 2026 13:47:07 +0530	[thread overview]
Message-ID: <yq5av7cashz0.fsf@kernel.org> (raw)
In-Reply-To: <20260525203341.9FE971F000E9@smtp.kernel.org>

sashiko-bot@kernel.org writes:

> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [Critical] Use-After-Free / Lifetime Mismatch due to borrowed KVM file reference
> --
>
> commit e5661ddf7c17ac12b18e45c9b4e38b80511ba1c0
> Author: Shameer Kolothum <skolothumtho@nvidia.com>
>
> iommufd/device: Associate KVM file pointer with iommufd_device
>     
> This commit extends iommufd_device_bind() to accept the device's KVM file
> and stores it in the iommufd_device struct. This provides TSM vDevice support
> access to the KVM associated with a VFIO device.
>
>> diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
>> index 170a7005f0bc8..718abdc0e627a 100644
>> --- a/drivers/iommu/iommufd/device.c
>> +++ b/drivers/iommu/iommufd/device.c
>> @@ -216,7 +217,9 @@ void iommufd_device_destroy(struct iommufd_object *obj)
>>   * The caller must undo this with iommufd_device_unbind()
>>   */
>>  struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
>> -					   struct device *dev, u32 *id)
>> +					   struct device *dev,
>> +					   struct file *kvm_file,
>> +					   u32 *id)
>>  {
>>  	struct iommufd_device *idev;
>>  	struct iommufd_group *igroup;
>
> [ ... ]
>
>> @@ -266,6 +269,8 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
>>  	if (!iommufd_selftest_is_mock_dev(dev))
>>  		iommufd_ctx_get(ictx);
>>  	idev->dev = dev;
>> +	/* reference is already taken in vfio_df_ioctl_bind_iommufd() */
>> +	idev->kvm_file = kvm_file;
>
> [Severity: Critical]
> Does this borrowed reference create a potential use-after-free regression if
> the iommufd_device outlives the VFIO device?
>
> If a concurrent ioctl on the iommufd device is maliciously stalled (for
> example, using userfaultfd during a copy_to_user in IOMMU_GET_HW_INFO),
> iommufd_object_remove() can time out after 60 seconds and return -EBUSY.
> This would fail to remove the idev object, leaving it alive in the iommufd
> xarray.
>
> VFIO owns the KVM file reference and unconditionally drops it when the VFIO
> device file is closed. If VFIO drops this reference while idev remains alive,
> the file could be freed, leaving idev->kvm_file as a dangling pointer.
>
> If the stalled ioctl is subsequently allowed to finish, a new
> IOMMU_VIOMMU_ALLOC ioctl could look up the leaked idev and attempt to call
> get_file(idev->kvm_file), resulting in a use-after-free on the freed struct
> file.
>
> Should this function take its own independent reference to kvm_file to
> ensure the file remains valid for the entire lifetime of the iommufd_device?
>

I don’t think we need to handle this because closing a VFIO cdev will
result in an iommufd unbind. That is, we should not find an idev that
outlives a VFIO cdev.

Shouldn’t iommufd_object_remove() failing to remove an object be
considered a bug?

-aneesh

  reply	other threads:[~2026-05-26  8:17 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-25 15:48 [PATCH v5 0/5] Add iommufd ioctls to support TSM operations Aneesh Kumar K.V (Arm)
2026-05-25 15:48 ` [PATCH v5 1/5] vfio: cache KVM VM file references instead of raw struct kvm pointers Aneesh Kumar K.V (Arm)
2026-05-25 16:47   ` sashiko-bot
2026-05-26  8:11     ` Aneesh Kumar K.V
2026-05-26 10:52   ` Anthony Krowiak
2026-05-25 15:48 ` [PATCH v5 2/5] iommufd/device: Associate KVM file pointer with iommufd_device Aneesh Kumar K.V (Arm)
2026-05-25 20:33   ` sashiko-bot
2026-05-26  8:17     ` Aneesh Kumar K.V [this message]
2026-05-25 15:48 ` [PATCH v5 3/5] iommufd/viommu: Keep a reference to the KVM file Aneesh Kumar K.V (Arm)
2026-05-25 15:48 ` [PATCH v5 4/5] iommufd/tsm: add vdevice TSM bind/unbind ioctl Aneesh Kumar K.V (Arm)
2026-05-25 21:44   ` sashiko-bot
2026-05-25 15:48 ` [PATCH v5 5/5] iommufd/vdevice: add TSM request ioctl Aneesh Kumar K.V (Arm)
2026-05-25 22:18   ` sashiko-bot
2026-05-26  8:18     ` Aneesh Kumar K.V
2026-05-27  0:16   ` Alexey Kardashevskiy
2026-05-27  6:17     ` Dan Williams (nvidia)
2026-05-27  6:56       ` Tian, Kevin
2026-05-27 12:51       ` Jason Gunthorpe
2026-05-27 15:34       ` Aneesh Kumar K.V
2026-05-27 17:49         ` Aneesh Kumar K.V
2026-05-27 22:49           ` Dan Williams (nvidia)
2026-06-02  5:10             ` Aneesh Kumar K.V
2026-06-02  8:40       ` Alexey Kardashevskiy

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=yq5av7cashz0.fsf@kernel.org \
    --to=aneesh.kumar@kernel.org \
    --cc=kvm@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