[Tarantool-patches] [PATCH 11/11][small] Add MAP_STACK to mmap() flags

sergeyb at tarantool.org sergeyb at tarantool.org
Tue May 12 15:32:35 MSK 2020


From: Sergey Bronnikov <sergeyb at tarantool.org>

OpenBSD enabled enforcing that the user stack pointer pointed to a
region of memory that had been mapped as a stack, see [1] for the
details. The upshot of this is that when usermode software wants to
allocate a stack, the region it's going to use must be mapped with the
MAP_STACK flag; if not, entry to the kernel will trap this and generate
a signal to kill the process.

Part of [2]

[1] https://undeadly.org/cgi?action=article;sid=20180310000858
[2] https://github.com/tarantool/tarantool/issues/4967
---
 small/features.c   | 4 ++++
 small/slab_arena.c | 7 +++++++
 2 files changed, 11 insertions(+)

diff --git a/small/features.c b/small/features.c
index 3adadae..bece95a 100644
--- a/small/features.c
+++ b/small/features.c
@@ -72,7 +72,11 @@ test_dontdump(void)
 	 * and work on it.
 	 */
 
+#if defined(__OpenBSD__)
+	ptr = mmap(NULL, size, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE | MAP_STACK, -1, 0);
+#else
 	ptr = mmap(NULL, size, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+#endif
 	if (ptr == MAP_FAILED) {
 		/*
 		 * We're out of memory, and cant guarantee anything.
diff --git a/small/slab_arena.c b/small/slab_arena.c
index 7661ef8..11b5364 100644
--- a/small/slab_arena.c
+++ b/small/slab_arena.c
@@ -95,6 +95,9 @@ mmap_checked(size_t size, size_t align, int flags)
 		flags = MAP_PRIVATE | MAP_ANONYMOUS;
 	else
 		flags = MAP_SHARED | MAP_ANONYMOUS;
+#if defined(__OpenBSD__)
+	flags |= MAP_STACK;
+#endif
 
 	/*
 	 * All mappings except the first are likely to
@@ -160,7 +163,11 @@ slab_arena_flags_init(struct slab_arena *arena, int flags)
 	 * map them to internal ones.
 	 */
 	if (!(flags & SLAB_ARENA_FLAG_MARK)) {
+#if defined(__OpenBSD__)
+		assert(flags & (MAP_PRIVATE | MAP_SHARED | MAP_STACK));
+#else
 		assert(flags & (MAP_PRIVATE | MAP_SHARED));
+#endif
 		if (flags == MAP_PRIVATE)
 			arena->flags = SLAB_ARENA_PRIVATE;
 		else
-- 
2.23.0



More information about the Tarantool-patches mailing list