vhost: fix possible null pointer dereference

Message ID 20230309113631.300351-1-maxime.coquelin@redhat.com (mailing list archive)
State Accepted, archived
Delegated to: Maxime Coquelin
Headers
Series vhost: fix possible null pointer dereference |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/intel-Functional success Functional PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/github-robot: build success github build: passed
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-abi-testing success Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-testing success Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS

Commit Message

Maxime Coquelin March 9, 2023, 11:36 a.m. UTC
  When handling VHOST_USER_SET_MEM_TABLE request ending
up in changing existing memory map, a device's memory
pointer may ends up being dereference while being NULL in
IOTLB cache flush function.

Coverity issue: 383646
Fixes: dea092d0addb ("vhost: fix madvise arguments alignment")

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 lib/vhost/vhost_user.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
  

Comments

Chenbo Xia March 10, 2023, 2:52 a.m. UTC | #1
> -----Original Message-----
> From: Maxime Coquelin <maxime.coquelin@redhat.com>
> Sent: Thursday, March 9, 2023 7:37 PM
> To: dev@dpdk.org; mkp@redhat.com; Xia, Chenbo <chenbo.xia@intel.com>;
> david.marchand@redhat.com
> Cc: Maxime Coquelin <maxime.coquelin@redhat.com>
> Subject: [PATCH] vhost: fix possible null pointer dereference
> 
> When handling VHOST_USER_SET_MEM_TABLE request ending
> up in changing existing memory map, a device's memory
> pointer may ends up being dereference while being NULL in
> IOTLB cache flush function.
> 
> Coverity issue: 383646
> Fixes: dea092d0addb ("vhost: fix madvise arguments alignment")
> 
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
> ---
>  lib/vhost/vhost_user.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c
> index 9e361082dc..23a6a4e2bd 100644
> --- a/lib/vhost/vhost_user.c
> +++ b/lib/vhost/vhost_user.c
> @@ -1355,16 +1355,16 @@ vhost_user_set_mem_table(struct virtio_net **pdev,
>  			async_notify = true;
>  		}
> 
> +		/* Flush IOTLB cache as previous HVAs are now invalid */
> +		if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
> +			for (i = 0; i < dev->nr_vring; i++)
> +				vhost_user_iotlb_flush_all(dev, dev->virtqueue[i]);
> +
>  		free_mem_region(dev);
>  		rte_free(dev->mem);
>  		dev->mem = NULL;
>  	}
> 
> -	/* Flush IOTLB cache as previous HVAs are now invalid */
> -	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
> -		for (i = 0; i < dev->nr_vring; i++)
> -			vhost_user_iotlb_flush_all(dev, dev->virtqueue[i]);
> -
>  	/*
>  	 * If VQ 0 has already been allocated, try to allocate on the same
>  	 * NUMA node. It can be reallocated later in numa_realloc().
> --
> 2.39.2

Reviewed-by: Chenbo Xia <chenbo.xia@intel.com>
  
Maxime Coquelin March 16, 2023, 2:47 p.m. UTC | #2
On 3/9/23 12:36, Maxime Coquelin wrote:
> When handling VHOST_USER_SET_MEM_TABLE request ending
> up in changing existing memory map, a device's memory
> pointer may ends up being dereference while being NULL in
> IOTLB cache flush function.
> 
> Coverity issue: 383646
> Fixes: dea092d0addb ("vhost: fix madvise arguments alignment")
> 
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
> ---
>   lib/vhost/vhost_user.c | 10 +++++-----
>   1 file changed, 5 insertions(+), 5 deletions(-)
> 

Applied to dpdk-next-virtio/main.

Thanks,
Maxime
  

Patch

diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c
index 9e361082dc..23a6a4e2bd 100644
--- a/lib/vhost/vhost_user.c
+++ b/lib/vhost/vhost_user.c
@@ -1355,16 +1355,16 @@  vhost_user_set_mem_table(struct virtio_net **pdev,
 			async_notify = true;
 		}
 
+		/* Flush IOTLB cache as previous HVAs are now invalid */
+		if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
+			for (i = 0; i < dev->nr_vring; i++)
+				vhost_user_iotlb_flush_all(dev, dev->virtqueue[i]);
+
 		free_mem_region(dev);
 		rte_free(dev->mem);
 		dev->mem = NULL;
 	}
 
-	/* Flush IOTLB cache as previous HVAs are now invalid */
-	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
-		for (i = 0; i < dev->nr_vring; i++)
-			vhost_user_iotlb_flush_all(dev, dev->virtqueue[i]);
-
 	/*
 	 * If VQ 0 has already been allocated, try to allocate on the same
 	 * NUMA node. It can be reallocated later in numa_realloc().