bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#9002: file-error Doing chown operation not permitted


From: Paul Eggert
Subject: bug#9002: file-error Doing chown operation not permitted
Date: Mon, 18 Jul 2011 10:24:06 -0700
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110621 Fedora/3.1.11-1.fc14 Thunderbird/3.1.11

In yet more testing I found a couple more issues.
First, many systems allow you to change gid on files
that you can't set the uid of, if you're a member of
the group and own the file.  Second, if we can't set
the uid and/or gid we shouldn't enable the setuid and/or
gid bits, as the result would be setuid and/or setgid
to the wrong user.  I pushed the following further
patch into the trunk.

* fileio.c (Fcopy_file): Adjust mode if fchown fails.  (Bug#9002)
If fchown fails to set both uid and gid, try to set just gid,
as that is sometimes allowed.  Adjust the file's mode to eliminate
setuid or setgid bits that are inappropriate if fchown fails.
=== modified file 'src/fileio.c'
--- src/fileio.c        2011-07-17 01:18:51 +0000
+++ src/fileio.c        2011-07-18 17:15:29 +0000
@@ -38,8 +38,6 @@
 #include <selinux/context.h>
 #endif

-#include <ignore-value.h>
-
 #include "lisp.h"
 #include "intervals.h"
 #include "buffer.h"
@@ -1961,9 +1959,21 @@
      owner and group.  */
   if (input_file_statable_p)
     {
+      int mode_mask = 07777;
       if (!NILP (preserve_uid_gid))
-       ignore_value (fchown (ofd, st.st_uid, st.st_gid));
-      if (fchmod (ofd, st.st_mode & 07777) != 0)
+       {
+         /* Attempt to change owner and group.  If that doesn't work
+            attempt to change just the group, as that is sometimes allowed.
+            Adjust the mode mask to eliminate setuid or setgid bits
+            that are inappropriate if the owner and group are wrong.  */
+         if (fchown (ofd, st.st_uid, st.st_gid) != 0)
+           {
+             mode_mask &= ~06000;
+             if (fchown (ofd, -1, st.st_gid) == 0)
+               mode_mask |= 02000;
+           }
+       }
+      if (fchmod (ofd, st.st_mode & mode_mask) != 0)
        report_file_error ("Doing chmod", Fcons (newname, Qnil));
     }
 #endif /* not MSDOS */






reply via email to

[Prev in Thread] Current Thread [Next in Thread]