9bb5d6
support: Add capability to fork an sgid child
9bb5d6
9bb5d6
Add a new function support_capture_subprogram_self_sgid that spawns an
9bb5d6
sgid child of the running program with its own image and returns the
9bb5d6
exit code of the child process.  This functionality is used by at
9bb5d6
least three tests in the testsuite at the moment, so it makes sense to
9bb5d6
consolidate.
9bb5d6
9bb5d6
There is also a new function support_subprogram_wait which should
9bb5d6
provide simple system() like functionality that does not set up file
9bb5d6
actions.  This is useful in cases where only the return code of the
9bb5d6
spawned subprocess is interesting.
9bb5d6
9bb5d6
This patch also ports tst-secure-getenv to this new function.  A
9bb5d6
subsequent patch will port other tests.  This also brings an important
9bb5d6
change to tst-secure-getenv behaviour.  Now instead of succeeding, the
9bb5d6
test fails as UNSUPPORTED if it is unable to spawn a setgid child,
9bb5d6
which is how it should have been in the first place.
9bb5d6
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
9bb5d6
9bb5d6
(cherry picked from commit 716a3bdc41b2b4b864dc64475015ba51e35e1273)
9bb5d6
9bb5d6
diff --git a/stdlib/tst-secure-getenv.c b/stdlib/tst-secure-getenv.c
9bb5d6
index a682b7493e41f200..156c92fea216729f 100644
9bb5d6
--- a/stdlib/tst-secure-getenv.c
9bb5d6
+++ b/stdlib/tst-secure-getenv.c
9bb5d6
@@ -30,156 +30,12 @@
9bb5d6
 #include <sys/wait.h>
9bb5d6
 #include <unistd.h>
9bb5d6
 
9bb5d6
+#include <support/check.h>
9bb5d6
 #include <support/support.h>
9bb5d6
+#include <support/capture_subprocess.h>
9bb5d6
 #include <support/test-driver.h>
9bb5d6
 
9bb5d6
 static char MAGIC_ARGUMENT[] = "run-actual-test";
9bb5d6
-#define MAGIC_STATUS 19
9bb5d6
-
9bb5d6
-/* Return a GID which is not our current GID, but is present in the
9bb5d6
-   supplementary group list.  */
9bb5d6
-static gid_t
9bb5d6
-choose_gid (void)
9bb5d6
-{
9bb5d6
-  const int count = 64;
9bb5d6
-  gid_t groups[count];
9bb5d6
-  int ret = getgroups (count, groups);
9bb5d6
-  if (ret < 0)
9bb5d6
-    {
9bb5d6
-      printf ("getgroups: %m\n");
9bb5d6
-      exit (1);
9bb5d6
-    }
9bb5d6
-  gid_t current = getgid ();
9bb5d6
-  for (int i = 0; i < ret; ++i)
9bb5d6
-    {
9bb5d6
-      if (groups[i] != current)
9bb5d6
-	return groups[i];
9bb5d6
-    }
9bb5d6
-  return 0;
9bb5d6
-}
9bb5d6
-
9bb5d6
-
9bb5d6
-/* Copies the executable into a restricted directory, so that we can
9bb5d6
-   safely make it SGID with the TARGET group ID.  Then runs the
9bb5d6
-   executable.  */
9bb5d6
-static int
9bb5d6
-run_executable_sgid (gid_t target)
9bb5d6
-{
9bb5d6
-  char *dirname = xasprintf ("%s/secure-getenv.%jd",
9bb5d6
-			     test_dir, (intmax_t) getpid ());
9bb5d6
-  char *execname = xasprintf ("%s/bin", dirname);
9bb5d6
-  int infd = -1;
9bb5d6
-  int outfd = -1;
9bb5d6
-  int ret = -1;
9bb5d6
-  if (mkdir (dirname, 0700) < 0)
9bb5d6
-    {
9bb5d6
-      printf ("mkdir: %m\n");
9bb5d6
-      goto err;
9bb5d6
-    }
9bb5d6
-  infd = open ("/proc/self/exe", O_RDONLY);
9bb5d6
-  if (infd < 0)
9bb5d6
-    {
9bb5d6
-      printf ("open (/proc/self/exe): %m\n");
9bb5d6
-      goto err;
9bb5d6
-    }
9bb5d6
-  outfd = open (execname, O_WRONLY | O_CREAT | O_EXCL, 0700);
9bb5d6
-  if (outfd < 0)
9bb5d6
-    {
9bb5d6
-      printf ("open (%s): %m\n", execname);
9bb5d6
-      goto err;
9bb5d6
-    }
9bb5d6
-  char buf[4096];
9bb5d6
-  for (;;)
9bb5d6
-    {
9bb5d6
-      ssize_t rdcount = read (infd, buf, sizeof (buf));
9bb5d6
-      if (rdcount < 0)
9bb5d6
-	{
9bb5d6
-	  printf ("read: %m\n");
9bb5d6
-	  goto err;
9bb5d6
-	}
9bb5d6
-      if (rdcount == 0)
9bb5d6
-	break;
9bb5d6
-      char *p = buf;
9bb5d6
-      char *end = buf + rdcount;
9bb5d6
-      while (p != end)
9bb5d6
-	{
9bb5d6
-	  ssize_t wrcount = write (outfd, buf, end - p);
9bb5d6
-	  if (wrcount == 0)
9bb5d6
-	    errno = ENOSPC;
9bb5d6
-	  if (wrcount <= 0)
9bb5d6
-	    {
9bb5d6
-	      printf ("write: %m\n");
9bb5d6
-	      goto err;
9bb5d6
-	    }
9bb5d6
-	  p += wrcount;
9bb5d6
-	}
9bb5d6
-    }
9bb5d6
-  if (fchown (outfd, getuid (), target) < 0)
9bb5d6
-    {
9bb5d6
-      printf ("fchown (%s): %m\n", execname);
9bb5d6
-      goto err;
9bb5d6
-    }
9bb5d6
-  if (fchmod (outfd, 02750) < 0)
9bb5d6
-    {
9bb5d6
-      printf ("fchmod (%s): %m\n", execname);
9bb5d6
-      goto err;
9bb5d6
-    }
9bb5d6
-  if (close (outfd) < 0)
9bb5d6
-    {
9bb5d6
-      printf ("close (outfd): %m\n");
9bb5d6
-      goto err;
9bb5d6
-    }
9bb5d6
-  if (close (infd) < 0)
9bb5d6
-    {
9bb5d6
-      printf ("close (infd): %m\n");
9bb5d6
-      goto err;
9bb5d6
-    }
9bb5d6
-
9bb5d6
-  int kid = fork ();
9bb5d6
-  if (kid < 0)
9bb5d6
-    {
9bb5d6
-      printf ("fork: %m\n");
9bb5d6
-      goto err;
9bb5d6
-    }
9bb5d6
-  if (kid == 0)
9bb5d6
-    {
9bb5d6
-      /* Child process.  */
9bb5d6
-      char *args[] = { execname, MAGIC_ARGUMENT, NULL };
9bb5d6
-      execve (execname, args, environ);
9bb5d6
-      printf ("execve (%s): %m\n", execname);
9bb5d6
-      _exit (1);
9bb5d6
-    }
9bb5d6
-  int status;
9bb5d6
-  if (waitpid (kid, &status, 0) < 0)
9bb5d6
-    {
9bb5d6
-      printf ("waitpid: %m\n");
9bb5d6
-      goto err;
9bb5d6
-    }
9bb5d6
-  if (!WIFEXITED (status) || WEXITSTATUS (status) != MAGIC_STATUS)
9bb5d6
-    {
9bb5d6
-      printf ("Unexpected exit status %d from child process\n",
9bb5d6
-	      status);
9bb5d6
-      goto err;
9bb5d6
-    }
9bb5d6
-  ret = 0;
9bb5d6
-
9bb5d6
-err:
9bb5d6
-  if (outfd >= 0)
9bb5d6
-    close (outfd);
9bb5d6
-  if (infd >= 0)
9bb5d6
-    close (infd);
9bb5d6
-  if (execname)
9bb5d6
-    {
9bb5d6
-      unlink (execname);
9bb5d6
-      free (execname);
9bb5d6
-    }
9bb5d6
-  if (dirname)
9bb5d6
-    {
9bb5d6
-      rmdir (dirname);
9bb5d6
-      free (dirname);
9bb5d6
-    }
9bb5d6
-  return ret;
9bb5d6
-}
9bb5d6
 
9bb5d6
 static int
9bb5d6
 do_test (void)
9bb5d6
@@ -201,15 +57,15 @@ do_test (void)
9bb5d6
       exit (1);
9bb5d6
     }
9bb5d6
 
