ssp.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Distributed under the terms of the GNU Lesser General Public License
  3. * $Header: $
  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. * Mike Frysinger <vapier[@]gentoo.org>
  17. */
  18. #if defined __SSP__ || defined __SSP_ALL__
  19. #error "file must not be compiled with stack protection enabled on it. Use -fno-stack-protector"
  20. #endif
  21. #ifdef __PROPOLICE_BLOCK_SEGV__
  22. # define SSP_SIGTYPE SIGSEGV
  23. #else
  24. # define SSP_SIGTYPE SIGABRT
  25. #endif
  26. #define openlog __openlog
  27. #define syslog __syslog
  28. #define closelog __closelog
  29. #define sigfillset __sigfillset_internal
  30. #define sigdelset __sigdelset_internal
  31. #define sigaction __sigaction
  32. #define kill __kill
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include <signal.h>
  36. #include <sys/syslog.h>
  37. static __always_inline void block_signals(void)
  38. {
  39. struct sigaction sa;
  40. sigset_t mask;
  41. sigfillset(&mask);
  42. sigdelset(&mask, SSP_SIGTYPE); /* Block all signal handlers */
  43. __sigprocmask(SIG_BLOCK, &mask, NULL); /* except SSP_SIGTYPE */
  44. /* Make the default handler associated with the signal handler */
  45. __memset(&sa, 0, sizeof(struct sigaction));
  46. sigfillset(&sa.sa_mask); /* Block all signals */
  47. sa.sa_flags = 0;
  48. sa.sa_handler = SIG_DFL;
  49. sigaction(SSP_SIGTYPE, &sa, NULL);
  50. }
  51. static __always_inline void ssp_write(int fd, const char *msg1, const char *msg2, const char *msg3)
  52. {
  53. __write(fd, msg1, __strlen(msg1));
  54. __write(fd, msg2, __strlen(msg2));
  55. __write(fd, msg3, __strlen(msg3));
  56. __write(fd, "()\n", 3);
  57. openlog("ssp", LOG_CONS | LOG_PID, LOG_USER);
  58. syslog(LOG_INFO, "%s%s%s()", msg1, msg2, msg3);
  59. closelog();
  60. }
  61. static __always_inline attribute_noreturn void terminate(void)
  62. {
  63. (void) kill(__getpid(), SSP_SIGTYPE);
  64. _exit_internal(127);
  65. }
  66. void attribute_noreturn __stack_smash_handler(char func[], int damaged __attribute__ ((unused)));
  67. void attribute_noreturn __stack_smash_handler(char func[], int damaged)
  68. {
  69. extern char *__progname;
  70. static const char message[] = ": stack smashing attack in function ";
  71. block_signals();
  72. ssp_write(STDERR_FILENO, __progname, message, func);
  73. /* The loop is added only to keep gcc happy. */
  74. while(1)
  75. terminate();
  76. }
  77. void attribute_noreturn __stack_chk_fail(void)
  78. {
  79. extern char *__progname;
  80. static const char msg1[] = "stack smashing detected: ";
  81. static const char msg3[] = " terminated";
  82. block_signals();
  83. ssp_write(STDERR_FILENO, msg1, __progname, msg3);
  84. /* The loop is added only to keep gcc happy. */
  85. while(1)
  86. terminate();
  87. }
  88. #if 0
  89. void attribute_noreturn __chk_fail(void)
  90. {
  91. extern char *__progname;
  92. static const char msg1[] = "buffer overflow detected: ";
  93. static const char msg3[] = " terminated";
  94. block_signals();
  95. ssp_write(STDERR_FILENO, msg1, __progname, msg3);
  96. /* The loop is added only to keep gcc happy. */
  97. while(1)
  98. terminate();
  99. }
  100. #endif