/* * Distributed under the terms of the GNU General Public License v2 * $Header: $ * * 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 - * Alexander Gabert - * The PaX Team - * Peter S. Mazinger - * Yoann Vandoorselaere - * Robert Connolly - * Cory Visi * */ #ifdef HAVE_CONFIG_H # include #endif #include #include #include #include #include #include #include #include #include #ifdef __SSP_USE_ERANDOM__ #include #endif #ifdef __PROPOLICE_BLOCK_SEGV__ #define SSP_SIGTYPE SIGSEGV #elif __PROPOLICE_BLOCK_KILL__ #define SSP_SIGTYPE SIGKILL #else #define SSP_SIGTYPE SIGABRT #endif unsigned long __guard = 0UL; void __guard_setup(void) __attribute__ ((constructor)); void __guard_setup(void) { size_t size; struct timeval tv; if (__guard != 0UL) return; __guard = 0xFF0A0D00UL; gettimeofday(&tv, NULL); __guard ^= tv.tv_usec ^ tv.tv_sec; #ifndef __SSP_QUICK_CANARY__ int fd=0; fd = open("/dev/urandom", O_RDONLY); if (fd != (-1)) { size = read(fd, (char *) &__guard, sizeof(__guard)); close(fd); } #ifdef __SSP_USE_ERANDOM__ else { int i=0, mib[3]; mib[0] = CTL_KERN; mib[1] = KERN_RANDOM; mib[2] = RANDOM_ERANDOM; for (i = 0; i < sizeof(__guard) / 4; i++) { size = sizeof(unsigned long); if (__sysctl(mib, 3, &__guard, &size, NULL, 0) == -1) break; } } #endif #endif /* Quick Canary */ } void __stack_smash_handler(char func[], int damaged __attribute__ ((unused))); void __stack_smash_handler(char func[], int damaged) { extern char *__progname; const char message[] = ": stack smashing attack in function "; struct sigaction sa; sigset_t mask; sigfillset(&mask); sigdelset(&mask, SSP_SIGTYPE); /* Block all signal handlers */ sigprocmask(SIG_BLOCK, &mask, NULL); /* except SSP_SIGTYPE */ /* Print error message to stderr and syslog */ fprintf(stderr, "%s%s%s()\n", __progname, message, func); syslog(LOG_INFO, "%s%s%s()", __progname, message, func); /* Make the default handler associated with the signal handler */ memset(&sa, 0, sizeof(struct sigaction)); sigfillset(&sa.sa_mask); /* Block all signals */ sa.sa_flags = 0; sa.sa_handler = SIG_DFL; sigaction(SSP_SIGTYPE, &sa, NULL); (void) kill(getpid(), SSP_SIGTYPE); _exit(127); }