xref: /haiku/src/system/libroot/os/stack_protector.cpp (revision 909af08f4328301fbdef1ffb41f566c3b5bec0c7)
1 /*
2  * Copyright 2021, Jérôme Duval, jerome.duval@gmail.com.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <fcntl.h>
8 #include <signal.h>
9 #include <stdlib.h>
10 #include <sys/cdefs.h>
11 
12 #include "private/system/symbol_visibility.h"
13 
14 
15 extern "C" {
16 
17 long __stack_chk_guard = 0;
18 
19 
20 void
21 __init_stack_protector()
22 {
23 	if (__stack_chk_guard != 0)
24 		return;
25 
26 	int status = getentropy(&__stack_chk_guard, sizeof(__stack_chk_guard));
27 	if (status != 0) {
28 		unsigned char* p = (unsigned char *)&__stack_chk_guard;
29 		p[0] = 0;
30 		p[1] = 0;
31 		p[2] = '\n';
32 		p[3] = 0xff;
33 	}
34 }
35 
36 
37 void
38 __stack_chk_fail()
39 {
40 	HIDDEN_FUNCTION(__stack_chk_fail_local);
41 	sigset_t mask;
42 	sigfillset(&mask);
43 	sigdelset(&mask, SIGABRT);
44 	sigprocmask(SIG_BLOCK, &mask, NULL);
45 
46 	abort();
47 }
48 
49 }
50 
51 extern "C" void __stack_chk_fail_local() HIDDEN_FUNCTION_ATTRIBUTE;
52 __weak_reference(__stack_chk_fail, __stack_chk_fail_local);
53