From patchwork Fri Jun 17 14:11:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 9184217 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9BEE86075D for ; Fri, 17 Jun 2016 14:54:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9B3C727248 for ; Fri, 17 Jun 2016 14:54:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9007A2839F; Fri, 17 Jun 2016 14:54:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 0F76C27248 for ; Fri, 17 Jun 2016 14:54:19 +0000 (UTC) Received: from localhost ([::1]:58125 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDvA2-0001OT-3a for patchwork-qemu-devel@patchwork.kernel.org; Fri, 17 Jun 2016 10:54:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45946) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDuUY-0007nh-Ap for qemu-devel@nongnu.org; Fri, 17 Jun 2016 10:11:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bDuUV-0007WC-OX for qemu-devel@nongnu.org; Fri, 17 Jun 2016 10:11:25 -0400 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:57874) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDuUV-0007Vx-Cn for qemu-devel@nongnu.org; Fri, 17 Jun 2016 10:11:23 -0400 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.84_2) (envelope-from ) id 1bDuUR-00085R-Js; Fri, 17 Jun 2016 15:11:19 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Date: Fri, 17 Jun 2016 15:11:19 +0100 Message-Id: <1466172679-10156-1-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.9.1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PATCH] oslib-posix: New qemu_alloc_stack() to allocate stack with correct perms X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: patches@linaro.org, Michael Tokarev , Paolo Bonzini , Leon Alrae , Aurelien Jarno , Richard Henderson Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Some architectures require the stack to be executable; notably this includes MIPS, because the kernel's floating point emulator may try to put trampoline code on the stack to handle some cases. (See https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=815409 for an example of this causing QEMU to crash.) Create a utility function qemu_alloc_stack() which allocates a block of memory for use as a stack with the correct permissions. Since we would prefer to make the stack non-executable if we can as a defence against code execution exploits, we detect whether the existing stack is mapped executable. Unfortunately this requires us to grovel through /proc/self/maps to determine the permissions on it. Signed-off-by: Peter Maydell --- This method of figuring out the correct perms for the stack is not exactly pretty; better suggestions welcome. NB that this utility function also gives us a handy place to put code for allocating a guard page at the bottom of the stack, or mapping it as MAP_GROWSDOWN, or whatever. --- include/sysemu/os-posix.h | 12 ++++++++ util/coroutine-sigaltstack.c | 2 +- util/coroutine-ucontext.c | 2 +- util/oslib-posix.c | 70 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 2 deletions(-) diff --git a/include/sysemu/os-posix.h b/include/sysemu/os-posix.h index 9c7dfdf..1dc9d3c 100644 --- a/include/sysemu/os-posix.h +++ b/include/sysemu/os-posix.h @@ -60,4 +60,16 @@ int qemu_utimens(const char *path, const qemu_timespec *times); bool is_daemonized(void); +/** + * qemu_alloc_stack: + * @sz: size of required stack in bytes + * + * Allocate memory that can be used as a stack, for instance for + * coroutines. If the memory cannot be allocated, this function + * will abort (like g_malloc()). + * + * Returns: pointer to (the lowest address of) the stack memory. + */ +void *qemu_alloc_stack(size_t sz); + #endif diff --git a/util/coroutine-sigaltstack.c b/util/coroutine-sigaltstack.c index a7c3366..209f6e5 100644 --- a/util/coroutine-sigaltstack.c +++ b/util/coroutine-sigaltstack.c @@ -164,7 +164,7 @@ Coroutine *qemu_coroutine_new(void) */ co = g_malloc0(sizeof(*co)); - co->stack = g_malloc(stack_size); + co->stack = qemu_alloc_stack(stack_size); co->base.entry_arg = &old_env; /* stash away our jmp_buf */ coTS = coroutine_get_thread_state(); diff --git a/util/coroutine-ucontext.c b/util/coroutine-ucontext.c index 2bb7e10..a455519 100644 --- a/util/coroutine-ucontext.c +++ b/util/coroutine-ucontext.c @@ -101,7 +101,7 @@ Coroutine *qemu_coroutine_new(void) } co = g_malloc0(sizeof(*co)); - co->stack = g_malloc(stack_size); + co->stack = qemu_alloc_stack(stack_size); co->base.entry_arg = &old_env; /* stash away our jmp_buf */ uc.uc_link = &old_uc; diff --git a/util/oslib-posix.c b/util/oslib-posix.c index e2e1d4d..311093e 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -497,3 +497,73 @@ pid_t qemu_fork(Error **errp) } return pid; } + +#if defined(__linux__) +static int stack_prot(void) +{ + static int prot; + gchar *maps, *start, *end; + + if (prot) { + return prot; + } + + /* Some architectures (notably MIPS) require an executable stack, but + * we would prefer to avoid making the stack executable unnecessarily, + * to defend against code execution exploits. + * Check whether the current stack is executable, and follow its lead. + * Unfortunately to do this we have to wade through /proc/self/maps + * looking for the stack memory. We default to assuming we need an + * executable stack and remove the permission only if we can successfully + * confirm that non-executable is OK. + */ + + prot = PROT_READ | PROT_WRITE | PROT_EXEC; + + if (!g_file_get_contents("/proc/self/maps", &maps, NULL, NULL)) { + return prot; + } + + /* We are looking for a line like this: + * 7fffbe217000-7fffbe238000 rw-p 00000000 00:00 0 [stack] + * and checking whether it says 'rw-' or 'rwx'. + * We look forwards for [stack], then back to the preceding newline, + * then forwards for the rw- between the two. + */ + end = g_strstr_len(maps, -1, "[stack]"); + if (!end) { + return prot; + } + start = g_strrstr_len(maps, end - maps, "\n"); + if (!start) { + start = maps; + } + if (g_strstr_len(start, end - start, "rw-")) { + prot &= ~PROT_EXEC; + } + + return prot; +} + +#else +static int stack_prot(void) +{ + /* Assume an executable stack is needed, since we can't detect it. */ + return PROT_READ | PROT_WRITE | PROT_EXEC; +} +#endif + +void *qemu_alloc_stack(size_t sz) +{ + /* Allocate memory for the coroutine's stack. + * Note that some architectures require this to be executable. + */ + int prot = stack_prot(); + void *stack = mmap(NULL, sz, prot, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + + if (stack == MAP_FAILED) { + g_error("%s: failed to allocate %zu bytes", __func__, sz); + } + + return stack; +}