+/* $OpenBSD: monitor_mm.c,v 1.15 2006/08/03 03:34:42 deraadt Exp $ */
/*
- * Copyright 2001 Niels Provos <provos@citi.umich.edu>
+ * Copyright 2002 Niels Provos <provos@citi.umich.edu>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
*/
#include "includes.h"
-RCSID("$OpenBSD$");
+#include <sys/types.h>
+#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
+#endif
+#include <sys/param.h>
+#include "openbsd-compat/sys-tree.h"
+
+#include <errno.h>
+#include <stdarg.h>
+#include <string.h>
-#include "ssh.h"
#include "xmalloc.h"
+#include "ssh.h"
#include "log.h"
#include "monitor_mm.h"
static int
mm_compare(struct mm_share *a, struct mm_share *b)
{
- return (a->address - b->address);
+ long diff = (char *)a->address - (char *)b->address;
+
+ if (diff == 0)
+ return (0);
+ else if (diff < 0)
+ return (-1);
+ else
+ return (1);
}
-RB_GENERATE(mmtree, mm_share, next, mm_compare);
+RB_GENERATE(mmtree, mm_share, next, mm_compare)
static struct mm_share *
mm_make_entry(struct mm_master *mm, struct mmtree *head,
tmp2 = RB_INSERT(mmtree, head, tmp);
if (tmp2 != NULL)
- fatal("mm_make_entry(%p): double address %p->%p(%d)",
- mm, tmp2, address, size);
+ fatal("mm_make_entry(%p): double address %p->%p(%lu)",
+ mm, tmp2, address, (u_long)size);
return (tmp);
}
else
mm = mm_xmalloc(mmalloc, sizeof(struct mm_master));
- /*
+ /*
* If the memory map has a mm_master it can be completely
* shared including authentication between the child
* and the client.
*/
mm->mmalloc = mmalloc;
- address = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_ANON|MAP_SHARED,
- -1, 0);
- if (address == MAP_FAILED)
- fatal("mmap(%d)", size);
+ address = xmmap(size);
+ if (address == (void *)MAP_FAILED)
+ fatal("mmap(%lu): %s", (u_long)size, strerror(errno));
mm->address = address;
mm->size = size;
/* Frees either the allocated or the free list */
-void
+static void
mm_freelist(struct mm_master *mmalloc, struct mmtree *head)
{
struct mm_share *mms, *next;
mm_freelist(mm->mmalloc, &mm->rb_free);
mm_freelist(mm->mmalloc, &mm->rb_allocated);
+#ifdef HAVE_MMAP
if (munmap(mm->address, mm->size) == -1)
- fatal("munmap(%p, %d)", mm->address, mm->size);
+ fatal("munmap(%p, %lu): %s", mm->address, (u_long)mm->size,
+ strerror(errno));
+#else
+ fatal("%s: UsePrivilegeSeparation=yes and Compression=yes not supported",
+ __func__);
+#endif
if (mm->mmalloc == NULL)
xfree(mm);
else
address = mm_malloc(mm, size);
if (address == NULL)
- fatal("%s: mm_malloc(%d)", __FUNCTION__, size);
+ fatal("%s: mm_malloc(%lu)", __func__, (u_long)size);
return (address);
}
if (size == 0)
fatal("mm_malloc: try to allocate 0 space");
+ if (size > SIZE_T_MAX - MM_MINSIZE + 1)
+ fatal("mm_malloc: size too big");
- size = ((size + MM_MINSIZE - 1) / MM_MINSIZE) * MM_MINSIZE;
+ size = ((size + (MM_MINSIZE - 1)) / MM_MINSIZE) * MM_MINSIZE;
RB_FOREACH(mms, mmtree, &mm->rb_free) {
if (mms->size >= size)
if (mms == NULL)
return (NULL);
- /* Debug */
+ /* Debug */
memset(mms->address, 0xd0, size);
tmp = mm_make_entry(mm, &mm->rb_allocated, mms->address, size);
if (mms == NULL)
fatal("mm_free(%p): can not find %p", mm, address);
- /* Debug */
+ /* Debug */
memset(mms->address, 0xd0, mms->size);
/* Remove from allocated list and insert in free list */
/* Check if range does not overlap */
if (prev != NULL && MM_ADDRESS_END(prev) > address)
- fatal("mm_free: memory corruption: %p(%d) > %p",
- prev->address, prev->size, address);
+ fatal("mm_free: memory corruption: %p(%lu) > %p",
+ prev->address, (u_long)prev->size, address);
/* See if we can merge backwards */
if (prev != NULL && MM_ADDRESS_END(prev) == address) {
return;
if (MM_ADDRESS_END(prev) > mms->address)
- fatal("mm_free: memory corruption: %p < %p(%d)",
- mms->address, prev->address, prev->size);
+ fatal("mm_free: memory corruption: %p < %p(%lu)",
+ mms->address, prev->address, (u_long)prev->size);
if (MM_ADDRESS_END(prev) != mms->address)
return;
mm_free(mm->mmalloc, mms);
}
-void
+static void
mm_sync_list(struct mmtree *oldtree, struct mmtree *newtree,
struct mm_master *mm, struct mm_master *mmold)
{
struct mm_master *mmold;
struct mmtree rb_free, rb_allocated;
- debug3("%s: Share sync", __FUNCTION__);
+ debug3("%s: Share sync", __func__);
mm = *pmm;
mmold = mm->mmalloc;
*pmm = mm;
*pmmalloc = mmalloc;
- debug3("%s: Share sync end", __FUNCTION__);
+ debug3("%s: Share sync end", __func__);
}
void