ssp.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. static const char message[] = ": stack smashing attack in function ";
  70. block_signals();
  71. ssp_write(STDERR_FILENO, __uclibc_progname, message, func);
  72. /* The loop is added only to keep gcc happy. */
  73. while(1)
  74. terminate();
  75. }
  76. void attribute_noreturn __stack_chk_fail(void)
  77. {
  78. static const char msg1[] = "stack smashing detected: ";
  79. static const char msg3[] = " terminated";
  80. block_signals();
  81. ssp_write(STDERR_FILENO, msg1, __uclibc_progname, msg3);
  82. /* The loop is added only to keep gcc happy. */
  83. while(1)
  84. terminate();
  85. }
  86. #if 0
  87. void attribute_noreturn __chk_fail(void)
  88. {
  89. static const char msg1[] = "buffer overflow detected: ";
  90. static const char msg3[] = " terminated";
  91. block_signals();
  92. ssp_write(STDERR_FILENO, msg1, __uclibc_progname, msg3);
  93. /* The loop is added only to keep gcc happy. */
  94. while(1)
  95. terminate();
  96. }
  97. #endif