From patchwork Thu Oct 10 18:44:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Martin X-Patchwork-Id: 175819 Delivered-To: patch@linaro.org Received: by 2002:a92:7e96:0:0:0:0:0 with SMTP id q22csp2683344ill; Thu, 10 Oct 2019 11:45:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqxZnrPR4F0G0kAqp8PeWC+xX/45SdWr6gShtgw0oZM0EsZTq7vVctZ8lkjxwoMhHx3cOoUJ X-Received: by 2002:a05:6402:1b92:: with SMTP id cc18mr9781633edb.129.1570733151661; Thu, 10 Oct 2019 11:45:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570733151; cv=none; d=google.com; s=arc-20160816; b=JKVR9fVaB5AKb6vfmAj0WmGTznNjl1BzCl4tGQdooubVEn2HBQOum7LPThogKu+TvK +ZIz4RdbNkHeAIyeOHKszSxeRNjmtqqwZqCblrDy6znfWPeq2PryFH0OVdO+V9j5AMhq nsXm/1xleACX16yomtTOc36Awba80RfbKrqMiI9BtjEPzrRfanUovUp/QeRuCvw6XjUN hS6h7rx4q1y7ycZaogiv68CV9SCVLxrfKvd9O/obl3OjFiwaKYrIvCvahw1QEsLerR4J 3XrU2RWbX7WTvyZ0IH/aAb3C3xbrLw2PkCIRt0vjLSgLiB6UGAzZ3Q2RKWVlB2xSOpA+ t98Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=Edd1ZGDk5v8u72Eeu+hFLEY+6W8whqinZhoKyu+gNdk=; b=Ywt3YcQnB7W65qhzeWCv+hgmrxRAWbK3dBWJiOXI0baOMMNIGySPTwmOtYnBCA2dfU NID6Wmtgwa/msY8xQkF5aFTOWZxO7nzFcZ8bxZNo9kwtMNsiRNHeC41Bl8DKMjw3eJw0 gpZTrJgtCEJrKmR/6l/eAnW6Hn+L2/5KOobIkDFazTinWLF0nneDsn5G0e6kxSk6d10Y JiTlEiY9Bx/PUCE3xnoVeGxf3wINxXNp8bsptBOMnTVUCjOGPZ0h8WXc0Qq9dAwrzJ/S 1avLL64VxQOluPslKpvv40VDfXgnyJmxLd7iSHRDC4Dq0q+g2GL05p+VYBzbvxDQFpXW 4/Lg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d25si3473579edv.319.2019.10.10.11.45.51; Thu, 10 Oct 2019 11:45:51 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727234AbfJJSpu (ORCPT + 22 others); Thu, 10 Oct 2019 14:45:50 -0400 Received: from foss.arm.com ([217.140.110.172]:38432 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726424AbfJJSps (ORCPT ); Thu, 10 Oct 2019 14:45:48 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 5D1AC1570; Thu, 10 Oct 2019 11:45:47 -0700 (PDT) Received: from e103592.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id AD7263F703; Thu, 10 Oct 2019 11:45:44 -0700 (PDT) From: Dave Martin To: linux-kernel@vger.kernel.org Cc: Andrew Jones , Arnd Bergmann , Catalin Marinas , Eugene Syromiatnikov , Florian Weimer , "H.J. Lu" , Jann Horn , Kees Cook , =?utf-8?q?Kristina_Mart=C5=A1enko?= , Mark Brown , Paul Elliott , Peter Zijlstra , Richard Henderson , Sudakshina Das , Szabolcs Nagy , Thomas Gleixner , Will Deacon , Yu-cheng Yu , Amit Kachhap , Vincenzo Frascino , linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v2 06/12] elf: Allow arch to tweak initial mmap prot flags Date: Thu, 10 Oct 2019 19:44:34 +0100 Message-Id: <1570733080-21015-7-git-send-email-Dave.Martin@arm.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1570733080-21015-1-git-send-email-Dave.Martin@arm.com> References: <1570733080-21015-1-git-send-email-Dave.Martin@arm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org An arch may want to tweak the mmap prot flags for an ELFexecutable's initial mappings. For example, arm64 is going to need to add PROT_BTI for executable pages in an ELF process whose executable is marked as using Branch Target Identification (an ARMv8.5-A control flow integrity feature). So that this can be done in a generic way, add a hook arch_elf_adjust_prot() to modify the prot flags as desired: arches can select CONFIG_HAVE_ELF_PROT and implement their own backend where necessary. By default, leave the prot flags unchanged. Signed-off-by: Dave Martin --- fs/Kconfig.binfmt | 3 +++ fs/binfmt_elf.c | 18 ++++++++++++------ include/linux/elf.h | 12 ++++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) -- 2.1.4 diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt index d2cfe07..2358368 100644 --- a/fs/Kconfig.binfmt +++ b/fs/Kconfig.binfmt @@ -36,6 +36,9 @@ config COMPAT_BINFMT_ELF config ARCH_BINFMT_ELF_STATE bool +config ARCH_HAVE_ELF_PROT + bool + config ARCH_USE_GNU_PROPERTY bool diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index ae345f6..dbfab2e 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -531,7 +531,8 @@ static inline int arch_check_elf(struct elfhdr *ehdr, bool has_interp, #endif /* !CONFIG_ARCH_BINFMT_ELF_STATE */ -static inline int make_prot(u32 p_flags) +static inline int make_prot(u32 p_flags, struct arch_elf_state *arch_state, + bool has_interp, bool is_interp) { int prot = 0; @@ -541,7 +542,8 @@ static inline int make_prot(u32 p_flags) prot |= PROT_WRITE; if (p_flags & PF_X) prot |= PROT_EXEC; - return prot; + + return arch_elf_adjust_prot(prot, arch_state, has_interp, is_interp); } /* This is much more generalized than the library routine read function, @@ -551,7 +553,8 @@ static inline int make_prot(u32 p_flags) static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex, struct file *interpreter, unsigned long *interp_map_addr, - unsigned long no_base, struct elf_phdr *interp_elf_phdata) + unsigned long no_base, struct elf_phdr *interp_elf_phdata, + struct arch_elf_state *arch_state) { struct elf_phdr *eppnt; unsigned long load_addr = 0; @@ -583,7 +586,8 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex, for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) { if (eppnt->p_type == PT_LOAD) { int elf_type = MAP_PRIVATE | MAP_DENYWRITE; - int elf_prot = make_prot(eppnt->p_flags); + int elf_prot = make_prot(eppnt->p_flags, arch_state, + true, true); unsigned long vaddr = 0; unsigned long k, map_addr; @@ -1040,7 +1044,8 @@ static int load_elf_binary(struct linux_binprm *bprm) } } - elf_prot = make_prot(elf_ppnt->p_flags); + elf_prot = make_prot(elf_ppnt->p_flags, &arch_state, + !!interpreter, false); elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE; @@ -1186,7 +1191,8 @@ static int load_elf_binary(struct linux_binprm *bprm) elf_entry = load_elf_interp(&loc->interp_elf_ex, interpreter, &interp_map_addr, - load_bias, interp_elf_phdata); + load_bias, interp_elf_phdata, + &arch_state); if (!IS_ERR((void *)elf_entry)) { /* * load_elf_interp() returns relocation diff --git a/include/linux/elf.h b/include/linux/elf.h index 7bdc6da..1b6e895 100644 --- a/include/linux/elf.h +++ b/include/linux/elf.h @@ -83,4 +83,16 @@ extern int arch_parse_elf_property(u32 type, const void *data, size_t datasz, bool compat, struct arch_elf_state *arch); #endif +#ifdef CONFIG_ARCH_HAVE_ELF_PROT +int arch_elf_adjust_prot(int prot, const struct arch_elf_state *state, + bool has_interp, bool is_interp); +#else +static inline int arch_elf_adjust_prot(int prot, + const struct arch_elf_state *state, + bool has_interp, bool is_interp) +{ + return prot; +} +#endif + #endif /* _LINUX_ELF_H */