From patchwork Tue May 29 04:38:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Yao X-Patchwork-Id: 10434727 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 37C78604D4 for ; Tue, 29 May 2018 09:59:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2A05C286B8 for ; Tue, 29 May 2018 09:59:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1EF2F286BA; Tue, 29 May 2018 09:59:59 +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=-5.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 45499286B8 for ; Tue, 29 May 2018 09:59:58 +0000 (UTC) Received: (qmail 32693 invoked by uid 550); 29 May 2018 09:59:41 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Delivered-To: moderator for kernel-hardening@lists.openwall.com Received: (qmail 19832 invoked from network); 29 May 2018 04:38:39 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=Uwm97qadTYfvXqUVS8BA7m42oQSdrPJvKD4autk3zTQ=; b=tlItpfLpnDxmV7Elv+2PWmTFTwQTj8WTcAH7KKp9s84sTA9r/6vDA/GU0g+3nbAdmN qPn8e90HoYgpSDOd6hkjpywgVLmYg9SwOAfjyiUUU1K0K6atyJsmHBVE1LQtFjuF9377 DR1NGcdxAFconoaLb7q88HhYOTD616eSZri1OxYmGcl347qcWBa66kbIbNzsjjFF082M rOiiSvV2gzFM6oYLNOcIn9kpx3JTufD15wSVFh5uiJ5KywqFkT4lWO9o7I7LH+/riywe 4/wod7JysZy7X2UUFN+AoYufgGaCYyyac+CVjETbKHMfISxKOZQJniLpTqR7uuityrB5 fwvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Uwm97qadTYfvXqUVS8BA7m42oQSdrPJvKD4autk3zTQ=; b=phSU+raPQsOfi+MYoGrtEF8L1DAyESsUra2Ml34ISvRZidUYm9XB29afi/fx/uGJkZ QL8jLxkJa9hWmX6SwtKm+qpfvMU2vYtFhic1H1VYcZTl961uamE6pL/FPlU4pLKg/82Q UwmdD6I+Qt5/IG12LPmStCdN0P3sqMv95qFPO5yChO7EUMdftDoyK7Y+hZVWT5cJK3Xm grvlIVE6u0Jl8KxzslHAFK++GdAYnTlAnT17PadDu3bYTFV8zWv7338uhiUx8FTV78SS U3TZAD8CDwbkp/hVTg7mWagueetG4d1ZV4/AFHaUKOQ3Ld+Nqemkezvgfw9PfuBQRVWX aSBg== X-Gm-Message-State: ALKqPwd9IvhhOVFHtZhVUI5F3SoLM/wI3BybZ0fkV7jvPNn3CfZD3PBS 4R9Bv5UZqsBj3wL4CtvA3pOqrA== X-Google-Smtp-Source: AB8JxZqoYNGxs/GIPjhqHFz6A7wOhrh5iIJPY2KAuHjCQXgBPLp8Ss1w0MxywYa1M7k3ZVFb4Z7Y5w== X-Received: by 2002:aa7:8201:: with SMTP id k1-v6mr15868356pfi.97.1527568707435; Mon, 28 May 2018 21:38:27 -0700 (PDT) From: YaoJun To: kernel-hardening@lists.openwall.com Cc: linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, jmorris@namei.org Subject: [PATCH 4/4] migrate swapper_pg_dir Date: Tue, 29 May 2018 12:38:20 +0800 Message-Id: <20180529043820.27948-1-yaojun8558363@gmail.com> X-Mailer: git-send-email 2.17.0 X-Virus-Scanned: ClamAV using ClamSMTP Migrate swapper_pg_dir and tramp_pg_dir. And its placement in the virtual address space does not correlate with the placement of the kernel. --- arch/arm64/mm/mmu.c | 67 ++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c index 26ba3e70a91c..0b1a77b8c757 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -55,6 +55,9 @@ u64 idmap_ptrs_per_pgd = PTRS_PER_PGD; u64 kimage_voffset __ro_after_init; EXPORT_SYMBOL(kimage_voffset); +#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 +pgd_t *new_tramp_pg_dir; +#endif phys_addr_t __pa_swapper_pg_dir; pgd_t *new_swapper_pg_dir = swapper_pg_dir; @@ -105,6 +108,25 @@ static phys_addr_t __init early_pgtable_alloc(void) return phys; } +#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 +static phys_addr_t __init early_pgtables_alloc(int num) +{ + int i; + phys_addr_t phys; + void *ptr; + + phys = memblock_alloc(PAGE_SIZE * num, PAGE_SIZE); + + for (i = 0; i < num; i++) { + ptr = pte_set_fixmap(phys + i * PAGE_SIZE); + memset(ptr, 0, PAGE_SIZE); + pte_clear_fixmap(); + } + + return phys; +} +#endif + static bool pgattr_change_is_safe(u64 old, u64 new) { /* @@ -554,6 +576,10 @@ static int __init map_entry_trampoline(void) __create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, PAGE_SIZE, prot, pgd_pgtable_alloc, 0); + memcpy(new_tramp_pg_dir, tramp_pg_dir, PGD_SIZE); + memblock_free(__pa_symbol(tramp_pg_dir), + __pa_symbol(swapper_pg_dir) - __pa_symbol(tramp_pg_dir)); + /* Map both the text and data into the kernel page table */ __set_fixmap(FIX_ENTRY_TRAMP_TEXT, pa_start, prot); if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { @@ -631,36 +657,33 @@ static void __init map_kernel(pgd_t *pgdp) */ void __init paging_init(void) { - phys_addr_t pgd_phys = early_pgtable_alloc(); - pgd_t *pgdp = pgd_set_fixmap(pgd_phys); + phys_addr_t pgd_phys; + pgd_t *pgdp; - __pa_swapper_pg_dir = __pa_symbol(swapper_pg_dir); +#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 + int pages; + pages = (__pa_symbol(swapper_pg_dir) - __pa_symbol(tramp_pg_dir) + + PAGE_SIZE) >> PAGE_SHIFT; + pgd_phys = early_pgtables_alloc(pages); + new_tramp_pg_dir = __va(pgd_phys); + __pa_swapper_pg_dir = pgd_phys + PAGE_SIZE; +#else + pgd_phys = early_pgtable_alloc(); + __pa_swapper_pg_dir = pgd_phys; +#endif + new_swapper_pg_dir = __va(__pa_swapper_pg_dir); + + pgdp = pgd_set_fixmap(__pa_swapper_pg_dir); map_kernel(pgdp); map_mem(pgdp); - /* - * We want to reuse the original swapper_pg_dir so we don't have to - * communicate the new address to non-coherent secondaries in - * secondary_entry, and so cpu_switch_mm can generate the address with - * adrp+add rather than a load from some global variable. - * - * To do this we need to go via a temporary pgd. - */ - cpu_replace_ttbr1(pgd_phys); - memcpy(swapper_pg_dir, pgdp, PGD_SIZE); cpu_replace_ttbr1(__pa_swapper_pg_dir); + init_mm.pgd = new_swapper_pg_dir; pgd_clear_fixmap(); - memblock_free(pgd_phys, PAGE_SIZE); - - /* - * We only reuse the PGD from the swapper_pg_dir, not the pud + pmd - * allocated with it. - */ - memblock_free(__pa_symbol(swapper_pg_dir) + PAGE_SIZE, - __pa_symbol(swapper_pg_end) - __pa_symbol(swapper_pg_dir) - - PAGE_SIZE); + memblock_free(__pa_symbol(swapper_pg_dir), + __pa_symbol(swapper_pg_end) - __pa_symbol(swapper_pg_dir)); } /*