[libvirt PATCH 0/3] qemu: IOMMUFD security label fix
Pavel Hrdina (3): security_apparmor: Use g_auto* in AppArmorSetSecurityHostdevLabel security: Cleanup hostdev label error logic qemu: Fix IOMMUFD and VFIO security labels src/qemu/qemu_hotplug.c | 2 +- src/qemu/qemu_process.c | 16 +++-- src/qemu/qemu_process.h | 3 +- src/security/security_apparmor.c | 76 +++++++++-------------- src/security/security_dac.c | 102 ++++++++++++++----------------- src/security/security_selinux.c | 90 ++++++++++++--------------- 6 files changed, 130 insertions(+), 159 deletions(-) -- 2.53.0
From: Pavel Hrdina <phrdina@redhat.com> Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/security/security_apparmor.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c index 6c5da2a650..74c5b10063 100644 --- a/src/security/security_apparmor.c +++ b/src/security/security_apparmor.c @@ -799,7 +799,7 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr, virDomainHostdevDef *dev, const char *vroot) { - struct SDPDOP *ptr; + g_autofree struct SDPDOP *ptr = NULL; int ret = -1; virSecurityLabelDef *secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_APPARMOR_NAME); @@ -831,13 +831,12 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr, switch (dev->source.subsys.type) { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: { - virUSBDevice *usb = + g_autoptr(virUSBDevice) usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device, vroot); if (!usb) goto done; ret = virUSBDeviceFileIterate(usb, AppArmorSetSecurityUSBLabel, ptr); - virUSBDeviceFree(usb); break; } @@ -850,13 +849,12 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr, if (pcisrc->driver.name == VIR_DEVICE_HOSTDEV_PCI_DRIVER_NAME_VFIO) { if (dev->source.subsys.u.pci.driver.iommufd != VIR_TRISTATE_BOOL_YES) { - char *vfioGroupDev = virPCIDeviceGetIOMMUGroupDev(pci); + g_autofree char *vfioGroupDev = virPCIDeviceGetIOMMUGroupDev(pci); if (!vfioGroupDev) { goto done; } ret = AppArmorSetSecurityPCILabel(pci, vfioGroupDev, ptr); - VIR_FREE(vfioGroupDev); } else { g_autofree char *vfiofdDev = NULL; @@ -877,7 +875,7 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr, case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: { virDomainHostdevSubsysSCSIHost *scsihostsrc = &scsisrc->u.host; - virSCSIDevice *scsi = + g_autoptr(virSCSIDevice) scsi = virSCSIDeviceNew(NULL, scsihostsrc->adapter, scsihostsrc->bus, scsihostsrc->target, scsihostsrc->unit, @@ -887,13 +885,11 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr, goto done; ret = virSCSIDeviceFileIterate(scsi, AppArmorSetSecuritySCSILabel, ptr); - virSCSIDeviceFree(scsi); - break; } case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST: { - virSCSIVHostDevice *host = virSCSIVHostDeviceNew(hostsrc->wwpn); + g_autoptr(virSCSIVHostDevice) host = virSCSIVHostDeviceNew(hostsrc->wwpn); if (!host) goto done; @@ -901,19 +897,16 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr, ret = virSCSIVHostDeviceFileIterate(host, AppArmorSetSecurityHostLabel, ptr); - virSCSIVHostDeviceFree(host); break; } case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV: { - char *vfiodev = NULL; + g_autofree char *vfiodev = NULL; if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr))) goto done; ret = AppArmorSetSecurityHostdevLabelHelper(vfiodev, ptr); - - VIR_FREE(vfiodev); break; } @@ -923,7 +916,6 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr, } done: - VIR_FREE(ptr); return ret; } -- 2.53.0
From: Pavel Hrdina <phrdina@redhat.com> Current code used mix of return, goto, break and setting ret variable. Simplify the logic to just return -1 on error. Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/security/security_apparmor.c | 56 +++++++++-------- src/security/security_dac.c | 103 ++++++++++++++++++------------- src/security/security_selinux.c | 87 ++++++++++++++------------ 3 files changed, 139 insertions(+), 107 deletions(-) diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c index 74c5b10063..1c3496893c 100644 --- a/src/security/security_apparmor.c +++ b/src/security/security_apparmor.c @@ -800,7 +800,6 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr, const char *vroot) { g_autofree struct SDPDOP *ptr = NULL; - int ret = -1; virSecurityLabelDef *secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_APPARMOR_NAME); virDomainHostdevSubsysUSB *usbsrc = &dev->source.subsys.u.usb; @@ -834,9 +833,10 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr, g_autoptr(virUSBDevice) usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device, vroot); if (!usb) - goto done; + return -1; - ret = virUSBDeviceFileIterate(usb, AppArmorSetSecurityUSBLabel, ptr); + if (virUSBDeviceFileIterate(usb, AppArmorSetSecurityUSBLabel, ptr) < 0) + return -1; break; } @@ -845,30 +845,32 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr, virPCIDeviceNew(&pcisrc->addr); if (!pci) - goto done; + return -1; if (pcisrc->driver.name == VIR_DEVICE_HOSTDEV_PCI_DRIVER_NAME_VFIO) { if (dev->source.subsys.u.pci.driver.iommufd != VIR_TRISTATE_BOOL_YES) { g_autofree char *vfioGroupDev = virPCIDeviceGetIOMMUGroupDev(pci); - if (!vfioGroupDev) { - goto done; - } - ret = AppArmorSetSecurityPCILabel(pci, vfioGroupDev, ptr); + if (!vfioGroupDev) + return -1; + + if (AppArmorSetSecurityPCILabel(pci, vfioGroupDev, ptr) < 0) + return -1; } else { g_autofree char *vfiofdDev = NULL; if (virPCIDeviceGetVfioPath(pci, &vfiofdDev) < 0) - goto done; + return -1; - ret = AppArmorSetSecurityPCILabel(pci, vfiofdDev, ptr); - if (ret < 0) - goto done; + if (AppArmorSetSecurityPCILabel(pci, vfiofdDev, ptr) < 0) + return -1; - ret = AppArmorSetSecurityPCILabel(pci, VIR_IOMMU_DEV_PATH, ptr); + if (AppArmorSetSecurityPCILabel(pci, VIR_IOMMU_DEV_PATH, ptr) < 0) + return -1; } } else { - ret = virPCIDeviceFileIterate(pci, AppArmorSetSecurityPCILabel, ptr); + if (virPCIDeviceFileIterate(pci, AppArmorSetSecurityPCILabel, ptr) < 0) + return -1; } break; } @@ -881,10 +883,11 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr, scsihostsrc->target, scsihostsrc->unit, dev->readonly, dev->shareable); - if (!scsi) - goto done; + if (!scsi) + return -1; - ret = virSCSIDeviceFileIterate(scsi, AppArmorSetSecuritySCSILabel, ptr); + if (virSCSIDeviceFileIterate(scsi, AppArmorSetSecuritySCSILabel, ptr) < 0) + return -1; break; } @@ -892,11 +895,13 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr, g_autoptr(virSCSIVHostDevice) host = virSCSIVHostDeviceNew(hostsrc->wwpn); if (!host) - goto done; + return -1; - ret = virSCSIVHostDeviceFileIterate(host, - AppArmorSetSecurityHostLabel, - ptr); + if (virSCSIVHostDeviceFileIterate(host, + AppArmorSetSecurityHostLabel, + ptr) < 0) { + return -1; + } break; } @@ -904,19 +909,18 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr, g_autofree char *vfiodev = NULL; if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr))) - goto done; + return -1; - ret = AppArmorSetSecurityHostdevLabelHelper(vfiodev, ptr); + if (AppArmorSetSecurityHostdevLabelHelper(vfiodev, ptr) < 0) + return -1; break; } case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST: - ret = 0; break; } - done: - return ret; + return 0; } diff --git a/src/security/security_dac.c b/src/security/security_dac.c index 390dfc7578..dc6dac0fb1 100644 --- a/src/security/security_dac.c +++ b/src/security/security_dac.c @@ -1234,7 +1234,6 @@ virSecurityDACSetHostdevLabel(virSecurityManager *mgr, virDomainHostdevSubsysSCSI *scsisrc = &dev->source.subsys.u.scsi; virDomainHostdevSubsysSCSIVHost *hostsrc = &dev->source.subsys.u.scsi_host; virDomainHostdevSubsysMediatedDev *mdevsrc = &dev->source.subsys.u.mdev; - int ret = -1; if (!priv->dynamicOwnership) return 0; @@ -1265,9 +1264,11 @@ virSecurityDACSetHostdevLabel(virSecurityManager *mgr, if (!(usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device, vroot))) return -1; - ret = virUSBDeviceFileIterate(usb, - virSecurityDACSetUSBLabel, - &cbdata); + if (virUSBDeviceFileIterate(usb, + virSecurityDACSetUSBLabel, + &cbdata) < 0) { + return -1; + } break; } @@ -1275,7 +1276,7 @@ virSecurityDACSetHostdevLabel(virSecurityManager *mgr, g_autoptr(virPCIDevice) pci = NULL; if (!virPCIDeviceExists(&pcisrc->addr)) - break; + return -1; pci = virPCIDeviceNew(&pcisrc->addr); @@ -1289,25 +1290,29 @@ virSecurityDACSetHostdevLabel(virSecurityManager *mgr, if (!vfioGroupDev) return -1; - ret = virSecurityDACSetHostdevLabelHelper(vfioGroupDev, - false, - &cbdata); + if (virSecurityDACSetHostdevLabelHelper(vfioGroupDev, + false, + &cbdata) < 0) { + return -1; + } } else { g_autofree char *vfiofdDev = NULL; if (virPCIDeviceGetVfioPath(pci, &vfiofdDev) < 0) return -1; - ret = virSecurityDACSetHostdevLabelHelper(vfiofdDev, false, &cbdata); - if (ret < 0) - break; + if (virSecurityDACSetHostdevLabelHelper(vfiofdDev, false, &cbdata) < 0) + return -1; - ret = virSecurityDACSetHostdevLabelHelper(VIR_IOMMU_DEV_PATH, false, &cbdata); + if (virSecurityDACSetHostdevLabelHelper(VIR_IOMMU_DEV_PATH, false, &cbdata) < 0) + return -1; } } else { - ret = virPCIDeviceFileIterate(pci, - virSecurityDACSetPCILabel, - &cbdata); + if (virPCIDeviceFileIterate(pci, + virSecurityDACSetPCILabel, + &cbdata) < 0) { + return -1; + } } break; } @@ -1323,9 +1328,11 @@ virSecurityDACSetHostdevLabel(virSecurityManager *mgr, if (!scsi) return -1; - ret = virSCSIDeviceFileIterate(scsi, - virSecurityDACSetSCSILabel, - &cbdata); + if (virSCSIDeviceFileIterate(scsi, + virSecurityDACSetSCSILabel, + &cbdata) < 0) { + return -1; + } break; } @@ -1335,9 +1342,11 @@ virSecurityDACSetHostdevLabel(virSecurityManager *mgr, if (!host) return -1; - ret = virSCSIVHostDeviceFileIterate(host, - virSecurityDACSetHostLabel, - &cbdata); + if (virSCSIVHostDeviceFileIterate(host, + virSecurityDACSetHostLabel, + &cbdata) < 0) { + return -1; + } break; } @@ -1347,16 +1356,16 @@ virSecurityDACSetHostdevLabel(virSecurityManager *mgr, if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr))) return -1; - ret = virSecurityDACSetHostdevLabelHelper(vfiodev, false, &cbdata); + if (virSecurityDACSetHostdevLabelHelper(vfiodev, false, &cbdata) < 0) + return -1; break; } case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST: - ret = 0; break; } - return ret; + return 0; } @@ -1414,7 +1423,6 @@ virSecurityDACRestoreHostdevLabel(virSecurityManager *mgr, virDomainHostdevSubsysSCSI *scsisrc = &dev->source.subsys.u.scsi; virDomainHostdevSubsysSCSIVHost *hostsrc = &dev->source.subsys.u.scsi_host; virDomainHostdevSubsysMediatedDev *mdevsrc = &dev->source.subsys.u.mdev; - int ret = -1; secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME); @@ -1441,7 +1449,8 @@ virSecurityDACRestoreHostdevLabel(virSecurityManager *mgr, if (!(usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device, vroot))) return -1; - ret = virUSBDeviceFileIterate(usb, virSecurityDACRestoreUSBLabel, mgr); + if (virUSBDeviceFileIterate(usb, virSecurityDACRestoreUSBLabel, mgr) < 0) + return -1; break; } @@ -1449,7 +1458,7 @@ virSecurityDACRestoreHostdevLabel(virSecurityManager *mgr, g_autoptr(virPCIDevice) pci = NULL; if (!virPCIDeviceExists(&pcisrc->addr)) - break; + return -1; pci = virPCIDeviceNew(&pcisrc->addr); @@ -1463,24 +1472,29 @@ virSecurityDACRestoreHostdevLabel(virSecurityManager *mgr, if (!vfioGroupDev) return -1; - ret = virSecurityDACRestoreFileLabelInternal(mgr, NULL, - vfioGroupDev, false); + if (virSecurityDACRestoreFileLabelInternal(mgr, NULL, + vfioGroupDev, false) < 0) { + return -1; + } } else { g_autofree char *vfiofdDev = NULL; if (virPCIDeviceGetVfioPath(pci, &vfiofdDev) < 0) return -1; - ret = virSecurityDACRestoreFileLabelInternal(mgr, NULL, - vfiofdDev, false); - if (ret < 0) - break; + if (virSecurityDACRestoreFileLabelInternal(mgr, NULL, + vfiofdDev, false) < 0) { + return -1; + } - ret = virSecurityDACRestoreFileLabelInternal(mgr, NULL, - VIR_IOMMU_DEV_PATH, false); + if (virSecurityDACRestoreFileLabelInternal(mgr, NULL, + VIR_IOMMU_DEV_PATH, false) < 0) { + return -1; + } } } else { - ret = virPCIDeviceFileIterate(pci, virSecurityDACRestorePCILabel, mgr); + if (virPCIDeviceFileIterate(pci, virSecurityDACRestorePCILabel, mgr) < 0) + return -1; } break; } @@ -1496,7 +1510,8 @@ virSecurityDACRestoreHostdevLabel(virSecurityManager *mgr, if (!scsi) return -1; - ret = virSCSIDeviceFileIterate(scsi, virSecurityDACRestoreSCSILabel, mgr); + if (virSCSIDeviceFileIterate(scsi, virSecurityDACRestoreSCSILabel, mgr) < 0) + return -1; break; } @@ -1506,9 +1521,11 @@ virSecurityDACRestoreHostdevLabel(virSecurityManager *mgr, if (!host) return -1; - ret = virSCSIVHostDeviceFileIterate(host, - virSecurityDACRestoreHostLabel, - mgr); + if (virSCSIVHostDeviceFileIterate(host, + virSecurityDACRestoreHostLabel, + mgr) < 0) { + return -1; + } break; } @@ -1518,16 +1535,16 @@ virSecurityDACRestoreHostdevLabel(virSecurityManager *mgr, if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr))) return -1; - ret = virSecurityDACRestoreFileLabelInternal(mgr, NULL, vfiodev, false); + if (virSecurityDACRestoreFileLabelInternal(mgr, NULL, vfiodev, false) < 0) + return -1; break; } case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST: - ret = 0; break; } - return ret; + return 0; } diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c index 9c498ab5f8..94a796ec49 100644 --- a/src/security/security_selinux.c +++ b/src/security/security_selinux.c @@ -2219,8 +2219,6 @@ virSecuritySELinuxSetHostdevSubsysLabel(virSecurityManager *mgr, virDomainHostdevSubsysMediatedDev *mdevsrc = &dev->source.subsys.u.mdev; virSecuritySELinuxCallbackData data = {.mgr = mgr, .def = def}; - int ret = -1; - /* Like virSecuritySELinuxSetImageLabelInternal() for a networked * disk, do nothing for an iSCSI hostdev */ @@ -2241,7 +2239,8 @@ virSecuritySELinuxSetHostdevSubsysLabel(virSecurityManager *mgr, if (!usb) return -1; - ret = virUSBDeviceFileIterate(usb, virSecuritySELinuxSetUSBLabel, &data); + if (virUSBDeviceFileIterate(usb, virSecuritySELinuxSetUSBLabel, &data) < 0) + return -1; break; } @@ -2249,7 +2248,7 @@ virSecuritySELinuxSetHostdevSubsysLabel(virSecurityManager *mgr, g_autoptr(virPCIDevice) pci = NULL; if (!virPCIDeviceExists(&pcisrc->addr)) - break; + return -1; pci = virPCIDeviceNew(&pcisrc->addr); @@ -2263,23 +2262,26 @@ virSecuritySELinuxSetHostdevSubsysLabel(virSecurityManager *mgr, if (!vfioGroupDev) return -1; - ret = virSecuritySELinuxSetHostdevLabelHelper(vfioGroupDev, - false, - &data); + if (virSecuritySELinuxSetHostdevLabelHelper(vfioGroupDev, + false, + &data) < 0) { + return -1; + } } else { g_autofree char *vfiofdDev = NULL; if (virPCIDeviceGetVfioPath(pci, &vfiofdDev) < 0) return -1; - ret = virSecuritySELinuxSetHostdevLabelHelper(vfiofdDev, false, &data); - if (ret) - break; + if (virSecuritySELinuxSetHostdevLabelHelper(vfiofdDev, false, &data) < 0) + return -1; - ret = virSecuritySELinuxSetHostdevLabelHelper(VIR_IOMMU_DEV_PATH, false, &data); + if (virSecuritySELinuxSetHostdevLabelHelper(VIR_IOMMU_DEV_PATH, false, &data) < 0) + return -1; } } else { - ret = virPCIDeviceFileIterate(pci, virSecuritySELinuxSetPCILabel, &data); + if (virPCIDeviceFileIterate(pci, virSecuritySELinuxSetPCILabel, &data) < 0) + return -1; } break; } @@ -2296,9 +2298,11 @@ virSecuritySELinuxSetHostdevSubsysLabel(virSecurityManager *mgr, if (!scsi) return -1; - ret = virSCSIDeviceFileIterate(scsi, - virSecuritySELinuxSetSCSILabel, - &data); + if (virSCSIDeviceFileIterate(scsi, + virSecuritySELinuxSetSCSILabel, + &data) < 0) { + return -1; + } break; } @@ -2308,9 +2312,11 @@ virSecuritySELinuxSetHostdevSubsysLabel(virSecurityManager *mgr, if (!host) return -1; - ret = virSCSIVHostDeviceFileIterate(host, - virSecuritySELinuxSetHostLabel, - &data); + if (virSCSIVHostDeviceFileIterate(host, + virSecuritySELinuxSetHostLabel, + &data) < 0) { + return -1; + } break; } @@ -2318,18 +2324,18 @@ virSecuritySELinuxSetHostdevSubsysLabel(virSecurityManager *mgr, g_autofree char *vfiodev = NULL; if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr))) - return ret; + return -1; - ret = virSecuritySELinuxSetHostdevLabelHelper(vfiodev, false, &data); + if (virSecuritySELinuxSetHostdevLabelHelper(vfiodev, false, &data) < 0) + return -1; break; } case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST: - ret = 0; break; } - return ret; + return 0; } @@ -2467,7 +2473,6 @@ virSecuritySELinuxRestoreHostdevSubsysLabel(virSecurityManager *mgr, virDomainHostdevSubsysSCSI *scsisrc = &dev->source.subsys.u.scsi; virDomainHostdevSubsysSCSIVHost *hostsrc = &dev->source.subsys.u.scsi_host; virDomainHostdevSubsysMediatedDev *mdevsrc = &dev->source.subsys.u.mdev; - int ret = -1; /* Like virSecuritySELinuxRestoreImageLabelInt() for a networked * disk, do nothing for an iSCSI hostdev @@ -2489,7 +2494,8 @@ virSecuritySELinuxRestoreHostdevSubsysLabel(virSecurityManager *mgr, if (!usb) return -1; - ret = virUSBDeviceFileIterate(usb, virSecuritySELinuxRestoreUSBLabel, mgr); + if (virUSBDeviceFileIterate(usb, virSecuritySELinuxRestoreUSBLabel, mgr) < 0) + return -1; break; } @@ -2497,7 +2503,7 @@ virSecuritySELinuxRestoreHostdevSubsysLabel(virSecurityManager *mgr, g_autoptr(virPCIDevice) pci = NULL; if (!virPCIDeviceExists(&pcisrc->addr)) - break; + return -1; pci = virPCIDeviceNew(&pcisrc->addr); @@ -2511,21 +2517,23 @@ virSecuritySELinuxRestoreHostdevSubsysLabel(virSecurityManager *mgr, if (!vfioGroupDev) return -1; - ret = virSecuritySELinuxRestoreFileLabel(mgr, vfioGroupDev, false, false); + if (virSecuritySELinuxRestoreFileLabel(mgr, vfioGroupDev, false, false) < 0) + return -1; } else { g_autofree char *vfiofdDev = NULL; if (virPCIDeviceGetVfioPath(pci, &vfiofdDev) < 0) return -1; - ret = virSecuritySELinuxRestoreFileLabel(mgr, vfiofdDev, false, false); - if (ret < 0) - break; + if (virSecuritySELinuxRestoreFileLabel(mgr, vfiofdDev, false, false) < 0) + return -1; - ret = virSecuritySELinuxRestoreFileLabel(mgr, VIR_IOMMU_DEV_PATH, false, false); + if (virSecuritySELinuxRestoreFileLabel(mgr, VIR_IOMMU_DEV_PATH, false, false) < 0) + return -1; } } else { - ret = virPCIDeviceFileIterate(pci, virSecuritySELinuxRestorePCILabel, mgr); + if (virPCIDeviceFileIterate(pci, virSecuritySELinuxRestorePCILabel, mgr) < 0) + return -1; } break; } @@ -2541,7 +2549,8 @@ virSecuritySELinuxRestoreHostdevSubsysLabel(virSecurityManager *mgr, if (!scsi) return -1; - ret = virSCSIDeviceFileIterate(scsi, virSecuritySELinuxRestoreSCSILabel, mgr); + if (virSCSIDeviceFileIterate(scsi, virSecuritySELinuxRestoreSCSILabel, mgr) < 0) + return -1; break; } @@ -2551,9 +2560,11 @@ virSecuritySELinuxRestoreHostdevSubsysLabel(virSecurityManager *mgr, if (!host) return -1; - ret = virSCSIVHostDeviceFileIterate(host, - virSecuritySELinuxRestoreHostLabel, - mgr); + if (virSCSIVHostDeviceFileIterate(host, + virSecuritySELinuxRestoreHostLabel, + mgr) < 0) { + return -1; + } break; } @@ -2563,16 +2574,16 @@ virSecuritySELinuxRestoreHostdevSubsysLabel(virSecurityManager *mgr, if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr))) return -1; - ret = virSecuritySELinuxRestoreFileLabel(mgr, vfiodev, false, false); + if (virSecuritySELinuxRestoreFileLabel(mgr, vfiodev, false, false) < 0) + return -1; break; } case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST: - ret = 0; break; } - return ret; + return 0; } -- 2.53.0
From: Pavel Hrdina <phrdina@redhat.com> When IOMMUFD support was introduced it incorrectly tried to lable `/dev/iommu` and `/dev/vfio/devices/vfioX` but they are not added to QEMU namespace because libvirt opens FDs and passes these FDs to QEMU. We need to label these FDs instead. Fixes: 7d2f91f9cb572ab95d0916bdd1a46dd198874529 Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/qemu/qemu_hotplug.c | 2 +- src/qemu/qemu_process.c | 16 ++++++++++++---- src/qemu/qemu_process.h | 3 ++- src/security/security_apparmor.c | 12 ------------ src/security/security_dac.c | 27 --------------------------- src/security/security_selinux.c | 23 ----------------------- 6 files changed, 15 insertions(+), 68 deletions(-) diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 40489b84db..b3f2a173a8 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -1613,7 +1613,7 @@ qemuDomainAttachHostPCIDevice(virQEMUDriver *driver, } if (virHostdevIsPCIDeviceWithIOMMUFD(hostdev)) { - if (qemuProcessOpenVfioDeviceFd(hostdev) < 0) + if (qemuProcessOpenVfioDeviceFd(vm, hostdev) < 0) goto error; if (!priv->iommufdState) { diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index a82ee4b15e..ab7cf03c0e 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -7728,13 +7728,16 @@ int qemuProcessOpenIommuFd(virDomainObj *vm) { qemuDomainObjPrivate *priv = vm->privateData; - int iommufd; + VIR_AUTOCLOSE iommufd = -1; VIR_DEBUG("Opening IOMMU FD for domain %s", vm->def->name); if ((iommufd = virIOMMUFDOpenDevice()) < 0) return -1; + if (qemuSecuritySetImageFDLabel(priv->driver->securityManager, vm->def, iommufd) < 0) + return -1; + priv->iommufd = qemuFDPassDirectNew("iommufd", &iommufd); return 0; @@ -7749,16 +7752,21 @@ qemuProcessOpenIommuFd(virDomainObj *vm) * Returns: 0 on success, -1 on failure */ int -qemuProcessOpenVfioDeviceFd(virDomainHostdevDef *hostdev) +qemuProcessOpenVfioDeviceFd(virDomainObj *vm, + virDomainHostdevDef *hostdev) { + qemuDomainObjPrivate *priv = vm->privateData; qemuDomainHostdevPrivate *hostdevPriv = QEMU_DOMAIN_HOSTDEV_PRIVATE(hostdev); virDomainHostdevSubsysPCI *pci = &hostdev->source.subsys.u.pci; g_autofree char *name = g_strdup_printf("hostdev-%s-fd", hostdev->info->alias); - int vfioDeviceFd; + VIR_AUTOCLOSE vfioDeviceFd = -1; if ((vfioDeviceFd = virPCIDeviceOpenVfioFd(&pci->addr)) < 0) return -1; + if (qemuSecuritySetImageFDLabel(priv->driver->securityManager, vm->def, vfioDeviceFd) < 0) + return -1; + hostdevPriv->vfioDeviceFd = qemuFDPassDirectNew(name, &vfioDeviceFd); return 0; @@ -7776,7 +7784,7 @@ qemuProcessPrepareHostHostdev(virDomainObj *vm) case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: if (virHostdevIsPCIDeviceWithIOMMUFD(hostdev)) { /* Open VFIO device FD */ - if (qemuProcessOpenVfioDeviceFd(hostdev) < 0) + if (qemuProcessOpenVfioDeviceFd(vm, hostdev) < 0) return -1; } break; diff --git a/src/qemu/qemu_process.h b/src/qemu/qemu_process.h index fccd41e1a6..5874214596 100644 --- a/src/qemu/qemu_process.h +++ b/src/qemu/qemu_process.h @@ -136,7 +136,8 @@ int qemuProcessPrepareHostBackendChardevHotplug(virDomainObj *vm, int qemuProcessOpenIommuFd(virDomainObj *vm); -int qemuProcessOpenVfioDeviceFd(virDomainHostdevDef *hostdev); +int qemuProcessOpenVfioDeviceFd(virDomainObj *vm, + virDomainHostdevDef *hostdev); int qemuProcessPrepareHost(virQEMUDriver *driver, virDomainObj *vm, diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c index 1c3496893c..40f13ec1a5 100644 --- a/src/security/security_apparmor.c +++ b/src/security/security_apparmor.c @@ -45,7 +45,6 @@ #include "virstring.h" #include "virscsi.h" #include "virmdev.h" -#include "viriommufd.h" #define VIR_FROM_THIS VIR_FROM_SECURITY @@ -856,17 +855,6 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr, if (AppArmorSetSecurityPCILabel(pci, vfioGroupDev, ptr) < 0) return -1; - } else { - g_autofree char *vfiofdDev = NULL; - - if (virPCIDeviceGetVfioPath(pci, &vfiofdDev) < 0) - return -1; - - if (AppArmorSetSecurityPCILabel(pci, vfiofdDev, ptr) < 0) - return -1; - - if (AppArmorSetSecurityPCILabel(pci, VIR_IOMMU_DEV_PATH, ptr) < 0) - return -1; } } else { if (virPCIDeviceFileIterate(pci, AppArmorSetSecurityPCILabel, ptr) < 0) diff --git a/src/security/security_dac.c b/src/security/security_dac.c index dc6dac0fb1..5aa13741e6 100644 --- a/src/security/security_dac.c +++ b/src/security/security_dac.c @@ -41,7 +41,6 @@ #include "virscsivhost.h" #include "virstring.h" #include "virutil.h" -#include "viriommufd.h" #define VIR_FROM_THIS VIR_FROM_SECURITY @@ -1295,17 +1294,6 @@ virSecurityDACSetHostdevLabel(virSecurityManager *mgr, &cbdata) < 0) { return -1; } - } else { - g_autofree char *vfiofdDev = NULL; - - if (virPCIDeviceGetVfioPath(pci, &vfiofdDev) < 0) - return -1; - - if (virSecurityDACSetHostdevLabelHelper(vfiofdDev, false, &cbdata) < 0) - return -1; - - if (virSecurityDACSetHostdevLabelHelper(VIR_IOMMU_DEV_PATH, false, &cbdata) < 0) - return -1; } } else { if (virPCIDeviceFileIterate(pci, @@ -1476,21 +1464,6 @@ virSecurityDACRestoreHostdevLabel(virSecurityManager *mgr, vfioGroupDev, false) < 0) { return -1; } - } else { - g_autofree char *vfiofdDev = NULL; - - if (virPCIDeviceGetVfioPath(pci, &vfiofdDev) < 0) - return -1; - - if (virSecurityDACRestoreFileLabelInternal(mgr, NULL, - vfiofdDev, false) < 0) { - return -1; - } - - if (virSecurityDACRestoreFileLabelInternal(mgr, NULL, - VIR_IOMMU_DEV_PATH, false) < 0) { - return -1; - } } } else { if (virPCIDeviceFileIterate(pci, virSecurityDACRestorePCILabel, mgr) < 0) diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c index 94a796ec49..89546e3316 100644 --- a/src/security/security_selinux.c +++ b/src/security/security_selinux.c @@ -41,7 +41,6 @@ #include "virconf.h" #include "virtpm.h" #include "virstring.h" -#include "viriommufd.h" #define VIR_FROM_THIS VIR_FROM_SECURITY @@ -2267,17 +2266,6 @@ virSecuritySELinuxSetHostdevSubsysLabel(virSecurityManager *mgr, &data) < 0) { return -1; } - } else { - g_autofree char *vfiofdDev = NULL; - - if (virPCIDeviceGetVfioPath(pci, &vfiofdDev) < 0) - return -1; - - if (virSecuritySELinuxSetHostdevLabelHelper(vfiofdDev, false, &data) < 0) - return -1; - - if (virSecuritySELinuxSetHostdevLabelHelper(VIR_IOMMU_DEV_PATH, false, &data) < 0) - return -1; } } else { if (virPCIDeviceFileIterate(pci, virSecuritySELinuxSetPCILabel, &data) < 0) @@ -2519,17 +2507,6 @@ virSecuritySELinuxRestoreHostdevSubsysLabel(virSecurityManager *mgr, if (virSecuritySELinuxRestoreFileLabel(mgr, vfioGroupDev, false, false) < 0) return -1; - } else { - g_autofree char *vfiofdDev = NULL; - - if (virPCIDeviceGetVfioPath(pci, &vfiofdDev) < 0) - return -1; - - if (virSecuritySELinuxRestoreFileLabel(mgr, vfiofdDev, false, false) < 0) - return -1; - - if (virSecuritySELinuxRestoreFileLabel(mgr, VIR_IOMMU_DEV_PATH, false, false) < 0) - return -1; } } else { if (virPCIDeviceFileIterate(pci, virSecuritySELinuxRestorePCILabel, mgr) < 0) -- 2.53.0
On a Monday in 2026, Pavel Hrdina via Devel wrote:
From: Pavel Hrdina <phrdina@redhat.com>
When IOMMUFD support was introduced it incorrectly tried to lable
*label
`/dev/iommu` and `/dev/vfio/devices/vfioX` but they are not added to QEMU namespace because libvirt opens FDs and passes these FDs to QEMU.
We need to label these FDs instead.
Fixes: 7d2f91f9cb572ab95d0916bdd1a46dd198874529 Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/qemu/qemu_hotplug.c | 2 +- src/qemu/qemu_process.c | 16 ++++++++++++---- src/qemu/qemu_process.h | 3 ++- src/security/security_apparmor.c | 12 ------------ src/security/security_dac.c | 27 --------------------------- src/security/security_selinux.c | 23 ----------------------- 6 files changed, 15 insertions(+), 68 deletions(-)
Series: Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano
On 3/2/26 14:14, Pavel Hrdina via Devel wrote:
From: Pavel Hrdina <phrdina@redhat.com>
When IOMMUFD support was introduced it incorrectly tried to lable `/dev/iommu` and `/dev/vfio/devices/vfioX` but they are not added to QEMU namespace because libvirt opens FDs and passes these FDs to QEMU.
We need to label these FDs instead.
Fixes: 7d2f91f9cb572ab95d0916bdd1a46dd198874529 Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/qemu/qemu_hotplug.c | 2 +- src/qemu/qemu_process.c | 16 ++++++++++++---- src/qemu/qemu_process.h | 3 ++- src/security/security_apparmor.c | 12 ------------ src/security/security_dac.c | 27 --------------------------- src/security/security_selinux.c | 23 ----------------------- 6 files changed, 15 insertions(+), 68 deletions(-)
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 40489b84db..b3f2a173a8 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -1613,7 +1613,7 @@ qemuDomainAttachHostPCIDevice(virQEMUDriver *driver, }
if (virHostdevIsPCIDeviceWithIOMMUFD(hostdev)) { - if (qemuProcessOpenVfioDeviceFd(hostdev) < 0) + if (qemuProcessOpenVfioDeviceFd(vm, hostdev) < 0) goto error;
if (!priv->iommufdState) { diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index a82ee4b15e..ab7cf03c0e 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -7728,13 +7728,16 @@ int qemuProcessOpenIommuFd(virDomainObj *vm) { qemuDomainObjPrivate *priv = vm->privateData; - int iommufd; + VIR_AUTOCLOSE iommufd = -1;
VIR_DEBUG("Opening IOMMU FD for domain %s", vm->def->name);
if ((iommufd = virIOMMUFDOpenDevice()) < 0) return -1;
+ if (qemuSecuritySetImageFDLabel(priv->driver->securityManager, vm->def, iommufd) < 0) + return -1; + priv->iommufd = qemuFDPassDirectNew("iommufd", &iommufd);
return 0; @@ -7749,16 +7752,21 @@ qemuProcessOpenIommuFd(virDomainObj *vm) * Returns: 0 on success, -1 on failure */ int -qemuProcessOpenVfioDeviceFd(virDomainHostdevDef *hostdev) +qemuProcessOpenVfioDeviceFd(virDomainObj *vm, + virDomainHostdevDef *hostdev) { + qemuDomainObjPrivate *priv = vm->privateData; qemuDomainHostdevPrivate *hostdevPriv = QEMU_DOMAIN_HOSTDEV_PRIVATE(hostdev); virDomainHostdevSubsysPCI *pci = &hostdev->source.subsys.u.pci; g_autofree char *name = g_strdup_printf("hostdev-%s-fd", hostdev->info->alias); - int vfioDeviceFd; + VIR_AUTOCLOSE vfioDeviceFd = -1;
if ((vfioDeviceFd = virPCIDeviceOpenVfioFd(&pci->addr)) < 0) return -1;
+ if (qemuSecuritySetImageFDLabel(priv->driver->securityManager, vm->def, vfioDeviceFd) < 0) + return -1; + hostdevPriv->vfioDeviceFd = qemuFDPassDirectNew(name, &vfioDeviceFd);
return 0; @@ -7776,7 +7784,7 @@ qemuProcessPrepareHostHostdev(virDomainObj *vm) case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: if (virHostdevIsPCIDeviceWithIOMMUFD(hostdev)) { /* Open VFIO device FD */ - if (qemuProcessOpenVfioDeviceFd(hostdev) < 0) + if (qemuProcessOpenVfioDeviceFd(vm, hostdev) < 0) return -1; } break; diff --git a/src/qemu/qemu_process.h b/src/qemu/qemu_process.h index fccd41e1a6..5874214596 100644 --- a/src/qemu/qemu_process.h +++ b/src/qemu/qemu_process.h @@ -136,7 +136,8 @@ int qemuProcessPrepareHostBackendChardevHotplug(virDomainObj *vm,
int qemuProcessOpenIommuFd(virDomainObj *vm);
-int qemuProcessOpenVfioDeviceFd(virDomainHostdevDef *hostdev); +int qemuProcessOpenVfioDeviceFd(virDomainObj *vm, + virDomainHostdevDef *hostdev);
int qemuProcessPrepareHost(virQEMUDriver *driver, virDomainObj *vm,
ACK to these hunk above. BUT...
diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c index 1c3496893c..40f13ec1a5 100644 --- a/src/security/security_apparmor.c +++ b/src/security/security_apparmor.c @@ -45,7 +45,6 @@ #include "virstring.h" #include "virscsi.h" #include "virmdev.h" -#include "viriommufd.h"
#define VIR_FROM_THIS VIR_FROM_SECURITY
@@ -856,17 +855,6 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr,
if (AppArmorSetSecurityPCILabel(pci, vfioGroupDev, ptr) < 0) return -1; - } else { - g_autofree char *vfiofdDev = NULL; - - if (virPCIDeviceGetVfioPath(pci, &vfiofdDev) < 0) - return -1; - - if (AppArmorSetSecurityPCILabel(pci, vfiofdDev, ptr) < 0) - return -1; - - if (AppArmorSetSecurityPCILabel(pci, VIR_IOMMU_DEV_PATH, ptr) < 0) - return -1;
I don't think removing these hunks (trimmed) is justified. If you'd take a look at virHostdevIsPCIDeviceWithIOMMUFD() it looks like this: return virHostdevIsPCIDevice(hostdev) && hostdev->source.subsys.u.pci.driver.name == VIR_DEVICE_HOSTDEV_PCI_DRIVER_NAME_VFIO && hostdev->source.subsys.u.pci.driver.iommufd == VIR_TRISTATE_BOOL_YES; Now, consider the following <hostdev/> (either as a part of domain XML or being hotplugged): <hostdev mode='subsystem' type='pci' managed='yes'> <source> <address domain='0x0000' bus='0x00' slot='0x14' function='0x3'/> </source> <address type='pci' domain='0x0000' bus='0x00' slot='0x0b' function='0x0'/> </hostdev> The ->source.subsys.u.pci.driver.name is autofilled to _VFIO, BUT ->source.subsys.u.pci.driver.iommufd is NOT expanded to _YES. I do not run a machine with SELinux, but in my testing, I can imitate the problem by starting a domain with +77:+77 DAC seclabel, and hotplugging aforementioned hostdev. I set a breakpoint to qemuDomainNamespaceSetupHostdev() so that I can mangle perms of /dev/vfio/vfio INSIDE the namespace (once the function finishes and nodes are created inside the namespace). Then I observe virSecurityDACSetHostdevLabel() (well qemuSecuritySetHostdevLabel()) changing perms only to /dev/vfio/8 (my hostdev is in IOMMU group #8) only to see QEMU fail: error: internal error: unable to execute QEMU command 'device_add': vfio 0000:00:14.3: failed to setup container for group 8: Could not open '/dev/vfio/vfio': Permission denied Michal
On Tue, Mar 03, 2026 at 12:52:45PM +0100, Michal Prívozník wrote:
On 3/2/26 14:14, Pavel Hrdina via Devel wrote:
From: Pavel Hrdina <phrdina@redhat.com>
When IOMMUFD support was introduced it incorrectly tried to lable `/dev/iommu` and `/dev/vfio/devices/vfioX` but they are not added to QEMU namespace because libvirt opens FDs and passes these FDs to QEMU.
We need to label these FDs instead.
Fixes: 7d2f91f9cb572ab95d0916bdd1a46dd198874529 Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/qemu/qemu_hotplug.c | 2 +- src/qemu/qemu_process.c | 16 ++++++++++++---- src/qemu/qemu_process.h | 3 ++- src/security/security_apparmor.c | 12 ------------ src/security/security_dac.c | 27 --------------------------- src/security/security_selinux.c | 23 ----------------------- 6 files changed, 15 insertions(+), 68 deletions(-)
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 40489b84db..b3f2a173a8 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -1613,7 +1613,7 @@ qemuDomainAttachHostPCIDevice(virQEMUDriver *driver, }
if (virHostdevIsPCIDeviceWithIOMMUFD(hostdev)) { - if (qemuProcessOpenVfioDeviceFd(hostdev) < 0) + if (qemuProcessOpenVfioDeviceFd(vm, hostdev) < 0) goto error;
if (!priv->iommufdState) { diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index a82ee4b15e..ab7cf03c0e 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -7728,13 +7728,16 @@ int qemuProcessOpenIommuFd(virDomainObj *vm) { qemuDomainObjPrivate *priv = vm->privateData; - int iommufd; + VIR_AUTOCLOSE iommufd = -1;
VIR_DEBUG("Opening IOMMU FD for domain %s", vm->def->name);
if ((iommufd = virIOMMUFDOpenDevice()) < 0) return -1;
+ if (qemuSecuritySetImageFDLabel(priv->driver->securityManager, vm->def, iommufd) < 0) + return -1; + priv->iommufd = qemuFDPassDirectNew("iommufd", &iommufd);
return 0; @@ -7749,16 +7752,21 @@ qemuProcessOpenIommuFd(virDomainObj *vm) * Returns: 0 on success, -1 on failure */ int -qemuProcessOpenVfioDeviceFd(virDomainHostdevDef *hostdev) +qemuProcessOpenVfioDeviceFd(virDomainObj *vm, + virDomainHostdevDef *hostdev) { + qemuDomainObjPrivate *priv = vm->privateData; qemuDomainHostdevPrivate *hostdevPriv = QEMU_DOMAIN_HOSTDEV_PRIVATE(hostdev); virDomainHostdevSubsysPCI *pci = &hostdev->source.subsys.u.pci; g_autofree char *name = g_strdup_printf("hostdev-%s-fd", hostdev->info->alias); - int vfioDeviceFd; + VIR_AUTOCLOSE vfioDeviceFd = -1;
if ((vfioDeviceFd = virPCIDeviceOpenVfioFd(&pci->addr)) < 0) return -1;
+ if (qemuSecuritySetImageFDLabel(priv->driver->securityManager, vm->def, vfioDeviceFd) < 0) + return -1; + hostdevPriv->vfioDeviceFd = qemuFDPassDirectNew(name, &vfioDeviceFd);
return 0; @@ -7776,7 +7784,7 @@ qemuProcessPrepareHostHostdev(virDomainObj *vm) case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: if (virHostdevIsPCIDeviceWithIOMMUFD(hostdev)) { /* Open VFIO device FD */ - if (qemuProcessOpenVfioDeviceFd(hostdev) < 0) + if (qemuProcessOpenVfioDeviceFd(vm, hostdev) < 0) return -1; } break; diff --git a/src/qemu/qemu_process.h b/src/qemu/qemu_process.h index fccd41e1a6..5874214596 100644 --- a/src/qemu/qemu_process.h +++ b/src/qemu/qemu_process.h @@ -136,7 +136,8 @@ int qemuProcessPrepareHostBackendChardevHotplug(virDomainObj *vm,
int qemuProcessOpenIommuFd(virDomainObj *vm);
-int qemuProcessOpenVfioDeviceFd(virDomainHostdevDef *hostdev); +int qemuProcessOpenVfioDeviceFd(virDomainObj *vm, + virDomainHostdevDef *hostdev);
int qemuProcessPrepareHost(virQEMUDriver *driver, virDomainObj *vm,
ACK to these hunk above. BUT...
diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c index 1c3496893c..40f13ec1a5 100644 --- a/src/security/security_apparmor.c +++ b/src/security/security_apparmor.c @@ -45,7 +45,6 @@ #include "virstring.h" #include "virscsi.h" #include "virmdev.h" -#include "viriommufd.h"
#define VIR_FROM_THIS VIR_FROM_SECURITY
@@ -856,17 +855,6 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr,
if (AppArmorSetSecurityPCILabel(pci, vfioGroupDev, ptr) < 0) return -1; - } else { - g_autofree char *vfiofdDev = NULL; - - if (virPCIDeviceGetVfioPath(pci, &vfiofdDev) < 0) - return -1; - - if (AppArmorSetSecurityPCILabel(pci, vfiofdDev, ptr) < 0) - return -1; - - if (AppArmorSetSecurityPCILabel(pci, VIR_IOMMU_DEV_PATH, ptr) < 0) - return -1;
I don't think removing these hunks (trimmed) is justified. If you'd take a look at virHostdevIsPCIDeviceWithIOMMUFD() it looks like this:
The code I'm removing didn't exist before commit 7d2f91f9cb572ab95d0916bdd1a46dd198874529 introduced it. Originally it only called the following code: g_autofree char *vfioGroupDev = virPCIDeviceGetIOMMUGroupDev(pci); if (!vfioGroupDev) return -1; if (AppArmorSetSecurityPCILabel(pci, vfioGroupDev, ptr) < 0) return -1; After this commit the new code looks like this: if (dev->source.subsys.u.pci.driver.iommufd != VIR_TRISTATE_BOOL_YES) { g_autofree char *vfioGroupDev = virPCIDeviceGetIOMMUGroupDev(pci); if (!vfioGroupDev) return -1; if (AppArmorSetSecurityPCILabel(pci, vfioGroupDev, ptr) < 0) return -1; } So it is skipped only if iommufd='yes'.
return virHostdevIsPCIDevice(hostdev) && hostdev->source.subsys.u.pci.driver.name == VIR_DEVICE_HOSTDEV_PCI_DRIVER_NAME_VFIO && hostdev->source.subsys.u.pci.driver.iommufd == VIR_TRISTATE_BOOL_YES;
Now, consider the following <hostdev/> (either as a part of domain XML or being hotplugged):
<hostdev mode='subsystem' type='pci' managed='yes'> <source> <address domain='0x0000' bus='0x00' slot='0x14' function='0x3'/> </source> <address type='pci' domain='0x0000' bus='0x00' slot='0x0b' function='0x0'/> </hostdev>
The ->source.subsys.u.pci.driver.name is autofilled to _VFIO, BUT ->source.subsys.u.pci.driver.iommufd is NOT expanded to _YES.
I do not run a machine with SELinux, but in my testing, I can imitate the problem by starting a domain with +77:+77 DAC seclabel, and hotplugging aforementioned hostdev. I set a breakpoint to qemuDomainNamespaceSetupHostdev() so that I can mangle perms of /dev/vfio/vfio INSIDE the namespace (once the function finishes and nodes are created inside the namespace). Then I observe virSecurityDACSetHostdevLabel() (well qemuSecuritySetHostdevLabel()) changing perms only to /dev/vfio/8 (my hostdev is in IOMMU group #8) only to see QEMU fail:
error: internal error: unable to execute QEMU command 'device_add': vfio 0000:00:14.3: failed to setup container for group 8: Could not open '/dev/vfio/vfio': Permission denied>
Before IOMMUFD changes were introduced the existing code did not label /dev/vfio/vfio and the code I'm removing doesn't label that path as well, it was labeling /dev/vfio/devices/vfio0. Pavel
Michal
On 3/3/26 13:45, Pavel Hrdina wrote:
On Tue, Mar 03, 2026 at 12:52:45PM +0100, Michal Prívozník wrote:
On 3/2/26 14:14, Pavel Hrdina via Devel wrote:
From: Pavel Hrdina <phrdina@redhat.com>
When IOMMUFD support was introduced it incorrectly tried to lable `/dev/iommu` and `/dev/vfio/devices/vfioX` but they are not added to QEMU namespace because libvirt opens FDs and passes these FDs to QEMU.
We need to label these FDs instead.
Fixes: 7d2f91f9cb572ab95d0916bdd1a46dd198874529 Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/qemu/qemu_hotplug.c | 2 +- src/qemu/qemu_process.c | 16 ++++++++++++---- src/qemu/qemu_process.h | 3 ++- src/security/security_apparmor.c | 12 ------------ src/security/security_dac.c | 27 --------------------------- src/security/security_selinux.c | 23 ----------------------- 6 files changed, 15 insertions(+), 68 deletions(-)
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 40489b84db..b3f2a173a8 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -1613,7 +1613,7 @@ qemuDomainAttachHostPCIDevice(virQEMUDriver *driver, }
if (virHostdevIsPCIDeviceWithIOMMUFD(hostdev)) { - if (qemuProcessOpenVfioDeviceFd(hostdev) < 0) + if (qemuProcessOpenVfioDeviceFd(vm, hostdev) < 0) goto error;
if (!priv->iommufdState) { diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index a82ee4b15e..ab7cf03c0e 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -7728,13 +7728,16 @@ int qemuProcessOpenIommuFd(virDomainObj *vm) { qemuDomainObjPrivate *priv = vm->privateData; - int iommufd; + VIR_AUTOCLOSE iommufd = -1;
VIR_DEBUG("Opening IOMMU FD for domain %s", vm->def->name);
if ((iommufd = virIOMMUFDOpenDevice()) < 0) return -1;
+ if (qemuSecuritySetImageFDLabel(priv->driver->securityManager, vm->def, iommufd) < 0) + return -1; + priv->iommufd = qemuFDPassDirectNew("iommufd", &iommufd);
return 0; @@ -7749,16 +7752,21 @@ qemuProcessOpenIommuFd(virDomainObj *vm) * Returns: 0 on success, -1 on failure */ int -qemuProcessOpenVfioDeviceFd(virDomainHostdevDef *hostdev) +qemuProcessOpenVfioDeviceFd(virDomainObj *vm, + virDomainHostdevDef *hostdev) { + qemuDomainObjPrivate *priv = vm->privateData; qemuDomainHostdevPrivate *hostdevPriv = QEMU_DOMAIN_HOSTDEV_PRIVATE(hostdev); virDomainHostdevSubsysPCI *pci = &hostdev->source.subsys.u.pci; g_autofree char *name = g_strdup_printf("hostdev-%s-fd", hostdev->info->alias); - int vfioDeviceFd; + VIR_AUTOCLOSE vfioDeviceFd = -1;
if ((vfioDeviceFd = virPCIDeviceOpenVfioFd(&pci->addr)) < 0) return -1;
+ if (qemuSecuritySetImageFDLabel(priv->driver->securityManager, vm->def, vfioDeviceFd) < 0) + return -1; + hostdevPriv->vfioDeviceFd = qemuFDPassDirectNew(name, &vfioDeviceFd);
return 0; @@ -7776,7 +7784,7 @@ qemuProcessPrepareHostHostdev(virDomainObj *vm) case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: if (virHostdevIsPCIDeviceWithIOMMUFD(hostdev)) { /* Open VFIO device FD */ - if (qemuProcessOpenVfioDeviceFd(hostdev) < 0) + if (qemuProcessOpenVfioDeviceFd(vm, hostdev) < 0) return -1; } break; diff --git a/src/qemu/qemu_process.h b/src/qemu/qemu_process.h index fccd41e1a6..5874214596 100644 --- a/src/qemu/qemu_process.h +++ b/src/qemu/qemu_process.h @@ -136,7 +136,8 @@ int qemuProcessPrepareHostBackendChardevHotplug(virDomainObj *vm,
int qemuProcessOpenIommuFd(virDomainObj *vm);
-int qemuProcessOpenVfioDeviceFd(virDomainHostdevDef *hostdev); +int qemuProcessOpenVfioDeviceFd(virDomainObj *vm, + virDomainHostdevDef *hostdev);
int qemuProcessPrepareHost(virQEMUDriver *driver, virDomainObj *vm,
ACK to these hunk above. BUT...
diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c index 1c3496893c..40f13ec1a5 100644 --- a/src/security/security_apparmor.c +++ b/src/security/security_apparmor.c @@ -45,7 +45,6 @@ #include "virstring.h" #include "virscsi.h" #include "virmdev.h" -#include "viriommufd.h"
#define VIR_FROM_THIS VIR_FROM_SECURITY
@@ -856,17 +855,6 @@ AppArmorSetSecurityHostdevLabel(virSecurityManager *mgr,
if (AppArmorSetSecurityPCILabel(pci, vfioGroupDev, ptr) < 0) return -1; - } else { - g_autofree char *vfiofdDev = NULL; - - if (virPCIDeviceGetVfioPath(pci, &vfiofdDev) < 0) - return -1; - - if (AppArmorSetSecurityPCILabel(pci, vfiofdDev, ptr) < 0) - return -1; - - if (AppArmorSetSecurityPCILabel(pci, VIR_IOMMU_DEV_PATH, ptr) < 0) - return -1;
I don't think removing these hunks (trimmed) is justified. If you'd take a look at virHostdevIsPCIDeviceWithIOMMUFD() it looks like this:
The code I'm removing didn't exist before commit 7d2f91f9cb572ab95d0916bdd1a46dd198874529 introduced it.
Ah, good point. It's pre-existing. So go ahead and merge these as they are. Michal
participants (3)
-
Ján Tomko -
Michal Prívozník -
Pavel Hrdina