c811450ba396ee92daac28792e559e515a8f6ad5
[gentoo.git] /
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().
5
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.
12
13 This reverts commit 5a8d8c499b2ef80eaa7b5abe2ec68d7101e613bf
14 ("libselinux: only mount /proc if necessary") and
15 commit 9df498884665d79474b79f0f30d1cd67df11bd3e
16 ("libselinux: Mount procfs before checking /proc/filesystems").
17
18 Signed-off-by: Stephen Smalley <sds@tycho.nsa.gov>
19 ---
20  libselinux/src/init.c        | 27 +++------------------------
21  libselinux/src/load_policy.c | 15 ++++++++++-----
22  2 files changed, 13 insertions(+), 29 deletions(-)
23
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
28 @@ -11,8 +11,6 @@
29  #include <sys/vfs.h>
30  #include <stdint.h>
31  #include <limits.h>
32 -#include <sys/mount.h>
33 -#include <linux/magic.h>
34  
35  #include "dso.h"
36  #include "policy.h"
37 @@ -58,26 +56,15 @@ static int verify_selinuxmnt(const char *mnt)
38  
39  int selinuxfs_exists(void)
40  {
41 -       int exists = 0, mnt_rc = -1, rc;
42 -       struct statfs sb;
43 +       int exists = 0;
44         FILE *fp = NULL;
45         char *buf = NULL;
46         size_t len;
47         ssize_t num;
48  
49 -       do {
50 -               rc = statfs("/proc", &sb);
51 -       } while (rc < 0 && errno == EINTR);
52 -
53 -       if (rc == 0 && ((uint32_t)sb.f_type != (uint32_t)PROC_SUPER_MAGIC))
54 -               mnt_rc = mount("proc", "/proc", "proc", 0, 0);
55 -
56         fp = fopen("/proc/filesystems", "r");
57 -       if (!fp) {
58 -               exists = 1; /* Fail as if it exists */
59 -               goto out;
60 -       }
61 -
62 +       if (!fp)
63 +               return 1; /* Fail as if it exists */
64         __fsetlocking(fp, FSETLOCKING_BYCALLER);
65  
66         num = getline(&buf, &len, fp);
67 @@ -91,14 +78,6 @@ int selinuxfs_exists(void)
68  
69         free(buf);
70         fclose(fp);
71 -
72 -out:
73 -#ifndef MNT_DETACH
74 -#define MNT_DETACH 2
75 -#endif
76 -       if (mnt_rc == 0)
77 -               umount2("/proc", MNT_DETACH);
78 -
79         return exists;
80  }
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
86 @@ -17,6 +17,10 @@
87  #include "policy.h"
88  #include <limits.h>
89  
90 +#ifndef MNT_DETACH
91 +#define MNT_DETACH 2
92 +#endif
93 +
94  int security_load_policy(void *data, size_t len)
95  {
96         char path[PATH_MAX];
97 @@ -348,11 +352,6 @@ int selinux_init_load_policy(int *enforce)
98                 fclose(cfg);
99                 free(buf);
100         }
101 -#ifndef MNT_DETACH
102 -#define MNT_DETACH 2
103 -#endif
104 -       if (rc == 0)
105 -               umount2("/proc", MNT_DETACH);
106  
107         /* 
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));
112                 }
113 +
114 +               if (rc == 0)
115 +                       umount2("/proc", MNT_DETACH);
116                  
117                 goto noload;
118         }
119         set_selinuxmnt(mntpoint);
120  
121 +       if (rc == 0)
122 +               umount2("/proc", MNT_DETACH);
123 +
124         /*
125          * Note:  The following code depends on having selinuxfs 
126          * already mounted and selinuxmnt set above.
127 -- 
128 2.7.3
129