1 /* Copyright (C) 2012-2019 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 /* 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. */
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. */
44 int count
= getgroups (0, NULL
);
47 printf ("getgroups: %m\n");
51 groups
= xcalloc (count
, sizeof (*groups
));
52 int ret
= getgroups (count
, groups
);
55 printf ("getgroups: %m\n");
58 gid_t current
= getgid ();
59 gid_t not_current
= 0;
60 for (int i
= 0; i
< ret
; ++i
)
62 if (groups
[i
] != current
)
64 not_current
= groups
[i
];
73 /* Copies the executable into a restricted directory, so that we can
74 safely make it SGID with the TARGET group ID. Then runs the
77 run_executable_sgid (gid_t target
)
79 char *dirname
= xasprintf ("%s/secure-getenv.%jd",
80 test_dir
, (intmax_t) getpid ());
81 char *execname
= xasprintf ("%s/bin", dirname
);
85 if (mkdir (dirname
, 0700) < 0)
87 printf ("mkdir: %m\n");
90 infd
= open ("/proc/self/exe", O_RDONLY
);
93 printf ("open (/proc/self/exe): %m\n");
96 outfd
= open (execname
, O_WRONLY
| O_CREAT
| O_EXCL
, 0700);
99 printf ("open (%s): %m\n", execname
);
105 ssize_t rdcount
= read (infd
, buf
, sizeof (buf
));
108 printf ("read: %m\n");
114 char *end
= buf
+ rdcount
;
117 ssize_t wrcount
= write (outfd
, buf
, end
- p
);
122 printf ("write: %m\n");
128 if (fchown (outfd
, getuid (), target
) < 0)
130 printf ("fchown (%s): %m\n", execname
);
133 if (fchmod (outfd
, 02750) < 0)
135 printf ("fchmod (%s): %m\n", execname
);
138 if (close (outfd
) < 0)
140 printf ("close (outfd): %m\n");
143 if (close (infd
) < 0)
145 printf ("close (infd): %m\n");
152 printf ("fork: %m\n");
158 char *args
[] = { execname
, MAGIC_ARGUMENT
, NULL
};
159 execve (execname
, args
, environ
);
160 printf ("execve (%s): %m\n", execname
);
164 if (waitpid (kid
, &status
, 0) < 0)
166 printf ("waitpid: %m\n");
169 if (!WIFEXITED (status
) || WEXITSTATUS (status
) != MAGIC_STATUS
)
171 printf ("Unexpected exit status %d from child process\n",
198 if (getenv ("PATH") == NULL
)
200 printf ("PATH not set\n");
203 if (secure_getenv ("PATH") == NULL
)
205 printf ("PATH not set according to secure_getenv\n");
208 if (strcmp (getenv ("PATH"), secure_getenv ("PATH")) != 0)
210 printf ("PATH mismatch (%s, %s)\n",
211 getenv ("PATH"), secure_getenv ("PATH"));
215 gid_t target
= choose_gid ();
219 "Could not find a suitable GID for user %jd, skipping test\n",
220 (intmax_t) getuid ());
223 return run_executable_sgid (target
);
227 alternative_main (int argc
, char **argv
)
229 if (argc
== 2 && strcmp (argv
[1], MAGIC_ARGUMENT
) == 0)
231 if (getgid () == getegid ())
233 /* This can happen if the file system is mounted nosuid. */
234 fprintf (stderr
, "SGID failed: GID and EGID match (%jd)\n",
235 (intmax_t) getgid ());
238 if (getenv ("PATH") == NULL
)
240 printf ("PATH variable not present\n");
243 if (secure_getenv ("PATH") != NULL
)
245 printf ("PATH variable not filtered out\n");
252 #define PREPARE alternative_main
253 #include <support/test-driver.c>