ssp.c 3.0 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 HAVE_DEV_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)
  42. {
  43. size_t size;
  44. struct timeval tv;
  45. #ifdef HAVE_DEV_ERANDOM
  46. int mib[3];
  47. #endif
  48. if (__guard != 0UL)
  49. return;
  50. #ifndef __SSP_QUICK_CANARY__
  51. #ifdef HAVE_DEV_ERANDOM
  52. /* Random is another depth in Linux, hence an array of 3. */
  53. mib[0] = CTL_KERN;
  54. mib[1] = KERN_RANDOM;
  55. mib[2] = RANDOM_ERANDOM;
  56. size = sizeof(unsigned long);
  57. if (__sysctl(mib, 3, &__guard, &size, NULL, 0) != (-1))
  58. if (__guard != 0UL)
  59. return;
  60. #endif
  61. /*
  62. * Attempt to open kernel pseudo random device if one exists before
  63. * opening urandom to avoid system entropy depletion.
  64. */
  65. {
  66. int fd;
  67. #ifdef HAVE_DEV_ERANDOM
  68. if ((fd = open("/dev/erandom", O_RDONLY)) == (-1))
  69. #endif
  70. fd = open("/dev/urandom", O_RDONLY);
  71. if (fd != (-1)) {
  72. size = read(fd, (char *) &__guard, sizeof(__guard));
  73. close(fd);
  74. if (size == sizeof(__guard))
  75. return;
  76. }
  77. }
  78. #endif
  79. /* If sysctl was unsuccessful, use the "terminator canary". */
  80. __guard = 0xFF0A0D00UL;
  81. /* Everything failed? Or we are using a weakened model of the
  82. * terminator canary */
  83. gettimeofday(&tv, NULL);
  84. __guard ^= tv.tv_usec ^ tv.tv_sec;
  85. }
  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 SIGABRT */
  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 sure the default handler is associated with the our 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. }