Received: by 10.223.185.116 with SMTP id b49csp2372284wrg; Thu, 22 Feb 2018 12:38:53 -0800 (PST) X-Google-Smtp-Source: AH8x2247mRKO4e579F+YRfkdwYgRum4YacMDtcX97NQzGAmyDxff6JQisgRL5N4ru2KcZUdlwbK+ X-Received: by 2002:a17:902:8c97:: with SMTP id t23-v6mr531929plo.372.1519331933750; Thu, 22 Feb 2018 12:38:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519331933; cv=none; d=google.com; s=arc-20160816; b=iLWCvkNdtjTquyyz7h+oKThNlv/Up6q9laFJ83alJoK3vstOgQWsqByxW+0XEWjbHw rVIkh0dpz7X36eJQKj/btFHbUfMGpU4IFsGwwNKp/Gg/hKD+J0/Mlv2mVH8wzXIHzPe6 k0TTTcwGlCAef6cKYcg3cGYqaq15SHX0JQpa6C46XZgWXoAL1GNvYy010zjOLTMG8skP FrbXGcSsYDNrc8x7nXVBPqPSF9K18w1OMEkubtEg2iHUfimbYc6uqNz2Kv7qll03fqd/ 5h4herLE3c69IhbaLBLrRTJirFr3aePWsQaM/ZbJK6YMph7IeWyYp3rs9IBo3D2x9Ttu Fbfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:in-reply-to:references:date :from:cc:to:subject:arc-authentication-results; bh=2YpkgkV0QKa6nvelSls53m2tBd7TnfdUG775Cm7VYxQ=; b=RqVQ26aww4C7LLhtj4fxDjivtLbnHAmQ7Kp2THX341CYFSPT3s1fAaelgvWmwPfjie v73LWVeVpJBYrtem5TXdKZhGQHJrS5FkEw3WopjDwpVW1vy7HaEILG9cnFp6gTxv9qJi smRSa2gTPj/wChZzAG8rZ+YIHNightOdpjDXKOOf1dr6N1/0vixvd+ap+HcSN0gJsCzU catU/U7d5ixhDFFVhg44JqI/GWYUDsXPpSdyphFU2aUkFK1cSkJd2wTQ8UZunGJzMADU 8UA5KejDKMTZJdy+esmFRYNZl1ALmcY7/H7FW9Qg6h2m+hSzv6iyRpTTMye+Dt97gfga V0ZQ== 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 z16si471274pge.522.2018.02.22.12.38.38; Thu, 22 Feb 2018 12:38:53 -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; 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 S1751481AbeBVUhS (ORCPT + 99 others); Thu, 22 Feb 2018 15:37:18 -0500 Received: from mga12.intel.com ([192.55.52.136]:15124 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751411AbeBVUhP (ORCPT ); Thu, 22 Feb 2018 15:37:15 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 22 Feb 2018 12:37:15 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.47,378,1515484800"; d="scan'208";a="33407697" Received: from viggo.jf.intel.com (HELO localhost.localdomain) ([10.54.39.119]) by orsmga001.jf.intel.com with ESMTP; 22 Feb 2018 12:37:14 -0800 Subject: [RFC][PATCH 10/10] x86/pti: clear _PAGE_GLOBAL for kernel image To: linux-kernel@vger.kernel.org Cc: Dave Hansen , aarcange@redhat.com, luto@kernel.org, torvalds@linux-foundation.org, keescook@google.com, hughd@google.com, jgross@suse.com, x86@kernel.org, namit@vmware.com From: Dave Hansen Date: Thu, 22 Feb 2018 12:37:07 -0800 References: <20180222203651.B776810C@viggo.jf.intel.com> In-Reply-To: <20180222203651.B776810C@viggo.jf.intel.com> Message-Id: <20180222203707.157CF647@viggo.jf.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dave Hansen The kernel page tables are inherited from head_64.S which rudely marks them as _PAGE_GLOBAL. For PTI, we have been relying on the grace of $DEITY and some insane behavior in pageattr.c. Now we do it properly. First, stop filtering out "unsupported" bits from being cleared in the pageattr code. It's fine to filter out *setting* these bits but it is insane to keep us from clearing them. Then, *explicitly* go clear _PAGE_GLOBAL from the kernel identity map. Do not rely on pageattr to do it magically. After this patch, we can see that "GLB" shows up in each copy of the page tables, that we have the same number of global entries in each and that they are the *same* entries. # grep -c GLB /sys/kernel/debug/page_tables/* /sys/kernel/debug/page_tables/current_kernel:11 /sys/kernel/debug/page_tables/current_user:11 /sys/kernel/debug/page_tables/kernel:11 # for f in `ls /sys/kernel/debug/page_tables/`; do grep GLB /sys/kernel/debug/page_tables/$f > $f.GLB; done # md5sum *.GLB 9caae8ad6a1fb53aca2407ec037f612d current_kernel.GLB 9caae8ad6a1fb53aca2407ec037f612d current_user.GLB 9caae8ad6a1fb53aca2407ec037f612d kernel.GLB A quick visual audit also shows that all the entries make sense. 0xfffffe0000000000 is the cpu_entry_area and 0xffffffff81c00000 is the entry/exit text: # grep -c GLB /sys/kernel/debug/page_tables/current_user 0xfffffe0000000000-0xfffffe0000002000 8K ro GLB NX pte 0xfffffe0000002000-0xfffffe0000003000 4K RW GLB NX pte 0xfffffe0000003000-0xfffffe0000006000 12K ro GLB NX pte 0xfffffe0000006000-0xfffffe0000007000 4K ro GLB x pte 0xfffffe0000007000-0xfffffe000000d000 24K RW GLB NX pte 0xfffffe000002d000-0xfffffe000002e000 4K ro GLB NX pte 0xfffffe000002e000-0xfffffe000002f000 4K RW GLB NX pte 0xfffffe000002f000-0xfffffe0000032000 12K ro GLB NX pte 0xfffffe0000032000-0xfffffe0000033000 4K ro GLB x pte 0xfffffe0000033000-0xfffffe0000039000 24K RW GLB NX pte 0xffffffff81c00000-0xffffffff81e00000 2M ro PSE GLB x pmd Signed-off-by: Dave Hansen Cc: Andrea Arcangeli Cc: Andy Lutomirski Cc: Linus Torvalds Cc: Kees Cook Cc: Hugh Dickins Cc: Juergen Gross Cc: x86@kernel.org Cc: Nadav Amit --- b/arch/x86/mm/init.c | 8 +------- b/arch/x86/mm/pageattr.c | 12 +++++++++--- b/arch/x86/mm/pti.c | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) diff -puN arch/x86/mm/init.c~clear-global-for-pti arch/x86/mm/init.c --- a/arch/x86/mm/init.c~clear-global-for-pti 2018-02-22 12:36:22.627036544 -0800 +++ b/arch/x86/mm/init.c 2018-02-22 12:36:22.634036544 -0800 @@ -161,12 +161,6 @@ struct map_range { static int page_size_mask; -static void enable_global_pages(void) -{ - if (!static_cpu_has(X86_FEATURE_PTI)) - __supported_pte_mask |= _PAGE_GLOBAL; -} - static void __init probe_page_size_mask(void) { /* @@ -187,7 +181,7 @@ static void __init probe_page_size_mask( __supported_pte_mask &= ~_PAGE_GLOBAL; if (boot_cpu_has(X86_FEATURE_PGE)) { cr4_set_bits_and_update_boot(X86_CR4_PGE); - enable_global_pages(); + __supported_pte_mask |= _PAGE_GLOBAL; } /* By the default is everything supported: */ diff -puN arch/x86/mm/pageattr.c~clear-global-for-pti arch/x86/mm/pageattr.c --- a/arch/x86/mm/pageattr.c~clear-global-for-pti 2018-02-22 12:36:22.629036544 -0800 +++ b/arch/x86/mm/pageattr.c 2018-02-22 12:36:22.635036544 -0800 @@ -1411,11 +1411,11 @@ static int change_page_attr_set_clr(unsi memset(&cpa, 0, sizeof(cpa)); /* - * Check, if we are requested to change a not supported - * feature: + * Check, if we are requested to set a not supported + * feature. Clearing non-supported features is OK. */ mask_set = canon_pgprot(mask_set); - mask_clr = canon_pgprot(mask_clr); + if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split) return 0; @@ -1758,6 +1758,12 @@ int set_memory_4k(unsigned long addr, in __pgprot(0), 1, 0, NULL); } +int set_memory_nonglobal(unsigned long addr, int numpages) +{ + return change_page_attr_clear(&addr, numpages, + __pgprot(_PAGE_GLOBAL), 0); +} + static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc) { struct cpa_data cpa; diff -puN arch/x86/mm/pti.c~clear-global-for-pti arch/x86/mm/pti.c --- a/arch/x86/mm/pti.c~clear-global-for-pti 2018-02-22 12:36:22.631036544 -0800 +++ b/arch/x86/mm/pti.c 2018-02-22 12:36:22.635036544 -0800 @@ -359,6 +359,28 @@ static void __init pti_clone_entry_text( } /* + * This is the only user for it and it is not arch-generic like + * the other set_memory.h functions. Just extern it. + */ +extern int set_memory_nonglobal(unsigned long addr, int numpages); +void pti_set_kernel_image_nonglobal(void) +{ + /* + * The identity map is created with PMDs, regardless of the + * actual length of the kernel. We need to clear + * _PAGE_GLOBAL up to a PMD boundary, not just to the end + * of the image. + */ + unsigned long start = PFN_ALIGN(_text); + unsigned long end = ALIGN((unsigned long)_end, PMD_PAGE_SIZE); + + pr_debug("x86/pti: Set kernel image: %lx - %lx non-global\n", + start, end); + + set_memory_nonglobal(start, (end - start) >> PAGE_SHIFT); +} + +/* * Initialize kernel page table isolation */ void __init pti_init(void) @@ -369,6 +391,10 @@ void __init pti_init(void) pr_info("enabled\n"); pti_clone_user_shared(); + + /* Undo all global bits from the init pagetables in head_64.S: */ + pti_set_kernel_image_nonglobal(); + /* Replace some of the global bits just for shared entry text: */ pti_clone_entry_text(); pti_setup_espfix64(); pti_setup_vsyscall(); _