From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Google-Smtp-Source: AG47ELvASc+GGWyruAE5dQjJKCMbzoGM2ylj58nGBt923kA9AYK5LBAqqGj0B6KloNdnj9ZI+T3j ARC-Seal: i=1; a=rsa-sha256; t=1521228612; cv=none; d=google.com; s=arc-20160816; b=Vf5r3EW+SaRXwJdD387VvW2F6SQmkhzyru/v6ra/1C3Pa6KMo67AewtyI+oJd2EPSF O/KqkWuZF6ToZN+CJm5VDkKDg+EB1kisTJLlPlfdHpmofUrklz7rafVlS8ov9gKpHMRq paXdFMsMY+bswXLhOG3eLWJ+45a3P+9trt/fF04w1Xxdoc+YZv9qjugK0T67AK5HdVCa iesIp9tcmpt/t9V9MGVBTtPcS0/RtlYnd9PFJ/SIEFmP/29/zu8HAd/CnTLgcPDogyfG mXNzEZ9v0KoQHRljho2Mzb6Ven7/Eww8JwIYwEme6J5/kA5JM5eB2OEnTWCKfwwYZU9S b58g== 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 :dkim-signature:arc-authentication-results; bh=xoM86xzg3gGTugfj4TH0x+xiIRDXWwChn6H5Pe0NE64=; b=ROzj38yq5qzHucR1GIDEdGxBrK9rdXsXFW0Yk7fJpM4bAGQ+SuwUyk81e+rnrSLZvQ UJLToQAOVs/u/5djIxuqtX49sxCV3yDG2FHKUS2yzUSTy9AxCTlyMCTZm0YOGHzGM5eq TWgpsKsDdB/1dy61qAUGuYZnQD7FQqcoDONrtlzzwtDymyTDNcVNxrzqn1EUuDf8q5Aq yftQZ7jKYHMVdae4WczUq+fFSVX+sWVaVs4wmIaujvcjoSFgtPBT5bvsRRQQNn7+Ncd+ eAO2EXwcFuElnZT3hdITWY0J6xqmnq4d7PYCA+ttYg4uAYCWpTeYrm9pp2Hnqyp4Oz+8 g/2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@8bytes.org header.s=mail-1 header.b=gMKIs3LF; spf=pass (google.com: domain of joro@8bytes.org designates 81.169.241.247 as permitted sender) smtp.mailfrom=joro@8bytes.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@8bytes.org header.s=mail-1 header.b=gMKIs3LF; spf=pass (google.com: domain of joro@8bytes.org designates 81.169.241.247 as permitted sender) smtp.mailfrom=joro@8bytes.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=8bytes.org 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 , jroedel@suse.de, joro@8bytes.org Subject: [PATCH 35/35] x86/entry/32: Add debug code to check entry/exit cr3 Date: Fri, 16 Mar 2018 20:29:53 +0100 Message-Id: <1521228593-3820-36-git-send-email-joro@8bytes.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1521228593-3820-1-git-send-email-joro@8bytes.org> References: <1521228593-3820-1-git-send-email-joro@8bytes.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: =?utf-8?q?1595123813310519355?= X-GMAIL-MSGID: =?utf-8?q?1595123813310519355?= X-Mailing-List: linux-kernel@vger.kernel.org List-ID: 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 672441c..b8a772c 100644 --- a/arch/x86/Kconfig.debug +++ b/arch/x86/Kconfig.debug @@ -345,6 +345,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 0250b79..ea19bcf 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 /* @@ -804,6 +829,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 @@ -898,6 +924,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 @@ -975,6 +1002,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_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