1 /*-
2  * Copyright (c) 2008
3  *	Thorsten Glaser <tg@mirbsd.org>
4  *
5  * Provided that these terms and disclaimer and all copyright notices
6  * are retained or reproduced in an accompanying document, permission
7  * is granted to deal in this work without restriction, including un-
8  * limited rights to use, publicly perform, distribute, sell, modify,
9  * merge, give away, or sublicence.
10  *
11  * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
12  * the utmost extent permitted by applicable law, neither express nor
13  * implied; without malicious intent or gross negligence. In no event
14  * may a licensor, author or contributor be held liable for indirect,
15  * direct, other damage, loss, or other issues arising in any way out
16  * of dealing in the work, even if advised of the possibility of such
17  * damage or existence of a defect, except proven that it results out
18  * of said person's immediate fault when using the work as intended.
19  */
20 
21 #include <sys/param.h>
22 #include <sys/sysctl.h>
23 #include "archdep.h"
24 
25 __RCSID("$MirOS: src/libexec/ld.so/ssp.c,v 1.3 2008/10/16 14:56:05 tg Exp $");
26 
27 #if (defined(__SSP__) || defined(__SSP_ALL__)) && \
28     !defined(__IN_MKDEP) && !defined(lint)
29 #error "You must compile this file with -fno-stack-protector"
30 #endif
31 
32 void __guard_setup(void);
33 __dead void __stack_smash_handler(char [], int);
34 
35 long __guard[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
36 
37 void
__guard_setup(void)38 __guard_setup(void)
39 {
40 	unsigned char *cp;
41 	size_t n;
42 	int mib[2];
43 
44 	cp = (unsigned char *)__guard;
45 
46 	*cp++ = 0;
47 	*cp++ = 0;
48 	*cp++ = '\n';
49 	*cp++ = 255;
50 
51 	n = sizeof (__guard) - 4;
52 	mib[0] = CTL_KERN;
53 	mib[1] = KERN_ARND;
54 	/* _dl_sysctl is implemented in asm and thusly safe to use */
55 	_dl_sysctl(mib, 2, cp, &n, NULL, 0);
56 }
57 
58 static const char ovmsg[] = "stack overflow in ld.so function ";
59 
60 void
__stack_smash_handler(char func[],int damaged)61 __stack_smash_handler(char func[], int damaged)
62 {
63 	_dl_write(2, ovmsg, sizeof (ovmsg) - 1);
64 	while (*func) {
65 		_dl_write(2, func, 1);
66 		func++;
67 	}
68 	_dl_write(2, "\n", 1);
69 	_dl_exit(127);
70 }
71