render / rpms / libvirt

Forked from rpms/libvirt 7 months ago
Clone
Daniel P. Berrange a008fc
diff -rup libvirt-0.6.1.orig/src/qemu_driver.c libvirt-0.6.1.new/src/qemu_driver.c
Daniel P. Berrange a008fc
--- libvirt-0.6.1.orig/src/qemu_driver.c	2009-03-17 11:57:04.000000000 +0000
Daniel P. Berrange a008fc
+++ libvirt-0.6.1.new/src/qemu_driver.c	2009-03-17 11:57:12.000000000 +0000
Daniel P. Berrange a008fc
@@ -3765,7 +3765,7 @@ static int qemudDomainAttachDevice(virDo
Daniel P. Berrange a008fc
                 goto cleanup;
Daniel P. Berrange a008fc
             }
Daniel P. Berrange a008fc
             if (driver->securityDriver)
Daniel P. Berrange a008fc
-                driver->securityDriver->domainSetSecurityImageLabel(dom->conn, vm, dev);
Daniel P. Berrange a008fc
+                driver->securityDriver->domainSetSecurityImageLabel(dom->conn, vm, dev->data.disk);
Daniel P. Berrange a008fc
             break;
Daniel P. Berrange a008fc
 
Daniel P. Berrange a008fc
         default:
Daniel P. Berrange a008fc
@@ -3901,7 +3901,7 @@ static int qemudDomainDetachDevice(virDo
Daniel P. Berrange a008fc
          dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_VIRTIO)) {
Daniel P. Berrange a008fc
         ret = qemudDomainDetachPciDiskDevice(dom->conn, vm, dev);
Daniel P. Berrange a008fc
         if (driver->securityDriver)
Daniel P. Berrange a008fc
-            driver->securityDriver->domainRestoreSecurityImageLabel(dom->conn, vm, dev);
Daniel P. Berrange a008fc
+            driver->securityDriver->domainRestoreSecurityImageLabel(dom->conn, dev->data.disk);
Daniel P. Berrange a008fc
     }
Daniel P. Berrange a008fc
     else
Daniel P. Berrange a008fc
         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
Daniel P. Berrange a008fc
diff -rup libvirt-0.6.1.orig/src/security.h libvirt-0.6.1.new/src/security.h
Daniel P. Berrange a008fc
--- libvirt-0.6.1.orig/src/security.h	2009-03-03 16:40:46.000000000 +0000
Daniel P. Berrange a008fc
+++ libvirt-0.6.1.new/src/security.h	2009-03-17 11:57:12.000000000 +0000
Daniel P. Berrange a008fc
@@ -32,11 +32,10 @@ typedef virSecurityDriverStatus (*virSec
Daniel P. Berrange a008fc
 typedef int (*virSecurityDriverOpen) (virConnectPtr conn,
Daniel P. Berrange a008fc
                                       virSecurityDriverPtr drv);
Daniel P. Berrange a008fc
 typedef int (*virSecurityDomainRestoreImageLabel) (virConnectPtr conn,
Daniel P. Berrange a008fc
-                                                   virDomainObjPtr vm,
Daniel P. Berrange a008fc
-                                                   virDomainDeviceDefPtr dev);
Daniel P. Berrange a008fc
+                                                   virDomainDiskDefPtr disk);
Daniel P. Berrange a008fc
 typedef int (*virSecurityDomainSetImageLabel) (virConnectPtr conn,
Daniel P. Berrange a008fc
                                                virDomainObjPtr vm,
Daniel P. Berrange a008fc
-                                               virDomainDeviceDefPtr dev);
Daniel P. Berrange a008fc
+                                               virDomainDiskDefPtr disk);
Daniel P. Berrange a008fc
 typedef int (*virSecurityDomainGenLabel) (virConnectPtr conn,
Daniel P. Berrange a008fc
                                           virDomainObjPtr sec);
