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 
19 #if defined __SSP__ || defined __SSP_ALL__
20 #error "file must not be compiled with stack protection enabled on it. Use -fno-stack-protector"
21 #endif
22 
23 #include <string.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #ifdef __UCLIBC_HAS_SYSLOG__
27 #include <sys/syslog.h>
28 #endif
29 
30 #ifdef __PROPOLICE_BLOCK_SEGV__
31 # define SSP_SIGTYPE SIGSEGV
32 #else
33 # define SSP_SIGTYPE SIGABRT
34 #endif
35 
do_write(const char * msg)36 static void do_write(const char *msg)
37 {
38 	/* could use inlined syscall here to be sure ... */
39 	return (void) write(STDERR_FILENO, msg, strlen(msg));
40 }
41 
do_msg(const char * msg1,const char * msg2,const char * msg3)42 static void __cold do_msg(const char *msg1, const char *msg2, const char *msg3)
43 {
44 	do_write(msg1);
45 	do_write(msg2);
46 	do_write(msg3);
47 	do_write("\n");
48 #ifdef __UCLIBC_HAS_SYSLOG__
49 	syslog(LOG_INFO, "%s%s%s()", msg1, msg2, msg3);
50 #endif
51 }
52 
53 static void __cold attribute_noreturn
ssp_handler(void)54 ssp_handler(void)
55 {
56 	pid_t pid;
57 	static const char msg_ssd[] = "*** stack smashing detected ***: ";
58 	static const char msg_terminated[] = " terminated";
59 
60 #ifdef __DODEBUG__
61 	struct sigaction sa;
62 	sigset_t mask;
63 
64 	__sigfillset(&mask);
65 	__sigdelset(&mask, SSP_SIGTYPE);	/* Block all signal handlers */
66 	sigprocmask(SIG_BLOCK, &mask, NULL);	/* except SSP_SIGTYPE */
67 #endif
68 
69 	do_msg(msg_ssd, __uclibc_progname, msg_terminated);
70 
71 	pid = getpid();
72 #ifdef __DODEBUG__
73 	/* Make the default handler associated with the signal handler */
74 	memset(&sa, 0, sizeof(sa));
75 	__sigfillset(&sa.sa_mask);	/* Block all signals */
76 	if (SIG_DFL) /* if it's constant zero, it's already done */
77 		sa.sa_handler = SIG_DFL;
78 	if (sigaction(SSP_SIGTYPE, &sa, NULL) == 0)
79 		(void)kill(pid, SSP_SIGTYPE);
80 #endif
81 	(void)kill(pid, SIGKILL);
82 	/* The loop is added only to keep gcc happy. */
83 	while(1)
84 		_exit(127);
85 }
86 
87 strong_alias(ssp_handler,__stack_chk_fail)
88