Skip to contentSkip to conversation

selinux: split no transition execve check

RFC PATCH In review Christian Göttsche 3 participants 5 messages Jan 25, 2022 – Jul 22, 2023 SELinux security module

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 selinux: split no transition execve check

In case a setuid or setgid binary is mislabeled with a generic context, either via a policy mistake or a move by the distribution package, executing it will be checked by the file permission execute_no_trans on the generic file context (e.g. bin_t). The setuid(2)/setgid(2) syscall within will then be checked against the unchanged caller process context, which might have been granted the capability permission setuid/ setgid to initially drop privileges. To avoid that scenario split the execute_no_trans permission in case of a setuid/setgid binary into a new permission execute_sxid_no_trans.

For backward compatibility this behavior is contained in a new policy capability.

---
 security/selinux/hooks.c                   | 9 ++++++++-
 security/selinux/include/classmap.h        | 2 +-
 security/selinux/include/policycap.h       | 1 +
 security/selinux/include/policycap_names.h | 3 ++-
 security/selinux/include/security.h        | 8 ++++++++
 5 files changed, 20 insertions(+), 3 deletions(-)
security/selinux/hooks.c +8 -1
index 5b6895e4fc29..b825fee39a70 100644
@@ -2348,9 +2348,16 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
 	ad.u.file = bprm->file;
 
 	if (new_tsec->sid == old_tsec->sid) {
+		u32 perm;
+
+		if (selinux_policycap_execute_sxid_no_trans() && is_sxid(inode->i_mode))
+			perm = FILE__EXECUTE_SXID_NO_TRANS;
+		else
+			perm = FILE__EXECUTE_NO_TRANS;
+
 		rc = avc_has_perm(&selinux_state,
 				  old_tsec->sid, isec->sid,
-				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
+				  SECCLASS_FILE, perm, &ad);
 		if (rc)
 			return rc;
 	} else {
security/selinux/include/classmap.h +1 -1
index 35aac62a662e..53a1eeeb86fb 100644
@@ -65,7 +65,7 @@ struct security_class_mapping secclass_map[] = {
 	    "quotaget", "watch", NULL } },
 	{ "file",
 	  { COMMON_FILE_PERMS,
-	    "execute_no_trans", "entrypoint", NULL } },
+	    "execute_no_trans", "entrypoint", "execute_sxid_no_trans", NULL } },
 	{ "dir",
 	  { COMMON_FILE_PERMS, "add_name", "remove_name",
 	    "reparent", "search", "rmdir", NULL } },
security/selinux/include/policycap.h +1 -0
index 2ec038efbb03..23929dc3e1db 100644
@@ -11,6 +11,7 @@ enum {
 	POLICYDB_CAPABILITY_CGROUPSECLABEL,
 	POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION,
 	POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS,
+	POLICYDB_CAPABILITY_EXECUTE_SXID_NO_TRANS,
 	__POLICYDB_CAPABILITY_MAX
 };
 #define POLICYDB_CAPABILITY_MAX (__POLICYDB_CAPABILITY_MAX - 1)
security/selinux/include/policycap_names.h +2 -1
index b89289f092c9..4c014c2cf352 100644
@@ -12,7 +12,8 @@ const char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX] = {
 	"always_check_network",
 	"cgroup_seclabel",
 	"nnp_nosuid_transition",
-	"genfs_seclabel_symlinks"
+	"genfs_seclabel_symlinks",
+	"execute_sxid_no_trans",
 };
 
 #endif /* _SELINUX_POLICYCAP_NAMES_H_ */
security/selinux/include/security.h +8 -0
index ac0ece01305a..ab95241b6b7b 100644
@@ -219,6 +219,14 @@ static inline bool selinux_policycap_genfs_seclabel_symlinks(void)
 	return READ_ONCE(state->policycap[POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS]);
 }
 
