blob: b9ea978a6604c3074163f77b2ae2c740a74f5a5b [file] [log] [blame]
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001/*
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"
Ben Lindstromc8615472002-03-26 03:20:45 +000027RCSID("$OpenBSD: monitor_mm.c,v 1.4 2002/03/25 20:12:10 stevesk Exp $");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000028
Kevin Steves86b9fe62002-04-07 17:08:53 +000029#ifdef HAVE_SYS_MMAN_H
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000030#include <sys/mman.h>
Kevin Steves86b9fe62002-04-07 17:08:53 +000031#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000032
33#include "ssh.h"
34#include "xmalloc.h"
35#include "log.h"
36#include "monitor_mm.h"
37
38static int
39mm_compare(struct mm_share *a, struct mm_share *b)
40{
41 return ((char *)a->address - (char *)b->address);
42}
43
44RB_GENERATE(mmtree, mm_share, next, mm_compare)
45
46static struct mm_share *
47mm_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)
Ben Lindstromc8615472002-03-26 03:20:45 +000061 fatal("mm_make_entry(%p): double address %p->%p(%lu)",
62 mm, tmp2, address, (u_long)size);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000063
64 return (tmp);
65}
66
67/* Creates a shared memory area of a certain size */
68
69struct mm_master *
70mm_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
Kevin Steves86b9fe62002-04-07 17:08:53 +000087#ifdef HAVE_MMAP
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000088 address = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_ANON|MAP_SHARED,
89 -1, 0);
90 if (address == MAP_FAILED)
Ben Lindstromc8615472002-03-26 03:20:45 +000091 fatal("mmap(%lu)", (u_long)size);
Kevin Steves86b9fe62002-04-07 17:08:53 +000092#else
93 fatal("%s: UsePrivilegeSeparation=yes not supported",
94 __FUNCTION__);
95#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000096
97 mm->address = address;
98 mm->size = size;
99
100 RB_INIT(&mm->rb_free);
101 RB_INIT(&mm->rb_allocated);
102
103 mm_make_entry(mm, &mm->rb_free, address, size);
104
105 return (mm);
106}
107
108/* Frees either the allocated or the free list */
109
110static void
111mm_freelist(struct mm_master *mmalloc, struct mmtree *head)
112{
113 struct mm_share *mms, *next;
114
115 for (mms = RB_ROOT(head); mms; mms = next) {
116 next = RB_NEXT(mmtree, head, mms);
117 RB_REMOVE(mmtree, head, mms);
118 if (mmalloc == NULL)
119 xfree(mms);
120 else
121 mm_free(mmalloc, mms);
122 }
123}
124
125/* Destroys a memory mapped area */
126
127void
128mm_destroy(struct mm_master *mm)
129{
130 mm_freelist(mm->mmalloc, &mm->rb_free);
131 mm_freelist(mm->mmalloc, &mm->rb_allocated);
132
133 if (munmap(mm->address, mm->size) == -1)
Ben Lindstromc8615472002-03-26 03:20:45 +0000134 fatal("munmap(%p, %lu)", mm->address, (u_long)mm->size);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000135 if (mm->mmalloc == NULL)
136 xfree(mm);
137 else
138 mm_free(mm->mmalloc, mm);
139}
140
141void *
142mm_xmalloc(struct mm_master *mm, size_t size)
143{
144 void *address;
145
146 address = mm_malloc(mm, size);
147 if (address == NULL)
Ben Lindstromc8615472002-03-26 03:20:45 +0000148 fatal("%s: mm_malloc(%lu)", __FUNCTION__, (u_long)size);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000149 return (address);
150}
151
152
153/* Allocates data from a memory mapped area */
154
155void *
156mm_malloc(struct mm_master *mm, size_t size)
157{
158 struct mm_share *mms, *tmp;
159
160 if (size == 0)
161 fatal("mm_malloc: try to allocate 0 space");
162
163 size = ((size + MM_MINSIZE - 1) / MM_MINSIZE) * MM_MINSIZE;
164
165 RB_FOREACH(mms, mmtree, &mm->rb_free) {
166 if (mms->size >= size)
167 break;
168 }
169
170 if (mms == NULL)
171 return (NULL);
172
173 /* Debug */
174 memset(mms->address, 0xd0, size);
175
176 tmp = mm_make_entry(mm, &mm->rb_allocated, mms->address, size);
177
178 /* Does not change order in RB tree */
179 mms->size -= size;
180 mms->address = (u_char *)mms->address + size;
181
182 if (mms->size == 0) {
183 RB_REMOVE(mmtree, &mm->rb_free, mms);
184 if (mm->mmalloc == NULL)
185 xfree(mms);
186 else
187 mm_free(mm->mmalloc, mms);
188 }
189
190 return (tmp->address);
191}
192
193/* Frees memory in a memory mapped area */
194
195void
196mm_free(struct mm_master *mm, void *address)
197{
198 struct mm_share *mms, *prev, tmp;
199
200 tmp.address = address;
201 mms = RB_FIND(mmtree, &mm->rb_allocated, &tmp);
202 if (mms == NULL)
203 fatal("mm_free(%p): can not find %p", mm, address);
204
205 /* Debug */
206 memset(mms->address, 0xd0, mms->size);
207
208 /* Remove from allocated list and insert in free list */
209 RB_REMOVE(mmtree, &mm->rb_allocated, mms);
210 if (RB_INSERT(mmtree, &mm->rb_free, mms) != NULL)
211 fatal("mm_free(%p): double address %p", mm, address);
212
213 /* Find previous entry */
214 prev = mms;
215 if (RB_LEFT(prev, next)) {
216 prev = RB_LEFT(prev, next);
217 while (RB_RIGHT(prev, next))
218 prev = RB_RIGHT(prev, next);
219 } else {
220 if (RB_PARENT(prev, next) &&
221 (prev == RB_RIGHT(RB_PARENT(prev, next), next)))
222 prev = RB_PARENT(prev, next);
223 else {
224 while (RB_PARENT(prev, next) &&
225 (prev == RB_LEFT(RB_PARENT(prev, next), next)))
226 prev = RB_PARENT(prev, next);
227 prev = RB_PARENT(prev, next);
228 }
229 }
230
231 /* Check if range does not overlap */
232 if (prev != NULL && MM_ADDRESS_END(prev) > address)
Ben Lindstromc8615472002-03-26 03:20:45 +0000233 fatal("mm_free: memory corruption: %p(%lu) > %p",
234 prev->address, (u_long)prev->size, address);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000235
236 /* See if we can merge backwards */
237 if (prev != NULL && MM_ADDRESS_END(prev) == address) {
238 prev->size += mms->size;
239 RB_REMOVE(mmtree, &mm->rb_free, mms);
240 if (mm->mmalloc == NULL)
241 xfree(mms);
242 else
243 mm_free(mm->mmalloc, mms);
244 } else
245 prev = mms;
246
247 if (prev == NULL)
248 return;
249
250 /* Check if we can merge forwards */
251 mms = RB_NEXT(mmtree, &mm->rb_free, prev);
252 if (mms == NULL)
253 return;
254
255 if (MM_ADDRESS_END(prev) > mms->address)
Ben Lindstromc8615472002-03-26 03:20:45 +0000256 fatal("mm_free: memory corruption: %p < %p(%lu)",
257 mms->address, prev->address, (u_long)prev->size);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000258 if (MM_ADDRESS_END(prev) != mms->address)
259 return;
260
261 prev->size += mms->size;
262 RB_REMOVE(mmtree, &mm->rb_free, mms);
263
264 if (mm->mmalloc == NULL)
265 xfree(mms);
266 else
267 mm_free(mm->mmalloc, mms);
268}
269
270static void
271mm_sync_list(struct mmtree *oldtree, struct mmtree *newtree,
272 struct mm_master *mm, struct mm_master *mmold)
273{
274 struct mm_master *mmalloc = mm->mmalloc;
275 struct mm_share *mms, *new;
276
277 /* Sync free list */
278 RB_FOREACH(mms, mmtree, oldtree) {
279 /* Check the values */
280 mm_memvalid(mmold, mms, sizeof(struct mm_share));
281 mm_memvalid(mm, mms->address, mms->size);
282
283 new = mm_xmalloc(mmalloc, sizeof(struct mm_share));
284 memcpy(new, mms, sizeof(struct mm_share));
285 RB_INSERT(mmtree, newtree, new);
286 }
287}
288
289void
290mm_share_sync(struct mm_master **pmm, struct mm_master **pmmalloc)
291{
292 struct mm_master *mm;
293 struct mm_master *mmalloc;
294 struct mm_master *mmold;
295 struct mmtree rb_free, rb_allocated;
296
297 debug3("%s: Share sync", __FUNCTION__);
298
299 mm = *pmm;
300 mmold = mm->mmalloc;
301 mm_memvalid(mmold, mm, sizeof(*mm));
302
303 mmalloc = mm_create(NULL, mm->size);
304 mm = mm_xmalloc(mmalloc, sizeof(struct mm_master));
305 memcpy(mm, *pmm, sizeof(struct mm_master));
306 mm->mmalloc = mmalloc;
307
308 rb_free = mm->rb_free;
309 rb_allocated = mm->rb_allocated;
310
311 RB_INIT(&mm->rb_free);
312 RB_INIT(&mm->rb_allocated);
313
314 mm_sync_list(&rb_free, &mm->rb_free, mm, mmold);
315 mm_sync_list(&rb_allocated, &mm->rb_allocated, mm, mmold);
316
317 mm_destroy(mmold);
318
319 *pmm = mm;
320 *pmmalloc = mmalloc;
321
322 debug3("%s: Share sync end", __FUNCTION__);
323}
324
325void
326mm_memvalid(struct mm_master *mm, void *address, size_t size)
327{
328 void *end = (u_char *)address + size;
329
330 if (address < mm->address)
331 fatal("mm_memvalid: address too small: %p", address);
332 if (end < address)
333 fatal("mm_memvalid: end < address: %p < %p", end, address);
334 if (end > (void *)((u_char *)mm->address + mm->size))
335 fatal("mm_memvalid: address too large: %p", address);
336}