Skip to contentSkip to conversation

selinux: fix security context comparison on execve(2)

PATCH In review Ondrej Mosnacek 3 participants 4 messages Jun 9, 2025 – Jun 10, 2025 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selinux: fix security context comparison on execve(2)

selinux_bprm_creds_for_exec() needs to compare the old and new SIDs to determine if the execve(2) operation is transitioning into a new context (where process { transition } and file { entrypoint } permissions would be checked) or not (file { execute_no_trans } would be checked). It does so by just comparing their numeric values.

However, after ae254858ce07 ("selinux: introduce an initial SID for early boot processes"), we can now easily get into a situation where the SID numbers differ, but the context is the same for both. Specifically when the policy assigns the same context for SECINITSID_KERNEL and SECINITSID_INIT - in this case when a process labeled with SECINITSID_INIT does execve(2) without a transition, security_transition_sid() will translate the unchanged context to the first matching SID number, which is SECINITSID_KERNEL, not SECINITSID_INIT. Thus the kernel thinks that a transition has happened and unexpectedly tests for the file { entrypoint } permission.

Fix this by checking the SID equality more carefully, trying context_equal() on the underlying contexts when it is necessary - in the rare case that the SIDs differ, but both are "initial" SIDs.

Fixes ae254858ce07 ("selinux: introduce an initial SID for early boot processes")
---
 security/selinux/hooks.c            |  2 +-
 security/selinux/include/security.h |  2 ++
 security/selinux/ss/services.c      | 35 +++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 1 deletion(-)
security/selinux/hooks.c +1 -1
index b8115df536abd..be95e6e83f1d0 100644
@@ -2369,7 +2369,7 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
 	ad.type = LSM_AUDIT_DATA_FILE;
 	ad.u.file = bprm->file;
 
