xref: /freebsd/sys/kern/stack_protector.c (revision db33c6f3ae9d1231087710068ee4ea5398aacca7)
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
12 __stack_chk_fail(void)
13 {
14 
15 	panic("stack overflow detected; backtrace may be corrupted");
16 }
17 
18 static void
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