1 --- stack_protector.c.orig 2010-10-07 09:55:01.000000000 -0700
2 +++ stack_protector.c 2010-10-07 09:55:30.000000000 -0700
7 -#include <sys/sysctl.h>
14 +#include <sys/types.h>
16 +#include "CrashReporterClient.h"
21 +long __stack_chk_guard[GUARD_MAX] = {0, 0, 0, 0, 0, 0, 0, 0};
22 +void __abort(void) __dead2;
23 +void __guard_setup(const char *apple[]) __attribute__ ((visibility ("hidden")));
24 +void __stack_chk_fail(void);
26 -extern int __sysctl(int *, u_int, void *, size_t *, void *, size_t);
28 +__guard_from_kernel(const char *str)
30 + unsigned long long val;
34 + /* Skip over the 'stack_guard=' key to the list of values */
35 + str = strchr(str, '=');
40 -long __guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
41 -static void __guard_setup(void) __attribute__ ((constructor));
42 -void __stack_smash_handler(char func[], int damaged __attribute__((unused)));
43 + while (str && idx < GUARD_MAX) {
45 + * Pull the next numeric string out of the list and convert it to
48 + strlcpy(tmp, str, 20);
49 + p = strchr(tmp, ',');
52 + val = strtoull(tmp, NULL, 0);
53 + __stack_chk_guard[idx] = (long)(val & ((unsigned long) -1));
55 + if ((str = strchr(str, ',')) != NULL)
63 +__guard_setup(const char *apple[])
70 - if (__guard[0] != 0)
71 + if (__stack_chk_guard[0] != 0)
76 + for (p = apple; p && *p; p++) {
77 + if (strstr(*p, "stack_guard") == *p) {
78 + __guard_from_kernel(*p);
79 + if (__stack_chk_guard[0] != 0)
84 - len = sizeof(__guard);
85 - if (__sysctl(mib, 2, __guard, &len, NULL, 0) == -1 ||
86 - len != sizeof(__guard)) {
87 - /* If sysctl was unsuccessful, use the "terminator canary". */
88 - ((unsigned char *)__guard)[0] = 0;
89 - ((unsigned char *)__guard)[1] = 0;
90 - ((unsigned char *)__guard)[2] = '\n';
91 - ((unsigned char *)__guard)[3] = 255;
92 + fd = open ("/dev/urandom", 0);
94 + len = read (fd, (char*)&__stack_chk_guard, sizeof(__stack_chk_guard));
96 + if (len == sizeof(__stack_chk_guard) &&
97 + *__stack_chk_guard != 0)
101 + /* If If a random generator can't be used, the protector switches the guard
102 + to the "terminator canary" */
103 + ((unsigned char *)__stack_chk_guard)[0] = 0;
104 + ((unsigned char *)__stack_chk_guard)[1] = 0;
105 + ((unsigned char *)__stack_chk_guard)[2] = '\n';
106 + ((unsigned char *)__stack_chk_guard)[3] = 255;
110 +#define STACKOVERFLOW "] stack overflow"
113 -__stack_smash_handler(char func[], int damaged)
116 - struct syslog_data sdata = SYSLOG_DATA_INIT;
117 - const char message[] = "stack overflow in function %s";
118 - struct sigaction sa;
121 - /* Immediately block all signal handlers from running code */
123 - sigdelset(&mask, SIGABRT);
124 - sigprocmask(SIG_BLOCK, &mask, NULL);
126 + char n[16]; // bigger than will hold the digits in a pid_t
128 + int pid = getpid();
129 + char message[sizeof(n) + sizeof(STACKOVERFLOW)] = "[";
130 + char prog[2*MAXCOMLEN+1] = {0};
132 + proc_name(pid, prog, 2*MAXCOMLEN);
133 + prog[2*MAXCOMLEN] = 0;
134 + np = n + sizeof(n);
137 + *--np = (pid % 10) + '0';
140 + strlcat(message, np, sizeof(message));
141 + strlcat(message, STACKOVERFLOW, sizeof(message));
142 /* This may fail on a chroot jail... */
143 - syslog_r(LOG_CRIT, &sdata, message, func);
145 - bzero(&sa, sizeof(struct sigaction));
146 - sigemptyset(&sa.sa_mask);
148 - sa.sa_handler = SIG_DFL;
149 - sigaction(SIGABRT, &sa, NULL);
151 - kill(getpid(), SIGABRT);
152 + _simple_asl_log_prog(ASL_LEVEL_CRIT, "user", message, prog);
155 + CRSetCrashLogMessage(message);