9bb5d6
-  gid_t target = choose_gid ();
9bb5d6
-  if (target == 0)
9bb5d6
-    {
9bb5d6
-      fprintf (stderr,
9bb5d6
-	       "Could not find a suitable GID for user %jd, skipping test\n",
9bb5d6
-	       (intmax_t) getuid ());
9bb5d6
-      exit (0);
9bb5d6
-    }
9bb5d6
-  return run_executable_sgid (target);
9bb5d6
+  int status = support_capture_subprogram_self_sgid (MAGIC_ARGUMENT);
9bb5d6
+
9bb5d6
+  if (WEXITSTATUS (status) == EXIT_UNSUPPORTED)
9bb5d6
+    return EXIT_UNSUPPORTED;
9bb5d6
+
9bb5d6
+  if (!WIFEXITED (status))
9bb5d6
+    FAIL_EXIT1 ("Unexpected exit status %d from child process\n", status);
9bb5d6
+
9bb5d6
+  return 0;
9bb5d6
 }
9bb5d6
 
9bb5d6
 static void
9bb5d6
@@ -218,23 +74,15 @@ alternative_main (int argc, char **argv)
9bb5d6
   if (argc == 2 && strcmp (argv[1], MAGIC_ARGUMENT) == 0)
9bb5d6
     {
9bb5d6
       if (getgid () == getegid ())
9bb5d6
-	{
9bb5d6
-	  /* This can happen if the file system is mounted nosuid.  */
9bb5d6
-	  fprintf (stderr, "SGID failed: GID and EGID match (%jd)\n",
9bb5d6
-		  (intmax_t) getgid ());
9bb5d6
-	  exit (MAGIC_STATUS);
9bb5d6
-	}
9bb5d6
+	/* This can happen if the file system is mounted nosuid.  */
9bb5d6
+	FAIL_UNSUPPORTED ("SGID failed: GID and EGID match (%jd)\n",
9bb5d6
+		   (intmax_t) getgid ());
9bb5d6
       if (getenv ("PATH") == NULL)
9bb5d6
-	{
9bb5d6
-	  printf ("PATH variable not present\n");
9bb5d6
-	  exit (3);
9bb5d6
-	}
9bb5d6
+	FAIL_EXIT (3, "PATH variable not present\n");
9bb5d6
       if (secure_getenv ("PATH") != NULL)
9bb5d6
-	{
9bb5d6
-	  printf ("PATH variable not filtered out\n");
9bb5d6
-	  exit (4);
9bb5d6
-	}
9bb5d6
-      exit (MAGIC_STATUS);
9bb5d6
+	FAIL_EXIT (4, "PATH variable not filtered out\n");
9bb5d6
+
9bb5d6
+      exit (EXIT_SUCCESS);
9bb5d6
     }
9bb5d6
 }
9bb5d6
 
9bb5d6
diff --git a/support/capture_subprocess.h b/support/capture_subprocess.h
9bb5d6
index 2d2384e73df0d2d0..72fb30504684a84e 100644
9bb5d6
--- a/support/capture_subprocess.h
9bb5d6
+++ b/support/capture_subprocess.h
9bb5d6
@@ -41,6 +41,12 @@ struct support_capture_subprocess support_capture_subprocess
9bb5d6
 struct support_capture_subprocess support_capture_subprogram
9bb5d6
   (const char *file, char *const argv[]);
9bb5d6
 
9bb5d6
+/* Copy the running program into a setgid binary and run it with CHILD_ID
9bb5d6
+   argument.  If execution is successful, return the exit status of the child
9bb5d6
+   program, otherwise return a non-zero failure exit code.  */
9bb5d6
+int support_capture_subprogram_self_sgid
9bb5d6
+  (char *child_id);
9bb5d6
+
9bb5d6
 /* Deallocate the subprocess data captured by
9bb5d6
    support_capture_subprocess.  */
9bb5d6
 void support_capture_subprocess_free (struct support_capture_subprocess *);
9bb5d6
diff --git a/support/subprocess.h b/support/subprocess.h
9bb5d6
index c031878d94c70c71..a19335ee5dbfcf98 100644
9bb5d6
--- a/support/subprocess.h
9bb5d6
+++ b/support/subprocess.h
9bb5d6
@@ -38,6 +38,11 @@ struct support_subprocess support_subprocess
9bb5d6
 struct support_subprocess support_subprogram
9bb5d6
   (const char *file, char *const argv[]);
9bb5d6
 
