@@ -36,6 +36,7 @@ typedef struct dbase_policydb dbase_t;
36
36
#include "database_policydb.h"
37
37
#include "handle.h"
38
38
39
+ #include <selinux/restorecon.h>
39
40
#include <selinux/selinux.h>
40
41
#include <sepol/policydb.h>
41
42
#include <sepol/module.h>
@@ -767,6 +768,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
767
768
if (!retval && rename (tmp , dst ) == -1 )
768
769
return -1 ;
769
770
771
+ semanage_setfiles (dst );
770
772
out :
771
773
errno = errsv ;
772
774
return retval ;
@@ -819,6 +821,8 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
819
821
goto cleanup ;
820
822
}
821
823
umask (mask );
824
+
825
+ semanage_setfiles (dst );
822
826
}
823
827
824
828
for (i = 0 ; i < len ; i ++ ) {
@@ -837,6 +841,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
837
841
goto cleanup ;
838
842
}
839
843
umask (mask );
844
+ semanage_setfiles (path2 );
840
845
} else if (S_ISREG (sb .st_mode ) && flag == 1 ) {
841
846
mask = umask (0077 );
842
847
if (semanage_copy_file (path , path2 , sb .st_mode ,
@@ -938,6 +943,7 @@ int semanage_mkdir(semanage_handle_t *sh, const char *path)
938
943
939
944
}
940
945
umask (mask );
946
+ semanage_setfiles (path );
941
947
}
942
948
else {
943
949
/* check that it really is a directory */
@@ -1614,16 +1620,19 @@ static int semanage_validate_and_compile_fcontexts(semanage_handle_t * sh)
1614
1620
semanage_final_path (SEMANAGE_FINAL_TMP , SEMANAGE_FC )) != 0 ) {
1615
1621
goto cleanup ;
1616
1622
}
1623
+ semanage_setfiles (semanage_final_path (SEMANAGE_FINAL_TMP , SEMANAGE_FC_BIN ));
1617
1624
1618
1625
if (sefcontext_compile (sh ,
1619
1626
semanage_final_path (SEMANAGE_FINAL_TMP , SEMANAGE_FC_LOCAL )) != 0 ) {
1620
1627
goto cleanup ;
1621
1628
}
1629
+ semanage_setfiles (semanage_final_path (SEMANAGE_FINAL_TMP , SEMANAGE_FC_LOCAL_BIN ));
1622
1630
1623
1631
if (sefcontext_compile (sh ,
1624
1632
semanage_final_path (SEMANAGE_FINAL_TMP , SEMANAGE_FC_HOMEDIRS )) != 0 ) {
1625
1633
goto cleanup ;
1626
1634
}
1635
+ semanage_setfiles (semanage_final_path (SEMANAGE_FINAL_TMP , SEMANAGE_FC_HOMEDIRS_BIN ));
1627
1636
1628
1637
status = 0 ;
1629
1638
cleanup :
@@ -3018,3 +3027,26 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
3018
3027
3019
3028
return 0 ;
3020
3029
}
3030
+
3031
+ /* Make sure the file context and ownership of files in the policy
3032
+ * store does not change */
3033
+ void semanage_setfiles (const char * path ){
3034
+ struct stat sb ;
3035
+ int fd ;
3036
+ /* Fix the user and role portions of the context, ignore errors
3037
+ * since this is not a critical operation */
3038
+ selinux_restorecon (path , SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY );
3039
+
3040
+ /* Make sure "path" is owned by root */
3041
+ if ((geteuid () != 0 || getegid () != 0 ) &&
3042
+ ((fd = open (path , O_RDONLY )) != -1 )){
3043
+ /* Skip files with the SUID or SGID bit set -- abuse protection */
3044
+ if ((fstat (fd , & sb ) != -1 ) &&
3045
+ !(S_ISREG (sb .st_mode ) &&
3046
+ (sb .st_mode & (S_ISUID | S_ISGID ))) &&
3047
+ (fchown (fd , 0 , 0 ) == -1 ))
3048
+ fprintf (stderr , "Warning! Could not set ownership of %s to root\n" , path );
3049
+
3050
+ close (fd );
3051
+ }
3052
+ }
0 commit comments