2 * Distributed under the terms of the GNU Lesser General Public License
5 * This is a modified version of Hiroaki Etoh's stack smashing routines
6 * implemented for glibc.
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>
19 #if defined __SSP__ || defined __SSP_ALL__
20 #error "file must not be compiled with stack protection enabled on it. Use -fno-stack-protector"
23 #ifdef __PROPOLICE_BLOCK_SEGV__
24 # define SSP_SIGTYPE SIGSEGV
26 # define SSP_SIGTYPE SIGABRT
29 #define openlog __openlog
30 #define syslog __syslog
31 #define closelog __closelog
32 #define sigfillset __sigfillset_internal
33 #define sigdelset __sigdelset_internal
34 #define sigaction __sigaction_internal
40 #include <sys/syslog.h>
42 static __always_inline void block_signals(void)
49 sigdelset(&mask, SSP_SIGTYPE); /* Block all signal handlers */
50 __sigprocmask(SIG_BLOCK, &mask, NULL); /* except SSP_SIGTYPE */
52 /* Make the default handler associated with the signal handler */
53 __memset(&sa, 0, sizeof(struct sigaction));
54 sigfillset(&sa.sa_mask); /* Block all signals */
56 sa.sa_handler = SIG_DFL;
57 sigaction(SSP_SIGTYPE, &sa, NULL);
60 static __always_inline void ssp_write(int fd, const char *msg1, const char *msg2, const char *msg3)
62 __write(fd, msg1, __strlen(msg1));
63 __write(fd, msg2, __strlen(msg2));
64 __write(fd, msg3, __strlen(msg3));
65 __write(fd, "()\n", 3);
66 openlog("ssp", LOG_CONS | LOG_PID, LOG_USER);
67 syslog(LOG_INFO, "%s%s%s()", msg1, msg2, msg3);
71 static __always_inline attribute_noreturn void terminate(void)
73 (void) kill(__getpid(), SSP_SIGTYPE);
77 void attribute_noreturn __stack_smash_handler(char func[], int damaged __attribute__ ((unused)));
78 void attribute_noreturn __stack_smash_handler(char func[], int damaged)
80 extern char *__progname;
81 static const char message[] = ": stack smashing attack in function ";
85 ssp_write(STDERR_FILENO, __progname, message, func);
87 /* The loop is added only to keep gcc happy. */
92 void attribute_noreturn __stack_chk_fail(void)
94 extern char *__progname;
95 static const char msg1[] = "stack smashing detected: ";
96 static const char msg3[] = " terminated";
100 ssp_write(STDERR_FILENO, msg1, __progname, msg3);
102 /* The loop is added only to keep gcc happy. */
108 void attribute_noreturn __chk_fail(void)
110 extern char *__progname;
111 static const char msg1[] = "buffer overflow detected: ";
112 static const char msg3[] = " terminated";
116 ssp_write(STDERR_FILENO, msg1, __progname, msg3);
118 /* The loop is added only to keep gcc happy. */