diff mbox series

[v2,kvmtool,14/30] vfio/pci: Don't access potentially unallocated regions

Message ID 20200123134805.1993-15-alexandru.elisei@arm.com (mailing list archive)
State New, archived
Headers show
Series Add reassignable BARs and PCIE 1.1 support | expand

Commit Message

Alexandru Elisei Jan. 23, 2020, 1:47 p.m. UTC
Don't try to configure a BAR if there is no region associated with it.

Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
---
 vfio/pci.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Comments

Andre Przywara Jan. 29, 2020, 6:17 p.m. UTC | #1
On Thu, 23 Jan 2020 13:47:49 +0000
Alexandru Elisei <alexandru.elisei@arm.com> wrote:

Hi,

> Don't try to configure a BAR if there is no region associated with it.
> 
> Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
> ---
>  vfio/pci.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/vfio/pci.c b/vfio/pci.c
> index 1f38f90c3ae9..f86a7d9b7032 100644
> --- a/vfio/pci.c
> +++ b/vfio/pci.c
> @@ -652,6 +652,8 @@ static int vfio_pci_fixup_cfg_space(struct vfio_device *vdev)
>  
>  	/* Initialise the BARs */
>  	for (i = VFIO_PCI_BAR0_REGION_INDEX; i <= VFIO_PCI_BAR5_REGION_INDEX; ++i) {
> +		if ((u32)i == vdev->info.num_regions)
> +			break;

My inner check-patch complains that we should not have code before declarations.
Can we solve this the same way as below?

Cheers,
Andre


>  		u64 base;
>  		struct vfio_region *region = &vdev->regions[i];
>  
> @@ -853,11 +855,12 @@ static int vfio_pci_configure_bar(struct kvm *kvm, struct vfio_device *vdev,
>  	u32 bar;
>  	size_t map_size;
>  	struct vfio_pci_device *pdev = &vdev->pci;
> -	struct vfio_region *region = &vdev->regions[nr];
> +	struct vfio_region *region;
>  
>  	if (nr >= vdev->info.num_regions)
>  		return 0;
>  
> +	region = &vdev->regions[nr];
>  	bar = pdev->hdr.bar[nr];
>  
>  	region->vdev = vdev;
Alexandru Elisei March 6, 2020, 10:54 a.m. UTC | #2
Hi,

On 1/29/20 6:17 PM, Andre Przywara wrote:
> On Thu, 23 Jan 2020 13:47:49 +0000
> Alexandru Elisei <alexandru.elisei@arm.com> wrote:
>
> Hi,
>
>> Don't try to configure a BAR if there is no region associated with it.
>>
>> Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
>> ---
>>  vfio/pci.c | 5 ++++-
>>  1 file changed, 4 insertions(+), 1 deletion(-)
>>
>> diff --git a/vfio/pci.c b/vfio/pci.c
>> index 1f38f90c3ae9..f86a7d9b7032 100644
>> --- a/vfio/pci.c
>> +++ b/vfio/pci.c
>> @@ -652,6 +652,8 @@ static int vfio_pci_fixup_cfg_space(struct vfio_device *vdev)
>>  
>>  	/* Initialise the BARs */
>>  	for (i = VFIO_PCI_BAR0_REGION_INDEX; i <= VFIO_PCI_BAR5_REGION_INDEX; ++i) {
>> +		if ((u32)i == vdev->info.num_regions)
>> +			break;
> My inner check-patch complains that we should not have code before declarations.
> Can we solve this the same way as below?

Sure, I'll change it and update the commit message accordingly.

Thanks, Alex

>
> Cheers,
> Andre
>
>
>>  		u64 base;
>>  		struct vfio_region *region = &vdev->regions[i];
>>  
>> @@ -853,11 +855,12 @@ static int vfio_pci_configure_bar(struct kvm *kvm, struct vfio_device *vdev,
>>  	u32 bar;
>>  	size_t map_size;
>>  	struct vfio_pci_device *pdev = &vdev->pci;
>> -	struct vfio_region *region = &vdev->regions[nr];
>> +	struct vfio_region *region;
>>  
>>  	if (nr >= vdev->info.num_regions)
>>  		return 0;
>>  
>> +	region = &vdev->regions[nr];
>>  	bar = pdev->hdr.bar[nr];
>>  
>>  	region->vdev = vdev;
diff mbox series

Patch

diff --git a/vfio/pci.c b/vfio/pci.c
index 1f38f90c3ae9..f86a7d9b7032 100644
--- a/vfio/pci.c
+++ b/vfio/pci.c
@@ -652,6 +652,8 @@  static int vfio_pci_fixup_cfg_space(struct vfio_device *vdev)
 
 	/* Initialise the BARs */
 	for (i = VFIO_PCI_BAR0_REGION_INDEX; i <= VFIO_PCI_BAR5_REGION_INDEX; ++i) {
+		if ((u32)i == vdev->info.num_regions)
+			break;
 		u64 base;
 		struct vfio_region *region = &vdev->regions[i];
 
@@ -853,11 +855,12 @@  static int vfio_pci_configure_bar(struct kvm *kvm, struct vfio_device *vdev,
 	u32 bar;
 	size_t map_size;
 	struct vfio_pci_device *pdev = &vdev->pci;
-	struct vfio_region *region = &vdev->regions[nr];
+	struct vfio_region *region;
 
 	if (nr >= vdev->info.num_regions)
 		return 0;
 
+	region = &vdev->regions[nr];
 	bar = pdev->hdr.bar[nr];
 
 	region->vdev = vdev;