Only force default umask during transaction (RhBug:494440)
authorPanu Matilainen <pmatilai@redhat.com>
Thu, 18 Nov 2010 10:50:55 +0000 (12:50 +0200)
committerPanu Matilainen <pmatilai@redhat.com>
Thu, 18 Nov 2010 10:50:55 +0000 (12:50 +0200)
- For rpm itself forging global umask on init is kinda convenient, but can
  be troublesome for API users. This is especially bad in python bindings
  where just importing the rpm module silently changes process umask with
  no good reason.
- Instead of global setting on init, only change the umask to 022 default
  for the duration of rpmtsRun() where it's necessary for consistent
  transaction results on implicitly created directories and files created
  by scriptlets. This way we dont affect callers and provide better
  "protection" for ourselves too - we don't know if API users change
  umask again behind our back if we just set it on initialization.
- To make matters more fun, Lua scripts can change our umask. Save
  and restore umask when running Lua scriptlets.

lib/rpmrc.c
lib/rpmscript.c
lib/transaction.c

index 634a021..1b47dc3 100644 (file)
@@ -1606,10 +1606,6 @@ exit:
 
 int rpmReadConfigFiles(const char * file, const char * target)
 {
-    mode_t mode = 0022;
-    /* Reset umask to its default umask(2) value. */
-    mode = umask(mode);
-
     /* Force preloading of dlopen()'ed libraries in case we go chrooting */
     (void) gethostbyname("localhost");
     (void) rpmInitCrypto();
index 6a05a1a..ed52608 100644 (file)
@@ -54,16 +54,20 @@ static rpmRC runLuaScript(int selinux, ARGV_const_t prefixes,
     var = rpmluavFree(var);
     rpmluaPop(lua);
 
-    /* Lua scripts can change our cwd, save and restore */
+    /* Lua scripts can change our cwd and umask, save and restore */
     cwd = open(".", O_RDONLY);
     if (cwd != -1) {
        int xx;
+       mode_t oldmask = umask(0);
+       umask(oldmask);
+
        if (chdir("/") == 0 && rpmluaRunScript(lua, script, sname) == 0) {
            rc = RPMRC_OK;
        }
        /* XXX no way to return error from restore meaningfully atm */
        xx = fchdir(cwd);
        close(cwd);
+       umask(oldmask);
     }
 
     rpmluaDelVar(lua, "arg");
index 07971c2..ed6f738 100644 (file)
@@ -1393,6 +1393,8 @@ int rpmtsRun(rpmts ts, rpmps okProbs, rpmprobFilterFlags ignoreSet)
     int rc = -1; /* assume failure */
     rpmlock lock = NULL;
     rpmps tsprobs = NULL;
+    /* Force default 022 umask during transaction for consistent results */
+    mode_t oldmask = umask(022);
 
     /* XXX programmer error segfault avoidance. */
     if (rpmtsNElements(ts) <= 0) {
@@ -1454,6 +1456,7 @@ int rpmtsRun(rpmts ts, rpmps okProbs, rpmprobFilterFlags ignoreSet)
 
 exit:
     /* Finish up... */
+    (void) umask(oldmask);
     (void) rpmtsFinish(ts);
     tsprobs = rpmpsFree(tsprobs);
     rpmlockFree(lock);