1*f14fb602SLionel Sambuc /* $NetBSD: stack_protector.c,v 1.8 2012/03/13 21:13:39 christos Exp $ */ 22fe8fb19SBen Gras /* $OpenBSD: stack_protector.c,v 1.10 2006/03/31 05:34:44 deraadt Exp $ */ 32fe8fb19SBen Gras 42fe8fb19SBen Gras /* 52fe8fb19SBen Gras * Copyright (c) 2002 Hiroaki Etoh, Federico G. Schwindt, and Miodrag Vallat. 62fe8fb19SBen Gras * All rights reserved. 72fe8fb19SBen Gras * 82fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without 92fe8fb19SBen Gras * modification, are permitted provided that the following conditions 102fe8fb19SBen Gras * are met: 112fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright 122fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer. 132fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright 142fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the 152fe8fb19SBen Gras * documentation and/or other materials provided with the distribution. 162fe8fb19SBen Gras * 172fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 182fe8fb19SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 192fe8fb19SBen Gras * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 202fe8fb19SBen Gras * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, 212fe8fb19SBen Gras * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 222fe8fb19SBen Gras * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 232fe8fb19SBen Gras * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 242fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 252fe8fb19SBen Gras * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 262fe8fb19SBen Gras * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 272fe8fb19SBen Gras * POSSIBILITY OF SUCH DAMAGE. 282fe8fb19SBen Gras * 292fe8fb19SBen Gras */ 302fe8fb19SBen Gras #include <sys/cdefs.h> 31*f14fb602SLionel Sambuc __RCSID("$NetBSD: stack_protector.c,v 1.8 2012/03/13 21:13:39 christos Exp $"); 322fe8fb19SBen Gras 332fe8fb19SBen Gras #ifdef _LIBC 342fe8fb19SBen Gras #include "namespace.h" 352fe8fb19SBen Gras #endif 362fe8fb19SBen Gras #include <sys/param.h> 372fe8fb19SBen Gras #include <sys/sysctl.h> 382fe8fb19SBen Gras #include <ssp/ssp.h> 392fe8fb19SBen Gras #include <signal.h> 402fe8fb19SBen Gras #include <string.h> 412fe8fb19SBen Gras #include <unistd.h> 422fe8fb19SBen Gras #ifdef _LIBC 432fe8fb19SBen Gras #include <syslog.h> 442fe8fb19SBen Gras #include "extern.h" 452fe8fb19SBen Gras #else 462fe8fb19SBen Gras #define __sysctl sysctl 472fe8fb19SBen Gras void xprintf(const char *fmt, ...); 482fe8fb19SBen Gras #include <stdlib.h> 492fe8fb19SBen Gras #endif 502fe8fb19SBen Gras 512fe8fb19SBen Gras long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 522fe8fb19SBen Gras static void __fail(const char *) __attribute__((__noreturn__)); 53*f14fb602SLionel Sambuc __dead void __stack_chk_fail_local(void); 542fe8fb19SBen Gras void __guard_setup(void); 552fe8fb19SBen Gras 562fe8fb19SBen Gras void 572fe8fb19SBen Gras __guard_setup(void) 582fe8fb19SBen Gras { 592fe8fb19SBen Gras #ifndef __minix 60*f14fb602SLionel Sambuc static const int mib[2] = { CTL_KERN, KERN_ARND }; 612fe8fb19SBen Gras size_t len; 622fe8fb19SBen Gras #endif 632fe8fb19SBen Gras 642fe8fb19SBen Gras if (__stack_chk_guard[0] != 0) 652fe8fb19SBen Gras return; 662fe8fb19SBen Gras 672fe8fb19SBen Gras #ifndef __minix 682fe8fb19SBen Gras len = sizeof(__stack_chk_guard); 69*f14fb602SLionel Sambuc if (__sysctl(mib, (u_int)__arraycount(mib), __stack_chk_guard, &len, 70*f14fb602SLionel Sambuc NULL, 0) == -1 || len != sizeof(__stack_chk_guard)) { 712fe8fb19SBen Gras #endif 722fe8fb19SBen Gras /* If sysctl was unsuccessful, use the "terminator canary". */ 732fe8fb19SBen Gras ((unsigned char *)(void *)__stack_chk_guard)[0] = 0; 742fe8fb19SBen Gras ((unsigned char *)(void *)__stack_chk_guard)[1] = 0; 752fe8fb19SBen Gras ((unsigned char *)(void *)__stack_chk_guard)[2] = '\n'; 762fe8fb19SBen Gras ((unsigned char *)(void *)__stack_chk_guard)[3] = 255; 772fe8fb19SBen Gras #ifndef __minix 782fe8fb19SBen Gras } 792fe8fb19SBen Gras #endif 802fe8fb19SBen Gras } 812fe8fb19SBen Gras 822fe8fb19SBen Gras /*ARGSUSED*/ 832fe8fb19SBen Gras static void 842fe8fb19SBen Gras __fail(const char *msg) 852fe8fb19SBen Gras { 862fe8fb19SBen Gras #ifdef _LIBC 872fe8fb19SBen Gras struct syslog_data sdata = SYSLOG_DATA_INIT; 882fe8fb19SBen Gras struct sigaction sa; 89*f14fb602SLionel Sambuc #endif 902fe8fb19SBen Gras sigset_t mask; 912fe8fb19SBen Gras 922fe8fb19SBen Gras /* Immediately block all signal handlers from running code */ 932fe8fb19SBen Gras (void)sigfillset(&mask); 942fe8fb19SBen Gras (void)sigdelset(&mask, SIGABRT); 952fe8fb19SBen Gras (void)sigprocmask(SIG_BLOCK, &mask, NULL); 962fe8fb19SBen Gras 972fe8fb19SBen Gras #ifdef _LIBC 982fe8fb19SBen Gras /* This may fail on a chroot jail... */ 992fe8fb19SBen Gras syslog_ss(LOG_CRIT, &sdata, "%s", msg); 1002fe8fb19SBen Gras #else 1012fe8fb19SBen Gras xprintf("%s: %s\n", getprogname(), msg); 1022fe8fb19SBen Gras #endif 1032fe8fb19SBen Gras 104*f14fb602SLionel Sambuc #ifdef _LIBC 1052fe8fb19SBen Gras (void)memset(&sa, 0, sizeof(sa)); 1062fe8fb19SBen Gras (void)sigemptyset(&sa.sa_mask); 1072fe8fb19SBen Gras sa.sa_flags = 0; 1082fe8fb19SBen Gras sa.sa_handler = SIG_DFL; 1092fe8fb19SBen Gras (void)sigaction(SIGABRT, &sa, NULL); 1102fe8fb19SBen Gras (void)raise(SIGABRT); 111*f14fb602SLionel Sambuc #endif 1122fe8fb19SBen Gras _exit(127); 1132fe8fb19SBen Gras } 1142fe8fb19SBen Gras 1152fe8fb19SBen Gras void 1162fe8fb19SBen Gras __stack_chk_fail(void) 1172fe8fb19SBen Gras { 1182fe8fb19SBen Gras __fail("stack overflow detected; terminated"); 1192fe8fb19SBen Gras } 1202fe8fb19SBen Gras 1212fe8fb19SBen Gras void 1222fe8fb19SBen Gras __chk_fail(void) 1232fe8fb19SBen Gras { 1242fe8fb19SBen Gras __fail("buffer overflow detected; terminated"); 1252fe8fb19SBen Gras } 1262fe8fb19SBen Gras 1272fe8fb19SBen Gras void 1282fe8fb19SBen Gras __stack_chk_fail_local(void) 1292fe8fb19SBen Gras { 1302fe8fb19SBen Gras __stack_chk_fail(); 1312fe8fb19SBen Gras } 132