Lines 16-35
Link Here
|
16 |
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
16 |
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
17 |
02111-1307 USA. */ |
17 |
02111-1307 USA. */ |
18 |
|
18 |
|
|
|
19 |
#if defined __SSP__ || defined __SSP_ALL__ |
20 |
|
21 |
/* An SSP failure handler that: |
22 |
* 1) does not use any function calls - which would |
23 |
* otherwise lead to nested calls to stack_chk_fail() |
24 |
* 2) uses no functions from the rest of libc, which |
25 |
* can lead to build problems pulling in copies of |
26 |
* swathes of libc into the other libraries. |
27 |
* This handler would not be valid on architectures for |
28 |
* which a pointer is not either a 32-bit or 64-bit |
29 |
* integer. It's definitely ok for i386, x86_64, and ppc. |
30 |
*/ |
31 |
|
32 |
#if defined __linux__ |
33 |
|
34 |
/* When including headers, define out the function names used |
35 |
* via syscalls, since the syscall macros require that the functions |
36 |
* they declare have the predefined names. This prevents duplicate |
37 |
* declaration issues, which are warnings on gcc-3 but have become |
38 |
* errors on gcc-4. |
39 |
*/ |
40 |
#define exit lcexit |
41 |
#include <stdlib.h> |
42 |
#undef exit |
43 |
#define write lcwrite |
44 |
#define getpid lcgetpid |
45 |
#define close lcclose |
46 |
#include <unistd.h> |
47 |
#undef write |
48 |
#undef getpid |
49 |
#undef close |
50 |
#define kill lckill |
51 |
#include <signal.h> |
52 |
#undef kill |
53 |
#include <linux/unistd.h> |
54 |
#include <sys/types.h> |
55 |
|
56 |
|
57 |
#ifndef __dietlibc__ |
58 |
|
59 |
#include <alloca.h> |
60 |
|
61 |
/* from sysdeps */ |
62 |
#include <socketcall.h> |
63 |
|
64 |
/* for the stuff in bits/socket.h */ |
65 |
#define socket lcsocket |
66 |
#define connect lcconnect |
67 |
#include <sys/socket.h> |
68 |
#undef socket |
69 |
#undef connect |
70 |
#include <sys/un.h> |
71 |
|
72 |
#endif |
73 |
|
74 |
/* Re-configure errno to a local one */ |
75 |
#ifdef errno |
76 |
#undef errno |
77 |
#endif |
78 |
#define errno __stack_chk_fail_errno |
79 |
static unsigned long int __stack_chk_fail_errno; |
80 |
|
81 |
static ssize_t write(int fd, const void *buf, size_t count) __attribute__ ((always_inline)); |
82 |
static _syscall3(ssize_t,write, int,fd, const void *,buf, size_t,count); |
83 |
|
84 |
static void exit(int status) __attribute__ ((always_inline)); |
85 |
static _syscall1(void,exit, int,status); |
86 |
|
87 |
static int kill(pid_t pid, int sig) __attribute__ ((always_inline)); |
88 |
static _syscall2(int,kill, pid_t,pid, int,sig); |
89 |
|
90 |
static pid_t getpid(void) __attribute__ ((always_inline)); |
91 |
static _syscall0(pid_t,getpid); |
92 |
|
93 |
#ifndef __dietlibc__ |
94 |
|
95 |
static int close(int fd) __attribute__ ((always_inline)); |
96 |
static _syscall1(int,close, int,fd); |
97 |
|
98 |
|
99 |
/* socketcall is present on most arches (including x86, arm (some), ppc, ppc64, mips, mips64, sparc, sparc64) |
100 |
* x86_86 and some arm do not have it, but does have socket and connect syscalls |
101 |
* Assume this when socketcall is not available. |
102 |
*/ |
103 |
#ifdef __NR_socketcall |
104 |
|
105 |
static long socketcall(int call, unsigned long *args) __attribute__ ((always_inline)); |
106 |
static _syscall2(long,socketcall, int,call, unsigned long *,args); |
107 |
|
108 |
#define DO_SOCKET(result,domain,type,protocol) \ |
109 |
socketargs[0] = domain; \ |
110 |
socketargs[1] = type; \ |
111 |
socketargs[2] = protocol; \ |
112 |
socketargs[3] = 0; \ |
113 |
result = socketcall(SOCKOP_socket, socketargs) |
114 |
|
115 |
#define DO_CONNECT(result,sockfd,serv_addr,addrlen) \ |
116 |
socketargs[0] = sockfd; \ |
117 |
socketargs[1] = (unsigned long int)serv_addr; \ |
118 |
socketargs[2] = addrlen; \ |
119 |
socketargs[3] = 0; \ |
120 |
result = socketcall(SOCKOP_connect, socketargs) |
121 |
|
122 |
#else |
123 |
|
124 |
static int socket(int domain, int type, int protocol) __attribute__ ((always_inline)); |
125 |
static _syscall3(int,socket, int,domain, int,type, int,protocol); |
126 |
|
127 |
#define DO_SOCKET(result,domain,type,protocol) \ |
128 |
result = socket(domain,type,protocol) |
129 |
|
130 |
static int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen) __attribute__ ((always_inline)); |
131 |
static _syscall3(int,connect, int,sockfd, const struct sockaddr *,serv_addr, socklen_t,addrlen); |
132 |
|
133 |
#define DO_CONNECT(result,sockfd,serv_addr,addrlen) \ |
134 |
result = connect(sockfd,(struct sockaddr *)serv_addr, addrlen) |
135 |
|
136 |
#endif |
137 |
|
138 |
#ifndef _PATH_LOG |
139 |
#define _PATH_LOG "/dev/log" |
140 |
#endif |
141 |
|
142 |
const char path_log[]=_PATH_LOG; |
143 |
|
144 |
extern char *__progname; |
145 |
|
146 |
#else |
147 |
|
148 |
static char *__progname = "<dietapp>"; |
149 |
|
150 |
#endif |
151 |
|
152 |
#else |
153 |
/* not building on linux */ |
154 |
|
155 |
#error "Gentoo SSP support: glibc can only be built with SSP on Linux" |
156 |
|
157 |
#endif |
158 |
|
159 |
#else |
160 |
/* not SSP */ |
161 |
|
162 |
#warning "Gentoo SSP support: building standard upstream handler, glibc itself is not protected" |
163 |
|
19 |
#include <stdio.h> |
164 |
#include <stdio.h> |
20 |
#include <stdlib.h> |
165 |
#include <stdlib.h> |
21 |
|
166 |
|
22 |
|
167 |
|
23 |
extern char **__libc_argv attribute_hidden; |
168 |
extern char **__libc_argv attribute_hidden; |
24 |
|
169 |
|
|
|
170 |
#endif |
171 |
/* endif SSP */ |
172 |
|
173 |
|
25 |
void |
174 |
void |
26 |
__attribute__ ((noreturn)) |
175 |
__attribute__ ((noreturn)) |
27 |
__stack_chk_fail (void) |
176 |
__stack_chk_fail (void) |
28 |
{ |
177 |
{ |
|
|
178 |
#if defined __SSP__ || defined __SSP_ALL__ |
179 |
|
180 |
#define MESSAGE_BUFSIZ 512 |
181 |
pid_t pid; |
182 |
int plen, i; |
183 |
char message[MESSAGE_BUFSIZ]; |
184 |
const char msg_prefix[]="*** stack smashing detected ***: "; |
185 |
const char msg_suffix[]=" terminated\n"; |
186 |
const char msg_unknown[]="<unknown>"; |
187 |
#ifndef __dietlibc__ |
188 |
int log_socket, connect_result; |
189 |
struct sockaddr_un sock; |
190 |
#ifdef __NR_socketcall |
191 |
unsigned long int *socketargs=(unsigned long int *)alloca(sizeof(unsigned long int)*4); |
192 |
#endif |
193 |
#endif |
194 |
|
195 |
/* build message */ |
196 |
#define strconcat(str) \ |
197 |
i=0; while ((str[i] != '\0') && ((i+plen)<(MESSAGE_BUFSIZ-1))) {\ |
198 |
message[plen+i]=str[i];\ |
199 |
i++;\ |
200 |
}\ |
201 |
plen+=i; |
202 |
plen=0; |
203 |
strconcat(msg_prefix); |
204 |
if (__progname != (char *)0) { |
205 |
strconcat(__progname); |
206 |
} else { |
207 |
strconcat(msg_unknown); |
208 |
} |
209 |
strconcat(msg_suffix); |
210 |
message[plen++]='\0'; |
211 |
|
212 |
/* Write out error message to STDERR */ |
213 |
write(STDERR_FILENO, message, plen); |
214 |
|
215 |
#ifndef __dietlibc__ |
216 |
/* Log to syslog; socket write to /dev/log */ |
217 |
|
218 |
/* Build socket address */ |
219 |
sock.sun_family = AF_UNIX; |
220 |
i=0; while ((path_log[i] != '\0') && (i<(sizeof(sock.sun_path)-1))) { |
221 |
sock.sun_path[i]=path_log[i]; |
222 |
i++; |
223 |
} |
224 |
sock.sun_path[i]='\0'; |
225 |
|
226 |
/* Try SOCK_DGRAM connection to syslog */ |
227 |
connect_result=-1; |
228 |
DO_SOCKET(log_socket,AF_UNIX,SOCK_DGRAM,0); |
229 |
if (log_socket != -1) { |
230 |
DO_CONNECT(connect_result,log_socket,(&sock),(sizeof(sock))); |
231 |
} |
232 |
if (connect_result == -1) { |
233 |
if (log_socket != -1) { |
234 |
close(log_socket); |
235 |
} |
236 |
/* Try SOCK_STREAM connection to syslog */ |
237 |
DO_SOCKET(log_socket,AF_UNIX,SOCK_STREAM,0); |
238 |
if (log_socket != -1) { |
239 |
DO_CONNECT(connect_result,log_socket,(&sock),(sizeof(sock))); |
240 |
} |
241 |
} |
242 |
/* If a successful connection was made, log the message */ |
243 |
if (connect_result != -1) { |
244 |
write(log_socket,message,plen); |
245 |
} |
246 |
if (log_socket != -1) { |
247 |
close(log_socket); |
248 |
} |
249 |
#endif |
250 |
|
251 |
/* Suicide - note; sigactions can't be added to SIGKILL, nor can it be masked */ |
252 |
pid=getpid(); |
253 |
kill(pid,SIGKILL); |
254 |
|
255 |
/* In case the kill didn't work, exit anyway |
256 |
* The loop prevents gcc thinking this routine returns |
257 |
*/ |
258 |
while (1) exit(EXIT_FAILURE); |
259 |
|
260 |
#else |
261 |
|
29 |
/* The loop is added only to keep gcc happy. */ |
262 |
/* The loop is added only to keep gcc happy. */ |
30 |
while (1) |
263 |
while (1) |
31 |
__libc_message (1, "*** stack smashing detected ***: %s terminated\n", |
264 |
__libc_message (1, "*** stack smashing detected ***: %s terminated\n", |
32 |
__libc_argv[0] ?: "<unknown>"); |
265 |
__libc_argv[0] ?: "<unknown>"); |
|
|
266 |
|
267 |
#endif |
33 |
} |
268 |
} |
34 |
|
269 |
|
35 |
#ifdef ENABLE_OLD_SSP_COMPAT |
270 |
#ifdef ENABLE_OLD_SSP_COMPAT |