From 0e9fee68b3bff24e4d3ab48de8129946202f3bc0 Mon Sep 17 00:00:00 2001
From: "Daniel P. Berrange" <berrange@redhat.com>
Date: Thu, 30 Jan 2014 17:47:39 +0000
Subject: [PATCH 13/14] CVE-2013-6456: Avoid unsafe use of /proc/$PID/root in
 LXC chardev hostdev hotplug

Rewrite lxcDomainAttachDeviceHostdevMiscLive function
to use the virProcessRunInMountNamespace helper. This avoids
risk of a malicious guest replacing /dev with a absolute
symlink, tricking the driver into changing the host OS
filesystem.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
(cherry picked from commit 1cadeafcaa422844a27ef622e2a7041d0235bcb3)

Conflicts:
	src/lxc/lxc_driver.c: OOM + cgroups error reporting
---
 src/lxc/lxc_driver.c | 69 ++++++++++++++++------------------------------------
 1 file changed, 21 insertions(+), 48 deletions(-)

Index: libvirt-1.0.5.9/src/lxc/lxc_driver.c
===================================================================
--- libvirt-1.0.5.9.orig/src/lxc/lxc_driver.c
+++ libvirt-1.0.5.9/src/lxc/lxc_driver.c
@@ -3629,11 +3629,7 @@ lxcDomainAttachDeviceHostdevMiscLive(vir
     virLXCDomainObjPrivatePtr priv = vm->privateData;
     virDomainHostdevDefPtr def = dev->data.hostdev;
     int ret = -1;
-    char *dst = NULL;
-    char *vroot = NULL;
     struct stat sb;
-    bool created = false;
-    mode_t mode = 0;
 
     if (!def->source.caps.u.misc.chardev) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
@@ -3661,16 +3657,14 @@ lxcDomainAttachDeviceHostdevMiscLive(vir
         goto cleanup;
     }
 
-    if (virAsprintf(&vroot, "/proc/%llu/root",
-                    (unsigned long long)priv->initpid) < 0) {
-        virReportOOMError();
-        goto cleanup;
-    }
-
-    if (virAsprintf(&dst, "%s/%s",
-                    vroot,
-                    def->source.caps.u.misc.chardev) < 0) {
-        virReportOOMError();
+    if (virCgroupAllowDevice(priv->cgroup,
+                             'c',
+                             major(sb.st_rdev),
+                             minor(sb.st_rdev),
+                             VIR_CGROUP_DEVICE_RWM) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unable to allow device %s"),
+                       def->source.caps.u.misc.chardev);
         goto cleanup;
     }
 
@@ -3679,29 +3673,19 @@ lxcDomainAttachDeviceHostdevMiscLive(vir
         goto cleanup;
     }
 
-    mode = 0700 | S_IFCHR;
-
-    VIR_DEBUG("Creating dev %s (%d,%d)",
-              def->source.caps.u.misc.chardev,
-              major(sb.st_rdev), minor(sb.st_rdev));
-    if (mknod(dst, mode, sb.st_rdev) < 0) {
-        virReportSystemError(errno,
-                             _("Unable to create device %s"),
-                             dst);
-        goto cleanup;
-    }
-    created = true;
-
-    if (virSecurityManagerSetHostdevLabel(driver->securityManager,
-                                          vm->def, def, vroot) < 0)
-        goto cleanup;
-
-    if (virCgroupAllowDevicePath(priv->cgroup, def->source.caps.u.misc.chardev,
-                                 VIR_CGROUP_DEVICE_RW |
-                                 VIR_CGROUP_DEVICE_MKNOD) != 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("cannot allow device %s for domain %s"),
-                       def->source.caps.u.misc.chardev, vm->def->name);
+    if (lxcDomainAttachDeviceMknod(driver,
+                                   0700 | S_IFBLK,
+                                   sb.st_rdev,
+                                   vm,
+                                   dev,
+                                   def->source.caps.u.misc.chardev) < 0) {
+        if (virCgroupDenyDevice(priv->cgroup,
+                                'c',
+                                major(sb.st_rdev),
+                                minor(sb.st_rdev),
+                                VIR_CGROUP_DEVICE_RWM) < 0)
+            VIR_WARN("cannot deny device %s for domain %s",
+                     def->source.caps.u.storage.block, vm->def->name);
         goto cleanup;
     }
 
@@ -3711,10 +3695,6 @@ lxcDomainAttachDeviceHostdevMiscLive(vir
 
 cleanup:
     virDomainAuditHostdev(vm, def, "attach", ret == 0);
-    if (dst && created && ret < 0)
-        unlink(dst);
-    VIR_FREE(dst);
-    VIR_FREE(vroot);
     return ret;
 }
 
