]>
git.saurik.com Git - apple/libc.git/blob - sys/OpenBSD/stack_protector.c
1 /* $OpenBSD: stack_protector.c,v 1.10 2006/03/31 05:34:44 deraadt Exp $ */
4 * Copyright (c) 2002 Hiroaki Etoh, Federico G. Schwindt, and Miodrag Vallat.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/param.h>
36 #include <sys/types.h>
38 #include "CrashReporterClient.h"
43 long __stack_chk_guard
[GUARD_MAX
] = {0, 0, 0, 0, 0, 0, 0, 0};
44 void __abort(void) __dead2
;
45 void __guard_setup(const char *apple
[]) __attribute__ ((visibility ("hidden")));
46 void __stack_chk_fail(void);
49 __guard_from_kernel(const char *str
)
51 unsigned long long val
;
55 /* Skip over the 'stack_guard=' key to the list of values */
56 str
= strchr(str
, '=');
61 while (str
&& idx
< GUARD_MAX
) {
63 * Pull the next numeric string out of the list and convert it to
66 strlcpy(tmp
, str
, 20);
70 val
= strtoull(tmp
, NULL
, 0);
71 __stack_chk_guard
[idx
] = (long)(val
& ((unsigned long) -1));
73 if ((str
= strchr(str
, ',')) != NULL
)
79 __guard_setup(const char *apple
[])
85 if (__stack_chk_guard
[0] != 0)
88 for (p
= apple
; p
&& *p
; p
++) {
89 if (strstr(*p
, "stack_guard") == *p
) {
90 __guard_from_kernel(*p
);
91 bzero((void*)*p
, strlen(*p
));
92 if (__stack_chk_guard
[0] != 0) {
98 fd
= open ("/dev/urandom", 0);
100 len
= read (fd
, (char*)&__stack_chk_guard
, sizeof(__stack_chk_guard
));
102 if (len
== sizeof(__stack_chk_guard
) &&
103 *__stack_chk_guard
!= 0)
107 /* If If a random generator can't be used, the protector switches the guard
108 to the "terminator canary" */
109 ((unsigned char *)__stack_chk_guard
)[0] = 0;
110 ((unsigned char *)__stack_chk_guard
)[1] = 0;
111 ((unsigned char *)__stack_chk_guard
)[2] = '\n';
112 ((unsigned char *)__stack_chk_guard
)[3] = 255;
115 #define STACKOVERFLOW "] stack overflow"
120 char n
[16]; // bigger than will hold the digits in a pid_t
123 char message
[sizeof(n
) + sizeof(STACKOVERFLOW
)] = "[";
124 char prog
[2*MAXCOMLEN
+1] = {0};
126 proc_name(pid
, prog
, 2*MAXCOMLEN
);
127 prog
[2*MAXCOMLEN
] = 0;
131 *--np
= (pid
% 10) + '0';
134 strlcat(message
, np
, sizeof(message
));
135 strlcat(message
, STACKOVERFLOW
, sizeof(message
));
136 /* This may fail on a chroot jail... */
137 _simple_asl_log_prog(ASL_LEVEL_CRIT
, "user", message
, prog
);
139 CRSetCrashLogMessage(message
);