virtio: pci: Check virtio configs are mapped
authorAndrew Scull <ascull@google.com>
Thu, 21 Apr 2022 16:11:14 +0000 (16:11 +0000)
committerTom Rini <trini@konsulko.com>
Tue, 3 May 2022 22:33:29 +0000 (18:33 -0400)
Prepare for calls to `virtio_pci_map_capability()` failing by returning
NULL on error. If this happens, later accesses to the pointers would be
unsafe so cause the probe to fail if such an error occurs.

Signed-off-by: Andrew Scull <ascull@google.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
drivers/virtio/virtio_pci_modern.c

index bf92d9d..f1e64a3 100644 (file)
@@ -533,7 +533,19 @@ static int virtio_pci_probe(struct udevice *udev)
                return -EINVAL;
        }
 
+       /* Map configuration structures */
+       priv->common = virtio_pci_map_capability(udev, &common_cap);
+       if (!priv->common) {
+               printf("(%s): could not map common config\n", udev->name);
+               return -EINVAL;
+       }
+
        priv->notify_len = notify_cap.length;
+       priv->notify_base = virtio_pci_map_capability(udev, &notify_cap);
+       if (!priv->notify_base) {
+               printf("(%s): could not map notify config\n", udev->name);
+               return -EINVAL;
+       }
 
        /*
         * Device capability is only mandatory for devices that have
@@ -545,11 +557,13 @@ static int virtio_pci_probe(struct udevice *udev)
        if (device) {
                priv->device_len = device_cap.length;
                priv->device = virtio_pci_map_capability(udev, &device_cap);
+               if (!priv->device) {
+                       printf("(%s): could not map device config\n",
+                              udev->name);
+                       return -EINVAL;
+               }
        }
 
-       /* Map configuration structures */
-       priv->common = virtio_pci_map_capability(udev, &common_cap);
-       priv->notify_base = virtio_pci_map_capability(udev, &notify_cap);
        debug("(%p): common @ %p, notify base @ %p, device @ %p\n",
              udev, priv->common, priv->notify_base, priv->device);