From patchwork Tue Mar 31 19:44:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 228536 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.3 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0FA83C43331 for ; Tue, 31 Mar 2020 19:45:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D9724208E0 for ; Tue, 31 Mar 2020 19:45:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1585683905; bh=LVCgjUy9qTgdF+Q0IqASpd6phMHypnwbQmDOdM1vzsA=; h=From:To:Cc:Subject:Date:List-ID:From; b=AJX9+uGgWenE7uX8rflAdoDBvGbmY55+QJHMUPMIIOsR+/+dMLL2gvtK3WxDSIip/ BKhLKOGgHwQh1RGcQ6Nsma3eUsnMDgdM9top/tp1sTZ5wSAek3XTM29OLWCp6OsuaL h9sf8VONWhHnmViF1GlEJmujJzrkCGs9FpqAvdew= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727937AbgCaTpF (ORCPT ); Tue, 31 Mar 2020 15:45:05 -0400 Received: from foss.arm.com ([217.140.110.172]:33312 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727768AbgCaTpF (ORCPT ); Tue, 31 Mar 2020 15:45:05 -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 6D3D430E; Tue, 31 Mar 2020 12:45:02 -0700 (PDT) Received: from localhost (unknown [10.37.6.21]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E1C283F68F; Tue, 31 Mar 2020 12:45:01 -0700 (PDT) From: Mark Brown To: Catalin Marinas , Will Deacon Cc: Mark Rutland , Amit Daniel Kachhap , Kees Cook , linux-arm-kernel@lists.infradead.org, Mark Brown , Szabolcs Nagy , stable@vger.kernel.org Subject: [PATCH] arm64: Always force a branch protection mode when the compiler has one Date: Tue, 31 Mar 2020 20:44:59 +0100 Message-Id: <20200331194459.54740-1-broonie@kernel.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org Compilers with branch protection support can be configured to enable it by default, it is likely that distributions will do this as part of deploying branch protection system wide. As well as the slight overhead from having some extra NOPs for unused branch protection features this can cause more serious problems when the kernel is providing pointer authentication to userspace but not built for pointer authentication itself. In that case our switching of keys for userspace can affect the kernel unexpectedly, causing pointer authentication instructions in the kernel to corrupt addresses. To ensure that we get consistent and reliable behaviour always explicitly initialise the branch protection mode, ensuring that the kernel is built the same way regardless of the compiler defaults. Fixes: 7503197562567 (arm64: add basic pointer authentication support) Reported-by: Szabolcs Nagy Signed-off-by: Mark Brown Cc: stable@vger.kernel.org --- arch/arm64/Kconfig | 4 ++++ arch/arm64/Makefile | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index d3efdc095a17..1e46746e8392 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -1537,6 +1537,10 @@ config ARM64_PTR_AUTH This feature works with FUNCTION_GRAPH_TRACER option only if DYNAMIC_FTRACE_WITH_REGS is enabled. +config CC_HAS_BRANCH_PROT_NONE + # GCC 9 or later, clang 8 or later + def_bool $(cc-option,-mbranch-protection=none) + config CC_HAS_BRANCH_PROT_PAC_RET # GCC 9 or later, clang 8 or later def_bool $(cc-option,-mbranch-protection=pac-ret+leaf) diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile index f15f92ba53e6..370fca6663c8 100644 --- a/arch/arm64/Makefile +++ b/arch/arm64/Makefile @@ -65,6 +65,10 @@ stack_protector_prepare: prepare0 include/generated/asm-offsets.h)) endif +# Ensure that if the compiler supports branch protection we default it +# off, this will be overridden if we are using branch protection. +branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_NONE) := -mbranch-protection=none + ifeq ($(CONFIG_ARM64_PTR_AUTH),y) branch-prot-flags-$(CONFIG_CC_HAS_SIGN_RETURN_ADDRESS) := -msign-return-address=all branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=pac-ret+leaf @@ -73,9 +77,10 @@ branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=pa # we pass it only to the assembler. This option is utilized only in case of non # integrated assemblers. branch-prot-flags-$(CONFIG_AS_HAS_PAC) += -Wa,-march=armv8.3-a -KBUILD_CFLAGS += $(branch-prot-flags-y) endif +KBUILD_CFLAGS += $(branch-prot-flags-y) + ifeq ($(CONFIG_CPU_BIG_ENDIAN), y) KBUILD_CPPFLAGS += -mbig-endian CHECKFLAGS += -D__AARCH64EB__