From patchwork Thu Aug 30 14:38:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu-cheng Yu X-Patchwork-Id: 10581835 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0AAD814E1 for ; Thu, 30 Aug 2018 14:44:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EE0ED2B560 for ; Thu, 30 Aug 2018 14:44:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E0CA52BC25; Thu, 30 Aug 2018 14:44:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 76C892B560 for ; Thu, 30 Aug 2018 14:44:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 11EB66B5222; Thu, 30 Aug 2018 10:43:47 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id EEF9D6B5220; Thu, 30 Aug 2018 10:43:46 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF4DB6B5220; Thu, 30 Aug 2018 10:43:46 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) by kanga.kvack.org (Postfix) with ESMTP id 3771C6B521E for ; Thu, 30 Aug 2018 10:43:46 -0400 (EDT) Received: by mail-pf1-f197.google.com with SMTP id c8-v6so4886477pfn.2 for ; Thu, 30 Aug 2018 07:43:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=tUVERZhykosIqoOxzOVgpuEQzQHuZq0XBD5oE1YaqTA=; b=GVywtPD+vhjUonBkwloUhwWn1pSBseYrSmxb2sUc/CMJbq2D0TlsScexIN+z7QUnXA 0BOuHPDthYJNJds4LbxgI7g+c9W56U2VVCjgHeP5ymAslzI5yKhIFHphua6C0oYF2WaB srFARDGxVcBqn+uquko1TCJyVKBBXRWw+wYrIIhZX5tK7xjJlw52LhAI1/Ds0xbPcYTT OJ5yVmMfBrJ6RSfOOxDov7HnuGcxK/jPB+if3eyDz7iOGUhELwpeorWK3Kg6CKmYTVbm 9nNdVBDu3rig8jdWdhARyJdvN+rctctkzlKMb6t0FprSjlyJYfRpRLEi+uxtuuD++Jx+ Wjqg== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Gm-Message-State: APzg51DfxrLlAVrgWTrEn4yKICTaB5XDpo0fj9MAmgWBSAlhuxj9aa8B Lm24MZ94dR7Tdd0iDbnFfrI4dyCEVC0vaE0KhOdRegT6JE3y5LXkQF9nw8VILRROsiTMLT+JW0k 9yT5Udcl0KfRRU2OY/TNLTkf7jWllISABDPTpguzuHw5nO/7SWYZVZx8RohFO0jbUNQ== X-Received: by 2002:a17:902:c6b:: with SMTP id 98-v6mr10601486pls.233.1535640225896; Thu, 30 Aug 2018 07:43:45 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYflbBCpU01DB2wy9+Q+RRB8Lq4qVfU95pZ//BeeqIRfmN14dvPDgrjm3/SX7gqvkX2oQse X-Received: by 2002:a17:902:c6b:: with SMTP id 98-v6mr10601454pls.233.1535640225158; Thu, 30 Aug 2018 07:43:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535640225; cv=none; d=google.com; s=arc-20160816; b=GQR26qvjmT6FV17gxq1vBmshOVJaialb8yE3S8mAzlItJYU54Q9Vv1JaNnYMbbw4mW 5tq2zM+vzH0cvN8vqNWTLyi+KiJ+91fyHmebhzURlulRiNJMudRnObXAAbp3goRZh0EE opzNxfVmKIt6M+nV2AhRp5WDvNu6zY1OTGfY3yQcVzJ2uQWv1FVHWAYKzI4fdvNE75Qz +TqL3VqV9K6eVCWWyfCmMrTEoDlTbB4fWGiGdHxUTSrprIBQxRvmiNCPxu5SWUB7z7LF 9jmsKg4dVZxNjydcA5HuKsSLXO//wcObULrKWa6iDqANhh830lbByN4EUTw0fgDPJe5I i3dw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=tUVERZhykosIqoOxzOVgpuEQzQHuZq0XBD5oE1YaqTA=; b=vph3LquthaaxhYQsWleZ5YkRu2z6d3pJFcJy+hq3H957b0pBeWw6GFI0N8jUNb85Vo HmMUn9Zy+hhOWMrshErIcOMCUR50TKbfuVKjTmE+1UcVtslo27/UpnKcyT6C4QxCzcp7 6c9QUPRQS4UAYeKv7RGK9/9PSNCHHysxOA6Nj7gtKz9xNmruaEKZQwY3yeuUYypO4oQ5 wNXoCuPHmyn7W92ukeWSfNNnKauzJLZR9l+jeUCXQjK1QZ0nr0zQBCgE4r9Kmy/KR0Ss yICQ0BT+yeNsw/byE6aQ/pu6MDa01/xi1o39cWHFkQNi86SLRusdsn/1Uo0nPiCuA+b4 qrQQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga03.intel.com (mga03.intel.com. [134.134.136.65]) by mx.google.com with ESMTPS id t2-v6si6809115pge.64.2018.08.30.07.43.45 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Aug 2018 07:43:45 -0700 (PDT) Received-SPF: pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.65 as permitted sender) client-ip=134.134.136.65; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Aug 2018 07:43:44 -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> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP 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)