ssp.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include <string.h>
  19. #include <unistd.h>
  20. #include <sys/syslog.h>
  21. #include <ssp-internal.h>
  22. static __always_inline void block_signals(void)
  23. {
  24. struct sigaction sa;
  25. sigset_t mask;
  26. sigfillset(&mask);
  27. sigdelset(&mask, SSP_SIGTYPE); /* Block all signal handlers */
  28. SIGPROCMASK(SIG_BLOCK, &mask, NULL); /* except SSP_SIGTYPE */
  29. /* Make the default handler associated with the signal handler */
  30. memset(&sa, 0, sizeof(struct sigaction));
  31. sigfillset(&sa.sa_mask); /* Block all signals */
  32. sa.sa_flags = 0;
  33. sa.sa_handler = SIG_DFL;
  34. SIGACTION(SSP_SIGTYPE, &sa, NULL);
  35. }
  36. static __always_inline void ssp_write(int fd, const char *msg1, const char *msg2, const char *msg3)
  37. {
  38. WRITE(fd, msg1, strlen(msg1));
  39. WRITE(fd, msg2, strlen(msg2));
  40. WRITE(fd, msg3, strlen(msg3));
  41. WRITE(fd, "()\n", 3);
  42. openlog("ssp", LOG_CONS | LOG_PID, LOG_USER);
  43. syslog(LOG_INFO, "%s%s%s()", msg1, msg2, msg3);
  44. closelog();
  45. }
  46. static __always_inline void terminate(void)
  47. {
  48. (void) KILL(GETPID(), SSP_SIGTYPE);
  49. EXIT(127);
  50. }
  51. void __attribute__ ((noreturn)) __stack_smash_handler(char func[], int damaged __attribute__ ((unused)));
  52. void __attribute__ ((noreturn)) __stack_smash_handler(char func[], int damaged)
  53. {
  54. extern char *__progname;
  55. static const char message[] = ": stack smashing attack in function ";
  56. block_signals();
  57. ssp_write(STDERR_FILENO, __progname, message, func);
  58. /* The loop is added only to keep gcc happy. */
  59. while(1)
  60. terminate();
  61. }
  62. void __attribute__ ((noreturn)) __stack_chk_fail(void)
  63. {
  64. extern char *__progname;
  65. static const char msg1[] = "stack smashing detected: ";
  66. static const char msg3[] = " terminated";
  67. block_signals();
  68. ssp_write(STDERR_FILENO, msg1, __progname, msg3);
  69. /* The loop is added only to keep gcc happy. */
  70. while(1)
  71. terminate();
  72. }
  73. #if 0
  74. void __attribute__ ((noreturn)) __chk_fail(void)
  75. {
  76. extern char *__progname;
  77. static const char msg1[] = "buffer overflow detected: ";
  78. static const char msg3[] = " terminated";
  79. block_signals();
  80. ssp_write(STDERR_FILENO, msg1, __progname, msg3);
  81. /* The loop is added only to keep gcc happy. */
  82. while(1)
  83. terminate();
  84. }
  85. #endif