1 #include <l4/sys/ipc.h>
2 
3 #include <stdint.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <signal.h>
7 
8 // taken from uclibc
9 #if defined __SSP__ || defined __SSP_ALL__
10 # error "file must not be compiled with stack protection enabled on it. Use -fno-stack-protector"
11 #endif
12 
13 #ifdef UCLIBC_LDSO
14 # define PROGNAME UCLIBC_LDSO
15 #else
16 # define PROGNAME __uclibc_progname
17 #endif
18 
19 uintptr_t _dl_setup_stack_chk_guard(void);
_dl_setup_stack_chk_guard(void)20 uintptr_t _dl_setup_stack_chk_guard(void)
21 {
22   return 0xFF0A0D00UL; //terminator canary
23 }
24 
25 #ifndef UCLIBC_LDSO // do not build for ldso
26 
terminate(void)27 inline static void terminate(void)
28 {
29   l4_ipc_sleep(l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_rel(2, 9)));
30 }
31 
ssp_write(int fd,const char * msg1,const char * msg2,const char * msg3)32 static void __cold ssp_write(int fd, const char *msg1, const char *msg2, const char *msg3)
33 {
34   write(fd, msg1, strlen(msg1));
35   write(fd, msg2, strlen(msg2));
36   write(fd, msg3, strlen(msg3));
37   write(fd, "()\n", 3);
38 }
39 
40 void __attribute__((noreturn)) __stack_chk_fail(void);
__stack_chk_fail(void)41 void __attribute__((noreturn)) __stack_chk_fail(void)
42 {
43   static const char msg1[] = "stack smashing detected: ";
44   static const char msg3[] = " terminated";
45 
46   ssp_write(STDERR_FILENO, msg1, PROGNAME, msg3);
47 
48   /* The loop is added only to keep gcc happy. */
49   while(1)
50     terminate();
51 }
52 
53 void __attribute__((noreturn)) __stack_smash_handler(char func[], int damaged __attribute__ ((unused)));
__stack_smash_handler(char func[],int damaged)54 void __attribute__((noreturn)) __stack_smash_handler(char func[], int damaged)
55 {
56   static const char message[] = ": stack smashing attack in function ";
57 
58   ssp_write(STDERR_FILENO, PROGNAME,  message, func);
59 
60   /* The loop is added only to keep gcc happy. */
61   while(1)
62     terminate();
63 }
64 
65 void __attribute__((noreturn)) __chk_fail(void);
libc_hidden_proto(__chk_fail)66 libc_hidden_proto(__chk_fail)
67 void __attribute__((noreturn)) __chk_fail(void)
68 {
69   static const char msg1[] = "buffer overflow detected: ";
70   static const char msg3[] = " terminated";
71 
72   ssp_write(STDERR_FILENO, msg1, PROGNAME, msg3);
73 
74   /* The loop is added only to keep gcc happy. */
75   while(1)
76     terminate();
77 }
78 libc_hidden_def(__chk_fail)
79 
80 #endif // UCLIBC_LDSO
81