Blame nptl/tst-stackguard1.c

Packit 6c4009
/* Copyright (C) 2005-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Jakub Jelinek <jakub@redhat.com>, 2005.
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
#include <errno.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
#include <stackguard-macros.h>
Packit 6c4009
#include <tls.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
static const char *command;
Packit 6c4009
static bool child;
Packit 6c4009
static uintptr_t stack_chk_guard_copy;
Packit 6c4009
static bool stack_chk_guard_copy_set;
Packit 6c4009
static int fds[2];
Packit 6c4009
Packit 6c4009
static void __attribute__ ((constructor))
Packit 6c4009
con (void)
Packit 6c4009
{
Packit 6c4009
  stack_chk_guard_copy = STACK_CHK_GUARD;
Packit 6c4009
  stack_chk_guard_copy_set = true;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
uintptr_t_cmp (const void *a, const void *b)
Packit 6c4009
{
Packit 6c4009
  if (*(uintptr_t *) a < *(uintptr_t *) b)
Packit 6c4009
    return 1;
Packit 6c4009
  if (*(uintptr_t *) a > *(uintptr_t *) b)
Packit 6c4009
    return -1;
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
tf (void *arg)
Packit 6c4009
{
Packit 6c4009
  if (stack_chk_guard_copy != STACK_CHK_GUARD)
Packit 6c4009
    {
Packit 6c4009
      puts ("STACK_CHK_GUARD changed in thread");
Packit 6c4009
      return (void *) 1L;
Packit 6c4009
    }
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  if (!stack_chk_guard_copy_set)
Packit 6c4009
    {
Packit 6c4009
      puts ("constructor has not been run");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (stack_chk_guard_copy != STACK_CHK_GUARD)
Packit 6c4009
    {
Packit 6c4009
      puts ("STACK_CHK_GUARD changed between constructor and do_test");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (child)
Packit 6c4009
    {
Packit 6c4009
      int i;
Packit 6c4009
      pthread_t th[4];
Packit 6c4009
      void *ret;
Packit 6c4009
      for (i = 0; i < 4; ++i)
Packit 6c4009
	if (pthread_create (&th[i], NULL, tf, NULL))
Packit 6c4009
	  {
Packit 6c4009
	    puts ("thread creation failed");
Packit 6c4009
	    return 1;
Packit 6c4009
	  }
Packit 6c4009
      for (i = 0; i < 4; ++i)
Packit 6c4009
	if (pthread_join (th[i], &ret))
Packit 6c4009
	  {
Packit 6c4009
	    puts ("thread join failed");
Packit 6c4009
	    return 1;
Packit 6c4009
	  }
Packit 6c4009
	else if (ret != NULL)
Packit 6c4009
	  return 1;
Packit 6c4009
Packit 6c4009
      write (2, &stack_chk_guard_copy, sizeof (stack_chk_guard_copy));
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (command == NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("missing --command or --child argument");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
#define N 16
Packit 6c4009
  uintptr_t child_stack_chk_guards[N + 1];
Packit 6c4009
  child_stack_chk_guards[N] = stack_chk_guard_copy;
Packit 6c4009
  int i;
Packit 6c4009
  for (i = 0; i < N; ++i)
Packit 6c4009
    {
Packit 6c4009
      if (pipe (fds) < 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("couldn't create pipe: %m\n");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      pid_t pid = fork ();
Packit 6c4009
      if (pid < 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("fork failed: %m\n");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (!pid)
Packit 6c4009
	{
Packit 6c4009
	  if (stack_chk_guard_copy != STACK_CHK_GUARD)
Packit 6c4009
	    {
Packit 6c4009
	      puts ("STACK_CHK_GUARD changed after fork");
Packit 6c4009
	      exit (1);
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  close (fds[0]);
Packit 6c4009
	  close (2);
Packit 6c4009
	  dup2 (fds[1], 2);
Packit 6c4009
	  close (fds[1]);
Packit 6c4009
Packit 6c4009
	  system (command);
Packit 6c4009
	  exit (0);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      close (fds[1]);
Packit 6c4009
Packit 6c4009
      if (TEMP_FAILURE_RETRY (read (fds[0], &child_stack_chk_guards[i],
Packit 6c4009
				    sizeof (uintptr_t))) != sizeof (uintptr_t))
Packit 6c4009
	{
Packit 6c4009
	  puts ("could not read stack_chk_guard value from child");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      close (fds[0]);
Packit 6c4009
Packit 6c4009
      pid_t termpid;
Packit 6c4009
      int status;
Packit 6c4009
      termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
Packit 6c4009
      if (termpid == -1)
Packit 6c4009
	{
Packit 6c4009
	  printf ("waitpid failed: %m\n");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      else if (termpid != pid)
Packit 6c4009
	{
Packit 6c4009
	  printf ("waitpid returned %ld != %ld\n",
Packit 6c4009
		  (long int) termpid, (long int) pid);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      else if (!WIFEXITED (status) || WEXITSTATUS (status))
Packit 6c4009
	{
Packit 6c4009
	  puts ("child hasn't exited with exit status 0");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  qsort (child_stack_chk_guards, N + 1, sizeof (uintptr_t), uintptr_t_cmp);
Packit 6c4009
Packit 6c4009
  uintptr_t default_guard = 0;
Packit 6c4009
  unsigned char *p = (unsigned char *) &default_guard;
Packit 6c4009
  p[sizeof (uintptr_t) - 1] = 255;
Packit 6c4009
  p[sizeof (uintptr_t) - 2] = '\n';
Packit 6c4009
  p[0] = 0;
Packit 6c4009
Packit 6c4009
  /* Test if the stack guard canaries are either randomized,
Packit 6c4009
     or equal to the default stack guard canary value.
Packit 6c4009
     Even with randomized stack guards it might happen
Packit 6c4009
     that the random number generator generates the same
Packit 6c4009
     values, but if that happens in more than half from
Packit 6c4009
     the 16 runs, something is very wrong.  */
Packit 6c4009
  int ndifferences = 0;
Packit 6c4009
  int ndefaults = 0;
Packit 6c4009
  for (i = 0; i < N; ++i)
Packit 6c4009
    {
Packit 6c4009
      if (child_stack_chk_guards[i] != child_stack_chk_guards[i+1])
Packit 6c4009
	ndifferences++;
Packit 6c4009
      else if (child_stack_chk_guards[i] == default_guard)
Packit 6c4009
	ndefaults++;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  printf ("differences %d defaults %d\n", ndifferences, ndefaults);
Packit 6c4009
Packit 6c4009
  if (ndifferences < N / 2 && ndefaults < N / 2)
Packit 6c4009
    {
Packit 6c4009
      puts ("stack guard canaries are not randomized enough");
Packit 6c4009
      puts ("nor equal to the default canary value");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define OPT_COMMAND	10000
Packit 6c4009
#define OPT_CHILD	10001
Packit 6c4009
#define CMDLINE_OPTIONS	\
Packit 6c4009
  { "command", required_argument, NULL, OPT_COMMAND },  \
Packit 6c4009
  { "child", no_argument, NULL, OPT_CHILD },
Packit 6c4009
#define CMDLINE_PROCESS	\
Packit 6c4009
  case OPT_COMMAND:	\
Packit 6c4009
    command = optarg;	\
Packit 6c4009
    break;		\
Packit 6c4009
  case OPT_CHILD:	\
Packit 6c4009
    child = true;	\
Packit 6c4009
    break;
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"