Blob Blame History Raw
From e294159bef2aa05b9c9de22ece4d7b8893fb68c9 Mon Sep 17 00:00:00 2001
Message-Id: <e294159bef2aa05b9c9de22ece4d7b8893fb68c9@dist-git>
From: Eric Blake <eblake@redhat.com>
Date: Wed, 26 Feb 2014 14:54:37 +0100
Subject: [PATCH] storage: skip selinux cleanup when fd not available

https://bugzilla.redhat.com/show_bug.cgi?id=1032370

When attempting to backport gluster pools to an older versoin
where there is no VIR_STRDUP, I got a crash from calling
strdup(,NULL). Rather than relying on the current else branch
safely doing nothing when there is no fd, it is easier to just
skip it. While at it, there's no need to explicitly set
perms.label to NULL after a VIR_FREE().

* src/storage/storage_backend.c
(virStorageBackendUpdateVolTargetInfoFD): Minor optimization.

Signed-off-by: Eric Blake <eblake@redhat.com>
(cherry picked from commit c8b8b50c3bcf46ebe5f3a152ff681779024a0846)
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/storage/storage_backend.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c
index ae24c74..982cf43 100644
--- a/src/storage/storage_backend.c
+++ b/src/storage/storage_backend.c
@@ -1356,24 +1356,22 @@ virStorageBackendUpdateVolTargetInfoFD(virStorageVolTargetPtr target,
 
 #if WITH_SELINUX
     /* XXX: make this a security driver call */
-    if (fd >= 0 && fgetfilecon_raw(fd, &filecon) == -1) {
-        if (errno != ENODATA && errno != ENOTSUP) {
-            virReportSystemError(errno,
-                                 _("cannot get file context of '%s'"),
-                                 target->path);
-            return -1;
+    if (fd >= 0) {
+        if (fgetfilecon_raw(fd, &filecon) == -1) {
+            if (errno != ENODATA && errno != ENOTSUP) {
+                virReportSystemError(errno,
+                                     _("cannot get file context of '%s'"),
+                                     target->path);
+                return -1;
+            }
         } else {
-            target->perms.label = NULL;
-        }
-    } else {
-        if (VIR_STRDUP(target->perms.label, filecon) < 0) {
+            if (VIR_STRDUP(target->perms.label, filecon) < 0) {
+                freecon(filecon);
+                return -1;
+            }
             freecon(filecon);
-            return -1;
         }
-        freecon(filecon);
     }
-#else
-    target->perms.label = NULL;
 #endif
 
     return 0;
-- 
1.9.0