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 A6781CA0EEC for ; Tue, 12 Sep 2023 14:17:46 +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=mKbAhV3Oir//GAiaBvdoV7Lg+caswN5t8qC4P0ii3OQ=; b=4YaEjpCZhDMDrdPQ/vI3xu1Fl2 WpW5z47dMlKSm2hRPag6vfUUqRxDfwZFzt8g8mToUntalXQrHU0nbPSqowDRVTMYADoXIEbwvtYZF jrjb+aHptIZm+VobczARKbO54gDlcqNjNIZXL9Y+H9n/m8bMpNbMM96FyiAI7yDf0hfO+9GKRLY7+ hgNh02MRjrwNleSx4UV7UltYuQZ6Lm65Se0acjAl0kBCKr5u7fzQZUc26ZGhUfa6n2Iy6iUxugkRh qPg0JjzHurzpWF6+pDpZ75dmszDS1YVu/+RfJWujZkLOQIso5S+aMe+C+NSsw6t6TZTIF9Ca/48Jb yFaq6DGA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qg4Cg-003VpY-0Y; Tue, 12 Sep 2023 14:17:22 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qg4Cc-003Vnx-2B for linux-arm-kernel@lists.infradead.org; Tue, 12 Sep 2023 14:17:20 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-59b6083fa00so43625437b3.0 for ; Tue, 12 Sep 2023 07:17:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694528236; x=1695133036; 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=rLlY/95tjuLc6IDcLl8sfILHPIdJZTxbO9x7CsV1Df0=; b=JELyO6xpjKRaNOUh+pQjSQ3dtoQnlXQxWNs7WyFD0Pc11F+6ETEjFruKzIjVReNdtl dcHrrC0pcjrwt7otu6H2NDYVcibZU4ZJx9PdyXqPlQaUm1rwhuav7h0/I5iaqmMy4QXe Iya+mkycIbd7uZLrZjOF+oBzEJieqkVZiYYDDpe6LlNZ3y/gAby9TQ/GskPYTv3a+bqD DVmBSAJsuQJpzqrHeRRQSCdT6bC3p2LG9c7HEhDLDv2zTzHJXTKy77PW3b8OgymgnrVI /dcJR9p20rNltvrr02okT5FGYufpDybImwbeUyR+NnR9ymBbDRRwkPJ0k5vx6VwrkWXZ Hpwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694528236; x=1695133036; 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=rLlY/95tjuLc6IDcLl8sfILHPIdJZTxbO9x7CsV1Df0=; b=HtRTV36DGE/DqgT9trnBocrT5kPLgDmCUBPK8iyyCbz2n5sBFab5/90g5lvkAMDCwC c3NGMVn8Y6+22Z16NkcRdM5mQ6oWio6+gzqTMKtXdDmvtjbl4FQTlL9+v8+JXLCohNq4 3CSLX1T0aQiQjjQ2G3Q4oPDaUx28VhIb7nt1ttat18Lvre5XXyDsVveGJhRdYEG6PGE5 FJ8SnBf5jngn0PaaAOa6atKfS3mzECWIkkO7lDxRubGhnrFd4jOsoQ1/DVncOmWuUlYa AXWHjW2MmDChb7Uaf/yTnhAYl3gPnebauYYSVvwFM0RPbwVl+vrQW/uhy/a34g600St+ LqTw== X-Gm-Message-State: AOJu0YzUpUPZJPcy9rMbZX5XsbziqmI53EGrmTj6Bp0hJP05ktE+3RMo GLZRu/seuULB7XcVtsLAZWYrbSfTwKBYz9tfxoizC8FqT1RIYhC03NjoUbTp+OpbquzQG6rEOwj Rkl1MtMN2gdl9LdPXIbXWNKP2ODxJT7r/W0GOXOXSjQk7id7/B8n5SZ3Splk8XG9jeLsbGAdpHi w= X-Google-Smtp-Source: AGHT+IFVnpOTrt0nqnGJuDHHnxt+XyUEbf6Rcx27SMEyboc1HsYylrV+SZo3851BFUkajTuhKGZxEJkZ X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a81:ae48:0:b0:592:7a69:f61b with SMTP id g8-20020a81ae48000000b005927a69f61bmr320805ywk.0.1694528236561; Tue, 12 Sep 2023 07:17:16 -0700 (PDT) Date: Tue, 12 Sep 2023 14:15:52 +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=2827; i=ardb@kernel.org; h=from:subject; bh=Gmv54HGEg26w3b7wLxjCBwmFzhfBCXDT9Z81d9Krxfg=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZWhaEbLg3a7aRejeX/OSFX4tfRqwBGt5H6Jz2If783+5 id27DNjRykLgxgHg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZjI/dOMDLvPLer/z3jp147D aXurrs22vtvMKeB5emlZ6N/MyOiwhbcY/jsIBR+WLeFQ6Ew7NXP26errdvwXVMwXWae63305e8M PMX4A X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230912141549.278777-65-ardb@google.com> Subject: [PATCH v4 02/61] arm64: mm: Take potential load offset into account when KASLR is off 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_071718_711469_BEFDF27C X-CRM114-Status: GOOD ( 20.29 ) 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 We enable CONFIG_RELOCATABLE even when CONFIG_RANDOMIZE_BASE is disabled, and this permits the loader (i.e., EFI) to place the kernel anywhere in physical memory as long as the base address is 64k aligned. This means that the 'KASLR' case described in the header that defines the size of the statically allocated page tables could take effect even when CONFIG_RANDMIZE_BASE=n. So check for CONFIG_RELOCATABLE instead. Signed-off-by: Ard Biesheuvel --- arch/arm64/include/asm/kernel-pgtable.h | 27 +++++--------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/arch/arm64/include/asm/kernel-pgtable.h b/arch/arm64/include/asm/kernel-pgtable.h index 85d26143faa5..83ddb14b95a5 100644 --- a/arch/arm64/include/asm/kernel-pgtable.h +++ b/arch/arm64/include/asm/kernel-pgtable.h @@ -37,27 +37,12 @@ /* - * If KASLR is enabled, then an offset K is added to the kernel address - * space. The bottom 21 bits of this offset are zero to guarantee 2MB - * alignment for PA and VA. - * - * For each pagetable level of the swapper, we know that the shift will - * be larger than 21 (for the 4KB granule case we use section maps thus - * the smallest shift is actually 30) thus there is the possibility that - * KASLR can increase the number of pagetable entries by 1, so we make - * room for this extra entry. - * - * Note KASLR cannot increase the number of required entries for a level - * by more than one because it increments both the virtual start and end - * addresses equally (the extra entry comes from the case where the end - * address is just pushed over a boundary and the start address isn't). + * A relocatable kernel may execute from an address that differs from the one at + * which it was linked. In the worst case, its runtime placement may intersect + * with two adjacent PGDIR entries, which means that an additional page table + * may be needed at each subordinate level. */ - -#ifdef CONFIG_RANDOMIZE_BASE -#define EARLY_KASLR (1) -#else -#define EARLY_KASLR (0) -#endif +#define EXTRA_PAGE __is_defined(CONFIG_RELOCATABLE) #define SPAN_NR_ENTRIES(vstart, vend, shift) \ ((((vend) - 1) >> (shift)) - ((vstart) >> (shift)) + 1) @@ -83,7 +68,7 @@ + EARLY_PGDS((vstart), (vend), add) /* each PGDIR needs a next level page table */ \ + EARLY_PUDS((vstart), (vend), add) /* each PUD needs a next level page table */ \ + EARLY_PMDS((vstart), (vend), add)) /* each PMD needs a next level page table */ -#define INIT_DIR_SIZE (PAGE_SIZE * EARLY_PAGES(KIMAGE_VADDR, _end, EARLY_KASLR)) +#define INIT_DIR_SIZE (PAGE_SIZE * EARLY_PAGES(KIMAGE_VADDR, _end, EXTRA_PAGE)) /* the initial ID map may need two extra pages if it needs to be extended */ #if VA_BITS < 48 -- 2.42.0.283.g2d96d420d3-goog _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel