Skip to contentSkip to conversation

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

PATCH Superseded Brad Smith 3 participants 12 messages Oct 7, 2018 – Oct 13, 2018

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Brad Smith

author
#0 lore raw

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

Use MAP_STACK in qemu_alloc_stack() on OpenBSD.

Added to -current and will be in our soon to be 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 +5 -1
index fbd0dc8c57..51e9a012c2 100644
@@ -611,7 +611,11 @@ void *qemu_alloc_stack(size_t *sz)
     *sz += pagesz;
 
     ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE,
-               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+               MAP_PRIVATE | MAP_ANONYMOUS
+#ifdef MAP_STACK
+               | MAP_STACK
+#endif
+               , -1, 0);
     if (ptr == MAP_FAILED) {
         perror("failed to allocate memory for stack");
         abort();
#1 lore raw
Quoted 28 lines · Brad Smith

Use MAP_STACK in qemu_alloc_stack() on OpenBSD.

Added to -current and will be in our soon to be 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 +0 -0
index fbd0dc8c57..51e9a012c2 100644
@@ -611,7 +611,11 @@ void *qemu_alloc_stack(size_t *sz)
     *sz += pagesz;
 

util/oslib-posix.c void *qemu_alloc_stack(size_t *sz) · line 621

Quoted 10 lines
util/oslib-posix.c +5 -1
     ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE,
-               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+               MAP_PRIVATE | MAP_ANONYMOUS
+#ifdef MAP_STACK
+               | MAP_STACK
+#endif
+               , -1, 0);
     if (ptr == MAP_FAILED) {
         perror("failed to allocate memory for stack");
         abort();

Can we handle it differently, storing MAP_* flags in a variable:

int flags = MAP_PRIVATE | MAP_ANONYMOUS;
#ifdef MAP_STACK
flags |= MAP_STACK;
#endif

ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE, flags, -1, 0);

This way it will look nicer as we won't ifdef the middle of a function call.

#2 lore raw
Quoted 42 lines · Kamil Rytarowski
Quoted 28 lines · Brad Smith

Use MAP_STACK in qemu_alloc_stack() on OpenBSD.

Added to -current and will be in our soon to be 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 +0 -0
index fbd0dc8c57..51e9a012c2 100644
@@ -611,7 +611,11 @@ void *qemu_alloc_stack(size_t *sz)
     *sz += pagesz;
 

util/oslib-posix.c void *qemu_alloc_stack(size_t *sz) · line 621

Quoted 10 lines
util/oslib-posix.c +5 -1
     ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE,
-               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+               MAP_PRIVATE | MAP_ANONYMOUS
+#ifdef MAP_STACK
+               | MAP_STACK
+#endif
+               , -1, 0);
     if (ptr == MAP_FAILED) {
         perror("failed to allocate memory for stack");
         abort();

Can we handle it differently, storing MAP_* flags in a variable:

Quoted 8 lines

int flags = MAP_PRIVATE | MAP_ANONYMOUS;
#ifdef MAP_STACK
flags |= MAP_STACK;
#endif

ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE, flags, -1, 0);

This way it will look nicer as we won't ifdef the middle of a function call.

The other nice way to handle that is to have osdep.h do #ifndef MAP_STACK
#define MAP_STACK 0
#endif
and then you can just unconditionally use MAP_STACK in your expression for the mmap flags.

I note that Linux also defines a MAP_STACK, about which the manpage says:
MAP_STACK (since Linux 2.6.27)
Allocate the mapping at an address suitable for a
process or thread stack.
This flag is currently a no-op, but is used in the
glibc threading imple‐
mentation so that if some architectures require special
treatment for stack
allocations, support can later be transparently
implemented for glibc.

So this patch would be opting Linux QEMU builds into whatever that potential future behaviour change is. That sounds I guess like it's more likely to be the right thing than the wrong thing, but it would be useful if some Linux expert could confirm...

thanks
-- PMM

#3 lore raw
Quoted 50 lines · Peter Maydell
Quoted 49 lines · Kamil Rytarowski
Quoted 28 lines · Brad Smith

Use MAP_STACK in qemu_alloc_stack() on OpenBSD.

Added to -current and will be in our soon to be 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 +0 -0
index fbd0dc8c57..51e9a012c2 100644
@@ -611,7 +611,11 @@ void *qemu_alloc_stack(size_t *sz)
     *sz += pagesz;
 

util/oslib-posix.c void *qemu_alloc_stack(size_t *sz) · line 621

Quoted 10 lines
util/oslib-posix.c +5 -1
     ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE,
