|
|
a35d80 |
tst-env-setuid: Use support_capture_subprogram_self_sgid
|
|
|
a35d80 |
|
|
|
a35d80 |
Use the support_capture_subprogram_self_sgid to spawn an sgid child.
|
|
|
a35d80 |
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
|
|
|
a35d80 |
|
|
|
a35d80 |
(cherry picked from commit ca335281068a1ed549a75ee64f90a8310755956f)
|
|
|
a35d80 |
|
|
|
a35d80 |
diff --git a/elf/tst-env-setuid.c b/elf/tst-env-setuid.c
|
|
|
a35d80 |
index 183a6dd133cfa16e..eda87f9dda293e79 100644
|
|
|
a35d80 |
--- a/elf/tst-env-setuid.c
|
|
|
a35d80 |
+++ b/elf/tst-env-setuid.c
|
|
|
a35d80 |
@@ -29,173 +29,12 @@
|
|
|
a35d80 |
#include <sys/wait.h>
|
|
|
a35d80 |
#include <unistd.h>
|
|
|
a35d80 |
|
|
|
a35d80 |
+#include <support/check.h>
|
|
|
a35d80 |
#include <support/support.h>
|
|
|
a35d80 |
#include <support/test-driver.h>
|
|
|
a35d80 |
+#include <support/capture_subprocess.h>
|
|
|
a35d80 |
|
|
|
a35d80 |
static char SETGID_CHILD[] = "setgid-child";
|
|
|
a35d80 |
-#define CHILD_STATUS 42
|
|
|
a35d80 |
-
|
|
|
a35d80 |
-/* Return a GID which is not our current GID, but is present in the
|
|
|
a35d80 |
- supplementary group list. */
|
|
|
a35d80 |
-static gid_t
|
|
|
a35d80 |
-choose_gid (void)
|
|
|
a35d80 |
-{
|
|
|
a35d80 |
- const int count = 64;
|
|
|
a35d80 |
- gid_t groups[count];
|
|
|
a35d80 |
- int ret = getgroups (count, groups);
|
|
|
a35d80 |
- if (ret < 0)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- printf ("getgroups: %m\n");
|
|
|
a35d80 |
- exit (1);
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
- gid_t current = getgid ();
|
|
|
a35d80 |
- for (int i = 0; i < ret; ++i)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- if (groups[i] != current)
|
|
|
a35d80 |
- return groups[i];
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
- return 0;
|
|
|
a35d80 |
-}
|
|
|
a35d80 |
-
|
|
|
a35d80 |
-/* Spawn and execute a program and verify that it returns the CHILD_STATUS. */
|
|
|
a35d80 |
-static pid_t
|
|
|
a35d80 |
-do_execve (char **args)
|
|
|
a35d80 |
-{
|
|
|
a35d80 |
- pid_t kid = vfork ();
|
|
|
a35d80 |
-
|
|
|
a35d80 |
- if (kid < 0)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- printf ("vfork: %m\n");
|
|
|
a35d80 |
- return -1;
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
-
|
|
|
a35d80 |
- if (kid == 0)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- /* Child process. */
|
|
|
a35d80 |
- execve (args[0], args, environ);
|
|
|
a35d80 |
- _exit (-errno);
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
-
|
|
|
a35d80 |
- if (kid < 0)
|
|
|
a35d80 |
- return 1;
|
|
|
a35d80 |
-
|
|
|
a35d80 |
- int status;
|
|
|
a35d80 |
-
|
|
|
a35d80 |
- if (waitpid (kid, &status, 0) < 0)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- printf ("waitpid: %m\n");
|
|
|
a35d80 |
- return 1;
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
-
|
|
|
a35d80 |
- if (WEXITSTATUS (status) == EXIT_UNSUPPORTED)
|
|
|
a35d80 |
- return EXIT_UNSUPPORTED;
|
|
|
a35d80 |
-
|
|
|
a35d80 |
- if (!WIFEXITED (status) || WEXITSTATUS (status) != CHILD_STATUS)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- printf ("Unexpected exit status %d from child process\n",
|
|
|
a35d80 |
- WEXITSTATUS (status));
|
|
|
a35d80 |
- return 1;
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
- return 0;
|
|
|
a35d80 |
-}
|
|
|
a35d80 |
-
|
|
|
a35d80 |
-/* Copies the executable into a restricted directory, so that we can
|
|
|
a35d80 |
- safely make it SGID with the TARGET group ID. Then runs the
|
|
|
a35d80 |
- executable. */
|
|
|
a35d80 |
-static int
|
|
|
a35d80 |
-run_executable_sgid (gid_t target)
|
|
|
a35d80 |
-{
|
|
|
a35d80 |
- char *dirname = xasprintf ("%s/tst-tunables-setuid.%jd",
|
|
|
a35d80 |
- test_dir, (intmax_t) getpid ());
|
|
|
a35d80 |
- char *execname = xasprintf ("%s/bin", dirname);
|
|
|
a35d80 |
- int infd = -1;
|
|
|
a35d80 |
- int outfd = -1;
|
|
|
a35d80 |
- int ret = 0;
|
|
|
a35d80 |
- if (mkdir (dirname, 0700) < 0)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- printf ("mkdir: %m\n");
|
|
|
a35d80 |
- goto err;
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
- infd = open ("/proc/self/exe", O_RDONLY);
|
|
|
a35d80 |
- if (infd < 0)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- printf ("open (/proc/self/exe): %m\n");
|
|
|
a35d80 |
- goto err;
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
- outfd = open (execname, O_WRONLY | O_CREAT | O_EXCL, 0700);
|
|
|
a35d80 |
- if (outfd < 0)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- printf ("open (%s): %m\n", execname);
|
|
|
a35d80 |
- goto err;
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
- char buf[4096];
|
|
|
a35d80 |
- for (;;)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- ssize_t rdcount = read (infd, buf, sizeof (buf));
|
|
|
a35d80 |
- if (rdcount < 0)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- printf ("read: %m\n");
|
|
|
a35d80 |
- goto err;
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
- if (rdcount == 0)
|
|
|
a35d80 |
- break;
|
|
|
a35d80 |
- char *p = buf;
|
|
|
a35d80 |
- char *end = buf + rdcount;
|
|
|
a35d80 |
- while (p != end)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- ssize_t wrcount = write (outfd, buf, end - p);
|
|
|
a35d80 |
- if (wrcount == 0)
|
|
|
a35d80 |
- errno = ENOSPC;
|
|
|
a35d80 |
- if (wrcount <= 0)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- printf ("write: %m\n");
|
|
|
a35d80 |
- goto err;
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
- p += wrcount;
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
- if (fchown (outfd, getuid (), target) < 0)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- printf ("fchown (%s): %m\n", execname);
|
|
|
a35d80 |
- goto err;
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
- if (fchmod (outfd, 02750) < 0)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- printf ("fchmod (%s): %m\n", execname);
|
|
|
a35d80 |
- goto err;
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
- if (close (outfd) < 0)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- printf ("close (outfd): %m\n");
|
|
|
a35d80 |
- goto err;
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
- if (close (infd) < 0)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- printf ("close (infd): %m\n");
|
|
|
a35d80 |
- goto err;
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
-
|
|
|
a35d80 |
- char *args[] = {execname, SETGID_CHILD, NULL};
|
|
|
a35d80 |
-
|
|
|
a35d80 |
- ret = do_execve (args);
|
|
|
a35d80 |
-
|
|
|
a35d80 |
-err:
|
|
|
a35d80 |
- if (outfd >= 0)
|
|
|
a35d80 |
- close (outfd);
|
|
|
a35d80 |
- if (infd >= 0)
|
|
|
a35d80 |
- close (infd);
|
|
|
a35d80 |
- if (execname)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- unlink (execname);
|
|
|
a35d80 |
- free (execname);
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
- if (dirname)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- rmdir (dirname);
|
|
|
a35d80 |
- free (dirname);
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
- return ret;
|
|
|
a35d80 |
-}
|
|
|
a35d80 |
|
|
|
a35d80 |
#ifndef test_child
|
|
|
a35d80 |
static int
|
|
|
a35d80 |
@@ -256,40 +95,32 @@ do_test (int argc, char **argv)
|
|
|
a35d80 |
if (argc == 2 && strcmp (argv[1], SETGID_CHILD) == 0)
|
|
|
a35d80 |
{
|
|
|
a35d80 |
if (getgid () == getegid ())
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- /* This can happen if the file system is mounted nosuid. */
|
|
|
a35d80 |
- fprintf (stderr, "SGID failed: GID and EGID match (%jd)\n",
|
|
|
a35d80 |
- (intmax_t) getgid ());
|
|
|
a35d80 |
- exit (EXIT_UNSUPPORTED);
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
+ /* This can happen if the file system is mounted nosuid. */
|
|
|
a35d80 |
+ FAIL_UNSUPPORTED ("SGID failed: GID and EGID match (%jd)\n",
|
|
|
a35d80 |
+ (intmax_t) getgid ());
|
|
|
a35d80 |
|
|
|
a35d80 |
int ret = test_child ();
|
|
|
a35d80 |
|
|
|
a35d80 |
if (ret != 0)
|
|
|
a35d80 |
exit (1);
|
|
|
a35d80 |
|
|
|
a35d80 |
- exit (CHILD_STATUS);
|
|
|
a35d80 |
+ exit (EXIT_SUCCESS);
|
|
|
a35d80 |
}
|
|
|
a35d80 |
else
|
|
|
a35d80 |
{
|
|
|
a35d80 |
if (test_parent () != 0)
|
|
|
a35d80 |
exit (1);
|
|
|
a35d80 |
|
|
|
a35d80 |
- /* Try running a setgid program. */
|
|
|
a35d80 |
- gid_t target = choose_gid ();
|
|
|
a35d80 |
- if (target == 0)
|
|
|
a35d80 |
- {
|
|
|
a35d80 |
- fprintf (stderr,
|
|
|
a35d80 |
- "Could not find a suitable GID for user %jd, skipping test\n",
|
|
|
a35d80 |
- (intmax_t) getuid ());
|
|
|
a35d80 |
- exit (0);
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
+ int status = support_capture_subprogram_self_sgid (SETGID_CHILD);
|
|
|
a35d80 |
|
|
|
a35d80 |
- return run_executable_sgid (target);
|
|
|
a35d80 |
- }
|
|
|
a35d80 |
+ if (WEXITSTATUS (status) == EXIT_UNSUPPORTED)
|
|
|
a35d80 |
+ return EXIT_UNSUPPORTED;
|
|
|
a35d80 |
+
|
|
|
a35d80 |
+ if (!WIFEXITED (status))
|
|
|
a35d80 |
+ FAIL_EXIT1 ("Unexpected exit status %d from child process\n", status);
|
|
|
a35d80 |
|
|
|
a35d80 |
- /* Something went wrong and our argv was corrupted. */
|
|
|
a35d80 |
- _exit (1);
|
|
|
a35d80 |
+ return 0;
|
|
|
a35d80 |
+ }
|
|
|
a35d80 |
}
|
|
|
a35d80 |
|
|
|
a35d80 |
#define TEST_FUNCTION_ARGV do_test
|