Skip to contentSkip to conversation

SElinux store file context and ownership change

RFC PATCH In review Vit Mojzis 3 participants 29 messages Jul 11, 2024 – Jul 29, 2024 SELinux security module

lore
Share & tools
lore

Vit Mojzis

author
#0 lore raw

Hello,
I'm trying to address a known "issue" where SELinux context of files in SELinux store gets changed on policy rebuild. This triggers some system verification tools and unnecessarily raises concerns in users. I created a patch using getfilecon and setfscreatecon, but am not sure if this is the best approach since it will not fix a context that has already been changed. Also, any files created as a result of execve need to be addressed separately (e.g. file_contexts.bin), maybe using selabel_lookup to get the proper label since that way sefcontext_compile does not need to know the path to the SELinux store (only to sandbox). I considered relabeling the whole sandbox before semanage_commit_sandbox, but that seems wasteful.

Then there is a related issue where the rebuild is performed as a
non-root user, causing files in the policy store to change ownership.
\# capsh --user=testuser --caps="cap_dac_override+eip cap_setpcap+ep" --addamb=cap_dac_override -- -c "semodule -B" This can actually cause issues in some scenarios (e.g. remote login failing).
Addressing this seems to require more drastic measures. My attempts to
use "chown" failed, even with the CAP_CHOWN capability and using
seteuid/setegid does not seem safe. Any suggestions would be
appreciated.

Thank you.

Vit

author
#1 lore raw

RFC PATCH 1/1 libsemanage: Do not change file context when copying files

Issue:

  # 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

---
 libsemanage/src/semanage_store.c | 24 +++++++++++++++++++++++-
 libsemanage/src/semanage_store.h |  1 +
 2 files changed, 24 insertions(+), 1 deletion(-)
libsemanage/src/semanage_store.c +23 -1
index 27c5d349..694c1519 100644
@@ -731,7 +731,9 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
 
 	if (!mode)
 		mode = S_IRUSR | S_IWUSR;
-	
+
+	semanage_setfscreatecon(src);
+
 	mask = umask(0);
 	if ((out = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, mode)) == -1) {
 		umask(mask);
@@ -812,6 +814,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
 	}
 
 	if (stat(dst, &sb) != 0) {
+		semanage_setfscreatecon(src);
 		mask = umask(0077);
 		if (mkdir(dst, S_IRWXU) != 0) {
 			umask(mask);
@@ -831,6 +834,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
 		snprintf(path2, sizeof(path2), "%s/%s", dst, names[i]->d_name);
 		if (S_ISDIR(sb.st_mode)) {
 			mask = umask(0077);
+			semanage_setfscreatecon(path);
 			if (mkdir(path2, 0700) == -1 ||
 			    semanage_copy_dir_flags(path, path2, flag) == -1) {
 				umask(mask);
@@ -952,6 +956,24 @@ cleanup:
 	return status;
 }
 
+/* Get file context of "path" and use it for the next file to be created.
+ * To be used for creating a sandbox labeled the same as the original SELinux store */
+int semanage_setfscreatecon(const char *path)
+{
+	int status = 0;
+	char *fcontext_raw = NULL;
+
+	if (getfilecon(path, &fcontext_raw) < 0){
+		fcontext_raw = NULL;
+	}
+
+	status = setfscreatecon_raw(fcontext_raw);
+
+	freecon(fcontext_raw);
+
+	return status;
+}
+
 /********************* sandbox management routines *********************/
 
 /* Creates a sandbox for a single client. Returns 0 if a
libsemanage/src/semanage_store.h +1 -0
index 1fc77da8..135e6ac6 100644
@@ -124,6 +124,7 @@ int semanage_get_cil_paths(semanage_handle_t * sh, semanage_module_info_t *modin
 int semanage_get_active_modules(semanage_handle_t *sh,
 			       semanage_module_info_t **modinfo, int *num_modules);
 
+int semanage_setfscreatecon(const char *path);
 
 /* lock file routines */
 int semanage_get_trans_lock(semanage_handle_t * sh);
Signature · 1 line
2.43.0
#2 lore raw

Re: [RFC PATCH] libsemanage: Do not change file context when copying files

Quoted 23 lines · Vit Mojzis

Issue:

  # semodule -B
  # ls -lZ  /etc/selinux/targeted/contexts/files
(quoted diff) +0 -4
-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
---
 libsemanage/src/semanage_store.c | 24 +++++++++++++++++++++++-
 libsemanage/src/semanage_store.h |  1 +
 2 files changed, 24 insertions(+), 1 deletion(-)
libsemanage/src/semanage_store.c +0 -0
index 27c5d349..694c1519 100644

libsemanage/src/semanage_store.c int semanage_copy_file(const char *src, const char *dst, mode_t mode, · line 738

Quoted 9 lines
libsemanage/src/semanage_store.c +3 -1
@@ -731,7 +731,9 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
 
        if (!mode)
                mode = S_IRUSR | S_IWUSR;
-
+
+       semanage_setfscreatecon(src);
+
        mask = umask(0);
        if ((out = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, mode)) == -1) {

The setfscreatecon context should be reset directly after the creating syscall (also in the failure case), e.g. via

void semanage_reset_setfscreatecon(void) {
    int saved_errno = errno;
    setfscreatecon_raw(NULL);
    errno = saved_errno;
}
Quoted 58 lines
libsemanage/src/semanage_store.c +20 -0
                umask(mask);
@@ -812,6 +814,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
        }
 
        if (stat(dst, &sb) != 0) {
+               semanage_setfscreatecon(src);
                mask = umask(0077);
                if (mkdir(dst, S_IRWXU) != 0) {
                        umask(mask);
@@ -831,6 +834,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
                snprintf(path2, sizeof(path2), "%s/%s", dst, names[i]->d_name);
                if (S_ISDIR(sb.st_mode)) {
                        mask = umask(0077);
+                       semanage_setfscreatecon(path);
                        if (mkdir(path2, 0700) == -1 ||
                            semanage_copy_dir_flags(path, path2, flag) == -1) {
                                umask(mask);
@@ -952,6 +956,24 @@ cleanup:
        return status;
 }
 
+/* Get file context of "path" and use it for the next file to be created.
+ * To be used for creating a sandbox labeled the same as the original SELinux store */
+int semanage_setfscreatecon(const char *path)
+{
+       int status = 0;
+       char *fcontext_raw = NULL;
+
+       if (getfilecon(path, &fcontext_raw) < 0){
+               fcontext_raw = NULL;
+       }
+
+       status = setfscreatecon_raw(fcontext_raw);
+
+       freecon(fcontext_raw);
+
+       return status;
+}
+
 /********************* sandbox management routines *********************/
 
 /* Creates a sandbox for a single client. Returns 0 if a
libsemanage/src/semanage_store.h +1 -1
index 1fc77da8..135e6ac6 100644
@@ -124,6 +124,7 @@ int semanage_get_cil_paths(semanage_handle_t * sh, semanage_module_info_t *modin
 int semanage_get_active_modules(semanage_handle_t *sh,
                               semanage_module_info_t **modinfo, int *num_modules);
 
+int semanage_setfscreatecon(const char *path);
 
 /* lock file routines */
 int semanage_get_trans_lock(semanage_handle_t * sh);
--

2.43.0

maintainer
#3 lore raw

Re: [RFC PATCH] libsemanage: Do not change file context when copying files

Quoted 38 lines · Christian Göttsche
Quoted 23 lines · Vit Mojzis

Issue:

  # semodule -B
  # ls -lZ  /etc/selinux/targeted/contexts/files
(quoted diff) +0 -4
-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
---
 libsemanage/src/semanage_store.c | 24 +++++++++++++++++++++++-
 libsemanage/src/semanage_store.h |  1 +
 2 files changed, 24 insertions(+), 1 deletion(-)
libsemanage/src/semanage_store.c +0 -0
index 27c5d349..694c1519 100644

libsemanage/src/semanage_store.c int semanage_copy_file(const char *src, const char *dst, mode_t mode, · line 738

Quoted 9 lines
libsemanage/src/semanage_store.c +3 -1
@@ -731,7 +731,9 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
 
        if (!mode)
                mode = S_IRUSR | S_IWUSR;
-
+
+       semanage_setfscreatecon(src);
+
        mask = umask(0);
        if ((out = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, mode)) == -1) {

The setfscreatecon context should be reset directly after the creating syscall (also in the failure case), e.g. via

Quoted 5 lines
void semanage_reset_setfscreatecon(void) {
    int saved_errno = errno;
    setfscreatecon_raw(NULL);
    errno = saved_errno;
}

Yes, agreed if you retain this approach instead of just using name-based type transitions to label these files automatically. Other options in addition to name-based type transitions:
- Call selinux_restorecon(3) after creating to label in accordance with file_contexts configuration, OR
- Use selabel_lookup(3) prior to creating to obtain the correct label rather than relying on the source label as authoritative.

maintainer
#4 lore raw
In reply to Vit Mojzis
Quoted 13 lines · Vit Mojzis

Hello,
I'm trying to address a known "issue" where SELinux context of files in SELinux store gets changed on policy rebuild. This triggers some system verification tools and unnecessarily raises concerns in users. I created a patch using getfilecon and setfscreatecon, but am not sure if this is the best approach since it will not fix a context that has already been changed. Also, any files created as a result of execve need to be addressed separately (e.g. file_contexts.bin), maybe using selabel_lookup to get the proper label since that way sefcontext_compile does not need to know the path to the SELinux store (only to sandbox). I considered relabeling the whole sandbox before semanage_commit_sandbox, but that seems wasteful.

Would a name-based type transition work? This would avoid the need for any code changes (to libsemanage or sefcontext_compile).

Otherwise, I think doing a selabel_lookup() followed by setfscreatecon() makes the most sense if you want to ensure that the file has the correct label at creation time, or calling selinux_restorecon(3) on the file after it is created prior to copying data to it would be even simpler.

Quoted 8 lines

Then there is a related issue where the rebuild is performed as a
non-root user, causing files in the policy store to change ownership.
\# capsh --user=testuser --caps="cap_dac_override+eip cap_setpcap+ep" --addamb=cap_dac_override -- -c "semodule -B" This can actually cause issues in some scenarios (e.g. remote login failing).
Addressing this seems to require more drastic measures. My attempts to
use "chown" failed, even with the CAP_CHOWN capability and using
seteuid/setegid does not seem safe. Any suggestions would be
appreciated.

setfsuid() is a safer option than seteuid() for filesystem operations, but what is preventing the chown() from working?

author
#5 lore raw
Quoted 15 lines · Stephen Smalley
Quoted 13 lines · Vit Mojzis

Hello,
I'm trying to address a known "issue" where SELinux context of files in SELinux store gets changed on policy rebuild. This triggers some system verification tools and unnecessarily raises concerns in users. I created a patch using getfilecon and setfscreatecon, but am not sure if this is the best approach since it will not fix a context that has already been changed. Also, any files created as a result of execve need to be addressed separately (e.g. file_contexts.bin), maybe using selabel_lookup to get the proper label since that way sefcontext_compile does not need to know the path to the SELinux store (only to sandbox). I considered relabeling the whole sandbox before semanage_commit_sandbox, but that seems wasteful.

Quoted 9 lines

Would a name-based type transition work? This would avoid the need for any code changes (to libsemanage or sefcontext_compile).

Otherwise, I think doing a selabel_lookup() followed by setfscreatecon() makes the most sense if you want to ensure that the file has the correct label at creation time, or calling selinux_restorecon(3) on the file after it is created prior to copying data to it would be even simpler.

Thank you for the suggestions. I haven't found a way to include the SELinux user and role in a type transition rule (and the type is not affected by this issue). But selinux_restorecon works just fine.

Quoted 11 lines
Quoted 8 lines

Then there is a related issue where the rebuild is performed as a
non-root user, causing files in the policy store to change ownership.
\# capsh --user=testuser --caps="cap_dac_override+eip cap_setpcap+ep" --addamb=cap_dac_override -- -c "semodule -B" This can actually cause issues in some scenarios (e.g. remote login failing).
Addressing this seems to require more drastic measures. My attempts to
use "chown" failed, even with the CAP_CHOWN capability and using
seteuid/setegid does not seem safe. Any suggestions would be
appreciated.

setfsuid() is a safer option than seteuid() for filesystem operations, but what is preventing the chown() from working?

Sorry, that was my bad. I didn't realize I had to also add dac_chown to "capsh --addamb". It works fine when I change the capabilities on the "semodule" binary, or use:

# sudo capsh --user=testuser --caps="cap_dac_override,cap_chown+eip" --addamb=cap_dac_override,cap_chown -- -c "semodule -B"

Thank you.

Vit

author
#6 lore raw

PATCH v2 1/1 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.

---
 libsemanage/src/semanage_store.c | 23 ++++++++++++++++++++++-
 libsemanage/src/semanage_store.h |  1 +
 2 files changed, 23 insertions(+), 1 deletion(-)
libsemanage/src/semanage_store.c +22 -1
index 27c5d349..12c30ad2 100644
@@ -36,6 +36,7 @@ typedef struct dbase_policydb dbase_t;
 #include "database_policydb.h"
 #include "handle.h"
 
+#include <selinux/restorecon.h>
 #include <selinux/selinux.h>
 #include <sepol/policydb.h>
 #include <sepol/module.h>
@@ -731,7 +732,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
 
 	if (!mode)
 		mode = S_IRUSR | S_IWUSR;
-	
+
 	mask = umask(0);
 	if ((out = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, mode)) == -1) {
 		umask(mask);
@@ -767,6 +768,8 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
 	if (!retval && rename(tmp, dst) == -1)
 		return -1;
 
+	semanage_setfiles(dst);
+
 out:
 	errno = errsv;
 	return retval;
@@ -819,6 +822,8 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
 			goto cleanup;
 		}
 		umask(mask);
+
+		semanage_setfiles(dst);
 	}
 
 	for (i = 0; i < len; i++) {
@@ -837,6 +842,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
 				goto cleanup;
 			}
 			umask(mask);
+			semanage_setfiles(path2);
 		} else if (S_ISREG(sb.st_mode) && flag == 1) {
 			mask = umask(0077);
 			if (semanage_copy_file(path, path2, sb.st_mode,
@@ -938,6 +944,7 @@ int semanage_mkdir(semanage_handle_t *sh, const char *path)
 
 		}
 		umask(mask);
+		semanage_setfiles(path);
 	}
 	else {
 		/* check that it really is a directory */
@@ -1614,16 +1621,19 @@ static int semanage_validate_and_compile_fcontexts(semanage_handle_t * sh)
 		    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC)) != 0) {
 		goto cleanup;
 	}
+	semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_BIN));
 
 	if (sefcontext_compile(sh,
 		    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL)) != 0) {
 		goto cleanup;
 	}
