From patchwork Fri Apr 29 13:13:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 12831999 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9D11AC433F5 for ; Fri, 29 Apr 2022 13:15:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=MJ+E5/1hRnbMM8cmDRooNkU6BGqIqBoBiKZLJID2K3s=; b=eZDLHt03WlHvug PV0palvWu6fRH03TONVdY/+zrT2uudYhxTp+vHv+ksckbIwjW9NnMy3EFYGjYG6FjvaZRSyHzICOx sxa7l2NUmlHb4eiukllN6uTLIjLmCpqD8Vd2mBP8+MnS75zdUOPPcFW2ODTCpjoO5h16JIc2uN7Yu zFrv/l97MoiNpU0OWEPhphZeeLq7kiw2pmsBa8BBys6o5iZVbn7Yc61LAliN1bF0gYKFqC7fMMlAy 75i/RaNF+a773m+bqRE8hdmJ2WXAsVnrIDxVKi4n1qGxwg4NaITOnq/JiLo6C3BfsM6DzqsXSpEda cMzy9hEeRB+Z908pKXew==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nkQRr-00BKKL-Af; Fri, 29 Apr 2022 13:14:15 +0000 Received: from ams.source.kernel.org ([145.40.68.75]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nkQRd-00BKIP-FQ for linux-arm-kernel@lists.infradead.org; Fri, 29 Apr 2022 13:14:03 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 37DAFB8350B; Fri, 29 Apr 2022 13:14:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 90768C385B1; Fri, 29 Apr 2022 13:13:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651238038; bh=F+6df/dSYYQQyN87qkGkrNYCl5hTwNhMk899VGHKEXE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HuxWdAwSMuVWe2aYf6pbLEdXc0dHog/aOGCD8gnRon+mAV9yU5NqE1I0erLuk48xY /iqQ9E+XyGSVk9xuNd6j2A2X/t7k4Rme8D69J8xoltKv5kuH+QsodgMe/Oe5PyQSXC R1uwm2cAHSkk7/p/MKiRunosLo5Lcu+KebNxgvZdF3JNXof/sCEnFIwoBcsB7G0KrY fcnrhXTTCdrGCVJtq8PrIgaDJgXId8EXHjFb43JIxfK2bIrD5Jw34nZpgiEMLjmVHk 1p/jZLw+94VzSiI6qT5s/XEH67PWSfc5uDy4RcPHg+Ci99QKoMGD0H79KbVb8Oi7Kd IGtu/PZyNQT8A== From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: catalin.marinas@arm.com, will@kernel.org, maz@kernel.org, mark.rutland@arm.com, Ard Biesheuvel Subject: [PATCH 1/2] arm64: lds: move special code sections out of kernel exec segment Date: Fri, 29 Apr 2022 15:13:46 +0200 Message-Id: <20220429131347.3621090-2-ardb@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220429131347.3621090-1-ardb@kernel.org> References: <20220429131347.3621090-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3357; h=from:subject; bh=F+6df/dSYYQQyN87qkGkrNYCl5hTwNhMk899VGHKEXE=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBia+SJhmnaDDev+KwmVae6lERhCZGEpzXDSjGEZ7bq w0+iPo+JAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYmvkiQAKCRDDTyI5ktmPJBzdC/ 93TEVqr4SSIRWy5TchEsDWwKhsLKtFYqMhSG6dSpNKQO3B5t3C/3IH+jo5pILgk/O6kxryq44A5frZ aAWb2O3/8GSk5+9Nxg+FjAuQOeWnqmNxyWuhJUijYxlXClcGRQEXYyDGE9NpP7g6NO8fjYGemZGmDu +Fn4EtsQe+FAgPvTDFNp8K+9JFLE8IOrPFkvk7W3C+cHsZ6iStR137WjZ5kBkPkESsNCprLpsI4Jua VcAdVg8RT+AFKb401oKSK5ChS19FqXy1LJizZFCflGOAJ77eFOaEf3oPutPO5g6A1aLzKWtn1fh/qm W88Rc7otktEnHv4F4so800WYwbMN8ut1OeVtJ9WbIi/HGKt+EO9TrbSn25xsndNyOuBfb+DG6bb/7U 1Y2FPLKrM+QXqxppdL8ZjFV3MqhL4Y4ZpLupkZly/bbfyazT2DeLVRHttTKei52ailhp0SarciDv9E dOf58sA7mCPo2ROVAQ9fnuVCMsuDGHDqsxNW9zdKRhQ5Y= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220429_061401_832219_738A0CB1 X-CRM114-Status: GOOD ( 14.30 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org There are a few code sections that are emitted into the kernel's executable .text segment simply because they contain code, but are actually never executed via this mapping, so they can happily live in a region that gets mapped without executable permissions, reducing the risk of being gadgetized. Note that the kexec and hibernate region contents are always copied into a fresh page, and so there is no need to align them as long as the overall size of each is below 4 KiB. Signed-off-by: Ard Biesheuvel --- The EL1 and HYP ID maps could receive a similar treatment, but this requires slightly more work, which I will cover in my boot mapping cleanup series. The HYP text section contains branches covered by jump labels, and moving it out of [_stext.._etext] makes the core jump label code very unhappy so moving that one out is left for a future effort. arch/arm64/kernel/vmlinux.lds.S | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S index edaf0faf766f..cf594c6a7b84 100644 --- a/arch/arm64/kernel/vmlinux.lds.S +++ b/arch/arm64/kernel/vmlinux.lds.S @@ -93,7 +93,6 @@ jiffies = jiffies_64; #ifdef CONFIG_HIBERNATION #define HIBERNATE_TEXT \ - . = ALIGN(SZ_4K); \ __hibernate_exit_text_start = .; \ *(.hibernate_exit.text) \ __hibernate_exit_text_end = .; @@ -103,7 +102,6 @@ jiffies = jiffies_64; #ifdef CONFIG_KEXEC_CORE #define KEXEC_TEXT \ - . = ALIGN(SZ_4K); \ __relocate_new_kernel_start = .; \ *(.kexec_relocate.text) \ __relocate_new_kernel_end = .; @@ -170,9 +168,6 @@ SECTIONS KPROBES_TEXT HYPERVISOR_TEXT IDMAP_TEXT - HIBERNATE_TEXT - KEXEC_TEXT - TRAMP_TEXT *(.gnu.warning) . = ALIGN(16); *(.got) /* Global offset table */ @@ -194,6 +189,13 @@ SECTIONS HYPERVISOR_DATA_SECTIONS + /* code sections that are never executed via the kernel mapping */ + .rodata.text : { + TRAMP_TEXT + HIBERNATE_TEXT + KEXEC_TEXT + } + idmap_pg_dir = .; . += IDMAP_DIR_SIZE; idmap_pg_end = .; @@ -337,8 +339,8 @@ ASSERT(__hyp_idmap_text_end - __hyp_idmap_text_start <= PAGE_SIZE, ASSERT(__idmap_text_end - (__idmap_text_start & ~(SZ_4K - 1)) <= SZ_4K, "ID map text too big or misaligned") #ifdef CONFIG_HIBERNATION -ASSERT(__hibernate_exit_text_end - (__hibernate_exit_text_start & ~(SZ_4K - 1)) - <= SZ_4K, "Hibernate exit text too big or misaligned") +ASSERT(__hibernate_exit_text_end - __hibernate_exit_text_start <= SZ_4K, + "Hibernate exit text is bigger than 4 KiB") #endif #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 ASSERT((__entry_tramp_text_end - __entry_tramp_text_start) <= 3*PAGE_SIZE, @@ -362,7 +364,7 @@ ASSERT(swapper_pg_dir - tramp_pg_dir == TRAMP_SWAPPER_OFFSET, #ifdef CONFIG_KEXEC_CORE /* kexec relocation code should fit into one KEXEC_CONTROL_PAGE_SIZE */ -ASSERT(__relocate_new_kernel_end - (__relocate_new_kernel_start & ~(SZ_4K - 1)) - <= SZ_4K, "kexec relocation code is too big or misaligned") +ASSERT(__relocate_new_kernel_end - __relocate_new_kernel_start <= SZ_4K, + "kexec relocation code is bigger than 4 KiB") ASSERT(KEXEC_CONTROL_PAGE_SIZE >= SZ_4K, "KEXEC_CONTROL_PAGE_SIZE is broken") #endif