render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
c401cc
From ca5a9fee089184bd26e1c0daefd3802c50f120c7 Mon Sep 17 00:00:00 2001
c401cc
Message-Id: <ca5a9fee089184bd26e1c0daefd3802c50f120c7@dist-git>
c401cc
From: "Daniel P. Berrange" <berrange@redhat.com>
c401cc
Date: Tue, 18 Feb 2014 15:45:44 -0700
c401cc
Subject: [PATCH] CVE-2013-6456: Avoid unsafe use of /proc/$PID/root in LXC
c401cc
 hotunplug code
c401cc
c401cc
Rewrite multiple hotunplug functions to to use the
c401cc
virProcessRunInMountNamespace helper. This avoids
c401cc
risk of a malicious guest replacing /dev with an 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 5fc590ad9f4071350a8df4d567ba88baacc8334d)
c401cc
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c401cc
---
c401cc
 src/lxc/lxc_driver.c | 79 ++++++++++++++++++++++++++--------------------------
c401cc
 1 file changed, 39 insertions(+), 40 deletions(-)
c401cc
c401cc
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
c401cc
index 91e491c..1aaa6e7 100644
c401cc
--- a/src/lxc/lxc_driver.c
c401cc
+++ b/src/lxc/lxc_driver.c
c401cc
@@ -3179,6 +3179,39 @@ lxcDomainAttachDeviceMknod(virLXCDriverPtr driver,
c401cc
 
c401cc
 
c401cc
 static int
c401cc
+lxcDomainAttachDeviceUnlinkHelper(pid_t pid ATTRIBUTE_UNUSED,
c401cc
+                                  void *opaque)
c401cc
+{
c401cc
+    const char *path = opaque;
c401cc
+
c401cc
+    VIR_DEBUG("Unlinking %s", path);
c401cc
+    if (unlink(path) < 0 && errno != ENOENT) {
c401cc
+        virReportSystemError(errno,
c401cc
+                             _("Unable to remove device %s"), path);
c401cc
+        return -1;
c401cc
+    }
c401cc
+
c401cc
+    return 0;
c401cc
+}
c401cc
+
c401cc
+
c401cc
+static int
c401cc
+lxcDomainAttachDeviceUnlink(virDomainObjPtr vm,
c401cc
+                            char *file)
c401cc
+{
c401cc
+    virLXCDomainObjPrivatePtr priv = vm->privateData;
c401cc
+
c401cc
+    if (virProcessRunInMountNamespace(priv->initpid,
c401cc
+                                      lxcDomainAttachDeviceUnlinkHelper,
c401cc
+                                      file) < 0) {
c401cc
+        return -1;
c401cc
+    }
c401cc
+
c401cc
+    return 0;
c401cc
+}
c401cc
+
c401cc
+
c401cc
+static int
c401cc
 lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
c401cc
                               virDomainObjPtr vm,
c401cc
                               virDomainDeviceDefPtr dev)
