tunables: Fix environment variable processing for setuid binaries (bz #21073)
[platform/upstream/glibc.git] / elf / tst-env-setuid.c
1 /* Copyright (C) 2012-2017 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <http://www.gnu.org/licenses/>.  */
17
18 /* Verify that tunables correctly filter out unsafe environment variables like
19    MALLOC_CHECK_ and MALLOC_MMAP_THRESHOLD_ but also retain
20    MALLOC_MMAP_THRESHOLD_ in an unprivileged child.  */
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31
32 #include <support/support.h>
33 #include <support/test-driver.h>
34
35 static char SETGID_CHILD[] = "setgid-child";
36 #define CHILD_STATUS 42
37
38 /* Return a GID which is not our current GID, but is present in the
39    supplementary group list.  */
40 static gid_t
41 choose_gid (void)
42 {
43   const int count = 64;
44   gid_t groups[count];
45   int ret = getgroups (count, groups);
46   if (ret < 0)
47     {
48       printf ("getgroups: %m\n");
49       exit (1);
50     }
51   gid_t current = getgid ();
52   for (int i = 0; i < ret; ++i)
53     {
54       if (groups[i] != current)
55         return groups[i];
56     }
57   return 0;
58 }
59
60 /* Spawn and execute a program and verify that it returns the CHILD_STATUS.  */
61 static pid_t
62 do_execve (char **args)
63 {
64   pid_t kid = vfork ();
65
66   if (kid < 0)
67     {
68       printf ("vfork: %m\n");
69       return -1;
70     }
71
72   if (kid == 0)
73     {
74       /* Child process.  */
75       execve (args[0], args, environ);
76       _exit (-errno);
77     }
78
79   if (kid < 0)
80     return 1;
81
82   int status;
83
84   if (waitpid (kid, &status, 0) < 0)
85     {
86       printf ("waitpid: %m\n");
87       return 1;
88     }
89
90   if (!WIFEXITED (status) || WEXITSTATUS (status) != CHILD_STATUS)
91     {
92       printf ("Unexpected exit status %d from child process\n",
93               status);
94       return 1;
95     }
96   return 0;
97 }
98
99 /* Copies the executable into a restricted directory, so that we can
100    safely make it SGID with the TARGET group ID.  Then runs the
101    executable.  */
102 static int
103 run_executable_sgid (gid_t target)
104 {
105   char *dirname = xasprintf ("%s/tst-tunables-setuid.%jd",
106                              test_dir, (intmax_t) getpid ());
107   char *execname = xasprintf ("%s/bin", dirname);
108   int infd = -1;
109   int outfd = -1;
110   int ret = 0;
111   if (mkdir (dirname, 0700) < 0)
112     {
113       printf ("mkdir: %m\n");
114       goto err;
115     }
116   infd = open ("/proc/self/exe", O_RDONLY);
117   if (infd < 0)
118     {
119       printf ("open (/proc/self/exe): %m\n");
120       goto err;
121     }
122   outfd = open (execname, O_WRONLY | O_CREAT | O_EXCL, 0700);
123   if (outfd < 0)
124     {
125       printf ("open (%s): %m\n", execname);
126       goto err;
127     }
128   char buf[4096];
129   for (;;)
130     {
131       ssize_t rdcount = read (infd, buf, sizeof (buf));
132       if (rdcount < 0)
133         {
134           printf ("read: %m\n");
135           goto err;
136         }
137       if (rdcount == 0)
138         break;
139       char *p = buf;
140       char *end = buf + rdcount;
141       while (p != end)
142         {
143           ssize_t wrcount = write (outfd, buf, end - p);
144           if (wrcount == 0)
145             errno = ENOSPC;
146           if (wrcount <= 0)
147             {
148               printf ("write: %m\n");
149               goto err;
150             }
151           p += wrcount;
152         }
153     }
154   if (fchown (outfd, getuid (), target) < 0)
155     {
156       printf ("fchown (%s): %m\n", execname);
157       goto err;
158     }
159   if (fchmod (outfd, 02750) < 0)
160     {
161       printf ("fchmod (%s): %m\n", execname);
162       goto err;
163     }
164   if (close (outfd) < 0)
165     {
166       printf ("close (outfd): %m\n");
167       goto err;
168     }
169   if (close (infd) < 0)
170     {
171       printf ("close (infd): %m\n");
172       goto err;
173     }
174
175   char *args[] = {execname, SETGID_CHILD, NULL};
176
177   ret = do_execve (args);
178
179 err:
180   if (outfd >= 0)
181     close (outfd);
182   if (infd >= 0)
183     close (infd);
184   if (execname)
185     {
186       unlink (execname);
187       free (execname);
188     }
189   if (dirname)
190     {
191       rmdir (dirname);
192       free (dirname);
193     }
194   return ret;
195 }
196
197 #ifndef test_child
198 static int
199 test_child (void)
200 {
201   if (getenv ("MALLOC_CHECK_") != NULL)
202     {
203       printf ("MALLOC_CHECK_ is still set\n");
204       return 1;
205     }
206
207   if (getenv ("MALLOC_MMAP_THRESHOLD_") == NULL)
208     {
209       printf ("MALLOC_MMAP_THRESHOLD_ lost\n");
210       return 1;
211     }
212
213   return 0;
214 }
215 #endif
216
217 #ifndef test_parent
218 static int
219 test_parent (void)
220 {
221   if (getenv ("MALLOC_CHECK_") == NULL)
222     {
223       printf ("MALLOC_CHECK_ lost\n");
224       return 1;
225     }
226
227   if (getenv ("MALLOC_MMAP_THRESHOLD_") == NULL)
228     {
229       printf ("MALLOC_MMAP_THRESHOLD_ lost\n");
230       return 1;
231     }
232
233   return 0;
234 }
235 #endif
236
237 static int
238 do_test_prep (int argc, char **argv)
239 {
240   /* Setgid child process.  */
241   if (argc == 2 && strcmp (argv[1], SETGID_CHILD) == 0)
242     {
243       if (getgid () == getegid ())
244         {
245           /* This can happen if the file system is mounted nosuid.  */
246           fprintf (stderr, "SGID failed: GID and EGID match (%jd)\n",
247                    (intmax_t) getgid ());
248           exit (EXIT_UNSUPPORTED);
249         }
250
251       int ret = test_child ();
252
253       if (ret != 0)
254         exit (1);
255
256       exit (CHILD_STATUS);
257     }
258   else
259     {
260       if (test_parent () != 0)
261         exit (1);
262
263       /* Try running a setgid program.  */
264       gid_t target = choose_gid ();
265       if (target == 0)
266         {
267           fprintf (stderr,
268                    "Could not find a suitable GID for user %jd, skipping test\n",
269                    (intmax_t) getuid ());
270           exit (0);
271         }
272
273       if (run_executable_sgid (target) == 0)
274         exit (0);
275     }
276
277   /* Something went wrong and our argv was corrupted.  */
278   _exit (1);
279 }
280
281 #define TEST_FUNCTION_ARGV do_test_prep
282 #include <support/test-driver.c>