From patchwork Tue Aug 16 20:33:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 597511 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:b345:0:0:0:0 with SMTP id w5csp2697832maz; Tue, 16 Aug 2022 13:43:29 -0700 (PDT) X-Google-Smtp-Source: AA6agR4tJxcw12h1hxDSw8FgxovJTlJ3QLyEHM47JpfEaL9mjPU1/DN/sQgDYkZxr8DZHMhavEt3 X-Received: by 2002:ad4:596e:0:b0:47a:2ae5:b65e with SMTP id eq14-20020ad4596e000000b0047a2ae5b65emr19119349qvb.109.1660682609481; Tue, 16 Aug 2022 13:43:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660682609; cv=none; d=google.com; s=arc-20160816; b=OzI0XJpzMJonzDA455Ih6Arze4KenmLwZhn5C8A1gIMdG4E08fbgKrgXjrMiJE32gg UEwT4ZnvRGgucOe+l6jSRMR5rUCiU91Rr68/dTs7KAN+YWg5k9W+MIx4NAYtlT7LYK2x grWC2Wy7V/Ob1di3EjZsety0hnej/Ulq/ffWSME2tJKCD9+R4jRqj1Kgr2itWUufsC4+ DV5baLT74rpyqY6MLC6D7ReC3j5Iuk6p118a3xHHHSukHIOTAPnA29x+2EFCcNaCISS/ 5YqUQxL/gvFA9wcHHtPe4Bjd2oCI9qg+t3FtaIgVHYe8JjDsW8lMCt5HG93ZXxUO3JEs FftQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=WyS1AkJjXgcx++s5EL+CHkdS52eWUfbVT52gpN5EMKs=; b=TjDpDU3BJl8Wf1osARMhzDSmWPl9Vp8NVz3IzNRK2iZNBJ6Rx+t3fhpcPNDhQZHvnl wEMOsCj+CCdCdOMwv+i3hpU2gcu3dDOBEC+8vHlNdBRiVtUcF/0FHlz2pv87U65vaye4 yVQOWYEyxlG5eXVpgrtdWkVdbGT8fNkqEY3GiERn90eboSKIm1fNbNdtxAHkKx/g1Tp3 0yXcfKk9Z/NZTpTRPbNPrpHrXPn3WrDra48xhnbjDUF/yEZFk/WMS1GlCdjHUIw9nF9a RAYIrjltFF/BdqrLM1d5AsKtqjkP9hFXj0MhUE84q+AA5ppC3qtbsauScUYHk09CeJ+C V4YA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Kqw28FrA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id r26-20020ae9d61a000000b006bb46869eeasi2618470qkk.699.2022.08.16.13.43.29 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Aug 2022 13:43:29 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Kqw28FrA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:38226 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oO3PM-0000VG-Ul for patch@linaro.org; Tue, 16 Aug 2022 16:43:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39456) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oO3GW-0004iv-3m for qemu-devel@nongnu.org; Tue, 16 Aug 2022 16:34:21 -0400 Received: from mail-oa1-x35.google.com ([2001:4860:4864:20::35]:45728) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oO3GS-0004UY-37 for qemu-devel@nongnu.org; Tue, 16 Aug 2022 16:34:19 -0400 Received: by mail-oa1-x35.google.com with SMTP id 586e51a60fabf-11ba6e79dd1so7806072fac.12 for ; Tue, 16 Aug 2022 13:34:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=WyS1AkJjXgcx++s5EL+CHkdS52eWUfbVT52gpN5EMKs=; b=Kqw28FrA0Ip0ok8/Z7eWT12WmrIOGLZ5XZmdEpFDDAOcAadGlugvqRahSfeIgGVMwk h34e2u0lkHX6DdBb/bYepYbsyxcSlYEgtQh7g/MC8q9OJsTnuD2QW0dkgvM0B6DO85Pp ziGgWUeWx/tldYbEKDgz9WjL7F+3IIRVOfP+k32GBsxeQ+9gRFkjE4pNOypVbieF6Etm oAM2XdGc4HLeRn2hJJO9DRRhNkTT347ZaG9sNebJjaGsn2cDKo0ms/CsOwnvrE3HEQ3Z tw7u/cQW5K6efJyoOv9abF+P1/6UjMRP0jf75RIhkMxixuILMhYfmElg2lc4sIFbW5bW +Ntw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=WyS1AkJjXgcx++s5EL+CHkdS52eWUfbVT52gpN5EMKs=; b=8A4x/alJb9nA0vRa4cKPwWOe+UY5RaLtkCBzOIAX18QFfWZTB/Jnj70uCIQL/muKOT yW05MA3ClAy8paRxEqDM+GAv+hMpm3b+bN99Lb5oT2yzvJoPbetv4sqB4OwRNT7IuygN y2LQc3eTfmAuAzN2zVK8KU0U7FRZWR5eGexpwaKrRh8/hDl/8Nn9drZN6sm+5ydmpz3z IYAj58zsLNrSZVr1nIsX6iBegZCThhQzPbi7JLm4gaNt0CumKt/KiC0NsE9fUpMNn7AY vXxs36swZVBSoYyC48XusjgVYFCJBvhOQ/Lh0rj05Yyw214y8wdH4l1Q/0AccAVMo+A1 yLNg== X-Gm-Message-State: ACgBeo2BDJAunr0E6+HvEgRgaeVgzwS+8lgwQiOYVx6KGX1RPgE0vjo4 vGFrkWFyjaSSmSCqnIlBVMN8zkXIUN/V7A== X-Received: by 2002:a05:6870:e2ca:b0:10e:6f87:baa with SMTP id w10-20020a056870e2ca00b0010e6f870baamr164238oad.58.1660682054340; Tue, 16 Aug 2022 13:34:14 -0700 (PDT) Received: from stoup.. ([2605:ef80:80f6:61fa:9bc2:5095:d612:5e22]) by smtp.gmail.com with ESMTPSA id y23-20020a056870b01700b0010ef8ccde67sm2285220oae.13.2022.08.16.13.34.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Aug 2022 13:34:13 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, alex.bennee@linaro.org, iii@linux.ibm.com Subject: [PATCH v2 04/33] linux-user: Honor PT_GNU_STACK Date: Tue, 16 Aug 2022 15:33:31 -0500 Message-Id: <20220816203400.161187-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220816203400.161187-1-richard.henderson@linaro.org> References: <20220816203400.161187-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2001:4860:4864:20::35; envelope-from=richard.henderson@linaro.org; helo=mail-oa1-x35.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Map the stack executable if required by default or on demand. Signed-off-by: Richard Henderson --- include/elf.h | 1 + linux-user/qemu.h | 1 + linux-user/elfload.c | 19 ++++++++++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/include/elf.h b/include/elf.h index 3a4bcb646a..3d6b9062c0 100644 --- a/include/elf.h +++ b/include/elf.h @@ -31,6 +31,7 @@ typedef int64_t Elf64_Sxword; #define PT_LOPROC 0x70000000 #define PT_HIPROC 0x7fffffff +#define PT_GNU_STACK (PT_LOOS + 0x474e551) #define PT_GNU_PROPERTY (PT_LOOS + 0x474e553) #define PT_MIPS_REGINFO 0x70000000 diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 7d90de1b15..e2e93fbd1d 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -48,6 +48,7 @@ struct image_info { uint32_t elf_flags; int personality; abi_ulong alignment; + bool exec_stack; /* Generic semihosting knows about these pointers. */ abi_ulong arg_strings; /* strings for argv */ diff --git a/linux-user/elfload.c b/linux-user/elfload.c index d783240a36..050cd1fa08 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -233,6 +233,7 @@ static bool init_guest_commpage(void) #define ELF_ARCH EM_386 #define ELF_PLATFORM get_elf_platform() +#define EXSTACK_DEFAULT true static const char *get_elf_platform(void) { @@ -309,6 +310,7 @@ static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *en #define ELF_ARCH EM_ARM #define ELF_CLASS ELFCLASS32 +#define EXSTACK_DEFAULT true static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) @@ -777,6 +779,7 @@ static inline void init_thread(struct target_pt_regs *regs, #else #define ELF_CLASS ELFCLASS32 +#define EXSTACK_DEFAULT true #endif @@ -974,6 +977,7 @@ static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *en #define ELF_CLASS ELFCLASS64 #define ELF_ARCH EM_LOONGARCH +#define EXSTACK_DEFAULT true #define elf_check_arch(x) ((x) == EM_LOONGARCH) @@ -1069,6 +1073,7 @@ static uint32_t get_elf_hwcap(void) #define ELF_CLASS ELFCLASS32 #endif #define ELF_ARCH EM_MIPS +#define EXSTACK_DEFAULT true #ifdef TARGET_ABI_MIPSN32 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2) @@ -1807,6 +1812,10 @@ static inline void init_thread(struct target_pt_regs *regs, #define bswaptls(ptr) bswap32s(ptr) #endif +#ifndef EXSTACK_DEFAULT +#define EXSTACK_DEFAULT false +#endif + #include "elf.h" /* We must delay the following stanzas until after "elf.h". */ @@ -2082,6 +2091,7 @@ static abi_ulong setup_arg_pages(struct linux_binprm *bprm, struct image_info *info) { abi_ulong size, error, guard; + int prot; size = guest_stack_size; if (size < STACK_LOWER_LIMIT) { @@ -2092,7 +2102,11 @@ static abi_ulong setup_arg_pages(struct linux_binprm *bprm, guard = qemu_real_host_page_size(); } - error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE, + prot = PROT_READ | PROT_WRITE; + if (info->exec_stack) { + prot |= PROT_EXEC; + } + error = target_mmap(0, size + guard, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (error == -1) { perror("mmap stack"); @@ -2920,6 +2934,7 @@ static void load_elf_image(const char *image_name, int image_fd, */ loaddr = -1, hiaddr = 0; info->alignment = 0; + info->exec_stack = EXSTACK_DEFAULT; for (i = 0; i < ehdr->e_phnum; ++i) { struct elf_phdr *eppnt = phdr + i; if (eppnt->p_type == PT_LOAD) { @@ -2962,6 +2977,8 @@ static void load_elf_image(const char *image_name, int image_fd, if (!parse_elf_properties(image_fd, info, eppnt, bprm_buf, &err)) { goto exit_errmsg; } + } else if (eppnt->p_type == PT_GNU_STACK) { + info->exec_stack = eppnt->p_flags & PF_X; } }