-               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+               MAP_PRIVATE | MAP_ANONYMOUS
+#ifdef MAP_STACK
+               | MAP_STACK
+#endif
+               , -1, 0);
     if (ptr == MAP_FAILED) {
         perror("failed to allocate memory for stack");
         abort();

Can we handle it differently, storing MAP_* flags in a variable:

int flags = MAP_PRIVATE | MAP_ANONYMOUS;
#ifdef MAP_STACK
flags |= MAP_STACK;
#endif

ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE, flags, -1, 0);

Quoted 9 lines
Quoted 1 line

This way it will look nicer as we won't ifdef the middle of a function call.

The other nice way to handle that is to have osdep.h do #ifndef MAP_STACK
#define MAP_STACK 0
#endif
and then you can just unconditionally use MAP_STACK in your expression for the mmap flags.

I assume that this is a cleaner solution.

Quoted 13 lines

I note that Linux also defines a MAP_STACK, about which the manpage says:
MAP_STACK (since Linux 2.6.27)
Allocate the mapping at an address suitable for a
process or thread stack.
This flag is currently a no-op, but is used in the
glibc threading imple‐
mentation so that if some architectures require special
treatment for stack
allocations, support can later be transparently
implemented for glibc.

Quoted 8 lines

So this patch would be opting Linux QEMU builds into whatever that potential future behaviour change is. That sounds I guess like it's more likely to be the right thing than the wrong thing, but it would be useful if some Linux expert could confirm...

thanks
-- PMM

There is a similar description on NetBSD:

MAP_STACK

Allocate a memory segment that can be used either for a process or thread stack. This currently has no effect, but its use is reserved for architectures that might require special treatment of that address space. Unimplemented.

Apparently OpenBSD started to overload it for some hardening.

author
#4 lore raw
Quoted 84 lines · Kamil Rytarowski
Quoted 49 lines · Peter Maydell
Quoted 48 lines · Kamil Rytarowski
Quoted 28 lines · Brad Smith

Use MAP_STACK in qemu_alloc_stack() on OpenBSD.

Added to -current and will be in our soon to be 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 +0 -0
index fbd0dc8c57..51e9a012c2 100644
@@ -611,7 +611,11 @@ void *qemu_alloc_stack(size_t *sz)
      *sz += pagesz;
 

util/oslib-posix.c void *qemu_alloc_stack(size_t *sz) · line 621

Quoted 10 lines
util/oslib-posix.c +5 -1
      ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE,
-               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+               MAP_PRIVATE | MAP_ANONYMOUS
+#ifdef MAP_STACK
+               | MAP_STACK
+#endif
+               , -1, 0);
      if (ptr == MAP_FAILED) {
          perror("failed to allocate memory for stack");
          abort();

Can we handle it differently, storing MAP_* flags in a variable:

int flags = MAP_PRIVATE | MAP_ANONYMOUS;
#ifdef MAP_STACK
flags |= MAP_STACK;
#endif

ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE, flags, -1, 0);

Quoted 8 lines
Quoted 1 line

This way it will look nicer as we won't ifdef the middle of a function call.

The other nice way to handle that is to have osdep.h do #ifndef MAP_STACK
#define MAP_STACK 0
#endif
and then you can just unconditionally use MAP_STACK in your expression for the mmap flags.

I assume that this is a cleaner solution.

Quoted 12 lines

I note that Linux also defines a MAP_STACK, about which the manpage says:
MAP_STACK (since Linux 2.6.27)
Allocate the mapping at an address suitable for a
process or thread stack.
This flag is currently a no-op, but is used in the
glibc threading imple‐
mentation so that if some architectures require special
treatment for stack
allocations, support can later be transparently
implemented for glibc.

Quoted 8 lines

So this patch would be opting Linux QEMU builds into whatever that potential future behaviour change is. That sounds I guess like it's more likely to be the right thing than the wrong thing, but it would be useful if some Linux expert could confirm...

thanks
-- PMM

There is a similar description on NetBSD:

MAP_STACK

Quoted 8 lines

Allocate a memory segment that can be used either for a process or thread stack. This currently has no effect, but its use is reserved for architectures that might require special treatment of that address space. Unimplemented.

Apparently OpenBSD started to overload it for some hardening.

And from FreeBSD...

MAP_STACK MAP_STACK implies MAP_ANON, and /offset/ of 0. The/fd/
argument must be -1 and /prot/ must include at least
PROT_READ and PROT_WRITE.

This option creates a memory region that grows to at
most/len/ bytes in size, starting from the stack top
and growing down. The stack top is the starting
address returned by the call, plus/len/ bytes. The
bottom of the stack at maximum growth is the starting
address returned by the call.

