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