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