Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1512669ybl; Tue, 13 Aug 2019 14:03:57 -0700 (PDT) X-Google-Smtp-Source: APXvYqxvr3qByKd9pxw4H9NEGJaZtlta9JTw70TtPnx/ne7Vh4CRvwthZ/p0NDlH66DRdX3ieD3+ X-Received: by 2002:a63:6c02:: with SMTP id h2mr37687736pgc.61.1565730236984; Tue, 13 Aug 2019 14:03:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565730236; cv=none; d=google.com; s=arc-20160816; b=hACJBFtSuj1uEZqajekoILXW1WSc85SBGQ2n5LpDjArMw4t784+eo/P7YWk24VLPOw lP+OqapulEFp0+sVUSuPCrT4vvUNAlzkiHLaw1/GoO2Ynz6AXonuqiSTcD62utVdKzS2 nS5dZkLI8WdBRVy1XPzRGp0WemNzIu/upwADkAEi7/HVpHZZ2/ukrv03s1FheLbczQxT rR2Mv03mAeMBeuDOFMvyha4AjmvwQMkUtBdRV6AWWWkaJczCr0wSqPp7DJsepYrR27kO 25AWZHplWrKB+4bDXUQmRjIloWhs42rtc+Y018KxyqglCdKBD4HH4toK9hQO9mkyAO5/ wLYQ== 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=nphOafVwX9Troi3iLoGKXRbLcVR4O4Umn02RMgxVmP4=; b=0+YNiPPX7cCxf0px07ejKJw5u8lEDm0mW85whlg96+M/G08NZZQeo1Sbtmo+pDx9A/ Z+97cPQJnyVt9B5+7+Px6TIZv9awTkbFPj4NvYDuXv5GqYXzVVuyKq4T3t/YSSfnn1BU TvuErKOmnCULg1yuUtY3Ur6r7GtD7dLawnLtWm5HnPyH/KOPpXwHdM0lI3gh6l0oN8uZ FI1YKn63ciMsv1E4s7HGYMsC1ckAxVpmnBl46VzUvArAUSuxSYNuUsa5vA1vB4U4dXLB m6yVrTmOyM/plie+EogSsGWAGQNtHhiiFkKzj6xfgZJgYlhZDxoudCOMV2Anuso31+xF LvRw== 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 w7si64130167pgl.323.2019.08.13.14.03.40; Tue, 13 Aug 2019 14:03:56 -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 S1726858AbfHMVCx (ORCPT + 99 others); Tue, 13 Aug 2019 17:02:53 -0400 Received: from mga06.intel.com ([134.134.136.31]:16075 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726819AbfHMVCw (ORCPT ); Tue, 13 Aug 2019 17:02:52 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 13 Aug 2019 14:02:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,382,1559545200"; d="scan'208";a="187901422" Received: from yyu32-desk1.sc.intel.com ([10.144.153.205]) by orsmga002.jf.intel.com with ESMTP; 13 Aug 2019 14:02:50 -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" , Vedvyas Shanbhogue , Dave Martin Cc: Yu-cheng Yu Subject: [PATCH v8 14/27] x86/mm: Shadow stack page fault error checking Date: Tue, 13 Aug 2019 13:52:12 -0700 Message-Id: <20190813205225.12032-15-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190813205225.12032-1-yu-cheng.yu@intel.com> References: <20190813205225.12032-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(), verify a shadow stack page fault is within a shadow stack memory area. It is always an error otherwise. For a valid shadow stack access, set FAULT_FLAG_WRITE to effect copy-on-write. 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 8691261faeb0..918b0e48b2eb 100644 --- a/arch/x86/include/asm/traps.h +++ b/arch/x86/include/asm/traps.h @@ -166,6 +166,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, @@ -174,5 +175,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 9ceacd1156db..75ec38d125fc 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1187,6 +1187,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))) @@ -1344,6 +1355,13 @@ void do_user_addr_fault(struct pt_regs *regs, 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 (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.17.1