Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp2318681imm; Thu, 11 Oct 2018 08:26:41 -0700 (PDT) X-Google-Smtp-Source: ACcGV62P9hyyEbPnHUBVKBZ526oIGSmBBWvDY8zzhlgfCdyGb4X0518oH3amwRyr/JZzKkVVAkD8 X-Received: by 2002:a17:902:d917:: with SMTP id c23-v6mr2017535plz.206.1539271601331; Thu, 11 Oct 2018 08:26:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539271601; cv=none; d=google.com; s=arc-20160816; b=PIMmETHgCbQY8ozyw/HS74YQCn7oW1Lzmyk2hC0kFWkZo8eaDL7jrscoyYnhDjO3ge UrHoeoYFp/OmkhDjLeNOGVwzSpVhehdcUj8WXBTRi6e16lsc06svS1lNQxHPcXxnXn/c +Zijw0tSkPjHQuE0kIoIdcYl81R2bCOoMQYguFDWmpvwsZOkQT/gcA4iwoOsJ6eGKZVi B/5zlXLakephfomwUNW35HNUyFKbv7IhPHIu/vRKwx348ggvulMp96KjdQ5r2Tvjx1K3 XrlxRLawG9q87OfG2vLt7j7h57/jFCHQau5zyw7TRasPfODqFKhwOVEcUwvm0IgrYW1i aUSA== 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=FkoMsTy+EC7394CGEjcwVLEJZyO1JXD/+hZO5YvifYjAYNOj3+dO6fgg6Sx520jWXZ q1CU5b8QsbU/4OE+YVKn+22nR4Vx0OrrrCj8wuH8sz722abmqPyGzvN5WW2w1ebJAdHw /sdwxW3NYzCCYvsQm4CuwKejsWd0p09JDYVW1xLqNujR/BzHeW2Uus+uiwiEM7Njxy3e TAFWb454UWXqDiwnIsu/gk3asfNBdlaJRGuGg119TEYGG1yDll9tarxFDCzw4YQuMBFY +peq4RGI9EjAcCRQoecQmnWzTvPFFOjkuEMv403Orr9k4PWbvW6JcQ2cZE5JYODRLFcy mT1Q== 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 f2-v6si25108081pgi.5.2018.10.11.08.26.26; Thu, 11 Oct 2018 08:26:41 -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 S1729014AbeJKWvo (ORCPT + 99 others); Thu, 11 Oct 2018 18:51:44 -0400 Received: from mga05.intel.com ([192.55.52.43]:3986 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728356AbeJKWsY (ORCPT ); Thu, 11 Oct 2018 18:48:24 -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 fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 11 Oct 2018 08:20:47 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,368,1534834800"; d="scan'208";a="78019126" Received: from 2b52.sc.intel.com ([143.183.136.147]) by fmsmga008.fm.intel.com with ESMTP; 11 Oct 2018 08:20:45 -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 , 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 Cc: Yu-cheng Yu Subject: [PATCH v5 15/27] x86/mm: Shadow stack page fault error checking Date: Thu, 11 Oct 2018 08:15:11 -0700 Message-Id: <20181011151523.27101-16-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181011151523.27101-1-yu-cheng.yu@intel.com> References: <20181011151523.27101-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