Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp38233imm; Thu, 30 Aug 2018 07:49:35 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZoFM2fwrBNaUBc1AhvlgWMNvklrSiRRbfqdVLvhaw4SD6R0hDhDJXqbBBboX7Mt+2hIJBY X-Received: by 2002:a17:902:110b:: with SMTP id d11-v6mr10388420pla.171.1535640575303; Thu, 30 Aug 2018 07:49:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535640575; cv=none; d=google.com; s=arc-20160816; b=f5+c83WFuj+ImL/mOl49s+3TBst0ytluTCmXn/z4YDc2BIbbf144XkC3t6j+IbfjBf gHTN/VlOSd+6hvFoOmtYijyP+xR2691/FNd4AOmrwBJAHheS6Yg1eKMfav/u0TbNFzcG bvJ7ZFaNngSdK2kuzXofG3GjKXEx3sgHR+qqpXXaAmqoloudFRAjJ536l6VHW/6buCD7 sWHHXIybON3CLlz4p+EmF/HE/F/ZpjwS1jvB2lSGoX5XHKzM8gWVEuBfqOep6SQvAgiG nYpN1fOR6R2vakuYfmkSAnWmgjMV8PwS33+ZOO2kBukGB/tUYDdk3FcWpfElet0lqKGH hcPQ== 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:arc-authentication-results; bh=tUVERZhykosIqoOxzOVgpuEQzQHuZq0XBD5oE1YaqTA=; b=xAvpb63sIvcXIeuxtbxLBSmEYOE9Qwzwo6SutHs37soCgzsIyw1SMqzvIBwVzvCc+b vU6A7ioMi7dWKE1XDPV/+fZQ+MuEmWGAWLRixQWaX1wjNuWVleCq1EWFCkXZNb0I8U2J NBVTy912fwsSJjWXKXGYHLzjiuJWKe/PA4bZYp6SEY8+ZO09pfryDDuM1vJNKZo0+Ls3 OM1V5e9EeX9lBe51Z84QIpSa2fMg3nfvOL/dTUBX5beJaOZrzoDk2s7mSMog9kXSVi1g GzR4LOyLZX7q1B0+e5bIhJsNQC9Sy2RkG/fg1JG+6yJFmM+lHPpMWMjuRexnh5Zm13SG 5jgQ== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d17-v6si6341465pgp.549.2018.08.30.07.49.18; Thu, 30 Aug 2018 07:49:35 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729793AbeH3Sth (ORCPT + 99 others); Thu, 30 Aug 2018 14:49:37 -0400 Received: from mga01.intel.com ([192.55.52.88]:38433 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729569AbeH3SqS (ORCPT ); Thu, 30 Aug 2018 14:46:18 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Aug 2018 07:43:43 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,307,1531810800"; d="scan'208";a="67186714" Received: from 2b52.sc.intel.com ([143.183.136.52]) by fmsmga008.fm.intel.com with ESMTP; 30 Aug 2018 07:43:41 -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 , Cyrill Gorcunov , Dave Hansen , Florian Weimer , "H.J. Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , "Ravi V. Shankar" , Vedvyas Shanbhogue Cc: Yu-cheng Yu Subject: [RFC PATCH v3 13/24] x86/mm: Shadow stack page fault error checking Date: Thu, 30 Aug 2018 07:38:53 -0700 Message-Id: <20180830143904.3168-14-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180830143904.3168-1-yu-cheng.yu@intel.com> References: <20180830143904.3168-1-yu-cheng.yu@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If a page fault is triggered by a shadow stack access (e.g. call/ret) or shadow stack management instructions (e.g. wrussq), then bit[6] of the page fault error code is set. In access_error(), we check if a shadow stack page fault is within a shadow stack memory area. Signed-off-by: Yu-cheng Yu --- arch/x86/include/asm/traps.h | 2 ++ arch/x86/mm/fault.c | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/arch/x86/include/asm/traps.h b/arch/x86/include/asm/traps.h index 5196050ff3d5..58ea2f5722e9 100644 --- a/arch/x86/include/asm/traps.h +++ b/arch/x86/include/asm/traps.h @@ -157,6 +157,7 @@ enum { * 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, @@ -165,5 +166,6 @@ 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_TRAPS_H */ diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index b9123c497e0a..3842353fb4a3 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1162,6 +1162,17 @@ access_error(unsigned long error_code, struct vm_area_struct *vma) (error_code & X86_PF_INSTR), foreign)) return 1; + /* + * Verify X86_PF_SHSTK is within a shadow stack VMA. + * It is always an error if there is a shadow stack + * fault outside a shadow stack VMA. + */ + 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))) @@ -1300,6 +1311,13 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code, perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); + /* + * If the fault is caused by a shadow stack access, + * i.e. CALL/RET/SAVEPREVSSP/RSTORSSP, then 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.17.1