xref: /openbsd-src/libexec/ld.so/util.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: util.c,v 1.21 2010/10/30 15:36:32 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 Per Fogelstrom, Opsycon AB
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/mman.h>
32 #include <sys/sysctl.h>
33 #include <string.h>
34 #include "archdep.h"
35 
36 /*
37  * Stack protector dummies.
38  * Ideally, a scheme to compile these stubs from libc should be used, but
39  * this would end up dragging too much code from libc here.
40  */
41 long __guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
42 
43 void __stack_smash_handler(char [], int);
44 
45 void
46 __stack_smash_handler(char func[], int damaged)
47 {
48 	_dl_exit(127);
49 }
50 
51 /*
52  * Static vars usable after bootstrapping.
53  */
54 static void *_dl_malloc_pool = 0;
55 static long *_dl_malloc_free = 0;
56 
57 char *
58 _dl_strdup(const char *orig)
59 {
60 	char *newstr;
61 	int len;
62 
63 	len = _dl_strlen(orig)+1;
64 	newstr = _dl_malloc(len);
65 	_dl_strlcpy(newstr, orig, len);
66 	return (newstr);
67 }
68 
69 
70 /*
71  * The following malloc/free code is a very simplified implementation
72  * of a malloc function. However, we do not need to be very complex here
73  * because we only free memory when 'dlclose()' is called and we can
74  * reuse at least the memory allocated for the object descriptor. We have
75  * one dynamic string allocated, the library name and it is likely that
76  * we can reuse that one to without a lot of complex colapsing code.
77  */
78 void *
79 _dl_malloc(size_t need)
80 {
81 	long *p, *t, *n, have;
82 
83 	need = (need + 2*DL_MALLOC_ALIGN - 1) & ~(DL_MALLOC_ALIGN - 1);
84 
85 	if ((t = _dl_malloc_free) != 0) {	/* Try free list first */
86 		n = (long *)&_dl_malloc_free;
87 		while (t && t[-1] < need) {
88 			n = t;
89 			t = (long *)*t;
90 		}
91 		if (t) {
92 			*n = *t;
93 			_dl_memset(t, 0, t[-1] - DL_MALLOC_ALIGN);
94 			return((void *)t);
95 		}
96 	}
97 	have = _dl_round_page((long)_dl_malloc_pool) - (long)_dl_malloc_pool;
98 	if (need > have) {
99 		if (have >= 8 + DL_MALLOC_ALIGN) {
100 			p = _dl_malloc_pool;
101 			p = (void *) ((long)p + DL_MALLOC_ALIGN);
102 			p[-1] = have;
103 			_dl_free((void *)p);		/* move to freelist */
104 		}
105 		_dl_malloc_pool = (void *)_dl_mmap((void *)0,
106 		    _dl_round_page(need), PROT_READ|PROT_WRITE,
107 		    MAP_ANON|MAP_PRIVATE, -1, 0);
108 		if (_dl_malloc_pool == 0 || _dl_mmap_error(_dl_malloc_pool)) {
109 			_dl_printf("Dynamic loader failure: malloc.\n");
110 			_dl_exit(7);
111 		}
112 	}
113 	p = _dl_malloc_pool;
114 	_dl_malloc_pool += need;
115 	_dl_memset(p, 0, need);
116 	p = (void *) ((long)p + DL_MALLOC_ALIGN);
117 	p[-1] = need;
118 	return (p);
119 }
120 
121 void
122 _dl_free(void *p)
123 {
124 	long *t = (long *)p;
125 
126 	*t = (long)_dl_malloc_free;
127 	_dl_malloc_free = p;
128 }
129 
130 
131 unsigned int
132 _dl_random(void)
133 {
134 	int mib[2];
135 	unsigned int rnd;
136 	size_t len;
137 
138 	mib[0] = CTL_KERN;
139 	mib[1] = KERN_ARND;
140 	len = sizeof(rnd);
141 	_dl_sysctl(mib, 2, &rnd, &len, NULL, 0);
142 
143 	return (rnd);
144 }
145 
146 
147