+static inline bool selinux_policycap_execute_sxid_no_trans(void)
+{
+	struct selinux_state *state = &selinux_state;
+
+	return READ_ONCE(state->policycap[POLICYDB_CAPABILITY_EXECUTE_SXID_NO_TRANS]);
+}
+
+
 struct selinux_policy_convert_data;
 
 struct selinux_load_state {
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 14 lines · Christian Göttsche

In case a setuid or setgid binary is mislabeled with a generic context, either via a policy mistake or a move by the distribution package, executing it will be checked by the file permission execute_no_trans on the generic file context (e.g. bin_t). The setuid(2)/setgid(2) syscall within will then be checked against the unchanged caller process context, which might have been granted the capability permission setuid/ setgid to initially drop privileges. To avoid that scenario split the execute_no_trans permission in case of a setuid/setgid binary into a new permission execute_sxid_no_trans.

For backward compatibility this behavior is contained in a new policy capability.

Quoted 8 lines
---
 security/selinux/hooks.c                   | 9 ++++++++-
 security/selinux/include/classmap.h        | 2 +-
 security/selinux/include/policycap.h       | 1 +
 security/selinux/include/policycap_names.h | 3 ++-
 security/selinux/include/security.h        | 8 ++++++++
 5 files changed, 20 insertions(+), 3 deletions(-)

Adding the refpolicy list to this thread as their opinion seems particularly relevant to this discussion.

FWIW, this looks reasonable to me but I would like to hear what others have to say.

Quoted 83 lines
security/selinux/hooks.c +8 -1
index 5b6895e4fc29..b825fee39a70 100644
@@ -2348,9 +2348,16 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
        ad.u.file = bprm->file;
 
        if (new_tsec->sid == old_tsec->sid) {
+               u32 perm;
+
+               if (selinux_policycap_execute_sxid_no_trans() && is_sxid(inode->i_mode))
+                       perm = FILE__EXECUTE_SXID_NO_TRANS;
+               else
+                       perm = FILE__EXECUTE_NO_TRANS;
+
                rc = avc_has_perm(&selinux_state,
                                  old_tsec->sid, isec->sid,
-                                 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
+                                 SECCLASS_FILE, perm, &ad);
                if (rc)
                        return rc;
        } else {
security/selinux/include/classmap.h +1 -1
index 35aac62a662e..53a1eeeb86fb 100644
@@ -65,7 +65,7 @@ struct security_class_mapping secclass_map[] = {
            "quotaget", "watch", NULL } },
        { "file",
          { COMMON_FILE_PERMS,
-           "execute_no_trans", "entrypoint", NULL } },
+           "execute_no_trans", "entrypoint", "execute_sxid_no_trans", NULL } },
        { "dir",
          { COMMON_FILE_PERMS, "add_name", "remove_name",
            "reparent", "search", "rmdir", NULL } },
security/selinux/include/policycap.h +1 -0
index 2ec038efbb03..23929dc3e1db 100644
@@ -11,6 +11,7 @@ enum {
        POLICYDB_CAPABILITY_CGROUPSECLABEL,
        POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION,
        POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS,
+       POLICYDB_CAPABILITY_EXECUTE_SXID_NO_TRANS,
        __POLICYDB_CAPABILITY_MAX
 };
 #define POLICYDB_CAPABILITY_MAX (__POLICYDB_CAPABILITY_MAX - 1)
security/selinux/include/policycap_names.h +2 -1
index b89289f092c9..4c014c2cf352 100644
@@ -12,7 +12,8 @@ const char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX] = {
        "always_check_network",
        "cgroup_seclabel",
        "nnp_nosuid_transition",
-       "genfs_seclabel_symlinks"
+       "genfs_seclabel_symlinks",
+       "execute_sxid_no_trans",
 };
 
 #endif /* _SELINUX_POLICYCAP_NAMES_H_ */
security/selinux/include/security.h +8 -1
index ac0ece01305a..ab95241b6b7b 100644
@@ -219,6 +219,14 @@ static inline bool selinux_policycap_genfs_seclabel_symlinks(void)
        return READ_ONCE(state->policycap[POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS]);
 }
 
