split out code for managing page spans
This commit is contained in:
parent
e93d039214
commit
e4648192c0
7
Makefile
7
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)
|
||||
|
81
malloc.c
81
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 {
|
||||
|
83
pages.c
Normal file
83
pages.c
Normal file
@ -0,0 +1,83 @@
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
16
pages.h
Normal file
16
pages.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef PAGES_H
|
||||
#define PAGES_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#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
|
Loading…
Reference in New Issue
Block a user