ssp.c 3.3 KB

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