+static inline bool selinux_policycap_execute_sxid_no_trans(void)
+{
+       struct selinux_state *state = &selinux_state;
+
+       return READ_ONCE(state->policycap[POLICYDB_CAPABILITY_EXECUTE_SXID_NO_TRANS]);
+}
+
+
 struct selinux_policy_convert_data;
 
 struct selinux_load_state {
--

2.34.1

Signature · 1 line
paul-moore.com
#2 lore raw
Quoted 25 lines · Paul Moore
Quoted 14 lines · Christian Göttsche

In case a setuid or setgid binary is mislabeled with a generic context, either via a policy mistake or a move by the distribution package, executing it will be checked by the file permission execute_no_trans on the generic file context (e.g. bin_t). The setuid(2)/setgid(2) syscall within will then be checked against the unchanged caller process context, which might have been granted the capability permission setuid/ setgid to initially drop privileges. To avoid that scenario split the execute_no_trans permission in case of a setuid/setgid binary into a new permission execute_sxid_no_trans.

For backward compatibility this behavior is contained in a new policy capability.

Quoted 8 lines
---
  security/selinux/hooks.c                   | 9 ++++++++-
  security/selinux/include/classmap.h        | 2 +-
  security/selinux/include/policycap.h       | 1 +
  security/selinux/include/policycap_names.h | 3 ++-
  security/selinux/include/security.h        | 8 ++++++++
  5 files changed, 20 insertions(+), 3 deletions(-)
Quoted 5 lines

Adding the refpolicy list to this thread as their opinion seems particularly relevant to this discussion.

FWIW, this looks reasonable to me but I would like to hear what others have to say.

I think this a band-aid to cover up the real problem, which is the mislabeled files.

Quoted 85 lines
Quoted 83 lines
security/selinux/hooks.c +8 -1
index 5b6895e4fc29..b825fee39a70 100644
@@ -2348,9 +2348,16 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
         ad.u.file = bprm->file;
 
         if (new_tsec->sid == old_tsec->sid) {
+               u32 perm;
+
+               if (selinux_policycap_execute_sxid_no_trans() && is_sxid(inode->i_mode))
+                       perm = FILE__EXECUTE_SXID_NO_TRANS;
+               else
+                       perm = FILE__EXECUTE_NO_TRANS;
+
                 rc = avc_has_perm(&selinux_state,
                                   old_tsec->sid, isec->sid,
-                                 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
+                                 SECCLASS_FILE, perm, &ad);
                 if (rc)
                         return rc;
         } else {
security/selinux/include/classmap.h +1 -1
index 35aac62a662e..53a1eeeb86fb 100644
@@ -65,7 +65,7 @@ struct security_class_mapping secclass_map[] = {
             "quotaget", "watch", NULL } },
         { "file",
           { COMMON_FILE_PERMS,
-           "execute_no_trans", "entrypoint", NULL } },
+           "execute_no_trans", "entrypoint", "execute_sxid_no_trans", NULL } },
         { "dir",
           { COMMON_FILE_PERMS, "add_name", "remove_name",
             "reparent", "search", "rmdir", NULL } },
security/selinux/include/policycap.h +1 -0
index 2ec038efbb03..23929dc3e1db 100644
@@ -11,6 +11,7 @@ enum {
         POLICYDB_CAPABILITY_CGROUPSECLABEL,
         POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION,
         POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS,
+       POLICYDB_CAPABILITY_EXECUTE_SXID_NO_TRANS,
         __POLICYDB_CAPABILITY_MAX
  };
  #define POLICYDB_CAPABILITY_MAX (__POLICYDB_CAPABILITY_MAX - 1)
security/selinux/include/policycap_names.h +2 -1
index b89289f092c9..4c014c2cf352 100644
@@ -12,7 +12,8 @@ const char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX] = {
         "always_check_network",
         "cgroup_seclabel",
         "nnp_nosuid_transition",
-       "genfs_seclabel_symlinks"
+       "genfs_seclabel_symlinks",
+       "execute_sxid_no_trans",
  };
 
  #endif /* _SELINUX_POLICYCAP_NAMES_H_ */
security/selinux/include/security.h +8 -1
index ac0ece01305a..ab95241b6b7b 100644
@@ -219,6 +219,14 @@ static inline bool selinux_policycap_genfs_seclabel_symlinks(void)
         return READ_ONCE(state->policycap[POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS]);
  }
 
