tst-secure-getenv.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* Copyright (C) 2012-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. /* Test that secure_getenv works by invoking the test as a SGID
  15. program with a group ID from the supplementary group list. This
  16. test can fail spuriously if the user is not a member of a suitable
  17. supplementary group. */
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <stdlib.h>
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. #include <sys/wait.h>
  26. #include <unistd.h>
  27. #include <support/support.h>
  28. #include <support/test-driver.h>
  29. static char MAGIC_ARGUMENT[] = "run-actual-test";
  30. #define MAGIC_STATUS 19
  31. /* Return a GID which is not our current GID, but is present in the
  32. supplementary group list. */
  33. static gid_t
  34. choose_gid (void)
  35. {
  36. const int count = 64;
  37. gid_t groups[count];
  38. int ret = getgroups (count, groups);
  39. if (ret < 0)
  40. {
  41. printf ("getgroups: %m\n");
  42. exit (1);
  43. }
  44. gid_t current = getgid ();
  45. for (int i = 0; i < ret; ++i)
  46. {
  47. if (groups[i] != current)
  48. return groups[i];
  49. }
  50. return 0;
  51. }
  52. /* Copies the executable into a restricted directory, so that we can
  53. safely make it SGID with the TARGET group ID. Then runs the
  54. executable. */
  55. static int
  56. run_executable_sgid (gid_t target)
  57. {
  58. char *dirname = xasprintf ("%s/secure-getenv.%jd",
  59. test_dir, (intmax_t) getpid ());
  60. char *execname = xasprintf ("%s/bin", dirname);
  61. int infd = -1;
  62. int outfd = -1;
  63. int ret = -1;
  64. if (mkdir (dirname, 0700) < 0)
  65. {
  66. printf ("mkdir: %m\n");
  67. goto err;
  68. }
  69. infd = open ("/proc/self/exe", O_RDONLY);
  70. if (infd < 0)
  71. {
  72. printf ("open (/proc/self/exe): %m\n");
  73. goto err;
  74. }
  75. outfd = open (execname, O_WRONLY | O_CREAT | O_EXCL, 0700);
  76. if (outfd < 0)
  77. {
  78. printf ("open (%s): %m\n", execname);
  79. goto err;
  80. }
  81. char buf[4096];
  82. for (;;)
  83. {
  84. ssize_t rdcount = read (infd, buf, sizeof (buf));
  85. if (rdcount < 0)
  86. {
  87. printf ("read: %m\n");
  88. goto err;
  89. }
  90. if (rdcount == 0)
  91. break;
  92. char *p = buf;
  93. char *end = buf + rdcount;
  94. while (p != end)
  95. {
  96. ssize_t wrcount = write (outfd, buf, end - p);
  97. if (wrcount == 0)
  98. errno = ENOSPC;
  99. if (wrcount <= 0)
  100. {
  101. printf ("write: %m\n");
  102. goto err;
  103. }
  104. p += wrcount;
  105. }
  106. }
  107. if (fchown (outfd, getuid (), target) < 0)
  108. {
  109. printf ("fchown (%s): %m\n", execname);
  110. goto err;
  111. }
  112. if (fchmod (outfd, 02750) < 0)
  113. {
  114. printf ("fchmod (%s): %m\n", execname);
  115. goto err;
  116. }
  117. if (close (outfd) < 0)
  118. {
  119. printf ("close (outfd): %m\n");
  120. goto err;
  121. }
  122. if (close (infd) < 0)
  123. {
  124. printf ("close (infd): %m\n");
  125. goto err;
  126. }
  127. int kid = fork ();
  128. if (kid < 0)
  129. {
  130. printf ("fork: %m\n");
  131. goto err;
  132. }
  133. if (kid == 0)
  134. {
  135. /* Child process. */
  136. char *args[] = { execname, MAGIC_ARGUMENT, NULL };
  137. execve (execname, args, environ);
  138. printf ("execve (%s): %m\n", execname);
  139. _exit (1);
  140. }
  141. int status;
  142. if (waitpid (kid, &status, 0) < 0)
  143. {
  144. printf ("waitpid: %m\n");
  145. goto err;
  146. }
  147. if (!WIFEXITED (status) || WEXITSTATUS (status) != MAGIC_STATUS)
  148. {
  149. printf ("Unexpected exit status %d from child process\n",
  150. status);
  151. goto err;
  152. }
  153. ret = 0;
  154. err:
  155. if (outfd >= 0)
  156. close (outfd);
  157. if (infd >= 0)
  158. close (infd);
  159. if (execname)
  160. {
  161. unlink (execname);
  162. free (execname);
  163. }
  164. if (dirname)
  165. {
  166. rmdir (dirname);
  167. free (dirname);
  168. }
  169. return ret;
  170. }
  171. static int
  172. do_test (void)
  173. {
  174. if (getenv ("PATH") == NULL)
  175. {
  176. printf ("PATH not set\n");
  177. exit (1);
  178. }
  179. if (secure_getenv ("PATH") == NULL)
  180. {
  181. printf ("PATH not set according to secure_getenv\n");
  182. exit (1);
  183. }
  184. if (strcmp (getenv ("PATH"), secure_getenv ("PATH")) != 0)
  185. {
  186. printf ("PATH mismatch (%s, %s)\n",
  187. getenv ("PATH"), secure_getenv ("PATH"));
  188. exit (1);
  189. }
  190. gid_t target = choose_gid ();
  191. if (target == 0)
  192. {
  193. fprintf (stderr,
  194. "Could not find a suitable GID for user %jd, skipping test\n",
  195. (intmax_t) getuid ());
  196. exit (0);
  197. }
  198. return run_executable_sgid (target);
  199. }
  200. static void
  201. alternative_main (int argc, char **argv)
  202. {
  203. if (argc == 2 && strcmp (argv[1], MAGIC_ARGUMENT) == 0)
  204. {
  205. if (getgid () == getegid ())
  206. {
  207. /* This can happen if the file system is mounted nosuid. */
  208. fprintf (stderr, "SGID failed: GID and EGID match (%jd)\n",
  209. (intmax_t) getgid ());
  210. exit (MAGIC_STATUS);
  211. }
  212. if (getenv ("PATH") == NULL)
  213. {
  214. printf ("PATH variable not present\n");
  215. exit (3);
  216. }
  217. if (secure_getenv ("PATH") != NULL)
  218. {
  219. printf ("PATH variable not filtered out\n");
  220. exit (4);
  221. }
  222. exit (MAGIC_STATUS);
  223. }
  224. }
  225. #define PREPARE alternative_main
  226. #include <support/test-driver.c>