Daniel P. Berrange a008fc
 typedef int (*virSecurityDomainGetLabel) (virConnectPtr conn,
Daniel P. Berrange a008fc
diff -rup libvirt-0.6.1.orig/src/security_selinux.c libvirt-0.6.1.new/src/security_selinux.c
Daniel P. Berrange a008fc
--- libvirt-0.6.1.orig/src/security_selinux.c	2009-03-03 16:40:46.000000000 +0000
Daniel P. Berrange a008fc
+++ libvirt-0.6.1.new/src/security_selinux.c	2009-03-17 11:57:12.000000000 +0000
Daniel P. Berrange a008fc
@@ -269,7 +269,7 @@ SELinuxGetSecurityLabel(virConnectPtr co
Daniel P. Berrange a008fc
 }
Daniel P. Berrange a008fc
 
Daniel P. Berrange a008fc
 static int
Daniel P. Berrange a008fc
-SELinuxSetFilecon(virConnectPtr conn, char *path, char *tcon)
Daniel P. Berrange a008fc
+SELinuxSetFilecon(virConnectPtr conn, const char *path, char *tcon)
Daniel P. Berrange a008fc
 {
Daniel P. Berrange a008fc
     char ebuf[1024];
Daniel P. Berrange a008fc
 
Daniel P. Berrange a008fc
@@ -288,28 +288,51 @@ SELinuxSetFilecon(virConnectPtr conn, ch
Daniel P. Berrange a008fc
 
Daniel P. Berrange a008fc
 static int
Daniel P. Berrange a008fc
 SELinuxRestoreSecurityImageLabel(virConnectPtr conn,
Daniel P. Berrange a008fc
-                                 virDomainObjPtr vm,
Daniel P. Berrange a008fc
-                                 virDomainDeviceDefPtr dev)
Daniel P. Berrange a008fc
+                                 virDomainDiskDefPtr disk)
Daniel P. Berrange a008fc
 {
Daniel P. Berrange a008fc
-    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;
Daniel P. Berrange a008fc
+    struct stat buf;
Daniel P. Berrange a008fc
+    security_context_t fcon = NULL;
Daniel P. Berrange a008fc
+    int rc = -1;
Daniel P. Berrange a008fc
+    char *newpath = NULL;
Daniel P. Berrange a008fc
+    const char *path = disk->src;
Daniel P. Berrange a008fc
 
Daniel P. Berrange a008fc
-    if (secdef->imagelabel) {
Daniel P. Berrange a008fc
-        return SELinuxSetFilecon(conn, dev->data.disk->src, default_image_context);
Daniel P. Berrange a008fc
+    if (disk->readonly || disk->shared)
Daniel P. Berrange a008fc
+        return 0;
Daniel P. Berrange a008fc
+
Daniel P. Berrange a008fc
+    if (lstat(path, &buf) != 0)
Daniel P. Berrange a008fc
+        return -1;
Daniel P. Berrange a008fc
+
Daniel P. Berrange a008fc
+    if (S_ISLNK(buf.st_mode)) {
Daniel P. Berrange a008fc
+        if (VIR_ALLOC_N(newpath, buf.st_size + 1) < 0)
Daniel P. Berrange a008fc
+            return -1;
Daniel P. Berrange a008fc
+
Daniel P. Berrange a008fc
+        if (readlink(path, newpath, buf.st_size) < 0)
Daniel P. Berrange a008fc
+            goto err;
Daniel P. Berrange a008fc
+        path = newpath;
Daniel P. Berrange a008fc
+        if (stat(path, &buf) != 0)
Daniel P. Berrange a008fc
+            goto err;
Daniel P. Berrange a008fc
     }
Daniel P. Berrange a008fc
-    return 0;
Daniel P. Berrange a008fc
+
Daniel P. Berrange a008fc
+    if (matchpathcon(path, buf.st_mode, &fcon) == 0)  {
Daniel P. Berrange a008fc
+        rc = SELinuxSetFilecon(conn, path, fcon);
Daniel P. Berrange a008fc
+    }
Daniel P. Berrange a008fc
+err:
Daniel P. Berrange a008fc
+    VIR_FREE(fcon);
Daniel P. Berrange a008fc
+    VIR_FREE(newpath);
Daniel P. Berrange a008fc
+    return rc;
Daniel P. Berrange a008fc
 }
Daniel P. Berrange a008fc
 
Daniel P. Berrange a008fc
 static int
Daniel P. Berrange a008fc
 SELinuxSetSecurityImageLabel(virConnectPtr conn,
Daniel P. Berrange a008fc
                              virDomainObjPtr vm,
Daniel P. Berrange a008fc
-                             virDomainDeviceDefPtr dev)
Daniel P. Berrange a008fc
+                             virDomainDiskDefPtr disk)
Daniel P. Berrange a008fc
 
Daniel P. Berrange a008fc
 {
Daniel P. Berrange a008fc
     const virSecurityLabelDefPtr secdef = &vm->def->seclabel;
Daniel P. Berrange a008fc
 
Daniel P. Berrange a008fc
-    if (secdef->imagelabel) {
Daniel P. Berrange a008fc
-        return SELinuxSetFilecon(conn, dev->data.disk->src, secdef->imagelabel);
Daniel P. Berrange a008fc
-    }
Daniel P. Berrange a008fc
+    if (secdef->imagelabel)
Daniel P. Berrange a008fc
+        return SELinuxSetFilecon(conn, disk->src, secdef->imagelabel);
Daniel P. Berrange a008fc
+
Daniel P. Berrange a008fc
     return 0;
Daniel P. Berrange a008fc
 }
Daniel P. Berrange a008fc
 
Daniel P. Berrange a008fc
@@ -322,7 +345,7 @@ SELinuxRestoreSecurityLabel(virConnectPt
Daniel P. Berrange a008fc
     int rc = 0;
Daniel P. Berrange a008fc
     if (secdef->imagelabel) {
Daniel P. Berrange a008fc
         for (i = 0 ; i < vm->def->ndisks ; i++) {
Daniel P. Berrange a008fc
-            if (SELinuxSetFilecon(conn, vm->def->disks[i]->src, default_image_context) < 0)
Daniel P. Berrange a008fc
+            if (SELinuxRestoreSecurityImageLabel(conn, vm->def->disks[i]) < 0)
Daniel P. Berrange a008fc
                 rc = -1;
Daniel P. Berrange a008fc
         }
Daniel P. Berrange a008fc
         VIR_FREE(secdef->model);
Daniel P. Berrange a008fc
@@ -368,16 +391,11 @@ SELinuxSetSecurityLabel(virConnectPtr co
Daniel P. Berrange a008fc
 
Daniel P. Berrange a008fc
     if (secdef->imagelabel) {
Daniel P. Berrange a008fc
         for (i = 0 ; i < vm->def->ndisks ; i++) {
Daniel P. Berrange a008fc
-            if(setfilecon(vm->def->disks[i]->src, secdef->imagelabel) < 0) {
Daniel P. Berrange a008fc
-                virSecurityReportError(conn, VIR_ERR_ERROR,
Daniel P. Berrange a008fc
-                                       _("%s: unable to set security context "
Daniel P. Berrange a008fc
-                                         "'\%s\' on %s: %s."), __func__,
Daniel P. Berrange a008fc
-                                       secdef->imagelabel,
Daniel P. Berrange a008fc
-                                       vm->def->disks[i]->src,
Daniel P. Berrange a008fc
-                                       virStrerror(errno, ebuf, sizeof ebuf));
Daniel P. Berrange a008fc
-                if (security_getenforce() == 1)
Daniel P. Berrange a008fc
-                    return -1;
Daniel P. Berrange a008fc
-            }
Daniel P. Berrange a008fc
+            if (vm->def->disks[i]->readonly ||
Daniel P. Berrange a008fc
+                vm->def->disks[i]->shared) continue;
Daniel P. Berrange a008fc
+
Daniel P. Berrange a008fc
+            if (SELinuxSetSecurityImageLabel(conn, vm, vm->def->disks[i]) < 0)
Daniel P. Berrange a008fc
+                return -1;
Daniel P. Berrange a008fc
         }
Daniel P. Berrange a008fc
     }
Daniel P. Berrange a008fc