Fix bug #7996 - sgid bit lost on folder rename.
authorJeremy Allison <jra@samba.org>
Thu, 31 Mar 2011 16:57:42 +0000 (09:57 -0700)
committerJeremy Allison <jra@samba.org>
Thu, 31 Mar 2011 16:57:42 +0000 (09:57 -0700)
Refuse to set dos attributes into unix mode bits on such a
folder.

source3/include/proto.h
source3/smbd/dosmode.c
source3/smbd/posix_acls.c

index aa0287cb318f267e568df1b408a9bb001686b6c0..9d82b13dceae6f9f4993bf157a455cd1aa32f96a 100644 (file)
@@ -4677,6 +4677,7 @@ uint32_t map_canon_ace_perms(int snum,
                                 mode_t perms,
                                 bool directory_ace);
 NTSTATUS unpack_nt_owners(connection_struct *conn, uid_t *puser, gid_t *pgrp, uint32 security_info_sent, const struct security_descriptor *psd);
+bool current_user_in_group(connection_struct *conn, gid_t gid);
 SMB_ACL_T free_empty_sys_acl(connection_struct *conn, SMB_ACL_T the_acl);
 NTSTATUS posix_fget_nt_acl(struct files_struct *fsp, uint32_t security_info,
                           struct security_descriptor **ppdesc);
index 325a3c646102fb9e069f8220d936ad8bd63fcb43..d8c12ee4c34ecbcc643b1e8aacdd8f2bdc892d8a 100644 (file)
@@ -796,6 +796,27 @@ int file_set_dosmode(connection_struct *conn, struct smb_filename *smb_fname,
                unixmode |= (smb_fname->st.st_ex_mode & (S_IWUSR|S_IWGRP|S_IWOTH));
        }
 
+       /*
+        * From the chmod 2 man page:
+        *
+        * "If the calling process is not privileged, and the group of the file
+        * does not match the effective group ID of the process or one of its
+        * supplementary group IDs, the S_ISGID bit will be turned off, but
+        * this will not cause an error to be returned."
+        *
+        * Simply refuse to do the chmod in this case.
+        */
+
+       if (S_ISDIR(smb_fname->st.st_ex_mode) && (unixmode & S_ISGID) &&
+                       geteuid() != sec_initial_uid() &&
+                       !current_user_in_group(conn, smb_fname->st.st_ex_gid)) {
+               DEBUG(3,("file_set_dosmode: setgid bit cannot be "
+                       "set for directory %s\n",
+                       smb_fname_str_dbg(smb_fname)));
+               errno = EPERM;
+               return -1;
+       }
+
        ret = SMB_VFS_CHMOD(conn, smb_fname->base_name, unixmode);
        if (ret == 0) {
                if(!newfile || (lret != -1)) {
index ebdc644ef9e9eaee6b25c08d34057471a313b84a..e41ee3c32f7b1f4b2492e664b04563c6bbb97680 100644 (file)
@@ -2651,7 +2651,7 @@ static canon_ace *canonicalise_acl(struct connection_struct *conn,
  Check if the current user group list contains a given group.
 ****************************************************************************/
 
-static bool current_user_in_group(connection_struct *conn, gid_t gid)
+bool current_user_in_group(connection_struct *conn, gid_t gid)
 {
        int i;
        const struct security_unix_token *utok = get_current_utok(conn);