]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virt-aa-helper: Rework USB hostdev handling
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 10 Jun 2025 12:31:20 +0000 (14:31 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 2 Jul 2025 11:54:17 +0000 (13:54 +0200)
For an USB device, the virt-aa-helper must put that
/dev/bus/usb/... path associated with given device. The way the
code is currently written not only leads to a memleak (the @usb
variable is allocated only to be overwritten right away), but is
needlessly cumbersome.

We can use virHostdevFindUSBDevice() to find the USB device,
check if its missing and if not add the path associated with it
into the profile.

While at it, also use automatic memory freeing for the variable.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/security/virt-aa-helper.c

index d4358ebf9c8a16754baa17ed731d3fddf4ed0b7f..a56d7e90620cdfd43c1eeddb6d2da457bd826e4d 100644 (file)
@@ -1040,24 +1040,21 @@ get_files(vahControl * ctl)
     for (i = 0; i < ctl->def->nhostdevs; i++)
         if (ctl->def->hostdevs[i]) {
             virDomainHostdevDef *dev = ctl->def->hostdevs[i];
-            virDomainHostdevSubsysUSB *usbsrc = &dev->source.subsys.u.usb;
 
             if (dev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
                 continue;
 
             switch (dev->source.subsys.type) {
             case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
-                virUSBDevice *usb =
-                    virUSBDeviceNew(usbsrc->bus, usbsrc->device, NULL);
+                g_autoptr(virUSBDevice) usb = NULL;
 
-                if (usb == NULL)
+                if (virHostdevFindUSBDevice(dev, true, &usb) < 0)
                     continue;
 
-                if (virHostdevFindUSBDevice(dev, true, &usb) < 0)
+                if (dev->missing)
                     continue;
 
                 rc = virUSBDeviceFileIterate(usb, file_iterate_hostdev_cb, &buf);
-                virUSBDeviceFree(usb);
                 if (rc != 0)
                     goto cleanup;
                 break;