Blame debug/tst-longjmp_chk3.c

Packit 6c4009
/* Make sure longjmp fortification catches bad signal stacks.
Packit 6c4009
   Copyright (C) 2013-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
#include <setjmp.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
static int do_test (void);
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"
Packit 6c4009
Packit 6c4009
static char buf[SIGSTKSZ * 4];
Packit 6c4009
static jmp_buf jb;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
handler (int sig)
Packit 6c4009
{
Packit 6c4009
  if (sig == SIGUSR1)
Packit 6c4009
    {
Packit 6c4009
      if (setjmp (jb) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("setjmp should not have been called");
Packit 6c4009
	  kill (getpid (), SIGTERM);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else if (sig == SIGABRT)
Packit 6c4009
    {
Packit 6c4009
      /* Yeah it worked.  */
Packit 6c4009
      _exit (0);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  stack_t ss;
Packit 6c4009
Packit 6c4009
  set_fortify_handler (handler);
Packit 6c4009
Packit 6c4009
  /* Create a valid signal stack and enable it.  */
Packit 6c4009
  ss.ss_sp = buf;
Packit 6c4009
  ss.ss_size = sizeof (buf);
Packit 6c4009
  ss.ss_flags = 0;
Packit 6c4009
  if (sigaltstack (&ss, NULL) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("first sigaltstack failed: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Trigger the signal handler which will create a jmpbuf that points to the
Packit 6c4009
     end of the signal stack.  */
Packit 6c4009
  signal (SIGUSR1, handler);
Packit 6c4009
  kill (getpid (), SIGUSR1);
Packit 6c4009
Packit 6c4009
  /* Shrink the signal stack so the jmpbuf is now invalid.
Packit 6c4009
     We adjust the start & end to handle stacks that grow up & down.  */
Packit 6c4009
  ss.ss_sp = buf + sizeof (buf) / 2;
Packit 6c4009
  ss.ss_size = sizeof (buf) / 4;
Packit 6c4009
  if (sigaltstack (&ss, NULL) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("second sigaltstack failed: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* This should fail.  */
Packit 6c4009
  longjmp (jb, 1);
Packit 6c4009
Packit 6c4009
  puts ("longjmp returned and shouldn't");
Packit 6c4009
  return 1;
Packit 6c4009
}