ssp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /* prototypes */
  41. extern int __libc_open (__const char *file, int oflag, mode_t mode);
  42. extern ssize_t __libc_read(int fd, void *buf, size_t count);
  43. extern int __libc_close (int fd);
  44. unsigned long __guard = 0UL;
  45. void __guard_setup(void) __attribute__ ((constructor));
  46. void __guard_setup(void)
  47. {
  48. size_t size;
  49. struct timeval tv;
  50. if (__guard != 0UL)
  51. return;
  52. /* Start with the "terminator canary". */
  53. __guard = 0xFF0A0D00UL;
  54. #ifndef __SSP_QUICK_CANARY__
  55. #ifdef __SSP_USE_ERANDOM__
  56. int mib[3];
  57. /* Random is another depth in Linux, hence an array of 3. */
  58. mib[0] = CTL_KERN;
  59. mib[1] = KERN_RANDOM;
  60. mib[2] = RANDOM_ERANDOM;
  61. size = sizeof(unsigned long);
  62. if (__sysctl(mib, 3, &__guard, &size, NULL, 0) != (-1))
  63. if (__guard != 0UL)
  64. return;
  65. #endif
  66. /*
  67. * Attempt to open kernel pseudo random device if one exists before
  68. * opening urandom to avoid system entropy depletion.
  69. */
  70. {
  71. int fd;
  72. #ifdef __SSP_USE_ERANDOM__
  73. if ((fd = __libc_open("/dev/erandom", O_RDONLY)) == (-1))
  74. #endif
  75. fd = __libc_open("/dev/urandom", O_RDONLY);
  76. if (fd != (-1)) {
  77. size = __libc_read(fd, (char *) &__guard, sizeof(__guard));
  78. __libc_close(fd);
  79. if (size == sizeof(__guard))
  80. return;
  81. }
  82. }
  83. #endif
  84. /* Everything failed? Or we are using a weakened model of the
  85. * terminator canary */
  86. gettimeofday(&tv, NULL);
  87. __guard ^= tv.tv_usec ^ tv.tv_sec;
  88. }
  89. void __stack_smash_handler(char func[], int damaged __attribute__ ((unused)));
  90. void __stack_smash_handler(char func[], int damaged)
  91. {
  92. extern char *__progname;
  93. const char message[] = ": stack smashing attack in function ";
  94. struct sigaction sa;
  95. sigset_t mask;
  96. sigfillset(&mask);
  97. sigdelset(&mask, SSP_SIGTYPE); /* Block all signal handlers */
  98. sigprocmask(SIG_BLOCK, &mask, NULL); /* except SSP_SIGTYPE */
  99. /* Print error message to stderr and syslog */
  100. fprintf(stderr, "%s%s%s()\n", __progname, message, func);
  101. syslog(LOG_INFO, "%s%s%s()", __progname, message, func);
  102. /* Make the default handler associated with the signal handler */
  103. memset(&sa, 0, sizeof(struct sigaction));
  104. sigfillset(&sa.sa_mask); /* Block all signals */
  105. sa.sa_flags = 0;
  106. sa.sa_handler = SIG_DFL;
  107. sigaction(SSP_SIGTYPE, &sa, NULL);
  108. (void) kill(getpid(), SSP_SIGTYPE);
  109. _exit(127);
  110. }