Re: [PATCH v4 3/9] s390/vfio-ap: Fix missing lock required to access list of ap_matrix_mdev objects

[email protected]
Newsgroups org.kernel.vger.linux-s390,org.kernel.vger.kvm
Message-ID <[email protected]>
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [Critical] The sysfs file 'status' is exposed to userspace before the device's driver data is fully initialized, leading to a potential NULL pointer dereference in status_show().
- [High] A race condition in vfio_ap_mdev_probe() allows userspace to bypass the 'one mdev per KVM' isolation restriction.
- [High] Eventfd contexts for `req_trigger` and `cfg_chg_trigger` are leaked when the mediated device is closed or removed.
--

--- Patch [3]: [PATCH v4 3/9] s390/vfio-ap: Fix missing lock required to access list of ap_matrix_mdev objects ---
commit b763c5c7bf7a38cb1fc2e2da2ff0df92d3ae360c
Author: Anthony Krowiak <[email protected]>

s390/vfio-ap: Fix missing lock required to access list of ap_matrix_mdev objects

This patch fixes list traversal synchronization for ap_matrix_mdev objects
by adding guests_lock around mdev_list access. It also removes redundant
zero-initialization of eventfd triggers in the probe function.

> diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
> index c6bee69cc22f6..f2d662e388bdc 100644
> --- a/drivers/s390/crypto/vfio_ap_ops.c
> +++ b/drivers/s390/crypto/vfio_ap_ops.c
> @@ -800,12 +800,17 @@ static int vfio_ap_mdev_probe(struct mdev_device *mdev)
>  	ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev);

[Severity: High]
This isn't a bug introduced by this patch, but does exposing the VFIO
device to userspace here create a race condition?

vfio_register_emulated_iommu_dev() makes the device visible to userspace
before it is added to matrix_dev->mdev_list under guests_lock. Could
userspace open multiple mdevs concurrently, allowing them to bypass the 'one
mdev per KVM' isolation restriction since vfio_ap_mdev_set_kvm() won't see
the other devices in the list yet?

>  	if (ret)
>  		goto err_put_vdev;
> -	matrix_mdev->req_trigger = NULL;
> -	matrix_mdev->cfg_chg_trigger = NULL;

[Severity: High]
This is a pre-existing issue, but since you are touching the eventfd triggers
here, are they properly cleaned up when the device is closed?

Userspace can set IRQ triggers via VFIO_DEVICE_SET_IRQS, which calls
eventfd_ctx_fdget() and takes a reference. If the user closes the device
without unregistering the IRQs, shouldn't vfio_ap_mdev_close_device() or
vfio_ap_mdev_remove() call eventfd_ctx_put() for req_trigger and
cfg_chg_trigger to prevent a memory and file descriptor leak?

> +
> +	/*
> +	 * Take the matrix_dev->guests_lock mutex before adding the matrix_mdev
> +	 * to the mdev_list. All functions that traverse the list must also hold
> +	 * this lock to guard against additions to or removals from the list
> +	 * while it is being traversed.
> +	 */
> +	mutex_lock(&matrix_dev->guests_lock);
>  	dev_set_drvdata(&mdev->dev, matrix_mdev);
> -	mutex_lock(&matrix_dev->mdevs_lock);
>  	list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
> -	mutex_unlock(&matrix_dev->mdevs_lock);
> +	mutex_unlock(&matrix_dev->guests_lock);
>  	return 0;

[ ... ]

> @@ -2316,6 +2323,7 @@ static ssize_t status_show(struct device *dev,
>  	struct ap_matrix_mdev *matrix_mdev;
>  	struct ap_device *apdev = to_ap_dev(dev);
>  
> +	mutex_lock(&matrix_dev->guests_lock);
>  	mutex_lock(&matrix_dev->mdevs_lock);
>  	q = dev_get_drvdata(&apdev->device);

[Severity: Critical]
This is a pre-existing issue, but is it possible for dev_get_drvdata() to
return a NULL pointer here if the status sysfs file is read concurrently
with device probing?

During vfio_ap_mdev_probe_queue(), sysfs_create_group() creates the status
attribute before dev_set_drvdata() initializes the driver data. Can a
concurrent read of the status file fetch a NULL pointer in status_show() and
pass it to vfio_ap_mdev_for_queue(), causing a crash when it dereferences
q->apqn?

>  	matrix_mdev = vfio_ap_mdev_for_queue(q);
> @@ -2343,6 +2351,7 @@ static ssize_t status_show(struct device *dev,
>  	}
>  
>  	mutex_unlock(&matrix_dev->mdevs_lock);
> +	mutex_unlock(&matrix_dev->guests_lock);
>  
>  	return nchars;
>  }

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.