Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp4123948pxb; Tue, 10 Nov 2020 08:29:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJzQXj8qkHSx/4QTXeXXvctHz86M702dDzyrCME+ouEgxBY7ImWcift8XET4oil+4/1+x846 X-Received: by 2002:a50:fd15:: with SMTP id i21mr99922eds.127.1605025758819; Tue, 10 Nov 2020 08:29:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605025758; cv=none; d=google.com; s=arc-20160816; b=Py9YVjtq8nej7s/rybtdKf8lTQhq46U6vQv8PWWOymi0d3I03kl1mpPmvqI5gQ2Mxj 2UBBxE5tueecXMV8llAIesNGnCengv40Upe4xuyJ1AV5yyzt2thGMNK7lBFZkSmrIuc6 /UKuy1d8oULzc4GM/rSRt4J7UgZ8mD79sxbICyn/KH+uEpqxVvLcsEAA/mL3NM9k0QJ2 o85306VCTysRaWvs6LXxc8JR1vuHq7r9VXL6L7wSuZ5vCQeuX92s6RJvAuNphVCVaxRl /gVu7z5P4T9OUi+MRL2cNv2l2FSBHlaNhi9jKo9O/iO3bqbz3Jp8v4dTP3fF2xKEcvgg /FDw== 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=KRa/cgL0nb77mNdVGEmR4aJ9MYSKdYDiVm3DQ6yZ3+s=; b=DLqXWkxL51yHEptL3Fa+8hFL06jJZTB4Ld+GQ3Kc0uFlopQNwAXvbLRWPPKFHgAJU2 hin4L239I9YxOFpqlVS4RcVIqzIMYOYCV82yCpjtdiU3sWUvG801JRJKMDnDLxg1vfP+ WlqlZJKUeF4eQ2bVQ/X4pJBSLEqUDFvlZ2BC7MiD5L/7FDCxYqoaolu1u3Wkm1epEppm PD3yiIs1PLsvxy0QCslUyvZL5sUz3h9GdcsNkL+Br4FLq0nB5dojY+IfJnh+YGh9yzSX rR0kZQeWjbOCc5m70choCpUA2FZYY/aetst+QOBpWmKu9SflLBGY+zicgZAQrBXL/CFL MDDw== 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 nt19si9560551ejb.490.2020.11.10.08.28.55; Tue, 10 Nov 2020 08:29:18 -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 S1732791AbgKJQYm (ORCPT + 99 others); Tue, 10 Nov 2020 11:24:42 -0500 Received: from mga02.intel.com ([134.134.136.20]:25995 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730397AbgKJQWy (ORCPT ); Tue, 10 Nov 2020 11:22:54 -0500 IronPort-SDR: nPMBHePLlO0FkjROieh4MMmzfL/ACSRXTrSDcN/KFF3xRQZEHdyFAEU0pMjL9xomsmuNEtDuyl +D8kTfddCRPA== X-IronPort-AV: E=McAfee;i="6000,8403,9801"; a="157008722" X-IronPort-AV: E=Sophos;i="5.77,466,1596524400"; d="scan'208";a="157008722" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2020 08:22:53 -0800 IronPort-SDR: jxit4pkfdtFSwf8JL80U7g+rEwtGKm686zoXKqxe6qVpbqHAYNZ6YLXWAhV0WY770ppuoY+7pf M4hhZE4xcfPQ== X-IronPort-AV: E=Sophos;i="5.77,466,1596524400"; d="scan'208";a="365572858" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2020 08:22:51 -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 v15 13/26] x86/mm: Shadow Stack page fault error checking Date: Tue, 10 Nov 2020 08:21:58 -0800 Message-Id: <20201110162211.9207-14-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201110162211.9207-1-yu-cheng.yu@intel.com> References: <20201110162211.9207-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_HW (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 305bc1214aef..205766c438b3 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 */ enum x86_pf_error_code { X86_PF_PROT = 1 << 0, @@ -19,6 +20,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, }; #endif /* _ASM_X86_TRAP_PF_H */ diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index 82bf37a5c9ec..941f55ee7c75 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1110,6 +1110,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))) @@ -1275,6 +1286,14 @@ void do_user_addr_fault(struct pt_regs *regs, perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); + /* + * Clearing _PAGE_DIRTY_HW 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