Currently, when we unmap a device in a secondary process, we free the
mmap area lists of its BARs. These lists are allocated by the primary
process in shared memory and are referenced from the mapped resource
entry, which stays in the shared list after a secondary process unmaps
the device. As a result, we have use-after-free (if another process
references the list) as well as a double-free (if another process, primary
or secondary, tries to free it).
Fix it by freeing the area lists only in the primary process, where the
mapped resource entry itself is freed.
Fixes: f60abf9757f2 ("bus/pci: add VFIO sparse mmap")
Cc: [email protected]
Cc: [email protected]
Signed-off-by: Anatoly Burakov <[email protected]>
---
drivers/bus/pci/linux/pci_vfio.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/drivers/bus/pci/linux/pci_vfio.c b/drivers/bus/pci/linux/pci_vfio.c
index 1c88d403d25..f4f9cad7409 100644
--- a/drivers/bus/pci/linux/pci_vfio.c
+++ b/drivers/bus/pci/linux/pci_vfio.c
@@ -1057,9 +1057,6 @@ find_and_unmap_vfio_resource(struct mapped_pci_res_list
*vfio_res_list,
pci_addr, maps[i].addr);
pci_unmap_resource(maps[i].addr, maps[i].size);
}
-
- if (maps[i].nr_areas > 0)
- rte_free(maps[i].areas);
}
return vfio_res;
@@ -1072,7 +1069,7 @@ pci_vfio_unmap_resource_primary(struct rte_pci_device
*dev)
struct rte_pci_addr *loc = &dev->addr;
struct mapped_pci_resource *vfio_res = NULL;
struct mapped_pci_res_list *vfio_res_list;
- int ret, vfio_dev_fd;
+ int i, ret, vfio_dev_fd;
/* store PCI address string */
snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
@@ -1113,6 +1110,10 @@ pci_vfio_unmap_resource_primary(struct rte_pci_device
*dev)
return -1;
}
+ /* area lists are shared with secondaries, so only the primary frees
them */
+ for (i = 0; i < vfio_res->nb_maps; i++)
+ rte_free(vfio_res->maps[i].areas);
+
TAILQ_REMOVE(vfio_res_list, vfio_res, next);
rte_free(vfio_res);
return 0;
--
2.52.0