ssp.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Stack protector support.
  2. Copyright (C) 2005-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. In addition to the permissions in the GNU General Public License, the
  9. Free Software Foundation gives you unlimited permission to link the
  10. compiled version of this file into combinations with other programs,
  11. and to distribute those combinations without any restriction coming
  12. from the use of this file. (The General Public License restrictions
  13. do apply in other respects; for example, they cover modification of
  14. the file, and distribution when not linked into a combine
  15. executable.)
  16. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  17. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  19. for more details.
  20. Under Section 7 of GPL version 3, you are granted additional
  21. permissions described in the GCC Runtime Library Exception, version
  22. 3.1, as published by the Free Software Foundation.
  23. You should have received a copy of the GNU General Public License and
  24. a copy of the GCC Runtime Library Exception along with this program;
  25. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  26. <http://www.gnu.org/licenses/>. */
  27. #include "config.h"
  28. #ifdef HAVE_ALLOCA_H
  29. # include <alloca.h>
  30. #endif
  31. #ifdef HAVE_MALLOC_H
  32. # include <malloc.h>
  33. #endif
  34. #ifdef HAVE_STRING_H
  35. # include <string.h>
  36. #endif
  37. #ifdef HAVE_UNISTD_H
  38. # include <unistd.h>
  39. #endif
  40. #ifdef HAVE_FCNTL_H
  41. # include <fcntl.h>
  42. #endif
  43. #ifdef HAVE_PATHS_H
  44. # include <paths.h>
  45. #endif
  46. #ifndef _PATH_TTY
  47. /* Native win32 apps don't know about /dev/tty but can print directly
  48. to the console using "CONOUT$" */
  49. #if defined (_WIN32) && !defined (__CYGWIN__)
  50. #include <windows.h>
  51. #include <wincrypt.h>
  52. # define _PATH_TTY "CONOUT$"
  53. #else
  54. # define _PATH_TTY "/dev/tty"
  55. #endif
  56. #endif
  57. #ifdef HAVE_SYSLOG_H
  58. # include <syslog.h>
  59. #endif
  60. void *__stack_chk_guard = 0;
  61. static void __attribute__ ((constructor))
  62. __guard_setup (void)
  63. {
  64. unsigned char *p;
  65. if (__stack_chk_guard != 0)
  66. return;
  67. #if defined (_WIN32) && !defined (__CYGWIN__)
  68. HCRYPTPROV hprovider = 0;
  69. if (CryptAcquireContext(&hprovider, NULL, NULL, PROV_RSA_FULL,
  70. CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
  71. {
  72. if (CryptGenRandom(hprovider, sizeof (__stack_chk_guard),
  73. (BYTE *)&__stack_chk_guard) && __stack_chk_guard != 0)
  74. {
  75. CryptReleaseContext(hprovider, 0);
  76. return;
  77. }
  78. CryptReleaseContext(hprovider, 0);
  79. }
  80. #else
  81. int fd = open ("/dev/urandom", O_RDONLY);
  82. if (fd != -1)
  83. {
  84. ssize_t size = read (fd, &__stack_chk_guard,
  85. sizeof (__stack_chk_guard));
  86. close (fd);
  87. if (size == sizeof(__stack_chk_guard) && __stack_chk_guard != 0)
  88. return;
  89. }
  90. #endif
  91. /* If a random generator can't be used, the protector switches the guard
  92. to the "terminator canary". */
  93. p = (unsigned char *) &__stack_chk_guard;
  94. p[sizeof(__stack_chk_guard)-1] = 255;
  95. p[sizeof(__stack_chk_guard)-2] = '\n';
  96. p[0] = 0;
  97. }
  98. static void
  99. fail (const char *msg1, size_t msg1len, const char *msg3)
  100. {
  101. #ifdef __GNU_LIBRARY__
  102. extern char * __progname;
  103. #else
  104. static const char __progname[] = "";
  105. #endif
  106. int fd;
  107. /* Print error message directly to the tty. This avoids Bad Things
  108. happening if stderr is redirected. */
  109. fd = open (_PATH_TTY, O_WRONLY);
  110. if (fd != -1)
  111. {
  112. static const char msg2[] = " terminated\n";
  113. size_t progname_len, len;
  114. char *buf, *p;
  115. progname_len = strlen (__progname);
  116. len = msg1len + progname_len + sizeof(msg2)-1 + 1;
  117. p = buf = alloca (len);
  118. memcpy (p, msg1, msg1len);
  119. p += msg1len;
  120. memcpy (p, __progname, progname_len);
  121. p += progname_len;
  122. memcpy (p, msg2, sizeof(msg2));
  123. while (len > 0)
  124. {
  125. ssize_t wrote = write (fd, buf, len);
  126. if (wrote < 0)
  127. break;
  128. buf += wrote;
  129. len -= wrote;
  130. }
  131. close (fd);
  132. }
  133. #ifdef HAVE_SYSLOG_H
  134. /* Only send the error to syslog if there was no tty available. */
  135. else
  136. syslog (LOG_CRIT, "%s", msg3);
  137. #endif /* HAVE_SYSLOG_H */
  138. /* Try very hard to exit. Note that signals may be blocked preventing
  139. the first two options from working. The use of volatile is here to
  140. prevent optimizers from "knowing" that __builtin_trap is called first,
  141. and that it doesn't return, and so "obviously" the rest of the code
  142. is dead. */
  143. {
  144. volatile int state;
  145. for (state = 0; ; state++)
  146. switch (state)
  147. {
  148. case 0:
  149. __builtin_trap ();
  150. break;
  151. case 1:
  152. *(volatile int *)-1L = 0;
  153. break;
  154. case 2:
  155. _exit (127);
  156. break;
  157. }
  158. }
  159. }
  160. void
  161. __stack_chk_fail (void)
  162. {
  163. const char *msg = "*** stack smashing detected ***: ";
  164. fail (msg, strlen (msg), "stack smashing detected: terminated");
  165. }
  166. void
  167. __chk_fail (void)
  168. {
  169. const char *msg = "*** buffer overflow detected ***: ";
  170. fail (msg, strlen (msg), "buffer overflow detected: terminated");
  171. }
  172. #ifdef HAVE_HIDDEN_VISIBILITY
  173. void
  174. __attribute__((visibility ("hidden")))
  175. __stack_chk_fail_local (void)
  176. {
  177. __stack_chk_fail ();
  178. }
  179. #endif