]>
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" | |
1588c277 | 27 | RCSID("$OpenBSD: monitor_mm.c,v 1.6 2002/06/04 23:05:49 markus 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 | { | |
41 | return ((char *)a->address - (char *)b->address); | |
42 | } | |
43 | ||
44 | RB_GENERATE(mmtree, mm_share, next, mm_compare) | |
45 | ||
46 | static struct mm_share * | |
47 | mm_make_entry(struct mm_master *mm, struct mmtree *head, | |
48 | void *address, size_t size) | |
49 | { | |
50 | struct mm_share *tmp, *tmp2; | |
51 | ||
52 | if (mm->mmalloc == NULL) | |
53 | tmp = xmalloc(sizeof(struct mm_share)); | |
54 | else | |
55 | tmp = mm_xmalloc(mm->mmalloc, sizeof(struct mm_share)); | |
56 | tmp->address = address; | |
57 | tmp->size = size; | |
58 | ||
59 | tmp2 = RB_INSERT(mmtree, head, tmp); | |
60 | if (tmp2 != NULL) | |
3074b20c | 61 | fatal("mm_make_entry(%p): double address %p->%p(%lu)", |
62 | mm, tmp2, address, (u_long)size); | |
1853d1ef | 63 | |
64 | return (tmp); | |
65 | } | |
66 | ||
67 | /* Creates a shared memory area of a certain size */ | |
68 | ||
69 | struct mm_master * | |
70 | mm_create(struct mm_master *mmalloc, size_t size) | |
71 | { | |
72 | void *address; | |
73 | struct mm_master *mm; | |
74 | ||
75 | if (mmalloc == NULL) | |
76 | mm = xmalloc(sizeof(struct mm_master)); | |
77 | else | |
78 | mm = mm_xmalloc(mmalloc, sizeof(struct mm_master)); | |
79 | ||
80 | /* | |
81 | * If the memory map has a mm_master it can be completely | |
82 | * shared including authentication between the child | |
83 | * and the client. | |
84 | */ | |
85 | mm->mmalloc = mmalloc; | |
86 | ||
1c6249af | 87 | #ifdef HAVE_MMAP_ANON_SHARED |
1853d1ef | 88 | address = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_ANON|MAP_SHARED, |
89 | -1, 0); | |
1e8f8c5b | 90 | #else |
1c6249af | 91 | fatal("%s: UsePrivilegeSeparation=yes and Compression=yes not supported", |
1588c277 | 92 | __func__); |
1e8f8c5b | 93 | #endif |
1853d1ef | 94 | |
95 | mm->address = address; | |
96 | mm->size = size; | |
97 | ||
98 | RB_INIT(&mm->rb_free); | |
99 | RB_INIT(&mm->rb_allocated); | |
100 | ||
101 | mm_make_entry(mm, &mm->rb_free, address, size); | |
102 | ||
103 | return (mm); | |
104 | } | |
105 | ||
106 | /* Frees either the allocated or the free list */ | |
107 | ||
108 | static void | |
109 | mm_freelist(struct mm_master *mmalloc, struct mmtree *head) | |
110 | { | |
111 | struct mm_share *mms, *next; | |
112 | ||
113 | for (mms = RB_ROOT(head); mms; mms = next) { | |
114 | next = RB_NEXT(mmtree, head, mms); | |
115 | RB_REMOVE(mmtree, head, mms); | |
116 | if (mmalloc == NULL) | |
117 | xfree(mms); | |
118 | else | |
119 | mm_free(mmalloc, mms); | |
120 | } | |
121 | } | |
122 | ||
123 | /* Destroys a memory mapped area */ | |
124 | ||
125 | void | |
126 | mm_destroy(struct mm_master *mm) | |
127 | { | |
128 | mm_freelist(mm->mmalloc, &mm->rb_free); | |
129 | mm_freelist(mm->mmalloc, &mm->rb_allocated); | |
130 | ||
1bf74eac | 131 | #ifdef HAVE_MMAP |
1853d1ef | 132 | if (munmap(mm->address, mm->size) == -1) |
441b3f63 | 133 | fatal("munmap(%p, %lu): %s", mm->address, (u_long)mm->size, |
c22d8cc0 | 134 | strerror(errno)); |
1bf74eac | 135 | #else |
136 | fatal("%s: UsePrivilegeSeparation=yes not supported", | |
1588c277 | 137 | __func__); |
1bf74eac | 138 | #endif |
1853d1ef | 139 | if (mm->mmalloc == NULL) |
140 | xfree(mm); | |
141 | else | |
142 | mm_free(mm->mmalloc, mm); | |
143 | } | |
144 | ||
145 | void * | |
146 | mm_xmalloc(struct mm_master *mm, size_t size) | |
147 | { | |
148 | void *address; | |
149 | ||
150 | address = mm_malloc(mm, size); | |
151 | if (address == NULL) | |
1588c277 | 152 | fatal("%s: mm_malloc(%lu)", __func__, (u_long)size); |
1853d1ef | 153 | return (address); |
154 | } | |
155 | ||
156 | ||
157 | /* Allocates data from a memory mapped area */ | |
158 | ||
159 | void * | |
160 | mm_malloc(struct mm_master *mm, size_t size) | |
161 | { | |
162 | struct mm_share *mms, *tmp; | |
163 | ||
164 | if (size == 0) | |
165 | fatal("mm_malloc: try to allocate 0 space"); | |
166 | ||
167 | size = ((size + MM_MINSIZE - 1) / MM_MINSIZE) * MM_MINSIZE; | |
168 | ||
169 | RB_FOREACH(mms, mmtree, &mm->rb_free) { | |
170 | if (mms->size >= size) | |
171 | break; | |
172 | } | |
173 | ||
174 | if (mms == NULL) | |
175 | return (NULL); | |
176 | ||
177 | /* Debug */ | |
178 | memset(mms->address, 0xd0, size); | |
179 | ||
180 | tmp = mm_make_entry(mm, &mm->rb_allocated, mms->address, size); | |
181 | ||
182 | /* Does not change order in RB tree */ | |
183 | mms->size -= size; | |
184 | mms->address = (u_char *)mms->address + size; | |
185 | ||
186 | if (mms->size == 0) { | |
187 | RB_REMOVE(mmtree, &mm->rb_free, mms); | |
188 | if (mm->mmalloc == NULL) | |
189 | xfree(mms); | |
190 | else | |
191 | mm_free(mm->mmalloc, mms); | |
192 | } | |
193 | ||
194 | return (tmp->address); | |
195 | } | |
196 | ||
197 | /* Frees memory in a memory mapped area */ | |
198 | ||
199 | void | |
200 | mm_free(struct mm_master *mm, void *address) | |
201 | { | |
202 | struct mm_share *mms, *prev, tmp; | |
203 | ||
204 | tmp.address = address; | |
205 | mms = RB_FIND(mmtree, &mm->rb_allocated, &tmp); | |
206 | if (mms == NULL) | |
207 | fatal("mm_free(%p): can not find %p", mm, address); | |
208 | ||
209 | /* Debug */ | |
210 | memset(mms->address, 0xd0, mms->size); | |
211 | ||
212 | /* Remove from allocated list and insert in free list */ | |
213 | RB_REMOVE(mmtree, &mm->rb_allocated, mms); | |
214 | if (RB_INSERT(mmtree, &mm->rb_free, mms) != NULL) | |
215 | fatal("mm_free(%p): double address %p", mm, address); | |
216 | ||
217 | /* Find previous entry */ | |
218 | prev = mms; | |
219 | if (RB_LEFT(prev, next)) { | |
220 | prev = RB_LEFT(prev, next); | |
221 | while (RB_RIGHT(prev, next)) | |
222 | prev = RB_RIGHT(prev, next); | |
223 | } else { | |
224 | if (RB_PARENT(prev, next) && | |
225 | (prev == RB_RIGHT(RB_PARENT(prev, next), next))) | |
226 | prev = RB_PARENT(prev, next); | |
227 | else { | |
228 | while (RB_PARENT(prev, next) && | |
229 | (prev == RB_LEFT(RB_PARENT(prev, next), next))) | |
230 | prev = RB_PARENT(prev, next); | |
231 | prev = RB_PARENT(prev, next); | |
232 | } | |
233 | } | |
234 | ||
235 | /* Check if range does not overlap */ | |
236 | if (prev != NULL && MM_ADDRESS_END(prev) > address) | |
3074b20c | 237 | fatal("mm_free: memory corruption: %p(%lu) > %p", |
238 | prev->address, (u_long)prev->size, address); | |
1853d1ef | 239 | |
240 | /* See if we can merge backwards */ | |
241 | if (prev != NULL && MM_ADDRESS_END(prev) == address) { | |
242 | prev->size += mms->size; | |
243 | RB_REMOVE(mmtree, &mm->rb_free, mms); | |
244 | if (mm->mmalloc == NULL) | |
245 | xfree(mms); | |
246 | else | |
247 | mm_free(mm->mmalloc, mms); | |
248 | } else | |
249 | prev = mms; | |
250 | ||
251 | if (prev == NULL) | |
252 | return; | |
253 | ||
254 | /* Check if we can merge forwards */ | |
255 | mms = RB_NEXT(mmtree, &mm->rb_free, prev); | |
256 | if (mms == NULL) | |
257 | return; | |
258 | ||
259 | if (MM_ADDRESS_END(prev) > mms->address) | |
3074b20c | 260 | fatal("mm_free: memory corruption: %p < %p(%lu)", |
261 | mms->address, prev->address, (u_long)prev->size); | |
1853d1ef | 262 | if (MM_ADDRESS_END(prev) != mms->address) |
263 | return; | |
264 | ||
265 | prev->size += mms->size; | |
266 | RB_REMOVE(mmtree, &mm->rb_free, mms); | |
267 | ||
268 | if (mm->mmalloc == NULL) | |
269 | xfree(mms); | |
270 | else | |
271 | mm_free(mm->mmalloc, mms); | |
272 | } | |
273 | ||
274 | static void | |
275 | mm_sync_list(struct mmtree *oldtree, struct mmtree *newtree, | |
276 | struct mm_master *mm, struct mm_master *mmold) | |
277 | { | |
278 | struct mm_master *mmalloc = mm->mmalloc; | |
279 | struct mm_share *mms, *new; | |
280 | ||
281 | /* Sync free list */ | |
282 | RB_FOREACH(mms, mmtree, oldtree) { | |
283 | /* Check the values */ | |
284 | mm_memvalid(mmold, mms, sizeof(struct mm_share)); | |
285 | mm_memvalid(mm, mms->address, mms->size); | |
286 | ||
287 | new = mm_xmalloc(mmalloc, sizeof(struct mm_share)); | |
288 | memcpy(new, mms, sizeof(struct mm_share)); | |
289 | RB_INSERT(mmtree, newtree, new); | |
290 | } | |
291 | } | |
292 | ||
293 | void | |
294 | mm_share_sync(struct mm_master **pmm, struct mm_master **pmmalloc) | |
295 | { | |
296 | struct mm_master *mm; | |
297 | struct mm_master *mmalloc; | |
298 | struct mm_master *mmold; | |
299 | struct mmtree rb_free, rb_allocated; | |
300 | ||
1588c277 | 301 | debug3("%s: Share sync", __func__); |
1853d1ef | 302 | |
303 | mm = *pmm; | |
304 | mmold = mm->mmalloc; | |
305 | mm_memvalid(mmold, mm, sizeof(*mm)); | |
306 | ||
307 | mmalloc = mm_create(NULL, mm->size); | |
308 | mm = mm_xmalloc(mmalloc, sizeof(struct mm_master)); | |
309 | memcpy(mm, *pmm, sizeof(struct mm_master)); | |
310 | mm->mmalloc = mmalloc; | |
311 | ||
312 | rb_free = mm->rb_free; | |
313 | rb_allocated = mm->rb_allocated; | |
314 | ||
315 | RB_INIT(&mm->rb_free); | |
316 | RB_INIT(&mm->rb_allocated); | |
317 | ||
318 | mm_sync_list(&rb_free, &mm->rb_free, mm, mmold); | |
319 | mm_sync_list(&rb_allocated, &mm->rb_allocated, mm, mmold); | |
320 | ||
321 | mm_destroy(mmold); | |
322 | ||
323 | *pmm = mm; | |
324 | *pmmalloc = mmalloc; | |
325 | ||
1588c277 | 326 | debug3("%s: Share sync end", __func__); |
1853d1ef | 327 | } |
328 | ||
329 | void | |
330 | mm_memvalid(struct mm_master *mm, void *address, size_t size) | |
331 | { | |
332 | void *end = (u_char *)address + size; | |
333 | ||
334 | if (address < mm->address) | |
335 | fatal("mm_memvalid: address too small: %p", address); | |
336 | if (end < address) | |
337 | fatal("mm_memvalid: end < address: %p < %p", end, address); | |
338 | if (end > (void *)((u_char *)mm->address + mm->size)) | |
339 | fatal("mm_memvalid: address too large: %p", address); | |
340 | } |