render / rpms / libvirt

Forked from rpms/libvirt 11 months ago
Clone
c401cc
From 9e7a97817e2f53416d5f87f4e322ce163af387b5 Mon Sep 17 00:00:00 2001
c401cc
Message-Id: <9e7a97817e2f53416d5f87f4e322ce163af387b5@dist-git>
c401cc
From: "Daniel P. Berrange" <berrange@redhat.com>
c401cc
Date: Tue, 18 Feb 2014 15:45:41 -0700
c401cc
Subject: [PATCH] CVE-2013-6456: Avoid unsafe use of /proc/$PID/root in LXC USB
c401cc
 hotplug
c401cc
c401cc
Rewrite lxcDomainAttachDeviceHostdevSubsysUSBLive function
c401cc
to use the virProcessRunInMountNamespace helper. This avoids
c401cc
risk of a malicious guest replacing /dev with a absolute
c401cc
symlink, tricking the driver into changing the host OS
c401cc
filesystem.
c401cc
c401cc
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
c401cc
(cherry picked from commit 7fba01c15c1f886b4235825692b4c13e88dd9f7b)
c401cc
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c401cc
---
c401cc
 src/lxc/lxc_driver.c | 73 ++++++++++++++++------------------------------------
c401cc
 1 file changed, 22 insertions(+), 51 deletions(-)
c401cc
c401cc
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
c401cc
index e38f037..a15b67a 100644
c401cc
--- a/src/lxc/lxc_driver.c
c401cc
+++ b/src/lxc/lxc_driver.c
c401cc
@@ -3120,6 +3120,13 @@ lxcDomainAttachDeviceMknodHelper(pid_t pid ATTRIBUTE_UNUSED,
c401cc
         def->src = tmpsrc;
c401cc
     }   break;
c401cc
 
c401cc
+    case VIR_DOMAIN_DEVICE_HOSTDEV: {
c401cc
+        virDomainHostdevDefPtr def = data->def->data.hostdev;
c401cc
+        if (virSecurityManagerSetHostdevLabel(data->driver->securityManager,
c401cc
+                                              data->vm->def, def, NULL) < 0)
c401cc
+            goto cleanup;
c401cc
+    }   break;
c401cc
+
c401cc
     default:
c401cc
         virReportError(VIR_ERR_INTERNAL_ERROR,
c401cc
                        _("Unexpected device type %d"),
c401cc
@@ -3414,13 +3421,8 @@ lxcDomainAttachDeviceHostdevSubsysUSBLive(virLXCDriverPtr driver,
c401cc
     virLXCDomainObjPrivatePtr priv = vm->privateData;
c401cc
     virDomainHostdevDefPtr def = dev->data.hostdev;
c401cc
     int ret = -1;
c401cc
-    char *vroot = NULL;
c401cc
     char *src = NULL;
c401cc
-    char *dstdir = NULL;
c401cc
-    char *dstfile = NULL;
c401cc
     struct stat sb;
c401cc
-    mode_t mode;
c401cc
-    bool created = false;
c401cc
     virUSBDevicePtr usb = NULL;
c401cc
 
c401cc
     if (virDomainHostdevFind(vm->def, def, NULL) >= 0) {
c401cc
@@ -3429,27 +3431,13 @@ lxcDomainAttachDeviceHostdevSubsysUSBLive(virLXCDriverPtr driver,
c401cc
         return -1;
c401cc
     }
c401cc
 
c401cc
-    if (virAsprintf(&vroot, "/proc/%llu/root",
c401cc
-                    (unsigned long long)priv->initpid) < 0)
c401cc
-        goto cleanup;
c401cc
-
c401cc
-    if (virAsprintf(&dstdir, "%s/dev/bus/usb/%03d",
c401cc
-                    vroot,
c401cc
-                    def->source.subsys.u.usb.bus) < 0)
c401cc
-        goto cleanup;
c401cc
-
c401cc
-    if (virAsprintf(&dstfile, "%s/%03d",
c401cc
-                    dstdir,
c401cc
-                    def->source.subsys.u.usb.device) < 0)
c401cc
-        goto cleanup;
c401cc
-
c401cc
     if (virAsprintf(&src, "/dev/bus/usb/%03d/%03d",
c401cc
                     def->source.subsys.u.usb.bus,
c401cc
                     def->source.subsys.u.usb.device) < 0)
c401cc
         goto cleanup;
c401cc
 
c401cc
     if (!(usb = virUSBDeviceNew(def->source.subsys.u.usb.bus,
c401cc
-                                def->source.subsys.u.usb.device, vroot)))
c401cc
+                                def->source.subsys.u.usb.device, NULL)))
c401cc
         goto cleanup;
c401cc
 
c401cc
     if (stat(src, &sb) < 0) {
c401cc
@@ -3465,53 +3453,36 @@ lxcDomainAttachDeviceHostdevSubsysUSBLive(virLXCDriverPtr driver,
c401cc
         goto cleanup;
c401cc
     }
c401cc
 
c401cc
-    mode = 0700 | S_IFCHR;
c401cc
-
c401cc
     if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs + 1) < 0)
c401cc
         goto cleanup;
c401cc
 
c401cc
-    if (virFileMakePath(dstdir) < 0) {
c401cc
-        virReportSystemError(errno,
c401cc
-                             _("Unable to create %s"), dstdir);
c401cc
-        goto cleanup;
c401cc
-    }
c401cc
-
c401cc
-    VIR_DEBUG("Creating dev %s (%d,%d)",
c401cc
-              dstfile, major(sb.st_rdev), minor(sb.st_rdev));
c401cc
-    if (mknod(dstfile, mode, sb.st_rdev) < 0) {
c401cc
-        virReportSystemError(errno,
c401cc
-                             _("Unable to create device %s"),
c401cc
-                             dstfile);
c401cc
-        goto cleanup;
c401cc
-    }
c401cc
-    created = true;
c401cc
-
c401cc
-    if (lxcContainerChown(vm->def, dstfile) < 0)
c401cc
-        goto cleanup;
c401cc
-
c401cc
-    if (virSecurityManagerSetHostdevLabel(driver->securityManager,
c401cc
-                                          vm->def, def, vroot) < 0)
c401cc
-        goto cleanup;
c401cc
-
c401cc
     if (virUSBDeviceFileIterate(usb,
c401cc
                                 virLXCSetupHostUsbDeviceCgroup,
c401cc
                                 priv->cgroup) < 0)
c401cc
         goto cleanup;
c401cc
 
c401cc
+    if (lxcDomainAttachDeviceMknod(driver,
c401cc
+                                   0700 | S_IFCHR,
c401cc
+                                   sb.st_rdev,
c401cc
+                                   vm,
c401cc
+                                   dev,
c401cc
+                                   src) < 0) {
c401cc
+        if (virUSBDeviceFileIterate(usb,
c401cc
+                                    virLXCTeardownHostUsbDeviceCgroup,
c401cc
+                                    priv->cgroup) < 0)
c401cc
+            VIR_WARN("cannot deny device %s for domain %s",
c401cc
+                     src, vm->def->name);
c401cc
+        goto cleanup;
c401cc
+    }
c401cc
+
c401cc
     vm->def->hostdevs[vm->def->nhostdevs++] = def;
c401cc
 
c401cc
     ret = 0;
c401cc
 
c401cc
 cleanup:
c401cc
     virDomainAuditHostdev(vm, def, "attach", ret == 0);
c401cc
-    if (ret < 0 && created)
c401cc
-        unlink(dstfile);
c401cc
-
c401cc
     virUSBDeviceFree(usb);
c401cc
     VIR_FREE(src);
c401cc
-    VIR_FREE(dstfile);
c401cc
-    VIR_FREE(dstdir);
c401cc
-    VIR_FREE(vroot);
c401cc
     return ret;
c401cc
 }
c401cc
 
c401cc
-- 
c401cc
1.9.0
c401cc