Received: by 10.192.165.156 with SMTP id m28csp772174imm; Mon, 16 Apr 2018 08:28:53 -0700 (PDT) X-Google-Smtp-Source: AIpwx480zV6E93+Du47VMTR+RliEeYM7pZVIEXwICZcg8Q14dXcuK+GiGeUfStJJZXRz3LF6woqg X-Received: by 2002:a17:902:6590:: with SMTP id c16-v6mr15818597plk.292.1523892533726; Mon, 16 Apr 2018 08:28:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523892533; cv=none; d=google.com; s=arc-20160816; b=wjgO9sc/NvcRDo8qTx95eh+pwPPpDZeWVQCWVjKYG4W6dx3ROQLJdE1iWlKc0dMZwf Wk4n/16LGkzgsYQCFPKBJGdyGQf1caOPmnHamYRJrmvfJEWiMpvgS4Y3D5M7Em/t3EEK N2ksJeb0UZeK/EwrU0TJrUkx0rzkS/3drmpZiAX52VBdsIAx4m570hilAXLz9drpulFM aaZgr/+dlGkjuyPlP52MKz7VqDCf+1bURauPnMOWdr1NMQHMKq3mW+5O/uYOymO6LXxH JR3s3NY7l2hlaMU2v05PMdajEnuXnXIrwDdQDDdM+HTVtKK1ByyMhkQ7/i7RP8xM3+X3 lgyg== 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=0PPzMdjhSd8a/6VGtXFfKOyp560QgzNmba8ExDuCf2WSzZZjjuwAJtAQN/ZdtMpPfi qR6+O66Rb4l7u1Ra5WkPlUlTLEYBki363dPw501yzp7knTLIdTchSOAmBuh1BkmXUYA7 MOauPkCEJ2yKR18wrVv1lgls5cPC3OxcVlMCHSm1+0FR128EzLBspC3S1LaVjoSyslbN iokwXC7R0kvTpkM2g9vuHc9D0jVPdRsjkhswb8UIUZm0Crny8bLiqHOOWGRbzDwavTPP xeSmKoLKyd4HVDmz9daPLLhekaQPlq8vkaMC0B3Id0G0Zdf3eRjFJ7bNBmgsX/5zqZov Dg0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@8bytes.org header.s=mail-1 header.b=fDUxoLAN; 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 a62-v6si12206756pla.185.2018.04.16.08.28.39; Mon, 16 Apr 2018 08:28:53 -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=fDUxoLAN; 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 S1753083AbeDPPZ6 (ORCPT + 99 others); Mon, 16 Apr 2018 11:25:58 -0400 Received: from 8bytes.org ([81.169.241.247]:36224 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753057AbeDPPZ4 (ORCPT ); Mon, 16 Apr 2018 11:25:56 -0400 Received: by theia.8bytes.org (Postfix, from userid 1000) id D9AE9B49; Mon, 16 Apr 2018 17:25:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=8bytes.org; s=mail-1; t=1523892342; bh=uB9uFqbEhDpcmRZbBtF1yKjhG+ZP1/XktKMzxENle0A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fDUxoLANNNmL1YNAtvR6UFFuP7C+NGxhObdKwx4XUfHiHZAtMyhcQvmXp+/LH6To4 hLT3Ubjz3WmwGiN9/XGn/EIbK/bD50N+v7Smbd7lQAENDnGJVHHxywqmJbrj5cOMG9 5g/J4WfJWEB2qng7Ype4SG5mgsyNra208508zQcLyEVTDvHNVMY8DmBYpqNAkebpS3 qfdq5e+fNVdEGa6whsE1mV4HtTzhf96WTRMoUiGG9fS7xOywZ4owzfJoJhOgjp1Q1u 3DGSerS9hyWgrtfbV9JB4qawDLLiRVhrmfJ32wKDMskLKBTMcvS7QdLEyePPHVaGAu UsGHBFLe/J4aQ== 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 35/35] x86/entry/32: Add debug code to check entry/exit cr3 Date: Mon, 16 Apr 2018 17:25:23 +0200 Message-Id: <1523892323-14741-36-git-send-email-joro@8bytes.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1523892323-14741-1-git-send-email-joro@8bytes.org> References: <1523892323-14741-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