xref: /freebsd/sys/kern/stack_protector.c (revision 830940567b49bb0c08dfaed40418999e76616909)
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 /* SI_SUB_EVENTHANDLER is right after SI_SUB_LOCK used by arc4rand() init. */
32 SYSINIT(stack_chk, SI_SUB_EVENTHANDLER, SI_ORDER_ANY, __stack_chk_init, NULL);
33