> From: Xiaoguang Wang <lege.w...@jaguarmicro.com>
> Sent: Friday, November 1, 2024 2:05 PM
> 
> Allocate one extra virtio_device_id as null terminator, otherwise
> vdpa_mgmtdev_get_classes() may iterate multiple times and visit undefined
> memory.
> 
> Fixes: ffbda8e9df10 ("vdpa/vp_vdpa : add vdpa tool support in vp_vdpa")
> Cc: sta...@vger.kernel.org
> Suggested-by: Parav Pandit <pa...@nvidia.com>
> Signed-off-by: Angus Chen <angus.c...@jaguarmicro.com>
> Signed-off-by: Xiaoguang Wang <lege.w...@jaguarmicro.com>
> 
> ---
> V2:
>   Use kcalloc() api.
> ---
>  drivers/vdpa/virtio_pci/vp_vdpa.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/vdpa/virtio_pci/vp_vdpa.c
> b/drivers/vdpa/virtio_pci/vp_vdpa.c
> index ac4ab22f7d8b..b6410a984f29 100644
> --- a/drivers/vdpa/virtio_pci/vp_vdpa.c
> +++ b/drivers/vdpa/virtio_pci/vp_vdpa.c
> @@ -612,7 +612,11 @@ static int vp_vdpa_probe(struct pci_dev *pdev,
> const struct pci_device_id *id)
>               goto mdev_err;
>       }
> 
> -     mdev_id = kzalloc(sizeof(struct virtio_device_id), GFP_KERNEL);
> +     /*
> +      * id_table should be a null terminated array, so allocate one
> additional
> +      * entry here, see vdpa_mgmtdev_get_classes().
> +      */
> +     mdev_id = kcalloc(2, sizeof(struct virtio_device_id), GFP_KERNEL);
>       if (!mdev_id) {
>               err = -ENOMEM;
>               goto mdev_id_err;

Now that mdev_id is an array, 
please change the id assignment as below. Even though existing code works is 
confusing to read it without an array.

-       mdev_id->device = mdev->id.device;
-       mdev_id->vendor = mdev->id.vendor;

+       mdev_id[0].device = mdev->id.device;
+       mdev_id[0].vendor = mdev->id.vendor;

> --
> 2.40.1


Reply via email to