From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp51.i.mail.ru (smtp51.i.mail.ru [94.100.177.111]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 07B9645BE2E for ; Tue, 12 May 2020 15:34:35 +0300 (MSK) From: sergeyb@tarantool.org Date: Tue, 12 May 2020 15:32:35 +0300 Message-Id: <3c3f944e5982510f3ae32435606631cc55621408.1589285302.git.sergeyb@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH 11/11][small] Add MAP_STACK to mmap() flags List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tarantool-patches@dev.tarantool.org Cc: o.piskunov@tarantool.org From: Sergey Bronnikov 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