ssp.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #ifdef __SSP__
  22. # error ssp.c has to be built w/ -fno-stack-protector
  23. #endif
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include <signal.h>
  29. #include <sys/types.h>
  30. #include <sys/un.h>
  31. #include <sys/syslog.h>
  32. #include <sys/time.h>
  33. #ifdef __SSP_USE_ERANDOM__
  34. # include <sys/sysctl.h>
  35. #endif
  36. #ifdef __PROPOLICE_BLOCK_SEGV__
  37. # define SSP_SIGTYPE SIGSEGV
  38. #elif __PROPOLICE_BLOCK_KILL__
  39. # define SSP_SIGTYPE SIGKILL
  40. #else
  41. # define SSP_SIGTYPE SIGABRT
  42. #endif
  43. unsigned long __guard = 0UL;
  44. /* Use of __* functions from the rest of glibc here avoids
  45. * initialisation problems for executables preloaded with
  46. * libraries that overload the associated standard library
  47. * functions.
  48. */
  49. #ifdef __UCLIBC__
  50. extern int __libc_open(__const char *file, int flags, ...);
  51. extern ssize_t __libc_read(int fd, void *buf, size_t count);
  52. extern int __libc_close(int fd);
  53. #else
  54. # define __libc_open(file, flags) __open(file, flags)
  55. # define __libc_read(fd, buf, count) __read(fd, buf, count)
  56. # define __libc_close(fd) __close(fd)
  57. #endif
  58. void __guard_setup(void) __attribute__ ((constructor));
  59. void __guard_setup(void)
  60. {
  61. size_t size;
  62. if (__guard != 0UL)
  63. return;
  64. /* Start with the "terminator canary". */
  65. __guard = 0xFF0A0D00UL;
  66. #ifndef __SSP_QUICK_CANARY__
  67. # ifdef __SSP_USE_ERANDOM__
  68. {
  69. int mib[3];
  70. /* Random is another depth in Linux, hence an array of 3. */
  71. mib[0] = CTL_KERN;
  72. mib[1] = KERN_RANDOM;
  73. mib[2] = RANDOM_ERANDOM;
  74. size = sizeof(unsigned long);
  75. if (__sysctl(mib, 3, &__guard, &size, NULL, 0) != (-1))
  76. if (__guard != 0UL)
  77. return;
  78. }
  79. # endif /* ifdef __SSP_USE_ERANDOM__ */
  80. /*
  81. * Attempt to open kernel pseudo random device if one exists before
  82. * opening urandom to avoid system entropy depletion.
  83. */
  84. {
  85. int fd;
  86. # ifdef __SSP_USE_ERANDOM__
  87. if ((fd = __libc_open("/dev/erandom", O_RDONLY)) == (-1))
  88. # endif
  89. fd = __libc_open("/dev/urandom", O_RDONLY);
  90. if (fd != (-1)) {
  91. size = __libc_read(fd, (char *) &__guard, sizeof(__guard));
  92. __libc_close(fd);
  93. if (size == sizeof(__guard))
  94. return;
  95. }
  96. }
  97. #endif /* ifndef __SSP_QUICK_CANARY__ */
  98. /* Everything failed? Or we are using a weakened model of the
  99. * terminator canary */
  100. {
  101. struct timeval tv;
  102. gettimeofday(&tv, NULL);
  103. __guard ^= tv.tv_usec ^ tv.tv_sec;
  104. }
  105. }
  106. void __stack_smash_handler(char func[], int damaged __attribute__ ((unused)));
  107. void __stack_smash_handler(char func[], int damaged)
  108. {
  109. extern char *__progname;
  110. const char message[] = ": stack smashing attack in function ";
  111. struct sigaction sa;
  112. sigset_t mask;
  113. sigfillset(&mask);
  114. sigdelset(&mask, SSP_SIGTYPE); /* Block all signal handlers */
  115. sigprocmask(SIG_BLOCK, &mask, NULL); /* except SSP_SIGTYPE */
  116. /* Print error message to stderr and syslog */
  117. fprintf(stderr, "%s%s%s()\n", __progname, message, func);
  118. syslog(LOG_INFO, "%s%s%s()", __progname, message, func);
  119. /* Make the default handler associated with the signal handler */
  120. memset(&sa, 0, sizeof(struct sigaction));
  121. sigfillset(&sa.sa_mask); /* Block all signals */
  122. sa.sa_flags = 0;
  123. sa.sa_handler = SIG_DFL;
  124. sigaction(SSP_SIGTYPE, &sa, NULL);
  125. (void) kill(getpid(), SSP_SIGTYPE);
  126. _exit(127);
  127. }