| 1 |  |  | /*	$OpenBSD: stack_protector.c,v 1.23 2016/03/21 22:41:28 bluhm Exp $	*/ | 
    
    | 2 |  |  |  | 
    
    | 3 |  |  | /* | 
    
    | 4 |  |  |  * Copyright (c) 2002 Hiroaki Etoh, Federico G. Schwindt, and Miodrag Vallat. | 
    
    | 5 |  |  |  * All rights reserved. | 
    
    | 6 |  |  |  * | 
    
    | 7 |  |  |  * Redistribution and use in source and binary forms, with or without | 
    
    | 8 |  |  |  * modification, are permitted provided that the following conditions | 
    
    | 9 |  |  |  * are met: | 
    
    | 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. | 
    
    | 15 |  |  |  * | 
    
    | 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. | 
    
    | 27 |  |  |  * | 
    
    | 28 |  |  |  */ | 
    
    | 29 |  |  |  | 
    
    | 30 |  |  | #include <signal.h> | 
    
    | 31 |  |  | #include <stdlib.h> | 
    
    | 32 |  |  | #include <string.h> | 
    
    | 33 |  |  | #include <syslog.h> | 
    
    | 34 |  |  | #include <unistd.h> | 
    
    | 35 |  |  |  | 
    
    | 36 |  |  | /* | 
    
    | 37 |  |  |  * Note: test below is for PIC not __PIC__.  This code must only be included | 
    
    | 38 |  |  |  * in the shared library and not in libc.a, but __PIC__ is set for libc.a | 
    
    | 39 |  |  |  * objects where PIE is supported | 
    
    | 40 |  |  |  * | 
    
    | 41 |  |  |  * XXX would this work? #if defined(__PIC__) && !defined(__PIE__) | 
    
    | 42 |  |  |  * XXX any archs which are always PIC (like mips64) but don't have PIE? | 
    
    | 43 |  |  |  */ | 
    
    | 44 |  |  | #ifdef PIC | 
    
    | 45 |  |  | #include <../csu/os-note-elf.h> | 
    
    | 46 |  |  |  | 
    
    | 47 |  |  | long __guard_local __dso_hidden __attribute__((section(".openbsd.randomdata"))); | 
    
    | 48 |  |  | #endif /* PIC */ | 
    
    | 49 |  |  |  | 
    
    | 50 |  |  | void | 
    
    | 51 |  |  | __stack_smash_handler(const char func[], int damaged) | 
    
    | 52 |  |  | { | 
    
    | 53 |  |  | 	struct sigaction sa; | 
    
    | 54 |  |  | 	sigset_t mask; | 
    
    | 55 |  |  | 	char buf[1024]; | 
    
    | 56 |  |  |  | 
    
    | 57 |  |  | 	/* Immediately block all signal handlers from running code */ | 
    
    | 58 |  |  | 	sigfillset(&mask); | 
    
    | 59 |  |  | 	sigdelset(&mask, SIGABRT); | 
    
    | 60 |  |  | 	sigprocmask(SIG_SETMASK, &mask, NULL); | 
    
    | 61 |  |  |  | 
    
    | 62 |  |  | 	/* <10> is LOG_CRIT */ | 
    
    | 63 |  |  | 	strlcpy(buf, "<10>", sizeof buf); | 
    
    | 64 |  |  |  | 
    
    | 65 |  |  | 	/* Make sure progname does not fill the whole buffer */ | 
    
    | 66 |  |  | 	strlcat(buf, __progname, sizeof(buf) / 2 ); | 
    
    | 67 |  |  |  | 
    
    | 68 |  |  | 	strlcat(buf, ": stack overflow in function ", sizeof buf); | 
    
    | 69 |  |  | 	strlcat(buf, func, sizeof buf); | 
    
    | 70 |  |  |  | 
    
    | 71 |  |  | 	sendsyslog(buf, strlen(buf), LOG_CONS); | 
    
    | 72 |  |  |  | 
    
    | 73 |  |  | 	memset(&sa, 0, sizeof(sa)); | 
    
    | 74 |  |  | 	sigemptyset(&sa.sa_mask); | 
    
    | 75 |  |  | 	sa.sa_flags = 0; | 
    
    | 76 |  |  | 	sa.sa_handler = SIG_DFL; | 
    
    | 77 |  |  | 	sigaction(SIGABRT, &sa, NULL); | 
    
    | 78 |  |  |  | 
    
    | 79 |  |  | 	thrkill(0, SIGABRT, NULL); | 
    
    | 80 |  |  |  | 
    
    | 81 |  |  | 	_exit(127); | 
    
    | 82 |  |  | } | 
    
    | 83 |  |  | DEF_STRONG(__stack_smash_handler); |