+	semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL_BIN));
 
 	if (sefcontext_compile(sh,
 		    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS)) != 0) {
 		goto cleanup;
 	}
+	semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS_BIN));
 
 	status = 0;
 cleanup:
@@ -3018,3 +3028,14 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
 	return 0;
 }
+
+/* Make sure the file context and ownership of files in the policy
+ * store does not change */
+void semanage_setfiles(const char *path){
+	/* Fix the user and role portions of the context, ignore errors
+	 * since this is not a critical operation */
+	selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+	/* Make sure "path" is owned by root */
+	if(geteuid() != 0 || getegid() != 0)
+		chown(path, 0, 0);
+}
libsemanage/src/semanage_store.h +1 -0
index 1fc77da8..e21dadeb 100644
@@ -124,6 +124,7 @@ int semanage_get_cil_paths(semanage_handle_t * sh, semanage_module_info_t *modin
 int semanage_get_active_modules(semanage_handle_t *sh,
 			       semanage_module_info_t **modinfo, int *num_modules);
 
+void semanage_setfiles(const char *path);
 
 /* lock file routines */
 int semanage_get_trans_lock(semanage_handle_t * sh);
Signature · 1 line
2.43.0
maintainer
#7 lore raw

Re: [PATCH v2] libsemanage: Preserve file context and ownership in policy store

Quoted 46 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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.
---
 libsemanage/src/semanage_store.c | 23 ++++++++++++++++++++++-
 libsemanage/src/semanage_store.h |  1 +
 2 files changed, 23 insertions(+), 1 deletion(-)
libsemanage/src/semanage_store.c +1 -0
index 27c5d349..12c30ad2 100644
@@ -36,6 +36,7 @@ typedef struct dbase_policydb dbase_t;
 #include "database_policydb.h"
 #include "handle.h"
 
+#include <selinux/restorecon.h>
 #include <selinux/selinux.h>
 #include <sepol/policydb.h>
 #include <sepol/module.h>

libsemanage/src/semanage_store.c int semanage_copy_file(const char *src, const char *dst, mode_t mode, · line 735

Quoted 5 lines
libsemanage/src/semanage_store.c +1 -1
@@ -731,7 +732,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
 
        if (!mode)
                mode = S_IRUSR | S_IWUSR;
-
+

We generally don't make unrelated whitespace changes in a patch.

Quoted 62 lines
libsemanage/src/semanage_store.c +10 -0
        mask = umask(0);
        if ((out = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, mode)) == -1) {
                umask(mask);
@@ -767,6 +768,8 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
        if (!retval && rename(tmp, dst) == -1)
                return -1;
 
+       semanage_setfiles(dst);
+
 out:
        errno = errsv;
        return retval;
@@ -819,6 +822,8 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
                        goto cleanup;
                }
                umask(mask);
+
+               semanage_setfiles(dst);
        }
 
        for (i = 0; i < len; i++) {
@@ -837,6 +842,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
                                goto cleanup;
                        }
                        umask(mask);
