1 /* Stack protector support. 2 Copyright (C) 2005-2018 Free Software Foundation, Inc. 3 4 This file is part of GCC. 5 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 9 version. 10 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 18 executable.) 19 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 23 for more details. 24 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. 28 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/>. */ 33 34 35 #include "config.h" 36 #ifdef HAVE_ALLOCA_H 37 # include <alloca.h> 38 #else 39 # include <stdlib.h> 40 #endif 41 #ifdef HAVE_MALLOC_H 42 # include <malloc.h> 43 #endif 44 #ifdef HAVE_STRING_H 45 # include <string.h> 46 #endif 47 #ifdef HAVE_UNISTD_H 48 # include <unistd.h> 49 #endif 50 #ifdef HAVE_FCNTL_H 51 # include <fcntl.h> 52 #endif 53 #ifdef HAVE_PATHS_H 54 # include <paths.h> 55 #endif 56 #ifndef _PATH_TTY 57 /* Native win32 apps don't know about /dev/tty but can print directly 58 to the console using "CONOUT$" */ 59 #if defined (_WIN32) && !defined (__CYGWIN__) 60 #include <windows.h> 61 #include <wincrypt.h> 62 # define _PATH_TTY "CONOUT$" 63 #else 64 # define _PATH_TTY "/dev/tty" 65 #endif 66 #endif 67 #ifdef HAVE_SYSLOG_H 68 # include <syslog.h> 69 #endif 70 71 void *__stack_chk_guard = 0; 72 73 static void __attribute__ ((constructor)) 74 __guard_setup (void) 75 { 76 unsigned char *p; 77 78 if (__stack_chk_guard != 0) 79 return; 80 81 #if defined (_WIN32) && !defined (__CYGWIN__) 82 HCRYPTPROV hprovider = 0; 83 if (CryptAcquireContext(&hprovider, NULL, NULL, PROV_RSA_FULL, 84 CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) 85 { 86 if (CryptGenRandom(hprovider, sizeof (__stack_chk_guard), 87 (BYTE *)&__stack_chk_guard) && __stack_chk_guard != 0) 88 { 89 CryptReleaseContext(hprovider, 0); 90 return; 91 } 92 CryptReleaseContext(hprovider, 0); 93 } 94 #else 95 int fd = open ("/dev/urandom", O_RDONLY); 96 if (fd != -1) 97 { 98 ssize_t size = read (fd, &__stack_chk_guard, 99 sizeof (__stack_chk_guard)); 100 close (fd); 101 if (size == sizeof(__stack_chk_guard) && __stack_chk_guard != 0) 102 return; 103 } 104 105 #endif 106 /* If a random generator can't be used, the protector switches the guard 107 to the "terminator canary". */ 108 p = (unsigned char *) &__stack_chk_guard; 109 p[sizeof(__stack_chk_guard)-1] = 255; 110 p[sizeof(__stack_chk_guard)-2] = '\n'; 111 p[0] = 0; 112 } 113 114 static void 115 fail (const char *msg1, size_t msg1len, const char *msg3) 116 { 117 #ifdef __GNU_LIBRARY__ 118 extern char * __progname; 119 #else 120 static const char __progname[] = ""; 121 #endif 122 int fd; 123 124 /* Print error message directly to the tty. This avoids Bad Things 125 happening if stderr is redirected. */ 126 fd = open (_PATH_TTY, O_WRONLY); 127 if (fd != -1) 128 { 129 static const char msg2[] = " terminated\n"; 130 size_t progname_len, len; 131 char *buf, *p; 132 133 progname_len = strlen (__progname); 134 len = msg1len + progname_len + sizeof(msg2)-1 + 1; 135 p = buf = alloca (len); 136 137 memcpy (p, msg1, msg1len); 138 p += msg1len; 139 memcpy (p, __progname, progname_len); 140 p += progname_len; 141 memcpy (p, msg2, sizeof(msg2)); 142 143 while (len > 0) 144 { 145 ssize_t wrote = write (fd, buf, len); 146 if (wrote < 0) 147 break; 148 buf += wrote; 149 len -= wrote; 150 } 151 close (fd); 152 } 153 154 #ifdef HAVE_SYSLOG_H 155 /* Only send the error to syslog if there was no tty available. */ 156 else 157 syslog (LOG_CRIT, "%s", msg3); 158 #endif /* HAVE_SYSLOG_H */ 159 160 /* Try very hard to exit. Note that signals may be blocked preventing 161 the first two options from working. The use of volatile is here to 162 prevent optimizers from "knowing" that __builtin_trap is called first, 163 and that it doesn't return, and so "obviously" the rest of the code 164 is dead. */ 165 { 166 volatile int state; 167 for (state = 0; ; state++) 168 switch (state) 169 { 170 case 0: 171 __builtin_trap (); 172 break; 173 case 1: 174 *(volatile int *)-1L = 0; 175 break; 176 case 2: 177 _exit (127); 178 break; 179 } 180 } 181 } 182 183 void 184 __stack_chk_fail (void) 185 { 186 const char *msg = "*** stack smashing detected ***: "; 187 fail (msg, strlen (msg), "stack smashing detected: terminated"); 188 } 189 190 void 191 __chk_fail (void) 192 { 193 const char *msg = "*** buffer overflow detected ***: "; 194 fail (msg, strlen (msg), "buffer overflow detected: terminated"); 195 } 196 197 #ifdef HAVE_HIDDEN_VISIBILITY 198 void 199 __attribute__((visibility ("hidden"))) 200 __stack_chk_fail_local (void) 201 { 202 __stack_chk_fail (); 203 } 204 #endif 205