Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1689355pxb; Fri, 20 Aug 2021 11:23:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxihpGX70UfgNMgPt2VkYA65a6KvFjNgyDysvmCPJbd/WwvfZ18BnvtsgUYgCndJWoWLRDU X-Received: by 2002:a17:906:781:: with SMTP id l1mr23094792ejc.289.1629483808384; Fri, 20 Aug 2021 11:23:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629483808; cv=none; d=google.com; s=arc-20160816; b=pJSHJhzaQpPqHhjLJ/0I0df1h9Vr+A11GkOUP3cMKZKi/lomIWMzRIABJY4c2ASHP3 C2eeOBFOBDQ6akWvni19VeeEbxp54J9dPIaqtKZC7aQLpr7VX+TFwO+5WTeuU8Kx2f0Z 2HajIjLKSA+I21gpj30naPJTPqdFSH20CddmTQj8kmG4PtnrWw0Skt+SmRnKosXnp5C0 +9KnmIwvVdM5YZvALwFC9MumAZrarvcfVYryMsQCePZkNkja022Vic2fK69RmJHHZ03K z2wWRpa4227wRxx/UsmIJ2WnpPuH/QQuRsc0C+mbCH9PFveE73bwGOaoNPoYJbCZdXec ARBQ== 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; bh=QW3Smr+wQtpk6hqgzaJOQTOjrnrfLmhQdHbfJ0aiQqU=; b=m6TO1hPVdEMA0YnKhoMkGMb059T3Qq1Bcmkn7RGInuJgCr9LRSetSijGFAtLwK+qRr tkms7gIi9MNgO+cUcM+nh5dZ5PLz8u2x/RPQCzxAbtjcGhM6mFTUFmBIdSGKzL24CRJj OcUc8jt2Hs65yzOMSPNwWfpg2AzEB8udcXNQGLt82N5u5gGZ85AmIpFOh6WAnZHBpLKO Vm81uDXmk5GyWIvzuod2Ht750s8cZnF/65HHpLuts8iv4HzPvEqh00X4qhcPWkKsmDOk ukNT/hHFg1QU5RWAmHsP1dHnYMKvUzpJsfVBWjT6d6f6J6Q+nRAsamMUz+RvMYE6OywM WEaw== 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 l21si7504786edc.240.2021.08.20.11.23.04; Fri, 20 Aug 2021 11:23:28 -0700 (PDT) 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 S237730AbhHTSUn (ORCPT + 99 others); Fri, 20 Aug 2021 14:20:43 -0400 Received: from mga05.intel.com ([192.55.52.43]:22433 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237845AbhHTSUO (ORCPT ); Fri, 20 Aug 2021 14:20:14 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10082"; a="302407819" X-IronPort-AV: E=Sophos;i="5.84,338,1620716400"; d="scan'208";a="302407819" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Aug 2021 11:18:48 -0700 X-IronPort-AV: E=Sophos;i="5.84,338,1620716400"; d="scan'208";a="533074722" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Aug 2021 11:18:47 -0700 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" , Dave Martin , Weijiang Yang , Pengfei Xu , Haitao Huang , Rick P Edgecombe Cc: Yu-cheng Yu , "Kirill A . Shutemov" Subject: [PATCH v29 15/32] x86/mm: Shadow Stack page fault error checking Date: Fri, 20 Aug 2021 11:11:44 -0700 Message-Id: <20210820181201.31490-16-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210820181201.31490-1-yu-cheng.yu@intel.com> References: <20210820181201.31490-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 Reviewed-by: Kirill A. Shutemov --- 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 b2eefdefc108..ad3350297e4b 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1100,6 +1100,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_SHADOW_STACK)) + return 1; + return 0; + } + if (error_code & X86_PF_WRITE) { /* write, present and write, not present: */ if (unlikely(!(vma->vm_flags & VM_WRITE))) @@ -1293,6 +1304,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 (error_code & X86_PF_SHSTK) + flags |= FAULT_FLAG_WRITE; if (error_code & X86_PF_WRITE) flags |= FAULT_FLAG_WRITE; if (error_code & X86_PF_INSTR) -- 2.21.0