/*
 * Distributed under the terms of the GNU General Public License v2
 * $Header: /var/cvsroot/gentoo-x86/sys-libs/glibc/files/2.3.3/ssp.c,v 1.3 2004/08/07 17:53:20 solar Exp $
 *
 * This is a modified version of Hiroaki Etoh's stack smashing routines
 * implemented for glibc.
 *
 * The following people have contributed input to this code.
 * Ned Ludd - <solar[@]gentoo.org>
 * Alexander Gabert - <pappy[@]gentoo.org>
 * The PaX Team - <pageexec[@]freemail.hu>
 * Peter S. Mazinger - <ps.m[@]gmx.net>
 * Yoann Vandoorselaere - <yoann[@]prelude-ids.org>
 * Robert Connolly - <robert[@]linuxfromscratch.org>
 * Cory Visi <cory@visi.name>
 * Mike Frysinger <vapier@gentoo.org>
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/syslog.h>
#include <sys/time.h>
#include <sys/sysctl.h>
#include <linux/unistd.h>
#include "ssp.h"

unsigned long __guard = 0UL;

/* Use of __* functions from the rest of glibc here avoids
 * initialisation problems for executables preloaded with
 * libraries that overload the associated standard library
 * functions.
 */
void
__guard_setup (void)
{
  size_t size;
#ifdef HAVE_DEV_ERANDOM
  int mib[3];
#endif

  if (__guard != 0UL)
    return;

#ifndef __SSP_QUICK_CANARY__
#ifdef HAVE_DEV_ERANDOM
  /* Random is another depth in Linux, hence an array of 3. */
  mib[0] = CTL_KERN;
  mib[1] = KERN_RANDOM;
  mib[2] = RANDOM_ERANDOM;

  size = sizeof(unsigned long);
  if (__sysctl (mib, 3, &__guard, &size, NULL, 0) != (-1))
    if (__guard != 0UL)
      return;
#endif /* ifdef HAVE_DEV_ERANDOM */
  /* 
   * Attempt to open kernel pseudo random device if one exists before 
   * opening urandom to avoid system entropy depletion.
   */
  {
    int fd;
#ifdef HAVE_DEV_ERANDOM
    if ((fd = OPEN("/dev/erandom", O_RDONLY)) == (-1))
#endif
      fd = OPEN("/dev/urandom", O_RDONLY);
    if (fd != (-1))
      {
	size = READ(fd, (char *) &__guard, sizeof(__guard));
	CLOSE(fd);
	if (size == sizeof(__guard))
	  return;
      }
  }
#endif /* ifndef __SSP_QUICK_CANARY__ */

  /* If sysctl was unsuccessful, use the "terminator canary". */
  __guard = 0xFF0A0D00UL; /*  0, LF, CR, 255 */
  {
    /* Everything failed? Or we are using a weakened model of the 
     * terminator canary */
    struct timeval tv;

    if (GETTIMEOFDAY(&tv, NULL) != (-1))
       __guard ^= tv.tv_usec ^ tv.tv_sec;
  }
}

void
__stack_smash_handler (char func[], int damaged)
{
  struct sigaction sa;
  const char message[] = "stack smashing attack in function %s";
  sigset_t mask;

  /* Immediately block all signal handlers from running code */
  sigfillset (&mask);
  sigdelset (&mask, SSP_SIGTYPE);
  SIGPROCMASK(SIG_BLOCK, &mask, NULL);

  /* print error message to syslog and stderr. */
  SYSLOG(LOG_CRIT, message, func);

  /* we should close the fd and reopen it? or see if there is a tty attached? */
  WRITE(STDERR_FILENO, message, sizeof(message)-3);
  WRITE(STDERR_FILENO, func, strlen(func));
  WRITE(STDERR_FILENO, "()\n", 3);

  /* unblock ssp abort signal */
  bzero (&sa, sizeof(struct sigaction));

  sigemptyset (&sa.sa_mask);
  sa.sa_flags = 0;
  sa.sa_handler = SIG_DFL;
  SIGACTION(SSP_SIGTYPE, &sa, NULL);
  KILL(GETPID(), SSP_SIGTYPE);
  EXIT(127);
  while(1);
}
