ssp.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include <string.h>
  27. #include <unistd.h>
  28. #include <signal.h>
  29. #if defined __UCLIBC_HAS_SYSLOG__
  30. #include <sys/syslog.h>
  31. #endif
  32. static void block_signals(void)
  33. {
  34. struct sigaction sa;
  35. sigset_t mask;
  36. __sigfillset(&mask);
  37. __sigdelset(&mask, SSP_SIGTYPE); /* Block all signal handlers */
  38. sigprocmask(SIG_BLOCK, &mask, NULL); /* except SSP_SIGTYPE */
  39. /* Make the default handler associated with the signal handler */
  40. memset(&sa, 0, sizeof(sa));
  41. __sigfillset(&sa.sa_mask); /* Block all signals */
  42. if (SIG_DFL) /* if it's constant zero, it's already done */
  43. sa.sa_handler = SIG_DFL;
  44. sigaction(SSP_SIGTYPE, &sa, NULL);
  45. }
  46. static void __cold ssp_write(int fd, const char *msg1, const char *msg2, const char *msg3)
  47. {
  48. write(fd, msg1, strlen(msg1));
  49. write(fd, msg2, strlen(msg2));
  50. write(fd, msg3, strlen(msg3));
  51. write(fd, "()\n", 3);
  52. #if defined __UCLIBC_HAS_SYSLOG__
  53. openlog("ssp", LOG_CONS | LOG_PID, LOG_USER);
  54. syslog(LOG_INFO, "%s%s%s()", msg1, msg2, msg3);
  55. closelog();
  56. #endif
  57. }
  58. static attribute_noreturn void terminate(void)
  59. {
  60. (void) kill(getpid(), SSP_SIGTYPE);
  61. _exit(127);
  62. }
  63. void __stack_smash_handler(char func[], int damaged __attribute__ ((unused))) attribute_noreturn __cold;
  64. void __stack_smash_handler(char func[], int damaged)
  65. {
  66. static const char message[] = ": stack smashing attack in function ";
  67. block_signals();
  68. ssp_write(STDERR_FILENO, __uclibc_progname, message, func);
  69. /* The loop is added only to keep gcc happy. */
  70. while(1)
  71. terminate();
  72. }
  73. void __stack_chk_fail(void) attribute_noreturn __cold;
  74. void __stack_chk_fail(void)
  75. {
  76. static const char msg1[] = "stack smashing detected: ";
  77. static const char msg3[] = " terminated";
  78. block_signals();
  79. ssp_write(STDERR_FILENO, msg1, __uclibc_progname, msg3);
  80. /* The loop is added only to keep gcc happy. */
  81. while(1)
  82. terminate();
  83. }
  84. void __chk_fail(void) attribute_noreturn;
  85. void __chk_fail(void)
  86. {
  87. static const char msg1[] = "buffer overflow detected: ";
  88. static const char msg3[] = " terminated";
  89. block_signals();
  90. ssp_write(STDERR_FILENO, msg1, __uclibc_progname, msg3);
  91. /* The loop is added only to keep gcc happy. */
  92. while(1)
  93. terminate();
  94. }