1*ea6088e7Sguenther /* $OpenBSD: stack_protector.c,v 1.24 2017/11/29 05:13:57 guenther Exp $ */
2fb929c0dSmiod
3fb929c0dSmiod /*
4fb929c0dSmiod * Copyright (c) 2002 Hiroaki Etoh, Federico G. Schwindt, and Miodrag Vallat.
5fb929c0dSmiod * All rights reserved.
6fb929c0dSmiod *
7fb929c0dSmiod * Redistribution and use in source and binary forms, with or without
8fb929c0dSmiod * modification, are permitted provided that the following conditions
9fb929c0dSmiod * are met:
10fb929c0dSmiod * 1. Redistributions of source code must retain the above copyright
11fb929c0dSmiod * notice, this list of conditions and the following disclaimer.
12fb929c0dSmiod * 2. Redistributions in binary form must reproduce the above copyright
13fb929c0dSmiod * notice, this list of conditions and the following disclaimer in the
14fb929c0dSmiod * documentation and/or other materials provided with the distribution.
15fb929c0dSmiod *
16fb929c0dSmiod * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17fb929c0dSmiod * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18fb929c0dSmiod * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19fb929c0dSmiod * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT,
20fb929c0dSmiod * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21fb929c0dSmiod * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22fb929c0dSmiod * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23fb929c0dSmiod * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24fb929c0dSmiod * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25fb929c0dSmiod * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26fb929c0dSmiod * POSSIBILITY OF SUCH DAMAGE.
27fb929c0dSmiod *
28fb929c0dSmiod */
29fb929c0dSmiod
3026fda0efSdavid #include <signal.h>
31e4224292Sguenther #include <stdlib.h>
3226fda0efSdavid #include <string.h>
33fb929c0dSmiod #include <syslog.h>
3426fda0efSdavid #include <unistd.h>
35fb929c0dSmiod
366897476fSguenther /*
376897476fSguenther * Note: test below is for PIC not __PIC__. This code must only be included
386897476fSguenther * in the shared library and not in libc.a, but __PIC__ is set for libc.a
396897476fSguenther * objects where PIE is supported
406897476fSguenther *
416897476fSguenther * XXX would this work? #if defined(__PIC__) && !defined(__PIE__)
426897476fSguenther * XXX any archs which are always PIC (like mips64) but don't have PIE?
436897476fSguenther */
446897476fSguenther #ifdef PIC
456897476fSguenther #include <../csu/os-note-elf.h>
466897476fSguenther
476897476fSguenther long __guard_local __dso_hidden __attribute__((section(".openbsd.randomdata")));
486897476fSguenther #endif /* PIC */
496897476fSguenther
50fb929c0dSmiod void
__stack_smash_handler(const char func[],int damaged)51e6f98e3aSguenther __stack_smash_handler(const char func[], int damaged)
52fb929c0dSmiod {
53fb929c0dSmiod struct sigaction sa;
54dcbc515cSderaadt sigset_t mask;
550e278963Sderaadt char buf[1024];
56fb929c0dSmiod
57dcbc515cSderaadt /* Immediately block all signal handlers from running code */
58dcbc515cSderaadt sigfillset(&mask);
59dcbc515cSderaadt sigdelset(&mask, SIGABRT);
6060d49506Sguenther sigprocmask(SIG_SETMASK, &mask, NULL);
61dcbc515cSderaadt
620e278963Sderaadt /* <10> is LOG_CRIT */
632282c45eScanacar strlcpy(buf, "<10>", sizeof buf);
640e278963Sderaadt
652282c45eScanacar /* Make sure progname does not fill the whole buffer */
662282c45eScanacar strlcat(buf, __progname, sizeof(buf) / 2 );
672282c45eScanacar
680e278963Sderaadt strlcat(buf, ": stack overflow in function ", sizeof buf);
690e278963Sderaadt strlcat(buf, func, sizeof buf);
700e278963Sderaadt
7146afc4a4Sbluhm sendsyslog(buf, strlen(buf), LOG_CONS);
72fb929c0dSmiod
7360d49506Sguenther memset(&sa, 0, sizeof(sa));
74fb929c0dSmiod sigemptyset(&sa.sa_mask);
75fb929c0dSmiod sa.sa_flags = 0;
76fb929c0dSmiod sa.sa_handler = SIG_DFL;
77fb929c0dSmiod sigaction(SIGABRT, &sa, NULL);
78fb929c0dSmiod
7960d49506Sguenther thrkill(0, SIGABRT, NULL);
80fb929c0dSmiod
81fb929c0dSmiod _exit(127);
82fb929c0dSmiod }
83*ea6088e7Sguenther DEF_BUILTIN(__stack_smash_handler);
84