From f84056cf6166332b1f15f3e6584a88f5d42273fe Mon Sep 17 00:00:00 2001
From: "Daniel P. Berrange" <berrange@redhat.com>
Date: Thu, 30 Jan 2014 17:58:36 +0000
Subject: [PATCH 14/14] CVE-2013-6456: Avoid unsafe use of /proc/$PID/root in
 LXC hotunplug code

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

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

Conflicts:
	src/lxc/lxc_driver.c: OOM + cgroups error reporting
---
 src/lxc/lxc_driver.c | 85 ++++++++++++++++++++++++----------------------------
 1 file changed, 39 insertions(+), 46 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
@@ -3224,6 +3224,39 @@ lxcDomainAttachDeviceMknod(virLXCDriverP
 
 
 static int
+lxcDomainAttachDeviceUnlinkHelper(pid_t pid ATTRIBUTE_UNUSED,
+                                  void *opaque)
+{
+    const char *path = opaque;
+
+    VIR_DEBUG("Unlinking %s", path);
+    if (unlink(path) < 0 && errno != ENOENT) {
+        virReportSystemError(errno,
+                             _("Unable to remove device %s"), path);
+        return -1;
+    }
+
+    return 0;
+}
+
+
+static int
+lxcDomainAttachDeviceUnlink(virDomainObjPtr vm,
+                            char *file)
+{
+    virLXCDomainObjPrivatePtr priv = vm->privateData;
+
+    if (virProcessRunInMountNamespace(priv->initpid,
+                                      lxcDomainAttachDeviceUnlinkHelper,
+                                      file) < 0) {
+        return -1;
+    }
+
+    return 0;
+}
+
+
+static int
 lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
                               virDomainObjPtr vm,
                               virDomainDeviceDefPtr dev)
@@ -3837,8 +3870,7 @@ lxcDomainDetachDeviceDiskLive(virDomainO
 
     def = vm->def->disks[i];
 
-    if (virAsprintf(&dst, "/proc/%llu/root/dev/%s",
-                    (unsigned long long)priv->initpid, def->dst) < 0) {
+    if (virAsprintf(&dst, "/dev/%s", def->dst) < 0) {
         virReportOOMError();
         goto cleanup;
     }
@@ -3849,11 +3881,8 @@ lxcDomainDetachDeviceDiskLive(virDomainO
         goto cleanup;
     }
 
-    VIR_DEBUG("Unlinking %s (backed by %s)", dst, def->src);
-    if (unlink(dst) < 0 && errno != ENOENT) {
+    if (lxcDomainAttachDeviceUnlink(vm, dst) < 0) {
         virDomainAuditDisk(vm, def->src, NULL, "detach", false);
-        virReportSystemError(errno,
-                             _("Unable to remove device %s"), dst);
         goto cleanup;
     }
     virDomainAuditDisk(vm, def->src, NULL, "detach", true);
@@ -3948,7 +3977,6 @@ lxcDomainDetachDeviceHostdevUSBLive(virL
     virDomainHostdevDefPtr def = NULL;
     int idx, ret = -1;
     char *dst = NULL;
-    char *vroot;
     virUSBDevicePtr usb = NULL;
 
     if ((idx = virDomainHostdevFind(vm->def,
@@ -3959,14 +3987,7 @@ lxcDomainDetachDeviceHostdevUSBLive(virL
         goto cleanup;
     }
 
-    if (virAsprintf(&vroot, "/proc/%llu/root",
-                    (unsigned long long)priv->initpid) < 0) {
-        virReportOOMError();
-        goto cleanup;
-    }
-
-    if (virAsprintf(&dst, "%s/dev/bus/usb/%03d/%03d",
-                    vroot,
+    if (virAsprintf(&dst, "/dev/bus/usb/%03d/%03d",
                     def->source.subsys.u.usb.bus,
                     def->source.subsys.u.usb.device) < 0) {
         virReportOOMError();
@@ -3983,11 +4004,8 @@ lxcDomainDetachDeviceHostdevUSBLive(virL
                                 def->source.subsys.u.usb.device, NULL)))
         goto cleanup;
 
-    VIR_DEBUG("Unlinking %s", dst);
-    if (unlink(dst) < 0 && errno != ENOENT) {
+    if (lxcDomainAttachDeviceUnlink(vm, dst) < 0) {
         virDomainAuditHostdev(vm, def, "detach", false);
-        virReportSystemError(errno,
-                             _("Unable to remove device %s"), dst);
         goto cleanup;
     }
     virDomainAuditHostdev(vm, def, "detach", true);
@@ -4019,7 +4037,6 @@ lxcDomainDetachDeviceHostdevStorageLive(
     virLXCDomainObjPrivatePtr priv = vm->privateData;
     virDomainHostdevDefPtr def = NULL;
     int i, ret = -1;
-    char *dst = NULL;
 
     if (!priv->initpid) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
@@ -4036,24 +4053,14 @@ lxcDomainDetachDeviceHostdevStorageLive(
         goto cleanup;
     }
 
-    if (virAsprintf(&dst, "/proc/%llu/root/%s",
-                    (unsigned long long)priv->initpid,
-                    def->source.caps.u.storage.block) < 0) {
-        virReportOOMError();
-        goto cleanup;
-    }
-
     if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("devices cgroup isn't mounted"));
         goto cleanup;
     }
 
-    VIR_DEBUG("Unlinking %s", dst);
-    if (unlink(dst) < 0 && errno != ENOENT) {
+    if (lxcDomainAttachDeviceUnlink(vm, def->source.caps.u.storage.block) < 0) {
         virDomainAuditHostdev(vm, def, "detach", false);
-        virReportSystemError(errno,
-                             _("Unable to remove device %s"), dst);
         goto cleanup;
     }
     virDomainAuditHostdev(vm, def, "detach", true);
@@ -4068,7 +4075,6 @@ lxcDomainDetachDeviceHostdevStorageLive(
     ret = 0;
 
 cleanup:
-    VIR_FREE(dst);
     return ret;
 }
 
@@ -4080,7 +4086,6 @@ lxcDomainDetachDeviceHostdevMiscLive(vir
     virLXCDomainObjPrivatePtr priv = vm->privateData;
     virDomainHostdevDefPtr def = NULL;
     int i, ret = -1;
-    char *dst = NULL;
 
     if (!priv->initpid) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
@@ -4097,24 +4102,14 @@ lxcDomainDetachDeviceHostdevMiscLive(vir
         goto cleanup;
     }
 
-    if (virAsprintf(&dst, "/proc/%llu/root/%s",
-                    (unsigned long long)priv->initpid,
-                    def->source.caps.u.misc.chardev) < 0) {
-        virReportOOMError();
-        goto cleanup;
-    }
-
     if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("devices cgroup isn't mounted"));
         goto cleanup;
     }
 
-    VIR_DEBUG("Unlinking %s", dst);
-    if (unlink(dst) < 0 && errno != ENOENT) {
+    if (lxcDomainAttachDeviceUnlink(vm, def->source.caps.u.misc.chardev) < 0) {
         virDomainAuditHostdev(vm, def, "detach", false);
-        virReportSystemError(errno,
-                             _("Unable to remove device %s"), dst);
         goto cleanup;
     }
     virDomainAuditHostdev(vm, def, "detach", true);
@@ -4129,7 +4124,6 @@ lxcDomainDetachDeviceHostdevMiscLive(vir
     ret = 0;
 
 cleanup:
-    VIR_FREE(dst);
     return ret;
 }
 
