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/param.h>
31fe0d386cSPedro F. Giffuni #include <sys/sysctl.h>
32fe0d386cSPedro F. Giffuni #include <errno.h>
33fe0d386cSPedro F. Giffuni #include <link.h>
34fe0d386cSPedro F. Giffuni #include <signal.h>
35fe0d386cSPedro F. Giffuni #include <string.h>
36fe0d386cSPedro F. Giffuni #include <syslog.h>
37fe0d386cSPedro F. Giffuni #include <unistd.h>
38fe0d386cSPedro F. Giffuni #include "libc_private.h"
39fe0d386cSPedro F. Giffuni 
40d0fa84f4SKyle Evans /*
41d0fa84f4SKyle Evans  * We give __guard_setup a defined priority early on so that statically linked
42d0fa84f4SKyle Evans  * applications have a defined priority at which __stack_chk_guard will be
43d0fa84f4SKyle Evans  * getting initialized.  This will not matter to most applications, because
44d0fa84f4SKyle Evans  * they're either not usually statically linked or they simply don't do things
45d0fa84f4SKyle Evans  * in constructors that would be adversely affected by their positioning with
46d0fa84f4SKyle Evans  * respect to this initialization.
47d0fa84f4SKyle Evans  */
48*f44df795SKyle Evans static void __guard_setup(void)
49d0fa84f4SKyle Evans     __attribute__((__constructor__ (200), __used__));
50d0fa84f4SKyle Evans 
51a34e99eeSKyle Evans extern long __stack_chk_guard[8];
52fe0d386cSPedro F. Giffuni extern int __sysctl(const int *name, u_int namelen, void *oldp,
53fe0d386cSPedro F. Giffuni     size_t *oldlenp, void *newp, size_t newlen);
54fe0d386cSPedro F. Giffuni 
55fe0d386cSPedro F. Giffuni long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
565487294dSKyle Evans static void __fail(const char *) __dead2;
575487294dSKyle Evans void __stack_chk_fail(void) __dead2;
585487294dSKyle Evans void __chk_fail(void) __dead2;
59fe0d386cSPedro F. Giffuni 
60fe0d386cSPedro F. Giffuni /*LINTED used*/
61fe0d386cSPedro F. Giffuni static void
__guard_setup(void)62fe0d386cSPedro F. Giffuni __guard_setup(void)
63fe0d386cSPedro F. Giffuni {
64fe0d386cSPedro F. Giffuni 	static const int mib[2] = { CTL_KERN, KERN_ARND };
65989b861fSKonstantin Belousov 	volatile long tmp_stack_chk_guard[nitems(__stack_chk_guard)];
66a34e99eeSKyle Evans 	size_t idx, len;
67a34e99eeSKyle Evans 	int error;
68fe0d386cSPedro F. Giffuni 
69fe0d386cSPedro F. Giffuni 	if (__stack_chk_guard[0] != 0)
70fe0d386cSPedro F. Giffuni 		return;
71989b861fSKonstantin Belousov 	/*
72989b861fSKonstantin Belousov 	 * Avoid using functions which might have stack protection
73989b861fSKonstantin Belousov 	 * enabled, to update the __stack_chk_guard.  First fetch the
74989b861fSKonstantin Belousov 	 * data into a temporal array, then do manual volatile copy to
75989b861fSKonstantin Belousov 	 * not allow optimizer to call memcpy() behind us.
76989b861fSKonstantin Belousov 	 */
77a34e99eeSKyle Evans 	error = _elf_aux_info(AT_CANARY,
78a34e99eeSKyle Evans 	    __DEQUALIFY(void *, tmp_stack_chk_guard),
79989b861fSKonstantin Belousov 	    sizeof(tmp_stack_chk_guard));
80989b861fSKonstantin Belousov 	if (error == 0 && tmp_stack_chk_guard[0] != 0) {
81989b861fSKonstantin Belousov 		for (idx = 0; idx < nitems(__stack_chk_guard); idx++) {
82989b861fSKonstantin Belousov 			__stack_chk_guard[idx] = tmp_stack_chk_guard[idx];
83989b861fSKonstantin Belousov 			tmp_stack_chk_guard[idx] = 0;
84989b861fSKonstantin Belousov 		}
85fe0d386cSPedro F. Giffuni 		return;
86989b861fSKonstantin Belousov 	}
87fe0d386cSPedro F. Giffuni 
88fe0d386cSPedro F. Giffuni 	len = sizeof(__stack_chk_guard);
89fe0d386cSPedro F. Giffuni 	if (__sysctl(mib, nitems(mib), __stack_chk_guard, &len, NULL, 0) ==
90fe0d386cSPedro F. Giffuni 	    -1 || len != sizeof(__stack_chk_guard)) {
91fe0d386cSPedro F. Giffuni 		/* If sysctl was unsuccessful, use the "terminator canary". */
92fe0d386cSPedro F. Giffuni 		((unsigned char *)(void *)__stack_chk_guard)[0] = 0;
93fe0d386cSPedro F. Giffuni 		((unsigned char *)(void *)__stack_chk_guard)[1] = 0;
94fe0d386cSPedro F. Giffuni 		((unsigned char *)(void *)__stack_chk_guard)[2] = '\n';
95fe0d386cSPedro F. Giffuni 		((unsigned char *)(void *)__stack_chk_guard)[3] = 255;
96fe0d386cSPedro F. Giffuni 	}
97fe0d386cSPedro F. Giffuni }
98fe0d386cSPedro F. Giffuni 
99fe0d386cSPedro F. Giffuni /*ARGSUSED*/
100fe0d386cSPedro F. Giffuni static void
__fail(const char * msg)101fe0d386cSPedro F. Giffuni __fail(const char *msg)
102fe0d386cSPedro F. Giffuni {
103fe0d386cSPedro F. Giffuni 	struct sigaction sa;
104fe0d386cSPedro F. Giffuni 	sigset_t mask;
105fe0d386cSPedro F. Giffuni 
106fe0d386cSPedro F. Giffuni 	/* Immediately block all signal handlers from running code */
107fe0d386cSPedro F. Giffuni 	(void)sigfillset(&mask);
108fe0d386cSPedro F. Giffuni 	(void)sigdelset(&mask, SIGABRT);
109fe0d386cSPedro F. Giffuni 	(void)sigprocmask(SIG_BLOCK, &mask, NULL);
110fe0d386cSPedro F. Giffuni 
111fe0d386cSPedro F. Giffuni 	/* This may fail on a chroot jail... */
112fe0d386cSPedro F. Giffuni 	syslog(LOG_CRIT, "%s", msg);
113fe0d386cSPedro F. Giffuni 
114fe0d386cSPedro F. Giffuni 	(void)memset(&sa, 0, sizeof(sa));
115fe0d386cSPedro F. Giffuni 	(void)sigemptyset(&sa.sa_mask);
116fe0d386cSPedro F. Giffuni 	sa.sa_flags = 0;
117fe0d386cSPedro F. Giffuni 	sa.sa_handler = SIG_DFL;
118fe0d386cSPedro F. Giffuni 	(void)sigaction(SIGABRT, &sa, NULL);
119fe0d386cSPedro F. Giffuni 	(void)kill(getpid(), SIGABRT);
120fe0d386cSPedro F. Giffuni 	_exit(127);
121fe0d386cSPedro F. Giffuni }
122fe0d386cSPedro F. Giffuni 
123fe0d386cSPedro F. Giffuni void
__stack_chk_fail(void)124fe0d386cSPedro F. Giffuni __stack_chk_fail(void)
125fe0d386cSPedro F. Giffuni {
126fe0d386cSPedro F. Giffuni 	__fail("stack overflow detected; terminated");
127fe0d386cSPedro F. Giffuni }
128fe0d386cSPedro F. Giffuni 
129fe0d386cSPedro F. Giffuni void
__chk_fail(void)130fe0d386cSPedro F. Giffuni __chk_fail(void)
131fe0d386cSPedro F. Giffuni {
132fe0d386cSPedro F. Giffuni 	__fail("buffer overflow detected; terminated");
133fe0d386cSPedro F. Giffuni }
134fe0d386cSPedro F. Giffuni 
135fe0d386cSPedro F. Giffuni #ifndef PIC
136fe0d386cSPedro F. Giffuni __weak_reference(__stack_chk_fail, __stack_chk_fail_local);
137fe0d386cSPedro F. Giffuni #endif
138