From e4648192c03912f17886ce5e97c7055875784b2b Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sun, 2 Sep 2018 02:03:27 -0400 Subject: [PATCH] split out code for managing page spans --- Makefile | 7 +++-- malloc.c | 81 +----------------------------------------------------- pages.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pages.h | 16 +++++++++++ 4 files changed, 104 insertions(+), 83 deletions(-) create mode 100644 pages.c create mode 100644 pages.h diff --git a/Makefile b/Makefile index 0fe2c1e..08fe295 100644 --- a/Makefile +++ b/Makefile @@ -2,16 +2,17 @@ CPPFLAGS := -D_GNU_SOURCE CFLAGS := -std=c11 -Wall -Wextra -O2 -flto -fPIC -fvisibility=hidden -fno-plt LDFLAGS := -Wl,--as-needed,-z,defs,-z,relro,-z,now LDLIBS := -lpthread -OBJECTS := chacha.o malloc.o memory.o random.o util.o +OBJECTS := chacha.o malloc.o memory.o pages.o random.o util.o hardened_malloc.so: $(OBJECTS) $(CC) $(CFLAGS) $(LDFLAGS) -shared $^ $(LDLIBS) -o $@ -malloc.o: malloc.c malloc.h memory.h random.h util.h +chacha.o: chacha.c chacha.h +malloc.o: malloc.c malloc.h memory.h pages.h random.h util.h memory.o: memory.c memory.h util.h +pages.o: pages.c pages.h memory.h util.h random.o: random.c random.h chacha.h util.h util.o: util.c util.h -chacha.o: chacha.c chacha.h clean: rm -f hardened_malloc.so $(OBJECTS) diff --git a/malloc.c b/malloc.c index 776bdc8..81e1013 100644 --- a/malloc.c +++ b/malloc.c @@ -13,92 +13,13 @@ #include "malloc.h" #include "memory.h" +#include "pages.h" #include "random.h" #include "util.h" static_assert(sizeof(void *) == 8, "64-bit only"); #define CACHELINE_SIZE 64 -#define PAGE_SHIFT 12 -#define PAGE_SIZE ((size_t)1 << PAGE_SHIFT) -#define PAGE_MASK ((size_t)(PAGE_SIZE - 1)) -#define PAGE_CEILING(s) (((s) + PAGE_MASK) & ~PAGE_MASK) - -#define ALIGNMENT_CEILING(s, alignment) (((s) + (alignment - 1)) & ((~(alignment)) + 1)) - -static void *allocate_pages(size_t usable_size, size_t guard_size, bool unprotect) { - size_t real_size; - if (unlikely(__builtin_add_overflow(usable_size, guard_size * 2, &real_size))) { - errno = ENOMEM; - return NULL; - } - void *real = memory_map(real_size); - if (real == NULL) { - return NULL; - } - void *usable = (char *)real + guard_size; - if (unprotect && memory_protect_rw(usable, usable_size)) { - memory_unmap(real, real_size); - return NULL; - } - return usable; -} - -static void deallocate_pages(void *usable, size_t usable_size, size_t guard_size) { - memory_unmap((char *)usable - guard_size, usable_size + guard_size * 2); -} - -static void *allocate_pages_aligned(size_t usable_size, size_t alignment, size_t guard_size) { - usable_size = PAGE_CEILING(usable_size); - if (unlikely(!usable_size)) { - errno = ENOMEM; - return NULL; - } - - size_t alloc_size; - if (unlikely(__builtin_add_overflow(usable_size, alignment - PAGE_SIZE, &alloc_size))) { - errno = ENOMEM; - return NULL; - } - - size_t real_alloc_size; - if (unlikely(__builtin_add_overflow(alloc_size, guard_size * 2, &real_alloc_size))) { - errno = ENOMEM; - return NULL; - } - - void *real = memory_map(real_alloc_size); - if (real == NULL) { - return NULL; - } - - void *usable = (char *)real + guard_size; - - size_t lead_size = ALIGNMENT_CEILING((uintptr_t)usable, alignment) - (uintptr_t)usable; - size_t trail_size = alloc_size - lead_size - usable_size; - void *base = (char *)usable + lead_size; - - if (memory_protect_rw(base, usable_size)) { - memory_unmap(real, real_alloc_size); - return NULL; - } - - if (lead_size) { - if (memory_unmap(real, lead_size)) { - memory_unmap(real, real_alloc_size); - return NULL; - } - } - - if (trail_size) { - if (memory_unmap((char *)base + usable_size + guard_size, trail_size)) { - memory_unmap(real, real_alloc_size); - return NULL; - } - } - - return base; -} static union { struct { diff --git a/pages.c b/pages.c new file mode 100644 index 0000000..e08c980 --- /dev/null +++ b/pages.c @@ -0,0 +1,83 @@ +#include +#include + +#include "memory.h" +#include "pages.h" +#include "util.h" + +#define ALIGNMENT_CEILING(s, alignment) (((s) + (alignment - 1)) & ((~(alignment)) + 1)) + +void *allocate_pages(size_t usable_size, size_t guard_size, bool unprotect) { + size_t real_size; + if (unlikely(__builtin_add_overflow(usable_size, guard_size * 2, &real_size))) { + errno = ENOMEM; + return NULL; + } + void *real = memory_map(real_size); + if (real == NULL) { + return NULL; + } + void *usable = (char *)real + guard_size; + if (unprotect && memory_protect_rw(usable, usable_size)) { + memory_unmap(real, real_size); + return NULL; + } + return usable; +} + +void deallocate_pages(void *usable, size_t usable_size, size_t guard_size) { + memory_unmap((char *)usable - guard_size, usable_size + guard_size * 2); +} + +void *allocate_pages_aligned(size_t usable_size, size_t alignment, size_t guard_size) { + usable_size = PAGE_CEILING(usable_size); + if (unlikely(!usable_size)) { + errno = ENOMEM; + return NULL; + } + + size_t alloc_size; + if (unlikely(__builtin_add_overflow(usable_size, alignment - PAGE_SIZE, &alloc_size))) { + errno = ENOMEM; + return NULL; + } + + size_t real_alloc_size; + if (unlikely(__builtin_add_overflow(alloc_size, guard_size * 2, &real_alloc_size))) { + errno = ENOMEM; + return NULL; + } + + void *real = memory_map(real_alloc_size); + if (real == NULL) { + return NULL; + } + + void *usable = (char *)real + guard_size; + + size_t lead_size = ALIGNMENT_CEILING((uintptr_t)usable, alignment) - (uintptr_t)usable; + size_t trail_size = alloc_size - lead_size - usable_size; + void *base = (char *)usable + lead_size; + + if (memory_protect_rw(base, usable_size)) { + memory_unmap(real, real_alloc_size); + return NULL; + } + + if (lead_size) { + if (memory_unmap(real, lead_size)) { + memory_unmap(real, real_alloc_size); + return NULL; + } + } + + if (trail_size) { + if (memory_unmap((char *)base + usable_size + guard_size, trail_size)) { + memory_unmap(real, real_alloc_size); + return NULL; + } + } + + return base; +} + diff --git a/pages.h b/pages.h new file mode 100644 index 0000000..7b49bec --- /dev/null +++ b/pages.h @@ -0,0 +1,16 @@ +#ifndef PAGES_H +#define PAGES_H + +#include +#include + +#define PAGE_SHIFT 12 +#define PAGE_SIZE ((size_t)1 << PAGE_SHIFT) +#define PAGE_MASK ((size_t)(PAGE_SIZE - 1)) +#define PAGE_CEILING(s) (((s) + PAGE_MASK) & ~PAGE_MASK) + +void *allocate_pages(size_t usable_size, size_t guard_size, bool unprotect); +void deallocate_pages(void *usable, size_t usable_size, size_t guard_size); +void *allocate_pages_aligned(size_t usable_size, size_t alignment, size_t guard_size); + +#endif