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 A52A9C61D97 for ; Fri, 24 Nov 2023 10:21:22 +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=pL6vRPyJMycEFe2I/69KEATftc /Z8eVOH4PynbmdCz9PcnD+At62QKMqhqdgduP8597b9SUpPpnb2uPQ/1n50RcmQshvNirt7blf/K6 B4NU9xjPa7CxAWF/3CBMY2Dk1VnILOl9OqyF7eg/+m2YdsN/hqE4cmsKSTSQb3L7vZEtMP1KTk+rF gW7QLcJ0SkG8e115KlKQZ33KIiUmD7iVF904A2Ml1R4UO1nsYK3g2sz9S1iXCOuMndv8SltGkOm/v c3ZB9rGce3qvR0yhwOUL0M/SVqomTA7u7/Q+g8NLoWheXfgWsPfWJjSq76lBjC0uRN+s7oy6O4raH xOpYPXnA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r6TIn-006oig-2y; Fri, 24 Nov 2023 10:20:49 +0000 Received: from mail-wm1-x349.google.com ([2a00:1450:4864:20::349]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r6TIb-006obD-2u for linux-arm-kernel@lists.infradead.org; Fri, 24 Nov 2023 10:20:39 +0000 Received: by mail-wm1-x349.google.com with SMTP id 5b1f17b1804b1-40b3487fa9eso11104555e9.2 for ; Fri, 24 Nov 2023 02:20:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700821236; x=1701426036; 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=KMV3J2PAaXypgRKtLkwa8Sn3zvDm03q5EAHYBYbqL3qgnHqdzWC4357WfaQT/R53eW T5FmFYTd18fVnJNxWFdYc5bjOYo4B0NrxEeCXCXiqUQpXVD6dSfDnNuwXwyxBpXmnWxn 7ULcg/HARSrLI0IvACzmn96srBAzJ54uo7MEqrbue14CgfSml8/UNGGZBQSJ+VPteRpe i6kttOnGWSNDVvJJ6Yf13rhh+oHjmNFULWkTypKaDKG/HMgP7KYGhzCvCAr7Cmq/4AWZ 0gECbuKNFrEJLmH3H0Bf+c2pwEVvh2dkMpYgssFivUPxNBuDUT2wHHCqKh+EPtzDHO4z BvrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700821236; x=1701426036; 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=Mq04k4gsEuM5xE0jTuoMe49fEwOwg9X90/qYrTM2pvoPlVX4VCbhkUIKg4SpokWB+D CLahakI0euZ7SEGn+zOlMqHyUhBu/G8BARVTZgibzs7+BifqmH+4FWLnIhTlx21AKZjF 3klIlTx0DQAjddPIwypGOsG3Y/I3QhJnqrcOFCeBvm/lQeUXdofZT9DNuS0i0k2l1/Al qcHPgx5oE0PL0JxNpJUB1oPNHM8JI+r4PBxOqWbe2k2G92IMTMgIh4vaNvKT+jg7ikiM T7oEw5ZHIkfgC5exP4nIWjX9zzaimh/JHTYT+S8G40WVlVuzuslIA/uAYPLGVVCiPDgk A8sQ== X-Gm-Message-State: AOJu0YyxYwJKCyiIJyJKLCu9tBvXfRo630w0rpQg3l9bVCWsAbsVGm1n XeqNJsKEjoZOKzVPJprDyo+OXsdLleb9yUJ4gos1KPjRhPUmEEm+9qG/KYSDFtjizUa1Kr3sHPA iWmLw59a5lFaq1wiXGKCwJej8RtwLfsX3LI3Qp2/DyCKl8DXHA/RRTeBOmdDMCVRorLW12w/WNI 0= X-Google-Smtp-Source: AGHT+IEFjhCc/t1AIjdKRtrJgSsH+nEdZdWoCT8lge3NHU9+bMZ1XDoo8ebq3un6/uDqsXiXW1DYirSk X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:600c:c09:b0:40b:2b38:a210 with SMTP id fm9-20020a05600c0c0900b0040b2b38a210mr43490wmb.8.1700821235994; Fri, 24 Nov 2023 02:20:35 -0800 (PST) Date: Fri, 24 Nov 2023 11:18:50 +0100 In-Reply-To: <20231124101840.944737-41-ardb@google.com> Mime-Version: 1.0 References: <20231124101840.944737-41-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=owGbwMvMwCFmkMcZplerG8N4Wi2JITWhoqeh0arLabmSm/tLuZv22l/Fr+64dlqu+aHfqyU/J KaWrHLpKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABOZtIGRYW6c/LMssbMX2pZ0 Pch4NVMj83zblPrss/6Zz6sPBHX9WsHwv7T5acfGXzte7yy57Gs5I9FuQZBxQrkEZ835n91Tt51 z4wcA X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Message-ID: <20231124101840.944737-51-ardb@google.com> Subject: [PATCH v5 10/39] 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-20231124_022037_936344_99B6707F X-CRM114-Status: GOOD ( 15.45 ) 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