]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libc/sysdeps/linux/common/ssp.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libc / sysdeps / linux / common / ssp.c
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
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
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
54 #ifdef __UCLIBC_HAS_SSP_COMPAT__
55 ssp_handler(char func[])
56 #else
57 ssp_handler(void)
58 #endif
59 {
60         pid_t pid;
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 ";
65 #endif
66
67 #ifdef __DODEBUG__
68         struct sigaction sa;
69         sigset_t mask;
70
71         __sigfillset(&mask);
72         __sigdelset(&mask, SSP_SIGTYPE);        /* Block all signal handlers */
73         sigprocmask(SIG_BLOCK, &mask, NULL);    /* except SSP_SIGTYPE */
74 #endif
75
76 #ifdef __UCLIBC_HAS_SSP_COMPAT__
77         if (func != NULL)
78                 do_msg(__uclibc_progname, msg_ssa, func);
79         else
80 #endif
81                 do_msg(msg_ssd, __uclibc_progname, msg_terminated);
82
83         pid = getpid();
84 #ifdef __DODEBUG__
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);
92 #endif
93         (void)kill(pid, SIGKILL);
94         /* The loop is added only to keep gcc happy. */
95         while(1)
96                 _exit(127);
97 }
98
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)
102 {
103         ssp_handler(func);
104 }
105
106 void __stack_chk_fail(void)
107 {
108         ssp_handler(NULL);
109 }
110 #else
111 strong_alias(ssp_handler,__stack_chk_fail)
112 #endif
113
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)
118 {
119         static const char msg_fail[] = "*** buffer overflow detected ***: ";
120         static const char msg_terminated[] = " terminated";
121         pid_t pid;
122
123         do_msg(msg_fail, __uclibc_progname, msg_terminated);
124
125         pid = getpid();
126         (void)kill(pid, SIGKILL);
127         /* The loop is added only to keep gcc happy. */
128         while(1)
129                 _exit(127);
130 }
131 libc_hidden_def(__chk_fail)
132 #endif