1 /* Copyright (C) 2012-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
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. */
32 #include <support/support.h>
33 #include <support/test-driver.h>
35 static char SETGID_CHILD[] = "setgid-child";
36 #define CHILD_STATUS 42
38 /* Return a GID which is not our current GID, but is present in the
39 supplementary group list. */
45 int ret = getgroups (count, groups);
48 printf ("getgroups: %m\n");
51 gid_t current = getgid ();
52 for (int i = 0; i < ret; ++i)
54 if (groups[i] != current)
60 /* Spawn and execute a program and verify that it returns the CHILD_STATUS. */
62 do_execve (char **args)
68 printf ("vfork: %m\n");
75 execve (args[0], args, environ);
84 if (waitpid (kid, &status, 0) < 0)
86 printf ("waitpid: %m\n");
90 if (!WIFEXITED (status) || WEXITSTATUS (status) != CHILD_STATUS)
92 printf ("Unexpected exit status %d from child process\n",
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
103 run_executable_sgid (gid_t target)
105 char *dirname = xasprintf ("%s/tst-tunables-setuid.%jd",
106 test_dir, (intmax_t) getpid ());
107 char *execname = xasprintf ("%s/bin", dirname);
111 if (mkdir (dirname, 0700) < 0)
113 printf ("mkdir: %m\n");
116 infd = open ("/proc/self/exe", O_RDONLY);
119 printf ("open (/proc/self/exe): %m\n");
122 outfd = open (execname, O_WRONLY | O_CREAT | O_EXCL, 0700);
125 printf ("open (%s): %m\n", execname);
131 ssize_t rdcount = read (infd, buf, sizeof (buf));
134 printf ("read: %m\n");
140 char *end = buf + rdcount;
143 ssize_t wrcount = write (outfd, buf, end - p);
148 printf ("write: %m\n");
154 if (fchown (outfd, getuid (), target) < 0)
156 printf ("fchown (%s): %m\n", execname);
159 if (fchmod (outfd, 02750) < 0)
161 printf ("fchmod (%s): %m\n", execname);
164 if (close (outfd) < 0)
166 printf ("close (outfd): %m\n");
169 if (close (infd) < 0)
171 printf ("close (infd): %m\n");
175 char *args[] = {execname, SETGID_CHILD, NULL};
177 ret = do_execve (args);
201 if (getenv ("MALLOC_CHECK_") != NULL)
203 printf ("MALLOC_CHECK_ is still set\n");
207 if (getenv ("MALLOC_MMAP_THRESHOLD_") == NULL)
209 printf ("MALLOC_MMAP_THRESHOLD_ lost\n");
221 if (getenv ("MALLOC_CHECK_") == NULL)
223 printf ("MALLOC_CHECK_ lost\n");
227 if (getenv ("MALLOC_MMAP_THRESHOLD_") == NULL)
229 printf ("MALLOC_MMAP_THRESHOLD_ lost\n");
238 do_test_prep (int argc, char **argv)
240 /* Setgid child process. */
241 if (argc == 2 && strcmp (argv[1], SETGID_CHILD) == 0)
243 if (getgid () == getegid ())
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);
251 int ret = test_child ();
260 if (test_parent () != 0)
263 /* Try running a setgid program. */
264 gid_t target = choose_gid ();
268 "Could not find a suitable GID for user %jd, skipping test\n",
269 (intmax_t) getuid ());
273 if (run_executable_sgid (target) == 0)
277 /* Something went wrong and our argv was corrupted. */
281 #define TEST_FUNCTION_ARGV do_test_prep
282 #include <support/test-driver.c>