+                       semanage_setfiles(path2);
                } else if (S_ISREG(sb.st_mode) && flag == 1) {
                        mask = umask(0077);
                        if (semanage_copy_file(path, path2, sb.st_mode,
@@ -938,6 +944,7 @@ int semanage_mkdir(semanage_handle_t *sh, const char *path)
 
                }
                umask(mask);
+               semanage_setfiles(path);
        }
        else {
                /* check that it really is a directory */
@@ -1614,16 +1621,19 @@ static int semanage_validate_and_compile_fcontexts(semanage_handle_t * sh)
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_BIN));
 
        if (sefcontext_compile(sh,
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL_BIN));
 
        if (sefcontext_compile(sh,
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS_BIN));
 
        status = 0;
 cleanup:
@@ -3018,3 +3028,14 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
        return 0;
 }
+

libsemanage/src/semanage_store.c int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len, · line 3041

Quoted 10 lines
libsemanage/src/semanage_store.c +10 -0
+/* Make sure the file context and ownership of files in the policy
+ * store does not change */
+void semanage_setfiles(const char *path){
+       /* Fix the user and role portions of the context, ignore errors
+        * since this is not a critical operation */
+       selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+       /* Make sure "path" is owned by root */
+       if(geteuid() != 0 || getegid() != 0)
+               chown(path, 0, 0);
+}

Arguably should check stat.st_uid/gid from stat(2) of path although perhaps it doesn't matter.
Need to make sure that these paths only exist in root-owned directories and can't be used to trigger a chown of some other arbitrary file to root ownership, e.g. some suid binary. Maybe refuse to chown() if stat.st_mode & (S_IFREG|S_ISUID) || stat.st_mode & (S_IFREG|S_ISGID)?

Quoted 16 lines
libsemanage/src/semanage_store.h +1 -1
index 1fc77da8..e21dadeb 100644
@@ -124,6 +124,7 @@ int semanage_get_cil_paths(semanage_handle_t * sh, semanage_module_info_t *modin
 int semanage_get_active_modules(semanage_handle_t *sh,
                               semanage_module_info_t **modinfo, int *num_modules);
 
+void semanage_setfiles(const char *path);
 
 /* lock file routines */
 int semanage_get_trans_lock(semanage_handle_t * sh);
--

2.43.0

maintainer
#8 lore raw

Re: [PATCH v2] libsemanage: Preserve file context and ownership in policy store

Quoted 129 lines · Stephen Smalley
Quoted 46 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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.
---
 libsemanage/src/semanage_store.c | 23 ++++++++++++++++++++++-
 libsemanage/src/semanage_store.h |  1 +
 2 files changed, 23 insertions(+), 1 deletion(-)
libsemanage/src/semanage_store.c +1 -0
index 27c5d349..12c30ad2 100644
@@ -36,6 +36,7 @@ typedef struct dbase_policydb dbase_t;
 #include "database_policydb.h"
 #include "handle.h"
 
+#include <selinux/restorecon.h>
 #include <selinux/selinux.h>
 #include <sepol/policydb.h>
 #include <sepol/module.h>

libsemanage/src/semanage_store.c int semanage_copy_file(const char *src, const char *dst, mode_t mode, · line 735

Quoted 5 lines
libsemanage/src/semanage_store.c +1 -1
@@ -731,7 +732,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
 
        if (!mode)
                mode = S_IRUSR | S_IWUSR;
-
+

We generally don't make unrelated whitespace changes in a patch.

Quoted 62 lines
libsemanage/src/semanage_store.c +10 -0
        mask = umask(0);
        if ((out = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, mode)) == -1) {
                umask(mask);
@@ -767,6 +768,8 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
        if (!retval && rename(tmp, dst) == -1)
                return -1;
 
+       semanage_setfiles(dst);
+
 out:
        errno = errsv;
        return retval;
@@ -819,6 +822,8 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
                        goto cleanup;
                }
                umask(mask);
+
+               semanage_setfiles(dst);
        }
 
        for (i = 0; i < len; i++) {
@@ -837,6 +842,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
                                goto cleanup;
                        }
                        umask(mask);
+                       semanage_setfiles(path2);
                } else if (S_ISREG(sb.st_mode) && flag == 1) {
                        mask = umask(0077);
                        if (semanage_copy_file(path, path2, sb.st_mode,
@@ -938,6 +944,7 @@ int semanage_mkdir(semanage_handle_t *sh, const char *path)
 
                }
                umask(mask);
+               semanage_setfiles(path);
        }
        else {
                /* check that it really is a directory */
@@ -1614,16 +1621,19 @@ static int semanage_validate_and_compile_fcontexts(semanage_handle_t * sh)
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_BIN));
 
        if (sefcontext_compile(sh,
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL_BIN));
 
        if (sefcontext_compile(sh,
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS_BIN));
 
        status = 0;
 cleanup:
@@ -3018,3 +3028,14 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
        return 0;
 }
+

libsemanage/src/semanage_store.c int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len, · line 3041

Quoted 10 lines
libsemanage/src/semanage_store.c +10 -0
+/* Make sure the file context and ownership of files in the policy
+ * store does not change */
+void semanage_setfiles(const char *path){
+       /* Fix the user and role portions of the context, ignore errors
+        * since this is not a critical operation */
+       selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+       /* Make sure "path" is owned by root */
+       if(geteuid() != 0 || getegid() != 0)
+               chown(path, 0, 0);
+}
Quoted 7 lines

Arguably should check stat.st_uid/gid from stat(2) of path although perhaps it doesn't matter.
Need to make sure that these paths only exist in root-owned directories and can't be used to trigger a chown of some other arbitrary file to root ownership, e.g. some suid binary. Maybe refuse to chown() if stat.st_mode & (S_IFREG|S_ISUID) || stat.st_mode & (S_IFREG|S_ISGID)?

Sorry, I munged that - should be something like S_ISREG(sb.st_mode) && (sb.st_mode & (S_ISUID|S_ISGID)).
Obviously still subject to races unless you do something like fd = open(path, O_PATH); fstat(fd, &sb); <test sb.st_mode>; fchown(fd, 0, 0);

author
#9 lore raw

Re: [PATCH v2] libsemanage: Preserve file context and ownership in policy store

Quoted 114 lines · Stephen Smalley
Quoted 112 lines · Stephen Smalley
Quoted 45 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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.
---
  libsemanage/src/semanage_store.c | 23 ++++++++++++++++++++++-
  libsemanage/src/semanage_store.h |  1 +
  2 files changed, 23 insertions(+), 1 deletion(-)
libsemanage/src/semanage_store.c +1 -0
index 27c5d349..12c30ad2 100644
@@ -36,6 +36,7 @@ typedef struct dbase_policydb dbase_t;
  #include "database_policydb.h"
  #include "handle.h"
 
+#include <selinux/restorecon.h>
  #include <selinux/selinux.h>
  #include <sepol/policydb.h>
  #include <sepol/module.h>

libsemanage/src/semanage_store.c int semanage_copy_file(const char *src, const char *dst, mode_t mode, · line 735

Quoted 5 lines
libsemanage/src/semanage_store.c +1 -1
@@ -731,7 +732,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
 
         if (!mode)
                 mode = S_IRUSR | S_IWUSR;
-
+

We generally don't make unrelated whitespace changes in a patch.

Quoted 59 lines
libsemanage/src/semanage_store.c +9 -0
         mask = umask(0);
         if ((out = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, mode)) == -1) {
                 umask(mask);
@@ -767,6 +768,8 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
         if (!retval && rename(tmp, dst) == -1)
                 return -1;
 
+       semanage_setfiles(dst);
+
  out:
         errno = errsv;
         return retval;
@@ -819,6 +822,8 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
                         goto cleanup;
                 }
                 umask(mask);
+
+               semanage_setfiles(dst);
         }
 
         for (i = 0; i < len; i++) {
@@ -837,6 +842,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
                                 goto cleanup;
                         }
                         umask(mask);
+                       semanage_setfiles(path2);
                 } else if (S_ISREG(sb.st_mode) && flag == 1) {
                         mask = umask(0077);
                         if (semanage_copy_file(path, path2, sb.st_mode,
@@ -938,6 +944,7 @@ int semanage_mkdir(semanage_handle_t *sh, const char *path)
 
                 }
                 umask(mask);
+               semanage_setfiles(path);
         }
         else {
                 /* check that it really is a directory */
@@ -1614,16 +1621,19 @@ static int semanage_validate_and_compile_fcontexts(semanage_handle_t * sh)
                     semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC)) != 0) {
                 goto cleanup;
         }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_BIN));
 
         if (sefcontext_compile(sh,
                     semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL)) != 0) {
                 goto cleanup;
         }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL_BIN));
 
         if (sefcontext_compile(sh,
                     semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS)) != 0) {
                 goto cleanup;
         }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS_BIN));
 
         status = 0;
  cleanup:
