blob: b4a6e40c90bfa7bd3a8477e2edbfbb0b8899dbe3 [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 Lindstrom0a4f7542002-08-20 18:36:25 +000027RCSID("$OpenBSD: monitor_mm.c,v 1.8 2002/08/02 14:43:15 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
Ben Lindstrom446d8862002-07-23 00:03:33 +000033#include "openbsd-compat/xmmap.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000034#include "ssh.h"
35#include "xmalloc.h"
36#include "log.h"
37#include "monitor_mm.h"
38
39static int
40mm_compare(struct mm_share *a, struct mm_share *b)
41{
Ben Lindstroma7961622002-07-04 00:08:23 +000042 long diff = (char *)a->address - (char *)b->address;
43
44 if (diff == 0)
45 return (0);
46 else if (diff < 0)
47 return (-1);
48 else
49 return (1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000050}
51
52RB_GENERATE(mmtree, mm_share, next, mm_compare)
53
54static struct mm_share *
55mm_make_entry(struct mm_master *mm, struct mmtree *head,
56 void *address, size_t size)
57{
58 struct mm_share *tmp, *tmp2;
59
60 if (mm->mmalloc == NULL)
61 tmp = xmalloc(sizeof(struct mm_share));
62 else
63 tmp = mm_xmalloc(mm->mmalloc, sizeof(struct mm_share));
64 tmp->address = address;
65 tmp->size = size;
66
67 tmp2 = RB_INSERT(mmtree, head, tmp);
68 if (tmp2 != NULL)
Ben Lindstromc8615472002-03-26 03:20:45 +000069 fatal("mm_make_entry(%p): double address %p->%p(%lu)",
70 mm, tmp2, address, (u_long)size);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000071
72 return (tmp);
73}
74
75/* Creates a shared memory area of a certain size */
76
77struct mm_master *
78mm_create(struct mm_master *mmalloc, size_t size)
79{
80 void *address;
81 struct mm_master *mm;
82
83 if (mmalloc == NULL)
84 mm = xmalloc(sizeof(struct mm_master));
85 else
86 mm = mm_xmalloc(mmalloc, sizeof(struct mm_master));
87
88 /*
89 * If the memory map has a mm_master it can be completely
90 * shared including authentication between the child
91 * and the client.
92 */
93 mm->mmalloc = mmalloc;
94
Tim Rice40017b02002-07-14 13:36:49 -070095 address = xmmap(size);
Ben Lindstroma95fd3f2002-06-26 00:22:57 +000096 if (address == MAP_FAILED)
97 fatal("mmap(%lu): %s", (u_long)size, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000098
99 mm->address = address;
100 mm->size = size;
101
102 RB_INIT(&mm->rb_free);
103 RB_INIT(&mm->rb_allocated);
104
105 mm_make_entry(mm, &mm->rb_free, address, size);
106
107 return (mm);
108}
109
110/* Frees either the allocated or the free list */
111
112static void
113mm_freelist(struct mm_master *mmalloc, struct mmtree *head)
114{
115 struct mm_share *mms, *next;
116
117 for (mms = RB_ROOT(head); mms; mms = next) {
118 next = RB_NEXT(mmtree, head, mms);
119 RB_REMOVE(mmtree, head, mms);
120 if (mmalloc == NULL)
121 xfree(mms);
122 else
123 mm_free(mmalloc, mms);
124 }
125}
126
127/* Destroys a memory mapped area */
128
129void
130mm_destroy(struct mm_master *mm)
131{
132 mm_freelist(mm->mmalloc, &mm->rb_free);
133 mm_freelist(mm->mmalloc, &mm->rb_allocated);
134
Tim Rice40017b02002-07-14 13:36:49 -0700135#ifdef HAVE_MMAP
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000136 if (munmap(mm->address, mm->size) == -1)
Ben Lindstrom4eeccc72002-06-07 01:57:25 +0000137 fatal("munmap(%p, %lu): %s", mm->address, (u_long)mm->size,
Ben Lindstrom105ccbe2002-06-06 20:33:06 +0000138 strerror(errno));
Kevin Steves265c9d02002-04-07 22:36:49 +0000139#else
Ben Lindstrom4e3c6312002-06-26 00:29:02 +0000140 fatal("%s: UsePrivilegeSeparation=yes and Compression=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000141 __func__);
Kevin Steves265c9d02002-04-07 22:36:49 +0000142#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000143 if (mm->mmalloc == NULL)
144 xfree(mm);
145 else
146 mm_free(mm->mmalloc, mm);
147}
148
149void *
150mm_xmalloc(struct mm_master *mm, size_t size)
151{
152 void *address;
153
154 address = mm_malloc(mm, size);
155 if (address == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000156 fatal("%s: mm_malloc(%lu)", __func__, (u_long)size);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000157 return (address);
158}
159
160
161/* Allocates data from a memory mapped area */
162
163void *
164mm_malloc(struct mm_master *mm, size_t size)
165{
166 struct mm_share *mms, *tmp;
167
168 if (size == 0)
169 fatal("mm_malloc: try to allocate 0 space");
Ben Lindstrom0a4f7542002-08-20 18:36:25 +0000170 if (size > SIZE_T_MAX - MM_MINSIZE + 1)
171 fatal("mm_malloc: size too big");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000172
Ben Lindstrom0a4f7542002-08-20 18:36:25 +0000173 size = ((size + (MM_MINSIZE - 1)) / MM_MINSIZE) * MM_MINSIZE;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000174
175 RB_FOREACH(mms, mmtree, &mm->rb_free) {
176 if (mms->size >= size)
177 break;
178 }
179
180 if (mms == NULL)
181 return (NULL);
182
183 /* Debug */
184 memset(mms->address, 0xd0, size);
185
186 tmp = mm_make_entry(mm, &mm->rb_allocated, mms->address, size);
187
188 /* Does not change order in RB tree */
189 mms->size -= size;
190 mms->address = (u_char *)mms->address + size;
191
192 if (mms->size == 0) {
193 RB_REMOVE(mmtree, &mm->rb_free, mms);
194 if (mm->mmalloc == NULL)
195 xfree(mms);
196 else
197 mm_free(mm->mmalloc, mms);
198 }
199
200 return (tmp->address);
201}
202
203/* Frees memory in a memory mapped area */
204
205void
206mm_free(struct mm_master *mm, void *address)
207{
208 struct mm_share *mms, *prev, tmp;
209
210 tmp.address = address;
211 mms = RB_FIND(mmtree, &mm->rb_allocated, &tmp);
212 if (mms == NULL)
213 fatal("mm_free(%p): can not find %p", mm, address);
214
215 /* Debug */
216 memset(mms->address, 0xd0, mms->size);
217
218 /* Remove from allocated list and insert in free list */
219 RB_REMOVE(mmtree, &mm->rb_allocated, mms);
220 if (RB_INSERT(mmtree, &mm->rb_free, mms) != NULL)
221 fatal("mm_free(%p): double address %p", mm, address);
222
223 /* Find previous entry */
224 prev = mms;
225 if (RB_LEFT(prev, next)) {
226 prev = RB_LEFT(prev, next);
227 while (RB_RIGHT(prev, next))
228 prev = RB_RIGHT(prev, next);
229 } else {
230 if (RB_PARENT(prev, next) &&
231 (prev == RB_RIGHT(RB_PARENT(prev, next), next)))
232 prev = RB_PARENT(prev, next);
233 else {
234 while (RB_PARENT(prev, next) &&
235 (prev == RB_LEFT(RB_PARENT(prev, next), next)))
236 prev = RB_PARENT(prev, next);
237 prev = RB_PARENT(prev, next);
238 }
239 }
240
241 /* Check if range does not overlap */
242 if (prev != NULL && MM_ADDRESS_END(prev) > address)
Ben Lindstromc8615472002-03-26 03:20:45 +0000243 fatal("mm_free: memory corruption: %p(%lu) > %p",
244 prev->address, (u_long)prev->size, address);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000245
246 /* See if we can merge backwards */
247 if (prev != NULL && MM_ADDRESS_END(prev) == address) {
248 prev->size += mms->size;
249 RB_REMOVE(mmtree, &mm->rb_free, mms);
250 if (mm->mmalloc == NULL)
251 xfree(mms);
252 else
253 mm_free(mm->mmalloc, mms);
254 } else
255 prev = mms;
256
257 if (prev == NULL)
258 return;
259
260 /* Check if we can merge forwards */
261 mms = RB_NEXT(mmtree, &mm->rb_free, prev);
262 if (mms == NULL)
263 return;
264
265 if (MM_ADDRESS_END(prev) > mms->address)
Ben Lindstromc8615472002-03-26 03:20:45 +0000266 fatal("mm_free: memory corruption: %p < %p(%lu)",
267 mms->address, prev->address, (u_long)prev->size);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000268 if (MM_ADDRESS_END(prev) != mms->address)
269 return;
270
271 prev->size += mms->size;
272 RB_REMOVE(mmtree, &mm->rb_free, mms);
273
274 if (mm->mmalloc == NULL)
275 xfree(mms);
276 else
277 mm_free(mm->mmalloc, mms);
278}
279
280static void
281mm_sync_list(struct mmtree *oldtree, struct mmtree *newtree,
282 struct mm_master *mm, struct mm_master *mmold)
283{
284 struct mm_master *mmalloc = mm->mmalloc;
285 struct mm_share *mms, *new;
286
287 /* Sync free list */
288 RB_FOREACH(mms, mmtree, oldtree) {
289 /* Check the values */
290 mm_memvalid(mmold, mms, sizeof(struct mm_share));
291 mm_memvalid(mm, mms->address, mms->size);
292
293 new = mm_xmalloc(mmalloc, sizeof(struct mm_share));
294 memcpy(new, mms, sizeof(struct mm_share));
295 RB_INSERT(mmtree, newtree, new);
296 }
297}
298
299void
300mm_share_sync(struct mm_master **pmm, struct mm_master **pmmalloc)
301{
302 struct mm_master *mm;
303 struct mm_master *mmalloc;
304 struct mm_master *mmold;
305 struct mmtree rb_free, rb_allocated;
306
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000307 debug3("%s: Share sync", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000308
309 mm = *pmm;
310 mmold = mm->mmalloc;
311 mm_memvalid(mmold, mm, sizeof(*mm));
312
313 mmalloc = mm_create(NULL, mm->size);
314 mm = mm_xmalloc(mmalloc, sizeof(struct mm_master));
315 memcpy(mm, *pmm, sizeof(struct mm_master));
316 mm->mmalloc = mmalloc;
317
318 rb_free = mm->rb_free;
319 rb_allocated = mm->rb_allocated;
320
321 RB_INIT(&mm->rb_free);
322 RB_INIT(&mm->rb_allocated);
323
324 mm_sync_list(&rb_free, &mm->rb_free, mm, mmold);
325 mm_sync_list(&rb_allocated, &mm->rb_allocated, mm, mmold);
326
327 mm_destroy(mmold);
328
329 *pmm = mm;
330 *pmmalloc = mmalloc;
331
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000332 debug3("%s: Share sync end", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000333}
334
335void
336mm_memvalid(struct mm_master *mm, void *address, size_t size)
337{
338 void *end = (u_char *)address + size;
339
340 if (address < mm->address)
341 fatal("mm_memvalid: address too small: %p", address);
342 if (end < address)
343 fatal("mm_memvalid: end < address: %p < %p", end, address);
344 if (end > (void *)((u_char *)mm->address + mm->size))
345 fatal("mm_memvalid: address too large: %p", address);
346}