xref: /freebsd/sys/kern/stack_protector.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
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 static void
21 __stack_chk_init(void *dummy __unused)
22 {
23 	size_t i;
24 	long guard[nitems(__stack_chk_guard)];
25 
26 	arc4rand(guard, sizeof(guard), 0);
27 	for (i = 0; i < nitems(guard); i++)
28 		__stack_chk_guard[i] = guard[i];
29 }
30 SYSINIT(stack_chk, SI_SUB_RANDOM, SI_ORDER_ANY, __stack_chk_init, NULL);
31