comparison libssp/ssp.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents 77e2b8dfacca
children 84e7813d76e9
comparison
equal deleted inserted replaced
68:561a7518be6b 111:04ced10e8804
1 /* Stack protector support. 1 /* Stack protector support.
2 Copyright (C) 2005, 2009 Free Software Foundation, Inc. 2 Copyright (C) 2005-2017 Free Software Foundation, Inc.
3 3
4 This file is part of GCC. 4 This file is part of GCC.
5 5
6 GCC is free software; you can redistribute it and/or modify it under 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 7 the terms of the GNU General Public License as published by the Free
53 #endif 53 #endif
54 #ifndef _PATH_TTY 54 #ifndef _PATH_TTY
55 /* Native win32 apps don't know about /dev/tty but can print directly 55 /* Native win32 apps don't know about /dev/tty but can print directly
56 to the console using "CONOUT$" */ 56 to the console using "CONOUT$" */
57 #if defined (_WIN32) && !defined (__CYGWIN__) 57 #if defined (_WIN32) && !defined (__CYGWIN__)
58 #include <windows.h>
59 #include <wincrypt.h>
58 # define _PATH_TTY "CONOUT$" 60 # define _PATH_TTY "CONOUT$"
59 #else 61 #else
60 # define _PATH_TTY "/dev/tty" 62 # define _PATH_TTY "/dev/tty"
61 #endif 63 #endif
62 #endif 64 #endif
68 70
69 static void __attribute__ ((constructor)) 71 static void __attribute__ ((constructor))
70 __guard_setup (void) 72 __guard_setup (void)
71 { 73 {
72 unsigned char *p; 74 unsigned char *p;
73 int fd;
74 75
75 if (__stack_chk_guard != 0) 76 if (__stack_chk_guard != 0)
76 return; 77 return;
77 78
78 fd = open ("/dev/urandom", O_RDONLY); 79 #if defined (_WIN32) && !defined (__CYGWIN__)
80 HCRYPTPROV hprovider = 0;
81 if (CryptAcquireContext(&hprovider, NULL, NULL, PROV_RSA_FULL,
82 CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
83 {
84 if (CryptGenRandom(hprovider, sizeof (__stack_chk_guard),
85 (BYTE *)&__stack_chk_guard) && __stack_chk_guard != 0)
86 {
87 CryptReleaseContext(hprovider, 0);
88 return;
89 }
90 CryptReleaseContext(hprovider, 0);
91 }
92 #else
93 int fd = open ("/dev/urandom", O_RDONLY);
79 if (fd != -1) 94 if (fd != -1)
80 { 95 {
81 ssize_t size = read (fd, &__stack_chk_guard, 96 ssize_t size = read (fd, &__stack_chk_guard,
82 sizeof (__stack_chk_guard)); 97 sizeof (__stack_chk_guard));
83 close (fd); 98 close (fd);
84 if (size == sizeof(__stack_chk_guard) && __stack_chk_guard != 0) 99 if (size == sizeof(__stack_chk_guard) && __stack_chk_guard != 0)
85 return; 100 return;
86 } 101 }
87 102
103 #endif
88 /* If a random generator can't be used, the protector switches the guard 104 /* If a random generator can't be used, the protector switches the guard
89 to the "terminator canary". */ 105 to the "terminator canary". */
90 p = (unsigned char *) &__stack_chk_guard; 106 p = (unsigned char *) &__stack_chk_guard;
91 p[sizeof(__stack_chk_guard)-1] = 255; 107 p[sizeof(__stack_chk_guard)-1] = 255;
92 p[sizeof(__stack_chk_guard)-2] = '\n'; 108 p[sizeof(__stack_chk_guard)-2] = '\n';
134 } 150 }
135 151
136 #ifdef HAVE_SYSLOG_H 152 #ifdef HAVE_SYSLOG_H
137 /* Only send the error to syslog if there was no tty available. */ 153 /* Only send the error to syslog if there was no tty available. */
138 else 154 else
139 syslog (LOG_CRIT, msg3); 155 syslog (LOG_CRIT, "%s", msg3);
140 #endif /* HAVE_SYSLOG_H */ 156 #endif /* HAVE_SYSLOG_H */
141 157
142 /* Try very hard to exit. Note that signals may be blocked preventing 158 /* Try very hard to exit. Note that signals may be blocked preventing
143 the first two options from working. The use of volatile is here to 159 the first two options from working. The use of volatile is here to
144 prevent optimizers from "knowing" that __builtin_trap is called first, 160 prevent optimizers from "knowing" that __builtin_trap is called first,