]> andersk Git - gssapi-openssh.git/blob - openssh/monitor_mm.c
Initial revision
[gssapi-openssh.git] / openssh / monitor_mm.c
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"
27 RCSID("$OpenBSD: monitor_mm.c,v 1.4 2002/03/25 20:12:10 stevesk Exp $");
28
29 #ifdef HAVE_SYS_MMAN_H
30 #include <sys/mman.h>
31 #endif
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)
61                 fatal("mm_make_entry(%p): double address %p->%p(%lu)",
62                     mm, tmp2, address, (u_long)size);
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
87 #if  defined(HAVE_MMAP) && defined(MAP_ANON)
88         address = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_ANON|MAP_SHARED,
89             -1, 0);
90         if (address == MAP_FAILED)
91                 fatal("mmap(%lu)", (u_long)size);
92 #else
93         fatal("%s: UsePrivilegeSeparation=yes not supported",
94             __FUNCTION__);
95 #endif
96
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
110 static void
111 mm_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
127 void
128 mm_destroy(struct mm_master *mm)
129 {
130         mm_freelist(mm->mmalloc, &mm->rb_free);
131         mm_freelist(mm->mmalloc, &mm->rb_allocated);
132
133 #ifdef HAVE_MMAP
134         if (munmap(mm->address, mm->size) == -1)
135                 fatal("munmap(%p, %lu)", mm->address, (u_long)mm->size);
136 #else
137         fatal("%s: UsePrivilegeSeparation=yes not supported",
138             __FUNCTION__);
139 #endif
140         if (mm->mmalloc == NULL)
141                 xfree(mm);
142         else
143                 mm_free(mm->mmalloc, mm);
144 }
145
146 void *
147 mm_xmalloc(struct mm_master *mm, size_t size)
148 {
149         void *address;
150
151         address = mm_malloc(mm, size);
152         if (address == NULL)
153                 fatal("%s: mm_malloc(%lu)", __FUNCTION__, (u_long)size);
154         return (address);
155 }
156
157
158 /* Allocates data from a memory mapped area */
159
160 void *
161 mm_malloc(struct mm_master *mm, size_t size)
162 {
163         struct mm_share *mms, *tmp;
164
165         if (size == 0)
166                 fatal("mm_malloc: try to allocate 0 space");
167
168         size = ((size + MM_MINSIZE - 1) / MM_MINSIZE) * MM_MINSIZE;
169
170         RB_FOREACH(mms, mmtree, &mm->rb_free) {
171                 if (mms->size >= size)
172                         break;
173         }
174
175         if (mms == NULL)
176                 return (NULL);
177
178         /* Debug */
179         memset(mms->address, 0xd0, size);
180
181         tmp = mm_make_entry(mm, &mm->rb_allocated, mms->address, size);
182
183         /* Does not change order in RB tree */
184         mms->size -= size;
185         mms->address = (u_char *)mms->address + size;
186
187         if (mms->size == 0) {
188                 RB_REMOVE(mmtree, &mm->rb_free, mms);
189                 if (mm->mmalloc == NULL)
190                         xfree(mms);
191                 else
192                         mm_free(mm->mmalloc, mms);
193         }
194
195         return (tmp->address);
196 }
197
198 /* Frees memory in a memory mapped area */
199
200 void
201 mm_free(struct mm_master *mm, void *address)
202 {
203         struct mm_share *mms, *prev, tmp;
204
205         tmp.address = address;
206         mms = RB_FIND(mmtree, &mm->rb_allocated, &tmp);
207         if (mms == NULL)
208                 fatal("mm_free(%p): can not find %p", mm, address);
209
210         /* Debug */
211         memset(mms->address, 0xd0, mms->size);
212
213         /* Remove from allocated list and insert in free list */
214         RB_REMOVE(mmtree, &mm->rb_allocated, mms);
215         if (RB_INSERT(mmtree, &mm->rb_free, mms) != NULL)
216                 fatal("mm_free(%p): double address %p", mm, address);
217
218         /* Find previous entry */
219         prev = mms;
220         if (RB_LEFT(prev, next)) {
221                 prev = RB_LEFT(prev, next);
222                 while (RB_RIGHT(prev, next))
223                         prev = RB_RIGHT(prev, next);
224         } else {
225                 if (RB_PARENT(prev, next) &&
226                     (prev == RB_RIGHT(RB_PARENT(prev, next), next)))
227                         prev = RB_PARENT(prev, next);
228                 else {
229                         while (RB_PARENT(prev, next) &&
230                             (prev == RB_LEFT(RB_PARENT(prev, next), next)))
231                                 prev = RB_PARENT(prev, next);
232                         prev = RB_PARENT(prev, next);
233                 }
234         }
235
236         /* Check if range does not overlap */
237         if (prev != NULL && MM_ADDRESS_END(prev) > address)
238                 fatal("mm_free: memory corruption: %p(%lu) > %p",
239                     prev->address, (u_long)prev->size, address);
240
241         /* See if we can merge backwards */
242         if (prev != NULL && MM_ADDRESS_END(prev) == address) {
243                 prev->size += mms->size;
244                 RB_REMOVE(mmtree, &mm->rb_free, mms);
245                 if (mm->mmalloc == NULL)
246                         xfree(mms);
247                 else
248                         mm_free(mm->mmalloc, mms);
249         } else
250                 prev = mms;
251
252         if (prev == NULL)
253                 return;
254
255         /* Check if we can merge forwards */
256         mms = RB_NEXT(mmtree, &mm->rb_free, prev);
257         if (mms == NULL)
258                 return;
259
260         if (MM_ADDRESS_END(prev) > mms->address)
261                 fatal("mm_free: memory corruption: %p < %p(%lu)",
262                     mms->address, prev->address, (u_long)prev->size);
263         if (MM_ADDRESS_END(prev) != mms->address)
264                 return;
265
266         prev->size += mms->size;
267         RB_REMOVE(mmtree, &mm->rb_free, mms);
268
269         if (mm->mmalloc == NULL)
270                 xfree(mms);
271         else
272                 mm_free(mm->mmalloc, mms);
273 }
274
275 static void
276 mm_sync_list(struct mmtree *oldtree, struct mmtree *newtree,
277     struct mm_master *mm, struct mm_master *mmold)
278 {
279         struct mm_master *mmalloc = mm->mmalloc;
280         struct mm_share *mms, *new;
281
282         /* Sync free list */
283         RB_FOREACH(mms, mmtree, oldtree) {
284                 /* Check the values */
285                 mm_memvalid(mmold, mms, sizeof(struct mm_share));
286                 mm_memvalid(mm, mms->address, mms->size);
287
288                 new = mm_xmalloc(mmalloc, sizeof(struct mm_share));
289                 memcpy(new, mms, sizeof(struct mm_share));
290                 RB_INSERT(mmtree, newtree, new);
291         }
292 }
293
294 void
295 mm_share_sync(struct mm_master **pmm, struct mm_master **pmmalloc)
296 {
297         struct mm_master *mm;
298         struct mm_master *mmalloc;
299         struct mm_master *mmold;
300         struct mmtree rb_free, rb_allocated;
301
302         debug3("%s: Share sync", __FUNCTION__);
303
304         mm = *pmm;
305         mmold = mm->mmalloc;
306         mm_memvalid(mmold, mm, sizeof(*mm));
307
308         mmalloc = mm_create(NULL, mm->size);
309         mm = mm_xmalloc(mmalloc, sizeof(struct mm_master));
310         memcpy(mm, *pmm, sizeof(struct mm_master));
311         mm->mmalloc = mmalloc;
312
313         rb_free = mm->rb_free;
314         rb_allocated = mm->rb_allocated;
315
316         RB_INIT(&mm->rb_free);
317         RB_INIT(&mm->rb_allocated);
318
319         mm_sync_list(&rb_free, &mm->rb_free, mm, mmold);
320         mm_sync_list(&rb_allocated, &mm->rb_allocated, mm, mmold);
321
322         mm_destroy(mmold);
323
324         *pmm = mm;
325         *pmmalloc = mmalloc;
326
327         debug3("%s: Share sync end", __FUNCTION__);
328 }
329
330 void
331 mm_memvalid(struct mm_master *mm, void *address, size_t size)
332 {
333         void *end = (u_char *)address + size;
334
335         if (address < mm->address)
336                 fatal("mm_memvalid: address too small: %p", address);
337         if (end < address)
338                 fatal("mm_memvalid: end < address: %p < %p", end, address);
339         if (end > (void *)((u_char *)mm->address + mm->size))
340                 fatal("mm_memvalid: address too large: %p", address);
341 }
This page took 0.108528 seconds and 5 git commands to generate.