xref: /freebsd/sys/kern/stack_protector.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3 
4 #include <sys/types.h>
5 #include <sys/param.h>
6 #include <sys/kernel.h>
7 #include <sys/systm.h>
8 #include <sys/libkern.h>
9 
10 long __stack_chk_guard[8] = {};
11 void __stack_chk_fail(void);
12 
13 void
14 __stack_chk_fail(void)
15 {
16 
17 	panic("stack overflow detected; backtrace may be corrupted");
18 }
19 
20 #define __arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
21 static void
22 __stack_chk_init(void *dummy __unused)
23 {
24 	size_t i;
25 	long guard[__arraycount(__stack_chk_guard)];
26 
27 	arc4rand(guard, sizeof(guard), 0);
28 	for (i = 0; i < __arraycount(guard); i++)
29 		__stack_chk_guard[i] = guard[i];
30 }
31 SYSINIT(stack_chk, SI_SUB_RANDOM, SI_ORDER_ANY, __stack_chk_init, NULL);
32