pci: fix non NULL-terminated vector used as C string overflow
[vpp.git] / src / vlib / linux / vfio.c
index d300a68..dc68c52 100644 (file)
@@ -40,7 +40,7 @@ vfio_map_physmem_page (vlib_main_t * vm, void *addr)
   vlib_physmem_main_t *vpm = &vm->physmem_main;
   linux_vfio_main_t *lvm = &vfio_main;
   struct vfio_iommu_type1_dma_map dm = { 0 };
-  uword log2_page_size = vpm->pmalloc_main->log2_page_sz;
+  uword log2_page_size = vpm->pmalloc_main->def_log2_page_sz;
   uword physmem_start = pointer_to_uword (vpm->pmalloc_main->base);
 
   if (lvm->container_fd == -1)
@@ -185,8 +185,9 @@ linux_vfio_group_get_device_fd (vlib_pci_addr_t * addr, int *fdp,
   int fd;
 
   *is_noiommu = 0;
-  s = format (s, "/sys/bus/pci/devices/%U/iommu_group", format_vlib_pci_addr,
-             addr);
+  s =
+    format (s, "/sys/bus/pci/devices/%U/iommu_group%c", format_vlib_pci_addr,
+           addr, 0);
   tmpstr = clib_sysfs_link_to_name ((char *) s);
   if (tmpstr)
     {
@@ -201,8 +202,8 @@ linux_vfio_group_get_device_fd (vlib_pci_addr_t * addr, int *fdp,
     }
   vec_reset_length (s);
 
-  s = format (s, "/sys/bus/pci/devices/%U/iommu_group/name",
-             format_vlib_pci_addr, addr);
+  s = format (s, "/sys/bus/pci/devices/%U/iommu_group/name%c",
+             format_vlib_pci_addr, addr, 0);
   err = clib_sysfs_read ((char *) s, "%s", &tmpstr);
   if (err == 0)
     {
@@ -246,6 +247,62 @@ linux_vfio_init (vlib_main_t * vm)
   return 0;
 }
 
+u8 *
+format_vfio_region_info (u8 * s, va_list * args)
+{
+  struct vfio_region_info *r = va_arg (*args, struct vfio_region_info *);
+
+  s = format (s, "region_info index:%u size:0x%lx offset:0x%lx flags:",
+             r->index, r->size, r->offset);
+
+  if (r->flags & VFIO_REGION_INFO_FLAG_READ)
+    s = format (s, " rd");
+
+  if (r->flags & VFIO_REGION_INFO_FLAG_WRITE)
+    s = format (s, " wr");
+
+  if (r->flags & VFIO_REGION_INFO_FLAG_MMAP)
+    s = format (s, " mmap");
+
+#ifdef VFIO_REGION_INFO_FLAG_CAPS
+  if (r->flags & VFIO_REGION_INFO_FLAG_CAPS)
+    s = format (s, " caps");
+#endif
+
+  s = format (s, " (0x%x)", r->flags);
+
+#ifdef VFIO_REGION_INFO_FLAG_CAPS
+  u32 cap_offset;
+
+  if ((r->flags & VFIO_REGION_INFO_FLAG_CAPS) == 0)
+    return s;
+
+  s = format (s, "\n caps:");
+  cap_offset = r->cap_offset;
+
+  do
+    {
+      struct vfio_info_cap_header *cap = (void *) r + cap_offset;
+#ifdef VFIO_REGION_INFO_CAP_SPARSE_MMAP
+      if (cap->id == VFIO_REGION_INFO_CAP_SPARSE_MMAP)
+       s = format (s, " sparse-mmap");
+#endif
+#ifdef VFIO_REGION_INFO_CAP_TYPE
+      if (cap->id == VFIO_REGION_INFO_CAP_TYPE)
+       s = format (s, " type");
+#endif
+#ifdef VFIO_REGION_INFO_CAP_MSIX_MAPPABLE
+      if (cap->id == VFIO_REGION_INFO_CAP_MSIX_MAPPABLE)
+       s = format (s, " msix-mappable");
+#endif
+      cap_offset = cap->next;
+    }
+  while (cap_offset);
+#endif
+
+  return s;
+}
+
 /*
  * fd.io coding-style-patch-verification: ON
  *