c401cc
@@ -3769,8 +3802,7 @@ lxcDomainDetachDeviceDiskLive(virDomainObjPtr vm,
c401cc
 
c401cc
     def = vm->def->disks[idx];
c401cc
 
c401cc
-    if (virAsprintf(&dst, "/proc/%llu/root/dev/%s",
c401cc
-                    (unsigned long long)priv->initpid, def->dst) < 0)
c401cc
+    if (virAsprintf(&dst, "/dev/%s", def->dst) < 0)
c401cc
         goto cleanup;
c401cc
 
c401cc
     if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
c401cc
@@ -3779,11 +3811,8 @@ lxcDomainDetachDeviceDiskLive(virDomainObjPtr vm,
c401cc
         goto cleanup;
c401cc
     }
c401cc
 
c401cc
-    VIR_DEBUG("Unlinking %s (backed by %s)", dst, def->src);
c401cc
-    if (unlink(dst) < 0 && errno != ENOENT) {
c401cc
+    if (lxcDomainAttachDeviceUnlink(vm, dst) < 0) {
c401cc
         virDomainAuditDisk(vm, def->src, NULL, "detach", false);
c401cc
-        virReportSystemError(errno,
c401cc
-                             _("Unable to remove device %s"), dst);
c401cc
         goto cleanup;
c401cc
     }
c401cc
     virDomainAuditDisk(vm, def->src, NULL, "detach", true);
c401cc
@@ -3878,7 +3907,6 @@ lxcDomainDetachDeviceHostdevUSBLive(virLXCDriverPtr driver,
c401cc
     virDomainHostdevDefPtr def = NULL;
c401cc
     int idx, ret = -1;
c401cc
     char *dst = NULL;
c401cc
-    char *vroot = NULL;
c401cc
     virUSBDevicePtr usb = NULL;
c401cc
 
c401cc
     if ((idx = virDomainHostdevFind(vm->def,
c401cc
@@ -3889,12 +3917,7 @@ lxcDomainDetachDeviceHostdevUSBLive(virLXCDriverPtr driver,
c401cc
         goto cleanup;
c401cc
     }
c401cc
 
c401cc
-    if (virAsprintf(&vroot, "/proc/%llu/root",
c401cc
-                    (unsigned long long)priv->initpid) < 0)
c401cc
-        goto cleanup;
c401cc
-
c401cc
-    if (virAsprintf(&dst, "%s/dev/bus/usb/%03d/%03d",
c401cc
-                    vroot,
c401cc
+    if (virAsprintf(&dst, "/dev/bus/usb/%03d/%03d",
c401cc
                     def->source.subsys.u.usb.bus,
c401cc
                     def->source.subsys.u.usb.device) < 0)
c401cc
         goto cleanup;
c401cc
@@ -3909,11 +3932,8 @@ lxcDomainDetachDeviceHostdevUSBLive(virLXCDriverPtr driver,
c401cc
                                 def->source.subsys.u.usb.device, NULL)))
c401cc
         goto cleanup;
c401cc
 
c401cc
-    VIR_DEBUG("Unlinking %s", dst);
c401cc
-    if (unlink(dst) < 0 && errno != ENOENT) {
c401cc
+    if (lxcDomainAttachDeviceUnlink(vm, dst) < 0) {
c401cc
         virDomainAuditHostdev(vm, def, "detach", false);
c401cc
-        virReportSystemError(errno,
c401cc
-                             _("Unable to remove device %s"), dst);
c401cc
         goto cleanup;
c401cc
     }
c401cc
     virDomainAuditHostdev(vm, def, "detach", true);
c401cc
@@ -3936,7 +3956,6 @@ lxcDomainDetachDeviceHostdevUSBLive(virLXCDriverPtr driver,
c401cc
 cleanup:
c401cc
     virUSBDeviceFree(usb);
c401cc
     VIR_FREE(dst);
c401cc
-    VIR_FREE(vroot);
c401cc
     return ret;
c401cc
 }
c401cc
 
c401cc
@@ -3948,7 +3967,6 @@ lxcDomainDetachDeviceHostdevStorageLive(virDomainObjPtr vm,
c401cc
     virLXCDomainObjPrivatePtr priv = vm->privateData;
c401cc
     virDomainHostdevDefPtr def = NULL;
c401cc
     int idx, ret = -1;
c401cc
-    char *dst = NULL;
c401cc
 
c401cc
     if (!priv->initpid) {
c401cc
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
c401cc
@@ -3965,22 +3983,14 @@ lxcDomainDetachDeviceHostdevStorageLive(virDomainObjPtr vm,
c401cc
         goto cleanup;
c401cc
     }
c401cc
 
c401cc
-    if (virAsprintf(&dst, "/proc/%llu/root/%s",
c401cc
-                    (unsigned long long)priv->initpid,
c401cc
-                    def->source.caps.u.storage.block) < 0)
c401cc
-        goto cleanup;
c401cc
-
c401cc
     if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
c401cc
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
c401cc
                        _("devices cgroup isn't mounted"));
c401cc
         goto cleanup;
c401cc
     }
c401cc
 
c401cc
-    VIR_DEBUG("Unlinking %s", dst);
c401cc
-    if (unlink(dst) < 0 && errno != ENOENT) {
c401cc
+    if (lxcDomainAttachDeviceUnlink(vm, def->source.caps.u.storage.block) < 0) {
c401cc
         virDomainAuditHostdev(vm, def, "detach", false);
c401cc
-        virReportSystemError(errno,
c401cc
-                             _("Unable to remove device %s"), dst);
c401cc
         goto cleanup;
c401cc
     }
c401cc
     virDomainAuditHostdev(vm, def, "detach", true);
c401cc
@@ -3995,7 +4005,6 @@ lxcDomainDetachDeviceHostdevStorageLive(virDomainObjPtr vm,
c401cc
     ret = 0;
c401cc
 
c401cc
 cleanup:
c401cc
-    VIR_FREE(dst);
c401cc
     return ret;
c401cc
 }
c401cc
 
c401cc
@@ -4007,7 +4016,6 @@ lxcDomainDetachDeviceHostdevMiscLive(virDomainObjPtr vm,
c401cc
     virLXCDomainObjPrivatePtr priv = vm->privateData;
c401cc
     virDomainHostdevDefPtr def = NULL;
c401cc
     int idx, ret = -1;
c401cc
-    char *dst = NULL;
c401cc
 
c401cc
     if (!priv->initpid) {
c401cc
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
c401cc
@@ -4024,22 +4032,14 @@ lxcDomainDetachDeviceHostdevMiscLive(virDomainObjPtr vm,
c401cc
         goto cleanup;
c401cc
     }
c401cc
 
c401cc
-    if (virAsprintf(&dst, "/proc/%llu/root/%s",
c401cc
-                    (unsigned long long)priv->initpid,
c401cc
-                    def->source.caps.u.misc.chardev) < 0)
c401cc
-        goto cleanup;
c401cc
-
c401cc
     if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
c401cc
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
c401cc
                        _("devices cgroup isn't mounted"));
c401cc
         goto cleanup;
c401cc
     }
c401cc
 
c401cc
-    VIR_DEBUG("Unlinking %s", dst);
c401cc
-    if (unlink(dst) < 0 && errno != ENOENT) {
c401cc
+    if (lxcDomainAttachDeviceUnlink(vm, def->source.caps.u.misc.chardev) < 0) {
c401cc
         virDomainAuditHostdev(vm, def, "detach", false);
c401cc
-        virReportSystemError(errno,
c401cc
-                             _("Unable to remove device %s"), dst);
c401cc
         goto cleanup;
c401cc
     }
c401cc
     virDomainAuditHostdev(vm, def, "detach", true);
c401cc
@@ -4054,7 +4054,6 @@ lxcDomainDetachDeviceHostdevMiscLive(virDomainObjPtr vm,
c401cc
     ret = 0;
c401cc
 
c401cc
 cleanup:
c401cc
-    VIR_FREE(dst);
c401cc
     return ret;
c401cc
 }
c401cc
 
c401cc
-- 
c401cc
1.9.0
c401cc