Received: by 10.223.185.116 with SMTP id b49csp3203591wrg; Mon, 5 Mar 2018 16:28:26 -0800 (PST) X-Google-Smtp-Source: AG47ELtzf2dVR0ZLeolIUurGRUbwCLSEDZH1W4tMmIUsEgBIASUJqrgrTXrkl++Alehp9DepUchN X-Received: by 2002:a17:902:501:: with SMTP id 1-v6mr14766989plf.283.1520296106461; Mon, 05 Mar 2018 16:28:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520296106; cv=none; d=google.com; s=arc-20160816; b=SJT9DSpZ7Vu2/kdk0xvBKetV3tu7j7jnO6auKTa6ar5n7Zx57HXDiaAyxaC5pk0wcG mprlHOHn5xDbnvPCkkUf32LYZXQiFyyTu9UykV2h2TkRgbaRsTPHJ8CZ/VFf6/fGaxan dq5bpm5zG0oLbNDD/26GqaI/fneQZgVmzarDXbKeY3vDPmaV3riwpKhWX3CeGYPuuLiv oS8wlk+bnRPdqRuwxpKqa/JR8enFPWwlgyBPL0KPvTgH5+bM9LyoOSRknSPAH/FRIWMg E7oR/RygoDM9hyzhLpR96UINkMduG5qXOKa42pIKLkSC3GDVhJyYGUVMtVVyYoGg/yf1 KRsg== 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:to:from:dkim-signature:arc-authentication-results; bh=Gsan5jcrZEXiIwOa3d7zS8zuvKw9MOGaBuVME/FjE+c=; b=hweQydAxr5GS44w33Sh9LkcaY7jDGb3fmydjv5aUzYNs3YTTBa5xThVmLell13H4LP IduBkusS+iMc2Z+wMBsXxp7cuURcltyK64FyBD7JYZpewtYvV/vujMx09PHt5jpVCGZq Ry2fHHnolre3yFUHWyvFg9t/XwlO/JOLIyRSlHhQSfV0JmBg1Fm2IIJKSRJxjF9zcMY8 Pt7c+xQJ7uix/E2j23HETrgZnel1xzDXa/pcfxyGtDKT/s2Go421xwoo9JiWItlNEyDs /HKXG2kRQJ4bHH5yE0y1V2C6XNO5iD0BwU6U/inVWYAIQRGcoejTfBaGzoWEgTlbDwEZ Eb3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=ucf2i9ZZ; 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=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c5-v6si9996117plr.684.2018.03.05.16.28.12; Mon, 05 Mar 2018 16:28:26 -0800 (PST) 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=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=ucf2i9ZZ; 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=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933580AbeCFA1G (ORCPT + 99 others); Mon, 5 Mar 2018 19:27:06 -0500 Received: from aserp2120.oracle.com ([141.146.126.78]:56728 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933324AbeCFA1D (ORCPT ); Mon, 5 Mar 2018 19:27:03 -0500 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w260QxHS038156; Tue, 6 Mar 2018 00:26:59 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : subject : date : message-id : in-reply-to : references; s=corp-2017-10-26; bh=Gsan5jcrZEXiIwOa3d7zS8zuvKw9MOGaBuVME/FjE+c=; b=ucf2i9ZZIar0j908WmP4o2DBNqeujcpbOFSU7E3hcyzUxwU63cDB9wfgeGbFlBHeJ+y6 kip4MtLIDVWaUBYyUupv3cQzT8lHH5ZoplgvhgrXhLQ44wZQZKdjgX0NZMDYN43QqYYk 8k5JiwlurXzVmwDsU8DKq7zdWGgkivaTfqB/lpx00FfUSjl6+QmBNpmOS2VHtrtqgqO7 InUN28QSKAt9INtA8Pelklyz5a3FvVmrhlo1JKW+KMaDXTCD1RFPEAnxaRC4zJAZyBKo aWBMYzLez1eAc1GXdxcKUECExPCflzCAsBAVGFZf8C2wz8XNpIbhOd0OCWV1W9vwYgsR 2g== Received: from userv0021.oracle.com (userv0021.oracle.com [156.151.31.71]) by aserp2120.oracle.com with ESMTP id 2ghe3kgg53-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 06 Mar 2018 00:26:59 +0000 Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by userv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w260QwVE026800 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 6 Mar 2018 00:26:58 GMT Received: from abhmp0008.oracle.com (abhmp0008.oracle.com [141.146.116.14]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w260QvPE029279; Tue, 6 Mar 2018 00:26:57 GMT Received: from localhost.localdomain (/98.216.35.41) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 05 Mar 2018 16:26:57 -0800 From: Pavel Tatashin To: steven.sistare@oracle.com, daniel.m.jordan@oracle.com, linux-kernel@vger.kernel.org, Alexander.Levin@microsoft.com, dan.j.williams@intel.com, sathyanarayanan.kuppuswamy@intel.com, pankaj.laxminarayan.bharadiya@intel.com, akuster@mvista.com, cminyard@mvista.com, pasha.tatashin@oracle.com, gregkh@linuxfoundation.org, stable@vger.kernel.org Subject: [PATCH 4.1 65/65] x86/pti/efi: broken conversion from efi to kernel page table Date: Mon, 5 Mar 2018 19:25:38 -0500 Message-Id: <20180306002538.1761-66-pasha.tatashin@oracle.com> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180306002538.1761-1-pasha.tatashin@oracle.com> References: <20180306002538.1761-1-pasha.tatashin@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8823 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1711220000 definitions=main-1803060005 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In entry_64.S we have code like this: /* Unconditionally use kernel CR3 for do_nmi() */ /* %rax is saved above, so OK to clobber here */ ALTERNATIVE "jmp 2f", "movq %cr3, %rax", X86_FEATURE_KAISER /* If PCID enabled, NOFLUSH now and NOFLUSH on return */ ALTERNATIVE "", "bts $63, %rax", X86_FEATURE_PCID pushq %rax /* mask off "user" bit of pgd address and 12 PCID bits: */ andq $(~(X86_CR3_PCID_ASID_MASK | KAISER_SHADOW_PGD_OFFSET)), %rax movq %rax, %cr3 2: /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */ call do_nmi With this instruction: andq $(~(X86_CR3_PCID_ASID_MASK | KAISER_SHADOW_PGD_OFFSET)), %rax We unconditionally switch from whatever our CR3 was to kernel page table. But, in arch/x86/platform/efi/efi_64.c We temporarily set a different page table, that does not have the kernel page table with 0x1000 offset from it. Look in efi_thunk() and efi_thunk_set_virtual_address_map(). So, while CR3 points to the other page table, we get an NMI interrupt, and clear 0x1000 from CR3, resulting in a bogus CR3 if the 0x1000 bit was set. The efi page table comes from realmode/rm/trampoline_64.S: arch/x86/realmode/rm/trampoline_64.S 141 .bss 142 .balign PAGE_SIZE 143 GLOBAL(trampoline_pgd) .space PAGE_SIZE Notice: alignment is PAGE_SIZE, so after applying KAISER_SHADOW_PGD_OFFSET which equal to PAGE_SIZE, we can get a different page table. But, even if we fix alignment, here the trampoline binary is later copied into dynamically allocated memory in reserve_real_mode(), so we need to fix that place as well. Fixes: 8a43ddfb93a0 ("KAISER: Kernel Address Isolation") Signed-off-by: Pavel Tatashin Reviewed-by: Steven Sistare Signed-off-by: Greg Kroah-Hartman (cherry picked from commit 7ec5d87df34a90758cf2aaf6824bb748454a8f35) Signed-off-by: Pavel Tatashin --- arch/x86/include/asm/kaiser.h | 10 ++++++++++ arch/x86/realmode/init.c | 4 +++- arch/x86/realmode/rm/trampoline_64.S | 3 ++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kaiser.h b/arch/x86/include/asm/kaiser.h index 5d85ddf26166..620619d8de57 100644 --- a/arch/x86/include/asm/kaiser.h +++ b/arch/x86/include/asm/kaiser.h @@ -19,6 +19,16 @@ #define KAISER_SHADOW_PGD_OFFSET 0x1000 +#ifdef CONFIG_PAGE_TABLE_ISOLATION +/* + * A page table address must have this alignment to stay the same when + * KAISER_SHADOW_PGD_OFFSET mask is applied + */ +#define KAISER_KERNEL_PGD_ALIGNMENT (KAISER_SHADOW_PGD_OFFSET << 1) +#else +#define KAISER_KERNEL_PGD_ALIGNMENT PAGE_SIZE +#endif + #ifdef __ASSEMBLY__ #ifdef CONFIG_PAGE_TABLE_ISOLATION diff --git a/arch/x86/realmode/init.c b/arch/x86/realmode/init.c index 0b7a63d98440..805a3271a137 100644 --- a/arch/x86/realmode/init.c +++ b/arch/x86/realmode/init.c @@ -4,6 +4,7 @@ #include #include #include +#include struct real_mode_header *real_mode_header; u32 *trampoline_cr4_features; @@ -15,7 +16,8 @@ void __init reserve_real_mode(void) size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob); /* Has to be under 1M so we can execute real-mode AP code. */ - mem = memblock_find_in_range(0, 1<<20, size, PAGE_SIZE); + mem = memblock_find_in_range(0, 1 << 20, size, + KAISER_KERNEL_PGD_ALIGNMENT); if (!mem) panic("Cannot allocate trampoline\n"); diff --git a/arch/x86/realmode/rm/trampoline_64.S b/arch/x86/realmode/rm/trampoline_64.S index dac7b20d2f9d..781cca63f795 100644 --- a/arch/x86/realmode/rm/trampoline_64.S +++ b/arch/x86/realmode/rm/trampoline_64.S @@ -30,6 +30,7 @@ #include #include #include +#include #include "realmode.h" .text @@ -139,7 +140,7 @@ tr_gdt: tr_gdt_end: .bss - .balign PAGE_SIZE + .balign KAISER_KERNEL_PGD_ALIGNMENT GLOBAL(trampoline_pgd) .space PAGE_SIZE .balign 8 -- 2.16.2