@@ -3018,3 +3028,14 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
libsemanage/src/semanage_store.c +5 -0
         return 0;
  }
+
+/* Make sure the file context and ownership of files in the policy
+ * store does not change */
+void semanage_setfiles(const char *path){
+       /* Fix the user and role portions of the context, ignore errors
Quoted 8 lines

libsemanage/src/semanage_store.c int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len, · line 3041

Quoted 6 lines
libsemanage/src/semanage_store.c +6 -0
+        * since this is not a critical operation */
+       selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+       /* Make sure "path" is owned by root */
+       if(geteuid() != 0 || getegid() != 0)
+               chown(path, 0, 0);
+}

Arguably should check stat.st_uid/gid from stat(2) of path although perhaps it doesn't matter.

Did you mean so that chown is not used needlessly, or to protect from changing ownership of other users' files?

Quoted 5 lines
Quoted 5 lines

Need to make sure that these paths only exist in root-owned directories and can't be used to trigger a chown of some other arbitrary file to root ownership, e.g. some suid binary. Maybe refuse to chown() if stat.st_mode & (S_IFREG|S_ISUID) || stat.st_mode & (S_IFREG|S_ISGID)?

Right,  that seems like a good idea. Thank you.

Quoted 6 lines

Sorry, I munged that - should be something like S_ISREG(sb.st_mode) && (sb.st_mode & (S_ISUID|S_ISGID)).
Obviously still subject to races unless you do something like fd = open(path, O_PATH); fstat(fd, &sb); <test sb.st_mode>; fchown(fd, 0, 0);

author
#10 lore raw

PATCH v3 1/1 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.

---
 libsemanage/src/semanage_store.c | 28 ++++++++++++++++++++++++++++
 libsemanage/src/semanage_store.h |  1 +
 2 files changed, 29 insertions(+)
libsemanage/src/semanage_store.c +28 -0
index 27c5d349..a7dd1f6f 100644
@@ -36,6 +36,7 @@ typedef struct dbase_policydb dbase_t;
 #include "database_policydb.h"
 #include "handle.h"
 
+#include <selinux/restorecon.h>
 #include <selinux/selinux.h>
 #include <sepol/policydb.h>
 #include <sepol/module.h>
@@ -767,6 +768,8 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
 	if (!retval && rename(tmp, dst) == -1)
 		return -1;
 
+	semanage_setfiles(dst);
+
 out:
 	errno = errsv;
 	return retval;
@@ -819,6 +822,8 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
 			goto cleanup;
 		}
 		umask(mask);
+
+		semanage_setfiles(dst);
 	}
 
 	for (i = 0; i < len; i++) {
@@ -837,6 +842,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
 				goto cleanup;
 			}
 			umask(mask);
+			semanage_setfiles(path2);
 		} else if (S_ISREG(sb.st_mode) && flag == 1) {
 			mask = umask(0077);
 			if (semanage_copy_file(path, path2, sb.st_mode,
@@ -938,6 +944,7 @@ int semanage_mkdir(semanage_handle_t *sh, const char *path)
 
 		}
 		umask(mask);
+		semanage_setfiles(path);
 	}
 	else {
 		/* check that it really is a directory */
@@ -1614,16 +1621,19 @@ static int semanage_validate_and_compile_fcontexts(semanage_handle_t * sh)
 		    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC)) != 0) {
 		goto cleanup;
 	}
+	semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_BIN));
 
 	if (sefcontext_compile(sh,
 		    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL)) != 0) {
 		goto cleanup;
 	}
+	semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL_BIN));
 
 	if (sefcontext_compile(sh,
 		    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS)) != 0) {
 		goto cleanup;
 	}
+	semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS_BIN));
 
 	status = 0;
 cleanup:
@@ -3018,3 +3028,21 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
 	return 0;
 }
+
+/* Make sure the file context and ownership of files in the policy
+ * store does not change */
+void semanage_setfiles(const char *path){
+	struct stat sb;
+
+	/* Fix the user and role portions of the context, ignore errors
+	 * since this is not a critical operation */
+	selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+
+	/* Make sure "path" is owned by root */
+	if (geteuid() != 0 || getegid() != 0)
+		/* Skip files with the SUID or SGID bit set -- abuse protection */
+		if ((stat(path, &sb) == -1) ||
+		    (S_ISREG(sb.st_mode) && (sb.st_mode & (S_ISUID | S_ISGID))))
+				return;
+		chown(path, 0, 0);
+}
libsemanage/src/semanage_store.h +1 -0
index 1fc77da8..e21dadeb 100644
@@ -124,6 +124,7 @@ int semanage_get_cil_paths(semanage_handle_t * sh, semanage_module_info_t *modin
 int semanage_get_active_modules(semanage_handle_t *sh,
 			       semanage_module_info_t **modinfo, int *num_modules);
 
+void semanage_setfiles(const char *path);
 
 /* lock file routines */
 int semanage_get_trans_lock(semanage_handle_t * sh);
Signature · 1 line
2.43.0
maintainer
#11 lore raw

Re: [PATCH v3] libsemanage: Preserve file context and ownership in policy store

Quoted 42 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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.

---

libsemanage/src/semanage_store.c +8 -0
@@ -3018,3 +3028,21 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
        return 0;
 }
+
+/* Make sure the file context and ownership of files in the policy
+ * store does not change */
+void semanage_setfiles(const char *path){
+       struct stat sb;
+
+       /* Fix the user and role portions of the context, ignore errors
+        * since this is not a critical operation */

libsemanage/src/semanage_store.c int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len, · line 3048

Quoted 10 lines
libsemanage/src/semanage_store.c +10 -0
+       selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+
+       /* Make sure "path" is owned by root */
+       if (geteuid() != 0 || getegid() != 0)
+               /* Skip files with the SUID or SGID bit set -- abuse protection */
+               if ((stat(path, &sb) == -1) ||
+                   (S_ISREG(sb.st_mode) && (sb.st_mode & (S_ISUID | S_ISGID))))
+                               return;
+               chown(path, 0, 0);
+}

Did you consider the fd = open(path, O_PATH); fstat(fd, &sb); ... fchown(fd, 0, 0); pattern to avoid a race between the check and chown (e.g. link changed from one file to another in between)?

author
#12 lore raw

Re: [PATCH v3] libsemanage: Preserve file context and ownership in policy store

Quoted 46 lines · Stephen Smalley
Quoted 45 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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.

---

libsemanage/src/semanage_store.c +13 -0
@@ -3018,3 +3028,21 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
         return 0;
  }
+
+/* Make sure the file context and ownership of files in the policy
+ * store does not change */
+void semanage_setfiles(const char *path){
+       struct stat sb;
+
+       /* Fix the user and role portions of the context, ignore errors
+        * since this is not a critical operation */
+       selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+
+       /* Make sure "path" is owned by root */
+       if (geteuid() != 0 || getegid() != 0)
+               /* Skip files with the SUID or SGID bit set -- abuse protection */
Quoted 9 lines

libsemanage/src/semanage_store.c int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len, · line 3048

Quoted 5 lines
libsemanage/src/semanage_store.c +5 -0
+               if ((stat(path, &sb) == -1) ||
+                   (S_ISREG(sb.st_mode) && (sb.st_mode & (S_ISUID | S_ISGID))))
+                               return;
+               chown(path, 0, 0);
+}

Did you consider the fd = open(path, O_PATH); fstat(fd, &sb); ... fchown(fd, 0, 0); pattern to avoid a race between the check and chown (e.g. link changed from one file to another in between)?

Briefly, the code would be a bit less readable (interweaving writing file content and ownership/labeling) and we'd still need the current approach for any file created by another binary (e.g. sefcontext_compile). I'll rewrite it if you prefer that approach, but do you expect such races to be common? The whole ownership issue already seems like a corner-case.

Vit

maintainer
#13 lore raw

Re: [PATCH v3] libsemanage: Preserve file context and ownership in policy store

Quoted 60 lines · Vit Mojzis
Quoted 46 lines · Stephen Smalley
Quoted 45 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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.

---

libsemanage/src/semanage_store.c +13 -0
@@ -3018,3 +3028,21 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
         return 0;
  }
+
+/* Make sure the file context and ownership of files in the policy
+ * store does not change */
+void semanage_setfiles(const char *path){
+       struct stat sb;
+
+       /* Fix the user and role portions of the context, ignore errors
+        * since this is not a critical operation */
+       selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+
+       /* Make sure "path" is owned by root */
+       if (geteuid() != 0 || getegid() != 0)
+               /* Skip files with the SUID or SGID bit set -- abuse protection */
Quoted 9 lines

libsemanage/src/semanage_store.c int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len, · line 3048

Quoted 5 lines
libsemanage/src/semanage_store.c +5 -0
+               if ((stat(path, &sb) == -1) ||
+                   (S_ISREG(sb.st_mode) && (sb.st_mode & (S_ISUID | S_ISGID))))
+                               return;
+               chown(path, 0, 0);
+}

