1 /* Copyright (C) 2012-2016 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. */
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. */
45 int ret
= getgroups (count
, groups
);
48 printf ("getgroups: %m\n");
51 gid_t current
= getgid ();
52 for (int i
= 0; i
< ret
; ++i
)
54 if (groups
[i
] != current
)
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
65 run_executable_sgid (gid_t target
)
72 if (asprintf (&dirname
, "%s/secure-getenv.%jd",
73 test_dir
, (intmax_t) getpid ()) < 0)
75 printf ("asprintf: %m\n");
78 if (mkdir (dirname
, 0700) < 0)
80 printf ("mkdir: %m\n");
83 if (asprintf (&execname
, "%s/bin", dirname
) < 0)
85 printf ("asprintf: %m\n");
88 infd
= open ("/proc/self/exe", O_RDONLY
);
91 printf ("open (/proc/self/exe): %m\n");
94 outfd
= open (execname
, O_WRONLY
| O_CREAT
| O_EXCL
, 0700);
97 printf ("open (%s): %m\n", execname
);
103 ssize_t rdcount
= read (infd
, buf
, sizeof (buf
));
106 printf ("read: %m\n");
112 char *end
= buf
+ rdcount
;
115 ssize_t wrcount
= write (outfd
, buf
, end
- p
);
120 printf ("write: %m\n");
126 if (fchown (outfd
, getuid (), target
) < 0)
128 printf ("fchown (%s): %m\n", execname
);
131 if (fchmod (outfd
, 02750) < 0)
133 printf ("fchmod (%s): %m\n", execname
);
136 if (close (outfd
) < 0)
138 printf ("close (outfd): %m\n");
141 if (close (infd
) < 0)
143 printf ("close (infd): %m\n");
150 printf ("fork: %m\n");
156 char *args
[] = { execname
, MAGIC_ARGUMENT
, NULL
};
157 execve (execname
, args
, environ
);
158 printf ("execve (%s): %m\n", execname
);
162 if (waitpid (kid
, &status
, 0) < 0)
164 printf ("waitpid: %m\n");
167 if (!WIFEXITED (status
) || WEXITSTATUS (status
) != MAGIC_STATUS
)
169 printf ("Unexpected exit status %d from child process\n",
196 if (getenv ("PATH") == NULL
)
198 printf ("PATH not set\n");
201 if (secure_getenv ("PATH") == NULL
)
203 printf ("PATH not set according to secure_getenv\n");
206 if (strcmp (getenv ("PATH"), secure_getenv ("PATH")) != 0)
208 printf ("PATH mismatch (%s, %s)\n",
209 getenv ("PATH"), secure_getenv ("PATH"));
213 gid_t target
= choose_gid ();
217 "Could not find a suitable GID for user %jd, skipping test\n",
218 (intmax_t) getuid ());
221 return run_executable_sgid (target
);
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 ());
236 if (getenv ("PATH") == NULL
)
238 printf ("PATH variable not present\n");
241 if (secure_getenv ("PATH") != NULL
)
243 printf ("PATH variable not filtered out\n");
250 #define PREPARE(argc, argv) alternative_main(argc, argv)
251 #define TEST_FUNCTION do_test ()
252 #include "../test-skeleton.c"