Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp673710pxb; Wed, 3 Feb 2021 15:01:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJyNRTSfiEsg1lHX92jU2SyfG3TDP0g9G2tcr9zWl/hE2Hs2vylrlIIrr2vD6bwYw0CIOaoD X-Received: by 2002:a05:6402:2690:: with SMTP id w16mr5528932edd.304.1612393301969; Wed, 03 Feb 2021 15:01:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612393301; cv=none; d=google.com; s=arc-20160816; b=rFruNikPh5izuLQMBUygqJOzdOWs3Fdw7kTpz21IowSzWshRMnWS597WUP45GaXINi y7f10ik5Nt6UoIB5XMkOppgRdd5BupNqab23mdyiUVsTusXMKk7RcRXUbEi/ZMR5hlxU Zd1Zrx4QFN9/PhiODSaLOURETLoSzu3FBMc92DpH5aAltHViHWpa8JNIM1C7vv7fcsu9 6qkLd9ke1BvS8o5LZYZdI011F7O4dpYzh2kMnJWeWLLEkcpFRsIl05tscc2D16EOSM11 iwPZ3DVQSbzsu6riVAaZcsSF1/ggmfY6YtUWgJKxqNO2uBq0oVTxQGpPgyVq0giQM3fA RCjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=eORL7PcwwCUc7RJQ0RwatA2f+L5u9CsncDxWRkQ29r4=; b=0AbLdyM8QnEENcPxeDVECimaWpP/EKNdeoGUdkt/QbeSehVrKtvkf2RpNWwLLxVRsv zCNKKhrb8YZ1AAiX0v1RRyc1s0BB8e6KrG9Rx+0UjMkJTrA9NAer6wp1LlGsJsyorB3s lHxnokEaaO2dfZ8G2dRabO59d+4KQqMiu2pqcmgulqt+bEtrfo1dIwBjBoFzh7F4Q5dg VP37UmUSxsBqgBqTtdXG6EIwR7dYVZyXE6WYd9IRKs3Bh7AOr8Avc1ERRU8yYk1BwrQs mZ4vLJTteD7BWiAxnzMubeNZd270JkAqTUuz2ogDDfbzrcYsT3LtNxTJ++IS//lp9Usl s4/g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a1si2264644eds.422.2021.02.03.15.01.17; Wed, 03 Feb 2021 15:01:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233380AbhBCW7H (ORCPT + 99 others); Wed, 3 Feb 2021 17:59:07 -0500 Received: from mga06.intel.com ([134.134.136.31]:30214 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233238AbhBCW6E (ORCPT ); Wed, 3 Feb 2021 17:58:04 -0500 IronPort-SDR: ZL02OKQnYOgoCANYl07XDITE635kQSnZMLGo0qxLlVM8rAonyfGiuyr1JRKw3dm88Gz1uTTxm3 u9wdVvzGHklQ== X-IronPort-AV: E=McAfee;i="6000,8403,9884"; a="242642362" X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="242642362" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 14:56:27 -0800 IronPort-SDR: 4cVRY76efhpwVQu/bcg6WAMas/WxazJTvfiBsSxCU+9ZiGuZbMVm0U4gwcSqSVf/mRaK/tibfI dNhHT0IWNKeg== X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="507921118" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 14:56:27 -0800 From: Yu-cheng Yu To: x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H.J. Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V. Shankar" , Vedvyas Shanbhogue , Dave Martin , Weijiang Yang , Pengfei Xu Cc: Yu-cheng Yu Subject: [PATCH v19 13/25] x86/mm: Shadow Stack page fault error checking Date: Wed, 3 Feb 2021 14:55:35 -0800 Message-Id: <20210203225547.32221-14-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210203225547.32221-1-yu-cheng.yu@intel.com> References: <20210203225547.32221-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Shadow stack accesses are those that are performed by the CPU where it expects to encounter a shadow stack mapping. These accesses are performed implicitly by CALL/RET at the site of the shadow stack pointer. These accesses are made explicitly by shadow stack management instructions like WRUSSQ. Shadow stacks accesses to shadow-stack mapping can see faults in normal, valid operation just like regular accesses to regular mappings. Shadow stacks need some of the same features like delayed allocation, swap and copy-on-write. Shadow stack accesses can also result in errors, such as when a shadow stack overflows, or if a shadow stack access occurs to a non-shadow-stack mapping. In handling a shadow stack page fault, verify it occurs within a shadow stack mapping. It is always an error otherwise. For valid shadow stack accesses, set FAULT_FLAG_WRITE to effect copy-on-write. Because clearing _PAGE_DIRTY (vs. _PAGE_RW) is used to trigger the fault, shadow stack read fault and shadow stack write fault are not differentiated and both are handled as a write access. Signed-off-by: Yu-cheng Yu Reviewed-by: Kees Cook --- arch/x86/include/asm/trap_pf.h | 2 ++ arch/x86/mm/fault.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/arch/x86/include/asm/trap_pf.h b/arch/x86/include/asm/trap_pf.h index 10b1de500ab1..afa524325e55 100644 --- a/arch/x86/include/asm/trap_pf.h +++ b/arch/x86/include/asm/trap_pf.h @@ -11,6 +11,7 @@ * bit 3 == 1: use of reserved bit detected * bit 4 == 1: fault was an instruction fetch * bit 5 == 1: protection keys block access + * bit 6 == 1: shadow stack access fault * bit 15 == 1: SGX MMU page-fault */ enum x86_pf_error_code { @@ -20,6 +21,7 @@ enum x86_pf_error_code { X86_PF_RSVD = 1 << 3, X86_PF_INSTR = 1 << 4, X86_PF_PK = 1 << 5, + X86_PF_SHSTK = 1 << 6, X86_PF_SGX = 1 << 15, }; diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index f1f1b5a0956a..4cfb8c474a82 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1131,6 +1131,17 @@ access_error(unsigned long error_code, struct vm_area_struct *vma) (error_code & X86_PF_INSTR), foreign)) return 1; + /* + * Verify a shadow stack access is within a shadow stack VMA. + * It is always an error otherwise. Normal data access to a + * shadow stack area is checked in the case followed. + */ + if (error_code & X86_PF_SHSTK) { + if (!(vma->vm_flags & VM_SHSTK)) + return 1; + return 0; + } + if (error_code & X86_PF_WRITE) { /* write, present and write, not present: */ if (unlikely(!(vma->vm_flags & VM_WRITE))) @@ -1296,6 +1307,14 @@ void do_user_addr_fault(struct pt_regs *regs, perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); + /* + * Clearing _PAGE_DIRTY is used to detect shadow stack access. + * This method cannot distinguish shadow stack read vs. write. + * For valid shadow stack accesses, set FAULT_FLAG_WRITE to effect + * copy-on-write. + */ + if (hw_error_code & X86_PF_SHSTK) + flags |= FAULT_FLAG_WRITE; if (hw_error_code & X86_PF_WRITE) flags |= FAULT_FLAG_WRITE; if (hw_error_code & X86_PF_INSTR) -- 2.21.0