xref: /dflybsd-src/sys/libkern/stack_protector.c (revision 479ab7f0492f2a51b48e8537e4f1dc686fc6014b)
104ff94b9SMatthew Dillon /*
204ff94b9SMatthew Dillon  * Copyright (c) 2002 Hiroaki Etoh.
304ff94b9SMatthew Dillon  * All rights reserved.
404ff94b9SMatthew Dillon  *
504ff94b9SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
604ff94b9SMatthew Dillon  * modification, are permitted provided that the following conditions
704ff94b9SMatthew Dillon  * are met:
804ff94b9SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
904ff94b9SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
1004ff94b9SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
1104ff94b9SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
1204ff94b9SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
1304ff94b9SMatthew Dillon  *
1404ff94b9SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
1504ff94b9SMatthew Dillon  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1604ff94b9SMatthew Dillon  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1704ff94b9SMatthew Dillon  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT,
1804ff94b9SMatthew Dillon  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1904ff94b9SMatthew Dillon  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2004ff94b9SMatthew Dillon  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2104ff94b9SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2204ff94b9SMatthew Dillon  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
2304ff94b9SMatthew Dillon  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2404ff94b9SMatthew Dillon  * POSSIBILITY OF SUCH DAMAGE.
2504ff94b9SMatthew Dillon  */
2604ff94b9SMatthew Dillon 
2704ff94b9SMatthew Dillon #include <sys/param.h>
286f2a5fe6SJoerg Sonnenberger #include <sys/endian.h>
29a8d03eb9SSascha Wildner #ifndef _KERNEL
30a8d03eb9SSascha Wildner extern void panic(const char *, ...);
31a8d03eb9SSascha Wildner #endif
3204ff94b9SMatthew Dillon 
3300612573SJoerg Sonnenberger void __stack_smash_handler(char[], int);
3404ff94b9SMatthew Dillon 
356f2a5fe6SJoerg Sonnenberger #if BYTE_ORDER == LITTLE_ENDIAN
366f2a5fe6SJoerg Sonnenberger int __guard = 0x00000aff;
376f2a5fe6SJoerg Sonnenberger #else
386f2a5fe6SJoerg Sonnenberger int __guard = 0xff0a0000;
396f2a5fe6SJoerg Sonnenberger #endif
408d06e7f9SDavid Rhodus 
418d06e7f9SDavid Rhodus void
__stack_smash_handler(char func[],int damaged)4200612573SJoerg Sonnenberger __stack_smash_handler(char func[], int damaged)
4304ff94b9SMatthew Dillon {
44*479ab7f0SSascha Wildner #ifdef __i386__ /* used in stand/boot */
4500612573SJoerg Sonnenberger 	void *caller;
4600612573SJoerg Sonnenberger 	__asm __volatile("movl 4(%%ebp), %0" : "=r" (caller));
4700612573SJoerg Sonnenberger 	panic("stack overflow in function %p (%s)", caller, func);
4800612573SJoerg Sonnenberger #else
4904ff94b9SMatthew Dillon 	panic("stack overflow in function %s", func);
5000612573SJoerg Sonnenberger #endif
5104ff94b9SMatthew Dillon }
52