From patchwork Wed Jul 18 09:41:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joerg Roedel X-Patchwork-Id: 10531925 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 398B3602CA for ; Wed, 18 Jul 2018 09:43:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 292F228FCD for ; Wed, 18 Jul 2018 09:43:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1D8A328FD4; Wed, 18 Jul 2018 09:43:45 +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=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable 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 A175F28FBC for ; Wed, 18 Jul 2018 09:43:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CAF246B02B1; Wed, 18 Jul 2018 05:41:44 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BE5476B02B2; Wed, 18 Jul 2018 05:41:44 -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 9BEC46B02B3; Wed, 18 Jul 2018 05:41:44 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by kanga.kvack.org (Postfix) with ESMTP id 3A6166B02B2 for ; Wed, 18 Jul 2018 05:41:44 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id d30-v6so1705529edd.0 for ; Wed, 18 Jul 2018 02:41:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=Bg/eCd/UereHKc7xI2vYykGYgiQmohcR4iekioPNYuY=; b=oxddd9EK3Wfg42fHlodIisyIAIQGprtTVy7ZdwSm2JTKm9zr80aSfNYYFF0r+j8uxq VPzuV2wfNIDtYKoaE0CDBgQU6cPFONcZCYs4oAmTakbfFIxSbC6oJFAK8hMgZ/kGaWvs ORzfyF+q0OVpkZOmGsfFMcCjLXkdEvxCSRt7/wjF33ZvTEzppchJC0eR75GnT1BIvOJz 3EXdSWy1pZBNOLYUVV6eFym+iw4i7PbaBg4DHqBjvXWqju4XJVq7jnrDco3ETrTW0kI0 Ng6flSS4EloUajlgkvO1eo6wb8Ifqe51s3GxYdMY9gOg3Ww7sLb9BpUz+veLj3lcdF5T bodA== X-Gm-Message-State: AOUpUlHZleeLVO8wQoEmsbs1XPfznleEX4qiRdeCRbk/SEKynapqYFlo WlleKzsqocqC+PW7Enrx3ZnlZDJBFDLlzsGchjA8k/dpkfeEwlGNZdyIxfndTxROgXqUzylL091 mqsR+UMRCD7gXuOs96MGiDv/bOYOe2R5Bvxxmy+bLaQeCBPvQNuDw6AnNAr+FQBiUFw== X-Received: by 2002:a50:c986:: with SMTP id w6-v6mr6268867edh.222.1531906903775; Wed, 18 Jul 2018 02:41:43 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfsA2hxoBnTkIcg0aqfK40Z11b06+ZHzxgexHf+BUOeucwABXAe/nkJN9/r150RXNv+kwh+ X-Received: by 2002:a50:c986:: with SMTP id w6-v6mr6268824edh.222.1531906903075; Wed, 18 Jul 2018 02:41:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531906903; cv=none; d=google.com; s=arc-20160816; b=j88qp36XtdLtazb5z7CyHJT5mnqVpDiFIcFSP49l0T0+NN0HuF54Rh3EiqbNNeLhlI IhATC2VdUN0ukOzI3ddJWxh2xSRMHmrX2AslfCivxUsN8vZ8rEvDVV7Y6DrXQzvR1YAS x4sjveH50OzbNCFhB7oMXSuglXZjUTjTneTAqfzqvizbzkVo73y8yDmZqiSMETmFvfud yDNtkc1KYmZVH7AHodoFpPSiUCV/pv+4qouBaAFryeHcY7DrUF2iYPXGh0twMUgh7N1a CnMZiHZDJAldMMf4atVkbQhsXfXJZCvcrQxsQjrl6n3tML6Z8K91Firo2Gs6LQQCLPJc dOlQ== 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=Bg/eCd/UereHKc7xI2vYykGYgiQmohcR4iekioPNYuY=; b=iw0UztfMm7F2X+oaQkmYnHcIlR0auvFdFRAF8ooyb5zuBIC7j7yuR7Q8q13Hans+3J GYFHMO4ffe22GUihy0gA7He5kvldP6OXtKTiatzFZfGHj+AAZmX1U8NaqQ9uymMdGVby MY083pdG/sVEAot5tWXS1ZryVHpSNvdCBF62USSjMrg23S93jPvWPpLOlnVB2mUWvV3/ YyVp8MB8K4PuGZKzr+8FGyVEIN12QaNlKpRKXVfHePVcnAl888+ogw9htCsmv6u9Js4w Q/tXEij+G3mW6kuNiyZi/kM82Snh2Fnbe5JwuN9rzW0lDqyr8SY9e9nxWvlM/pjq7AJu OYSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@8bytes.org header.s=mail-1 header.b=UhySdUdQ; spf=pass (google.com: domain of joro@8bytes.org designates 2a01:238:4383:600:38bc:a715:4b6d:a889 as permitted sender) smtp.mailfrom=joro@8bytes.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: from theia.8bytes.org (8bytes.org. [2a01:238:4383:600:38bc:a715:4b6d:a889]) by mx.google.com with ESMTPS id f33-v6si1037678edd.346.2018.07.18.02.41.42 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Jul 2018 02:41:43 -0700 (PDT) Received-SPF: pass (google.com: domain of joro@8bytes.org designates 2a01:238:4383:600:38bc:a715:4b6d:a889 as permitted sender) client-ip=2a01:238:4383:600:38bc:a715:4b6d:a889; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@8bytes.org header.s=mail-1 header.b=UhySdUdQ; spf=pass (google.com: domain of joro@8bytes.org designates 2a01:238:4383:600:38bc:a715:4b6d:a889 as permitted sender) smtp.mailfrom=joro@8bytes.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: by theia.8bytes.org (Postfix, from userid 1000) id DFB7C3C8E; Wed, 18 Jul 2018 11:41:26 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=8bytes.org; s=mail-1; t=1531906887; bh=MhPUX6wbhHnJzTrKEUtYXPXwMU2vHuBPvNMwNILcEDA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UhySdUdQ2DChsDZW8vGhmCTg4ARPdWi49apEk0GbA/5LLVe/RFKEyBlYQ/KjvE1YV wkUZ2minFAJXTYIKeUL5gxHF7H2bTvP/CCT8kaENLLNfe1t9sZdq12yvQoNTYz4lud v1lshAG5es1GOc1iFEnUzLvUodFKjg9RfdJHwklTwVT9LxDUNTlag3yuYBBtrsSj8r ZulJQCoVM5HikyLRSglAAXjEg1ECJGiUSQha3BzO69VrzJESTXnwB3Gh7JDfyMnqqI 9FNJJj/Z6Tftf3yX8O4uSQAie47ZT3E33+Ib2Rs9WlF95RtZ2s9a/ufyBo0TUvXkZl PavcP+yNyRP4g== 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 39/39] x86/entry/32: Add debug code to check entry/exit cr3 Date: Wed, 18 Jul 2018 11:41:16 +0200 Message-Id: <1531906876-13451-40-git-send-email-joro@8bytes.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1531906876-13451-1-git-send-email-joro@8bytes.org> References: <1531906876-13451-1-git-send-email-joro@8bytes.org> 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 From: Joerg Roedel Add code to check whether we enter and leave the kernel with the correct cr3 and make it depend on CONFIG_DEBUG_ENTRY. 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/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 b1541c7..010cdb4 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)