Stacks created with MAP_STACK automatically grow. Guards prevent inadvertent use of the regions into which those stacks can grow without requiring mapping
the whole stack in advance.

#5 lore raw
Quoted 13 lines · Brad Smith

And from FreeBSD...

MAP_STACK MAP_STACK implies MAP_ANON, and offset of 0. The fd
argument must be -1 and prot must include at least PROT_READ and PROT_WRITE.

This option creates a memory region that grows to at most len bytes in size, starting from the stack top and growing down. The stack top is the starting address returned by the call, plus len bytes. The bottom of the stack at maximum growth is the starting address returned by the call.

Quoted 4 lines

Stacks created with MAP_STACK automatically grow. Guards prevent inadvertent use of the regions into which those stacks can grow without requiring mapping the whole stack in advance.

Hmm. That "automatically growing" part sounds like behaviour we definitely do not want for our use case. So we're going to need to make this OS-specific :-(

thanks
-- PMM

#6 lore raw
Quoted 14 lines · Peter Maydell
Quoted 13 lines · Brad Smith

And from FreeBSD...

MAP_STACK MAP_STACK implies MAP_ANON, and offset of 0. The fd
argument must be -1 and prot must include at least PROT_READ and PROT_WRITE.

This option creates a memory region that grows to at most len bytes in size, starting from the stack top and growing down. The stack top is the starting address returned by the call, plus len bytes. The bottom of the stack at maximum growth is the starting address returned by the call.

Quoted 9 lines
Quoted 4 lines

Stacks created with MAP_STACK automatically grow. Guards prevent inadvertent use of the regions into which those stacks can grow without requiring mapping the whole stack in advance.

Hmm. That "automatically growing" part sounds like behaviour we definitely do not want for our use case. So we're going to need to make this OS-specific :-(

I propose to restrict MAP_STACK it to OpenBSD (with a comment in the code). Once it will be needed by someone else will be able to enable it for other OSes.

Quoted 3 lines

thanks
-- PMM

author
#7 lore raw
Quoted 19 lines · Kamil Rytarowski
Quoted 18 lines · Peter Maydell
Quoted 17 lines · Brad Smith

And from FreeBSD...

MAP_STACK MAP_STACK implies MAP_ANON, and offset of 0. The fd
argument must be -1 and prot must include at least PROT_READ and PROT_WRITE.

This option creates a memory region that grows to at most len bytes in size, starting from the stack top and growing down. The stack top is the starting address returned by the call, plus len bytes. The bottom of the stack at maximum growth is the starting address returned by the call.

Stacks created with MAP_STACK automatically grow. Guards prevent inadvertent use of the regions into which those stacks can grow without requiring mapping the whole stack in advance.

Quoted 7 lines
Quoted 4 lines

Hmm. That "automatically growing" part sounds like behaviour we definitely do not want for our use case. So we're going to need to make this OS-specific :-(

I propose to restrict MAP_STACK it to OpenBSD (with a comment in the code). Once it will be needed by someone else will be able to enable it for other OSes.

I was going to propose doing something like that but you had replied before I did. What sort of comment did you have in mind?

#8 lore raw
Quoted 25 lines · Brad Smith
Quoted 23 lines · Kamil Rytarowski
Quoted 22 lines · Peter Maydell
Quoted 13 lines · Brad Smith

And from FreeBSD...

      MAP_STACK MAP_STACK implies MAP_ANON, and offset of 0.  The fd argument must be -1 and prot must include at least PROT_READ and PROT_WRITE.

This option creates a memory region that grows to at most len bytes in size, starting from the stack top and growing down.  The stack top is the starting address returned by the call, plus len bytes.  The bottom of the stack at maximum growth is the starting address returned by the call.

Quoted 4 lines

Stacks created with MAP_STACK automatically grow. Guards prevent inadvertent use of the regions into which those stacks can grow without requiring mapping the whole stack in advance.

Hmm. That "automatically growing" part sounds like behaviour we definitely do not want for our use case. So we're going to need to make this OS-specific :-(

Quoted 8 lines
Quoted 3 lines

I propose to restrict MAP_STACK it to OpenBSD (with a comment in the code). Once it will be needed by someone else will be able to enable it for other OSes.

I was going to propose doing something like that but you had replied before I did.
What sort of comment did you have in mind?

Why do we want it only on OpenBSD and its either unneeded or meaning something else on other OSes.

#ifdef __OpenBSD__
flags |= MAP_STACK;
#endif

