ssp.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Distributed under the terms of the GNU General Public License v2
  3. * $Header: /var/cvs/uClibc/libc/sysdeps/linux/common/ssp.c,v 1.6 2005/01/11 17:01:53 vapier Exp $
  4. *
  5. * This is a modified version of Hiroaki Etoh's stack smashing routines
  6. * implemented for glibc.
  7. *
  8. * The following people have contributed input to this code.
  9. * Ned Ludd - <solar[@]gentoo.org>
  10. * Alexander Gabert - <pappy[@]gentoo.org>
  11. * The PaX Team - <pageexec[@]freemail.hu>
  12. * Peter S. Mazinger - <ps.m[@]gmx.net>
  13. * Yoann Vandoorselaere - <yoann[@]prelude-ids.org>
  14. * Robert Connolly - <robert[@]linuxfromscratch.org>
  15. * Cory Visi <cory@visi.name>
  16. *
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <signal.h>
  26. #include <sys/types.h>
  27. #include <sys/un.h>
  28. #include <sys/syslog.h>
  29. #include <sys/time.h>
  30. #ifdef __SSP_USE_ERANDOM__
  31. #include <sys/sysctl.h>
  32. #endif
  33. #ifdef __PROPOLICE_BLOCK_SEGV__
  34. #define SSP_SIGTYPE SIGSEGV
  35. #elif __PROPOLICE_BLOCK_KILL__
  36. #define SSP_SIGTYPE SIGKILL
  37. #else
  38. #define SSP_SIGTYPE SIGABRT
  39. #endif
  40. unsigned long __guard = 0UL;
  41. void __guard_setup(void) __attribute__ ((constructor));
  42. void __guard_setup(void)
  43. {
  44. size_t size;
  45. struct timeval tv;
  46. if (__guard != 0UL)
  47. return;
  48. /* Start with the "terminator canary". */
  49. __guard = 0xFF0A0D00UL;
  50. #ifndef __SSP_QUICK_CANARY__
  51. #ifdef __SSP_USE_ERANDOM__
  52. int mib[3];
  53. /* Random is another depth in Linux, hence an array of 3. */
  54. mib[0] = CTL_KERN;
  55. mib[1] = KERN_RANDOM;
  56. mib[2] = RANDOM_ERANDOM;
  57. size = sizeof(unsigned long);
  58. if (__sysctl(mib, 3, &__guard, &size, NULL, 0) != (-1))
  59. if (__guard != 0UL)
  60. return;
  61. #endif
  62. /*
  63. * Attempt to open kernel pseudo random device if one exists before
  64. * opening urandom to avoid system entropy depletion.
  65. */
  66. {
  67. int fd;
  68. #ifdef __SSP_USE_ERANDOM__
  69. if ((fd = __libc_open("/dev/erandom", O_RDONLY)) == (-1))
  70. #endif
  71. fd = __libc_open("/dev/urandom", O_RDONLY);
  72. if (fd != (-1)) {
  73. size = __libc_read(fd, (char *) &__guard, sizeof(__guard));
  74. __libc_close(fd);
  75. if (size == sizeof(__guard))
  76. return;
  77. }
  78. }
  79. #endif
  80. /* Everything failed? Or we are using a weakened model of the
  81. * terminator canary */
  82. gettimeofday(&tv, NULL);
  83. __guard ^= tv.tv_usec ^ tv.tv_sec;
  84. }
  85. void __stack_smash_handler(char func[], int damaged __attribute__ ((unused)));
  86. void __stack_smash_handler(char func[], int damaged)
  87. {
  88. extern char *__progname;
  89. const char message[] = ": stack smashing attack in function ";
  90. struct sigaction sa;
  91. sigset_t mask;
  92. sigfillset(&mask);
  93. sigdelset(&mask, SSP_SIGTYPE); /* Block all signal handlers */
  94. sigprocmask(SIG_BLOCK, &mask, NULL); /* except SSP_SIGTYPE */
  95. /* Print error message to stderr and syslog */
  96. fprintf(stderr, "%s%s%s()\n", __progname, message, func);
  97. syslog(LOG_INFO, "%s%s%s()", __progname, message, func);
  98. /* Make the default handler associated with the signal handler */
  99. memset(&sa, 0, sizeof(struct sigaction));
  100. sigfillset(&sa.sa_mask); /* Block all signals */
  101. sa.sa_flags = 0;
  102. sa.sa_handler = SIG_DFL;
  103. sigaction(SSP_SIGTYPE, &sa, NULL);
  104. (void) kill(getpid(), SSP_SIGTYPE);
  105. _exit(127);
  106. }