From patchwork Tue Jan 23 14:53:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13527535 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 1002DC47DDB for ; Tue, 23 Jan 2024 14:55:18 +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=idHjmJTOHePxpUY8nG2b7Ql+E+uSddcqLP8Mc66EN5I=; b=Tmn6cRFpZTYPmRfHkYuu7FrtsG lmrk0gLzqp3ObWtBweJAvMD5QP/O1hxp3j3q8dNe6uLyh3cIs1hOtWcYWuj8lckY+ei7zmmXvUJUO T27J9vnu3+MuKLXfkHF0KDlpF6Xlw9nVFTA8Q0ShrSjQhDwGs0i67jwh16NYVbsqWr2o5/PoNs4XP 0/03QlFAH40UgHDNo6w7AE8R69R4rJjQcKLl6pMBJCXPQg+u3kzzhI4MZdMs/VOn7EXDzrJHGtuBD nlo4Rk1a1UDtqPOGP0Hudw/PPw69LtX/DHNpnbT9p/NL19MVq/M+4753E82Z7uRF6viU+gaQ5ZQqZ 2T2YLfWQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rSIAu-00GtmW-2c; Tue, 23 Jan 2024 14:54:52 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rSIAg-00GtfY-2U for linux-arm-kernel@lists.infradead.org; Tue, 23 Jan 2024 14:54:40 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-dc253fee264so6518774276.2 for ; Tue, 23 Jan 2024 06:54:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706021677; x=1706626477; 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=62asg65Pu52oGNnRn/JupEiRgpOjTOLJtlcIrLV4Xuw=; b=kx/DDRSlv9nEwqgQltvbzVgAJ+cdYd5UEcWdgdtPWWPa1qTx1Ayaluv0R9dNgU+92P 8WY63475YQEtUO8pb7qDIntkPVsqFbyq6YihFzOUcnpq//i360Cb1GHjm70I3sP+QWt6 cQ0TxZy+Zf4zHtC05+OgaiJSaFP/s7GRaIFCTYwafBGaiPDPiqfax7OqTpfWwKtLEDE+ BrNxZ3jux7ohamTiiApXWmVApCc4Md6U/zPvh/N5Kx6m8wJ2vAJj8rBR59FwjJ0HgH6t lq2CvuqgZ2WKLD9tzoEdnfN0T4uWTPX0jW6A1Y150ItjMOYqYDKimSOmeDfBHCIJIerl WwZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706021677; x=1706626477; 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=62asg65Pu52oGNnRn/JupEiRgpOjTOLJtlcIrLV4Xuw=; b=rZ/LSTUZDhcyLcfFmVm5ULKk0uncwR45LGaEELoiCB0RysW53+68BrwHZlBuD9Nizi aI4YqHszLSc8iPbDMAIvCWmug6gIAPWkR+pzW6+kx2HKqVjvlfKR+Vds/yJrfPv1KUJr GiN/Mc04nknd7LNilM8byrori3Rh80iZpx35jzIaafHW7gWqiRc7ox53Xs5Uh8pbPGWK uBSy0KI6RAQCyutGbCl4G7h8At/RuYxWEyJC8cB2g+sLcjWvTxT1aCYf+MSgiGyuVyoS xMrkFRPCq8KZHRXM0+T65EY3oGPBXuMkHdKcSYpBonZvF+FCKxX/9s7sul+IRw+xgj26 jHVw== X-Gm-Message-State: AOJu0Yz4chzj96H1ay1gu9iehOkcVERLUxA/I+3ageZJ+F9QBpruuhop VNDo/CnSyYz/1YRcIyHKNgRrNG2nO8ExePyjjF+wqu8+cW9DFukDTUcyx27ObeLlwKyKVG3RoXc +G//d9bwBfNVGtGOcvuzmh9JQ+lgZ2zdqCOlo0KdJfLxZgqeOX46oSZbvyegfum5SkAUdu6ZA+f SnIS03/0+GaBXpGWelPXDiPue2Wy3aCjzi5TS78q6N X-Google-Smtp-Source: AGHT+IGXlNZxzKmWFxzplS7FfMzuf3DZ3E6vXsGh729PWZU+KsnfhlTxLRUQtXx/+SspGy83loRYLkiM X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a25:8209:0:b0:dc2:2ae2:6ca with SMTP id q9-20020a258209000000b00dc22ae206camr2886215ybk.5.1706021677656; Tue, 23 Jan 2024 06:54:37 -0800 (PST) Date: Tue, 23 Jan 2024 15:53:06 +0100 In-Reply-To: <20240123145258.1462979-52-ardb+git@google.com> Mime-Version: 1.0 References: <20240123145258.1462979-52-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2810; i=ardb@kernel.org; h=from:subject; bh=0rBiGe78OB3uG2sV1+OHG9RVJcuVmafaaV1uQQAQPqk=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIXX9pUuBzo5a/p+ZFbW+7fu40IdZr+7ZpyOlJctuiGiGs jaEz+DtKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABO5bsnwP9gzuHDinIt2J5XY Wzcv2vxSfcmhpNQfE7jfNrou2LV9miAjw3GlRYKR13R2n9C9f/Om2qpSkyalDGuV+nsiC76qLPP /xwoA X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123145258.1462979-59-ardb+git@google.com> Subject: [PATCH v7 07/50] 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240123_065438_810196_D395B1B2 X-CRM114-Status: GOOD ( 15.91 ) 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. Acked-by: Mark Rutland 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); }