Add a test case for [BZ #23196]
[glibc.git] / stdlib / tst-secure-getenv.c
bloba682b7493e41f20012bb28cd23ea3b79a831ca44
1 /* Copyright (C) 2012-2018 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 #include <support/support.h>
34 #include <support/test-driver.h>
36 static char MAGIC_ARGUMENT[] = "run-actual-test";
37 #define MAGIC_STATUS 19
39 /* Return a GID which is not our current GID, but is present in the
40 supplementary group list. */
41 static gid_t
42 choose_gid (void)
44 const int count = 64;
45 gid_t groups[count];
46 int ret = getgroups (count, groups);
47 if (ret < 0)
49 printf ("getgroups: %m\n");
50 exit (1);
52 gid_t current = getgid ();
53 for (int i = 0; i < ret; ++i)
55 if (groups[i] != current)
56 return groups[i];
58 return 0;
62 /* Copies the executable into a restricted directory, so that we can
63 safely make it SGID with the TARGET group ID. Then runs the
64 executable. */
65 static int
66 run_executable_sgid (gid_t target)
68 char *dirname = xasprintf ("%s/secure-getenv.%jd",
69 test_dir, (intmax_t) getpid ());
70 char *execname = xasprintf ("%s/bin", dirname);
71 int infd = -1;
72 int outfd = -1;
73 int ret = -1;
74 if (mkdir (dirname, 0700) < 0)
76 printf ("mkdir: %m\n");
77 goto err;
79 infd = open ("/proc/self/exe", O_RDONLY);
80 if (infd < 0)
82 printf ("open (/proc/self/exe): %m\n");
83 goto err;
85 outfd = open (execname, O_WRONLY | O_CREAT | O_EXCL, 0700);
86 if (outfd < 0)
88 printf ("open (%s): %m\n", execname);
89 goto err;
91 char buf[4096];
92 for (;;)
94 ssize_t rdcount = read (infd, buf, sizeof (buf));
95 if (rdcount < 0)
97 printf ("read: %m\n");
98 goto err;
100 if (rdcount == 0)
101 break;
102 char *p = buf;
103 char *end = buf + rdcount;
104 while (p != end)
106 ssize_t wrcount = write (outfd, buf, end - p);
107 if (wrcount == 0)
108 errno = ENOSPC;
109 if (wrcount <= 0)
111 printf ("write: %m\n");
112 goto err;
114 p += wrcount;
117 if (fchown (outfd, getuid (), target) < 0)
119 printf ("fchown (%s): %m\n", execname);
120 goto err;
122 if (fchmod (outfd, 02750) < 0)
124 printf ("fchmod (%s): %m\n", execname);
125 goto err;
127 if (close (outfd) < 0)
129 printf ("close (outfd): %m\n");
130 goto err;
132 if (close (infd) < 0)
134 printf ("close (infd): %m\n");
135 goto err;
138 int kid = fork ();
139 if (kid < 0)
141 printf ("fork: %m\n");
142 goto err;
144 if (kid == 0)
146 /* Child process. */
147 char *args[] = { execname, MAGIC_ARGUMENT, NULL };
148 execve (execname, args, environ);
149 printf ("execve (%s): %m\n", execname);
150 _exit (1);
152 int status;
153 if (waitpid (kid, &status, 0) < 0)
155 printf ("waitpid: %m\n");
156 goto err;
158 if (!WIFEXITED (status) || WEXITSTATUS (status) != MAGIC_STATUS)
160 printf ("Unexpected exit status %d from child process\n",
161 status);
162 goto err;
164 ret = 0;
166 err:
167 if (outfd >= 0)
168 close (outfd);
169 if (infd >= 0)
170 close (infd);
171 if (execname)
173 unlink (execname);
174 free (execname);
176 if (dirname)
178 rmdir (dirname);
179 free (dirname);
181 return ret;
184 static int
185 do_test (void)
187 if (getenv ("PATH") == NULL)
189 printf ("PATH not set\n");
190 exit (1);
192 if (secure_getenv ("PATH") == NULL)
194 printf ("PATH not set according to secure_getenv\n");
195 exit (1);
197 if (strcmp (getenv ("PATH"), secure_getenv ("PATH")) != 0)
199 printf ("PATH mismatch (%s, %s)\n",
200 getenv ("PATH"), secure_getenv ("PATH"));
201 exit (1);
204 gid_t target = choose_gid ();
205 if (target == 0)
207 fprintf (stderr,
208 "Could not find a suitable GID for user %jd, skipping test\n",
209 (intmax_t) getuid ());
210 exit (0);
212 return run_executable_sgid (target);
215 static void
216 alternative_main (int argc, char **argv)
218 if (argc == 2 && strcmp (argv[1], MAGIC_ARGUMENT) == 0)
220 if (getgid () == getegid ())
222 /* This can happen if the file system is mounted nosuid. */
223 fprintf (stderr, "SGID failed: GID and EGID match (%jd)\n",
224 (intmax_t) getgid ());
225 exit (MAGIC_STATUS);
227 if (getenv ("PATH") == NULL)
229 printf ("PATH variable not present\n");
230 exit (3);
232 if (secure_getenv ("PATH") != NULL)
234 printf ("PATH variable not filtered out\n");
235 exit (4);
237 exit (MAGIC_STATUS);
241 #define PREPARE alternative_main
242 #include <support/test-driver.c>