+static inline bool selinux_policycap_execute_sxid_no_trans(void)
+{
+       struct selinux_state *state = &selinux_state;
+
+       return READ_ONCE(state->policycap[POLICYDB_CAPABILITY_EXECUTE_SXID_NO_TRANS]);
+}
+
+
  struct selinux_policy_convert_data;
 
  struct selinux_load_state {
--

2.34.1

Signature · 1 line
Chris PeBenito
maintainer
#3 lore raw
Quoted 26 lines · Chris PeBenito
Quoted 25 lines · Paul Moore
Quoted 22 lines · Christian Göttsche

In case a setuid or setgid binary is mislabeled with a generic context, either via a policy mistake or a move by the distribution package, executing it will be checked by the file permission execute_no_trans on the generic file context (e.g. bin_t). The setuid(2)/setgid(2) syscall within will then be checked against the unchanged caller process context, which might have been granted the capability permission setuid/ setgid to initially drop privileges. To avoid that scenario split the execute_no_trans permission in case of a setuid/setgid binary into a new permission execute_sxid_no_trans.

For backward compatibility this behavior is contained in a new policy capability.

---
  security/selinux/hooks.c                   | 9 ++++++++-
  security/selinux/include/classmap.h        | 2 +-
  security/selinux/include/policycap.h       | 1 +
  security/selinux/include/policycap_names.h | 3 ++-
  security/selinux/include/security.h        | 8 ++++++++
  5 files changed, 20 insertions(+), 3 deletions(-)
Quoted 7 lines
Quoted 5 lines

Adding the refpolicy list to this thread as their opinion seems particularly relevant to this discussion.

FWIW, this looks reasonable to me but I would like to hear what others have to say.

I think this a band-aid to cover up the real problem, which is the mislabeled files.

It's hard to disagree with that, and the kernel is probably the wrong place to apply a band-aid unless it is the only option left.

Quoted 89 lines
Quoted 79 lines
Quoted 79 lines
security/selinux/hooks.c +8 -1
index 5b6895e4fc29..b825fee39a70 100644
@@ -2348,9 +2348,16 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
         ad.u.file = bprm->file;
 
         if (new_tsec->sid == old_tsec->sid) {
+               u32 perm;
+
+               if (selinux_policycap_execute_sxid_no_trans() && is_sxid(inode->i_mode))
+                       perm = FILE__EXECUTE_SXID_NO_TRANS;
+               else
+                       perm = FILE__EXECUTE_NO_TRANS;
+
                 rc = avc_has_perm(&selinux_state,
                                   old_tsec->sid, isec->sid,
-                                 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
+                                 SECCLASS_FILE, perm, &ad);
                 if (rc)
                         return rc;
         } else {
security/selinux/include/classmap.h +1 -1
index 35aac62a662e..53a1eeeb86fb 100644
@@ -65,7 +65,7 @@ struct security_class_mapping secclass_map[] = {
             "quotaget", "watch", NULL } },
         { "file",
           { COMMON_FILE_PERMS,
-           "execute_no_trans", "entrypoint", NULL } },
+           "execute_no_trans", "entrypoint", "execute_sxid_no_trans", NULL } },
         { "dir",
           { COMMON_FILE_PERMS, "add_name", "remove_name",
             "reparent", "search", "rmdir", NULL } },
security/selinux/include/policycap.h +1 -0
index 2ec038efbb03..23929dc3e1db 100644
@@ -11,6 +11,7 @@ enum {
         POLICYDB_CAPABILITY_CGROUPSECLABEL,
         POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION,
         POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS,
+       POLICYDB_CAPABILITY_EXECUTE_SXID_NO_TRANS,
         __POLICYDB_CAPABILITY_MAX
  };
  #define POLICYDB_CAPABILITY_MAX (__POLICYDB_CAPABILITY_MAX - 1)
security/selinux/include/policycap_names.h +2 -1
index b89289f092c9..4c014c2cf352 100644
@@ -12,7 +12,8 @@ const char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX] = {
         "always_check_network",
         "cgroup_seclabel",
         "nnp_nosuid_transition",
-       "genfs_seclabel_symlinks"
+       "genfs_seclabel_symlinks",
+       "execute_sxid_no_trans",
  };
 
  #endif /* _SELINUX_POLICYCAP_NAMES_H_ */
security/selinux/include/security.h +8 -0
index ac0ece01305a..ab95241b6b7b 100644
@@ -219,6 +219,14 @@ static inline bool selinux_policycap_genfs_seclabel_symlinks(void)
         return READ_ONCE(state->policycap[POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS]);
  }
 
+static inline bool selinux_policycap_execute_sxid_no_trans(void)
+{
+       struct selinux_state *state = &selinux_state;
+
+       return READ_ONCE(state->policycap[POLICYDB_CAPABILITY_EXECUTE_SXID_NO_TRANS]);
+}
+
+
  struct selinux_policy_convert_data;
 
