ssp.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.5 2004/11/25 19:10:39 solar 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. #include <sys/sysctl.h>
  31. #ifdef __PROPOLICE_BLOCK_SEGV__
  32. #define SSP_SIGTYPE SIGSEGV
  33. #elif __PROPOLICE_BLOCK_KILL__
  34. #define SSP_SIGTYPE SIGKILL
  35. #else
  36. #define SSP_SIGTYPE SIGABRT
  37. #endif
  38. unsigned long __guard = 0UL;
  39. void __guard_setup(void)
  40. {
  41. size_t size;
  42. struct timeval tv;
  43. #ifdef HAVE_DEV_ERANDOM
  44. int mib[3];
  45. #endif
  46. if (__guard != 0UL)
  47. return;
  48. #ifndef __SSP_QUICK_CANARY__
  49. #ifdef HAVE_DEV_ERANDOM
  50. /* Random is another depth in Linux, hence an array of 3. */
  51. mib[0] = CTL_KERN;
  52. mib[1] = KERN_RANDOM;
  53. mib[2] = RANDOM_ERANDOM;
  54. size = sizeof(unsigned long);
  55. if (__sysctl(mib, 3, &__guard, &size, NULL, 0) != (-1))
  56. if (__guard != 0UL)
  57. return;
  58. #endif
  59. /*
  60. * Attempt to open kernel pseudo random device if one exists before
  61. * opening urandom to avoid system entropy depletion.
  62. */
  63. {
  64. int fd;
  65. #ifdef HAVE_DEV_ERANDOM
  66. if ((fd = open("/dev/erandom", O_RDONLY)) == (-1))
  67. #endif
  68. fd = open("/dev/urandom", O_RDONLY);
  69. if (fd != (-1)) {
  70. size = read(fd, (char *) &__guard, sizeof(__guard));
  71. close(fd);
  72. if (size == sizeof(__guard))
  73. return;
  74. }
  75. }
  76. #endif
  77. /* If sysctl was unsuccessful, use the "terminator canary". */
  78. __guard = 0xFF0A0D00UL;
  79. /* Everything failed? Or we are using a weakened model of the
  80. * terminator canary */
  81. gettimeofday(&tv, NULL);
  82. __guard ^= tv.tv_usec ^ tv.tv_sec;
  83. }
  84. void __stack_smash_handler(char func[], int damaged)
  85. {
  86. extern char *__progname;
  87. const char message[] = ": stack smashing attack in function ";
  88. struct sigaction sa;
  89. sigset_t mask;
  90. sigfillset(&mask);
  91. sigdelset(&mask, SSP_SIGTYPE); /* Block all signal handlers */
  92. sigprocmask(SIG_BLOCK, &mask, NULL); /* except SIGABRT */
  93. /* print error message to stderr and syslog */
  94. fprintf(stderr, "%s%s%s()\n", __progname, message, func);
  95. syslog(LOG_INFO, "%s%s%s()", __progname, message, func);
  96. /* Make sure the default handler is associated with the our signal handler */
  97. memset(&sa, 0, sizeof(struct sigaction));
  98. sigfillset(&sa.sa_mask); /* Block all signals */
  99. sa.sa_flags = 0;
  100. sa.sa_handler = SIG_DFL;
  101. sigaction(SSP_SIGTYPE, &sa, NULL);
  102. (void) kill(getpid(), SSP_SIGTYPE);
  103. _exit(127);
  104. }