Blame elf/tst-env-setuid-tunables.c

Packit 6c4009
/* Copyright (C) 2017-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 tunables like
Packit 6c4009
   glibc.malloc.check and glibc.malloc.mmap_threshold but also retain
Packit 6c4009
   glibc.malloc.mmap_threshold in an unprivileged child.  */
Packit 6c4009
Packit 6c4009
/* This is compiled as part of the testsuite but needs to see
Packit 6c4009
   HAVE_TUNABLES. */
Packit 6c4009
#define _LIBC 1
Packit 6c4009
#include "config.h"
Packit 6c4009
#undef _LIBC
Packit 6c4009
Packit Bot 90749e
#include <errno.h>
Packit Bot 90749e
#include <fcntl.h>
Packit Bot 90749e
#include <stdlib.h>
Packit Bot 90749e
#include <stdint.h>
Packit Bot 90749e
#include <stdio.h>
Packit Bot 90749e
#include <string.h>
Packit Bot 90749e
#include <sys/stat.h>
Packit Bot 90749e
#include <sys/wait.h>
Packit Bot 90749e
#include <unistd.h>
Packit Bot 90749e
#include <intprops.h>
Packit Bot 90749e
#include <array_length.h>
Packit Bot 90749e
Packit Bot 90749e
#include <support/check.h>
Packit Bot 90749e
#include <support/support.h>
Packit Bot 90749e
#include <support/test-driver.h>
Packit Bot 90749e
#include <support/capture_subprocess.h>
Packit Bot 90749e
Packit Bot 90749e
const char *teststrings[] =
Packit Bot 90749e
{
Packit Bot 90749e
  "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
Packit Bot 916157
  "glibc.malloc.check=2:glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
Packit Bot 916157
  "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096:glibc.malloc.check=2",
Packit Bot 916157
  "glibc.malloc.perturb=0x800",
Packit Bot 916157
  "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
Packit Bot 916157
  "glibc.malloc.perturb=0x800:not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
Packit Bot 916157
  "glibc.not_valid.check=2:glibc.malloc.mmap_threshold=4096",
Packit Bot 916157
  "not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
Packit Bot 916157
  "glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096:glibc.malloc.check=2",
Packit Bot 916157
  "glibc.malloc.check=4:glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096",
Packit Bot 916157
  ":glibc.malloc.garbage=2:glibc.malloc.check=1",
Packit Bot 916157
  "glibc.malloc.check=1:glibc.malloc.check=2",
Packit Bot 916157
  "not_valid.malloc.check=2",
Packit Bot 916157
  "glibc.not_valid.check=2",
Packit Bot 90749e
};
Packit 6c4009
Packit Bot 90749e
const char *resultstrings[] =
Packit Bot 90749e
{
Packit Bot 90749e
  "glibc.malloc.mmap_threshold=4096",
Packit Bot 916157
  "glibc.malloc.mmap_threshold=4096",
Packit Bot 916157
  "glibc.malloc.mmap_threshold=4096",
Packit Bot 916157
  "glibc.malloc.perturb=0x800",
Packit Bot 916157
  "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
Packit Bot 916157
  "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
Packit Bot 916157
  "glibc.malloc.mmap_threshold=4096",
Packit Bot 916157
  "glibc.malloc.mmap_threshold=4096",
Packit Bot 916157
  "",
Packit Bot 916157
  "",
Packit Bot 916157
  "",
Packit Bot 916157
  "",
Packit Bot 916157
  "",
Packit Bot 916157
  "",
Packit Bot 90749e
};
Packit 6c4009
Packit 6c4009
static int
Packit Bot 90749e
test_child (int off)
Packit 6c4009
{
Packit 6c4009
  const char *val = getenv ("GLIBC_TUNABLES");
Packit 6c4009
Packit 6c4009
#if HAVE_TUNABLES
Packit Bot 90749e
  if (val != NULL && strcmp (val, resultstrings[off]) == 0)
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  if (val != NULL)
Packit Bot 90749e
    printf ("[%d] Unexpected GLIBC_TUNABLES VALUE %s\n", off, val);
Packit 6c4009
Packit 6c4009
  return 1;
Packit 6c4009
#else
Packit 6c4009
  if (val != NULL)
Packit 6c4009
    {
Packit Bot 90749e
      printf ("[%d] GLIBC_TUNABLES not cleared\n", off);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  return 0;
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit Bot 90749e
do_test (int argc, char **argv)
Packit 6c4009
{
Packit Bot 90749e
  /* Setgid child process.  */
Packit Bot 90749e
  if (argc == 2)
Packit Bot 90749e
    {
Packit Bot 90749e
      if (getgid () == getegid ())
Packit Bot 90749e
	/* This can happen if the file system is mounted nosuid.  */
Packit Bot 90749e
	FAIL_UNSUPPORTED ("SGID failed: GID and EGID match (%jd)\n",
Packit Bot 90749e
			  (intmax_t) getgid ());
Packit 6c4009
Packit Bot 90749e
      int ret = test_child (atoi (argv[1]));
Packit 6c4009
Packit Bot 90749e
      if (ret != 0)
Packit Bot 90749e
	exit (1);
Packit 6c4009
Packit Bot 90749e
      exit (EXIT_SUCCESS);
Packit Bot 90749e
    }
Packit Bot 90749e
  else
Packit Bot 90749e
    {
Packit Bot 90749e
      int ret = 0;
Packit Bot 90749e
Packit Bot 90749e
      /* Spawn tests.  */
Packit Bot 90749e
      for (int i = 0; i < array_length (teststrings); i++)
Packit Bot 90749e
	{
Packit Bot 90749e
	  char buf[INT_BUFSIZE_BOUND (int)];
Packit Bot 90749e
Packit Bot 90749e
	  printf ("Spawned test for %s (%d)\n", teststrings[i], i);
Packit Bot 90749e
	  snprintf (buf, sizeof (buf), "%d\n", i);
Packit Bot 90749e
	  if (setenv ("GLIBC_TUNABLES", teststrings[i], 1) != 0)
Packit Bot 90749e
	    exit (1);
Packit Bot 90749e
Packit Bot 90749e
	  int status = support_capture_subprogram_self_sgid (buf);
Packit Bot 90749e
Packit Bot 90749e
	  /* Bail out early if unsupported.  */
Packit Bot 90749e
	  if (WEXITSTATUS (status) == EXIT_UNSUPPORTED)
Packit Bot 90749e
	    return EXIT_UNSUPPORTED;
Packit Bot 90749e
Packit Bot 90749e
	  ret |= status;
Packit Bot 90749e
	}
Packit Bot 90749e
      return ret;
Packit Bot 90749e
    }
Packit 6c4009
}
Packit Bot 90749e
Packit Bot 90749e
#define TEST_FUNCTION_ARGV do_test
Packit Bot 90749e
#include <support/test-driver.c>