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"
26 #ifdef __UCLIBC_HAS_SYSLOG__
27 #include <sys/syslog.h>
30 #ifdef __PROPOLICE_BLOCK_SEGV__
31 # define SSP_SIGTYPE SIGSEGV
33 # define SSP_SIGTYPE SIGABRT
36 static void do_write(const char *msg)
38 /* could use inlined syscall here to be sure ... */
39 return (void) write(STDERR_FILENO, msg, strlen(msg));
42 static void __cold do_msg(const char *msg1, const char *msg2, const char *msg3)
48 #ifdef __UCLIBC_HAS_SYSLOG__
49 syslog(LOG_INFO, "%s%s%s()", msg1, msg2, msg3);
53 static void __cold attribute_noreturn
54 #ifdef __UCLIBC_HAS_SSP_COMPAT__
55 ssp_handler(char func[])
61 static const char msg_ssd[] = "*** stack smashing detected ***: ";
62 static const char msg_terminated[] = " terminated";
63 #ifdef __UCLIBC_HAS_SSP_COMPAT__
64 static const char msg_ssa[] = ": stack smashing attack in function ";
72 __sigdelset(&mask, SSP_SIGTYPE); /* Block all signal handlers */
73 sigprocmask(SIG_BLOCK, &mask, NULL); /* except SSP_SIGTYPE */
76 #ifdef __UCLIBC_HAS_SSP_COMPAT__
78 do_msg(__uclibc_progname, msg_ssa, func);
81 do_msg(msg_ssd, __uclibc_progname, msg_terminated);
85 /* Make the default handler associated with the signal handler */
86 memset(&sa, 0, sizeof(sa));
87 __sigfillset(&sa.sa_mask); /* Block all signals */
88 if (SIG_DFL) /* if it's constant zero, it's already done */
89 sa.sa_handler = SIG_DFL;
90 if (sigaction(SSP_SIGTYPE, &sa, NULL) == 0)
91 (void)kill(pid, SSP_SIGTYPE);
93 (void)kill(pid, SIGKILL);
94 /* The loop is added only to keep gcc happy. */
99 #ifdef __UCLIBC_HAS_SSP_COMPAT__
100 void __stack_smash_handler(char func[], int damaged) attribute_noreturn __cold;
101 void __stack_smash_handler(char func[], int damaged attribute_unused)
106 void __stack_chk_fail(void)
111 strong_alias(ssp_handler,__stack_chk_fail)
114 #ifdef __UCLIBC_HAS_FORTIFY__
115 /* should be redone when activated to use common code above.
116 * for now, it works without debugging support */
117 void __chk_fail(void)
119 static const char msg_fail[] = "*** buffer overflow detected ***: ";
120 static const char msg_terminated[] = " terminated";
123 do_msg(msg_fail, __uclibc_progname, msg_terminated);
126 (void)kill(pid, SIGKILL);
127 /* The loop is added only to keep gcc happy. */
131 libc_hidden_def(__chk_fail)