Blame elf/tst-env-setuid.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
/* Verify that tunables correctly filter out unsafe environment variables like
Packit 6c4009
   MALLOC_CHECK_ and MALLOC_MMAP_THRESHOLD_ but also retain
Packit 6c4009
   MALLOC_MMAP_THRESHOLD_ in an unprivileged child.  */
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 SETGID_CHILD[] = "setgid-child";
Packit 6c4009
#define CHILD_STATUS 42
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
/* Spawn and execute a program and verify that it returns the CHILD_STATUS.  */
Packit 6c4009
static pid_t
Packit 6c4009
do_execve (char **args)
Packit 6c4009
{
Packit 6c4009
  pid_t kid = vfork ();
Packit 6c4009
Packit 6c4009
  if (kid < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("vfork: %m\n");
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (kid == 0)
Packit 6c4009
    {
Packit 6c4009
      /* Child process.  */
Packit 6c4009
      execve (args[0], args, environ);
Packit 6c4009
      _exit (-errno);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (kid < 0)
Packit 6c4009
    return 1;
Packit 6c4009
Packit 6c4009
  int status;
Packit 6c4009
Packit 6c4009
  if (waitpid (kid, &status, 0) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("waitpid: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (WEXITSTATUS (status) == EXIT_UNSUPPORTED)
Packit 6c4009
    return EXIT_UNSUPPORTED;
Packit 6c4009
Packit 6c4009
  if (!WIFEXITED (status) || WEXITSTATUS (status) != CHILD_STATUS)
Packit 6c4009
    {
Packit 6c4009
      printf ("Unexpected exit status %d from child process\n",
Packit 6c4009
	      WEXITSTATUS (status));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  return 0;
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/tst-tunables-setuid.%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 = 0;
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
  char *args[] = {execname, SETGID_CHILD, NULL};
Packit 6c4009
Packit 6c4009
  ret = do_execve (args);
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
#ifndef test_child
Packit 6c4009
static int
Packit 6c4009
test_child (void)
Packit 6c4009
{
Packit 6c4009
  if (getenv ("MALLOC_CHECK_") != NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("MALLOC_CHECK_ is still set\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (getenv ("MALLOC_MMAP_THRESHOLD_") == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("MALLOC_MMAP_THRESHOLD_ lost\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (getenv ("LD_HWCAP_MASK") != NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("LD_HWCAP_MASK still set\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifndef test_parent
Packit 6c4009
static int
Packit 6c4009
test_parent (void)
Packit 6c4009
{
Packit 6c4009
  if (getenv ("MALLOC_CHECK_") == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("MALLOC_CHECK_ lost\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (getenv ("MALLOC_MMAP_THRESHOLD_") == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("MALLOC_MMAP_THRESHOLD_ lost\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (getenv ("LD_HWCAP_MASK") == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("LD_HWCAP_MASK lost\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (int argc, char **argv)
Packit 6c4009
{
Packit 6c4009
  /* Setgid child process.  */
Packit 6c4009
  if (argc == 2 && strcmp (argv[1], SETGID_CHILD) == 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 (EXIT_UNSUPPORTED);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      int ret = test_child ();
Packit 6c4009
Packit 6c4009
      if (ret != 0)
Packit 6c4009
	exit (1);
Packit 6c4009
Packit 6c4009
      exit (CHILD_STATUS);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (test_parent () != 0)
Packit 6c4009
	exit (1);
Packit 6c4009
Packit 6c4009
      /* Try running a setgid program.  */
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
Packit 6c4009
      return run_executable_sgid (target);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Something went wrong and our argv was corrupted.  */
Packit 6c4009
  _exit (1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION_ARGV do_test
Packit 6c4009
#include <support/test-driver.c>