Skip to content

Commit d96f27b

Browse files
vmojzisstephensmalley
authored andcommitted
libsemanage: Preserve file context and ownership in policy store
Make sure that file context (all parts) and ownership of files/directories in policy store does not change no matter which user and under which context executes policy rebuild. Fixes: # semodule -B # ls -lZ /etc/selinux/targeted/contexts/files -rw-r--r--. 1 root root unconfined_u:object_r:file_context_t:s0 421397 Jul 11 09:57 file_contexts -rw-r--r--. 1 root root unconfined_u:object_r:file_context_t:s0 593470 Jul 11 09:57 file_contexts.bin -rw-r--r--. 1 root root unconfined_u:object_r:file_context_t:s0 14704 Jul 11 09:57 file_contexts.homedirs -rw-r--r--. 1 root root unconfined_u:object_r:file_context_t:s0 20289 Jul 11 09:57 file_contexts.homedirs.bin SELinux user changed from system_u to the user used to execute semodule # capsh --user=testuser --caps="cap_dac_override,cap_chown+eip" --addamb=cap_dac_override,cap_chown -- -c "semodule -B" # ls -lZ /etc/selinux/targeted/contexts/files -rw-r--r--. 1 testuser testuser unconfined_u:object_r:file_context_t:s0 421397 Jul 19 09:10 file_contexts -rw-r--r--. 1 testuser testuser unconfined_u:object_r:file_context_t:s0 593470 Jul 19 09:10 file_contexts.bin -rw-r--r--. 1 testuser testuser unconfined_u:object_r:file_context_t:s0 14704 Jul 19 09:10 file_contexts.homedirs -rw-r--r--. 1 testuser testuser unconfined_u:object_r:file_context_t:s0 20289 Jul 19 09:10 file_contexts.homedirs.bin Both file context and ownership changed -- causes remote login failures and other issues in some scenarios. Signed-off-by: Vit Mojzis <[email protected]> Acked-by: Stephen Smalley <[email protected]>
1 parent 7974aea commit d96f27b

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

libsemanage/src/semanage_store.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ typedef struct dbase_policydb dbase_t;
3636
#include "database_policydb.h"
3737
#include "handle.h"
3838

39+
#include <selinux/restorecon.h>
3940
#include <selinux/selinux.h>
4041
#include <sepol/policydb.h>
4142
#include <sepol/module.h>
@@ -767,6 +768,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
767768
if (!retval && rename(tmp, dst) == -1)
768769
return -1;
769770

771+
semanage_setfiles(dst);
770772
out:
771773
errno = errsv;
772774
return retval;
@@ -819,6 +821,8 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
819821
goto cleanup;
820822
}
821823
umask(mask);
824+
825+
semanage_setfiles(dst);
822826
}
823827

824828
for (i = 0; i < len; i++) {
@@ -837,6 +841,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
837841
goto cleanup;
838842
}
839843
umask(mask);
844+
semanage_setfiles(path2);
840845
} else if (S_ISREG(sb.st_mode) && flag == 1) {
841846
mask = umask(0077);
842847
if (semanage_copy_file(path, path2, sb.st_mode,
@@ -938,6 +943,7 @@ int semanage_mkdir(semanage_handle_t *sh, const char *path)
938943

939944
}
940945
umask(mask);
946+
semanage_setfiles(path);
941947
}
942948
else {
943949
/* check that it really is a directory */
@@ -1614,16 +1620,19 @@ static int semanage_validate_and_compile_fcontexts(semanage_handle_t * sh)
16141620
semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC)) != 0) {
16151621
goto cleanup;
16161622
}
1623+
semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_BIN));
16171624

16181625
if (sefcontext_compile(sh,
16191626
semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL)) != 0) {
16201627
goto cleanup;
16211628
}
1629+
semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL_BIN));
16221630

16231631
if (sefcontext_compile(sh,
16241632
semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS)) != 0) {
16251633
goto cleanup;
16261634
}
1635+
semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS_BIN));
16271636

16281637
status = 0;
16291638
cleanup:
@@ -3018,3 +3027,26 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
30183027

30193028
return 0;
30203029
}
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+
}

libsemanage/src/semanage_store.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ int semanage_get_cil_paths(semanage_handle_t * sh, semanage_module_info_t *modin
124124
int semanage_get_active_modules(semanage_handle_t *sh,
125125
semanage_module_info_t **modinfo, int *num_modules);
126126

127+
void semanage_setfiles(const char *path);
127128

128129
/* lock file routines */
129130
int semanage_get_trans_lock(semanage_handle_t * sh);

0 commit comments

Comments
 (0)