9bb5d6
+/* Invoke program FILE with ARGV arguments by using posix_spawn and wait for it
9bb5d6
+   to complete.  Return program exit status.  */
9bb5d6
+int support_subprogram_wait
9bb5d6
+  (const char *file, char *const argv[]);
9bb5d6
+
9bb5d6
 /* Wait for the subprocess indicated by PROC::PID.  Return the status
9bb5d6
    indicate by waitpid call.  */
9bb5d6
 int support_process_wait (struct support_subprocess *proc);
9bb5d6
diff --git a/support/support_capture_subprocess.c b/support/support_capture_subprocess.c
9bb5d6
index c475e2004da3183e..eec5371d5602aa29 100644
9bb5d6
--- a/support/support_capture_subprocess.c
9bb5d6
+++ b/support/support_capture_subprocess.c
9bb5d6
@@ -20,11 +20,14 @@
9bb5d6
 #include <support/capture_subprocess.h>
9bb5d6
 
9bb5d6
 #include <errno.h>
9bb5d6
+#include <fcntl.h>
9bb5d6
 #include <stdlib.h>
9bb5d6
 #include <support/check.h>
9bb5d6
 #include <support/xunistd.h>
9bb5d6
 #include <support/xsocket.h>
9bb5d6
 #include <support/xspawn.h>
9bb5d6
+#include <support/support.h>
9bb5d6
+#include <support/test-driver.h>
9bb5d6
 
9bb5d6
 static void
9bb5d6
 transfer (const char *what, struct pollfd *pfd, struct xmemstream *stream)
9bb5d6
@@ -101,6 +104,129 @@ support_capture_subprogram (const char *file, char *const argv[])
9bb5d6
   return result;
9bb5d6
 }
9bb5d6
 
