Skip to contentSkip to conversation

libselinux: Do not fall back to /selinux on a sysfs mount failure

PATCH In review Stephen Smalley 2 participants 3 messages Mar 4 – May 11 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 libselinux: Do not fall back to /selinux on a sysfs mount failure

Even if we cannot mount sysfs ourselves (e.g. in a user namespace), we can still try to mount on /sys/fs/selinux first prior to falling back to /selinux.

---
 libselinux/src/load_policy.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
libselinux/src/load_policy.c +5 -10
index 8e737a23..bb7f2dd0 100644
@@ -290,16 +290,11 @@ int selinux_init_load_policy(int *enforce)
 	 */
 	const char *mntpoint = NULL;
 	/* First make sure /sys is mounted */
-	if (mount("sysfs", "/sys", "sysfs", 0, 0) == 0 || errno == EBUSY) {
-		/* MS_NODEV can't be set because of /sys/fs/selinux/null device, used by Android */
-		if (mount(SELINUXFS, SELINUXMNT, SELINUXFS, MS_NOEXEC | MS_NOSUID, 0) == 0 || errno == EBUSY) {
-			mntpoint = SELINUXMNT;
-		} else {
-			/* check old mountpoint */
-			if (mount(SELINUXFS, OLDSELINUXMNT, SELINUXFS, 0, 0) == 0 || errno == EBUSY) {
-				mntpoint = OLDSELINUXMNT;
-			}
-		}
+	(void) mount("sysfs", "/sys", "sysfs", 0, 0);
+
+	/* MS_NODEV can't be set because of /sys/fs/selinux/null device, used by Android */
+	if (mount(SELINUXFS, SELINUXMNT, SELINUXFS, MS_NOEXEC | MS_NOSUID, 0) == 0 || errno == EBUSY) {
+		mntpoint = SELINUXMNT;
 	} else {
 		/* check old mountpoint */
 		if (mount(SELINUXFS, OLDSELINUXMNT, SELINUXFS, 0, 0) == 0 || errno == EBUSY) {
Signature · 1 line
2.52.0
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Stephen Smalley

Even if we cannot mount sysfs ourselves (e.g. in a user namespace), we can still try to mount on /sys/fs/selinux first prior to falling back to /selinux.

Quoted 32 lines
---
 libselinux/src/load_policy.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
libselinux/src/load_policy.c +5 -10
index 8e737a23..bb7f2dd0 100644
@@ -290,16 +290,11 @@ int selinux_init_load_policy(int *enforce)
 	 */
 	const char *mntpoint = NULL;
 	/* First make sure /sys is mounted */
-	if (mount("sysfs", "/sys", "sysfs", 0, 0) == 0 || errno == EBUSY) {
-		/* MS_NODEV can't be set because of /sys/fs/selinux/null device, used by Android */
-		if (mount(SELINUXFS, SELINUXMNT, SELINUXFS, MS_NOEXEC | MS_NOSUID, 0) == 0 || errno == EBUSY) {
-			mntpoint = SELINUXMNT;
-		} else {
-			/* check old mountpoint */
-			if (mount(SELINUXFS, OLDSELINUXMNT, SELINUXFS, 0, 0) == 0 || errno == EBUSY) {
-				mntpoint = OLDSELINUXMNT;
-			}
-		}
+	(void) mount("sysfs", "/sys", "sysfs", 0, 0);
+
+	/* MS_NODEV can't be set because of /sys/fs/selinux/null device, used by Android */
+	if (mount(SELINUXFS, SELINUXMNT, SELINUXFS, MS_NOEXEC | MS_NOSUID, 0) == 0 || errno == EBUSY) {
+		mntpoint = SELINUXMNT;
 	} else {
 		/* check old mountpoint */
 		if (mount(SELINUXFS, OLDSELINUXMNT, SELINUXFS, 0, 0) == 0 || errno == EBUSY) {
Signature · 1 line
2.52.0