Did you consider the fd = open(path, O_PATH); fstat(fd, &sb); ... fchown(fd, 0, 0); pattern to avoid a race between the check and chown (e.g. link changed from one file to another in between)?

Quoted 3 lines

Briefly, the code would be a bit less readable (interweaving writing file content and ownership/labeling) and we'd still need the current approach for any file created by another binary (e.g. sefcontext_compile).

Not sure I understand that last part - why can't you do the same open(path, O_PATH); fstat(fd, &sb); ... fchown(fd, 0, 0); for files created by a helper program - just do it in the parent after the child exits?

Quoted 3 lines

I'll rewrite it if you prefer that approach, but do you expect such races to be common? The whole ownership issue already seems like a corner-case.

Shrug. That sort of race has been exploited in the past to relabel or chown a file of your choosing by switching out one symlink for another at the last minute but admittedly we are doing the restorecon by path so your code is consistent.
No big deal to me either way.

author
#14 lore raw

Re: [PATCH v3] libsemanage: Preserve file context and ownership in policy store

Quoted 59 lines · Stephen Smalley
Quoted 58 lines · Vit Mojzis
Quoted 55 lines · Stephen Smalley
Quoted 40 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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.

---

libsemanage/src/semanage_store.c +8 -0
@@ -3018,3 +3028,21 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
          return 0;
   }
+
+/* Make sure the file context and ownership of files in the policy
+ * store does not change */
+void semanage_setfiles(const char *path){
+       struct stat sb;
+
+       /* Fix the user and role portions of the context, ignore errors
+        * since this is not a critical operation */

libsemanage/src/semanage_store.c int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len, · line 3048

Quoted 10 lines
libsemanage/src/semanage_store.c +10 -0
+       selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+
+       /* Make sure "path" is owned by root */
+       if (geteuid() != 0 || getegid() != 0)
+               /* Skip files with the SUID or SGID bit set -- abuse protection */
+               if ((stat(path, &sb) == -1) ||
+                   (S_ISREG(sb.st_mode) && (sb.st_mode & (S_ISUID | S_ISGID))))
+                               return;
+               chown(path, 0, 0);
+}

Did you consider the fd = open(path, O_PATH); fstat(fd, &sb); ... fchown(fd, 0, 0); pattern to avoid a race between the check and chown (e.g. link changed from one file to another in between)?

Quoted 7 lines
Quoted 3 lines

Briefly, the code would be a bit less readable (interweaving writing file content and ownership/labeling) and we'd still need the current approach for any file created by another binary (e.g. sefcontext_compile).

Not sure I understand that last part - why can't you do the same open(path, O_PATH); fstat(fd, &sb); ... fchown(fd, 0, 0); for files created by a helper program - just do it in the parent after the child exits?

Sorry, I misunderstood. I thought you wanted to add the fchown into existing code that opens the file. I actually tried that in semanage_copy_file, but chown to root caused the rename to fail (and selinux_restorecon had to be changed to setfscreatecon because of permissions as well) so I had to go back to fixing everything after the rename.
Also, I had to switch O_PATH for O_RDONLY since O_PATH does not permit fchown.

Good catch with the curly braces after "if". What a rookie mistake.

Quoted 10 lines
Quoted 4 lines

I'll rewrite it if you prefer that approach, but do you expect such races to be common? The whole ownership issue already seems like a corner-case.

Shrug. That sort of race has been exploited in the past to relabel or chown a file of your choosing by switching out one symlink for another at the last minute but admittedly we are doing the restorecon by path so your code is consistent.
No big deal to me either way.

author
#15 lore raw

PATCH v4 1/1 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.

---
 libsemanage/src/semanage_store.c | 31 +++++++++++++++++++++++++++++++
 libsemanage/src/semanage_store.h |  1 +
 2 files changed, 32 insertions(+)
libsemanage/src/semanage_store.c +31 -0
index 27c5d349..95596a93 100644
@@ -36,6 +36,7 @@ typedef struct dbase_policydb dbase_t;
 #include "database_policydb.h"
 #include "handle.h"
 
+#include <selinux/restorecon.h>
 #include <selinux/selinux.h>
 #include <sepol/policydb.h>
 #include <sepol/module.h>
@@ -767,6 +768,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
 	if (!retval && rename(tmp, dst) == -1)
 		return -1;
 
+	semanage_setfiles(dst);
 out:
 	errno = errsv;
 	return retval;
@@ -819,6 +821,8 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
 			goto cleanup;
 		}
 		umask(mask);
+
+		semanage_setfiles(dst);
 	}
 
 	for (i = 0; i < len; i++) {
@@ -837,6 +841,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
 				goto cleanup;
 			}
 			umask(mask);
+			semanage_setfiles(path2);
 		} else if (S_ISREG(sb.st_mode) && flag == 1) {
 			mask = umask(0077);
 			if (semanage_copy_file(path, path2, sb.st_mode,
@@ -938,6 +943,7 @@ int semanage_mkdir(semanage_handle_t *sh, const char *path)
 
 		}
 		umask(mask);
+		semanage_setfiles(path);
 	}
 	else {
 		/* check that it really is a directory */
@@ -1614,16 +1620,19 @@ static int semanage_validate_and_compile_fcontexts(semanage_handle_t * sh)
 		    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC)) != 0) {
 		goto cleanup;
 	}
+	semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_BIN));
 
 	if (sefcontext_compile(sh,
 		    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL)) != 0) {
 		goto cleanup;
 	}
+	semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL_BIN));
 
 	if (sefcontext_compile(sh,
 		    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS)) != 0) {
 		goto cleanup;
 	}
+	semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS_BIN));
 
 	status = 0;
 cleanup:
@@ -3018,3 +3027,25 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
 	return 0;
 }
