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