From patchwork Mon Apr 11 09:48:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 12808905 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 5D1D6C433EF for ; Mon, 11 Apr 2022 10:09:14 +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=BUcSZAh1hhINH7Lf1+vvb9faZ2F56j4RWeLnrpMbSak=; b=KCLDdcMMsA/tc6 17v8uKeLaOz7CWc1MDLbHAapVBCTlJ7VAvAKjMVoet4LszXx6olhh+tNStPInvzmsQpqUdOcCsP6f Bmz8u7V8UJiz/y9LlvlBFXGV0uZU5jn0mgqn7R5SqcJvfSl1KOcEr+ht37ivEyJcfm3qJ3qBBUunv KGQHCrvnspS5DEglQsyQrfXI0NunFJVSNXuo/mViULb9Qse8lRkyB1Lehj/VYsiv2SeqOG7huRo0m I5FWpRtBcdOT7N7z+tVAlMuionSWl0Gdt8NPsb5G0MIAcrTCOhoq+6MZ4Z19wr5dejM96NYz9yJnP U/PJvTfyz2vqnGXpBGig==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ndqxl-008BO9-9k; Mon, 11 Apr 2022 10:08:01 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ndqfp-0082WW-Bc for linux-arm-kernel@lists.infradead.org; Mon, 11 Apr 2022 09:49:31 +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 dfw.source.kernel.org (Postfix) with ESMTPS id C10F86115F; Mon, 11 Apr 2022 09:49:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 46926C385AF; Mon, 11 Apr 2022 09:49:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1649670568; bh=p0HM6bJ2W5zi0lRXz9RkRFCa7ovecepIMCIRdegPyDw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SBvb2MHe5VVkg+UBXHLC7juXlD6aiuUwLnyzwwqZ44gxE7ojvYZL1ToBDS3g1jzJS 52EgSUvcz7l5s0VoKqSHGM1b7BEZGQins/KGgWxv0NsFo4wP04sjoSPSPUeONF6uTD VNolwzcpmPGRWJSqnTlss+ZXjAR5WpYiBRlorDEKeiNJFdgfYFys2+NqzIt63HASAZ JmOvKnsbxal1Vv0i/1g+n7jhXaO5ZxB1Eln/xeRUZaDwGnBdFRdjQHHI7ibO472Z7R yeAq8hewZXbHmHZFZYpviAjUWJELDTUJ4Hqb+gKzJGj6AWFQNayzTEqel9WTZmmhya 37pKGuw50GL3w== From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: linux-efi@vger.kernel.org, Ard Biesheuvel , Marc Zyngier , Will Deacon , Mark Rutland , Kees Cook , Catalin Marinas , Mark Brown Subject: [PATCH v3 19/30] arm64: kaslr: deal with init called with VA randomization enabled Date: Mon, 11 Apr 2022 11:48:13 +0200 Message-Id: <20220411094824.4176877-20-ardb@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220411094824.4176877-1-ardb@kernel.org> References: <20220411094824.4176877-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2118; h=from:subject; bh=p0HM6bJ2W5zi0lRXz9RkRFCa7ovecepIMCIRdegPyDw=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBiU/lUlLpgYuYd8m7Cituz9DWVDd29GEV96RwqmEfh 2lFiCsuJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYlP5VAAKCRDDTyI5ktmPJECKC/ 9GnbFIfvmXhg2KARoQsvFqlt63XFKF9d/aWERxIFvnLtrQsQLeqf/OLJdFL6wAJlFAHrV2W1iAJxQh GEUT1pHP677KDVLRYwOeSdYlDL/kOMm3k2Esr4q0AZ0UqQ0NQUdf5g7GxLvELN+GSY32rW3vda4SXw vKZ6WDc8KZ3UEaQAS5Va2zv4e7NYC8W7zBvud0ggMtw8s05EkzDjxbvFRWdh6aUFXvCJlaqbn62nqQ OyXo1m5Y+p/0W9+Ob1eBo0+GhCN44i2hsUMC1dGPLVTbEln/v6fV3iMRxIbLB+c4JNICJD9923x3dn Z8Q89V7eREGfv+C+dx6HOolM02k0O/6TozbRvqKORlUauZqJ6risIz2Plgf3mKZBGW8Cz1ImRq2SGn v3CZxOwPO6n6Ke5COipWf1gaB+itOgimspc01+52kE03uWj49ZOnga/Uuzp0LHPJXnulBFUWAUHxxC Pq9ybiPLmaOpnGkO2TuJZKYa6sRAUyAm0kqjB3Hf4yQwU= 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-20220411_024929_540018_EA9761B4 X-CRM114-Status: GOOD ( 18.41 ) 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 We will be entering kaslr_init() fully randomized, and so any addresses taken by this code already take the randomization into account. This means that taking the address of _end or _etext and adding offset to it produces the wrong value, given that _end and _etext references will have been fixed up already, and therefore already incorporate offset. So instead of referring to these symbols directly, use their offsets relative to _text, which should produce values that depend on the size and layout of the Image only. Then, add KIMAGE_VADDR to obtain the unrandomized values. Signed-off-by: Ard Biesheuvel --- arch/arm64/kernel/kaslr.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kernel/kaslr.c b/arch/arm64/kernel/kaslr.c index d5542666182f..3b12715642ce 100644 --- a/arch/arm64/kernel/kaslr.c +++ b/arch/arm64/kernel/kaslr.c @@ -141,6 +141,8 @@ u64 __init kaslr_early_init(void) return offset % SZ_2G; if (IS_ENABLED(CONFIG_RANDOMIZE_MODULE_REGION_FULL)) { + u64 end = (u64)_end - (u64)_text + KIMAGE_VADDR; + /* * Randomize the module region over a 2 GB window covering the * kernel. This reduces the risk of modules leaking information @@ -150,9 +152,11 @@ u64 __init kaslr_early_init(void) * resolved normally.) */ module_range = SZ_2G - (u64)(_end - _stext); - module_alloc_base = max((u64)_end + offset - SZ_2G, + module_alloc_base = max(end + offset - SZ_2G, (u64)MODULES_VADDR); } else { + u64 end = (u64)_etext - (u64)_text + KIMAGE_VADDR; + /* * Randomize the module region by setting module_alloc_base to * a PAGE_SIZE multiple in the range [_etext - MODULES_VSIZE, @@ -163,7 +167,7 @@ u64 __init kaslr_early_init(void) * when ARM64_MODULE_PLTS is enabled. */ module_range = MODULES_VSIZE - (u64)(_etext - _stext); - module_alloc_base = (u64)_etext + offset - MODULES_VSIZE; + module_alloc_base = end + offset - MODULES_VSIZE; } /* use the lower 21 bits to randomize the base of the module region */