Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2260921imm; Thu, 19 Jul 2018 16:41:59 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfdtLBKVuomBbgckOayJk3SBUTybBGhtUbOKL1flaqpuv7id//hUxRXF1UoOxw9+nhHmxG/ X-Received: by 2002:a17:902:6105:: with SMTP id t5-v6mr12044515plj.92.1532043719726; Thu, 19 Jul 2018 16:41:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532043719; cv=none; d=google.com; s=arc-20160816; b=t1VEK8sX/u4YmYKcMfWyhMWy9kC6XGmaDrhlQ2mY6a364HPOGePh+WPC0I+2uk40xC 4S/khq7A1rwdW+k3mJd0D4OTwO8R99AmH47WU0KNz7ZYkt/2POqQnn2FGucVxnm1nwZI uzjos6s1yWEVRYLyeH8BLdY9uz01wintzcp9FitpcYfes5aTQAsOHnGlrblACKl95Qge LTiBZr4TMGQRrEb16mdys4IAz5vTXFtbmkV6dRIqOEIgj8yw6HCabiNsXCPDWROz3pmB w4/YadAJ1C03n93h6y2Jr9eJ8SxBjc1/akGOkmQ5Md58Vckokl761JMfoSOf4wbHA74d jBNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date:arc-authentication-results; bh=RzQQRayqNJdOTAFGx7HmcY/Zk4FPFB+2an4L7KAVoGs=; b=v5Tgl5nlELCg9+PKX2SwRTREWlp5tonEOoeRzlIUUvedvBzMIfZtB3ljP2s8OtDDrt eTe2u0XXKnxjayYiLtdz3GTeQNYqN8Z8u8ZbCyB+gAj5n+l2X9mef6mbpnY+UG/22YD4 /hs8NQclnBQrAFd3h9Bf6QslLa4Svy/Q+PwfMHLq8kjuel0ZdujZ36rNAI0+AypDBubR w35VYWaZ2CYPwDAQP3SN3HofS34IB9roxpu+CfbPOkpr9ctKj78ZcBLgoyWTFuW3OZv6 ylxYWs4UvEWHMyMPaz6PAE8Zhd3RRKZAAy9g9PUN+HQ8Fdml0uPVOFnuh0KSbpViK8UT EM3g== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z3-v6si406231pgl.579.2018.07.19.16.41.44; Thu, 19 Jul 2018 16:41:59 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730966AbeGTAZV (ORCPT + 99 others); Thu, 19 Jul 2018 20:25:21 -0400 Received: from terminus.zytor.com ([198.137.202.136]:41267 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727553AbeGTAZV (ORCPT ); Thu, 19 Jul 2018 20:25:21 -0400 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTPS id w6JNcqBO2454662 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 19 Jul 2018 16:38:52 -0700 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id w6JNcqim2454659; Thu, 19 Jul 2018 16:38:52 -0700 Date: Thu, 19 Jul 2018 16:38:52 -0700 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Joerg Roedel Message-ID: Cc: dvlasenk@redhat.com, boris.ostrovsky@oracle.com, David.Laight@aculab.com, llong@redhat.com, luto@kernel.org, hpa@zytor.com, jroedel@suse.de, peterz@infradead.org, pavel@ucw.cz, eduval@amazon.com, aarcange@redhat.com, jpoimboe@redhat.com, jgross@suse.com, mingo@kernel.org, bp@alien8.de, torvalds@linux-foundation.org, brgerst@gmail.com, dhgutteridge@sympatico.ca, linux-kernel@vger.kernel.org, will.deacon@arm.com, dave.hansen@intel.com, jkosina@suse.cz, tglx@linutronix.de, gregkh@linuxfoundation.org Reply-To: aarcange@redhat.com, jpoimboe@redhat.com, eduval@amazon.com, pavel@ucw.cz, hpa@zytor.com, peterz@infradead.org, jroedel@suse.de, David.Laight@aculab.com, luto@kernel.org, llong@redhat.com, boris.ostrovsky@oracle.com, dvlasenk@redhat.com, jkosina@suse.cz, dave.hansen@intel.com, will.deacon@arm.com, gregkh@linuxfoundation.org, tglx@linutronix.de, linux-kernel@vger.kernel.org, dhgutteridge@sympatico.ca, brgerst@gmail.com, bp@alien8.de, mingo@kernel.org, jgross@suse.com, torvalds@linux-foundation.org In-Reply-To: <1531906876-13451-40-git-send-email-joro@8bytes.org> References: <1531906876-13451-40-git-send-email-joro@8bytes.org> To: linux-tip-commits@vger.kernel.org Subject: [tip:x86/pti] x86/entry/32: Add debug code to check entry/exit CR3 Git-Commit-ID: 97193702c6d353e12aefc0fb2f73a98ca421cd56 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00, DATE_IN_FUTURE_96_Q autolearn=ham autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 97193702c6d353e12aefc0fb2f73a98ca421cd56 Gitweb: https://git.kernel.org/tip/97193702c6d353e12aefc0fb2f73a98ca421cd56 Author: Joerg Roedel AuthorDate: Wed, 18 Jul 2018 11:41:16 +0200 Committer: Thomas Gleixner CommitDate: Fri, 20 Jul 2018 01:11:49 +0200 x86/entry/32: Add debug code to check entry/exit CR3 Add code to check whether the kernel is entered and left with the correct CR3 and make it depend on CONFIG_DEBUG_ENTRY. This is needed because there is no NX protection of user-addresses in the kernel-CR3 on x86-32 and that type of bug would not be detected otherwise. Signed-off-by: Joerg Roedel Signed-off-by: Thomas Gleixner Tested-by: Pavel Machek Cc: "H . Peter Anvin" Cc: linux-mm@kvack.org Cc: Linus Torvalds Cc: Andy Lutomirski Cc: Dave Hansen Cc: Josh Poimboeuf Cc: Juergen Gross Cc: Peter Zijlstra Cc: Borislav Petkov Cc: Jiri Kosina Cc: Boris Ostrovsky Cc: Brian Gerst Cc: David Laight Cc: Denys Vlasenko Cc: Eduardo Valentin Cc: Greg KH Cc: Will Deacon Cc: aliguori@amazon.com Cc: daniel.gruss@iaik.tugraz.at Cc: hughd@google.com Cc: keescook@google.com Cc: Andrea Arcangeli Cc: Waiman Long Cc: "David H . Gutteridge" Cc: joro@8bytes.org Link: https://lkml.kernel.org/r/1531906876-13451-40-git-send-email-joro@8bytes.org --- arch/x86/entry/entry_32.S | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/arch/x86/entry/entry_32.S b/arch/x86/entry/entry_32.S index b1541c74c71a..010cdb41e3c7 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_DEBUG_ENTRY + 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 @@ -213,6 +231,8 @@ .macro SAVE_ALL_NMI cr3_reg:req SAVE_ALL + BUG_IF_WRONG_CR3 + /* * Now switch the CR3 when PTI is enabled. * @@ -224,6 +244,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 @@ -287,6 +308,8 @@ .Lswitched_\@: + BUG_IF_WRONG_CR3 + RESTORE_REGS pop=\pop .endm @@ -357,6 +380,8 @@ ALTERNATIVE "", "jmp .Lend_\@", X86_FEATURE_XENPV + BUG_IF_WRONG_CR3 + SWITCH_TO_KERNEL_CR3 scratch_reg=%eax /* @@ -799,6 +824,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 @@ -893,6 +919,7 @@ ENTRY(entry_SYSENTER_32) * whereas POPF does not.) */ btrl $X86_EFLAGS_IF_BIT, (%esp) + BUG_IF_WRONG_CR3 no_user_check=1 popfl popl %eax @@ -970,6 +997,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: @@ -983,6 +1012,7 @@ restore_all: restore_all_kernel: TRACE_IRQS_IRET PARANOID_EXIT_TO_KERNEL_MODE + BUG_IF_WRONG_CR3 RESTORE_REGS 4 jmp .Lirq_return @@ -990,6 +1020,19 @@ restore_all_kernel: ENTRY(iret_exc ) pushl $0 # no error code pushl $do_iret_error + +#ifdef CONFIG_DEBUG_ENTRY + /* + * 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)