blob: f72a180ea80a46f78d01c711a95a4506d7c9fa5c [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 Lindstroma7961622002-07-04 00:08:23 +000027RCSID("$OpenBSD: monitor_mm.c,v 1.7 2002/06/28 01:49:31 millert 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{
Ben Lindstroma7961622002-07-04 00:08:23 +000041 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);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000049}
50
51RB_GENERATE(mmtree, mm_share, next, mm_compare)
52
53static struct mm_share *
54mm_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)
Ben Lindstromc8615472002-03-26 03:20:45 +000068 fatal("mm_make_entry(%p): double address %p->%p(%lu)",
69 mm, tmp2, address, (u_long)size);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070
71 return (tmp);
72}
73
74/* Creates a shared memory area of a certain size */
75
76struct mm_master *
77mm_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
Ben Lindstrom6b0c96a2002-06-25 03:22:03 +000094#ifdef HAVE_MMAP_ANON_SHARED
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095 address = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_ANON|MAP_SHARED,
96 -1, 0);
Ben Lindstroma95fd3f2002-06-26 00:22:57 +000097 if (address == MAP_FAILED)
98 fatal("mmap(%lu): %s", (u_long)size, strerror(errno));
Kevin Steves86b9fe62002-04-07 17:08:53 +000099#else
Ben Lindstrom6b0c96a2002-06-25 03:22:03 +0000100 fatal("%s: UsePrivilegeSeparation=yes and Compression=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000101 __func__);
Kevin Steves86b9fe62002-04-07 17:08:53 +0000102#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000103
104 mm->address = address;
105 mm->size = size;
106
107 RB_INIT(&mm->rb_free);
108 RB_INIT(&mm->rb_allocated);
109
110 mm_make_entry(mm, &mm->rb_free, address, size);
111
112 return (mm);
113}
114
115/* Frees either the allocated or the free list */
116
117static void
118mm_freelist(struct mm_master *mmalloc, struct mmtree *head)
119{
120 struct mm_share *mms, *next;
121
122 for (mms = RB_ROOT(head); mms; mms = next) {
123 next = RB_NEXT(mmtree, head, mms);
124 RB_REMOVE(mmtree, head, mms);
125 if (mmalloc == NULL)
126 xfree(mms);
127 else
128 mm_free(mmalloc, mms);
129 }
130}
131
132/* Destroys a memory mapped area */
133
134void
135mm_destroy(struct mm_master *mm)
136{
137 mm_freelist(mm->mmalloc, &mm->rb_free);
138 mm_freelist(mm->mmalloc, &mm->rb_allocated);
139
Ben Lindstrom4e3c6312002-06-26 00:29:02 +0000140#ifdef HAVE_MMAP_ANON_SHARED
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000141 if (munmap(mm->address, mm->size) == -1)
Ben Lindstrom4eeccc72002-06-07 01:57:25 +0000142 fatal("munmap(%p, %lu): %s", mm->address, (u_long)mm->size,
Ben Lindstrom105ccbe2002-06-06 20:33:06 +0000143 strerror(errno));
Kevin Steves265c9d02002-04-07 22:36:49 +0000144#else
Ben Lindstrom4e3c6312002-06-26 00:29:02 +0000145 fatal("%s: UsePrivilegeSeparation=yes and Compression=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000146 __func__);
Kevin Steves265c9d02002-04-07 22:36:49 +0000147#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000148 if (mm->mmalloc == NULL)
149 xfree(mm);
150 else
151 mm_free(mm->mmalloc, mm);
152}
153
154void *
155mm_xmalloc(struct mm_master *mm, size_t size)
156{
157 void *address;
158
159 address = mm_malloc(mm, size);
160 if (address == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000161 fatal("%s: mm_malloc(%lu)", __func__, (u_long)size);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000162 return (address);
163}
164
165
166/* Allocates data from a memory mapped area */
167
168void *
169mm_malloc(struct mm_master *mm, size_t size)
170{
171 struct mm_share *mms, *tmp;
172
173 if (size == 0)
174 fatal("mm_malloc: try to allocate 0 space");
175
176 size = ((size + MM_MINSIZE - 1) / MM_MINSIZE) * MM_MINSIZE;
177
178 RB_FOREACH(mms, mmtree, &mm->rb_free) {
179 if (mms->size >= size)
180 break;
181 }
182
183 if (mms == NULL)
184 return (NULL);
185
186 /* Debug */
187 memset(mms->address, 0xd0, size);
188
189 tmp = mm_make_entry(mm, &mm->rb_allocated, mms->address, size);
190
191 /* Does not change order in RB tree */
192 mms->size -= size;
193 mms->address = (u_char *)mms->address + size;
194
195 if (mms->size == 0) {
196 RB_REMOVE(mmtree, &mm->rb_free, mms);
197 if (mm->mmalloc == NULL)
198 xfree(mms);
199 else
200 mm_free(mm->mmalloc, mms);
201 }
202
203 return (tmp->address);
204}
205
206/* Frees memory in a memory mapped area */
207
208void
209mm_free(struct mm_master *mm, void *address)
210{
211 struct mm_share *mms, *prev, tmp;
212
213 tmp.address = address;
214 mms = RB_FIND(mmtree, &mm->rb_allocated, &tmp);
215 if (mms == NULL)
216 fatal("mm_free(%p): can not find %p", mm, address);
217
218 /* Debug */
219 memset(mms->address, 0xd0, mms->size);
220
221 /* Remove from allocated list and insert in free list */
222 RB_REMOVE(mmtree, &mm->rb_allocated, mms);
223 if (RB_INSERT(mmtree, &mm->rb_free, mms) != NULL)
224 fatal("mm_free(%p): double address %p", mm, address);
225
226 /* Find previous entry */
227 prev = mms;
228 if (RB_LEFT(prev, next)) {
229 prev = RB_LEFT(prev, next);
230 while (RB_RIGHT(prev, next))
231 prev = RB_RIGHT(prev, next);
232 } else {
233 if (RB_PARENT(prev, next) &&
234 (prev == RB_RIGHT(RB_PARENT(prev, next), next)))
235 prev = RB_PARENT(prev, next);
236 else {
237 while (RB_PARENT(prev, next) &&
238 (prev == RB_LEFT(RB_PARENT(prev, next), next)))
239 prev = RB_PARENT(prev, next);
240 prev = RB_PARENT(prev, next);
241 }
242 }
243
244 /* Check if range does not overlap */
245 if (prev != NULL && MM_ADDRESS_END(prev) > address)
Ben Lindstromc8615472002-03-26 03:20:45 +0000246 fatal("mm_free: memory corruption: %p(%lu) > %p",
247 prev->address, (u_long)prev->size, address);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000248
249 /* See if we can merge backwards */
250 if (prev != NULL && MM_ADDRESS_END(prev) == address) {
251 prev->size += mms->size;
252 RB_REMOVE(mmtree, &mm->rb_free, mms);
253 if (mm->mmalloc == NULL)
254 xfree(mms);
255 else
256 mm_free(mm->mmalloc, mms);
257 } else
258 prev = mms;
259
260 if (prev == NULL)
261 return;
262
263 /* Check if we can merge forwards */
264 mms = RB_NEXT(mmtree, &mm->rb_free, prev);
265 if (mms == NULL)
266 return;
267
268 if (MM_ADDRESS_END(prev) > mms->address)
Ben Lindstromc8615472002-03-26 03:20:45 +0000269 fatal("mm_free: memory corruption: %p < %p(%lu)",
270 mms->address, prev->address, (u_long)prev->size);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000271 if (MM_ADDRESS_END(prev) != mms->address)
272 return;
273
274 prev->size += mms->size;
275 RB_REMOVE(mmtree, &mm->rb_free, mms);
276
277 if (mm->mmalloc == NULL)
278 xfree(mms);
279 else
280 mm_free(mm->mmalloc, mms);
281}
282
283static void
284mm_sync_list(struct mmtree *oldtree, struct mmtree *newtree,
285 struct mm_master *mm, struct mm_master *mmold)
286{
287 struct mm_master *mmalloc = mm->mmalloc;
288 struct mm_share *mms, *new;
289
290 /* Sync free list */
291 RB_FOREACH(mms, mmtree, oldtree) {
292 /* Check the values */
293 mm_memvalid(mmold, mms, sizeof(struct mm_share));
294 mm_memvalid(mm, mms->address, mms->size);
295
296 new = mm_xmalloc(mmalloc, sizeof(struct mm_share));
297 memcpy(new, mms, sizeof(struct mm_share));
298 RB_INSERT(mmtree, newtree, new);
299 }
300}
301
302void
303mm_share_sync(struct mm_master **pmm, struct mm_master **pmmalloc)
304{
305 struct mm_master *mm;
306 struct mm_master *mmalloc;
307 struct mm_master *mmold;
308 struct mmtree rb_free, rb_allocated;
309
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000310 debug3("%s: Share sync", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000311
312 mm = *pmm;
313 mmold = mm->mmalloc;
314 mm_memvalid(mmold, mm, sizeof(*mm));
315
316 mmalloc = mm_create(NULL, mm->size);
317 mm = mm_xmalloc(mmalloc, sizeof(struct mm_master));
318 memcpy(mm, *pmm, sizeof(struct mm_master));
319 mm->mmalloc = mmalloc;
320
321 rb_free = mm->rb_free;
322 rb_allocated = mm->rb_allocated;
323
324 RB_INIT(&mm->rb_free);
325 RB_INIT(&mm->rb_allocated);
326
327 mm_sync_list(&rb_free, &mm->rb_free, mm, mmold);
328 mm_sync_list(&rb_allocated, &mm->rb_allocated, mm, mmold);
329
330 mm_destroy(mmold);
331
332 *pmm = mm;
333 *pmmalloc = mmalloc;
334
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000335 debug3("%s: Share sync end", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000336}
337
338void
339mm_memvalid(struct mm_master *mm, void *address, size_t size)
340{
341 void *end = (u_char *)address + size;
342
343 if (address < mm->address)
344 fatal("mm_memvalid: address too small: %p", address);
345 if (end < address)
346 fatal("mm_memvalid: end < address: %p < %p", end, address);
347 if (end > (void *)((u_char *)mm->address + mm->size))
348 fatal("mm_memvalid: address too large: %p", address);
349}