Skip to contentSkip to conversation

oslib-posix: Use MAP_STACK in qemu_alloc_stack() on OpenBSD

PATCH In review Brad Smith 2 participants 3 messages Oct 19, 2018 – Nov 6, 2018

lore
Share & tools
lore

Brad Smith

author
#0 lore raw

PATCH v3 1/1 oslib-posix: Use MAP_STACK in qemu_alloc_stack() on OpenBSD

Use MAP_STACK in qemu_alloc_stack() on OpenBSD.

Added to our 6.4 release.

MAP_STACK Indicate that the mapping is used as a stack. This
flag must be used in combination with MAP_ANON and MAP_PRIVATE.

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and syscall) confirm the stack register points at MAP_STACK memory, otherwise SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified to create a MAP_STACK sub-region which satisfies alignment requirements. Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the contents of the region -- there is no mprotect() equivalent operation, so there is no MAP_STACK-adding gadget.

util/oslib-posix.c +13 -2
index fbd0dc8c57..c1bee2a581 100644
@@ -596,6 +596,7 @@ pid_t qemu_fork(Error **errp)
 void *qemu_alloc_stack(size_t *sz)
 {
     void *ptr, *guardpage;
+    int flags;
 #ifdef CONFIG_DEBUG_STACK_USAGE
     void *ptr2;
 #endif
@@ -610,8 +611,18 @@ void *qemu_alloc_stack(size_t *sz)
     /* allocate one extra page for the guard page */
     *sz += pagesz;
 
-    ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE,
-               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+    flags = MAP_PRIVATE | MAP_ANONYMOUS;
+#if defined(MAP_STACK) && defined(__OpenBSD__)
+    /* Only enable MAP_STACK on OpenBSD. Other OS's such as
+     * Linux/FreeBSD/NetBSD have a flag with the same name
+     * but have differing functionality. OpenBSD will SEGV
+     * if it spots execution with a stack pointer pointing
+     * at memory that was not allocated with MAP_STACK.
+     */
+    flags |= MAP_STACK;
+#endif
+
+    ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE, flags, -1, 0);
     if (ptr == MAP_FAILED) {
         perror("failed to allocate memory for stack");
         abort();
author
#1 lore raw

ping.

Quoted 55 lines · Brad Smith

Use MAP_STACK in qemu_alloc_stack() on OpenBSD.

Added to our 6.4 release.

MAP_STACK Indicate that the mapping is used as a stack. This
flag must be used in combination with MAP_ANON and MAP_PRIVATE.

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and syscall) confirm the stack register points at MAP_STACK memory, otherwise SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified to create a MAP_STACK sub-region which satisfies alignment requirements. Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the contents of the region -- there is no mprotect() equivalent operation, so there is no MAP_STACK-adding gadget.

util/oslib-posix.c +13 -2
index fbd0dc8c57..c1bee2a581 100644
@@ -596,6 +596,7 @@ pid_t qemu_fork(Error **errp)
  void *qemu_alloc_stack(size_t *sz)
  {
      void *ptr, *guardpage;
+    int flags;
  #ifdef CONFIG_DEBUG_STACK_USAGE
      void *ptr2;
  #endif
@@ -610,8 +611,18 @@ void *qemu_alloc_stack(size_t *sz)
      /* allocate one extra page for the guard page */
      *sz += pagesz;
 
-    ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE,
-               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+    flags = MAP_PRIVATE | MAP_ANONYMOUS;
+#if defined(MAP_STACK) && defined(__OpenBSD__)
+    /* Only enable MAP_STACK on OpenBSD. Other OS's such as
+     * Linux/FreeBSD/NetBSD have a flag with the same name
+     * but have differing functionality. OpenBSD will SEGV
+     * if it spots execution with a stack pointer pointing
+     * at memory that was not allocated with MAP_STACK.
+     */
+    flags |= MAP_STACK;
+#endif
+
+    ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE, flags, -1, 0);
      if (ptr == MAP_FAILED) {
          perror("failed to allocate memory for stack");
          abort();
#2 lore raw
Quoted 2 lines · Brad Smith

ping.

Thanks for the ping, applied to master for rc0.

-- PMM