1 /* Copyright (C) 2012-2021 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 <https://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 (WEXITSTATUS (status) == EXIT_UNSUPPORTED)
91 return EXIT_UNSUPPORTED;
93 if (!WIFEXITED (status) || WEXITSTATUS (status) != CHILD_STATUS)
95 printf ("Unexpected exit status %d from child process\n",
96 WEXITSTATUS (status));
102 /* Copies the executable into a restricted directory, so that we can
103 safely make it SGID with the TARGET group ID. Then runs the
106 run_executable_sgid (gid_t target)
108 char *dirname = xasprintf ("%s/tst-tunables-setuid.%jd",
109 test_dir, (intmax_t) getpid ());
110 char *execname = xasprintf ("%s/bin", dirname);
114 if (mkdir (dirname, 0700) < 0)
116 printf ("mkdir: %m\n");
119 infd = open ("/proc/self/exe", O_RDONLY);
122 printf ("open (/proc/self/exe): %m\n");
125 outfd = open (execname, O_WRONLY | O_CREAT | O_EXCL, 0700);
128 printf ("open (%s): %m\n", execname);
134 ssize_t rdcount = read (infd, buf, sizeof (buf));
137 printf ("read: %m\n");
143 char *end = buf + rdcount;
146 ssize_t wrcount = write (outfd, buf, end - p);
151 printf ("write: %m\n");
157 if (fchown (outfd, getuid (), target) < 0)
159 printf ("fchown (%s): %m\n", execname);
162 if (fchmod (outfd, 02750) < 0)
164 printf ("fchmod (%s): %m\n", execname);
167 if (close (outfd) < 0)
169 printf ("close (outfd): %m\n");
172 if (close (infd) < 0)
174 printf ("close (infd): %m\n");
178 char *args[] = {execname, SETGID_CHILD, NULL};
180 ret = do_execve (args);
204 if (getenv ("MALLOC_CHECK_") != NULL)
206 printf ("MALLOC_CHECK_ is still set\n");
210 if (getenv ("MALLOC_MMAP_THRESHOLD_") == NULL)
212 printf ("MALLOC_MMAP_THRESHOLD_ lost\n");
216 if (getenv ("LD_HWCAP_MASK") != NULL)
218 printf ("LD_HWCAP_MASK still set\n");
230 if (getenv ("MALLOC_CHECK_") == NULL)
232 printf ("MALLOC_CHECK_ lost\n");
236 if (getenv ("MALLOC_MMAP_THRESHOLD_") == NULL)
238 printf ("MALLOC_MMAP_THRESHOLD_ lost\n");
242 if (getenv ("LD_HWCAP_MASK") == NULL)
244 printf ("LD_HWCAP_MASK lost\n");
253 do_test (int argc, char **argv)
255 /* Setgid child process. */
256 if (argc == 2 && strcmp (argv[1], SETGID_CHILD) == 0)
258 if (getgid () == getegid ())
260 /* This can happen if the file system is mounted nosuid. */
261 fprintf (stderr, "SGID failed: GID and EGID match (%jd)\n",
262 (intmax_t) getgid ());
263 exit (EXIT_UNSUPPORTED);
266 int ret = test_child ();
275 if (test_parent () != 0)
278 /* Try running a setgid program. */
279 gid_t target = choose_gid ();
283 "Could not find a suitable GID for user %jd, skipping test\n",
284 (intmax_t) getuid ());
288 return run_executable_sgid (target);
291 /* Something went wrong and our argv was corrupted. */
295 #define TEST_FUNCTION_ARGV do_test
296 #include <support/test-driver.c>