Quoted 6 lines
Quoted 4 lines
security/selinux/include/security.h +0 -1
  struct selinux_load_state {
--

2.34.1

--
Chris PeBenito

Signature · 1 line
paul-moore.com
#4 lore raw
Quoted 31 lines · Paul Moore
Quoted 29 lines · Chris PeBenito
Quoted 28 lines · Paul Moore
Quoted 14 lines · Christian Göttsche

In case a setuid or setgid binary is mislabeled with a generic context, either via a policy mistake or a move by the distribution package, executing it will be checked by the file permission execute_no_trans on the generic file context (e.g. bin_t). The setuid(2)/setgid(2) syscall within will then be checked against the unchanged caller process context, which might have been granted the capability permission setuid/ setgid to initially drop privileges. To avoid that scenario split the execute_no_trans permission in case of a setuid/setgid binary into a new permission execute_sxid_no_trans.

For backward compatibility this behavior is contained in a new policy capability.

Quoted 8 lines
---
  security/selinux/hooks.c                   | 9 ++++++++-
  security/selinux/include/classmap.h        | 2 +-
  security/selinux/include/policycap.h       | 1 +
  security/selinux/include/policycap_names.h | 3 ++-
  security/selinux/include/security.h        | 8 ++++++++
  5 files changed, 20 insertions(+), 3 deletions(-)

Adding the refpolicy list to this thread as their opinion seems particularly relevant to this discussion.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

FWIW, this looks reasonable to me but I would like to hear what others have to say.

I think this a band-aid to cover up the real problem, which is the mislabeled files.

It's hard to disagree with that, and the kernel is probably the wrong place to apply a band-aid unless it is the only option left.

Adding a new datapoint to this RFC:

An unprivileged user can via the setuid binary newgrp(1) write arbitrary content to files he can open in write mode but not actually write to, e.g. /proc/sys/kernel/ns_last_pid [1].
This also is reproducible on Fedora where /usr/bin/newgrp has the generic context bin_t, so the write (and capable check) happens in the caller context of unconfined_t.

With the proposed permission split applied and instead of granting unconfined_t the new permission execute_sxid_no_trans but relying on an intermediate template domain $1_newgrp_t the access would have been denied, due to lack of permissions of the templated newgrp domain. With a generic file type of bin_t newgrp would not be executable for callers otherwise. Most setuid binaries are already labeled with a private type, newgrp and pkexec are the only ones left on a head-less Fedora system.
One could still argue this is a policy neglect, but normally policy neglects result in missing permissions and reduced functionality, not in unintended privilege escalation.

[1]: https://github.com/shadow-maint/shadow/pull/758

Quoted 94 lines
Quoted 87 lines
Quoted 79 lines
Quoted 79 lines
security/selinux/hooks.c +8 -1
index 5b6895e4fc29..b825fee39a70 100644
@@ -2348,9 +2348,16 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
         ad.u.file = bprm->file;
 
         if (new_tsec->sid == old_tsec->sid) {
+               u32 perm;
+
+               if (selinux_policycap_execute_sxid_no_trans() && is_sxid(inode->i_mode))
+                       perm = FILE__EXECUTE_SXID_NO_TRANS;
+               else
+                       perm = FILE__EXECUTE_NO_TRANS;
+
                 rc = avc_has_perm(&selinux_state,
                                   old_tsec->sid, isec->sid,
-                                 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
+                                 SECCLASS_FILE, perm, &ad);
                 if (rc)
                         return rc;
         } else {
security/selinux/include/classmap.h +1 -1
index 35aac62a662e..53a1eeeb86fb 100644
@@ -65,7 +65,7 @@ struct security_class_mapping secclass_map[] = {
             "quotaget", "watch", NULL } },
         { "file",
           { COMMON_FILE_PERMS,
-           "execute_no_trans", "entrypoint", NULL } },
+           "execute_no_trans", "entrypoint", "execute_sxid_no_trans", NULL } },
         { "dir",
           { COMMON_FILE_PERMS, "add_name", "remove_name",
             "reparent", "search", "rmdir", NULL } },
security/selinux/include/policycap.h +1 -0
index 2ec038efbb03..23929dc3e1db 100644
@@ -11,6 +11,7 @@ enum {
         POLICYDB_CAPABILITY_CGROUPSECLABEL,
         POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION,
         POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS,
+       POLICYDB_CAPABILITY_EXECUTE_SXID_NO_TRANS,
         __POLICYDB_CAPABILITY_MAX
  };
  #define POLICYDB_CAPABILITY_MAX (__POLICYDB_CAPABILITY_MAX - 1)
security/selinux/include/policycap_names.h +2 -1
index b89289f092c9..4c014c2cf352 100644
@@ -12,7 +12,8 @@ const char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX] = {
         "always_check_network",
         "cgroup_seclabel",
         "nnp_nosuid_transition",
-       "genfs_seclabel_symlinks"
+       "genfs_seclabel_symlinks",
+       "execute_sxid_no_trans",
  };
 
  #endif /* _SELINUX_POLICYCAP_NAMES_H_ */
security/selinux/include/security.h +8 -0
index ac0ece01305a..ab95241b6b7b 100644
@@ -219,6 +219,14 @@ static inline bool selinux_policycap_genfs_seclabel_symlinks(void)
         return READ_ONCE(state->policycap[POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS]);
  }
 
+static inline bool selinux_policycap_execute_sxid_no_trans(void)
+{
+       struct selinux_state *state = &selinux_state;
+
+       return READ_ONCE(state->policycap[POLICYDB_CAPABILITY_EXECUTE_SXID_NO_TRANS]);
+}
+
+
  struct selinux_policy_convert_data;
 
Quoted 6 lines
Quoted 4 lines
security/selinux/include/security.h +0 -1
  struct selinux_load_state {
--

2.34.1

Quoted 2 lines

--
Chris PeBenito

--
paul-moore.com