1 /* Stack protector support.
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 You should have received a copy of the GNU General Public License
26 along with GCC; see the file COPYING. If not, write to the Free
27 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
30 /* As a special exception, if you link this library with files compiled with
31 GCC to produce an executable, this does not cause the resulting executable
32 to be covered by the GNU General Public License. This exception does not
33 however invalidate any other reasons why the executable file might be
34 covered by the GNU General Public License. */
53 # define _PATH_TTY "/dev/tty"
59 void *__stack_chk_guard
= 0;
61 static void __attribute__ ((constructor
))
67 if (__stack_chk_guard
!= 0)
70 fd
= open ("/dev/urandom", O_RDONLY
);
73 ssize_t size
= read (fd
, &__stack_chk_guard
,
74 sizeof (__stack_chk_guard
));
76 if (size
== sizeof(__stack_chk_guard
) && __stack_chk_guard
!= 0)
80 /* If a random generator can't be used, the protector switches the guard
81 to the "terminator canary". */
82 p
= (unsigned char *) &__stack_chk_guard
;
83 p
[sizeof(__stack_chk_guard
)-1] = 255;
84 p
[sizeof(__stack_chk_guard
)-2] = '\n';
89 fail (const char *msg1
, size_t msg1len
, const char *msg3
)
91 #ifdef __GNU_LIBRARY__
92 extern char * __progname
;
94 static const char __progname
[] = "";
98 /* Print error message directly to the tty. This avoids Bad Things
99 happening if stderr is redirected. */
100 fd
= open (_PATH_TTY
, O_WRONLY
);
103 static const char msg2
[] = " terminated\n";
104 size_t progname_len
, len
;
107 progname_len
= strlen (__progname
);
108 len
= msg1len
+ progname_len
+ sizeof(msg2
)-1 + 1;
109 p
= buf
= alloca (len
);
111 memcpy (p
, msg1
, msg1len
);
113 memcpy (p
, __progname
, progname_len
);
115 memcpy (p
, msg2
, sizeof(msg2
));
119 ssize_t wrote
= write (fd
, buf
, len
);
129 /* Only send the error to syslog if there was no tty available. */
131 syslog (LOG_CRIT
, msg3
);
132 #endif /* HAVE_SYSLOG_H */
134 /* Try very hard to exit. Note that signals may be blocked preventing
135 the first two options from working. The use of volatile is here to
136 prevent optimizers from "knowing" that __builtin_trap is called first,
137 and that it doesn't return, and so "obviously" the rest of the code
141 for (state
= 0; ; state
++)
148 *(volatile int *)-1L = 0;
158 __stack_chk_fail (void)
160 const char *msg
= "*** stack smashing detected ***: ";
161 fail (msg
, strlen (msg
), "stack smashing detected: terminated");
167 const char *msg
= "*** buffer overflow detected ***: ";
168 fail (msg
, strlen (msg
), "buffer overflow detected: terminated");
171 #ifdef HAVE_HIDDEN_VISIBILITY
173 __attribute__((visibility ("hidden")))
174 __stack_chk_fail_local (void)