Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp778796imm; Fri, 21 Sep 2018 08:10:06 -0700 (PDT) X-Google-Smtp-Source: ANB0VdaL+U0xuU5qKINsU/Ymwzm1TlBSLsW7HBGqoCuTps2GA+Lp+rWcSljZoy6g89m+aNwntf3/ X-Received: by 2002:a62:384a:: with SMTP id f71-v6mr46849385pfa.48.1537542606042; Fri, 21 Sep 2018 08:10:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537542606; cv=none; d=google.com; s=arc-20160816; b=VAY6rnx/E8an/fd6qG4LuffNjLs1ydLIKxrHRTKxJUaylZ+I3RP+wqN3TTHpiJJOWa 3C9coycxYts/U1KvNoyOweFycKfiFND9Wgr28H414KR5aVvilUuWkROX2xjHRLIVESVb tCRASztOglY8OwGQH3/ojmaOYn2pPGXfa4VkZ214FCGmZ2GkRUjEvRVTyub2trvRfKwb hpnhyp6sbn5hwtp68y3TdkZ6wtASmxjpA+zJCTRTSsJgKsZikpRh55PChmeWGjq60Q2f rnBj9evuSu4VqHSYHohj+u4qHaV36d8gbWov233z5Vp3iKok1fP81fgnMYiopS2y8Je/ Y7fQ== 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; bh=k4I+IBksg1YUf85BSJ9dR9AdYeEvkDMb/4N/mPBj2zE=; b=St30kUXwDecJLbh7axtAOaiF4QHUE85+8YRGIH6QRcyxANnSZobNh3jG2+zFuiRFUv t03TTio4QmsfGykyfDvCSjkxleQN0xoKAO/m3DyFck/lIYyQJx7r397rvjdIKTSTOAhd skojgIrf/ZSeohPII6JbqY04Q7qV8ysTPBS2rxCgNbayzExh/f332lCLJDU3Nzy5/Cih Wc9wSQ+YwCnCKrWxDO6QpxvVC2C58RIv2F9QARQjjR/d3xrIXnkrAlw1TkHscYFzwbUD DondP1250fQGZcAiruse1X+8hqWdO6NTYKwcCP5OIZ6iRKe0Ob64CiA1D707zqrtiOXw q4wg== 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 w16-v6si28275153pga.104.2018.09.21.08.09.48; Fri, 21 Sep 2018 08:10:06 -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 S2390606AbeIUU6k (ORCPT + 99 others); Fri, 21 Sep 2018 16:58:40 -0400 Received: from mga09.intel.com ([134.134.136.24]:19987 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390454AbeIUU6K (ORCPT ); Fri, 21 Sep 2018 16:58:10 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 21 Sep 2018 08:08:50 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,285,1534834800"; d="scan'208";a="71856568" Received: from 2b52.sc.intel.com ([143.183.136.51]) by fmsmga007.fm.intel.com with ESMTP; 21 Sep 2018 08:08:49 -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 , Randy Dunlap , "Ravi V. Shankar" , Vedvyas Shanbhogue Cc: Yu-cheng Yu Subject: [RFC PATCH v4 13/27] x86/mm: Shadow stack page fault error checking Date: Fri, 21 Sep 2018 08:03:37 -0700 Message-Id: <20180921150351.20898-14-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180921150351.20898-1-yu-cheng.yu@intel.com> References: <20180921150351.20898-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 47bebfe6efa7..7c3877a982f4 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