From patchwork Tue Feb 13 16:05:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxwell Bland X-Patchwork-Id: 13555328 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 A6AB9C48260 for ; Tue, 13 Feb 2024 16:06:23 +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:Subject:Message-ID:Date:From: MIME-Version:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=cgEf0p29iaUX76rg+XyKtXxAyolAMHVAzZvPdoWM7Mg=; b=ZCVDulACi6+jYs KEZKwWm0FJ+iL7xqK62eEGLqlGtsu5Z8OJ2jgZp3wTLAKhvVifQxXmcy5H85UFR5ZnokvHAdDRfJ1 62HHspnQje2VoeXem1r9wJQ7Ex4cAjjltgXHLva8cdsbY/X8Z6FpT+WjowzL7u4O8zcdjxZL6T87F qpLiTqnNjmfXQE9kx5Tl5+CQHOstecUpawkQHrYOBCdbZoaMbub/gWKuUFtwDBE7otiRN0zzZ1jqK H/b+pKlF5n5qa3rgcU5duMTM3m4sCL6bgQJDiAimf3H+hs5wfuIZ9CSmmCLgJ5L/zdwKUzrqsZgik AwFxY/zJOzqn1GMsvAQQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rZvIO-00000009sTm-3umE; Tue, 13 Feb 2024 16:06:08 +0000 Received: from mail-lf1-x12e.google.com ([2a00:1450:4864:20::12e]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rZvIF-00000009sLU-2B3d for linux-arm-kernel@lists.infradead.org; Tue, 13 Feb 2024 16:06:01 +0000 Received: by mail-lf1-x12e.google.com with SMTP id 2adb3069b0e04-511a04c837bso64476e87.0 for ; Tue, 13 Feb 2024 08:05:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=motorola-com.20230601.gappssmtp.com; s=20230601; t=1707840356; x=1708445156; darn=lists.infradead.org; h=cc:to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=CEDvSvYhyauFeH8HCgsVW9bjAlPcgBCfLAOG21KUOeA=; b=o9AZHDw7gXnriwqUO3nC84erk/fO+ZaoE7tlX+JZw+rBNttFE9nKfelzJUZIX6UcW6 fKjiJqdVEnA8ZqlG3bRzhSVUfIFdmeJZachfnv52eWPTWkLYQuf6kJ8ZNpfzQMF+nCCi B5IeFSOsU4QMS1Y2svpxDBoZH3mxS5B4Gx/AvgxADw15fBB+gIFrPgTJHI/7J7JMkKCt m6mditIuYis0Ygf74Zs+fmDSvUmfqdLkiG7CZbndWkDtryHFczVBZM/apJhiUFqSQJ8E i3cwr06K0+18SfnZ25SsDf/6K85vgqBdX0tKkd8QGimf/D+IxfzvjG+AgfQAwo870CX5 QhRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707840356; x=1708445156; h=cc:to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=CEDvSvYhyauFeH8HCgsVW9bjAlPcgBCfLAOG21KUOeA=; b=YCp7F6Dip21r+kcUNcG9uh69/8UTiquWi970JuJP97CxmxO7WwpUqf5TZcxDQTW9TY 3Ynd4WQuePfAGIqV6Hnu9JO28l4K07vBsMe4/gdo1s0+2S9T2cOnh5t2Lc4laB3PjUXT rnisB8NAz7d+rw0ForYeCghdurZZ/VzpOsOx3PXupAiBPgf1vqKRxukseC19WcaZBVcT L1v324EuN8GvcuXCrWJqohSj+4LsFq2tVKuTBO6uLw+449kXFuftcVglxsSieekHkxFL 99KGupPzbZVoFbTq67+xbfMA4K64sfQINUjVdzEd3nAnxFspzHhhheYKGVl0D87qHXKk OFEA== X-Gm-Message-State: AOJu0YzTei+IETs/XMwFFrSOTbtRN++rex0TYi77eKS9a7Y1Lm1AMtoy xLiSSibIx1ZQEQGEM+eA58d38WqZCBV8Ys1Yrx1rQ8tOX/lyLCTTjbTjl9FxR2FiN1mLu+OZ1R8 4allUuUIFFmnM+LgwMg1eF5kIdRtJTj4zR36Nb/O7kzLCqHRpeKqr X-Google-Smtp-Source: AGHT+IHYMpkxa2matGsJqOQHdcpvyUVrmAhdJnBDChaCNAXCjN5JzqoJXHX6E/S0U3nduZoXLj/LKbNruFUGLFN1u3c= X-Received: by 2002:a05:6512:158f:b0:511:47f7:62e0 with SMTP id bp15-20020a056512158f00b0051147f762e0mr9204732lfb.21.1707840356309; Tue, 13 Feb 2024 08:05:56 -0800 (PST) MIME-Version: 1.0 From: Maxwell Bland Date: Tue, 13 Feb 2024 10:05:45 -0600 Message-ID: Subject: [PATCH] arm64: allow post-init vmalloc PXNTable To: linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, catalin.marinas@arm.com, will@kernel.org, dennis@kernel.org, tj@kernel.org, cl@linux.com, akpm@linux-foundation.org, shikemeng@huaweicloud.com, david@redhat.com, rppt@kernel.org, anshuman.khandual@arm.com, willy@infradead.org, ryan.roberts@arm.com, rick.p.edgecombe@intel.com, pcc@google.com, mbland@motorola.com, mark.rutland@arm.com, rmk+kernel@armlinux.org.uk, tglx@linutronix.de, gshan@redhat.com, gregkh@linuxfoundation.org, Jonathan.Cameron@huawei.com, james.morse@arm.com, awheeler@motorola.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240213_080559_909921_1D95B10B X-CRM114-Status: GOOD ( 24.89 ) 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 Apologies if this is a duplicate mail, it will be the last one. Moto's SMTP server sucks!! Ensures that PXNTable can be set on all table descriptors allocated through vmalloc. Normally, PXNTable is set only during initial memory mapping and does not apply thereafter, making it possible for attackers to target post-init allocated writable PTEs as a staging region for injection of their code into the kernel. Presently it is not possible to efficiently prevent these attacks as VMALLOC_END overlaps with _text, e.g.: VMALLOC_START ffff800080000000 VMALLOC_END fffffbfff0000000 _text ffffb6c0c1400000 _end ffffb6c0c3e40000 Setting VMALLOC_END to _text in init would resolve this issue with the caveat of a sizeable reduction in the size of available vmalloc memory due to requirements on aslr randomness. However, there are circumstances where this trade-off is necessary: in particular, hypervisor-level security monitors where 1) the microarchitecture contains race conditions on PTE level updates or 2) a per-PTE update verifier comes at a significant hit to performance. Because the address of _text is aslr-sensitive and this patch associates this value with VMALLOC_END, we remove the use of VMALLOC_END in a print statement in mm/percpu.c. However, only the format string is updated in crash_core.c, since we are dead at that point regardless. VMALLOC_END is updated in kernel/setup.c to associate the feature closely with aslr and region allocation code. Signed-off-by: Maxwell Bland Signed-off-by: Maxwell Bland --- arch/arm64/Kconfig | 13 +++++++++++++ arch/arm64/include/asm/pgtable.h | 6 ++++++ arch/arm64/include/asm/vmalloc-pxn.h | 10 ++++++++++ arch/arm64/kernel/crash_core.c | 2 +- arch/arm64/kernel/setup.c | 9 +++++++++ mm/percpu.c | 4 ++-- 6 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 arch/arm64/include/asm/vmalloc-pxn.h rc = -EINVAL; base-commit: 716f4aaa7b48a55c73d632d0657b35342b1fefd7 diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index aa7c1d435139..5f1e75d70e14 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -2165,6 +2165,19 @@ config ARM64_DEBUG_PRIORITY_MASKING If unsure, say N endif # ARM64_PSEUDO_NMI +config ARM64_VMALLOC_PXN + bool "Ensures table descriptors pointing to kernel data are PXNTable" + help + Reduces the range of the kernel data vmalloc region to remove any + overlap with kernel code, making it possible to enable the PXNTable + bit on table descriptors allocated after the kernel's initial memory + mapping. + + This increases the performance of security monitors which protect + against malicious updates to page table entries. + + If unsure, say N. + config RELOCATABLE bool "Build a relocatable kernel image" if EXPERT select ARCH_HAS_RELR diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 79ce70fbb751..49f64ea77c81 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -22,7 +22,9 @@ * and fixed mappings */ #define VMALLOC_START (MODULES_END) +#ifndef CONFIG_ARM64_VMALLOC_PXN #define VMALLOC_END (VMEMMAP_START - SZ_256M) +#endif #define vmemmap ((struct page *)VMEMMAP_START - (memstart_addr >> PAGE_SHIFT)) @@ -35,6 +37,10 @@ #include #include +#ifdef CONFIG_ARM64_VMALLOC_PXN +#include +#endif + #ifdef CONFIG_TRANSPARENT_HUGEPAGE #define __HAVE_ARCH_FLUSH_PMD_TLB_RANGE diff --git a/arch/arm64/include/asm/vmalloc-pxn.h b/arch/arm64/include/asm/vmalloc-pxn.h new file mode 100644 index 000000000000..c8c4f878eb62 --- /dev/null +++ b/arch/arm64/include/asm/vmalloc-pxn.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_ARM64_VMALLOC_PXN_H +#define _ASM_ARM64_VMALLOC_PXN_H + +#ifdef CONFIG_ARM64_VMALLOC_PXN +extern u64 __vmalloc_end __ro_after_init; +#define VMALLOC_END (__vmalloc_end) +#endif /* CONFIG_ARM64_VMALLOC_PXN */ + +#endif /* _ASM_ARM64_VMALLOC_PXN_H */ diff --git a/arch/arm64/kernel/crash_core.c b/arch/arm64/kernel/crash_core.c index 66cde752cd74..39dccae11a40 100644 --- a/arch/arm64/kernel/crash_core.c +++ b/arch/arm64/kernel/crash_core.c @@ -24,7 +24,7 @@ void arch_crash_save_vmcoreinfo(void) vmcoreinfo_append_str("NUMBER(MODULES_VADDR)=0x%lx\n", MODULES_VADDR); vmcoreinfo_append_str("NUMBER(MODULES_END)=0x%lx\n", MODULES_END); vmcoreinfo_append_str("NUMBER(VMALLOC_START)=0x%lx\n", VMALLOC_START); - vmcoreinfo_append_str("NUMBER(VMALLOC_END)=0x%lx\n", VMALLOC_END); + vmcoreinfo_append_str("NUMBER(VMALLOC_END)=0x%llx\n", VMALLOC_END); vmcoreinfo_append_str("NUMBER(VMEMMAP_START)=0x%lx\n", VMEMMAP_START); vmcoreinfo_append_str("NUMBER(VMEMMAP_END)=0x%lx\n", VMEMMAP_END); vmcoreinfo_append_str("NUMBER(kimage_voffset)=0x%llx\n", diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c index 42c690bb2d60..b7ccee672743 100644 --- a/arch/arm64/kernel/setup.c +++ b/arch/arm64/kernel/setup.c @@ -54,6 +54,11 @@ #include #include +#ifdef CONFIG_ARM64_VMALLOC_PXN +u64 __vmalloc_end __ro_after_init = VMEMMAP_START - SZ_256M; +EXPORT_SYMBOL(__vmalloc_end); +#endif /* CONFIG_ARM64_VMALLOC_PXN */ + static int num_standard_resources; static struct resource *standard_resources; @@ -298,6 +303,10 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p) kaslr_init(); +#ifdef CONFIG_ARM64_VMALLOC_PXN + __vmalloc_end = ALIGN_DOWN((u64) _text, PMD_SIZE); +#endif + /* * If know now we are going to need KPTI then use non-global * mappings from the start, avoiding the cost of rewriting diff --git a/mm/percpu.c b/mm/percpu.c index 4e11fc1e6def..a902500ebfa0 100644 --- a/mm/percpu.c +++ b/mm/percpu.c @@ -3128,8 +3128,8 @@ int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size, /* warn if maximum distance is further than 75% of vmalloc space */ if (max_distance > VMALLOC_TOTAL * 3 / 4) { - pr_warn("max_distance=0x%lx too large for vmalloc space 0x%lx\n", - max_distance, VMALLOC_TOTAL); + pr_warn("max_distance=0x%lx too large for vmalloc space\n", + max_distance); #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK /* and fail if we have fallback */