1 /* Stack protector support.
2 Copyright (C) 2005-2017 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 3, 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 Under Section 7 of GPL version 3, you are granted additional
26 permissions described in the GCC Runtime Library Exception, version
27 3.1, as published by the Free Software Foundation.
29 You should have received a copy of the GNU General Public License and
30 a copy of the GCC Runtime Library Exception along with this program;
31 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
32 <http://www.gnu.org/licenses/>. */
55 /* Native win32 apps don't know about /dev/tty but can print directly
56 to the console using "CONOUT$" */
57 #if defined (_WIN32) && !defined (__CYGWIN__)
60 # define _PATH_TTY "CONOUT$"
62 # define _PATH_TTY "/dev/tty"
69 void *__stack_chk_guard
= 0;
71 static void __attribute__ ((constructor
))
77 if (__stack_chk_guard
!= 0)
80 #if defined (_WIN32) && !defined (__CYGWIN__)
81 HCRYPTPROV hprovider
= 0;
82 if (CryptAcquireContext(&hprovider
, NULL
, NULL
, PROV_RSA_FULL
,
83 CRYPT_VERIFYCONTEXT
| CRYPT_SILENT
))
85 if (CryptGenRandom(hprovider
, sizeof (__stack_chk_guard
),
86 (BYTE
*)&__stack_chk_guard
) && __stack_chk_guard
!= 0)
88 CryptReleaseContext(hprovider
, 0);
91 CryptReleaseContext(hprovider
, 0);
94 fd
= open ("/dev/urandom", O_RDONLY
);
97 ssize_t size
= read (fd
, &__stack_chk_guard
,
98 sizeof (__stack_chk_guard
));
100 if (size
== sizeof(__stack_chk_guard
) && __stack_chk_guard
!= 0)
105 /* If a random generator can't be used, the protector switches the guard
106 to the "terminator canary". */
107 p
= (unsigned char *) &__stack_chk_guard
;
108 p
[sizeof(__stack_chk_guard
)-1] = 255;
109 p
[sizeof(__stack_chk_guard
)-2] = '\n';
114 fail (const char *msg1
, size_t msg1len
, const char *msg3
)
116 #ifdef __GNU_LIBRARY__
117 extern char * __progname
;
119 static const char __progname
[] = "";
123 /* Print error message directly to the tty. This avoids Bad Things
124 happening if stderr is redirected. */
125 fd
= open (_PATH_TTY
, O_WRONLY
);
128 static const char msg2
[] = " terminated\n";
129 size_t progname_len
, len
;
132 progname_len
= strlen (__progname
);
133 len
= msg1len
+ progname_len
+ sizeof(msg2
)-1 + 1;
134 p
= buf
= alloca (len
);
136 memcpy (p
, msg1
, msg1len
);
138 memcpy (p
, __progname
, progname_len
);
140 memcpy (p
, msg2
, sizeof(msg2
));
144 ssize_t wrote
= write (fd
, buf
, len
);
154 /* Only send the error to syslog if there was no tty available. */
156 syslog (LOG_CRIT
, "%s", msg3
);
157 #endif /* HAVE_SYSLOG_H */
159 /* Try very hard to exit. Note that signals may be blocked preventing
160 the first two options from working. The use of volatile is here to
161 prevent optimizers from "knowing" that __builtin_trap is called first,
162 and that it doesn't return, and so "obviously" the rest of the code
166 for (state
= 0; ; state
++)
173 *(volatile int *)-1L = 0;
183 __stack_chk_fail (void)
185 const char *msg
= "*** stack smashing detected ***: ";
186 fail (msg
, strlen (msg
), "stack smashing detected: terminated");
192 const char *msg
= "*** buffer overflow detected ***: ";
193 fail (msg
, strlen (msg
), "buffer overflow detected: terminated");
196 #ifdef HAVE_HIDDEN_VISIBILITY
198 __attribute__((visibility ("hidden")))
199 __stack_chk_fail_local (void)