648606
From d519580bd638ceb48829ae66557ca3c5941b4a5f Mon Sep 17 00:00:00 2001
648606
From: Florian Festi <ffesti@redhat.com>
648606
Date: Wed, 4 May 2016 14:05:06 +0200
648606
Subject: [PATCH] Set permissions before moving new files to their final place
648606
648606
---
648606
 lib/fsm.c | 37 ++++++++++++++++++++-----------------
648606
 1 file changed, 20 insertions(+), 17 deletions(-)
648606
648606
diff --git a/lib/fsm.c b/lib/fsm.c
648606
index 1ee7e67..3bb23a4 100644
648606
--- a/lib/fsm.c
648606
+++ b/lib/fsm.c
648606
@@ -621,14 +621,15 @@ static FSM_t fsmFree(FSM_t fsm)
648606
 
648606
 /* Find and set file security context */
648606
 static int fsmSetSELabel(struct selabel_handle *sehandle,
648606
-			 const char *path, mode_t mode)
648606
+			 const char *path, const char * nominalpath,
648606
+			 mode_t mode)
648606
 {
648606
     int rc = 0;
648606
 #if WITH_SELINUX
648606
     if (sehandle) {
648606
 	security_context_t scon = NULL;
648606
 
648606
-	if (selabel_lookup_raw(sehandle, &scon, path, mode) == 0) {
648606
+	if (selabel_lookup_raw(sehandle, &scon, nominalpath, mode) == 0) {
648606
 	    rc = lsetfilecon(path, scon);
648606
 
648606
 	    if (_fsm_debug) {
648606
@@ -1215,7 +1216,7 @@ static int fsmMkdirs(rpmfi fi, rpmfs fs, struct selabel_handle *sehandle)
648606
 		mode_t mode = S_IFDIR | (_dirPerms & 07777);
648606
 		rc = fsmMkdir(dn, mode);
648606
 		if (!rc) {
648606
-		    rc = fsmSetSELabel(sehandle, dn, mode);
648606
+		    rc = fsmSetSELabel(sehandle, dn, dn, mode);
648606
 
648606
 		    rpmlog(RPMLOG_DEBUG,
648606
 			    "%s directory created with perms %04o\n",
648606
@@ -1534,22 +1535,11 @@ static int fsmCommit(FSM_t fsm, int ix)
648606
 	/* Backup on-disk file if needed. Directories are handled earlier */
648606
 	if (!S_ISDIR(st->st_mode))
648606
 	    rc = fsmBackup(fsm);
648606
-        /* Rename temporary to final file name. */
648606
-        if (!S_ISDIR(st->st_mode) && (fsm->suffix || fsm->nsuffix)) {
648606
-            char *npath = fsmFsPath(fsm, 0, fsm->nsuffix);
648606
-            rc = fsmRename(fsm->path, npath, fsm->mapFlags);
648606
-            if (!rc && fsm->nsuffix) {
648606
-                char * opath = fsmFsPath(fsm, 0, NULL);
648606
-                rpmlog(RPMLOG_WARNING, _("%s created as %s\n"),
648606
-                       opath, npath);
648606
-                free(opath);
648606
-            }
648606
-            free(fsm->path);
648606
-            fsm->path = npath;
648606
-        }
648606
         /* Set file security context (if enabled) */
648606
         if (!rc && !getuid()) {
648606
-            rc = fsmSetSELabel(fsm->sehandle, fsm->path, fsm->sb.st_mode);
648606
+	    char * opath = fsmFsPath(fsm, 0, NULL);
648606
+	    rc = fsmSetSELabel(fsm->sehandle, fsm->path, opath, fsm->sb.st_mode);
648606
+	    opath = _free(opath);
648606
         }
648606
         if (S_ISLNK(st->st_mode)) {
648606
             if (!rc && !getuid())
648606
@@ -1571,6 +1561,19 @@ static int fsmCommit(FSM_t fsm, int ix)
648606
                 rc = fsmSetFCaps(fsm->path, rpmfiFCapsIndex(fi, ix));
648606
             }
648606
         }
648606
+        /* Rename temporary to final file name. */
648606
+        if (!rc && !S_ISDIR(st->st_mode) && (fsm->suffix || fsm->nsuffix)) {
648606
+            char *npath = fsmFsPath(fsm, 0, fsm->nsuffix);
648606
+            rc = fsmRename(fsm->path, npath, fsm->mapFlags);
648606
+            if (!rc && fsm->nsuffix) {
648606
+                char * opath = fsmFsPath(fsm, 0, NULL);
648606
+                rpmlog(RPMLOG_WARNING, _("%s created as %s\n"),
648606
+                       opath, npath);
648606
+                free(opath);
648606
+            }
648606
+            free(fsm->path);
648606
+            fsm->path = npath;
648606
+        }
648606
     }
648606
 
648606
     if (rc && fsm->failedFile && *fsm->failedFile == NULL) {
648606
-- 
648606
2.5.5
648606