1fe0d386cSPedro F. Giffuni /* $NetBSD: stack_protector.c,v 1.4 2006/11/22 17:23:25 christos Exp $	*/
2fe0d386cSPedro F. Giffuni /* $OpenBSD: stack_protector.c,v 1.10 2006/03/31 05:34:44 deraadt Exp $	*/
3fe0d386cSPedro F. Giffuni /*
4fe0d386cSPedro F. Giffuni  * Copyright (c) 2002 Hiroaki Etoh, Federico G. Schwindt, and Miodrag Vallat.
5fe0d386cSPedro F. Giffuni  * All rights reserved.
6fe0d386cSPedro F. Giffuni  *
7fe0d386cSPedro F. Giffuni  * Redistribution and use in source and binary forms, with or without
8fe0d386cSPedro F. Giffuni  * modification, are permitted provided that the following conditions
9fe0d386cSPedro F. Giffuni  * are met:
10fe0d386cSPedro F. Giffuni  * 1. Redistributions of source code must retain the above copyright
11fe0d386cSPedro F. Giffuni  *    notice, this list of conditions and the following disclaimer.
12fe0d386cSPedro F. Giffuni  * 2. Redistributions in binary form must reproduce the above copyright
13fe0d386cSPedro F. Giffuni  *    notice, this list of conditions and the following disclaimer in the
14fe0d386cSPedro F. Giffuni  *    documentation and/or other materials provided with the distribution.
15fe0d386cSPedro F. Giffuni  *
16fe0d386cSPedro F. Giffuni  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17fe0d386cSPedro F. Giffuni  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18fe0d386cSPedro F. Giffuni  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19fe0d386cSPedro F. Giffuni  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT,
20fe0d386cSPedro F. Giffuni  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21fe0d386cSPedro F. Giffuni  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22fe0d386cSPedro F. Giffuni  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23fe0d386cSPedro F. Giffuni  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24fe0d386cSPedro F. Giffuni  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25fe0d386cSPedro F. Giffuni  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26fe0d386cSPedro F. Giffuni  * POSSIBILITY OF SUCH DAMAGE.
27fe0d386cSPedro F. Giffuni  *
28fe0d386cSPedro F. Giffuni  */
29fe0d386cSPedro F. Giffuni 
30fe0d386cSPedro F. Giffuni #include <sys/cdefs.h>
31fe0d386cSPedro F. Giffuni __FBSDID("$FreeBSD$");
32fe0d386cSPedro F. Giffuni 
33fe0d386cSPedro F. Giffuni #include <sys/param.h>
34fe0d386cSPedro F. Giffuni #include <sys/sysctl.h>
35fe0d386cSPedro F. Giffuni #include <errno.h>
36fe0d386cSPedro F. Giffuni #include <link.h>
37fe0d386cSPedro F. Giffuni #include <signal.h>
38fe0d386cSPedro F. Giffuni #include <string.h>
39fe0d386cSPedro F. Giffuni #include <syslog.h>
40fe0d386cSPedro F. Giffuni #include <unistd.h>
41fe0d386cSPedro F. Giffuni #include "libc_private.h"
42fe0d386cSPedro F. Giffuni 
43*b6c023d0SGordon Tetlow /*
44*b6c023d0SGordon Tetlow  * We give __guard_setup a defined priority early on so that statically linked
45*b6c023d0SGordon Tetlow  * applications have a defined priority at which __stack_chk_guard will be
46*b6c023d0SGordon Tetlow  * getting initialized.  This will not matter to most applications, because
47*b6c023d0SGordon Tetlow  * they're either not usually statically linked or they simply don't do things
48*b6c023d0SGordon Tetlow  * in constructors that would be adversely affected by their positioning with
49*b6c023d0SGordon Tetlow  * respect to this initialization.
50*b6c023d0SGordon Tetlow  *
51*b6c023d0SGordon Tetlow  * This conditional should be removed when GCC 4.2 is removed.
52*b6c023d0SGordon Tetlow  */
53*b6c023d0SGordon Tetlow #if __has_attribute(__constructor__) || __GNUC_PREREQ__(4, 3)
54*b6c023d0SGordon Tetlow #define	_GUARD_SETUP_CTOR_ATTR	 \
55*b6c023d0SGordon Tetlow     __attribute__((__constructor__ (200), __used__));
56*b6c023d0SGordon Tetlow #else
57*b6c023d0SGordon Tetlow #define	_GUARD_SETUP_CTOR_ATTR	\
58*b6c023d0SGordon Tetlow     __attribute__((__constructor__, __used__));
59*b6c023d0SGordon Tetlow #endif
60*b6c023d0SGordon Tetlow 
61fe0d386cSPedro F. Giffuni extern int __sysctl(const int *name, u_int namelen, void *oldp,
62fe0d386cSPedro F. Giffuni     size_t *oldlenp, void *newp, size_t newlen);
63fe0d386cSPedro F. Giffuni 
64fe0d386cSPedro F. Giffuni long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
65*b6c023d0SGordon Tetlow static void __guard_setup(void) _GUARD_SETUP_CTOR_ATTR;
66fe0d386cSPedro F. Giffuni static void __fail(const char *);
67fe0d386cSPedro F. Giffuni void __stack_chk_fail(void);
68fe0d386cSPedro F. Giffuni void __chk_fail(void);
69fe0d386cSPedro F. Giffuni 
70fe0d386cSPedro F. Giffuni /*LINTED used*/
71fe0d386cSPedro F. Giffuni static void
__guard_setup(void)72fe0d386cSPedro F. Giffuni __guard_setup(void)
73fe0d386cSPedro F. Giffuni {
74fe0d386cSPedro F. Giffuni 	static const int mib[2] = { CTL_KERN, KERN_ARND };
75989b861fSKonstantin Belousov 	volatile long tmp_stack_chk_guard[nitems(__stack_chk_guard)];
76fe0d386cSPedro F. Giffuni 	size_t len;
77989b861fSKonstantin Belousov 	int error, idx;
78fe0d386cSPedro F. Giffuni 
79fe0d386cSPedro F. Giffuni 	if (__stack_chk_guard[0] != 0)
80fe0d386cSPedro F. Giffuni 		return;
81989b861fSKonstantin Belousov 	/*
82989b861fSKonstantin Belousov 	 * Avoid using functions which might have stack protection
83989b861fSKonstantin Belousov 	 * enabled, to update the __stack_chk_guard.  First fetch the
84989b861fSKonstantin Belousov 	 * data into a temporal array, then do manual volatile copy to
85989b861fSKonstantin Belousov 	 * not allow optimizer to call memcpy() behind us.
86989b861fSKonstantin Belousov 	 */
87989b861fSKonstantin Belousov 	error = _elf_aux_info(AT_CANARY, (void *)tmp_stack_chk_guard,
88989b861fSKonstantin Belousov 	    sizeof(tmp_stack_chk_guard));
89989b861fSKonstantin Belousov 	if (error == 0 && tmp_stack_chk_guard[0] != 0) {
90989b861fSKonstantin Belousov 		for (idx = 0; idx < nitems(__stack_chk_guard); idx++) {
91989b861fSKonstantin Belousov 			__stack_chk_guard[idx] = tmp_stack_chk_guard[idx];
92989b861fSKonstantin Belousov 			tmp_stack_chk_guard[idx] = 0;
93989b861fSKonstantin Belousov 		}
94fe0d386cSPedro F. Giffuni 		return;
95989b861fSKonstantin Belousov 	}
96fe0d386cSPedro F. Giffuni 
97fe0d386cSPedro F. Giffuni 	len = sizeof(__stack_chk_guard);
98fe0d386cSPedro F. Giffuni 	if (__sysctl(mib, nitems(mib), __stack_chk_guard, &len, NULL, 0) ==
99fe0d386cSPedro F. Giffuni 	    -1 || len != sizeof(__stack_chk_guard)) {
100fe0d386cSPedro F. Giffuni 		/* If sysctl was unsuccessful, use the "terminator canary". */
101fe0d386cSPedro F. Giffuni 		((unsigned char *)(void *)__stack_chk_guard)[0] = 0;
102fe0d386cSPedro F. Giffuni 		((unsigned char *)(void *)__stack_chk_guard)[1] = 0;
103fe0d386cSPedro F. Giffuni 		((unsigned char *)(void *)__stack_chk_guard)[2] = '\n';
104fe0d386cSPedro F. Giffuni 		((unsigned char *)(void *)__stack_chk_guard)[3] = 255;
105fe0d386cSPedro F. Giffuni 	}
106fe0d386cSPedro F. Giffuni }
107fe0d386cSPedro F. Giffuni 
108fe0d386cSPedro F. Giffuni /*ARGSUSED*/
109fe0d386cSPedro F. Giffuni static void
__fail(const char * msg)110fe0d386cSPedro F. Giffuni __fail(const char *msg)
111fe0d386cSPedro F. Giffuni {
112fe0d386cSPedro F. Giffuni 	struct sigaction sa;
113fe0d386cSPedro F. Giffuni 	sigset_t mask;
114fe0d386cSPedro F. Giffuni 
115fe0d386cSPedro F. Giffuni 	/* Immediately block all signal handlers from running code */
116fe0d386cSPedro F. Giffuni 	(void)sigfillset(&mask);
117fe0d386cSPedro F. Giffuni 	(void)sigdelset(&mask, SIGABRT);
118fe0d386cSPedro F. Giffuni 	(void)sigprocmask(SIG_BLOCK, &mask, NULL);
119fe0d386cSPedro F. Giffuni 
120fe0d386cSPedro F. Giffuni 	/* This may fail on a chroot jail... */
121fe0d386cSPedro F. Giffuni 	syslog(LOG_CRIT, "%s", msg);
122fe0d386cSPedro F. Giffuni 
123fe0d386cSPedro F. Giffuni 	(void)memset(&sa, 0, sizeof(sa));
124fe0d386cSPedro F. Giffuni 	(void)sigemptyset(&sa.sa_mask);
125fe0d386cSPedro F. Giffuni 	sa.sa_flags = 0;
126fe0d386cSPedro F. Giffuni 	sa.sa_handler = SIG_DFL;
127fe0d386cSPedro F. Giffuni 	(void)sigaction(SIGABRT, &sa, NULL);
128fe0d386cSPedro F. Giffuni 	(void)kill(getpid(), SIGABRT);
129fe0d386cSPedro F. Giffuni 	_exit(127);
130fe0d386cSPedro F. Giffuni }
131fe0d386cSPedro F. Giffuni 
132fe0d386cSPedro F. Giffuni void
__stack_chk_fail(void)133fe0d386cSPedro F. Giffuni __stack_chk_fail(void)
134fe0d386cSPedro F. Giffuni {
135fe0d386cSPedro F. Giffuni 	__fail("stack overflow detected; terminated");
136fe0d386cSPedro F. Giffuni }
137fe0d386cSPedro F. Giffuni 
138fe0d386cSPedro F. Giffuni void
__chk_fail(void)139fe0d386cSPedro F. Giffuni __chk_fail(void)
140fe0d386cSPedro F. Giffuni {
141fe0d386cSPedro F. Giffuni 	__fail("buffer overflow detected; terminated");
142fe0d386cSPedro F. Giffuni }
143fe0d386cSPedro F. Giffuni 
144fe0d386cSPedro F. Giffuni #ifndef PIC
145fe0d386cSPedro F. Giffuni __weak_reference(__stack_chk_fail, __stack_chk_fail_local);
146fe0d386cSPedro F. Giffuni #endif
147