+
+/* Make sure the file context and ownership of files in the policy
+ * store does not change */
+void semanage_setfiles(const char *path){
+	struct stat sb;
+	int fd;
+	/* Fix the user and role portions of the context, ignore errors
+	 * since this is not a critical operation */
+	selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+
+	/* Make sure "path" is owned by root */
+	if ((geteuid() != 0 || getegid() != 0) &&
+	    ((fd = open(path, O_RDONLY)) != -1)){
+		/* Skip files with the SUID or SGID bit set -- abuse protection */
+		if ((fstat(fd, &sb) != -1) &&
+		    !(S_ISREG(sb.st_mode) &&
+		      (sb.st_mode & (S_ISUID | S_ISGID))))
+			fchown(fd, 0, 0);
+
+		close(fd);
+	}
+}
libsemanage/src/semanage_store.h +1 -0
index 1fc77da8..e21dadeb 100644
@@ -124,6 +124,7 @@ int semanage_get_cil_paths(semanage_handle_t * sh, semanage_module_info_t *modin
 int semanage_get_active_modules(semanage_handle_t *sh,
 			       semanage_module_info_t **modinfo, int *num_modules);
 
+void semanage_setfiles(const char *path);
 
 /* lock file routines */
 int semanage_get_trans_lock(semanage_handle_t * sh);
Signature · 1 line
2.43.0
maintainer
#16 lore raw

Re: [PATCH v4] libsemanage: Preserve file context and ownership in policy store

Quoted 38 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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.
---
 libsemanage/src/semanage_store.c | 31 +++++++++++++++++++++++++++++++
 libsemanage/src/semanage_store.h |  1 +
 2 files changed, 32 insertions(+)
libsemanage/src/semanage_store.c +0 -0
index 27c5d349..95596a93 100644

libsemanage/src/semanage_store.c typedef struct dbase_policydb dbase_t; · line 42

Quoted 7 lines
libsemanage/src/semanage_store.c +1 -0
@@ -36,6 +36,7 @@ typedef struct dbase_policydb dbase_t;
 #include "database_policydb.h"
 #include "handle.h"
 
+#include <selinux/restorecon.h>
 #include <selinux/selinux.h>
 #include <sepol/policydb.h>
 #include <sepol/module.h>

I get the following error when building:

$ make DESTDIR=~/obj install > make.out
In file included from semanage_store.c:39:
/home/sdsmall/obj/usr/include/selinux/restorecon.h:137:52: error:
‘struct selabel_handle’ declared inside parameter list will not be
visible outside of this definition or declaration [-Werror]
  137 | extern void selinux_restorecon_set_sehandle(struct
selabel_handle *hndl);
      |                                                    ^~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[2]: *** [Makefile:111: semanage_store.o] Error 1
make[1]: *** [Makefile:15: install] Error 2
make: *** [Makefile:40: install] Error 1

It appear that either we need to modify libselinux/include/selinux/restorecon.h to #include <selinux/handle.h> prior to using selabel_handle or to add it prior to including selinux/restorecon.h here. The first option seems correct to me.

author
#17 lore raw

PATCH 1/1 libselinux/restorecon: Include <selinux/label.h>

restorecon.h uses types defined in label.h, so it needs to include label.h (or code using restorecon.h also needs to include label.h, which is not practical).

Fixes:

  $ make DESTDIR=~/obj install > make.out
In file included from semanage_store.c:39:
/home/sdsmall/obj/usr/include/selinux/restorecon.h:137:52: error:
‘struct selabel_handle’ declared inside parameter list will not be
visible outside of this definition or declaration [-Werror]
  137 | extern void selinux_restorecon_set_sehandle(struct
selabel_handle *hndl);
      |                                                    ^~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[2]: *** [Makefile:111: semanage_store.o] Error 1
make[1]: *** [Makefile:15: install] Error 2
make: *** [Makefile:40: install] Error 1

---
Sorry, did not notice the error since for some reason fedora-rawhide build with the patch was successful:
https://copr.fedorainfracloud.org/coprs/vmojzis/userspace_test/build/7789598/
But I managed to reporoduce it on a fresh system with the command you provided.
I assume you meant label.h instead of handle.h.

 libselinux/include/selinux/restorecon.h | 2 ++
 1 file changed, 2 insertions(+)
libselinux/include/selinux/restorecon.h +2 -0
index b10fe684..5be6542c 100644
@@ -1,6 +1,8 @@
 #ifndef _RESTORECON_H_
 #define _RESTORECON_H_
 
+#include <selinux/label.h>
+
 #include <sys/types.h>
 #include <stddef.h>
 #include <stdarg.h>
Signature · 1 line
2.43.0
maintainer Acked-by on v1 1/1
#18 lore raw

Re: [PATCH] libselinux/restorecon: Include <selinux/label.h>

Quoted 19 lines · Vit Mojzis

restorecon.h uses types defined in label.h, so it needs to include label.h (or code using restorecon.h also needs to include label.h, which is not practical).

Fixes:

  $ make DESTDIR=~/obj install > make.out
In file included from semanage_store.c:39:
/home/sdsmall/obj/usr/include/selinux/restorecon.h:137:52: error:
‘struct selabel_handle’ declared inside parameter list will not be
visible outside of this definition or declaration [-Werror]
  137 | extern void selinux_restorecon_set_sehandle(struct
selabel_handle *hndl);
      |                                                    ^~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[2]: *** [Makefile:111: semanage_store.o] Error 1
make[1]: *** [Makefile:15: install] Error 2
make: *** [Makefile:40: install] Error 1
Quoted 1 line
Quoted 7 lines

---
Sorry, did not notice the error since for some reason fedora-rawhide build with the patch was successful:
https://copr.fedorainfracloud.org/coprs/vmojzis/userspace_test/build/7789598/
But I managed to reporoduce it on a fresh system with the command you provided.
I assume you meant label.h instead of handle.h.

Correct, thanks. Probably not a fatal error in the default build.

Quoted 20 lines
 libselinux/include/selinux/restorecon.h | 2 ++
 1 file changed, 2 insertions(+)
libselinux/include/selinux/restorecon.h +2 -1
index b10fe684..5be6542c 100644
@@ -1,6 +1,8 @@
 #ifndef _RESTORECON_H_
 #define _RESTORECON_H_
 
+#include <selinux/label.h>
+
 #include <sys/types.h>
 #include <stddef.h>
 #include <stdarg.h>
--

2.43.0

maintainer
#19 lore raw

Re: [PATCH] libselinux/restorecon: Include <selinux/label.h>

Quoted 21 lines · Stephen Smalley
Quoted 19 lines · Vit Mojzis

restorecon.h uses types defined in label.h, so it needs to include label.h (or code using restorecon.h also needs to include label.h, which is not practical).

Fixes:

  $ make DESTDIR=~/obj install > make.out
In file included from semanage_store.c:39:
/home/sdsmall/obj/usr/include/selinux/restorecon.h:137:52: error:
‘struct selabel_handle’ declared inside parameter list will not be
visible outside of this definition or declaration [-Werror]
  137 | extern void selinux_restorecon_set_sehandle(struct
selabel_handle *hndl);
      |                                                    ^~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[2]: *** [Makefile:111: semanage_store.o] Error 1
make[1]: *** [Makefile:15: install] Error 2
make: *** [Makefile:40: install] Error 1
Quoted 3 lines
Quoted 1 line

This patch has been applied to main.

maintainer Acked-by on v4 1/1
#20 lore raw

Re: [PATCH v4] libsemanage: Preserve file context and ownership in policy store

In reply to Vit Mojzis
Quoted 27 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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.
Quoted 1 line

With the selinux/restorecon.h fix applied first,

Quoted 112 lines
---
 libsemanage/src/semanage_store.c | 31 +++++++++++++++++++++++++++++++
 libsemanage/src/semanage_store.h |  1 +
 2 files changed, 32 insertions(+)
libsemanage/src/semanage_store.c +31 -0
index 27c5d349..95596a93 100644
@@ -36,6 +36,7 @@ typedef struct dbase_policydb dbase_t;
 #include "database_policydb.h"
 #include "handle.h"
 
+#include <selinux/restorecon.h>
 #include <selinux/selinux.h>
 #include <sepol/policydb.h>
 #include <sepol/module.h>
@@ -767,6 +768,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
        if (!retval && rename(tmp, dst) == -1)
                return -1;
 
+       semanage_setfiles(dst);
 out:
        errno = errsv;
        return retval;
@@ -819,6 +821,8 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
                        goto cleanup;
                }
                umask(mask);
+
+               semanage_setfiles(dst);
        }
 
        for (i = 0; i < len; i++) {
@@ -837,6 +841,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
                                goto cleanup;
                        }
                        umask(mask);
+                       semanage_setfiles(path2);
                } else if (S_ISREG(sb.st_mode) && flag == 1) {
                        mask = umask(0077);
                        if (semanage_copy_file(path, path2, sb.st_mode,
@@ -938,6 +943,7 @@ int semanage_mkdir(semanage_handle_t *sh, const char *path)
 
                }
                umask(mask);
+               semanage_setfiles(path);
        }
        else {
                /* check that it really is a directory */
@@ -1614,16 +1620,19 @@ static int semanage_validate_and_compile_fcontexts(semanage_handle_t * sh)
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_BIN));
 
        if (sefcontext_compile(sh,
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL_BIN));
 
        if (sefcontext_compile(sh,
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS_BIN));
 
        status = 0;
 cleanup:
@@ -3018,3 +3027,25 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
        return 0;
 }
