Skip to contentSkip to conversation

selinux_init_load_policy: setenforce(0) if security_disable() fails

PATCH In review Will Woods 3 participants 4 messages Apr 29, 2014 – May 13, 2014 SELinux security module

lore
Share & tools
lore

Will Woods

author
#0 lore raw

PATCH 1/1 selinux_init_load_policy: setenforce(0) if security_disable() fails

If you run selinux_init_load_policy() after a chroot/switch-root, it's possible that your *previous* root loaded policy, but your *new* root wants SELinux disabled.

We can't disable SELinux in this case, but we *do* need to make sure it's permissive. Otherwise we may continue to enforce the old policy.

So, if seconfig = -1, but security_disable() fails, we set *enforce=0, and then let the existing code handle the security_{get,set}enforce stuff.

Once that's handled, exit with failure via "goto noload", as before.

---
 libselinux/src/load_policy.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
libselinux/src/load_policy.c +11 -6
index e419f1a..21ee58b 100644
@@ -417,13 +417,15 @@ int selinux_init_load_policy(int *enforce)
 			/* Successfully disabled, so umount selinuxfs too. */
 			umount(selinux_mnt);
 			fini_selinuxmnt();
+			goto noload;
+		} else {
+			/*
+			 * It's possible that this failed because policy has
+			 * already been loaded. We can't disable SELinux now,
+			 * so the best we can do is force it to be permissive.
+			 */
+			*enforce = 0;
 		}
-		/*
-		 * If we failed to disable, SELinux will still be 
-		 * effectively permissive, because no policy is loaded. 
-		 * No need to call security_setenforce(0) here.
-		 */
-		goto noload;
 	}
 
 	/*
@@ -442,6 +444,9 @@ int selinux_init_load_policy(int *enforce)
 		}
 	}
 
+	if (seconfig == -1)
+		goto noload;
+
 	/* Load the policy. */
 	return selinux_mkload_policy(0);
 
Signature · 1 line
1.9.0
#1 lore raw
Quoted 43 lines · Will Woods

If you run selinux_init_load_policy() after a chroot/switch-root, it's possible that your *previous* root loaded policy, but your *new* root wants SELinux disabled.

We can't disable SELinux in this case, but we *do* need to make sure it's permissive. Otherwise we may continue to enforce the old policy.

So, if seconfig = -1, but security_disable() fails, we set *enforce=0, and then let the existing code handle the security_{get,set}enforce stuff.

Once that's handled, exit with failure via "goto noload", as before.

---
 libselinux/src/load_policy.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
libselinux/src/load_policy.c +8 -6
index e419f1a..21ee58b 100644
@@ -417,13 +417,15 @@ int selinux_init_load_policy(int *enforce)
 			/* Successfully disabled, so umount selinuxfs too. */
 			umount(selinux_mnt);
 			fini_selinuxmnt();
+			goto noload;
+		} else {
+			/*
+			 * It's possible that this failed because policy has
+			 * already been loaded. We can't disable SELinux now,
+			 * so the best we can do is force it to be permissive.
+			 */
+			*enforce = 0;
 		}
-		/*
-		 * If we failed to disable, SELinux will still be 
-		 * effectively permissive, because no policy is loaded. 
-		 * No need to call security_setenforce(0) here.
-		 */
-		goto noload;
 	}
 
 	/*

libselinux/src/load_policy.c int selinux_init_load_policy(int *enforce) · line 452

Quoted 9 lines
libselinux/src/load_policy.c +3 -0
@@ -442,6 +444,9 @@ int selinux_init_load_policy(int *enforce)
 		}
 	}
 
+	if (seconfig == -1)
+		goto noload;
+
 	/* Load the policy. */
 	return selinux_mkload_policy(0);
 

We attempted to make changes for this, and I believe it ended badly.

https://bugzilla.redhat.com/show_bug.cgi?id=1046470

author
#2 lore raw
Quoted 13 lines · Will Woods
Quoted 12 lines

If you run selinux_init_load_policy() after a chroot/switch-root, it's possible that your *previous* root loaded policy, but your *new* root wants SELinux disabled.

We can't disable SELinux in this case, but we *do* need to make sure it's permissive. Otherwise we may continue to enforce the old policy.

So, if seconfig = -1, but security_disable() fails, we set *enforce=0, and then let the existing code handle the security_{get,set}enforce stuff.

Once that's handled, exit with failure via "goto noload", as before.

Quoted 4 lines · Daniel J Walsh

We attempted to make changes for this, and I believe it ended badly.

https://bugzilla.redhat.com/show_bug.cgi?id=1046470

I'm a bit confused - the patch I sent here doesn't modify the "SELINUX=disabled" behavior *unless* security_disable() fails.

Since that code is only reached after successfully mounting selinuxfs, I'm pretty sure that failure can only mean we've loaded policy, and thus SELinux can no longer be disabled.

The only behavior *this* patch changes is what happens after that; it now tries to do security_setenforce(0) before returning.

That bug *is* relevant to the other email I sent, since it concerns the "SELinux=disabled" check overriding the commandline - and yeah, I'm happy to leave that check alone if you're worried about that, but I was curious about its origin.

-w

#3 lore raw
In reply to Will Woods
Quoted 43 lines · Will Woods

If you run selinux_init_load_policy() after a chroot/switch-root, it's possible that your *previous* root loaded policy, but your *new* root wants SELinux disabled.

We can't disable SELinux in this case, but we *do* need to make sure it's permissive. Otherwise we may continue to enforce the old policy.

So, if seconfig = -1, but security_disable() fails, we set *enforce=0, and then let the existing code handle the security_{get,set}enforce stuff.

Once that's handled, exit with failure via "goto noload", as before.

---
 libselinux/src/load_policy.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
libselinux/src/load_policy.c +8 -6
index e419f1a..21ee58b 100644
@@ -417,13 +417,15 @@ int selinux_init_load_policy(int *enforce)
 			/* Successfully disabled, so umount selinuxfs too. */
 			umount(selinux_mnt);
 			fini_selinuxmnt();
+			goto noload;
+		} else {
+			/*
+			 * It's possible that this failed because policy has
+			 * already been loaded. We can't disable SELinux now,
+			 * so the best we can do is force it to be permissive.
+			 */
+			*enforce = 0;
 		}
-		/*
-		 * If we failed to disable, SELinux will still be 
-		 * effectively permissive, because no policy is loaded. 
-		 * No need to call security_setenforce(0) here.
-		 */
-		goto noload;
 	}
 
 	/*

libselinux/src/load_policy.c int selinux_init_load_policy(int *enforce) · line 451

Quoted 8 lines
libselinux/src/load_policy.c +3 -0
@@ -442,6 +444,9 @@ int selinux_init_load_policy(int *enforce)
 		}
 	}
 
+	if (seconfig == -1)
+		goto noload;
+
 	/* Load the policy. */
 	return selinux_mkload_policy(0);

Applied on #next, will be in libselinux 2.4.