Received: by 10.192.165.148 with SMTP id m20csp3579973imm; Mon, 23 Apr 2018 08:51:57 -0700 (PDT) X-Google-Smtp-Source: AIpwx48c5zshcOQAzacfqkwvApk55gNtphmbhnotUlOoDGm7zW/d/vD5FmDeYXmGngdY4m9/snkJ X-Received: by 2002:a17:902:788b:: with SMTP id q11-v6mr21290601pll.76.1524498717650; Mon, 23 Apr 2018 08:51:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524498717; cv=none; d=google.com; s=arc-20160816; b=jNNYOu7wVpODz2z2fhTgzCu59UU1859Bspt3sY/IsTC7FSm3TMlO8GR6VXnS+lC/fF GdCSPKvMrWD9f9g5XHEHqPtkCRn0jAcQRYHuAP3d56Mb+5HY+q5YxbbVbKUTU0DhGKq8 nRKpUJSSPU2FvAB60NpDCxRp9cDhcXlB1xMAov6hbVCDIt/epmX+0MkjKTIAASbM4DcO iyvcWpeePr+KbaQghDrkuaaAzPR9ziwtC3wT3deSmPVuuQRjcFIQW6rQQ5sJnRdsLTpu eqAv+FLp3dvWOgIzChzZ41HrLX6NekaJ8w7S8KvhUCnMZgB+UZsoqTc4xUZVb3fC8TLT O3DA== 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:dkim-signature:arc-authentication-results; bh=g4Mvb5nbP5KaTzLhUrpKObR4w1hg4TZ23NVo0lBHPNg=; b=nwyLymN21oStl3VFWkoVeim0WPadcAdxxAhJ8HW5GexHe2E+8ht7slJ0qL//VBKhaP EkVl1oBYOjm9S6/wp/j2dCIe6p2Xt6pxFqOiLZ10drLFWWO4N35pMZI+0Ivv3406m/0+ u7C645iVlQhKizmHTs0/tjxdGs9CHWaDqPHlFwXi/kFCmfh1uzUOYT5NunswPS+6Ea/e rXDYKj/vM6CSJUUbjmQlGXESMJG0qb49pC/k+FRe/gWZcBmiNBqgt72QWDXqVJnLNL1c T02xcve7mvfPX6ml25+2B+oMvLx9WPHfojlEOXaT0mn5vZjW6Kp440wnQeM7yxpuKIXA Bxvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@8bytes.org header.s=mail-1 header.b=W9cvbJTa; 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=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c11si9504256pgv.446.2018.04.23.08.51.43; Mon, 23 Apr 2018 08:51:57 -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; dkim=fail (test mode) header.i=@8bytes.org header.s=mail-1 header.b=W9cvbJTa; 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=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755999AbeDWPtH (ORCPT + 99 others); Mon, 23 Apr 2018 11:49:07 -0400 Received: from 8bytes.org ([81.169.241.247]:33282 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755902AbeDWPsF (ORCPT ); Mon, 23 Apr 2018 11:48:05 -0400 Received: by theia.8bytes.org (Postfix, from userid 1000) id 224AAAA5; Mon, 23 Apr 2018 17:47:48 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=8bytes.org; s=mail-1; t=1524498470; bh=uB9uFqbEhDpcmRZbBtF1yKjhG+ZP1/XktKMzxENle0A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W9cvbJTamBBjofnkc3qv3CMfZju6fa2K9XdaaGWi+RH2V8s9HmUlbx3c6jFza1BMJ leq4WgQHNUM67I84wVH0sU05WexmQAxtbkB0EhMIRCEZA9aBh7X+qOmVbRwAdkI2Kv 0hBv5jNicAd1o3kcEON6+Mlu9mWIpWfmHqWn+4fS8Rc7KtIBA6bsAxglY4XFz8jUtS 6toGD25vXLKBm53tYJx83NSe/wN6N8IlHCJes/MNuzP+kUIkC0Q6UALODGj54/oPvV I+/+AxvxgoIKkZlYVQWFC9x9upbAC3nTBgwgSH16jWEoHazvA232brTTUZvH4VRPoo O4D04jlwv9Y2A== From: Joerg Roedel To: Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" Cc: x86@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Andy Lutomirski , Dave Hansen , Josh Poimboeuf , Juergen Gross , Peter Zijlstra , Borislav Petkov , Jiri Kosina , Boris Ostrovsky , Brian Gerst , David Laight , Denys Vlasenko , Eduardo Valentin , Greg KH , Will Deacon , aliguori@amazon.com, daniel.gruss@iaik.tugraz.at, hughd@google.com, keescook@google.com, Andrea Arcangeli , Waiman Long , Pavel Machek , "David H . Gutteridge" , jroedel@suse.de, joro@8bytes.org Subject: [PATCH 37/37] x86/entry/32: Add debug code to check entry/exit cr3 Date: Mon, 23 Apr 2018 17:47:40 +0200 Message-Id: <1524498460-25530-38-git-send-email-joro@8bytes.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1524498460-25530-1-git-send-email-joro@8bytes.org> References: <1524498460-25530-1-git-send-email-joro@8bytes.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel Add a config option that enabled code to check that we enter and leave the kernel with the correct cr3. This is needed because we have no NX protection of user-addresses in the kernel-cr3 on x86-32 and wouldn't notice that type of bug otherwise. Signed-off-by: Joerg Roedel --- arch/x86/Kconfig.debug | 12 ++++++++++++ arch/x86/entry/entry_32.S | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/arch/x86/Kconfig.debug b/arch/x86/Kconfig.debug index 192e4d2..a57f556 100644 --- a/arch/x86/Kconfig.debug +++ b/arch/x86/Kconfig.debug @@ -337,6 +337,18 @@ config X86_DEBUG_FPU If unsure, say N. +config X86_DEBUG_ENTRY_CR3 + bool "Debug CR3 for Kernel entry/exit" + depends on X86_32 && PAGE_TABLE_ISOLATION + ---help--- + Add instructions to the x86-32 entry code to check whether the kernel + is entered and left with the correct CR3. When PTI is enabled, this + checks whether we enter the kernel with the user-space cr3 when + coming from user-mode and if we leave with user-cr3 back to + user-space. + + If unsure, say N. + config PUNIT_ATOM_DEBUG tristate "ATOM Punit debug driver" depends on PCI diff --git a/arch/x86/entry/entry_32.S b/arch/x86/entry/entry_32.S index f47e535..6b371a9 100644 --- a/arch/x86/entry/entry_32.S +++ b/arch/x86/entry/entry_32.S @@ -166,6 +166,24 @@ .Lend_\@: .endm +.macro BUG_IF_WRONG_CR3 no_user_check=0 +#ifdef CONFIG_X86_DEBUG_ENTRY_CR3 + ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI + .if \no_user_check == 0 + /* coming from usermode? */ + testl $SEGMENT_RPL_MASK, PT_CS(%esp) + jz .Lend_\@ + .endif + /* On user-cr3? */ + movl %cr3, %eax + testl $PTI_SWITCH_MASK, %eax + jnz .Lend_\@ + /* From userspace with kernel cr3 - BUG */ + ud2 +.Lend_\@: +#endif +.endm + /* * Switch to kernel cr3 if not already loaded and return current cr3 in * \scratch_reg @@ -218,6 +236,8 @@ .macro SAVE_ALL_NMI cr3_reg:req SAVE_ALL + BUG_IF_WRONG_CR3 + /* * Now switch the CR3 when PTI is enabled. * @@ -229,6 +249,7 @@ .Lend_\@: .endm + /* * This is a sneaky trick to help the unwinder find pt_regs on the stack. The * frame pointer is replaced with an encoded pointer to pt_regs. The encoding @@ -292,6 +313,8 @@ .Lswitched_\@: + BUG_IF_WRONG_CR3 + RESTORE_REGS pop=\pop .endm @@ -362,6 +385,8 @@ ALTERNATIVE "", "jmp .Lend_\@", X86_FEATURE_XENPV + BUG_IF_WRONG_CR3 + SWITCH_TO_KERNEL_CR3 scratch_reg=%eax /* @@ -803,6 +828,7 @@ ENTRY(entry_SYSENTER_32) */ pushfl pushl %eax + BUG_IF_WRONG_CR3 no_user_check=1 SWITCH_TO_KERNEL_CR3 scratch_reg=%eax popl %eax popfl @@ -897,6 +923,7 @@ ENTRY(entry_SYSENTER_32) * whereas POPF does not.) */ btr $X86_EFLAGS_IF_BIT, (%esp) + BUG_IF_WRONG_CR3 no_user_check=1 popfl popl %eax @@ -974,6 +1001,8 @@ restore_all: /* Switch back to user CR3 */ SWITCH_TO_USER_CR3 scratch_reg=%eax + BUG_IF_WRONG_CR3 + /* Restore user state */ RESTORE_REGS pop=4 # skip orig_eax/error_code .Lirq_return: @@ -987,6 +1016,7 @@ restore_all: restore_all_kernel: TRACE_IRQS_IRET PARANOID_EXIT_TO_KERNEL_MODE + BUG_IF_WRONG_CR3 RESTORE_REGS 4 jmp .Lirq_return @@ -994,6 +1024,19 @@ restore_all_kernel: ENTRY(iret_exc ) pushl $0 # no error code pushl $do_iret_error + +#ifdef CONFIG_X86_DEBUG_ENTRY_CR3 + /* + * The stack-frame here is the one that iret faulted on, so its a + * return-to-user frame. We are on kernel-cr3 because we come here from + * the fixup code. This confuses the CR3 checker, so switch to user-cr3 + * as the checker expects it. + */ + pushl %eax + SWITCH_TO_USER_CR3 scratch_reg=%eax + popl %eax +#endif + jmp common_exception .previous _ASM_EXTABLE(.Lirq_return, iret_exc) -- 2.7.4