Skip to contentSkip to conversation

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

PATCH In review Guido Trentalancia 2 participants 5 messages May 24, 2025 – Jun 9, 2025 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Only set the initial SID for early-boot userspace tasks if the policy supports the new capability

Before the policy is loaded, only set the initial SID for early-boot userspace tasks if the policy supports such capability.

Check for POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT,
otherwise preserve the old behavior of using the
SECINITSID_KERNEL label for early-boot userspace
processes.

Fixes ae254858ce07 ("selinux: introduce an initial SID for early boot processes")
---
 security/selinux/hooks.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/selinux/hooks.c +1 -1
@@ -2319,7 +2319,7 @@ static int selinux_bprm_creds_for_exec(s
 	 * early boot end up with a label different from SECINITSID_KERNEL
 	 * (if the policy chooses to set SECINITSID_INIT != SECINITSID_KERNEL).
 	 */
-	if (!selinux_initialized()) {
+	if (!selinux_initialized() && selinux_policycap_userspace_initial_context()) {
 		new_tsec->sid = SECINITSID_INIT;
 		/* also clear the exec_sid just in case */
 		new_tsec->exec_sid = 0;
#1 lore raw
Quoted 20 lines · Guido Trentalancia

Before the policy is loaded, only set the initial SID for early-boot userspace tasks if the policy supports such capability.

Check for POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT,
otherwise preserve the old behavior of using the
SECINITSID_KERNEL label for early-boot userspace
processes.

Fixes ae254858ce07 ("selinux: introduce an initial SID for early boot processes")
---
 security/selinux/hooks.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

security/selinux/hooks.c 2025-05-24 14:36:58.453997848 +0200 static int selinux_bprm_creds_for_exec(s · line 2325

Quoted 8 lines
security/selinux/hooks.c 2025-05-24 14:36:58.453997848 +0200 renamed from security/selinux/hooks.c 2025-05-24 14:37:45.499997561 +0200 +1 -1
@@ -2319,7 +2319,7 @@ static int selinux_bprm_creds_for_exec(s
         * early boot end up with a label different from SECINITSID_KERNEL
         * (if the policy chooses to set SECINITSID_INIT != SECINITSID_KERNEL).
         */
-       if (!selinux_initialized()) {
+       if (!selinux_initialized() && selinux_policycap_userspace_initial_context()) {
                new_tsec->sid = SECINITSID_INIT;
                /* also clear the exec_sid just in case */
                new_tsec->exec_sid = 0;

This doesn't make sense - obviously you can't check what capabilities the policy supports when it's not loaded yet... Does the current behavior break something for you?

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

#2 lore raw

Yes, it obviously breaks something: systems based on sysvinit fail to boot !

The full bug report is here:

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

Another solution was initially proposed as a dracut patch, however it has not been merged yet.

You are right, the kernel patch proposed here is not acceptable, as the policy capabilities cannot be checked before the policy is loaded.

Guido

Quoted 44 lines · Ondrej Mosnacek
Quoted 27 lines · Guido Trentalancia

Before the policy is loaded, only set the initial SID for early-boot userspace tasks if the policy supports such capability.

Check for POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT,
otherwise preserve the old behavior of using the
SECINITSID_KERNEL label for early-boot userspace
processes.

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

boot processes")

---
 security/selinux/hooks.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/selinux/hooks.c 2025-05-24 14:36:58.453997848 +0200 renamed from security/selinux/hooks.c 2025-05-24 14:37:45.499997561 +0200 +0 -0
@@ -2319,7 +2319,7 @@ static int selinux_bprm_creds_for_exec(s
         * early boot end up with a label different from

SECINITSID_KERNEL
* (if the policy chooses to set SECINITSID_INIT !=
SECINITSID_KERNEL).

         */
security/selinux/hooks.c 2025-05-24 14:36:58.453997848 +0200 renamed from security/selinux/hooks.c 2025-05-24 14:37:45.499997561 +0200 +1 -1
-       if (!selinux_initialized()) {
+       if (!selinux_initialized() &&
Quoted 5 lines
selinux_policycap_userspace_initial_context()) {
                new_tsec->sid = SECINITSID_INIT;
                /* also clear the exec_sid just in case */
                new_tsec->exec_sid = 0;

This doesn't make sense - obviously you can't check what capabilities the policy supports when it's not loaded yet... Does the current behavior break something for you?

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

#3 lore raw
Quoted 7 lines · Guido Trentalancia

Yes, it obviously breaks something: systems based on sysvinit fail to boot !

The full bug report is here:

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

Ah, the entrypoint denials are indeed unexpected in this case and the cause really is the ae254858ce07 commit. I'll send a patch shortly that should fix them.

BTW, the reason why @LoganSchmalz saw a similar error on Fedora with systemd is likely that they had enabled the "selinux" dracut module, which is not necessary with systemd (which can handle SELinux setup on its own) and will only break things with the Fedora selinux-policy (even when the entrypoint bug is fixed).

Thank you for reporting and for your efforts to fix the issue!

Quoted 4 lines

Another solution was initially proposed as a dracut patch, however it has not been merged yet.

Quoted 51 lines

You are right, the kernel patch proposed here is not acceptable, as the policy capabilities cannot be checked before the policy is loaded.

Guido

Quoted 44 lines · Ondrej Mosnacek
Quoted 27 lines · Guido Trentalancia

Before the policy is loaded, only set the initial SID for early-boot userspace tasks if the policy supports such capability.

Check for POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT,
otherwise preserve the old behavior of using the
SECINITSID_KERNEL label for early-boot userspace
processes.

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

boot processes")

---
 security/selinux/hooks.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/selinux/hooks.c 2025-05-24 14:36:58.453997848 +0200 renamed from security/selinux/hooks.c 2025-05-24 14:37:45.499997561 +0200 +0 -0
@@ -2319,7 +2319,7 @@ static int selinux_bprm_creds_for_exec(s
         * early boot end up with a label different from

SECINITSID_KERNEL
* (if the policy chooses to set SECINITSID_INIT !=
SECINITSID_KERNEL).

         */
security/selinux/hooks.c 2025-05-24 14:36:58.453997848 +0200 renamed from security/selinux/hooks.c 2025-05-24 14:37:45.499997561 +0200 +1 -1
-       if (!selinux_initialized()) {
+       if (!selinux_initialized() &&
Quoted 5 lines
selinux_policycap_userspace_initial_context()) {
                new_tsec->sid = SECINITSID_INIT;
                /* also clear the exec_sid just in case */
                new_tsec->exec_sid = 0;

This doesn't make sense - obviously you can't check what capabilities the policy supports when it's not loaded yet... Does the current behavior break something for you?

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

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

#4 lore raw
In reply to Ondrej Mosnacek

Can you please check if this v2 patch sorts out the issue ? It does sort things for me...

Before the policy is loaded, only set the initial SID for early-boot userspace tasks if the policy supports such capability.

We cannot actually check if the policy supports the new POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT capability, so we perform a speculative call to security_transition_sid() with SECINITSID_INIT as the SID parameter and check if it returns SECINITSID_INIT and no transition is defined.

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

Fixes ae254858ce07 ("selinux: introduce an initial SID for early boot processes")
---
 security/selinux/hooks.c |   30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
security/selinux/hooks.c +22 -8
@@ -2295,6 +2295,7 @@ static int selinux_bprm_creds_for_exec(s
 	struct inode_security_struct *isec;
 	struct common_audit_data ad;
 	struct inode *inode = file_inode(bprm->file);
+	u32 saved_sid;
 	int rc;
 
 	/* SELinux context only depends on initial program or script and not
@@ -2314,16 +2315,29 @@ static int selinux_bprm_creds_for_exec(s
 	new_tsec->sockcreate_sid = 0;
 
 	/*
-	 * Before policy is loaded, label any task outside kernel space
-	 * as SECINITSID_INIT, so that any userspace tasks surviving from
-	 * early boot end up with a label different from SECINITSID_KERNEL
-	 * (if the policy chooses to set SECINITSID_INIT != SECINITSID_KERNEL).
+	 * Before policy is loaded, label any task outside kernel space as
+	 * SECINITSID_INIT, so that any userspace task surviving from early
+	 * boot ends up with a label different from SECINITSID_KERNEL: this
+	 * is needed to support the new POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT
+	 * capability.
+	 *
+	 * Because the policy is not loaded yet, we cannot check whether such
+	 * capability is enabled in the policy, hence we check if a call to
+	 * security_transition_sid() returns SECINITSID_INIT when called with
+	 * SECINITSID_INIT and there is no transition defined.
 	 */
 	if (!selinux_initialized()) {
-		new_tsec->sid = SECINITSID_INIT;
-		/* also clear the exec_sid just in case */
-		new_tsec->exec_sid = 0;
-		return 0;
+		saved_sid = current_sid();
+		rc = security_transition_sid(SECINITSID_INIT, SECINITSID_INIT,
+					     SECCLASS_PROCESS, NULL,
+					     &new_tsec->sid);
+		if (rc && new_tsec->sid == SECINITSID_INIT) {
+			/* also clear the exec_sid just in case */
+			new_tsec->exec_sid = 0;
+			return 0;
+		} else {
+			new_tsec->sid = saved_sid;
+		}
 	}
 
 	if (old_tsec->exec_sid) {
Quoted 45 lines · Ondrej Mosnacek
Quoted 27 lines · Guido Trentalancia

Before the policy is loaded, only set the initial SID for early-boot userspace tasks if the policy supports such capability.

Check for POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT,
otherwise preserve the old behavior of using the
SECINITSID_KERNEL label for early-boot userspace
processes.

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

boot processes")

---
 security/selinux/hooks.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/selinux/hooks.c 2025-05-24 14:36:58.453997848 +0200 renamed from security/selinux/hooks.c 2025-05-24 14:37:45.499997561 +0200 +0 -0
@@ -2319,7 +2319,7 @@ static int selinux_bprm_creds_for_exec(s
         * early boot end up with a label different from

SECINITSID_KERNEL
* (if the policy chooses to set SECINITSID_INIT !=
SECINITSID_KERNEL).

         */
security/selinux/hooks.c 2025-05-24 14:36:58.453997848 +0200 renamed from security/selinux/hooks.c 2025-05-24 14:37:45.499997561 +0200 +1 -1
-       if (!selinux_initialized()) {
+       if (!selinux_initialized() &&
Quoted 5 lines
selinux_policycap_userspace_initial_context()) {
                new_tsec->sid = SECINITSID_INIT;
                /* also clear the exec_sid just in case */
                new_tsec->exec_sid = 0;

This doesn't make sense - obviously you can't check what capabilities the policy supports when it's not loaded yet... Does the current behavior break something for you?

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