Skip to contentSkip to conversation

selinux: change security_compute_sid to return the ssid or tsid on match

PATCH In review Stephen Smalley 3 participants 11 messages Jun 10, 2025 – Jun 21, 2025 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH v2 1/1 selinux: change security_compute_sid to return the ssid or tsid on match

If the end result of a security_compute_sid() computation matches the ssid or tsid, return that SID rather than looking it up again. This avoids the problem of multiple initial SIDs that map to the same context.

Fixes ae254858ce07 ("selinux: introduce an initial SID for early boot processes")

---
v2 fixes a compiler error.

 security/selinux/ss/services.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
security/selinux/ss/services.c +11 -5
index 7becf3808818..d185754c2786 100644
@@ -1909,11 +1909,17 @@ static int security_compute_sid(u32 ssid,
 			goto out_unlock;
 	}
 	/* Obtain the sid for the context. */
-	rc = sidtab_context_to_sid(sidtab, &newcontext, out_sid);
-	if (rc == -ESTALE) {
-		rcu_read_unlock();
-		context_destroy(&newcontext);
-		goto retry;
+	if (context_equal(scontext, &newcontext))
+		*out_sid = ssid;
+	else if (context_equal(tcontext, &newcontext))
+		*out_sid = tsid;
+	else {
+		rc = sidtab_context_to_sid(sidtab, &newcontext, out_sid);
+		if (rc == -ESTALE) {
+			rcu_read_unlock();
+			context_destroy(&newcontext);
+			goto retry;
+		}
 	}
 out_unlock:
 	rcu_read_unlock();
Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 6 lines · Stephen Smalley

If the end result of a security_compute_sid() computation matches the ssid or tsid, return that SID rather than looking it up again. This avoids the problem of multiple initial SIDs that map to the same context.

Quoted 8 lines
Fixes ae254858ce07 ("selinux: introduce an initial SID for early boot processes")

---
v2 fixes a compiler error.

 security/selinux/ss/services.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

Assuming we can get a tested-by from Guido, I believe I like this approach the best.

Quoted 29 lines
security/selinux/ss/services.c +11 -6
index 7becf3808818..d185754c2786 100644
@@ -1909,11 +1909,17 @@ static int security_compute_sid(u32 ssid,
                        goto out_unlock;
        }
        /* Obtain the sid for the context. */
-       rc = sidtab_context_to_sid(sidtab, &newcontext, out_sid);
-       if (rc == -ESTALE) {
-               rcu_read_unlock();
-               context_destroy(&newcontext);
-               goto retry;
+       if (context_equal(scontext, &newcontext))
+               *out_sid = ssid;
+       else if (context_equal(tcontext, &newcontext))
+               *out_sid = tsid;
+       else {
+               rc = sidtab_context_to_sid(sidtab, &newcontext, out_sid);
+               if (rc == -ESTALE) {
+                       rcu_read_unlock();
+                       context_destroy(&newcontext);
+                       goto retry;
+               }
        }
 out_unlock:
        rcu_read_unlock();
--

2.49.0

Signature · 1 line
paul-moore.com
#2 lore raw
In reply to Stephen Smalley

Hello,

I have just tested Stephen's patch and it solves the problem as an alternative to the dracut patch and other patches posted here along with the issue description:

https://github.com/dracut-ng/dracut-ng/issues/377

Please note that I have only tested the success of the boot process with sysvinit and not other aspects of the system functionality, other init daemons or regressions that might be caused as a result of applying this patch.

Guido

Quoted 44 lines · Stephen Smalley

If the end result of a security_compute_sid() computation matches the ssid or tsid, return that SID rather than looking it up again. This avoids the problem of multiple initial SIDs that map to the same context.

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

boot processes")

---
v2 fixes a compiler error.

 security/selinux/ss/services.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
security/selinux/ss/services.c +11 -5
index 7becf3808818..d185754c2786 100644
@@ -1909,11 +1909,17 @@ static int security_compute_sid(u32 ssid,
 			goto out_unlock;
 	}
 	/* Obtain the sid for the context. */
-	rc = sidtab_context_to_sid(sidtab, &newcontext, out_sid);
-	if (rc == -ESTALE) {
-		rcu_read_unlock();
-		context_destroy(&newcontext);
-		goto retry;
+	if (context_equal(scontext, &newcontext))
+		*out_sid = ssid;
+	else if (context_equal(tcontext, &newcontext))
+		*out_sid = tsid;
+	else {
+		rc = sidtab_context_to_sid(sidtab, &newcontext,
 out_sid);
+		if (rc == -ESTALE) {
+			rcu_read_unlock();
+			context_destroy(&newcontext);
+			goto retry;
+		}
 	}
 out_unlock:

rcu_read_unlock();

maintainer
#3 lore raw
Quoted 7 lines · Guido Trentalancia

Hello,

I have just tested Stephen's patch and it solves the problem as an alternative to the dracut patch and other patches posted here along with the issue description:

Quoted 6 lines

https://github.com/dracut-ng/dracut-ng/issues/377

Please note that I have only tested the success of the boot process with sysvinit and not other aspects of the system functionality, other init daemons or regressions that might be caused as a result of applying this patch.

Understood, thanks for the testing you've done.

Would you be okay if I added your "Tested-by:" tag to the patch?

Quoted 45 lines · Stephen Smalley
Quoted 44 lines

If the end result of a security_compute_sid() computation matches the ssid or tsid, return that SID rather than looking it up again. This avoids the problem of multiple initial SIDs that map to the same context.

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

boot processes")

---
v2 fixes a compiler error.

 security/selinux/ss/services.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
security/selinux/ss/services.c +11 -5
index 7becf3808818..d185754c2786 100644
@@ -1909,11 +1909,17 @@ static int security_compute_sid(u32 ssid,
                      goto out_unlock;
      }
      /* Obtain the sid for the context. */
-     rc = sidtab_context_to_sid(sidtab, &newcontext, out_sid);
-     if (rc == -ESTALE) {
-             rcu_read_unlock();
-             context_destroy(&newcontext);
-             goto retry;
+     if (context_equal(scontext, &newcontext))
+             *out_sid = ssid;
+     else if (context_equal(tcontext, &newcontext))
+             *out_sid = tsid;
+     else {
+             rc = sidtab_context_to_sid(sidtab, &newcontext,
 out_sid);
+             if (rc == -ESTALE) {
+                     rcu_read_unlock();
+                     context_destroy(&newcontext);
+                     goto retry;
+             }
      }
 out_unlock:
      rcu_read_unlock();
Signature · 1 line
paul-moore.com
#4 lore raw

I have very little time to look at the underlying problem and, as already explained, I have only tested that it solves dracut-ng issue 377.

This change has widened the scope considerably compared to the other proposed patch, I cannot so easily certify that it does not introduce regressions or problems in other scenarios.

The main point is that the offending code in ae254858ce07 is only executed when the policy has not been loaded yet (and it sets the SID to SECINITSID_INIT).

On the other hand, when the policy is not loaded security_compute_sid() is reduced to:

switch (orig_tclass) {
                case SECCLASS_PROCESS: /* kernel value */
                        *out_sid = ssid;
                        break;
                goto out;

return rc;

In other words, when the policy has not been loaded yet (which is when the problem is introduced), the code in this change is not reached and security_compute_sid() simply returns ssid (when called with SECCLASS_PROCESS). So, the mechanism of action of this change on the problem appears to be indirect in some way.

Moreover, security_compute_sid() is called by several other functions rather than simply by security_transition_sid(), so without proper extensive testing in multiple use cases (read multiple code paths) it's very difficult to say the change is fully tested...

I would recommend more extensive testing.

Guido

Quoted 70 lines · Paul Moore
Quoted 7 lines · Guido Trentalancia

Hello,

I have just tested Stephen's patch and it solves the problem as an alternative to the dracut patch and other patches posted here along with the issue description:

Quoted 7 lines

https://github.com/dracut-ng/dracut-ng/issues/377

Please note that I have only tested the success of the boot process with sysvinit and not other aspects of the system functionality, other
init daemons or regressions that might be caused as a result of applying this patch.

Understood, thanks for the testing you've done.

Would you be okay if I added your "Tested-by:" tag to the patch?

Quoted 47 lines · Stephen Smalley
Quoted 46 lines

If the end result of a security_compute_sid() computation matches the
ssid or tsid, return that SID rather than looking it up again. This
avoids the problem of multiple initial SIDs that map to the same context.

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

boot processes")

---
v2 fixes a compiler error.

 security/selinux/ss/services.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
security/selinux/ss/services.c +11 -5
index 7becf3808818..d185754c2786 100644
@@ -1909,11 +1909,17 @@ static int security_compute_sid(u32 ssid,
                      goto out_unlock;
      }
      /* Obtain the sid for the context. */
-     rc = sidtab_context_to_sid(sidtab, &newcontext, out_sid);
-     if (rc == -ESTALE) {
-             rcu_read_unlock();
-             context_destroy(&newcontext);
-             goto retry;
+     if (context_equal(scontext, &newcontext))
+             *out_sid = ssid;
+     else if (context_equal(tcontext, &newcontext))
+             *out_sid = tsid;
+     else {
+             rc = sidtab_context_to_sid(sidtab, &newcontext,
 out_sid);
+             if (rc == -ESTALE) {
+                     rcu_read_unlock();
+                     context_destroy(&newcontext);
+                     goto retry;
+             }
      }
 out_unlock:
      rcu_read_unlock();
author maintainer
#5 lore raw
Quoted 29 lines · Guido Trentalancia

I have very little time to look at the underlying problem and, as already explained, I have only tested that it solves dracut-ng issue 377.

This change has widened the scope considerably compared to the other proposed patch, I cannot so easily certify that it does not introduce regressions or problems in other scenarios.

The main point is that the offending code in ae254858ce07 is only executed when the policy has not been loaded yet (and it sets the SID to SECINITSID_INIT).

On the other hand, when the policy is not loaded security_compute_sid() is reduced to:

switch (orig_tclass) {
                case SECCLASS_PROCESS: /* kernel value */
                        *out_sid = ssid;
                        break;
                goto out;

return rc;

In other words, when the policy has not been loaded yet (which is when the problem is introduced), the code in this change is not reached and security_compute_sid() simply returns ssid (when called with SECCLASS_PROCESS). So, the mechanism of action of this change on the problem appears to be indirect in some way.

Quoted 6 lines

Moreover, security_compute_sid() is called by several other functions rather than simply by security_transition_sid(), so without proper extensive testing in multiple use cases (read multiple code paths) it's very difficult to say the change is fully tested...

I would recommend more extensive testing.

It passes the selinux-testsuite without any problems, FWIW.

Quoted 74 lines

Guido

Quoted 70 lines · Paul Moore
Quoted 7 lines · Guido Trentalancia

Hello,

I have just tested Stephen's patch and it solves the problem as an alternative to the dracut patch and other patches posted here along with the issue description:

Quoted 7 lines

https://github.com/dracut-ng/dracut-ng/issues/377

Please note that I have only tested the success of the boot process with sysvinit and not other aspects of the system functionality, other
init daemons or regressions that might be caused as a result of applying this patch.

Understood, thanks for the testing you've done.

Would you be okay if I added your "Tested-by:" tag to the patch?

Quoted 47 lines · Stephen Smalley
Quoted 46 lines

If the end result of a security_compute_sid() computation matches the
ssid or tsid, return that SID rather than looking it up again. This
avoids the problem of multiple initial SIDs that map to the same context.

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

boot processes")

---
v2 fixes a compiler error.

 security/selinux/ss/services.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
security/selinux/ss/services.c +11 -5
index 7becf3808818..d185754c2786 100644
@@ -1909,11 +1909,17 @@ static int security_compute_sid(u32 ssid,
                      goto out_unlock;
      }
      /* Obtain the sid for the context. */
-     rc = sidtab_context_to_sid(sidtab, &newcontext, out_sid);
-     if (rc == -ESTALE) {
-             rcu_read_unlock();
-             context_destroy(&newcontext);
-             goto retry;
+     if (context_equal(scontext, &newcontext))
+             *out_sid = ssid;
+     else if (context_equal(tcontext, &newcontext))
+             *out_sid = tsid;
+     else {
+             rc = sidtab_context_to_sid(sidtab, &newcontext,
 out_sid);
+             if (rc == -ESTALE) {
+                     rcu_read_unlock();
+                     context_destroy(&newcontext);
+                     goto retry;
+             }
      }
 out_unlock:
      rcu_read_unlock();
#6 lore raw

Ok then, as long as it passes the testsuite it should be sufficiently tested for most purposes, I suppose.

You can add the Tested-by line, if you like...

Thanks,

Guido

Quoted 135 lines · Stephen Smalley
Quoted 37 lines · Guido Trentalancia

I have very little time to look at the underlying problem and, as already explained, I have only tested that it solves dracut-ng issue
377.

This change has widened the scope considerably compared to the other
proposed patch, I cannot so easily certify that it does not introduce
regressions or problems in other scenarios.

The main point is that the offending code in ae254858ce07 is only executed when the policy has not been loaded yet (and it sets the SID
to SECINITSID_INIT).

On the other hand, when the policy is not loaded
security_compute_sid()
is reduced to:

switch (orig_tclass) {
                case SECCLASS_PROCESS: /* kernel value */
                        *out_sid = ssid;
                        break;
                goto out;

return rc;

In other words, when the policy has not been loaded yet (which is when
the problem is introduced), the code in this change is not reached and
security_compute_sid() simply returns ssid (when called with SECCLASS_PROCESS). So, the mechanism of action of this change on the
problem appears to be indirect in some way.

Quoted 8 lines

Moreover, security_compute_sid() is called by several other functions
rather than simply by security_transition_sid(), so without proper extensive testing in multiple use cases (read multiple code paths) it's
very difficult to say the change is fully tested...

I would recommend more extensive testing.

It passes the selinux-testsuite without any problems, FWIW.

Quoted 85 lines

Guido

Quoted 81 lines · Paul Moore
Quoted 11 lines · Guido Trentalancia

Hello,

I have just tested Stephen's patch and it solves the problem as an
alternative to the dracut patch and other patches posted here along
with the issue description:

https://github.com/dracut-ng/dracut-ng/issues/377

Quoted 7 lines

Please note that I have only tested the success of the boot process
with sysvinit and not other aspects of the system functionality,
other
init daemons or regressions that might be caused as a result of applying this patch.

Understood, thanks for the testing you've done.

Would you be okay if I added your "Tested-by:" tag to the patch?

Quoted 54 lines · Stephen Smalley
Quoted 53 lines

If the end result of a security_compute_sid() computation matches
the
ssid or tsid, return that SID rather than looking it up again.
This
avoids the problem of multiple initial SIDs that map to the same
context.

Fixes ae254858ce07 ("selinux: introduce an initial SID for

early
boot processes")

m>
---
v2 fixes a compiler error.

 security/selinux/ss/services.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
security/selinux/ss/services.c +11 -5
index 7becf3808818..d185754c2786 100644
@@ -1909,11 +1909,17 @@ static int security_compute_sid(u32
 ssid,
                      goto out_unlock;
      }
      /* Obtain the sid for the context. */
-     rc = sidtab_context_to_sid(sidtab, &newcontext,
 out_sid);
-     if (rc == -ESTALE) {
-             rcu_read_unlock();
-             context_destroy(&newcontext);
-             goto retry;
+     if (context_equal(scontext, &newcontext))
+             *out_sid = ssid;
+     else if (context_equal(tcontext, &newcontext))
+             *out_sid = tsid;
+     else {
+             rc = sidtab_context_to_sid(sidtab, &newcontext,
 out_sid);
+             if (rc == -ESTALE) {
+                     rcu_read_unlock();
+                     context_destroy(&newcontext);
+                     goto retry;
+             }
      }
 out_unlock:
      rcu_read_unlock();
maintainer
#7 lore raw
Quoted 5 lines · Guido Trentalancia

Ok then, as long as it passes the testsuite it should be sufficiently tested for most purposes, I suppose.

You can add the Tested-by line, if you like...

Thanks Guido.

I'm thinking this might be a good candidate for stable, what do others think?

Quoted 136 lines · Stephen Smalley
Quoted 135 lines
Quoted 37 lines · Guido Trentalancia

I have very little time to look at the underlying problem and, as already explained, I have only tested that it solves dracut-ng issue
377.

This change has widened the scope considerably compared to the other
proposed patch, I cannot so easily certify that it does not introduce
regressions or problems in other scenarios.

The main point is that the offending code in ae254858ce07 is only executed when the policy has not been loaded yet (and it sets the SID
to SECINITSID_INIT).

On the other hand, when the policy is not loaded
security_compute_sid()
is reduced to:

switch (orig_tclass) {
                case SECCLASS_PROCESS: /* kernel value */
                        *out_sid = ssid;
                        break;
                goto out;

return rc;

In other words, when the policy has not been loaded yet (which is when
the problem is introduced), the code in this change is not reached and
security_compute_sid() simply returns ssid (when called with SECCLASS_PROCESS). So, the mechanism of action of this change on the
problem appears to be indirect in some way.

Quoted 8 lines

Moreover, security_compute_sid() is called by several other functions
rather than simply by security_transition_sid(), so without proper extensive testing in multiple use cases (read multiple code paths) it's
very difficult to say the change is fully tested...

I would recommend more extensive testing.

It passes the selinux-testsuite without any problems, FWIW.

Quoted 85 lines

Guido

Quoted 81 lines · Paul Moore
Quoted 11 lines · Guido Trentalancia

Hello,

I have just tested Stephen's patch and it solves the problem as an
alternative to the dracut patch and other patches posted here along
with the issue description:

https://github.com/dracut-ng/dracut-ng/issues/377

Quoted 7 lines

Please note that I have only tested the success of the boot process
with sysvinit and not other aspects of the system functionality,
other
init daemons or regressions that might be caused as a result of applying this patch.

Understood, thanks for the testing you've done.

Would you be okay if I added your "Tested-by:" tag to the patch?

Quoted 54 lines · Stephen Smalley
Quoted 53 lines

If the end result of a security_compute_sid() computation matches
the
ssid or tsid, return that SID rather than looking it up again.
This
avoids the problem of multiple initial SIDs that map to the same
context.

Fixes ae254858ce07 ("selinux: introduce an initial SID for

early
boot processes")

m>
---
v2 fixes a compiler error.

 security/selinux/ss/services.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
security/selinux/ss/services.c +11 -5
index 7becf3808818..d185754c2786 100644
@@ -1909,11 +1909,17 @@ static int security_compute_sid(u32
 ssid,
                      goto out_unlock;
      }
      /* Obtain the sid for the context. */
-     rc = sidtab_context_to_sid(sidtab, &newcontext,
 out_sid);
-     if (rc == -ESTALE) {
-             rcu_read_unlock();
-             context_destroy(&newcontext);
-             goto retry;
+     if (context_equal(scontext, &newcontext))
+             *out_sid = ssid;
+     else if (context_equal(tcontext, &newcontext))
+             *out_sid = tsid;
+     else {
+             rc = sidtab_context_to_sid(sidtab, &newcontext,
 out_sid);
+             if (rc == -ESTALE) {
+                     rcu_read_unlock();
+                     context_destroy(&newcontext);
+                     goto retry;
+             }
      }
 out_unlock:
      rcu_read_unlock();
Signature · 1 line
paul-moore.com
#8 lore raw

PATCH 1/2 selinux: prevent potential comparison mismatches when computing the SID (was [PATCH v2] selinux: change security_compute_sid to return the ssid or tsid on match)

Yes Paul, I found more time to look at it and it should be merged into stable.

However, I thought the following two patches might help giving the whole updated security_compute_sid() function more stability.

I have not fully tested them through the testsuite, so this is open to discussion and more extensive testing...

The context computed internally by security_compute_sid() is now used to compute the output SID directly by context comparison (fix for ae254858ce07).

In order to prevent potential comparison mismatches between the source and target contexts, the role that is computed internally is determined more accurately by applying the same criteria that is used for the user and for the type of the internally computed context in the case of sockets and process class.

---
 security/selinux/ss/services.c |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
security/selinux/ss/services.c +6 -3
@@ -1826,10 +1826,13 @@ retry:
 	} else if (cladatum && cladatum->default_role == DEFAULT_TARGET) {
 		newcontext.role = tcontext->role;
 	} else {
-		if ((tclass == policydb->process_class) || sock)
+		if ((tclass == policydb->process_class) || sock) {
+			/* Use the role of process. */
 			newcontext.role = scontext->role;
-		else
-			newcontext.role = OBJECT_R_VAL;
+		} else {
+			/* Use the role of the related object. */
+			newcontext.role = tcontext->role;
+		}
 	}
 
 	/* Set the type.
Quoted 170 lines · Paul Moore
Quoted 6 lines · Guido Trentalancia

Ok then, as long as it passes the testsuite it should be sufficiently
tested for most purposes, I suppose.

You can add the Tested-by line, if you like...

Thanks Guido.

I'm thinking this might be a good candidate for stable, what do others think?

Quoted 153 lines · Stephen Smalley
Quoted 152 lines
Quoted 52 lines · Guido Trentalancia

I have very little time to look at the underlying problem and, as
already explained, I have only tested that it solves dracut-ng issue
377.

This change has widened the scope considerably compared to the other
proposed patch, I cannot so easily certify that it does not introduce
regressions or problems in other scenarios.

The main point is that the offending code in ae254858ce07 is only
executed when the policy has not been loaded yet (and it sets the
SID
to SECINITSID_INIT).

On the other hand, when the policy is not loaded
security_compute_sid()
is reduced to:

switch (orig_tclass) {
                case SECCLASS_PROCESS: /* kernel value */
                        *out_sid = ssid;
                        break;
                goto out;

return rc;

In other words, when the policy has not been loaded yet (which is
when
the problem is introduced), the code in this change is not reached
and
security_compute_sid() simply returns ssid (when called with SECCLASS_PROCESS). So, the mechanism of action of this change on
the
problem appears to be indirect in some way.

Moreover, security_compute_sid() is called by several other functions
rather than simply by security_transition_sid(), so without proper
extensive testing in multiple use cases (read multiple code paths)
it's
very difficult to say the change is fully tested...

Quoted 1 line

I would recommend more extensive testing.

It passes the selinux-testsuite without any problems, FWIW.

Quoted 94 lines

Guido

Quoted 90 lines · Paul Moore
Quoted 13 lines · Guido Trentalancia

Hello,

I have just tested Stephen's patch and it solves the problem as
an
alternative to the dracut patch and other patches posted here
along
with the issue description:

https://github.com/dracut-ng/dracut-ng/issues/377

Quoted 8 lines

Please note that I have only tested the success of the boot process
with sysvinit and not other aspects of the system functionality,
other
init daemons or regressions that might be caused as a result of
applying this patch.

Understood, thanks for the testing you've done.

Would you be okay if I added your "Tested-by:" tag to the patch?

Quoted 59 lines · Stephen Smalley
Quoted 58 lines

If the end result of a security_compute_sid() computation matches
the
ssid or tsid, return that SID rather than looking it up again.
This
avoids the problem of multiple initial SIDs that map to the
same
context.

Fixes ae254858ce07 ("selinux: introduce an initial SID

for
early
boot processes")

l.co
m>
---
v2 fixes a compiler error.

 security/selinux/ss/services.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
security/selinux/ss/services.c +0 -0
index 7becf3808818..d185754c2786 100644
@@ -1909,11 +1909,17 @@ static int

security_compute_sid(u32
ssid,

                      goto out_unlock;
      }
      /* Obtain the sid for the context. */

- rc = sidtab_context_to_sid(sidtab, &newcontext,
out_sid);

security/selinux/ss/services.c +6 -4
-     if (rc == -ESTALE) {
-             rcu_read_unlock();
-             context_destroy(&newcontext);
-             goto retry;
+     if (context_equal(scontext, &newcontext))
+             *out_sid = ssid;
+     else if (context_equal(tcontext, &newcontext))
+             *out_sid = tsid;
+     else {
+             rc = sidtab_context_to_sid(sidtab,

&newcontext,
out_sid);

security/selinux/ss/services.c +5 -0
+             if (rc == -ESTALE) {
+                     rcu_read_unlock();
+                     context_destroy(&newcontext);
+                     goto retry;
+             }
      }
 out_unlock:
      rcu_read_unlock();
#9 lore raw

PATCH 2/2 selinux: when computing the SID before policy load take into account the socket class case (was [PATCH v2] selinux: change security_compute_sid to return the ssid or tsid on match)

In reply to Paul Moore

When computing the SID before the policy is loaded, take into account not only the case of the process class, but also the case of sockets classes.

---
 security/selinux/ss/services.c |    6 ++++++
 1 file changed, 6 insertions(+)
security/selinux/ss/services.c +6 -0
@@ -1750,6 +1750,12 @@ static int security_compute_sid(u32 ssid
 	bool sock;
 
 	if (!selinux_initialized()) {
+		sock = security_is_socket_class(orig_tclass);
+		if (sock) {
+			*out_sid = ssid;
+			goto out;
+		}
+
 		switch (orig_tclass) {
 		case SECCLASS_PROCESS: /* kernel value */
 			*out_sid = ssid;
Quoted 170 lines · Paul Moore
Quoted 6 lines · Guido Trentalancia

Ok then, as long as it passes the testsuite it should be sufficiently
tested for most purposes, I suppose.

You can add the Tested-by line, if you like...

Thanks Guido.

I'm thinking this might be a good candidate for stable, what do others think?

Quoted 153 lines · Stephen Smalley
Quoted 152 lines
Quoted 52 lines · Guido Trentalancia

I have very little time to look at the underlying problem and, as
already explained, I have only tested that it solves dracut-ng issue
377.

This change has widened the scope considerably compared to the other
proposed patch, I cannot so easily certify that it does not introduce
regressions or problems in other scenarios.

The main point is that the offending code in ae254858ce07 is only
executed when the policy has not been loaded yet (and it sets the
SID
to SECINITSID_INIT).

On the other hand, when the policy is not loaded
security_compute_sid()
is reduced to:

switch (orig_tclass) {
                case SECCLASS_PROCESS: /* kernel value */
                        *out_sid = ssid;
                        break;
                goto out;

return rc;

In other words, when the policy has not been loaded yet (which is
when
the problem is introduced), the code in this change is not reached
and
security_compute_sid() simply returns ssid (when called with SECCLASS_PROCESS). So, the mechanism of action of this change on
the
problem appears to be indirect in some way.

Moreover, security_compute_sid() is called by several other functions
rather than simply by security_transition_sid(), so without proper
extensive testing in multiple use cases (read multiple code paths)
it's
very difficult to say the change is fully tested...

Quoted 1 line

I would recommend more extensive testing.

It passes the selinux-testsuite without any problems, FWIW.

Quoted 94 lines

Guido

Quoted 90 lines · Paul Moore
Quoted 13 lines · Guido Trentalancia

Hello,

I have just tested Stephen's patch and it solves the problem as
an
alternative to the dracut patch and other patches posted here
along
with the issue description:

https://github.com/dracut-ng/dracut-ng/issues/377

Quoted 8 lines

Please note that I have only tested the success of the boot process
with sysvinit and not other aspects of the system functionality,
other
init daemons or regressions that might be caused as a result of
applying this patch.

Understood, thanks for the testing you've done.

Would you be okay if I added your "Tested-by:" tag to the patch?

Quoted 59 lines · Stephen Smalley
Quoted 58 lines

If the end result of a security_compute_sid() computation matches
the
ssid or tsid, return that SID rather than looking it up again.
This
avoids the problem of multiple initial SIDs that map to the
same
context.

Fixes ae254858ce07 ("selinux: introduce an initial SID

for
early
boot processes")

l.co
m>
---
v2 fixes a compiler error.

 security/selinux/ss/services.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
security/selinux/ss/services.c +0 -0
index 7becf3808818..d185754c2786 100644
@@ -1909,11 +1909,17 @@ static int

security_compute_sid(u32
ssid,

                      goto out_unlock;
      }
      /* Obtain the sid for the context. */

- rc = sidtab_context_to_sid(sidtab, &newcontext,
out_sid);

security/selinux/ss/services.c +6 -4
-     if (rc == -ESTALE) {
-             rcu_read_unlock();
-             context_destroy(&newcontext);
-             goto retry;
+     if (context_equal(scontext, &newcontext))
+             *out_sid = ssid;
+     else if (context_equal(tcontext, &newcontext))
+             *out_sid = tsid;
+     else {
+             rc = sidtab_context_to_sid(sidtab,

&newcontext,
out_sid);

security/selinux/ss/services.c +5 -0
+             if (rc == -ESTALE) {
+                     rcu_read_unlock();
+                     context_destroy(&newcontext);
+                     goto retry;
+             }
      }
 out_unlock:
      rcu_read_unlock();
maintainer
#10 lore raw
In reply to Stephen Smalley
Quoted 6 lines · On Jun 10, 2025 Stephen Smalley <[email protected]> wrote:

If the end result of a security_compute_sid() computation matches the ssid or tsid, return that SID rather than looking it up again. This avoids the problem of multiple initial SIDs that map to the same context.

Quoted 8 lines
Fixes ae254858ce07 ("selinux: introduce an initial SID for early boot processes")

---
v2 fixes a compiler error.

 security/selinux/ss/services.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

Added Guido's 'Tested-by' tag, marked for stable, and merged into selinux/stable-6.16 with the plan of sending this up to Linus next week.

Thanks everyone!

--
paul-moore.com