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