render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
404507
From f0e402ae3edf7717f1f2c1317f8b445ad20fdda4 Mon Sep 17 00:00:00 2001
404507
Message-Id: <f0e402ae3edf7717f1f2c1317f8b445ad20fdda4@dist-git>
404507
From: Pavel Hrdina <phrdina@redhat.com>
404507
Date: Tue, 5 Dec 2017 14:02:34 +0100
404507
Subject: [PATCH] qemu: fix security labeling for attach/detach of char devices
404507
404507
Commit e93d844b90 was not enough to fix the permission denied
404507
issue.  We need to apply security labels as well.
404507
404507
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1465833
404507
404507
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
404507
(cherry picked from commit 1c57eea3625f59a80bea08d8779837a40acc4660)
404507
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
404507
Reviewed-by: Erik Skultety <eskultet@redhat.com>
404507
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
404507
---
404507
 src/qemu/qemu_hotplug.c  | 10 ++++++++
404507
 src/qemu/qemu_security.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++
404507
 src/qemu/qemu_security.h |  8 +++++++
404507
 3 files changed, 78 insertions(+)
404507
404507
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
404507
index 56e8a93885..eab9ad794a 100644
404507
--- a/src/qemu/qemu_hotplug.c
404507
+++ b/src/qemu/qemu_hotplug.c
404507
@@ -1818,6 +1818,7 @@ int qemuDomainAttachChrDevice(virConnectPtr conn,
404507
     bool chardevAttached = false;
404507
     bool teardowncgroup = false;
404507
     bool teardowndevice = false;
404507
+    bool teardownlabel = false;
404507
     char *tlsAlias = NULL;
404507
     char *secAlias = NULL;
404507
     bool need_release = false;
404507
@@ -1838,6 +1839,10 @@ int qemuDomainAttachChrDevice(virConnectPtr conn,
404507
         goto cleanup;
404507
     teardowndevice = true;
404507
 
404507
+    if (qemuSecuritySetChardevLabel(driver, vm, chr) < 0)
404507
+        goto cleanup;
404507
+    teardownlabel = true;
404507
+
404507
     if (qemuSetupChardevCgroup(vm, chr) < 0)
404507
         goto cleanup;
404507
     teardowncgroup = true;
404507
@@ -1880,6 +1885,8 @@ int qemuDomainAttachChrDevice(virConnectPtr conn,
404507
             qemuDomainReleaseDeviceAddress(vm, &chr->info, NULL);
404507
         if (teardowncgroup && qemuTeardownChardevCgroup(vm, chr) < 0)
404507
             VIR_WARN("Unable to remove chr device cgroup ACL on hotplug fail");
404507
+        if (teardownlabel && qemuSecurityRestoreChardevLabel(driver, vm, chr) < 0)
404507
+            VIR_WARN("Unable to restore security label on char device");
404507
         if (teardowndevice && qemuDomainNamespaceTeardownChardev(driver, vm, chr) < 0)
404507
             VIR_WARN("Unable to remove chr device from /dev");
404507
     }
404507
@@ -4111,6 +4118,9 @@ qemuDomainRemoveChrDevice(virQEMUDriverPtr driver,
404507
     if (qemuTeardownChardevCgroup(vm, chr) < 0)
404507
         VIR_WARN("Failed to remove chr device cgroup ACL");
404507
 
404507
+    if (qemuSecurityRestoreChardevLabel(driver, vm, chr) < 0)
404507
+        VIR_WARN("Unable to restore security label on char device");
404507
+
404507
     if (qemuDomainNamespaceTeardownChardev(driver, vm, chr) < 0)
404507
         VIR_WARN("Unable to remove chr device from /dev");
404507
 
404507
diff --git a/src/qemu/qemu_security.c b/src/qemu/qemu_security.c
404507
index e7d2bbd5a3..2aced22d2d 100644
404507
--- a/src/qemu/qemu_security.c
404507
+++ b/src/qemu/qemu_security.c
404507
@@ -364,3 +364,63 @@ qemuSecurityRestoreInputLabel(virDomainObjPtr vm,
404507
     virSecurityManagerTransactionAbort(driver->securityManager);
404507
     return ret;
404507
 }
404507
+
404507
+
404507
+int
404507
+qemuSecuritySetChardevLabel(virQEMUDriverPtr driver,
404507
+                            virDomainObjPtr vm,
404507
+                            virDomainChrDefPtr chr)
404507
+{
404507
+    int ret = -1;
404507
+    qemuDomainObjPrivatePtr priv = vm->privateData;
404507
+
404507
+    if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT) &&
404507
+        virSecurityManagerTransactionStart(driver->securityManager) < 0)
404507
+        goto cleanup;
404507
+
404507
+    if (virSecurityManagerSetChardevLabel(driver->securityManager,
404507
+                                          vm->def,
404507
+                                          chr->source,
404507
+                                          priv->chardevStdioLogd) < 0)
404507
+        goto cleanup;
404507
+
404507
+    if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT) &&
404507
+        virSecurityManagerTransactionCommit(driver->securityManager,
404507
+                                            vm->pid) < 0)
404507
+        goto cleanup;
404507
+
404507
+    ret = 0;
404507
+ cleanup:
404507
+    virSecurityManagerTransactionAbort(driver->securityManager);
404507
+    return ret;
404507
+}
404507
+
404507
+
404507
+int
404507
+qemuSecurityRestoreChardevLabel(virQEMUDriverPtr driver,
404507
+                                virDomainObjPtr vm,
404507
+                                virDomainChrDefPtr chr)
404507
+{
404507
+    int ret = -1;
404507
+    qemuDomainObjPrivatePtr priv = vm->privateData;
404507
+
404507
+    if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT) &&
404507
+        virSecurityManagerTransactionStart(driver->securityManager) < 0)
404507
+        goto cleanup;
404507
+
404507
+    if (virSecurityManagerRestoreChardevLabel(driver->securityManager,
404507
+                                              vm->def,
404507
+                                              chr->source,
404507
+                                              priv->chardevStdioLogd) < 0)
404507
+        goto cleanup;
404507
+
404507
+    if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT) &&
404507
+        virSecurityManagerTransactionCommit(driver->securityManager,
404507
+                                            vm->pid) < 0)
404507
+        goto cleanup;
404507
+
404507
+    ret = 0;
404507
+ cleanup:
404507
+    virSecurityManagerTransactionAbort(driver->securityManager);
404507
+    return ret;
404507
+}
404507
diff --git a/src/qemu/qemu_security.h b/src/qemu/qemu_security.h
404507
index 76d63f06ec..d54ce6fead 100644
404507
--- a/src/qemu/qemu_security.h
404507
+++ b/src/qemu/qemu_security.h
404507
@@ -76,6 +76,14 @@ int qemuSecuritySetInputLabel(virDomainObjPtr vm,
404507
 int qemuSecurityRestoreInputLabel(virDomainObjPtr vm,
404507
                                   virDomainInputDefPtr input);
404507
 
404507
+int qemuSecuritySetChardevLabel(virQEMUDriverPtr driver,
404507
+                                virDomainObjPtr vm,
404507
+                                virDomainChrDefPtr chr);
404507
+
404507
+int qemuSecurityRestoreChardevLabel(virQEMUDriverPtr driver,
404507
+                                    virDomainObjPtr vm,
404507
+                                    virDomainChrDefPtr chr);
404507
+
404507
 /* Please note that for these APIs there is no wrapper yet. Do NOT blindly add
404507
  * new APIs here. If an API can touch a /dev file add a proper wrapper instead.
404507
  */
404507
-- 
404507
2.15.1
404507