From patchwork Wed Dec 13 08:40:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13490551 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 40C55C4167D for ; Wed, 13 Dec 2023 08:41:29 +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=wGBe6E9cIqSA1COGSgoGTBL0oKQoTYvGfL+d69kdGaM=; b=it5XB9z4AGphU8e/OBPy5URhnB 4wKVLIJeLu0y3NwVsbwGA3OnpQ4HmS+D3eVNdgnmGoaziJx8rNiV3syJjyGYbiGObMZGVnpGS6BuH xcRgLsuzEyM2mdId0zWydwSvQNw3JqStDdJQLpjUeKK23j9eq1i3uy/Hz66oQlALicRpKIpHtKu8p bhvsuoY3bhuZaw7VzGW8rTyTD/HNTG14v91IYa6bv60X+upYSjwP4clg+t9nt9MWWaI9ClfVNYgbm QVAzd+zV6f7o+2fw7Kb7AZhdSNCqyeEodjUuO/eeeBA5mXDalhfNnRMA+v6Z8/iKT3OnN/ePWuD6d qV/VjjVQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rDKnf-00E3Mk-21; Wed, 13 Dec 2023 08:41:03 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rDKnY-00E3Ju-2P for linux-arm-kernel@lists.infradead.org; Wed, 13 Dec 2023 08:40:58 +0000 Received: by mail-wr1-x449.google.com with SMTP id ffacd0b85a97d-333501e22caso5287305f8f.2 for ; Wed, 13 Dec 2023 00:40:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702456855; x=1703061655; 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=BX+wtcx32mlMaYxBO5qCEKCbHJOmCWQWGX70DB2Gh3c=; b=USiYLW2jI+PArGrCi4uN52PCQ/yWWR/ctpFqeV5jK/jUpVcOj61dMZWRc8nIiTXlTV /AXlSaUnB3A9/fu+GbcAU2SRnbLeSA8b16h0ih9lFrYzcqOZmcaZsTagCU9SKP1qCdTg jxTvBOM/Xk2CgHo5RQLa8mV2TPQC9TKeNOFFH73P3+awYiEBbx7HV6WULFw29zIanI8p n9OQd93OIHQ64BuRX/5zh/nGHCsVfWhc8J3QVAb3p9Kf5KV0egjwAfEy59bmJUZ34Xwm ZPMNiGvSfSVX3yg4vS0tLaDPKnM8rl99U2yIj/pl7N7OtEUEuH5MCvv6w2wRi7ppNgH0 vGWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702456855; x=1703061655; 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=BX+wtcx32mlMaYxBO5qCEKCbHJOmCWQWGX70DB2Gh3c=; b=YMVr9hlJ6Tl6EXVPNLmpAPbZzdZT1t9IxGHgpL1kyIHMJwW8RdIaS35BYN3HVB3MAG c0jE0YqjCpvHnWaWcmF51NVgw1U0nLn/aKwMdY8rynNmg9qKbSNZeLkTm3K55dTcHChO 9JKFN+erb9DAAoLnyKxIXE3M7jrmT8Pj/y8YEGPK56B06stM0os1diP7Seg9Qh1Ef3B2 TRrc8fKCdUEHSkxemzYLelDrQuFoUvp+YCFQauTY9bI0E4qHtCwNKBl45XgBl9yzF6u7 zS1FLFqpvrxMpfZp3uFmLJvIIO6bHjnaDMVaAkau4FyPQxbC7I5Biu78/12ovNhXWV0A VlkQ== X-Gm-Message-State: AOJu0Yzwx9C/zJYwJ1qthlczTG7B3/T2hY+muDj1BaQaZ0LTBYM7RySd RqTTiH+a+KqpR9jio/lW32V9qN3QOxOsMN3Gdfu7qbvvOGV7CUSIz2LKvSzVETbZ8wqh2Vg30Tn xfRut9+JctmZZ0iUKj4kt0HpecX7VqRlRf7KaiOhK3e37RcaRTFu8WErn/nKXE8SF5suDnhEvfW 8= X-Google-Smtp-Source: AGHT+IGR0z+EnBVucZm+4vYqP5kGE6FZswCqbjTP9a187glIu6MkPzFznNvTt/xaSdr1N7wKWKiln7rY X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a5d:6daf:0:b0:333:543a:9f9b with SMTP id u15-20020a5d6daf000000b00333543a9f9bmr38509wrs.11.1702456854719; Wed, 13 Dec 2023 00:40:54 -0800 (PST) Date: Wed, 13 Dec 2023 09:40:32 +0100 In-Reply-To: <20231213084024.2367360-9-ardb@google.com> Mime-Version: 1.0 References: <20231213084024.2367360-9-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=XJvCPbUowF3LdiBGPhgFAl7bd7Pk9sOwkOq/ytxs+3M=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIbUy9/9CBTFmhQn/t7efybXiksnacul0b0/q34RpFyOu3 HWfuFiso5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAExE8Ccjw9KGP9FnCw+Z62Xm XLi7iHF9iXG3+Jv1JedEDoduVKs3y2P4Xxt9Rm9b/Ge3NKtiieshmred54e8NjPrmCLxkTcgMpC ZBwA= X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231213084024.2367360-16-ardb@google.com> Subject: [PATCH v7 7/7] 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231213_004056_782169_1F504CE8 X-CRM114-Status: GOOD ( 15.17 ) 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 5e4dc72ab1bd..e931ce078a00 100644 --- a/arch/arm64/kernel/image-vars.h +++ b/arch/arm64/kernel/image-vars.h @@ -36,6 +36,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); }