From patchwork Tue Sep 12 14:16:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13381765 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 4D8ABCA0EEB for ; Tue, 12 Sep 2023 14:18:11 +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:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=S556F9/ZlVVRLnLZSf6vOBH1mD4mhxpG45u7JnTEA+Q=; b=vLzbIFzj+/I8HjdehjOahabi9q FqzwiEX8cuPyL3fEENhSuzjv5MqIzGGiWnkBL8Qkk46iwbFwClNcJ+CW3TY/UZpQ0naTvDIFOjsXH 1zGEEsVsovVPeH7z+hzyRMs9GCk1kbRPc7Dlvmr8AYwQoMcOwZwSAsBRGGRw2FOoTJWX+LrK+Bi42 +18O8dXM2+b0TqXk3k7BCelJet7U82ECuA9bNIC9+ZjGKNIYeyoBWN7MQF1MQf5fvVUzdiln79moj mKP3osl2hPZ5CG+Cjqyfj5AyT686ORLU7MFlv9qEcm7c4D2Iwir7dAXBLOtbwQndQfFeTigNWVMjQ tmZgmFzg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qg4D3-003W3I-2o; Tue, 12 Sep 2023 14:17:45 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qg4Cx-003VxL-0E for linux-arm-kernel@lists.infradead.org; Tue, 12 Sep 2023 14:17:40 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-58c8cbf0a0dso122419657b3.1 for ; Tue, 12 Sep 2023 07:17:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694528257; x=1695133057; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ONCaJLYcCgdni3j1wgWy9nMUxs3EQ633sZ++p1ZZL0w=; b=pb4c7sgkGkp5IDXcvEnIij/FLw+KWess2zeimRxKH2KIzbAMyJM5jl/BfF2MVhWqh5 j3aO4LK76De4V6bOFSytz6urj8MT7Rd1l5jXVdrGvBzRwkEZGfeFeZRViaP6x4qz2sa3 VuTeje7zNaqr1jWAr1X5n/hxsbtCVVUKau2a7vmPVawQqYbylWNsuMjlH/YmKvm/2c6l fYl5+PpN/IiT5uZBPShetUapZFBbLmVHVbyliM4Yz3B9TmcfptI0jwOnP91LITTp5f1F b/MHUtOPSnWUZxA0dPRn6hxwUWrJSOjtTGu1xN+17/+V7A9AjqU2bcWO55BiHE9gLllm 1Smg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694528257; x=1695133057; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ONCaJLYcCgdni3j1wgWy9nMUxs3EQ633sZ++p1ZZL0w=; b=wlIfVRCxF/jQfHts1pSSC98BjTNIc/t+95oQ8ez7T9aNVYju/GWTEMgXs/CW1eahj+ WIjcPLzTsibF23/YLpwDs/GFWTVL0X37sXhQQg3YLseeD/PExe9Ii7jfSW98kjOxH6a7 YaagPQ1WGkfr2erfXwUx36sI44vjrTxdOrOM6E//6LOz1JZN5NweqTDCnL/dmZQAvCFB wqNehmQirCYU+vdMQ4dxU53C1TOUtoSD9/gRvPtjYDFWdpdYTPwZjOpcLZHoFqUFJ7br M/Iy+BHY6jqhZpfqDATDMOx3XXgpwUOn+8AAG/mtCgWUWHvwzZ505quqwbGQKQx1M/rj QJRA== X-Gm-Message-State: AOJu0YwgylYPC5/Ad1l4SxIl281kUfb5lI9ylyD9jzY3uIz6yvVtnFBa P8WVzd4PblVbdRu+HNsJiDxs115WU6Hj77LmLBSJfQ72777YIZbBqj16vP2ilWgxdICQbmCgVc7 msLRvyQ2kXeIhpQELZ5v6S2JDCdSW3jQzE1jegLpVMPUYifH0bmKssBDZav10rnCiHw5k0Oxssx c= X-Google-Smtp-Source: AGHT+IE4eMaEjl+bOnVc0xBss16iPzVzTmjCOvT3C/l5PX3Tf5uznrV6XLBewZyPqVR+aiGw3djMhCle X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a81:bc4a:0:b0:59b:b0b1:d75a with SMTP id b10-20020a81bc4a000000b0059bb0b1d75amr32621ywl.4.1694528257636; Tue, 12 Sep 2023 07:17:37 -0700 (PDT) Date: Tue, 12 Sep 2023 14:16:00 +0000 In-Reply-To: <20230912141549.278777-63-ardb@google.com> Mime-Version: 1.0 References: <20230912141549.278777-63-ardb@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2763; i=ardb@kernel.org; h=from:subject; bh=DdRQ4AAoBXXFaR1Awn7Do8PULwk4CqCK4XGt9V+4C0c=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZWhaEElz6u/tdGndgSm15+yk5ebJLH0qe13q9r5HVGG0 21X1ER1lLIwiHEwyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgIm43WBkuHBpXs+kToGP1oXs M+M839/9LRiQWhUtmRJ5gMe/99qzYwy/mI6FG8xiP9XrO3sxY4SpoMTS+73xi04GXrfW6lxmlFH ODAA= X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230912141549.278777-73-ardb@google.com> Subject: [PATCH v4 10/61] arm64: kaslr: Adjust randomization range dynamically From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel , Catalin Marinas , Will Deacon , Marc Zyngier , Mark Rutland , Ryan Roberts , Anshuman Khandual , Kees Cook , Joey Gouly X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230912_071739_119278_63AF2CEA X-CRM114-Status: GOOD ( 16.86 ) 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 From: Ard Biesheuvel Currently, we base the KASLR randomization range on a rough estimate of the available space in the upper VA region: the lower 1/4th has the module region and the upper 1/4th has the fixmap, vmemmap and PCI I/O ranges, and so we pick a random location in the remaining space in the middle. Once we enable support for 5-level paging with 4k pages, this no longer works: the vmemmap region, being dimensioned to cover a 52-bit linear region, takes up so much space in the upper VA region (the size of which is based on a 48-bit VA space for compatibility with non-LVA hardware) that the region above the vmalloc region takes up more than a quarter of the available space. So instead of a heuristic, let's derive the randomization range from the actual boundaries of the vmalloc region. Signed-off-by: Ard Biesheuvel --- arch/arm64/kernel/image-vars.h | 2 ++ arch/arm64/kernel/pi/kaslr_early.c | 11 ++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/arch/arm64/kernel/image-vars.h b/arch/arm64/kernel/image-vars.h index 35f3c7959513..2cc3aa4c27c7 100644 --- a/arch/arm64/kernel/image-vars.h +++ b/arch/arm64/kernel/image-vars.h @@ -34,6 +34,8 @@ PROVIDE(__pi___memcpy = __pi_memcpy); PROVIDE(__pi___memmove = __pi_memmove); PROVIDE(__pi___memset = __pi_memset); +PROVIDE(__pi_vabits_actual = vabits_actual); + #ifdef CONFIG_KVM /* diff --git a/arch/arm64/kernel/pi/kaslr_early.c b/arch/arm64/kernel/pi/kaslr_early.c index 17bff6e399e4..b9e0bb4bc6a9 100644 --- a/arch/arm64/kernel/pi/kaslr_early.c +++ b/arch/arm64/kernel/pi/kaslr_early.c @@ -14,6 +14,7 @@ #include #include +#include /* taken from lib/string.c */ static char *__strstr(const char *s1, const char *s2) @@ -87,7 +88,7 @@ static u64 get_kaslr_seed(void *fdt) asmlinkage u64 kaslr_early_init(void *fdt) { - u64 seed; + u64 seed, range; if (is_kaslr_disabled_cmdline(fdt)) return 0; @@ -102,9 +103,9 @@ asmlinkage u64 kaslr_early_init(void *fdt) /* * OK, so we are proceeding with KASLR enabled. Calculate a suitable * kernel image offset from the seed. Let's place the kernel in the - * middle half of the VMALLOC area (VA_BITS_MIN - 2), and stay clear of - * the lower and upper quarters to avoid colliding with other - * allocations. + * 'middle' half of the VMALLOC area, and stay clear of the lower and + * upper quarters to avoid colliding with other allocations. */ - return BIT(VA_BITS_MIN - 3) + (seed & GENMASK(VA_BITS_MIN - 3, 0)); + range = (VMALLOC_END - KIMAGE_VADDR) / 2; + return range / 2 + (((__uint128_t)range * seed) >> 64); }