]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - crypto/openssh/monitor_mm.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / crypto / openssh / monitor_mm.c
1 /* $OpenBSD: monitor_mm.c,v 1.17 2013/05/17 00:13:13 djm Exp $ */
2 /*
3  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "includes.h"
28
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_MMAN_H
31 #include <sys/mman.h>
32 #endif
33 #include <sys/param.h>
34 #include "openbsd-compat/sys-tree.h"
35
36 #include <errno.h>
37 #include <stdarg.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "xmalloc.h"
42 #include "ssh.h"
43 #include "log.h"
44 #include "monitor_mm.h"
45
46 static int
47 mm_compare(struct mm_share *a, struct mm_share *b)
48 {
49         long diff = (char *)a->address - (char *)b->address;
50
51         if (diff == 0)
52                 return (0);
53         else if (diff < 0)
54                 return (-1);
55         else
56                 return (1);
57 }
58
59 RB_GENERATE(mmtree, mm_share, next, mm_compare)
60
61 static struct mm_share *
62 mm_make_entry(struct mm_master *mm, struct mmtree *head,
63     void *address, size_t size)
64 {
65         struct mm_share *tmp, *tmp2;
66
67         if (mm->mmalloc == NULL)
68                 tmp = xmalloc(sizeof(struct mm_share));
69         else
70                 tmp = mm_xmalloc(mm->mmalloc, sizeof(struct mm_share));
71         tmp->address = address;
72         tmp->size = size;
73
74         tmp2 = RB_INSERT(mmtree, head, tmp);
75         if (tmp2 != NULL)
76                 fatal("mm_make_entry(%p): double address %p->%p(%lu)",
77                     mm, tmp2, address, (u_long)size);
78
79         return (tmp);
80 }
81
82 /* Creates a shared memory area of a certain size */
83
84 struct mm_master *
85 mm_create(struct mm_master *mmalloc, size_t size)
86 {
87         void *address;
88         struct mm_master *mm;
89
90         if (mmalloc == NULL)
91                 mm = xmalloc(sizeof(struct mm_master));
92         else
93                 mm = mm_xmalloc(mmalloc, sizeof(struct mm_master));
94
95         /*
96          * If the memory map has a mm_master it can be completely
97          * shared including authentication between the child
98          * and the client.
99          */
100         mm->mmalloc = mmalloc;
101
102         address = xmmap(size);
103         if (address == (void *)MAP_FAILED)
104                 fatal("mmap(%lu): %s", (u_long)size, strerror(errno));
105
106         mm->address = address;
107         mm->size = size;
108
109         RB_INIT(&mm->rb_free);
110         RB_INIT(&mm->rb_allocated);
111
112         mm_make_entry(mm, &mm->rb_free, address, size);
113
114         return (mm);
115 }
116
117 /* Frees either the allocated or the free list */
118
119 static void
120 mm_freelist(struct mm_master *mmalloc, struct mmtree *head)
121 {
122         struct mm_share *mms, *next;
123
124         for (mms = RB_ROOT(head); mms; mms = next) {
125                 next = RB_NEXT(mmtree, head, mms);
126                 RB_REMOVE(mmtree, head, mms);
127                 if (mmalloc == NULL)
128                         free(mms);
129                 else
130                         mm_free(mmalloc, mms);
131         }
132 }
133
134 /* Destroys a memory mapped area */
135
136 void
137 mm_destroy(struct mm_master *mm)
138 {
139         mm_freelist(mm->mmalloc, &mm->rb_free);
140         mm_freelist(mm->mmalloc, &mm->rb_allocated);
141
142 #ifdef HAVE_MMAP
143         if (munmap(mm->address, mm->size) == -1)
144                 fatal("munmap(%p, %lu): %s", mm->address, (u_long)mm->size,
145                     strerror(errno));
146 #else
147         fatal("%s: UsePrivilegeSeparation=yes and Compression=yes not supported",
148             __func__);
149 #endif
150         if (mm->mmalloc == NULL)
151                 free(mm);
152         else
153                 mm_free(mm->mmalloc, mm);
154 }
155
156 void *
157 mm_xmalloc(struct mm_master *mm, size_t size)
158 {
159         void *address;
160
161         address = mm_malloc(mm, size);
162         if (address == NULL)
163                 fatal("%s: mm_malloc(%lu)", __func__, (u_long)size);
164         return (address);
165 }
166
167
168 /* Allocates data from a memory mapped area */
169
170 void *
171 mm_malloc(struct mm_master *mm, size_t size)
172 {
173         struct mm_share *mms, *tmp;
174
175         if (size == 0)
176                 fatal("mm_malloc: try to allocate 0 space");
177         if (size > SIZE_T_MAX - MM_MINSIZE + 1)
178                 fatal("mm_malloc: size too big");
179
180         size = ((size + (MM_MINSIZE - 1)) / MM_MINSIZE) * MM_MINSIZE;
181
182         RB_FOREACH(mms, mmtree, &mm->rb_free) {
183                 if (mms->size >= size)
184                         break;
185         }
186
187         if (mms == NULL)
188                 return (NULL);
189
190         /* Debug */
191         memset(mms->address, 0xd0, size);
192
193         tmp = mm_make_entry(mm, &mm->rb_allocated, mms->address, size);
194
195         /* Does not change order in RB tree */
196         mms->size -= size;
197         mms->address = (u_char *)mms->address + size;
198
199         if (mms->size == 0) {
200                 RB_REMOVE(mmtree, &mm->rb_free, mms);
201                 if (mm->mmalloc == NULL)
202                         free(mms);
203                 else
204                         mm_free(mm->mmalloc, mms);
205         }
206
207         return (tmp->address);
208 }
209
210 /* Frees memory in a memory mapped area */
211
212 void
213 mm_free(struct mm_master *mm, void *address)
214 {
215         struct mm_share *mms, *prev, tmp;
216
217         tmp.address = address;
218         mms = RB_FIND(mmtree, &mm->rb_allocated, &tmp);
219         if (mms == NULL)
220                 fatal("mm_free(%p): can not find %p", mm, address);
221
222         /* Debug */
223         memset(mms->address, 0xd0, mms->size);
224
225         /* Remove from allocated list and insert in free list */
226         RB_REMOVE(mmtree, &mm->rb_allocated, mms);
227         if (RB_INSERT(mmtree, &mm->rb_free, mms) != NULL)
228                 fatal("mm_free(%p): double address %p", mm, address);
229
230         /* Find previous entry */
231         prev = mms;
232         if (RB_LEFT(prev, next)) {
233                 prev = RB_LEFT(prev, next);
234                 while (RB_RIGHT(prev, next))
235                         prev = RB_RIGHT(prev, next);
236         } else {
237                 if (RB_PARENT(prev, next) &&
238                     (prev == RB_RIGHT(RB_PARENT(prev, next), next)))
239                         prev = RB_PARENT(prev, next);
240                 else {
241                         while (RB_PARENT(prev, next) &&
242                             (prev == RB_LEFT(RB_PARENT(prev, next), next)))
243                                 prev = RB_PARENT(prev, next);
244                         prev = RB_PARENT(prev, next);
245                 }
246         }
247
248         /* Check if range does not overlap */
249         if (prev != NULL && MM_ADDRESS_END(prev) > address)
250                 fatal("mm_free: memory corruption: %p(%lu) > %p",
251                     prev->address, (u_long)prev->size, address);
252
253         /* See if we can merge backwards */
254         if (prev != NULL && MM_ADDRESS_END(prev) == address) {
255                 prev->size += mms->size;
256                 RB_REMOVE(mmtree, &mm->rb_free, mms);
257                 if (mm->mmalloc == NULL)
258                         free(mms);
259                 else
260                         mm_free(mm->mmalloc, mms);
261         } else
262                 prev = mms;
263
264         if (prev == NULL)
265                 return;
266
267         /* Check if we can merge forwards */
268         mms = RB_NEXT(mmtree, &mm->rb_free, prev);
269         if (mms == NULL)
270                 return;
271
272         if (MM_ADDRESS_END(prev) > mms->address)
273                 fatal("mm_free: memory corruption: %p < %p(%lu)",
274                     mms->address, prev->address, (u_long)prev->size);
275         if (MM_ADDRESS_END(prev) != mms->address)
276                 return;
277
278         prev->size += mms->size;
279         RB_REMOVE(mmtree, &mm->rb_free, mms);
280
281         if (mm->mmalloc == NULL)
282                 free(mms);
283         else
284                 mm_free(mm->mmalloc, mms);
285 }
286
287 static void
288 mm_sync_list(struct mmtree *oldtree, struct mmtree *newtree,
289     struct mm_master *mm, struct mm_master *mmold)
290 {
291         struct mm_master *mmalloc = mm->mmalloc;
292         struct mm_share *mms, *new;
293
294         /* Sync free list */
295         RB_FOREACH(mms, mmtree, oldtree) {
296                 /* Check the values */
297                 mm_memvalid(mmold, mms, sizeof(struct mm_share));
298                 mm_memvalid(mm, mms->address, mms->size);
299
300                 new = mm_xmalloc(mmalloc, sizeof(struct mm_share));
301                 memcpy(new, mms, sizeof(struct mm_share));
302                 RB_INSERT(mmtree, newtree, new);
303         }
304 }
305
306 void
307 mm_share_sync(struct mm_master **pmm, struct mm_master **pmmalloc)
308 {
309         struct mm_master *mm;
310         struct mm_master *mmalloc;
311         struct mm_master *mmold;
312         struct mmtree rb_free, rb_allocated;
313
314         debug3("%s: Share sync", __func__);
315
316         mm = *pmm;
317         mmold = mm->mmalloc;
318         mm_memvalid(mmold, mm, sizeof(*mm));
319
320         mmalloc = mm_create(NULL, mm->size);
321         mm = mm_xmalloc(mmalloc, sizeof(struct mm_master));
322         memcpy(mm, *pmm, sizeof(struct mm_master));
323         mm->mmalloc = mmalloc;
324
325         rb_free = mm->rb_free;
326         rb_allocated = mm->rb_allocated;
327
328         RB_INIT(&mm->rb_free);
329         RB_INIT(&mm->rb_allocated);
330
331         mm_sync_list(&rb_free, &mm->rb_free, mm, mmold);
332         mm_sync_list(&rb_allocated, &mm->rb_allocated, mm, mmold);
333
334         mm_destroy(mmold);
335
336         *pmm = mm;
337         *pmmalloc = mmalloc;
338
339         debug3("%s: Share sync end", __func__);
340 }
341
342 void
343 mm_memvalid(struct mm_master *mm, void *address, size_t size)
344 {
345         void *end = (u_char *)address + size;
346
347         if (address < mm->address)
348                 fatal("mm_memvalid: address too small: %p", address);
349         if (end < address)
350                 fatal("mm_memvalid: end < address: %p < %p", end, address);
351         if (end > (void *)((u_char *)mm->address + mm->size))
352                 fatal("mm_memvalid: address too large: %p", address);
353 }