+
+/* Make sure the file context and ownership of files in the policy
+ * store does not change */
+void semanage_setfiles(const char *path){
+       struct stat sb;
+       int fd;
+       /* Fix the user and role portions of the context, ignore errors
+        * since this is not a critical operation */
+       selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+
+       /* Make sure "path" is owned by root */
+       if ((geteuid() != 0 || getegid() != 0) &&
+           ((fd = open(path, O_RDONLY)) != -1)){
+               /* Skip files with the SUID or SGID bit set -- abuse protection */
+               if ((fstat(fd, &sb) != -1) &&
+                   !(S_ISREG(sb.st_mode) &&
+                     (sb.st_mode & (S_ISUID | S_ISGID))))
+                       fchown(fd, 0, 0);
+
+               close(fd);
+       }
+}
libsemanage/src/semanage_store.h +1 -1
index 1fc77da8..e21dadeb 100644
@@ -124,6 +124,7 @@ int semanage_get_cil_paths(semanage_handle_t * sh, semanage_module_info_t *modin
 int semanage_get_active_modules(semanage_handle_t *sh,
                               semanage_module_info_t **modinfo, int *num_modules);
 
+void semanage_setfiles(const char *path);
 
 /* lock file routines */
 int semanage_get_trans_lock(semanage_handle_t * sh);
--

2.43.0

maintainer
#21 lore raw

Re: [PATCH v4] libsemanage: Preserve file context and ownership in policy store

Quoted 26 lines · Stephen Smalley
Quoted 24 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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
Quoted 8 lines
Quoted 4 lines

Both file context and ownership changed -- causes remote login failures and other issues in some scenarios.

With the selinux/restorecon.h fix applied first,

Ah, spoke too soon. The GitHub CI testing failed with this:
semanage_store.c: In function ‘semanage_setfiles’:
520 semanage_store.c:3047:25: error: ignoring return value of ‘fchown’ declared with attribute ‘warn_unused_result’ [-Werror=unused-result] 521 3047 | fchown(fd, 0, 0);
522 | ^~~~~~~~~~~~~~~~

maintainer
#22 lore raw

Re: [PATCH v4] libsemanage: Preserve file context and ownership in policy store

Quoted 36 lines · Stephen Smalley
Quoted 33 lines · Stephen Smalley
Quoted 27 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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.
Quoted 1 line

With the selinux/restorecon.h fix applied first,

Quoted 8 lines
Quoted 1 line

Ah, spoke too soon. The GitHub CI testing failed with this:
semanage_store.c: In function ‘semanage_setfiles’:
520 semanage_store.c:3047:25: error: ignoring return value of ‘fchown’ declared with attribute ‘warn_unused_result’ [-Werror=unused-result] 521 3047 | fchown(fd, 0, 0);
522 | ^~~~~~~~~~~~~~~~

Unfortunately, you can't just mark this as intentional, as per
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509.
Casting to void doesn't eliminate the warning. Saving the return value but not using it somewhere triggers another warning.
Calling assert() if rc != 0 breaks make test in libsemanage. We don't have a handle here or in the immediate callers so can't just call ERR(). Changing it to:
rc = fchown(fd, 0, 0);
if (rc)
fprintf(stderr, "Warning! Could not set ownership of %s to root\n",path);
seems to solve the warning problem but is a bit ugly.

author
#23 lore raw

PATCH v5 1/1 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.

---
Changes in V5:
- Check return value of fchown and warn user if it fails

Should I start creating github pull requests for each patch to catch this type of issue in the future (so that I don't waste your time)?

 libsemanage/src/semanage_store.c | 32 ++++++++++++++++++++++++++++++++
 libsemanage/src/semanage_store.h |  1 +
 2 files changed, 33 insertions(+)
libsemanage/src/semanage_store.c +32 -0
index 27c5d349..0ac2e5b2 100644
@@ -36,6 +36,7 @@ typedef struct dbase_policydb dbase_t;
 #include "database_policydb.h"
 #include "handle.h"
 
+#include <selinux/restorecon.h>
 #include <selinux/selinux.h>
 #include <sepol/policydb.h>
 #include <sepol/module.h>
@@ -767,6 +768,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
 	if (!retval && rename(tmp, dst) == -1)
 		return -1;
 
+	semanage_setfiles(dst);
 out:
 	errno = errsv;
 	return retval;
@@ -819,6 +821,8 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
 			goto cleanup;
 		}
 		umask(mask);
+
+		semanage_setfiles(dst);
 	}
 
 	for (i = 0; i < len; i++) {
@@ -837,6 +841,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
 				goto cleanup;
 			}
 			umask(mask);
+			semanage_setfiles(path2);
 		} else if (S_ISREG(sb.st_mode) && flag == 1) {
 			mask = umask(0077);
 			if (semanage_copy_file(path, path2, sb.st_mode,
@@ -938,6 +943,7 @@ int semanage_mkdir(semanage_handle_t *sh, const char *path)
 
 		}
 		umask(mask);
+		semanage_setfiles(path);
 	}
 	else {
 		/* check that it really is a directory */
@@ -1614,16 +1620,19 @@ static int semanage_validate_and_compile_fcontexts(semanage_handle_t * sh)
 		    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC)) != 0) {
 		goto cleanup;
 	}
+	semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_BIN));
 
 	if (sefcontext_compile(sh,
 		    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL)) != 0) {
 		goto cleanup;
 	}
+	semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL_BIN));
 
 	if (sefcontext_compile(sh,
 		    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS)) != 0) {
 		goto cleanup;
 	}
+	semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS_BIN));
 
 	status = 0;
 cleanup:
@@ -3018,3 +3027,26 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
 	return 0;
 }
+
+/* Make sure the file context and ownership of files in the policy
+ * store does not change */
+void semanage_setfiles(const char *path){
+	struct stat sb;
+	int fd;
+	/* Fix the user and role portions of the context, ignore errors
+	 * since this is not a critical operation */
+	selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+
+	/* Make sure "path" is owned by root */
+	if ((geteuid() != 0 || getegid() != 0) &&
+	    ((fd = open(path, O_RDONLY)) != -1)){
+		/* Skip files with the SUID or SGID bit set -- abuse protection */
+		if ((fstat(fd, &sb) != -1) &&
+		    !(S_ISREG(sb.st_mode) &&
+		      (sb.st_mode & (S_ISUID | S_ISGID))) &&
+		    (fchown(fd, 0, 0) == -1))
+			fprintf(stderr, "Warning! Could not set ownership of %s to root\n", path);
+
+		close(fd);
+	}
+}
libsemanage/src/semanage_store.h +1 -0
index 1fc77da8..e21dadeb 100644
@@ -124,6 +124,7 @@ int semanage_get_cil_paths(semanage_handle_t * sh, semanage_module_info_t *modin
 int semanage_get_active_modules(semanage_handle_t *sh,
 			       semanage_module_info_t **modinfo, int *num_modules);
 
+void semanage_setfiles(const char *path);
 
 /* lock file routines */
 int semanage_get_trans_lock(semanage_handle_t * sh);
Signature · 1 line
2.43.0
maintainer Acked-by on v5 1/1
#24 lore raw

Re: [PATCH v5] libsemanage: Preserve file context and ownership in policy store

Quoted 27 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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.
Quoted 1 line
Quoted 6 lines

---
Changes in V5:
- Check return value of fchown and warn user if it fails

Should I start creating github pull requests for each patch to catch this type of issue in the future (so that I don't waste your time)?

You don't need to create a PR to trigger the GitHub CI testing; just push the change to a branch of your own fork of the selinux repo and it will run the tests for you.

Quoted 113 lines
 libsemanage/src/semanage_store.c | 32 ++++++++++++++++++++++++++++++++
 libsemanage/src/semanage_store.h |  1 +
 2 files changed, 33 insertions(+)
libsemanage/src/semanage_store.c +32 -0
index 27c5d349..0ac2e5b2 100644
@@ -36,6 +36,7 @@ typedef struct dbase_policydb dbase_t;
 #include "database_policydb.h"
 #include "handle.h"
 
+#include <selinux/restorecon.h>
 #include <selinux/selinux.h>
 #include <sepol/policydb.h>
 #include <sepol/module.h>
@@ -767,6 +768,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
        if (!retval && rename(tmp, dst) == -1)
                return -1;
 
+       semanage_setfiles(dst);
 out:
        errno = errsv;
        return retval;
@@ -819,6 +821,8 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
                        goto cleanup;
                }
                umask(mask);
+
+               semanage_setfiles(dst);
        }
 
        for (i = 0; i < len; i++) {
@@ -837,6 +841,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
                                goto cleanup;
                        }
                        umask(mask);
+                       semanage_setfiles(path2);
                } else if (S_ISREG(sb.st_mode) && flag == 1) {
                        mask = umask(0077);
                        if (semanage_copy_file(path, path2, sb.st_mode,
@@ -938,6 +943,7 @@ int semanage_mkdir(semanage_handle_t *sh, const char *path)
 
                }
                umask(mask);
+               semanage_setfiles(path);
        }
        else {
                /* check that it really is a directory */
@@ -1614,16 +1620,19 @@ static int semanage_validate_and_compile_fcontexts(semanage_handle_t * sh)
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_BIN));
 
        if (sefcontext_compile(sh,
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL_BIN));
 
        if (sefcontext_compile(sh,
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS_BIN));
 
        status = 0;
 cleanup:
@@ -3018,3 +3027,26 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
        return 0;
 }
