ssp.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include <sys/syslog.h>
  30. libc_hidden_proto(memset)
  31. libc_hidden_proto(strlen)
  32. libc_hidden_proto(sigaction)
  33. libc_hidden_proto(sigfillset)
  34. libc_hidden_proto(sigdelset)
  35. libc_hidden_proto(sigprocmask)
  36. libc_hidden_proto(write)
  37. libc_hidden_proto(openlog)
  38. libc_hidden_proto(syslog)
  39. libc_hidden_proto(closelog)
  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(struct sigaction));
  52. sigfillset(&sa.sa_mask); /* Block all signals */
  53. sa.sa_flags = 0;
  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)
  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. openlog("ssp", LOG_CONS | LOG_PID, LOG_USER);
  64. syslog(LOG_INFO, "%s%s%s()", msg1, msg2, msg3);
  65. closelog();
  66. }
  67. static attribute_noreturn void terminate(void)
  68. {
  69. (void) kill(getpid(), SSP_SIGTYPE);
  70. _exit(127);
  71. }
  72. void __stack_smash_handler(char func[], int damaged __attribute__ ((unused))) attribute_noreturn;
  73. void __stack_smash_handler(char func[], int damaged)
  74. {
  75. static const char message[] = ": stack smashing attack in function ";
  76. block_signals();
  77. ssp_write(STDERR_FILENO, __uclibc_progname, message, func);
  78. /* The loop is added only to keep gcc happy. */
  79. while(1)
  80. terminate();
  81. }
  82. void __stack_chk_fail(void) attribute_noreturn;
  83. void __stack_chk_fail(void)
  84. {
  85. static const char msg1[] = "stack smashing detected: ";
  86. static const char msg3[] = " terminated";
  87. block_signals();
  88. ssp_write(STDERR_FILENO, msg1, __uclibc_progname, msg3);
  89. /* The loop is added only to keep gcc happy. */
  90. while(1)
  91. terminate();
  92. }
  93. #if 0
  94. void __chk_fail(void) attribute_noreturn;
  95. void __chk_fail(void)
  96. {
  97. static const char msg1[] = "buffer overflow detected: ";
  98. static const char msg3[] = " terminated";
  99. block_signals();
  100. ssp_write(STDERR_FILENO, msg1, __uclibc_progname, msg3);
  101. /* The loop is added only to keep gcc happy. */
  102. while(1)
  103. terminate();
  104. }
  105. #endif