]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libssp/ssp.c
[Ada] Get rid of more references to Universal_Integer in expanded code
[thirdparty/gcc.git] / libssp / ssp.c
index 85220a8e375aa952b5766bb2b7fa5275b311f956..28f3e9cc64a2c4faf573ee5878e5ac62e1e7f189 100644 (file)
@@ -1,5 +1,5 @@
 /* Stack protector support.
-   Copyright (C) 2005-2013 Free Software Foundation, Inc.
+   Copyright (C) 2005-2020 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -55,6 +55,8 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 /* Native win32 apps don't know about /dev/tty but can print directly
    to the console using  "CONOUT$"   */
 #if defined (_WIN32) && !defined (__CYGWIN__)
+#include <windows.h>
+#include <wincrypt.h>
 # define _PATH_TTY "CONOUT$"
 #else
 # define _PATH_TTY "/dev/tty"
@@ -70,12 +72,25 @@ static void __attribute__ ((constructor))
 __guard_setup (void)
 {
   unsigned char *p;
-  int fd;
 
   if (__stack_chk_guard != 0)
     return;
 
-  fd = open ("/dev/urandom", O_RDONLY);
+#if defined (_WIN32) && !defined (__CYGWIN__)
+  HCRYPTPROV hprovider = 0;
+  if (CryptAcquireContext(&hprovider, NULL, NULL, PROV_RSA_FULL,
+                          CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
+    {
+      if (CryptGenRandom(hprovider, sizeof (__stack_chk_guard),
+          (BYTE *)&__stack_chk_guard) &&  __stack_chk_guard != 0)
+        {
+           CryptReleaseContext(hprovider, 0);
+           return;
+        }
+      CryptReleaseContext(hprovider, 0);
+    }
+#else
+  int fd = open ("/dev/urandom", O_RDONLY);
   if (fd != -1)
     {
       ssize_t size = read (fd, &__stack_chk_guard,
@@ -85,6 +100,7 @@ __guard_setup (void)
         return;
     }
 
+#endif
   /* If a random generator can't be used, the protector switches the guard
      to the "terminator canary".  */
   p = (unsigned char *) &__stack_chk_guard;
@@ -136,7 +152,7 @@ fail (const char *msg1, size_t msg1len, const char *msg3)
 #ifdef HAVE_SYSLOG_H
   /* Only send the error to syslog if there was no tty available.  */
   else
-    syslog (LOG_CRIT, msg3);
+    syslog (LOG_CRIT, "%s", msg3);
 #endif /* HAVE_SYSLOG_H */
 
   /* Try very hard to exit.  Note that signals may be blocked preventing