+
+/* Make sure the file context and ownership of files in the policy
+ * store does not change */
+void semanage_setfiles(const char *path){
+       struct stat sb;
+       int fd;
+       /* Fix the user and role portions of the context, ignore errors
+        * since this is not a critical operation */
+       selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+
+       /* Make sure "path" is owned by root */
+       if ((geteuid() != 0 || getegid() != 0) &&
+           ((fd = open(path, O_RDONLY)) != -1)){
+               /* Skip files with the SUID or SGID bit set -- abuse protection */
+               if ((fstat(fd, &sb) != -1) &&
+                   !(S_ISREG(sb.st_mode) &&
+                     (sb.st_mode & (S_ISUID | S_ISGID))) &&
+                   (fchown(fd, 0, 0) == -1))
+                       fprintf(stderr, "Warning! Could not set ownership of %s to root\n", path);
+
+               close(fd);
+       }
+}
libsemanage/src/semanage_store.h +1 -1
index 1fc77da8..e21dadeb 100644
@@ -124,6 +124,7 @@ int semanage_get_cil_paths(semanage_handle_t * sh, semanage_module_info_t *modin
 int semanage_get_active_modules(semanage_handle_t *sh,
                               semanage_module_info_t **modinfo, int *num_modules);
 
+void semanage_setfiles(const char *path);
 
 /* lock file routines */
 int semanage_get_trans_lock(semanage_handle_t * sh);
--

2.43.0

maintainer
#25 lore raw

Re: [PATCH v5] libsemanage: Preserve file context and ownership in policy store

Quoted 37 lines · Stephen Smalley
Quoted 27 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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.
Quoted 1 line
Quoted 4 lines

---
Changes in V5:
- Check return value of fchown and warn user if it fails

Quoted 6 lines
Quoted 2 lines

Should I start creating github pull requests for each patch to catch this type of issue in the future (so that I don't waste your time)?

You don't need to create a PR to trigger the GitHub CI testing; just push the change to a branch of your own fork of the selinux repo and it will run the tests for you.

And this patch has also been applied to main. Thanks!

#26 lore raw

Re: [PATCH v5] libsemanage: Preserve file context and ownership in policy store

In reply to Vit Mojzis
Quoted 110 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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.

---
Changes in V5:
- Check return value of fchown and warn user if it fails

Should I start creating github pull requests for each patch to catch this type of issue in the future (so that I don't waste your time)?

 libsemanage/src/semanage_store.c | 32 ++++++++++++++++++++++++++++++++
 libsemanage/src/semanage_store.h |  1 +
 2 files changed, 33 insertions(+)
libsemanage/src/semanage_store.c +11 -0
index 27c5d349..0ac2e5b2 100644
@@ -36,6 +36,7 @@ typedef struct dbase_policydb dbase_t;
 #include "database_policydb.h"
 #include "handle.h"
 
+#include <selinux/restorecon.h>
 #include <selinux/selinux.h>
 #include <sepol/policydb.h>
 #include <sepol/module.h>
@@ -767,6 +768,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
        if (!retval && rename(tmp, dst) == -1)
                return -1;
 
+       semanage_setfiles(dst);
 out:
        errno = errsv;
        return retval;
@@ -819,6 +821,8 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
                        goto cleanup;
                }
                umask(mask);
+
+               semanage_setfiles(dst);
        }
 
        for (i = 0; i < len; i++) {
@@ -837,6 +841,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
                                goto cleanup;
                        }
                        umask(mask);
+                       semanage_setfiles(path2);
                } else if (S_ISREG(sb.st_mode) && flag == 1) {
                        mask = umask(0077);
                        if (semanage_copy_file(path, path2, sb.st_mode,
@@ -938,6 +943,7 @@ int semanage_mkdir(semanage_handle_t *sh, const char *path)
 
                }
                umask(mask);
+               semanage_setfiles(path);
        }
        else {
                /* check that it really is a directory */
@@ -1614,16 +1620,19 @@ static int semanage_validate_and_compile_fcontexts(semanage_handle_t * sh)
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_BIN));
 
        if (sefcontext_compile(sh,
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL_BIN));
 
        if (sefcontext_compile(sh,
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS_BIN));
 
        status = 0;
 cleanup:
@@ -3018,3 +3027,26 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
        return 0;
 }
+
+/* Make sure the file context and ownership of files in the policy

libsemanage/src/semanage_store.c int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len, · line 3041

Quoted 10 lines
libsemanage/src/semanage_store.c +10 -0
+ * store does not change */
+void semanage_setfiles(const char *path){
+       struct stat sb;
+       int fd;
+       /* Fix the user and role portions of the context, ignore errors
+        * since this is not a critical operation */
+       selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+
+       /* Make sure "path" is owned by root */
+       if ((geteuid() != 0 || getegid() != 0) &&

I currently do not understand this condition.
Doesn't it check that we run *not* as root (in which case fchown(2) will probably fail)?

Quoted 27 lines
libsemanage/src/semanage_store.c +11 -0
+           ((fd = open(path, O_RDONLY)) != -1)){
+               /* Skip files with the SUID or SGID bit set -- abuse protection */
+               if ((fstat(fd, &sb) != -1) &&
+                   !(S_ISREG(sb.st_mode) &&
+                     (sb.st_mode & (S_ISUID | S_ISGID))) &&
+                   (fchown(fd, 0, 0) == -1))
+                       fprintf(stderr, "Warning! Could not set ownership of %s to root\n", path);
+
+               close(fd);
+       }
+}
libsemanage/src/semanage_store.h +1 -1
index 1fc77da8..e21dadeb 100644
@@ -124,6 +124,7 @@ int semanage_get_cil_paths(semanage_handle_t * sh, semanage_module_info_t *modin
 int semanage_get_active_modules(semanage_handle_t *sh,
                               semanage_module_info_t **modinfo, int *num_modules);
 
+void semanage_setfiles(const char *path);
 
 /* lock file routines */
 int semanage_get_trans_lock(semanage_handle_t * sh);
--

2.43.0

maintainer
#27 lore raw

Re: [PATCH v5] libsemanage: Preserve file context and ownership in policy store

Quoted 118 lines · Christian Göttsche
Quoted 116 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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.

---
Changes in V5:
- Check return value of fchown and warn user if it fails

Should I start creating github pull requests for each patch to catch this type of issue in the future (so that I don't waste your time)?

 libsemanage/src/semanage_store.c | 32 ++++++++++++++++++++++++++++++++
 libsemanage/src/semanage_store.h |  1 +
 2 files changed, 33 insertions(+)
libsemanage/src/semanage_store.c +17 -0
index 27c5d349..0ac2e5b2 100644
@@ -36,6 +36,7 @@ typedef struct dbase_policydb dbase_t;
 #include "database_policydb.h"
 #include "handle.h"
 
+#include <selinux/restorecon.h>
 #include <selinux/selinux.h>
 #include <sepol/policydb.h>
 #include <sepol/module.h>
@@ -767,6 +768,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
        if (!retval && rename(tmp, dst) == -1)
                return -1;
 
+       semanage_setfiles(dst);
 out:
        errno = errsv;
        return retval;
@@ -819,6 +821,8 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
                        goto cleanup;
                }
                umask(mask);
+
+               semanage_setfiles(dst);
        }
 
        for (i = 0; i < len; i++) {
@@ -837,6 +841,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
                                goto cleanup;
                        }
                        umask(mask);
+                       semanage_setfiles(path2);
                } else if (S_ISREG(sb.st_mode) && flag == 1) {
                        mask = umask(0077);
                        if (semanage_copy_file(path, path2, sb.st_mode,
@@ -938,6 +943,7 @@ int semanage_mkdir(semanage_handle_t *sh, const char *path)
 
                }
                umask(mask);
+               semanage_setfiles(path);
        }
        else {
                /* check that it really is a directory */
@@ -1614,16 +1620,19 @@ static int semanage_validate_and_compile_fcontexts(semanage_handle_t * sh)
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_BIN));
 
        if (sefcontext_compile(sh,
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL_BIN));
 
        if (sefcontext_compile(sh,
                    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS)) != 0) {
                goto cleanup;
        }
+       semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS_BIN));
 
        status = 0;
 cleanup:
@@ -3018,3 +3027,26 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
        return 0;
 }
+
+/* Make sure the file context and ownership of files in the policy
+ * store does not change */
+void semanage_setfiles(const char *path){
+       struct stat sb;
+       int fd;
+       /* Fix the user and role portions of the context, ignore errors
+        * since this is not a critical operation */
Quoted 8 lines

libsemanage/src/semanage_store.c int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len, · line 3041

Quoted 4 lines
libsemanage/src/semanage_store.c +4 -0
+       selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+
+       /* Make sure "path" is owned by root */
+       if ((geteuid() != 0 || getegid() != 0) &&

I currently do not understand this condition.
Doesn't it check that we run *not* as root (in which case fchown(2) will probably fail)?

See the patch description. He is trying to run it with just capabilities and not as root.

maintainer
#28 lore raw

Re: [PATCH v3] libsemanage: Preserve file context and ownership in policy store

In reply to Vit Mojzis
Quoted 40 lines · Vit Mojzis

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
(quoted diff) +0 -8
-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.

---

libsemanage/src/semanage_store.c +7 -0
@@ -3018,3 +3028,21 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,
 
        return 0;
 }
+
+/* Make sure the file context and ownership of files in the policy
+ * store does not change */
+void semanage_setfiles(const char *path){
+       struct stat sb;
+
+       /* Fix the user and role portions of the context, ignore errors

libsemanage/src/semanage_store.c int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len, · line 3047

Quoted 10 lines
libsemanage/src/semanage_store.c +10 -0
+        * since this is not a critical operation */
+       selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);
+
+       /* Make sure "path" is owned by root */
+       if (geteuid() != 0 || getegid() != 0)
+               /* Skip files with the SUID or SGID bit set -- abuse protection */
+               if ((stat(path, &sb) == -1) ||
+                   (S_ISREG(sb.st_mode) && (sb.st_mode & (S_ISUID | S_ISGID))))
+                               return;
+               chown(path, 0, 0);

I think you meant to wrap the body of the above if statement with { } so that the chown() only executes if geteuid() != 0 or getegid() != 0 too.
Yes?
This isn't Python ;)

Quoted 1 line
libsemanage/src/semanage_store.c +1 -0
+}