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 
43d0fa84f4SKyle Evans /*
44d0fa84f4SKyle Evans  * We give __guard_setup a defined priority early on so that statically linked
45d0fa84f4SKyle Evans  * applications have a defined priority at which __stack_chk_guard will be
46d0fa84f4SKyle Evans  * getting initialized.  This will not matter to most applications, because
47d0fa84f4SKyle Evans  * they're either not usually statically linked or they simply don't do things
48d0fa84f4SKyle Evans  * in constructors that would be adversely affected by their positioning with
49d0fa84f4SKyle Evans  * respect to this initialization.
505ba134a4SKyle Evans  *
515ba134a4SKyle Evans  * This conditional should be removed when GCC 4.2 is removed.
52d0fa84f4SKyle Evans  */
534e0706cbSKyle Evans #if __has_attribute(__constructor__) || __GNUC_PREREQ__(4, 3)
54d0fa84f4SKyle Evans #define	_GUARD_SETUP_CTOR_ATTR	 \
55d0fa84f4SKyle Evans     __attribute__((__constructor__ (200), __used__));
565ba134a4SKyle Evans #else
575ba134a4SKyle Evans #define	_GUARD_SETUP_CTOR_ATTR	\
585ba134a4SKyle Evans     __attribute__((__constructor__, __used__));
59d0fa84f4SKyle Evans #endif
60d0fa84f4SKyle Evans 
61a34e99eeSKyle Evans extern long __stack_chk_guard[8];
62fe0d386cSPedro F. Giffuni extern int __sysctl(const int *name, u_int namelen, void *oldp,
63fe0d386cSPedro F. Giffuni     size_t *oldlenp, void *newp, size_t newlen);
64fe0d386cSPedro F. Giffuni 
65fe0d386cSPedro F. Giffuni long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
66d0fa84f4SKyle Evans static void __guard_setup(void) _GUARD_SETUP_CTOR_ATTR;
67*a663c839SKyle Evans static void __fail(const char *) __dead2;
68*a663c839SKyle Evans void __stack_chk_fail(void) __dead2;
69*a663c839SKyle Evans void __chk_fail(void) __dead2;
70fe0d386cSPedro F. Giffuni 
71fe0d386cSPedro F. Giffuni /*LINTED used*/
72fe0d386cSPedro F. Giffuni static void
__guard_setup(void)73fe0d386cSPedro F. Giffuni __guard_setup(void)
74fe0d386cSPedro F. Giffuni {
75fe0d386cSPedro F. Giffuni 	static const int mib[2] = { CTL_KERN, KERN_ARND };
76989b861fSKonstantin Belousov 	volatile long tmp_stack_chk_guard[nitems(__stack_chk_guard)];
77a34e99eeSKyle Evans 	size_t idx, len;
78a34e99eeSKyle Evans 	int error;
79fe0d386cSPedro F. Giffuni 
80fe0d386cSPedro F. Giffuni 	if (__stack_chk_guard[0] != 0)
81fe0d386cSPedro F. Giffuni 		return;
82989b861fSKonstantin Belousov 	/*
83989b861fSKonstantin Belousov 	 * Avoid using functions which might have stack protection
84989b861fSKonstantin Belousov 	 * enabled, to update the __stack_chk_guard.  First fetch the
85989b861fSKonstantin Belousov 	 * data into a temporal array, then do manual volatile copy to
86989b861fSKonstantin Belousov 	 * not allow optimizer to call memcpy() behind us.
87989b861fSKonstantin Belousov 	 */
88a34e99eeSKyle Evans 	error = _elf_aux_info(AT_CANARY,
89a34e99eeSKyle Evans 	    __DEQUALIFY(void *, tmp_stack_chk_guard),
90989b861fSKonstantin Belousov 	    sizeof(tmp_stack_chk_guard));
91989b861fSKonstantin Belousov 	if (error == 0 && tmp_stack_chk_guard[0] != 0) {
92989b861fSKonstantin Belousov 		for (idx = 0; idx < nitems(__stack_chk_guard); idx++) {
93989b861fSKonstantin Belousov 			__stack_chk_guard[idx] = tmp_stack_chk_guard[idx];
94989b861fSKonstantin Belousov 			tmp_stack_chk_guard[idx] = 0;
95989b861fSKonstantin Belousov 		}
96fe0d386cSPedro F. Giffuni 		return;
97989b861fSKonstantin Belousov 	}
98fe0d386cSPedro F. Giffuni 
99fe0d386cSPedro F. Giffuni 	len = sizeof(__stack_chk_guard);
100fe0d386cSPedro F. Giffuni 	if (__sysctl(mib, nitems(mib), __stack_chk_guard, &len, NULL, 0) ==
101fe0d386cSPedro F. Giffuni 	    -1 || len != sizeof(__stack_chk_guard)) {
102fe0d386cSPedro F. Giffuni 		/* If sysctl was unsuccessful, use the "terminator canary". */
103fe0d386cSPedro F. Giffuni 		((unsigned char *)(void *)__stack_chk_guard)[0] = 0;
104fe0d386cSPedro F. Giffuni 		((unsigned char *)(void *)__stack_chk_guard)[1] = 0;
105fe0d386cSPedro F. Giffuni 		((unsigned char *)(void *)__stack_chk_guard)[2] = '\n';
106fe0d386cSPedro F. Giffuni 		((unsigned char *)(void *)__stack_chk_guard)[3] = 255;
107fe0d386cSPedro F. Giffuni 	}
108fe0d386cSPedro F. Giffuni }
109fe0d386cSPedro F. Giffuni 
110fe0d386cSPedro F. Giffuni /*ARGSUSED*/
111fe0d386cSPedro F. Giffuni static void
__fail(const char * msg)112fe0d386cSPedro F. Giffuni __fail(const char *msg)
113fe0d386cSPedro F. Giffuni {
114fe0d386cSPedro F. Giffuni 	struct sigaction sa;
115fe0d386cSPedro F. Giffuni 	sigset_t mask;
116fe0d386cSPedro F. Giffuni 
117fe0d386cSPedro F. Giffuni 	/* Immediately block all signal handlers from running code */
118fe0d386cSPedro F. Giffuni 	(void)sigfillset(&mask);
119fe0d386cSPedro F. Giffuni 	(void)sigdelset(&mask, SIGABRT);
120fe0d386cSPedro F. Giffuni 	(void)sigprocmask(SIG_BLOCK, &mask, NULL);
121fe0d386cSPedro F. Giffuni 
122fe0d386cSPedro F. Giffuni 	/* This may fail on a chroot jail... */
123fe0d386cSPedro F. Giffuni 	syslog(LOG_CRIT, "%s", msg);
124fe0d386cSPedro F. Giffuni 
125fe0d386cSPedro F. Giffuni 	(void)memset(&sa, 0, sizeof(sa));
126fe0d386cSPedro F. Giffuni 	(void)sigemptyset(&sa.sa_mask);
127fe0d386cSPedro F. Giffuni 	sa.sa_flags = 0;
128fe0d386cSPedro F. Giffuni 	sa.sa_handler = SIG_DFL;
129fe0d386cSPedro F. Giffuni 	(void)sigaction(SIGABRT, &sa, NULL);
130fe0d386cSPedro F. Giffuni 	(void)kill(getpid(), SIGABRT);
131fe0d386cSPedro F. Giffuni 	_exit(127);
132fe0d386cSPedro F. Giffuni }
133fe0d386cSPedro F. Giffuni 
134fe0d386cSPedro F. Giffuni void
__stack_chk_fail(void)135fe0d386cSPedro F. Giffuni __stack_chk_fail(void)
136fe0d386cSPedro F. Giffuni {
137fe0d386cSPedro F. Giffuni 	__fail("stack overflow detected; terminated");
138fe0d386cSPedro F. Giffuni }
139fe0d386cSPedro F. Giffuni 
140fe0d386cSPedro F. Giffuni void
__chk_fail(void)141fe0d386cSPedro F. Giffuni __chk_fail(void)
142fe0d386cSPedro F. Giffuni {
143fe0d386cSPedro F. Giffuni 	__fail("buffer overflow detected; terminated");
144fe0d386cSPedro F. Giffuni }
145fe0d386cSPedro F. Giffuni 
146fe0d386cSPedro F. Giffuni #ifndef PIC
147fe0d386cSPedro F. Giffuni __weak_reference(__stack_chk_fail, __stack_chk_fail_local);
148fe0d386cSPedro F. Giffuni #endif
149