Blame stdlib/tst-secure-getenv.c

Packit 6c4009
/* Copyright (C) 2012-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* Test that secure_getenv works by invoking the test as a SGID
Packit 6c4009
   program with a group ID from the supplementary group list.  This
Packit 6c4009
   test can fail spuriously if the user is not a member of a suitable
Packit 6c4009
   supplementary group.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/test-driver.h>
Packit 6c4009
Packit 6c4009
static char MAGIC_ARGUMENT[] = "run-actual-test";
Packit 6c4009
#define MAGIC_STATUS 19
Packit 6c4009
Packit 6c4009
/* Return a GID which is not our current GID, but is present in the
Packit 6c4009
   supplementary group list.  */
Packit 6c4009
static gid_t
Packit 6c4009
choose_gid (void)
Packit 6c4009
{
Packit 6c4009
  const int count = 64;
Packit 6c4009
  gid_t groups[count];
Packit 6c4009
  int ret = getgroups (count, groups);
Packit 6c4009
  if (ret < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("getgroups: %m\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  gid_t current = getgid ();
Packit 6c4009
  for (int i = 0; i < ret; ++i)
Packit 6c4009
    {
Packit 6c4009
      if (groups[i] != current)
Packit 6c4009
	return groups[i];
Packit 6c4009
    }
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Copies the executable into a restricted directory, so that we can
Packit 6c4009
   safely make it SGID with the TARGET group ID.  Then runs the
Packit 6c4009
   executable.  */
Packit 6c4009
static int
Packit 6c4009
run_executable_sgid (gid_t target)
Packit 6c4009
{
Packit 6c4009
  char *dirname = xasprintf ("%s/secure-getenv.%jd",
Packit 6c4009
			     test_dir, (intmax_t) getpid ());
Packit 6c4009
  char *execname = xasprintf ("%s/bin", dirname);
Packit 6c4009
  int infd = -1;
Packit 6c4009
  int outfd = -1;
Packit 6c4009
  int ret = -1;
Packit 6c4009
  if (mkdir (dirname, 0700) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("mkdir: %m\n");
Packit 6c4009
      goto err;
Packit 6c4009
    }
Packit 6c4009
  infd = open ("/proc/self/exe", O_RDONLY);
Packit 6c4009
  if (infd < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("open (/proc/self/exe): %m\n");
Packit 6c4009
      goto err;
Packit 6c4009
    }
Packit 6c4009
  outfd = open (execname, O_WRONLY | O_CREAT | O_EXCL, 0700);
Packit 6c4009
  if (outfd < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("open (%s): %m\n", execname);
Packit 6c4009
      goto err;
Packit 6c4009
    }
Packit 6c4009
  char buf[4096];
Packit 6c4009
  for (;;)
Packit 6c4009
    {
Packit 6c4009
      ssize_t rdcount = read (infd, buf, sizeof (buf));
Packit 6c4009
      if (rdcount < 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("read: %m\n");
Packit 6c4009
	  goto err;
Packit 6c4009
	}
Packit 6c4009
      if (rdcount == 0)
Packit 6c4009
	break;
Packit 6c4009
      char *p = buf;
Packit 6c4009
      char *end = buf + rdcount;
Packit 6c4009
      while (p != end)
Packit 6c4009
	{
Packit 6c4009
	  ssize_t wrcount = write (outfd, buf, end - p);
Packit 6c4009
	  if (wrcount == 0)
Packit 6c4009
	    errno = ENOSPC;
Packit 6c4009
	  if (wrcount <= 0)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("write: %m\n");
Packit 6c4009
	      goto err;
Packit 6c4009
	    }
Packit 6c4009
	  p += wrcount;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  if (fchown (outfd, getuid (), target) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("fchown (%s): %m\n", execname);
Packit 6c4009
      goto err;
Packit 6c4009
    }
Packit 6c4009
  if (fchmod (outfd, 02750) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("fchmod (%s): %m\n", execname);
Packit 6c4009
      goto err;
Packit 6c4009
    }
Packit 6c4009
  if (close (outfd) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("close (outfd): %m\n");
Packit 6c4009
      goto err;
Packit 6c4009
    }
Packit 6c4009
  if (close (infd) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("close (infd): %m\n");
Packit 6c4009
      goto err;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int kid = fork ();
Packit 6c4009
  if (kid < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("fork: %m\n");
Packit 6c4009
      goto err;
Packit 6c4009
    }
Packit 6c4009
  if (kid == 0)
Packit 6c4009
    {
Packit 6c4009
      /* Child process.  */
Packit 6c4009
      char *args[] = { execname, MAGIC_ARGUMENT, NULL };
Packit 6c4009
      execve (execname, args, environ);
Packit 6c4009
      printf ("execve (%s): %m\n", execname);
Packit 6c4009
      _exit (1);
Packit 6c4009
    }
Packit 6c4009
  int status;
Packit 6c4009
  if (waitpid (kid, &status, 0) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("waitpid: %m\n");
Packit 6c4009
      goto err;
Packit 6c4009
    }
Packit 6c4009
  if (!WIFEXITED (status) || WEXITSTATUS (status) != MAGIC_STATUS)
Packit 6c4009
    {
Packit 6c4009
      printf ("Unexpected exit status %d from child process\n",
Packit 6c4009
	      status);
Packit 6c4009
      goto err;
Packit 6c4009
    }
Packit 6c4009
  ret = 0;
Packit 6c4009
Packit 6c4009
err:
Packit 6c4009
  if (outfd >= 0)
Packit 6c4009
    close (outfd);
Packit 6c4009
  if (infd >= 0)
Packit 6c4009
    close (infd);
Packit 6c4009
  if (execname)
Packit 6c4009
    {
Packit 6c4009
      unlink (execname);
Packit 6c4009
      free (execname);
Packit 6c4009
    }
Packit 6c4009
  if (dirname)
Packit 6c4009
    {
Packit 6c4009
      rmdir (dirname);
Packit 6c4009
      free (dirname);
Packit 6c4009
    }
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  if (getenv ("PATH") == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("PATH not set\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  if (secure_getenv ("PATH") == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("PATH not set according to secure_getenv\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  if (strcmp (getenv ("PATH"), secure_getenv ("PATH")) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("PATH mismatch (%s, %s)\n",
Packit 6c4009
	      getenv ("PATH"), secure_getenv ("PATH"));
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  gid_t target = choose_gid ();
Packit 6c4009
  if (target == 0)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr,
Packit 6c4009
	       "Could not find a suitable GID for user %jd, skipping test\n",
Packit 6c4009
	       (intmax_t) getuid ());
Packit 6c4009
      exit (0);
Packit 6c4009
    }
Packit 6c4009
  return run_executable_sgid (target);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
alternative_main (int argc, char **argv)
Packit 6c4009
{
Packit 6c4009
  if (argc == 2 && strcmp (argv[1], MAGIC_ARGUMENT) == 0)
Packit 6c4009
    {
Packit 6c4009
      if (getgid () == getegid ())
Packit 6c4009
	{
Packit 6c4009
	  /* This can happen if the file system is mounted nosuid.  */
Packit 6c4009
	  fprintf (stderr, "SGID failed: GID and EGID match (%jd)\n",
Packit 6c4009
		  (intmax_t) getgid ());
Packit 6c4009
	  exit (MAGIC_STATUS);
Packit 6c4009
	}
Packit 6c4009
      if (getenv ("PATH") == NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("PATH variable not present\n");
Packit 6c4009
	  exit (3);
Packit 6c4009
	}
Packit 6c4009
      if (secure_getenv ("PATH") != NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("PATH variable not filtered out\n");
Packit 6c4009
	  exit (4);
Packit 6c4009
	}
Packit 6c4009
      exit (MAGIC_STATUS);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define PREPARE alternative_main
Packit 6c4009
#include <support/test-driver.c>