From: Otto Moerbeek Date: Fri, 4 Jan 2019 12:09:08 +0000 (+0100) Subject: Use mmap for stack allocation, adding the MAP_STACK flag on OpenBSD. X-Git-Tag: rec-4.2.0-alpha1~62^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=15880e10ff1cd1fc4420fd686e4b310a36c7fb98;p=thirdparty%2Fpdns.git Use mmap for stack allocation, adding the MAP_STACK flag on OpenBSD. On a userland to kernel transition OpenBSD checks if the stack pointer points to a piece of mem marked MAP_STACK to prevent a large class of exploits. Since we setup out own stack we must use mmap on OpenBSD, and we might do that on other systems as well. --- diff --git a/pdns/lazy_allocator.hh b/pdns/lazy_allocator.hh index 1b9308851a..629981c2cd 100644 --- a/pdns/lazy_allocator.hh +++ b/pdns/lazy_allocator.hh @@ -25,6 +25,13 @@ #include #include #include +#include +#include + +// On OpenBSD mem used as stack should be marked MAP_STACK +#if !defined(MAP_STACK) +#define MAP_STACK 0 +#endif template struct lazy_allocator { @@ -36,17 +43,16 @@ struct lazy_allocator { pointer allocate (size_type const n) { - return static_cast(::operator new (n * sizeof(value_type))); + void *p = mmap(NULL, n * sizeof(value_type), + PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_STACK, -1, 0); + if (p == MAP_FAILED) + throw std::bad_alloc(); + return static_cast(p); } void deallocate (pointer const ptr, size_type const n) noexcept { -#if defined(__cpp_sized_deallocation) && (__cpp_sized_deallocation >= 201309) - ::operator delete (ptr, n * sizeof(value_type)); -#else - (void) n; - ::operator delete (ptr); -#endif + munmap(ptr, n * sizeof(value_type)); } void construct (T*) const noexcept {}