ssp.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #ifdef __UCLIBC_HAS_SSP_COMPAT__
  64. void __stack_smash_handler(char func[], int damaged __attribute__ ((unused))) attribute_noreturn __cold;
  65. void __stack_smash_handler(char func[], int damaged)
  66. {
  67. static const char message[] = ": stack smashing attack in function ";
  68. block_signals();
  69. ssp_write(STDERR_FILENO, __uclibc_progname, message, func);
  70. /* The loop is added only to keep gcc happy. */
  71. while(1)
  72. terminate();
  73. }
  74. #endif
  75. #ifdef __UCLIBC_HAS_SSP__
  76. void __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. #endif
  87. #ifdef __UCLIBC_HAS_FORTIFY__
  88. void __chk_fail(void)
  89. {
  90. static const char msg1[] = "buffer overflow detected: ";
  91. static const char msg3[] = " terminated";
  92. block_signals();
  93. ssp_write(STDERR_FILENO, msg1, __uclibc_progname, msg3);
  94. /* The loop is added only to keep gcc happy. */
  95. while(1)
  96. terminate();
  97. }
  98. libc_hidden_def(__chk_fail)
  99. #endif