9bb5d6
+/* Copies the executable into a restricted directory, so that we can
9bb5d6
+   safely make it SGID with the TARGET group ID.  Then runs the
9bb5d6
+   executable.  */
9bb5d6
+static int
9bb5d6
+copy_and_spawn_sgid (char *child_id, gid_t gid)
9bb5d6
+{
9bb5d6
+  char *dirname = xasprintf ("%s/tst-tunables-setuid.%jd",
9bb5d6
+			     test_dir, (intmax_t) getpid ());
9bb5d6
+  char *execname = xasprintf ("%s/bin", dirname);
9bb5d6
+  int infd = -1;
9bb5d6
+  int outfd = -1;
9bb5d6
+  int ret = 1, status = 1;
9bb5d6
+
9bb5d6
+  TEST_VERIFY (mkdir (dirname, 0700) == 0);
9bb5d6
+  if (support_record_failure_is_failed ())
9bb5d6
+    goto err;
9bb5d6
+
9bb5d6
+  infd = open ("/proc/self/exe", O_RDONLY);
9bb5d6
+  if (infd < 0)
9bb5d6
+    FAIL_UNSUPPORTED ("unsupported: Cannot read binary from procfs\n");
9bb5d6
+
9bb5d6
+  outfd = open (execname, O_WRONLY | O_CREAT | O_EXCL, 0700);
9bb5d6
+  TEST_VERIFY (outfd >= 0);
9bb5d6
+  if (support_record_failure_is_failed ())
9bb5d6
+    goto err;
9bb5d6
+
9bb5d6
+  char buf[4096];
9bb5d6
+  for (;;)
9bb5d6
+    {
9bb5d6
+      ssize_t rdcount = read (infd, buf, sizeof (buf));
9bb5d6
+      TEST_VERIFY (rdcount >= 0);
9bb5d6
+      if (support_record_failure_is_failed ())
9bb5d6
+	goto err;
9bb5d6
+      if (rdcount == 0)
9bb5d6
+	break;
9bb5d6
+      char *p = buf;
9bb5d6
+      char *end = buf + rdcount;
9bb5d6
+      while (p != end)
9bb5d6
+	{
9bb5d6
+	  ssize_t wrcount = write (outfd, buf, end - p);
9bb5d6
+	  if (wrcount == 0)
9bb5d6
+	    errno = ENOSPC;
9bb5d6
+	  TEST_VERIFY (wrcount > 0);
9bb5d6
+	  if (support_record_failure_is_failed ())
9bb5d6
+	    goto err;
9bb5d6
+	  p += wrcount;
9bb5d6
+	}
9bb5d6
+    }
9bb5d6
+  TEST_VERIFY (fchown (outfd, getuid (), gid) == 0);
9bb5d6
+  if (support_record_failure_is_failed ())
9bb5d6
+    goto err;
9bb5d6
+  TEST_VERIFY (fchmod (outfd, 02750) == 0);
9bb5d6
+  if (support_record_failure_is_failed ())
9bb5d6
+    goto err;
9bb5d6
+  TEST_VERIFY (close (outfd) == 0);
9bb5d6
+  if (support_record_failure_is_failed ())
9bb5d6
+    goto err;
9bb5d6
+  TEST_VERIFY (close (infd) == 0);
9bb5d6
+  if (support_record_failure_is_failed ())
9bb5d6
+    goto err;
9bb5d6
+
9bb5d6
+  /* We have the binary, now spawn the subprocess.  Avoid using
9bb5d6
+     support_subprogram because we only want the program exit status, not the
9bb5d6
+     contents.  */
9bb5d6
+  ret = 0;
9bb5d6
+
9bb5d6
+  char * const args[] = {execname, child_id, NULL};
9bb5d6
+
9bb5d6
+  status = support_subprogram_wait (args[0], args);
9bb5d6
+
9bb5d6
+err:
9bb5d6
+  if (outfd >= 0)
9bb5d6
+    close (outfd);
9bb5d6
+  if (infd >= 0)
9bb5d6
+    close (infd);
9bb5d6
+  if (execname != NULL)
9bb5d6
+    {
9bb5d6
+      unlink (execname);
9bb5d6
+      free (execname);
9bb5d6
+    }
9bb5d6
+  if (dirname != NULL)
9bb5d6
+    {
9bb5d6
+      rmdir (dirname);
9bb5d6
+      free (dirname);
9bb5d6
+    }
9bb5d6
+
9bb5d6
+  if (ret != 0)
9bb5d6
+    FAIL_EXIT1("Failed to make sgid executable for test\n");
9bb5d6
+
9bb5d6
+  return status;
9bb5d6
+}
9bb5d6
+
9bb5d6
+int
9bb5d6
+support_capture_subprogram_self_sgid (char *child_id)
9bb5d6
+{
9bb5d6
+  gid_t target = 0;
9bb5d6
+  const int count = 64;
9bb5d6
+  gid_t groups[count];
9bb5d6
+
9bb5d6
+  /* Get a GID which is not our current GID, but is present in the
9bb5d6
+     supplementary group list.  */
9bb5d6
+  int ret = getgroups (count, groups);
9bb5d6
+  if (ret < 0)
9bb5d6
+    FAIL_UNSUPPORTED("Could not get group list for user %jd\n",
9bb5d6
+		     (intmax_t) getuid ());
9bb5d6
+
9bb5d6
+  gid_t current = getgid ();
9bb5d6
+  for (int i = 0; i < ret; ++i)
9bb5d6
+    {
9bb5d6
+      if (groups[i] != current)
9bb5d6
+	{
9bb5d6
+	  target = groups[i];
9bb5d6
+	  break;
9bb5d6
+	}
9bb5d6
+    }
9bb5d6
+
9bb5d6
+  if (target == 0)
9bb5d6
+    FAIL_UNSUPPORTED("Could not find a suitable GID for user %jd\n",
9bb5d6
+		     (intmax_t) getuid ());
9bb5d6
+
9bb5d6
+  return copy_and_spawn_sgid (child_id, target);
9bb5d6
+}
9bb5d6
+
9bb5d6
 void
9bb5d6
 support_capture_subprocess_free (struct support_capture_subprocess *p)
9bb5d6
 {
9bb5d6
diff --git a/support/support_subprocess.c b/support/support_subprocess.c
9bb5d6
index af01827cac81d80c..f7ee28af2531eda8 100644
9bb5d6
--- a/support/support_subprocess.c
9bb5d6
+++ b/support/support_subprocess.c
9bb5d6
@@ -92,6 +92,19 @@ support_subprogram (const char *file, char *const argv[])
9bb5d6
   return result;
9bb5d6
 }
9bb5d6
 
9bb5d6
+int
9bb5d6
+support_subprogram_wait (const char *file, char *const argv[])
9bb5d6
+{
9bb5d6
+  posix_spawn_file_actions_t fa;
9bb5d6
+
9bb5d6
+  posix_spawn_file_actions_init (&fa);
9bb5d6
+  struct support_subprocess res = support_subprocess_init ();
9bb5d6
+
9bb5d6
+  res.pid = xposix_spawn (file, &fa, NULL, argv, environ);
9bb5d6
+
9bb5d6
+  return support_process_wait (&res;;
9bb5d6
+}
9bb5d6
+
9bb5d6
 int
9bb5d6
 support_process_wait (struct support_subprocess *proc)
9bb5d6
 {