-	if (new_tsec->sid == old_tsec->sid) {
+	if (security_sids_equal(new_tsec->sid, old_tsec->sid)) {
 		rc = avc_has_perm(old_tsec->sid, isec->sid,
 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
 		if (rc)
security/selinux/include/security.h +2 -0
index 278c144c22d60..c3ed350718d1d 100644
@@ -299,6 +299,8 @@ int security_context_to_sid_default(const char *scontext, u32 scontext_len,
 int security_context_to_sid_force(const char *scontext, u32 scontext_len,
 				  u32 *sid);
 
+bool security_sids_equal(u32 sid1, u32 sid2);
+
 int security_get_user_sids(u32 fromsid, const char *username, u32 **sids, u32 *nel);
 
 int security_port_sid(u8 protocol, u16 port, u32 *out_sid);
security/selinux/ss/services.c +35 -0
index 7becf3808818a..297317763f6d4 100644
@@ -1448,6 +1448,41 @@ int security_sid_to_context_inval(u32 sid,
 					    scontext_len, 1, 1);
 }
 
+/**
+ * security_sids_equal - Determine if two SIDs map to the same context.
+ * @sid1: first SID
+ * @sid2: second SID
+ */
+bool security_sids_equal(u32 sid1, u32 sid2)
+{
+	struct context *c1, *c2;
+	struct selinux_policy *policy;
+	struct sidtab *sidtab;
+	bool res;
+
+	if (!selinux_initialized())
+		return sid1 == sid2;
+
+	if (sid1 == sid2)
+		return true;
+
+	if (sid1 > SECINITSID_NUM || sid2 > SECINITSID_NUM)
+		return false;
+
+	/* Initial SIDs may map to the same context, so do a full comparison */
+	rcu_read_lock();
+	policy = rcu_dereference(selinux_state.policy);
+	sidtab = policy->sidtab;
+	c1 = sidtab_search(sidtab, sid1);
+	c2 = sidtab_search(sidtab, sid2);
+	if (!c1 || !c2)
+		res = false;
+	else
+		res = context_equal(c1, c2);
+	rcu_read_unlock();
+	return res;
+}
+
 /*
  * Caveat:  Mutates scontext.
  */
Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 18 lines · Ondrej Mosnacek

selinux_bprm_creds_for_exec() needs to compare the old and new SIDs to determine if the execve(2) operation is transitioning into a new context (where process { transition } and file { entrypoint } permissions would be checked) or not (file { execute_no_trans } would be checked). It does so by just comparing their numeric values.

However, after ae254858ce07 ("selinux: introduce an initial SID for early boot processes"), we can now easily get into a situation where the SID numbers differ, but the context is the same for both. Specifically when the policy assigns the same context for SECINITSID_KERNEL and SECINITSID_INIT - in this case when a process labeled with SECINITSID_INIT does execve(2) without a transition, security_transition_sid() will translate the unchanged context to the first matching SID number, which is SECINITSID_KERNEL, not SECINITSID_INIT. Thus the kernel thinks that a transition has happened and unexpectedly tests for the file { entrypoint } permission.

Quoted 3 lines

Fix this by checking the SID equality more carefully, trying context_equal() on the underlying contexts when it is necessary - in the rare case that the SIDs differ, but both are "initial" SIDs.

We check for SID equality elsewhere as well, e.g. file_has_perm() and selinux_binder_transfer_file(), ioctl_has_perm(), and selinux_kernel_load_from_file() to decide whether to check fd use permission, selinux_file_permission() to decide whether we need to revalidate permissions, selinux_binder_transaction() to decide whether to check binder impersonate permission, task_avdcache_search() to decide whether we can use the avdcache. I'm wondering if we wouldn't be better off just ensuring that security_transition_sid() returns SECINITSID_INIT when called with SECINITSID_INIT and there is no transition defined.

Quoted 85 lines
Fixes ae254858ce07 ("selinux: introduce an initial SID for early boot processes")
---
 security/selinux/hooks.c            |  2 +-
 security/selinux/include/security.h |  2 ++
 security/selinux/ss/services.c      | 35 +++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 1 deletion(-)
security/selinux/hooks.c +1 -1
index b8115df536abd..be95e6e83f1d0 100644
@@ -2369,7 +2369,7 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
        ad.type = LSM_AUDIT_DATA_FILE;
        ad.u.file = bprm->file;
 
-       if (new_tsec->sid == old_tsec->sid) {
+       if (security_sids_equal(new_tsec->sid, old_tsec->sid)) {
                rc = avc_has_perm(old_tsec->sid, isec->sid,
                                  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
                if (rc)
security/selinux/include/security.h +2 -0
index 278c144c22d60..c3ed350718d1d 100644
@@ -299,6 +299,8 @@ int security_context_to_sid_default(const char *scontext, u32 scontext_len,
 int security_context_to_sid_force(const char *scontext, u32 scontext_len,
                                  u32 *sid);
 
+bool security_sids_equal(u32 sid1, u32 sid2);
+
 int security_get_user_sids(u32 fromsid, const char *username, u32 **sids, u32 *nel);
 
 int security_port_sid(u8 protocol, u16 port, u32 *out_sid);
security/selinux/ss/services.c +35 -1
index 7becf3808818a..297317763f6d4 100644
@@ -1448,6 +1448,41 @@ int security_sid_to_context_inval(u32 sid,
                                            scontext_len, 1, 1);
 }
 
+/**
+ * security_sids_equal - Determine if two SIDs map to the same context.
+ * @sid1: first SID
+ * @sid2: second SID
+ */
+bool security_sids_equal(u32 sid1, u32 sid2)
+{
+       struct context *c1, *c2;
+       struct selinux_policy *policy;
+       struct sidtab *sidtab;
+       bool res;
+
+       if (!selinux_initialized())
+               return sid1 == sid2;
+
+       if (sid1 == sid2)
+               return true;
+
+       if (sid1 > SECINITSID_NUM || sid2 > SECINITSID_NUM)
+               return false;
+
+       /* Initial SIDs may map to the same context, so do a full comparison */
+       rcu_read_lock();
+       policy = rcu_dereference(selinux_state.policy);
+       sidtab = policy->sidtab;
+       c1 = sidtab_search(sidtab, sid1);
+       c2 = sidtab_search(sidtab, sid2);
+       if (!c1 || !c2)
+               res = false;
+       else
+               res = context_equal(c1, c2);
+       rcu_read_unlock();
+       return res;
+}
+
 /*
  * Caveat:  Mutates scontext.
  */
--

2.49.0

#2 lore raw

I have created a v2 patch following Stephen's advice. It keeps the boot process successful on sysvinit with the dracut selinux module enabled.

Can you please check if it also works on other setups ?

The patch has been posted with its original subject + v2 tag added.

[PATCH v2] Only set the initial SID for early-boot userspace tasks if the policy supports the new capability

Thanks,

Guido

Quoted 141 lines · Stephen Smalley
Quoted 26 lines · Ondrej Mosnacek

selinux_bprm_creds_for_exec() needs to compare the old and new SIDs to
determine if the execve(2) operation is transitioning into a new context
(where process { transition } and file { entrypoint } permissions would
be checked) or not (file { execute_no_trans } would be checked). It does
so by just comparing their numeric values.

However, after ae254858ce07 ("selinux: introduce an initial SID for early boot processes"), we can now easily get into a situation where the
SID numbers differ, but the context is the same for both. Specifically
when the policy assigns the same context for SECINITSID_KERNEL and SECINITSID_INIT - in this case when a process labeled with SECINITSID_INIT does execve(2) without a transition, security_transition_sid() will translate the unchanged context to the
first matching SID number, which is SECINITSID_KERNEL, not SECINITSID_INIT. Thus the kernel thinks that a transition has happened
and unexpectedly tests for the file { entrypoint } permission.

Quoted 4 lines

Fix this by checking the SID equality more carefully, trying context_equal() on the underlying contexts when it is necessary - in the
rare case that the SIDs differ, but both are "initial" SIDs.

We check for SID equality elsewhere as well, e.g. file_has_perm() and selinux_binder_transfer_file(), ioctl_has_perm(), and selinux_kernel_load_from_file() to decide whether to check fd use permission, selinux_file_permission() to decide whether we need to revalidate permissions, selinux_binder_transaction() to decide whether
to check binder impersonate permission, task_avdcache_search() to decide whether we can use the avdcache. I'm wondering if we wouldn't be better off just ensuring that security_transition_sid() returns SECINITSID_INIT when called with SECINITSID_INIT and there is no transition defined.

Quoted 96 lines
Fixes ae254858ce07 ("selinux: introduce an initial SID for early

boot processes")

---
 security/selinux/hooks.c            |  2 +-
 security/selinux/include/security.h |  2 ++
 security/selinux/ss/services.c      | 35
+++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 1 deletion(-)
security/selinux/hooks.c +1 -1
index b8115df536abd..be95e6e83f1d0 100644
@@ -2369,7 +2369,7 @@ static int selinux_bprm_creds_for_exec(struct
 linux_binprm *bprm)
        ad.type = LSM_AUDIT_DATA_FILE;
        ad.u.file = bprm->file;
 
-       if (new_tsec->sid == old_tsec->sid) {
+       if (security_sids_equal(new_tsec->sid, old_tsec->sid)) {
                rc = avc_has_perm(old_tsec->sid, isec->sid,
                                  SECCLASS_FILE,
FILE__EXECUTE_NO_TRANS, &ad);
                if (rc)
security/selinux/include/security.h +2 -0
index 278c144c22d60..c3ed350718d1d 100644
@@ -299,6 +299,8 @@ int security_context_to_sid_default(const char
 *scontext, u32 scontext_len,
 int security_context_to_sid_force(const char *scontext, u32
 scontext_len,
                                  u32 *sid);
 
+bool security_sids_equal(u32 sid1, u32 sid2);
+
 int security_get_user_sids(u32 fromsid, const char *username, u32

**sids, u32 *nel);

int security_port_sid(u8 protocol, u16 port, u32 *out_sid);

security/selinux/ss/services.c +2 -0
index 7becf3808818a..297317763f6d4 100644
@@ -1448,6 +1448,41 @@ int security_sid_to_context_inval(u32 sid,
                                            scontext_len, 1, 1);
 }
 
+/**
+ * security_sids_equal - Determine if two SIDs map to the same

context.
+ * @sid1: first SID
+ * @sid2: second SID
+ */

security/selinux/ss/services.c +17 -0
+bool security_sids_equal(u32 sid1, u32 sid2)
+{
+       struct context *c1, *c2;
+       struct selinux_policy *policy;
+       struct sidtab *sidtab;
+       bool res;
+
+       if (!selinux_initialized())
+               return sid1 == sid2;
+
+       if (sid1 == sid2)
+               return true;
+
+       if (sid1 > SECINITSID_NUM || sid2 > SECINITSID_NUM)
+               return false;
+
+       /* Initial SIDs may map to the same context, so do a full

comparison */

security/selinux/ss/services.c +13 -1
+       rcu_read_lock();
+       policy = rcu_dereference(selinux_state.policy);
+       sidtab = policy->sidtab;
+       c1 = sidtab_search(sidtab, sid1);
+       c2 = sidtab_search(sidtab, sid2);
+       if (!c1 || !c2)
+               res = false;
+       else
+               res = context_equal(c1, c2);
+       rcu_read_unlock();
+       return res;
+}
+
 /*
  * Caveat:  Mutates scontext.
  */
--

2.49.0

#3 lore raw
In reply to Stephen Smalley
Quoted 20 lines · Stephen Smalley
Quoted 18 lines · Ondrej Mosnacek

selinux_bprm_creds_for_exec() needs to compare the old and new SIDs to determine if the execve(2) operation is transitioning into a new context (where process { transition } and file { entrypoint } permissions would be checked) or not (file { execute_no_trans } would be checked). It does so by just comparing their numeric values.

However, after ae254858ce07 ("selinux: introduce an initial SID for early boot processes"), we can now easily get into a situation where the SID numbers differ, but the context is the same for both. Specifically when the policy assigns the same context for SECINITSID_KERNEL and SECINITSID_INIT - in this case when a process labeled with SECINITSID_INIT does execve(2) without a transition, security_transition_sid() will translate the unchanged context to the first matching SID number, which is SECINITSID_KERNEL, not SECINITSID_INIT. Thus the kernel thinks that a transition has happened and unexpectedly tests for the file { entrypoint } permission.

Quoted 8 lines
Quoted 3 lines

Fix this by checking the SID equality more carefully, trying context_equal() on the underlying contexts when it is necessary - in the rare case that the SIDs differ, but both are "initial" SIDs.

We check for SID equality elsewhere as well, e.g. file_has_perm() and selinux_binder_transfer_file(), ioctl_has_perm(), and selinux_kernel_load_from_file() to decide whether to check fd use permission,

BTW, this case already has quite a tricky behavior, because if e.g. only the MLS part of the context differs, you suddenly need to have the something_t self:fd { use } permission allowed for the fd to get passed, even though policies don't seem to have that commonly allowed (at least Fedora's one doesn't, based on a quick sesearch). OTOH, we can't simplify it to just comparing the types, because the other fields can affect the decision as well...

And actually with the entrypoint permission it's even trickier, because you are forced to allow entrypoint to any type pair that can wind up in an MLS-only transition and then it becomes possible to enter the domain through that executable type even from other domains.

Quoted 4 lines

selinux_file_permission() to decide whether we need to revalidate permissions, selinux_binder_transaction() to decide whether to check binder impersonate permission, task_avdcache_search() to decide whether we can use the avdcache.

AFAICT, except for selinux_binder_transaction(), here the implication is just that a slower path might be taken unnecessarily, so not a big deal. selinux_binder_transaction() is then a similar case as the fd use permission.

Quoted 4 lines

I'm wondering if we wouldn't
be better off just ensuring that security_transition_sid() returns SECINITSID_INIT when called with SECINITSID_INIT and there is no transition defined.

Not sure if that can be (easily) done, since security_compute_sid() has tons of logic to recompute all the different fields (user, role, type, mls) independently. We would need to carefully track if anything actually changed and that could be prone to errors.

--
Ondrej Mosnacek
Senior Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.