1 From 32773a99b1f0cf2b61b5f5a33359684b18aab1ed Mon Sep 17 00:00:00 2001
2 From: Stephen Smalley <sds@tycho.nsa.gov>
3 Date: Fri, 13 May 2016 11:59:47 -0400
4 Subject: [PATCH] Avoid mounting /proc outside of selinux_init_load_policy().
6 Temporarily mounting /proc within selinuxfs_exists() can cause
7 problems since it can be called by a libselinux constructor and
8 therefore may be invoked by every program linked with libselinux.
9 Since this was only motivated originally by a situation where
10 selinuxfs_exists() was called from selinux_init_load_policy()
11 before /proc was mounted, fix it in selinux_init_load_policy() instead.
13 This reverts commit 5a8d8c499b2ef80eaa7b5abe2ec68d7101e613bf
14 ("libselinux: only mount /proc if necessary") and
15 commit 9df498884665d79474b79f0f30d1cd67df11bd3e
16 ("libselinux: Mount procfs before checking /proc/filesystems").
18 Signed-off-by: Stephen Smalley <sds@tycho.nsa.gov>
20 libselinux/src/init.c | 27 +++------------------------
21 libselinux/src/load_policy.c | 15 ++++++++++-----
22 2 files changed, 13 insertions(+), 29 deletions(-)
24 diff --git a/libselinux/src/init.c b/libselinux/src/init.c
25 index 3530594..3c687a2 100644
26 --- libselinux/src/init.c
27 +++ libselinux/src/init.c
32 -#include <sys/mount.h>
33 -#include <linux/magic.h>
37 @@ -58,26 +56,15 @@ static int verify_selinuxmnt(const char *mnt)
39 int selinuxfs_exists(void)
41 - int exists = 0, mnt_rc = -1, rc;
50 - rc = statfs("/proc", &sb);
51 - } while (rc < 0 && errno == EINTR);
53 - if (rc == 0 && ((uint32_t)sb.f_type != (uint32_t)PROC_SUPER_MAGIC))
54 - mnt_rc = mount("proc", "/proc", "proc", 0, 0);
56 fp = fopen("/proc/filesystems", "r");
58 - exists = 1; /* Fail as if it exists */
63 + return 1; /* Fail as if it exists */
64 __fsetlocking(fp, FSETLOCKING_BYCALLER);
66 num = getline(&buf, &len, fp);
67 @@ -91,14 +78,6 @@ int selinuxfs_exists(void)
77 - umount2("/proc", MNT_DETACH);
81 hidden_def(selinuxfs_exists)
82 diff --git a/libselinux/src/load_policy.c b/libselinux/src/load_policy.c
83 index 21ee58b..4f39fc7 100644
84 --- libselinux/src/load_policy.c
85 +++ libselinux/src/load_policy.c
94 int security_load_policy(void *data, size_t len)
97 @@ -348,11 +352,6 @@ int selinux_init_load_policy(int *enforce)
102 -#define MNT_DETACH 2
105 - umount2("/proc", MNT_DETACH);
108 * Determine the final desired mode.
109 @@ -400,11 +399,17 @@ int selinux_init_load_policy(int *enforce)
110 /* Only emit this error if selinux was not disabled */
111 fprintf(stderr, "Mount failed for selinuxfs on %s: %s\n", SELINUXMNT, strerror(errno));
115 + umount2("/proc", MNT_DETACH);
119 set_selinuxmnt(mntpoint);
122 + umount2("/proc", MNT_DETACH);
125 * Note: The following code depends on having selinuxfs
126 * already mounted and selinuxmnt set above.