Cleanup fesetexceptflag to use the same logic as the ARM version. No functional changes.
[glibc.git] / stdlib / tst-secure-getenv.c
blobe5da404b8235c08b4055c897d426cadbeaac927a
1 /* Copyright (C) 2012-2014 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 /* Test that secure_getenv works by invoking the test as a SGID
19 program with a group ID from the supplementary group list. This
20 test can fail spuriously if the user is not a member of a suitable
21 supplementary group. */
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
33 static char MAGIC_ARGUMENT[] = "run-actual-test";
34 #define MAGIC_STATUS 19
36 static const char *test_dir;
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;
61 /* Copies the executable into a restricted directory, so that we can
62 safely make it SGID with the TARGET group ID. Then runs the
63 executable. */
64 static int
65 run_executable_sgid (gid_t target)
67 char *dirname = 0;
68 char *execname = 0;
69 int infd = -1;
70 int outfd = -1;
71 int ret = -1;
72 if (asprintf (&dirname, "%s/secure-getenv.%jd",
73 test_dir, (intmax_t) getpid ()) < 0)
75 printf ("asprintf: %m\n");
76 goto err;
78 if (mkdir (dirname, 0700) < 0)
80 printf ("mkdir: %m\n");
81 goto err;
83 if (asprintf (&execname, "%s/bin", dirname) < 0)
85 printf ("asprintf: %m\n");
86 goto err;
88 infd = open ("/proc/self/exe", O_RDONLY);
89 if (infd < 0)
91 printf ("open (/proc/self/exe): %m\n");
92 goto err;
94 outfd = open (execname, O_WRONLY | O_CREAT | O_EXCL, 0700);
95 if (outfd < 0)
97 printf ("open (%s): %m\n", execname);
98 goto err;
100 char buf[4096];
101 for (;;)
103 ssize_t rdcount = read (infd, buf, sizeof (buf));
104 if (rdcount < 0)
106 printf ("read: %m\n");
107 goto err;
109 if (rdcount == 0)
110 break;
111 char *p = buf;
112 char *end = buf + rdcount;
113 while (p != end)
115 ssize_t wrcount = write (outfd, buf, end - p);
116 if (wrcount == 0)
117 errno = ENOSPC;
118 if (wrcount <= 0)
120 printf ("write: %m\n");
121 goto err;
123 p += wrcount;
126 if (fchown (outfd, getuid (), target) < 0)
128 printf ("fchown (%s): %m\n", execname);
129 goto err;
131 if (fchmod (outfd, 02750) < 0)
133 printf ("fchmod (%s): %m\n", execname);
134 goto err;
136 if (close (outfd) < 0)
138 printf ("close (outfd): %m\n");
139 goto err;
141 if (close (infd) < 0)
143 printf ("close (infd): %m\n");
144 goto err;
147 int kid = fork ();
148 if (kid < 0)
150 printf ("fork: %m\n");
151 goto err;
153 if (kid == 0)
155 /* Child process. */
156 char *args[] = { execname, MAGIC_ARGUMENT, NULL };
157 execve (execname, args, environ);
158 printf ("execve (%s): %m\n", execname);
159 _exit (1);
161 int status;
162 if (waitpid (kid, &status, 0) < 0)
164 printf ("waitpid: %m\n");
165 goto err;
167 if (!WIFEXITED (status) || WEXITSTATUS (status) != MAGIC_STATUS)
169 printf ("Unexpected exit status %d from child process\n",
170 status);
171 goto err;
173 ret = 0;
175 err:
176 if (outfd >= 0)
177 close (outfd);
178 if (infd >= 0)
179 close (infd);
180 if (execname)
182 unlink (execname);
183 free (execname);
185 if (dirname)
187 rmdir (dirname);
188 free (dirname);
190 return ret;
193 static int
194 do_test (void)
196 if (getenv ("PATH") == NULL)
198 printf ("PATH not set\n");
199 exit (1);
201 if (secure_getenv ("PATH") == NULL)
203 printf ("PATH not set according to secure_getenv\n");
204 exit (1);
206 if (strcmp (getenv ("PATH"), secure_getenv ("PATH")) != 0)
208 printf ("PATH mismatch (%s, %s)\n",
209 getenv ("PATH"), secure_getenv ("PATH"));
210 exit (1);
213 gid_t target = choose_gid ();
214 if (target == 0)
216 fprintf (stderr,
217 "Could not find a suitable GID for user %jd, skipping test\n",
218 (intmax_t) getuid ());
219 exit (0);
221 return run_executable_sgid (target);
224 static void
225 alternative_main (int argc, char **argv)
227 if (argc == 2 && strcmp (argv[1], MAGIC_ARGUMENT) == 0)
229 if (getgid () == getegid ())
231 /* This can happen if the file system is mounted nosuid. */
232 fprintf (stderr, "SGID failed: GID and EGID match (%jd)\n",
233 (intmax_t) getgid ());
234 exit (MAGIC_STATUS);
236 if (getenv ("PATH") == NULL)
238 printf ("PATH variable not present\n");
239 exit (3);
241 if (secure_getenv ("PATH") != NULL)
243 printf ("PATH variable not filtered out\n");
244 exit (4);
246 exit (MAGIC_STATUS);
250 #define PREPARE(argc, argv) alternative_main(argc, argv)
251 #define TEST_FUNCTION do_test ()
252 #include "../test-skeleton.c"