Consolidate non cancellable writev call
[glibc.git] / elf / tst-env-setuid.c
blobeec408eb5db6f371cc67f4e40ae67160fd032dfd
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. */
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>
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. */
40 static gid_t
41 choose_gid (void)
43 const int count = 64;
44 gid_t groups[count];
45 int ret = getgroups (count, groups);
46 if (ret < 0)
48 printf ("getgroups: %m\n");
49 exit (1);
51 gid_t current = getgid ();
52 for (int i = 0; i < ret; ++i)
54 if (groups[i] != current)
55 return groups[i];
57 return 0;
60 /* Spawn and execute a program and verify that it returns the CHILD_STATUS. */
61 static pid_t
62 do_execve (char **args)
64 pid_t kid = vfork ();
66 if (kid < 0)
68 printf ("vfork: %m\n");
69 return -1;
72 if (kid == 0)
74 /* Child process. */
75 execve (args[0], args, environ);
76 _exit (-errno);
79 if (kid < 0)
80 return 1;
82 int status;
84 if (waitpid (kid, &status, 0) < 0)
86 printf ("waitpid: %m\n");
87 return 1;
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));
97 return 1;
99 return 0;
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
104 executable. */
105 static int
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);
111 int infd = -1;
112 int outfd = -1;
113 int ret = 0;
114 if (mkdir (dirname, 0700) < 0)
116 printf ("mkdir: %m\n");
117 goto err;
119 infd = open ("/proc/self/exe", O_RDONLY);
120 if (infd < 0)
122 printf ("open (/proc/self/exe): %m\n");
123 goto err;
125 outfd = open (execname, O_WRONLY | O_CREAT | O_EXCL, 0700);
126 if (outfd < 0)
128 printf ("open (%s): %m\n", execname);
129 goto err;
131 char buf[4096];
132 for (;;)
134 ssize_t rdcount = read (infd, buf, sizeof (buf));
135 if (rdcount < 0)
137 printf ("read: %m\n");
138 goto err;
140 if (rdcount == 0)
141 break;
142 char *p = buf;
143 char *end = buf + rdcount;
144 while (p != end)
146 ssize_t wrcount = write (outfd, buf, end - p);
147 if (wrcount == 0)
148 errno = ENOSPC;
149 if (wrcount <= 0)
151 printf ("write: %m\n");
152 goto err;
154 p += wrcount;
157 if (fchown (outfd, getuid (), target) < 0)
159 printf ("fchown (%s): %m\n", execname);
160 goto err;
162 if (fchmod (outfd, 02750) < 0)
164 printf ("fchmod (%s): %m\n", execname);
165 goto err;
167 if (close (outfd) < 0)
169 printf ("close (outfd): %m\n");
170 goto err;
172 if (close (infd) < 0)
174 printf ("close (infd): %m\n");
175 goto err;
178 char *args[] = {execname, SETGID_CHILD, NULL};
180 ret = do_execve (args);
182 err:
183 if (outfd >= 0)
184 close (outfd);
185 if (infd >= 0)
186 close (infd);
187 if (execname)
189 unlink (execname);
190 free (execname);
192 if (dirname)
194 rmdir (dirname);
195 free (dirname);
197 return ret;
200 #ifndef test_child
201 static int
202 test_child (void)
204 if (getenv ("MALLOC_CHECK_") != NULL)
206 printf ("MALLOC_CHECK_ is still set\n");
207 return 1;
210 if (getenv ("MALLOC_MMAP_THRESHOLD_") == NULL)
212 printf ("MALLOC_MMAP_THRESHOLD_ lost\n");
213 return 1;
216 if (getenv ("LD_HWCAP_MASK") != NULL)
218 printf ("LD_HWCAP_MASK still set\n");
219 return 1;
222 return 0;
224 #endif
226 #ifndef test_parent
227 static int
228 test_parent (void)
230 if (getenv ("MALLOC_CHECK_") == NULL)
232 printf ("MALLOC_CHECK_ lost\n");
233 return 1;
236 if (getenv ("MALLOC_MMAP_THRESHOLD_") == NULL)
238 printf ("MALLOC_MMAP_THRESHOLD_ lost\n");
239 return 1;
242 if (getenv ("LD_HWCAP_MASK") == NULL)
244 printf ("LD_HWCAP_MASK lost\n");
245 return 1;
248 return 0;
250 #endif
252 static int
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 ();
268 if (ret != 0)
269 exit (1);
271 exit (CHILD_STATUS);
273 else
275 if (test_parent () != 0)
276 exit (1);
278 /* Try running a setgid program. */
279 gid_t target = choose_gid ();
280 if (target == 0)
282 fprintf (stderr,
283 "Could not find a suitable GID for user %jd, skipping test\n",
284 (intmax_t) getuid ());
285 exit (0);
288 return run_executable_sgid (target);
291 /* Something went wrong and our argv was corrupted. */
292 _exit (1);
295 #define TEST_FUNCTION_ARGV do_test
296 #include <support/test-driver.c>