]>
Commit | Line | Data |
---|---|---|
1853d1ef | 1 | /* |
2 | * Copyright 2002 Niels Provos <provos@citi.umich.edu> | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * | |
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 | */ | |
25 | ||
26 | #include "includes.h" | |
a64d3560 | 27 | RCSID("$OpenBSD: monitor_mm.c,v 1.7 2002/06/28 01:49:31 millert Exp $"); |
1853d1ef | 28 | |
1e8f8c5b | 29 | #ifdef HAVE_SYS_MMAN_H |
1853d1ef | 30 | #include <sys/mman.h> |
1e8f8c5b | 31 | #endif |
1853d1ef | 32 | |
33 | #include "ssh.h" | |
34 | #include "xmalloc.h" | |
35 | #include "log.h" | |
36 | #include "monitor_mm.h" | |
37 | ||
38 | static int | |
39 | mm_compare(struct mm_share *a, struct mm_share *b) | |
40 | { | |
a64d3560 | 41 | long diff = (char *)a->address - (char *)b->address; |
42 | ||
43 | if (diff == 0) | |
44 | return (0); | |
45 | else if (diff < 0) | |
46 | return (-1); | |
47 | else | |
48 | return (1); | |
1853d1ef | 49 | } |
50 | ||
51 | RB_GENERATE(mmtree, mm_share, next, mm_compare) | |
52 | ||
53 | static struct mm_share * | |
54 | mm_make_entry(struct mm_master *mm, struct mmtree *head, | |
55 | void *address, size_t size) | |
56 | { | |
57 | struct mm_share *tmp, *tmp2; | |
58 | ||
59 | if (mm->mmalloc == NULL) | |
60 | tmp = xmalloc(sizeof(struct mm_share)); | |
61 | else | |
62 | tmp = mm_xmalloc(mm->mmalloc, sizeof(struct mm_share)); | |
63 | tmp->address = address; | |
64 | tmp->size = size; | |
65 | ||
66 | tmp2 = RB_INSERT(mmtree, head, tmp); | |
67 | if (tmp2 != NULL) | |
3074b20c | 68 | fatal("mm_make_entry(%p): double address %p->%p(%lu)", |
69 | mm, tmp2, address, (u_long)size); | |
1853d1ef | 70 | |
71 | return (tmp); | |
72 | } | |
73 | ||
74 | /* Creates a shared memory area of a certain size */ | |
75 | ||
76 | struct mm_master * | |
77 | mm_create(struct mm_master *mmalloc, size_t size) | |
78 | { | |
79 | void *address; | |
80 | struct mm_master *mm; | |
81 | ||
82 | if (mmalloc == NULL) | |
83 | mm = xmalloc(sizeof(struct mm_master)); | |
84 | else | |
85 | mm = mm_xmalloc(mmalloc, sizeof(struct mm_master)); | |
86 | ||
87 | /* | |
88 | * If the memory map has a mm_master it can be completely | |
89 | * shared including authentication between the child | |
90 | * and the client. | |
91 | */ | |
92 | mm->mmalloc = mmalloc; | |
93 | ||
4165b82e | 94 | address = xmmap(size); |
78beb77d | 95 | if (address == MAP_FAILED) |
96 | fatal("mmap(%lu): %s", (u_long)size, strerror(errno)); | |
1853d1ef | 97 | |
98 | mm->address = address; | |
99 | mm->size = size; | |
100 | ||
101 | RB_INIT(&mm->rb_free); | |
102 | RB_INIT(&mm->rb_allocated); | |
103 | ||
104 | mm_make_entry(mm, &mm->rb_free, address, size); | |
105 | ||
106 | return (mm); | |
107 | } | |
108 | ||
109 | /* Frees either the allocated or the free list */ | |
110 | ||
111 | static void | |
112 | mm_freelist(struct mm_master *mmalloc, struct mmtree *head) | |
113 | { | |
114 | struct mm_share *mms, *next; | |
115 | ||
116 | for (mms = RB_ROOT(head); mms; mms = next) { | |
117 | next = RB_NEXT(mmtree, head, mms); | |
118 | RB_REMOVE(mmtree, head, mms); | |
119 | if (mmalloc == NULL) | |
120 | xfree(mms); | |
121 | else | |
122 | mm_free(mmalloc, mms); | |
123 | } | |
124 | } | |
125 | ||
126 | /* Destroys a memory mapped area */ | |
127 | ||
128 | void | |
129 | mm_destroy(struct mm_master *mm) | |
130 | { | |
131 | mm_freelist(mm->mmalloc, &mm->rb_free); | |
132 | mm_freelist(mm->mmalloc, &mm->rb_allocated); | |
133 | ||
4165b82e | 134 | #ifdef HAVE_MMAP |
1853d1ef | 135 | if (munmap(mm->address, mm->size) == -1) |
441b3f63 | 136 | fatal("munmap(%p, %lu): %s", mm->address, (u_long)mm->size, |
c22d8cc0 | 137 | strerror(errno)); |
1bf74eac | 138 | #else |
88cb875c | 139 | fatal("%s: UsePrivilegeSeparation=yes and Compression=yes not supported", |
1588c277 | 140 | __func__); |
1bf74eac | 141 | #endif |
1853d1ef | 142 | if (mm->mmalloc == NULL) |
143 | xfree(mm); | |
144 | else | |
145 | mm_free(mm->mmalloc, mm); | |
146 | } | |
147 | ||
148 | void * | |
149 | mm_xmalloc(struct mm_master *mm, size_t size) | |
150 | { | |
151 | void *address; | |
152 | ||
153 | address = mm_malloc(mm, size); | |
154 | if (address == NULL) | |
1588c277 | 155 | fatal("%s: mm_malloc(%lu)", __func__, (u_long)size); |
1853d1ef | 156 | return (address); |
157 | } | |
158 | ||
159 | ||
160 | /* Allocates data from a memory mapped area */ | |
161 | ||
162 | void * | |
163 | mm_malloc(struct mm_master *mm, size_t size) | |
164 | { | |
165 | struct mm_share *mms, *tmp; | |
166 | ||
167 | if (size == 0) | |
168 | fatal("mm_malloc: try to allocate 0 space"); | |
169 | ||
170 | size = ((size + MM_MINSIZE - 1) / MM_MINSIZE) * MM_MINSIZE; | |
171 | ||
172 | RB_FOREACH(mms, mmtree, &mm->rb_free) { | |
173 | if (mms->size >= size) | |
174 | break; | |
175 | } | |
176 | ||
177 | if (mms == NULL) | |
178 | return (NULL); | |
179 | ||
180 | /* Debug */ | |
181 | memset(mms->address, 0xd0, size); | |
182 | ||
183 | tmp = mm_make_entry(mm, &mm->rb_allocated, mms->address, size); | |
184 | ||
185 | /* Does not change order in RB tree */ | |
186 | mms->size -= size; | |
187 | mms->address = (u_char *)mms->address + size; | |
188 | ||
189 | if (mms->size == 0) { | |
190 | RB_REMOVE(mmtree, &mm->rb_free, mms); | |
191 | if (mm->mmalloc == NULL) | |
192 | xfree(mms); | |
193 | else | |
194 | mm_free(mm->mmalloc, mms); | |
195 | } | |
196 | ||
197 | return (tmp->address); | |
198 | } | |
199 | ||
200 | /* Frees memory in a memory mapped area */ | |
201 | ||
202 | void | |
203 | mm_free(struct mm_master *mm, void *address) | |
204 | { | |
205 | struct mm_share *mms, *prev, tmp; | |
206 | ||
207 | tmp.address = address; | |
208 | mms = RB_FIND(mmtree, &mm->rb_allocated, &tmp); | |
209 | if (mms == NULL) | |
210 | fatal("mm_free(%p): can not find %p", mm, address); | |
211 | ||
212 | /* Debug */ | |
213 | memset(mms->address, 0xd0, mms->size); | |
214 | ||
215 | /* Remove from allocated list and insert in free list */ | |
216 | RB_REMOVE(mmtree, &mm->rb_allocated, mms); | |
217 | if (RB_INSERT(mmtree, &mm->rb_free, mms) != NULL) | |
218 | fatal("mm_free(%p): double address %p", mm, address); | |
219 | ||
220 | /* Find previous entry */ | |
221 | prev = mms; | |
222 | if (RB_LEFT(prev, next)) { | |
223 | prev = RB_LEFT(prev, next); | |
224 | while (RB_RIGHT(prev, next)) | |
225 | prev = RB_RIGHT(prev, next); | |
226 | } else { | |
227 | if (RB_PARENT(prev, next) && | |
228 | (prev == RB_RIGHT(RB_PARENT(prev, next), next))) | |
229 | prev = RB_PARENT(prev, next); | |
230 | else { | |
231 | while (RB_PARENT(prev, next) && | |
232 | (prev == RB_LEFT(RB_PARENT(prev, next), next))) | |
233 | prev = RB_PARENT(prev, next); | |
234 | prev = RB_PARENT(prev, next); | |
235 | } | |
236 | } | |
237 | ||
238 | /* Check if range does not overlap */ | |
239 | if (prev != NULL && MM_ADDRESS_END(prev) > address) | |
3074b20c | 240 | fatal("mm_free: memory corruption: %p(%lu) > %p", |
241 | prev->address, (u_long)prev->size, address); | |
1853d1ef | 242 | |
243 | /* See if we can merge backwards */ | |
244 | if (prev != NULL && MM_ADDRESS_END(prev) == address) { | |
245 | prev->size += mms->size; | |
246 | RB_REMOVE(mmtree, &mm->rb_free, mms); | |
247 | if (mm->mmalloc == NULL) | |
248 | xfree(mms); | |
249 | else | |
250 | mm_free(mm->mmalloc, mms); | |
251 | } else | |
252 | prev = mms; | |
253 | ||
254 | if (prev == NULL) | |
255 | return; | |
256 | ||
257 | /* Check if we can merge forwards */ | |
258 | mms = RB_NEXT(mmtree, &mm->rb_free, prev); | |
259 | if (mms == NULL) | |
260 | return; | |
261 | ||
262 | if (MM_ADDRESS_END(prev) > mms->address) | |
3074b20c | 263 | fatal("mm_free: memory corruption: %p < %p(%lu)", |
264 | mms->address, prev->address, (u_long)prev->size); | |
1853d1ef | 265 | if (MM_ADDRESS_END(prev) != mms->address) |
266 | return; | |
267 | ||
268 | prev->size += mms->size; | |
269 | RB_REMOVE(mmtree, &mm->rb_free, mms); | |
270 | ||
271 | if (mm->mmalloc == NULL) | |
272 | xfree(mms); | |
273 | else | |
274 | mm_free(mm->mmalloc, mms); | |
275 | } | |
276 | ||
277 | static void | |
278 | mm_sync_list(struct mmtree *oldtree, struct mmtree *newtree, | |
279 | struct mm_master *mm, struct mm_master *mmold) | |
280 | { | |
281 | struct mm_master *mmalloc = mm->mmalloc; | |
282 | struct mm_share *mms, *new; | |
283 | ||
284 | /* Sync free list */ | |
285 | RB_FOREACH(mms, mmtree, oldtree) { | |
286 | /* Check the values */ | |
287 | mm_memvalid(mmold, mms, sizeof(struct mm_share)); | |
288 | mm_memvalid(mm, mms->address, mms->size); | |
289 | ||
290 | new = mm_xmalloc(mmalloc, sizeof(struct mm_share)); | |
291 | memcpy(new, mms, sizeof(struct mm_share)); | |
292 | RB_INSERT(mmtree, newtree, new); | |
293 | } | |
294 | } | |
295 | ||
296 | void | |
297 | mm_share_sync(struct mm_master **pmm, struct mm_master **pmmalloc) | |
298 | { | |
299 | struct mm_master *mm; | |
300 | struct mm_master *mmalloc; | |
301 | struct mm_master *mmold; | |
302 | struct mmtree rb_free, rb_allocated; | |
303 | ||
1588c277 | 304 | debug3("%s: Share sync", __func__); |
1853d1ef | 305 | |
306 | mm = *pmm; | |
307 | mmold = mm->mmalloc; | |
308 | mm_memvalid(mmold, mm, sizeof(*mm)); | |
309 | ||
310 | mmalloc = mm_create(NULL, mm->size); | |
311 | mm = mm_xmalloc(mmalloc, sizeof(struct mm_master)); | |
312 | memcpy(mm, *pmm, sizeof(struct mm_master)); | |
313 | mm->mmalloc = mmalloc; | |
314 | ||
315 | rb_free = mm->rb_free; | |
316 | rb_allocated = mm->rb_allocated; | |
317 | ||
318 | RB_INIT(&mm->rb_free); | |
319 | RB_INIT(&mm->rb_allocated); | |
320 | ||
321 | mm_sync_list(&rb_free, &mm->rb_free, mm, mmold); | |
322 | mm_sync_list(&rb_allocated, &mm->rb_allocated, mm, mmold); | |
323 | ||
324 | mm_destroy(mmold); | |
325 | ||
326 | *pmm = mm; | |
327 | *pmmalloc = mmalloc; | |
328 | ||
1588c277 | 329 | debug3("%s: Share sync end", __func__); |
1853d1ef | 330 | } |
331 | ||
332 | void | |
333 | mm_memvalid(struct mm_master *mm, void *address, size_t size) | |
334 | { | |
335 | void *end = (u_char *)address + size; | |
336 | ||
337 | if (address < mm->address) | |
338 | fatal("mm_memvalid: address too small: %p", address); | |
339 | if (end < address) | |
340 | fatal("mm_memvalid: end < address: %p < %p", end, address); | |
341 | if (end > (void *)((u_char *)mm->address + mm->size)) | |
342 | fatal("mm_memvalid: address too large: %p", address); | |
343 | } |