author
#9 lore raw
Quoted 35 lines · Kamil Rytarowski
Quoted 25 lines · Brad Smith
Quoted 23 lines · Kamil Rytarowski
Quoted 22 lines · Peter Maydell
Quoted 13 lines · Brad Smith

And from FreeBSD...

?????????? MAP_STACK MAP_STACK implies MAP_ANON, and offset of 0.?? The fd argument must be -1 and prot must include at least
PROT_READ and PROT_WRITE.

This option creates a memory region that grows to at most len bytes in size, starting from the stack top and growing down.?? The stack top is the starting address returned by the call, plus len bytes.?? The bottom of the stack at maximum growth is the starting address returned by the call.

Quoted 4 lines

Stacks created with MAP_STACK automatically grow. Guards prevent inadvertent use of the regions into which those stacks can grow without requiring mapping the whole stack in advance.

Hmm. That "automatically growing" part sounds like behaviour we definitely do not want for our use case. So we're going to need to make this OS-specific :-(

Quoted 8 lines
Quoted 3 lines

I propose to restrict MAP_STACK it to OpenBSD (with a comment in the code). Once it will be needed by someone else will be able to enable it for other OSes.

I was going to propose doing something like that but you had replied before I did.
What sort of comment did you have in mind?

Quoted 6 lines

Why do we want it only on OpenBSD and its either unneeded or meaning something else on other OSes.

#ifdef __OpenBSD__
flags |= MAP_STACK;
#endif

How about the following?

util/oslib-posix.c +10 -2
index fbd0dc8c57..7814e61114 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,15 @@ 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. */
+    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();
Reviewed-by
#10 lore raw
Quoted 36 lines · Brad Smith
Quoted 35 lines · Kamil Rytarowski
Quoted 33 lines · Brad Smith
Quoted 19 lines · Kamil Rytarowski
Quoted 18 lines · Peter Maydell
Quoted 17 lines · Brad Smith

And from FreeBSD...

?????????? MAP_STACK MAP_STACK implies MAP_ANON, and offset of 0.?? The fd argument must be -1 and prot must include at least
PROT_READ and PROT_WRITE.

This option creates a memory region that grows to at most len bytes in size, starting from the stack top and growing down.?? The stack top is the starting address returned by the call, plus len bytes.?? The bottom of the stack at maximum growth is the starting address returned by the call.

Stacks created with MAP_STACK automatically grow. Guards prevent inadvertent use of the regions into which those stacks can grow without requiring mapping the whole stack in advance.

Quoted 7 lines
Quoted 4 lines

Hmm. That "automatically growing" part sounds like behaviour we definitely do not want for our use case. So we're going to need to make this OS-specific :-(

I propose to restrict MAP_STACK it to OpenBSD (with a comment in the code). Once it will be needed by someone else will be able to enable it for other OSes.

I was going to propose doing something like that but you had replied before I did.
What sort of comment did you have in mind?

Quoted 9 lines
Quoted 6 lines

Why do we want it only on OpenBSD and its either unneeded or meaning something else on other OSes.

#ifdef __OpenBSD__
flags |= MAP_STACK;
#endif

How about the following?

It looks good to me.

Quoted 32 lines
util/oslib-posix.c +10 -2
index fbd0dc8c57..7814e61114 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,15 @@ 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. */
+    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
#11 lore raw
In reply to Kamil Rytarowski
Quoted 42 lines · Kamil Rytarowski
Quoted 28 lines · Brad Smith

Use MAP_STACK in qemu_alloc_stack() on OpenBSD.

Added to -current and will be in our soon to be 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 +0 -0
index fbd0dc8c57..51e9a012c2 100644
@@ -611,7 +611,11 @@ void *qemu_alloc_stack(size_t *sz)
     *sz += pagesz;
 

util/oslib-posix.c void *qemu_alloc_stack(size_t *sz) · line 621

Quoted 10 lines
util/oslib-posix.c +5 -1
     ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE,
-               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+               MAP_PRIVATE | MAP_ANONYMOUS
+#ifdef MAP_STACK
+               | MAP_STACK
+#endif
+               , -1, 0);
     if (ptr == MAP_FAILED) {
         perror("failed to allocate memory for stack");
         abort();

Can we handle it differently, storing MAP_* flags in a variable:

Quoted 8 lines

int flags = MAP_PRIVATE | MAP_ANONYMOUS;
#ifdef MAP_STACK
flags |= MAP_STACK;
#endif

ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE, flags, -1, 0);

This way it will look nicer as we won't ifdef the middle of a function call.

How about the following?

util/oslib-posix.c +7 -2
index fbd0dc8c57..f8ee349c9c 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,12 @@ 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;
+#ifdef 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();