]> git.saurik.com Git - apple/libc.git/blob - sys/OpenBSD/stack_protector.c.patch
e6766cc7b6b6aee38e547f0785531391777f50ea
[apple/libc.git] / sys / OpenBSD / stack_protector.c.patch
1 Index: stack_protector.c
2 ===================================================================
3 --- stack_protector.c (revision 31377)
4 +++ stack_protector.c (working copy)
5 @@ -32,44 +32,41 @@ static char rcsid[] = "$OpenBSD: stack_p
6 #include <sys/param.h>
7 #include <sys/sysctl.h>
8 #include <syslog.h>
9 +#include <sys/types.h>
10 +#include <unistd.h>
11 +#include <fcntl.h>
12 +
13 +extern void __abort(void) __dead2;
14 +long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
15 +void __guard_setup(void) __attribute__ ((visibility ("hidden")));
16 +void __stack_chk_fail(void);
17
18 -long __guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
19 -static void __guard_setup(void) __attribute__ ((constructor));
20 -void __stack_smash_handler(char func[], int damaged __attribute__((unused)));
21 -
22 -static void
23 +void
24 __guard_setup(void)
25 {
26 int fd;
27 - if (__guard[0]!=0) return;
28 + if (__stack_chk_guard[0]!=0) return;
29 fd = open ("/dev/urandom", 0);
30 if (fd != -1) {
31 - ssize_t size = read (fd, (char*)&__guard, sizeof(__guard));
32 + ssize_t size = read (fd, (char*)&__stack_chk_guard,
33 + sizeof(__stack_chk_guard));
34 close (fd) ;
35 - if (size == sizeof(__guard)) return;
36 + if (size == sizeof(__stack_chk_guard)
37 + && *__stack_chk_guard != 0) return;
38 }
39 /* If a random generator can't be used, the protector switches the guard
40 to the "terminator canary" */
41 - ((char*)__guard)[0] = 0; ((char*)__guard)[1] = 0;
42 - ((char*)__guard)[2] = '\n'; ((char*)__guard)[3] = 255;
43 + ((char*)__stack_chk_guard)[0] = 0; ((char*)__stack_chk_guard)[1] = 0;
44 + ((char*)__stack_chk_guard)[2] = '\n'; ((char*)__stack_chk_guard)[3] = 255;
45 }
46
47 void
48 -__stack_smash_handler(char func[], int damaged)
49 +__stack_chk_fail()
50 {
51 - const char message[] = "stack overflow in function %s";
52 - struct sigaction sa;
53 + const char message[] = "[%d] stack overflow";
54
55 /* this may fail on a chroot jail, though luck */
56 - syslog(LOG_CRIT, message, func);
57 -
58 - bzero(&sa, sizeof(struct sigaction));
59 - sigemptyset(&sa.sa_mask);
60 - sa.sa_flags = 0;
61 - sa.sa_handler = SIG_DFL;
62 - sigaction(SIGABRT, &sa, NULL);
63 -
64 - kill(getpid(), SIGABRT);
65 + syslog(LOG_CRIT, message, getpid());
66
67 - _exit(127);
68 + __abort();
69 }