<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" dir="ltr">
<p style="margin-top:0;margin-bottom:0">Hi Michael,</p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">Thanks for looking at the submission. I've made the proposed changes and can verify that the fix works. :)</p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0"></p>
<div>There error has gone away with the container starting. Below is a snippet of the device node from the intermediate mount namespace that eventually ends up target container's mount namespace:<br>
<div>$ sudo nsenter -t 7397 -m<br>
# ls -laZ /var/run/libvirt/lxc/lxc_0.dev/bus/usb/001/002<br>
crwx------. 1 root root system_u:object_r:svirt_sandbox_file_t:s0:c600,c910 189, 1 Jan 23 14:55 /var/run/libvirt/lxc/lxc_0.dev/bus/usb/001/002</div>
<br>
</div>
Thanks,
<p></p>
<p style="margin-top:0;margin-bottom:0">Randy<br>
</p>
<br>
<br>
<div style="color: rgb(0, 0, 0);">
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>From:</b> Michal Privoznik <mprivozn@redhat.com><br>
<b>Sent:</b> Friday, January 19, 2018 11:19 AM<br>
<b>To:</b> Randy Aybar; libvir-list@redhat.com<br>
<b>Subject:</b> Re: [libvirt] Libvirt fails to apply security context to fd/node to USB device</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">On 01/16/2018 07:20 PM, Randy Aybar wrote:<br>
> Hi,<br>
> <br>
> <br>
> I'm attempting to attach and expose a USB device (WiFi adapter for testing) to an LXC container with SELinux enabled. But when enabling the XML snippet, the container fails to start with this error:<br>
> <br>
> <br>
> 2018-01-12 19:24:31.914+0000: 2181: error : virSecuritySELinuxSetFileconHelper:1182 : unable to set security context 'system_u:object_r:svirt_sandbox_file_t:s0:c139,c284' on '//var/run/libvirt/lxc/lxc_0.dev/bus/usb//dev/bus/usb/002/002': No such file or directory<br>
> <br>
> Failure in libvirt_lxc startup: unable to set security context 'system_u:object_r:svirt_sandbox_file_t:s0:c139,c284' on '//var/run/libvirt/lxc/lxc_0.dev/bus/usb//dev/bus/usb/002/002': No such file or directory<br>
<br>
Yes, this is a libvirt bug. And your analysis is coorect. The problem is:<br>
<br>
1) in virLXCControllerSetupHostdevSubsysUSB the first part of path is<br>
constructed: vroot = /var/run/libvirt/lxc/lxc_0.dev/bus/usb<br>
<br>
2) then, virSecurityManagerSetHostdevLabel() is called, which<br>
subsequently calls virSecuritySELinuxSetHostdevSubsysLabel().<br>
<br>
3) The SELinuxSetHostdevSubsysLabel() calls virUSBDeviceNew(..,vroot)<br>
where vroot is the path from step 1). The virUSBDeviceNew then does:<br>
<br>
    if (virAsprintf(&dev->path, "%s" USB_DEVFS "%03d/%03d",<br>
                    vroot ? vroot : "",<br>
                    dev->bus, dev->dev) < 0) {<br>
        virUSBDeviceFree(dev);<br>
        return NULL;<br>
    }<br>
<br>
where USB_DEVFS is defined as:<br>
<br>
# define USB_DEVFS "/dev/bus/usb/"<br>
<br>
So in the end, dev->path contains the path that you're seeing. I think<br>
this the fix:<br>
<br>
diff --git a/src/util/virusb.c b/src/util/virusb.c<br>
index 6359235ff..99ee08657 100644<br>
--- a/src/util/virusb.c<br>
+++ b/src/util/virusb.c<br>
@@ -343,9 +343,9 @@ virUSBDeviceNew(unsigned int bus,<br>
         virUSBDeviceFree(dev);<br>
         return NULL;<br>
     }<br>
-    if (virAsprintf(&dev->path, "%s" USB_DEVFS "%03d/%03d",<br>
-                    vroot ? vroot : "",<br>
-                    dev->bus, dev->dev) < 0) {<br>
+<br>
+    if ((vroot && virAsprintf(&dev->path, "%s/%03d/%03d", vroot, dev->bus, dev->dev) < 0) ||<br>
+        (!vroot && virAsprintf(&dev->path, USB_DEVFS "%03d/%03d", dev->bus, dev->dev) < 0)) {<br>
         virUSBDeviceFree(dev);<br>
         return NULL;<br>
     }<br>
<br>
(of course after breaking down the long lines). Can you please test it?<br>
<br>
Michal<br>
</div>
</span></font></div>
</div>
</div>
</body>
</html>