From mboxrd@z Thu Jan 1 00:00:00 1970 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 3A64EC46CA9 for ; Wed, 29 Nov 2023 12:31:40 +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=PuV1abELZfVWcfkvztgVYRwOlQSQ4fp+Fmd/SCB8s9g=; b=ELsYKrAxxNwoODJVUg960mvqne 6bsDrGU+Zj62cRjcWfv4Nh/e0gcv7+JOxEtI6Ybc3MT1MEPhRFCJRKQC7AuOZ7CKSPREtEVN7rW8o ShFjj2SquJefLFut1TnWuMDs2fSEi80gJ+mDv4kJfWztnR0+SWVFc0hF2KKyI7w3lOev7j02BR8du C033I6op+UjqBwWBOB2h/AYBCdANgUiyEoNSlCaqrBIbUjOTs/VhGu6m7QHVM1UNmfuB6cVGH97/8 ZYrpngyQsdCTS6cdtw27706E8tBs4qnmG9EKvD+WR6Pl4PLYcwcJ896EWtB0FqPjgZ4w8GFODiavh TS15VvKA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r8Jig-008KDT-0m; Wed, 29 Nov 2023 12:31:10 +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 1r8IZX-007zAP-2H for linux-arm-kernel@lists.infradead.org; Wed, 29 Nov 2023 11:17:41 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5ca26c07848so102199737b3.0 for ; Wed, 29 Nov 2023 03:17:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701256658; x=1701861458; 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=TemnQ6kclmDQ1Y/tK0SncTYLfLpErlQSmwcRZLsuZ5M=; b=SuOppCrS7q07pAEJ3Uj4qQC1b4AKm4ArHC+NArqKqV4ZPfiZCPV7Qp6XZrJgN0MMXL z12ANlLRHBNvJMqizIYeT7z9hJQvkBgwVq+96ScLaqQ+Tej2KI5k8MpUMrx04IKtjNmW KxJtJInYWtX/MMnXPgBSjJo4pEYKv2+JrAVofVR7TRLIIIxSogITnpBpCaDIF4gQv1ev Xnr1AOO5AQ3jCR8eld0WMfWRm/k9XFE97XcArPAeGH+CnHynQ0E+oZrkLbLgS861hLrU A6SecdjPkhyOvkfWeT1g8ASCidA/eWhysu9bKK9I7barCOOLeBoA46vP+HFtEwnNv5f4 l74w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701256658; x=1701861458; 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=TemnQ6kclmDQ1Y/tK0SncTYLfLpErlQSmwcRZLsuZ5M=; b=YZ1J+kHjeBoiNTZN7b49BJJKT20lTkxyOOqMlxEhSoIdGX0/UR7NsUECkK9U8KWlsQ OPthDxiuPtyOh4QzueKK7EmegsZ+uM1m8JYLO0wCP7EmZx1Npy10QY4jCrYgNHRu7xae JJCj49TifDSjWpJSVy9NbkA0NTenk+itnqql6Msxz+QzoXeuzeLXZvid1LaWYQQV9hNC KduMFTvR7BEtndy20k1TfMLwDcj9r5DD0QQm99C/ZhGjk2tVWOMoKnX3mDWvnin9yDq/ Gl/88LWrhuBwAYKC1NcUsJpnMgEAlYMZmQ752YSRsGnSZvx1oCpiuBGk1ZtPeZQBNKk/ 2W+w== X-Gm-Message-State: AOJu0Yzkx22e70raxGO7Z0kSnLRQD62H85hMWAmYEX29Lkj2Im6CJw20 EAmS5KPeTQpZrQPeUrSqwn8a3/wnextZ3iZTXXdvKpVFgZW/kHCogSfMdYflc8J3HNfOTbsV2Hv +hUfRaOHuZ3BPOlqSZJ8RvZXp1akZNwprMtYYbK6+ePEmc8uRRH/GI4Wxz3YmRq5uPs3nQsvSuu s= X-Google-Smtp-Source: AGHT+IGftKIg8g7toes0IFvl7pkJxszCbkePK9iwSqgrwIAu9bFMiL/SwKWRtsqYSb7aE2hCGidQ49i8 X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a81:d444:0:b0:5d2:913a:6421 with SMTP id g4-20020a81d444000000b005d2913a6421mr16598ywl.8.1701256658111; Wed, 29 Nov 2023 03:17:38 -0800 (PST) Date: Wed, 29 Nov 2023 12:16:06 +0100 In-Reply-To: <20231129111555.3594833-43-ardb@google.com> Mime-Version: 1.0 References: <20231129111555.3594833-43-ardb@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2767; i=ardb@kernel.org; h=from:subject; bh=b4D2PcJszYXmkQEIBtlIkaYoXvVpy75o5lzK1n09vY8=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JITVdtryh0arLabmSm/tLuZv22l/Fr+64dlqu+aHfqyU/J KaWrHLpKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABORFGVkmCTZcNFNb+2edAOL VfFfePYJPU13YtOLkak/2X9qWbmEKcN/n6b0zZn2jav27GdNO/lAOypHrIb58+n1locqyh8Hpzm wAAA= X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Message-ID: <20231129111555.3594833-53-ardb@google.com> Subject: [PATCH v6 10/41] 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-20231129_031739_748249_3F782497 X-CRM114-Status: GOOD ( 16.06 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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); } -- 2.43.0.rc1.413.gea7ed67945-goog _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel