From patchwork Wed Dec 6 12:35:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 120826 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp7000665qgn; Wed, 6 Dec 2017 04:37:27 -0800 (PST) X-Google-Smtp-Source: AGs4zMa00MslHuHr5fz4dF/zET2Nl8Dt3ku6f8aMC3aGb/4jWhNhgBksZdMDnQA6Yy798oc3SHhJ X-Received: by 10.84.128.76 with SMTP id 70mr21633326pla.171.1512563847820; Wed, 06 Dec 2017 04:37:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1512563847; cv=none; d=google.com; s=arc-20160816; b=SuEf/5Cej6CaTwYwAD8LF1T0yYcavwWPJXTJPpcvMLI4kWln5OssmWgE6ISJvqSAg0 vfAQKSSvXQBqUGVMC8nk0zHBKHpxy6BJuM1hEJMVY8PNiT5euyVuUVW4LQ8B1PdRe3Id 9JAvyS5LF/hR8A8keS1XEt+UzU4CPB0L33UaQKoBukzaSerH1nzTKeYXZFUl4DkSKwgf +bF690DYbrouvRtuEcTmgOxi4rpiTZVTArUKOy7vQCOVrwMw2ijHaSQSAO/XDAxf7Pz4 AQtEUVeHJBDLaa7t0hzKvWeeO7/o6oi3IcF9Et2wX2BgN4XmDY+KdYCKZB0OWAaRUemM Ekdg== 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:cc:to:from:arc-authentication-results; bh=vkivRbjPJxZ6U5AiBhzxetZ3KZKwpfh7wwCUoC1F3uQ=; b=OZPznLShZ7+O9LjTsewN3i8r8sIP1RzSysmR9gVxdKH6r0EANVH67yJqbktcWktu/E G7I6HEt/3Xe3DWIa8w8+Wp1WxWaIvi9SzL3MYbd7MsEYQwnV+BKPtVOTp7ujtjB06IqS UlXbZi6U9S3n4MSwhRz5OOyzfG5T0dDyqIQDYtE50GIvUWuj7mepiQ9E19W1IF6q+yXR iRzx4fwZGILDqs0MWRrpIPPwFjRTRd/zB+Hv3n9BH3jmutLrS6aTwL84R8RP5NBZGjQH AtM1krlehLcBLrET81g9HymHuL9v9MQAHz5brKVhqNJ3eks1/5NwqaW/8LuHAvADF/59 mnaA== 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 w3si1841951pge.245.2017.12.06.04.37.27; Wed, 06 Dec 2017 04:37:27 -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 S1752545AbdLFMhX (ORCPT + 28 others); Wed, 6 Dec 2017 07:37:23 -0500 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:34838 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752212AbdLFMgO (ORCPT ); Wed, 6 Dec 2017 07:36:14 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 569711AD7; Wed, 6 Dec 2017 04:36:12 -0800 (PST) Received: from edgewater-inn.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 282C83F5BD; Wed, 6 Dec 2017 04:36:12 -0800 (PST) Received: by edgewater-inn.cambridge.arm.com (Postfix, from userid 1000) id 4274F1AE3A0E; Wed, 6 Dec 2017 12:36:17 +0000 (GMT) From: Will Deacon To: linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org, catalin.marinas@arm.com, mark.rutland@arm.com, ard.biesheuvel@linaro.org, sboyd@codeaurora.org, dave.hansen@linux.intel.com, keescook@chromium.org, msalter@redhat.com, labbott@redhat.com, tglx@linutronix.de, Will Deacon Subject: [PATCH v3 20/20] arm64: kaslr: Put kernel vectors address in separate data page Date: Wed, 6 Dec 2017 12:35:39 +0000 Message-Id: <1512563739-25239-21-git-send-email-will.deacon@arm.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1512563739-25239-1-git-send-email-will.deacon@arm.com> References: <1512563739-25239-1-git-send-email-will.deacon@arm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The literal pool entry for identifying the vectors base is the only piece of information in the trampoline page that identifies the true location of the kernel. This patch moves it into its own page, which is only mapped by the full kernel page table, which protects against any accidental leakage of the trampoline contents. Suggested-by: Ard Biesheuvel Signed-off-by: Will Deacon --- arch/arm64/include/asm/fixmap.h | 1 + arch/arm64/kernel/entry.S | 11 +++++++++++ arch/arm64/kernel/vmlinux.lds.S | 35 ++++++++++++++++++++++++++++------- arch/arm64/mm/mmu.c | 10 +++++++++- 4 files changed, 49 insertions(+), 8 deletions(-) -- 2.1.4 diff --git a/arch/arm64/include/asm/fixmap.h b/arch/arm64/include/asm/fixmap.h index 8119b49be98d..ec1e6d6fa14c 100644 --- a/arch/arm64/include/asm/fixmap.h +++ b/arch/arm64/include/asm/fixmap.h @@ -59,6 +59,7 @@ enum fixed_addresses { #endif /* CONFIG_ACPI_APEI_GHES */ #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 + FIX_ENTRY_TRAMP_DATA, FIX_ENTRY_TRAMP_TEXT, #define TRAMP_VALIAS (__fix_to_virt(FIX_ENTRY_TRAMP_TEXT)) #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */ diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S index 3eabcb194c87..a70c6dd2cc19 100644 --- a/arch/arm64/kernel/entry.S +++ b/arch/arm64/kernel/entry.S @@ -1030,7 +1030,13 @@ alternative_else_nop_endif msr tpidrro_el0, x30 // Restored in kernel_ventry .endif tramp_map_kernel x30 +#ifdef CONFIG_RANDOMIZE_BASE + adr x30, tramp_vectors + PAGE_SIZE +alternative_insn isb, nop, ARM64_WORKAROUND_QCOM_FALKOR_E1003 + ldr x30, [x30] +#else ldr x30, =vectors +#endif prfm plil1strm, [x30, #(1b - tramp_vectors)] msr vbar_el1, x30 add x30, x30, #(1b - tramp_vectors) @@ -1073,6 +1079,11 @@ END(tramp_exit_compat) .ltorg .popsection // .entry.tramp.text +#ifdef CONFIG_RANDOMIZE_BASE + .pushsection ".entry.tramp.data", "a" // .entry.tramp.data + .quad vectors + .popsection // .entry.tramp.data +#endif /* CONFIG_RANDOMIZE_BASE */ #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */ /* diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S index 6b4260f22aab..976109b3ae51 100644 --- a/arch/arm64/kernel/vmlinux.lds.S +++ b/arch/arm64/kernel/vmlinux.lds.S @@ -58,15 +58,28 @@ jiffies = jiffies_64; #endif #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 -#define TRAMP_TEXT \ - . = ALIGN(PAGE_SIZE); \ - VMLINUX_SYMBOL(__entry_tramp_text_start) = .; \ - *(.entry.tramp.text) \ - . = ALIGN(PAGE_SIZE); \ +#define TRAMP_TEXT \ + . = ALIGN(PAGE_SIZE); \ + VMLINUX_SYMBOL(__entry_tramp_text_start) = .; \ + *(.entry.tramp.text) \ + . = ALIGN(PAGE_SIZE); \ VMLINUX_SYMBOL(__entry_tramp_text_end) = .; +#ifdef CONFIG_RANDOMIZE_BASE +#define TRAMP_DATA \ + .entry.tramp.data : { \ + . = ALIGN(PAGE_SIZE); \ + VMLINUX_SYMBOL(__entry_tramp_data_start) = .; \ + *(.entry.tramp.data) \ + . = ALIGN(PAGE_SIZE); \ + VMLINUX_SYMBOL(__entry_tramp_data_end) = .; \ + } +#else +#define TRAMP_DATA +#endif /* CONFIG_RANDOMIZE_BASE */ #else #define TRAMP_TEXT -#endif +#define TRAMP_DATA +#endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */ /* * The size of the PE/COFF section that covers the kernel image, which @@ -137,6 +150,7 @@ SECTIONS RO_DATA(PAGE_SIZE) /* everything from this point to */ EXCEPTION_TABLE(8) /* __init_begin will be marked RO NX */ NOTES + TRAMP_DATA . = ALIGN(SEGMENT_ALIGN); __init_begin = .; @@ -251,7 +265,14 @@ ASSERT(__idmap_text_end - (__idmap_text_start & ~(SZ_4K - 1)) <= SZ_4K, ASSERT(__hibernate_exit_text_end - (__hibernate_exit_text_start & ~(SZ_4K - 1)) <= SZ_4K, "Hibernate exit text too big or misaligned") #endif - +#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 +ASSERT((__entry_tramp_text_end - __entry_tramp_text_start) == PAGE_SIZE, + "Entry trampoline text too big") +#ifdef CONFIG_RANDOMIZE_BASE +ASSERT((__entry_tramp_data_end - __entry_tramp_data_start) == PAGE_SIZE, + "Entry trampoline data too big") +#endif +#endif /* * If padding is applied before .head.text, virt<->phys conversions will fail. */ diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c index fe68a48c64cb..916d9ced1c3f 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -541,8 +541,16 @@ static int __init map_entry_trampoline(void) __create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, PAGE_SIZE, prot, pgd_pgtable_alloc, 0); - /* ...as well as the kernel page table */ + /* 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)) { + extern char __entry_tramp_data_start[]; + + __set_fixmap(FIX_ENTRY_TRAMP_DATA, + __pa_symbol(__entry_tramp_data_start), + PAGE_KERNEL_RO); + } + return 0; } core_initcall(map_entry_trampoline);