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 7BCB1C5DF74 for ; Tue, 18 Aug 2026 14:13:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References :Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=9Cqfq6txhZErfhbyj7xjA3Rbs/jkv6MSn+BaaqatuCE=; b=YYZRvkueQxAvUKD/4TR2pDBcsK 0MEOJpzXVZ3Buh9xT2PSvxG96T8yI1UqgFuAwqSSROWIaNFztErmDP14UjXBX3/olfd3uNdsuZizM bCBsPOpCylZ8vdmN/muk8djI2yLvx3J542hzbadIV5szHnZNBXx978ITxOt9jBoTJVJiRyHUeOuAn KH4JDHhKz7xOPHPwunI8hBNm0DI23L5sX3RI58E4FrrzNnx4ldWU9ujqhixccIXi3eiMH3dh3dWt1 BADuQYeO29E1xExJe+0G4iTvG++in+xnBnJckeYBuqq+tsC9+54IojrkvDiNaF2LNjj2av1bK7cHa +UyPu7JA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wwKZP-000000088bD-03pq; Tue, 18 Aug 2026 14:13:39 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wwKZD-000000088OT-0VfD for linux-arm-kernel@lists.infradead.org; Tue, 18 Aug 2026 14:13:32 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A5C1119F0; Tue, 18 Aug 2026 07:13:22 -0700 (PDT) Received: from localhost.localdomain (e123572-lin.cambridge.arm.com [10.2.208.46]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E129D3F85F; Tue, 18 Aug 2026 07:13:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1787062406; bh=sJbE8NLgwo2nj84nkxPSpL2i2LPH5XpUVTvG2CACATQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=pn0sG1iwAdXKOT8As39XdvEFBPfyHGDscQwApXXCbtq2hCt5N/+FYO6jVj/cRfp3m 2Qnjopn7ObgQ/mbtudgsNE7BxrpFcbry2fUFK2Zl2gX3G5lmJ8SbOgH4LgJspRphpV +XRir/ROwBftafAib4c/ol4obZUqX+lrFOh17KvA= From: Kevin Brodsky Date: Tue, 18 Aug 2026 15:09:07 +0100 Subject: [PATCH RFC v9 25/25] mm: Add basic tests for kpkeys_hardened_pgtables MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260818-kpkeys-v9-25-743ad31b2c8f@arm.com> References: <20260818-kpkeys-v9-0-743ad31b2c8f@arm.com> In-Reply-To: <20260818-kpkeys-v9-0-743ad31b2c8f@arm.com> To: linux-hardening@vger.kernel.org Cc: Kevin Brodsky , Andrew Morton , Andy Lutomirski , Catalin Marinas , Dave Hansen , "David Hildenbrand (Arm)" , Jann Horn , Jeff Xu , Joey Gouly , Kees Cook , Linu Cherian , Linus Walleij , Marc Zyngier , Mark Brown , Matthew Wilcox , Maxwell Bland , "Mike Rapoport (IBM)" , Peter Zijlstra , Pierre Langlois , =?utf-8?q?Pierre-Cl=C3=A9ment_Tosi?= , Quentin Perret , Rick Edgecombe , Ryan Roberts , Vlastimil Babka , Will Deacon , Yang Shi , Yeoreum Yun , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, x86@kernel.org, Ira Weiny , Lorenzo Stoakes , Thomas Gleixner X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1787062277; l=8176; i=kevin.brodsky@arm.com; s=20260427; h=from:subject:message-id; bh=sJbE8NLgwo2nj84nkxPSpL2i2LPH5XpUVTvG2CACATQ=; b=/Y+TYwvWUK3vc0eahhukYehcUiD/3b33M9t3s/hJxjH6bh7B3GD/N9ACWMr4VtA5S/wl2fGS6 WzlkzrrDe4tAwx5fOYQ1z3I8W1zmiXQ9VSvH0YRMMyCH8Gxo/v3UxiX X-Developer-Key: i=kevin.brodsky@arm.com; a=ed25519; pk=N2QG+eJKrvkNovwhhwJhnJ4+ScVfsGCHldmqLfcMTFs= X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260818_071327_315685_721B05DD X-CRM114-Status: GOOD ( 24.18 ) 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 Add basic tests for the kpkeys_hardened_pgtables feature: try to perform direct writes to kernel and user page table entries and ensure they fail. Multiple cases are considered for kernel page tables, as early page tables are allocated and/or protected in a different way. The tests are builtin (cannot be built as a module) because they refer to multiple symbols that are not exported (e.g. copy_to_kernel_nofault()). Signed-off-by: Kevin Brodsky --- mm/Makefile | 1 + mm/tests/kpkeys_hardened_pgtables_kunit.c | 200 ++++++++++++++++++++++++++++++ security/Kconfig.hardening | 12 ++ 3 files changed, 213 insertions(+) diff --git a/mm/Makefile b/mm/Makefile index 35982b6efb56..e31e58294845 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -148,3 +148,4 @@ obj-$(CONFIG_EXECMEM) += execmem.o obj-$(CONFIG_TMPFS_QUOTA) += shmem_quota.o obj-$(CONFIG_LAZY_MMU_MODE_KUNIT_TEST) += tests/lazy_mmu_mode_kunit.o obj-$(CONFIG_KPKEYS_HARDENED_PGTABLES) += kpkeys_hardened_pgtables.o +obj-$(CONFIG_KPKEYS_HARDENED_PGTABLES_KUNIT_TEST) += tests/kpkeys_hardened_pgtables_kunit.o diff --git a/mm/tests/kpkeys_hardened_pgtables_kunit.c b/mm/tests/kpkeys_hardened_pgtables_kunit.c new file mode 100644 index 000000000000..80d4f5144e48 --- /dev/null +++ b/mm/tests/kpkeys_hardened_pgtables_kunit.c @@ -0,0 +1,200 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include + +static void free_page_wrapper(void *ctx) +{ + __free_page((struct page *)ctx); +} + +KUNIT_DEFINE_ACTION_WRAPPER(vfree_wrapper, vfree, const void *); + +static pud_t *pud_off_k(unsigned long va) +{ + return pud_offset(p4d_offset(pgd_offset_k(va), va), va); +} + +static pte_t *get_kernel_pte(unsigned long addr) +{ + pmd_t *pmdp = pmd_off_k(addr); + + if (!pmdp || pmd_leaf(*pmdp)) + return NULL; + + return pte_offset_kernel(pmdp, addr); +} + +#define write_pgtable(type, ptr) do { \ + type##_t val; \ + int ret; \ + \ + pr_debug("%s: writing to "#type" at %px\n", __func__, (ptr)); \ + \ + val = type##p_get(ptr); \ + ret = copy_to_kernel_nofault(ptr, &val, sizeof(val)); \ + KUNIT_EXPECT_EQ_MSG(test, ret, -EFAULT, \ + "Direct "#type" write wasn't prevented"); \ +} while (0) + +/* + * Try to write linear map page tables, at every level. This is worthwhile + * because those page table pages are obtained from different allocators: + * + * - Static memory (part of the kernel image) for PGD + * - memblock for PUD and possibly PMD/PTE + * - pagetable_alloc() (buddy allocator) for PMD/PTE if large block mappings are + * used and the linear map is split after being created + */ +static void write_direct_map_pgtables(struct kunit *test) +{ + struct page *page; + unsigned long addr; + pgd_t *pgdp; + p4d_t *p4dp; + pud_t *pudp; + pmd_t *pmdp; + pte_t *ptep; + int ret; + + if (!kpkeys_supported()) + kunit_skip(test, "kpkeys are not supported"); + + page = alloc_page(GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, page); + ret = kunit_add_action_or_reset(test, free_page_wrapper, page); + KUNIT_ASSERT_EQ(test, ret, 0); + + /* Ensure page is PTE-mapped (splitting the linear map if necessary) */ + ret = set_direct_map_invalid_noflush(page); + KUNIT_ASSERT_EQ(test, ret, 0); + ret = set_direct_map_default_noflush(page); + KUNIT_ASSERT_EQ(test, ret, 0); + + addr = (unsigned long)page_address(page); + + pgdp = pgd_offset_k(addr); + KUNIT_ASSERT_NOT_NULL_MSG(test, pgdp, "Failed to get PGD"); + /* + * swapper_pg_dir is still writable at this stage, so don't check it. + * It is not protected by kpkeys_hardened_pgtables because it should be + * made read-only by mark_rodata_ro(). However since these + * KUnit tests are builtin, they are run before mark_rodata_ro() is + * called. + */ + + p4dp = p4d_offset(pgdp, addr); + KUNIT_ASSERT_NOT_NULL_MSG(test, p4dp, "Failed to get P4D"); + if (!mm_p4d_folded(&init_mm)) + write_pgtable(p4d, p4dp); + + pudp = pud_offset(p4dp, addr); + KUNIT_ASSERT_NOT_NULL_MSG(test, pudp, "Failed to get PUD"); + if (!mm_pud_folded(&init_mm)) + write_pgtable(pud, pudp); + + pmdp = pmd_offset(pudp, addr); + KUNIT_ASSERT_NOT_NULL_MSG(test, pmdp, "Failed to get PMD"); + write_pgtable(pmd, pmdp); + + ptep = pte_offset_kernel(pmdp, addr); + KUNIT_ASSERT_NOT_NULL_MSG(test, ptep, "Failed to get PTE"); + write_pgtable(pte, ptep); +} + +/* Worth checking since the kernel image is mapped with static page tables */ +static void write_kernel_image_pud(struct kunit *test) +{ + pud_t *pudp; + + if (!kpkeys_supported()) + kunit_skip(test, "kpkeys are not supported"); + + /* The kernel is probably block-mapped, check the PUD to be safe */ + pudp = pud_off_k((unsigned long)&init_mm); + KUNIT_ASSERT_NOT_NULL_MSG(test, pudp, "Failed to get PUD"); + + write_pgtable(pud, pudp); +} + +static void write_kernel_vmalloc_pte(struct kunit *test) +{ + void *mem; + pte_t *ptep; + int ret; + + if (!kpkeys_supported()) + kunit_skip(test, "kpkeys are not supported"); + + mem = vmalloc(PAGE_SIZE); + KUNIT_ASSERT_NOT_NULL(test, mem); + ret = kunit_add_action_or_reset(test, vfree_wrapper, mem); + KUNIT_ASSERT_EQ(test, ret, 0); + + /* vmalloc() without VM_ALLOW_HUGE_VMAP is PTE-mapped */ + ptep = get_kernel_pte((unsigned long)mem); + KUNIT_ASSERT_NOT_NULL_MSG(test, ptep, "Failed to get PTE"); + + write_pgtable(pte, ptep); +} + +static void write_vmemmap_pmd(struct kunit *test) +{ + struct page *page; + pmd_t *pmdp; + + if (!kpkeys_supported()) + kunit_skip(test, "kpkeys are not supported"); + + /* + * We just need the address of some struct page, so we can free the + * page right away. + */ + page = alloc_page(GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, page); + __free_page(page); + + /* vmemmap may use PMD block mappings */ + pmdp = pmd_off_k((unsigned long)page); + KUNIT_ASSERT_NOT_NULL_MSG(test, pmdp, "Failed to get PMD"); + write_pgtable(pmd, pmdp); +} + +static void write_user_pmd(struct kunit *test) +{ + pmd_t *pmdp; + unsigned long uaddr; + + if (!kpkeys_supported()) + kunit_skip(test, "kpkeys are not supported"); + + uaddr = kunit_vm_mmap(test, NULL, 0, PAGE_SIZE, PROT_READ, + MAP_ANONYMOUS | MAP_PRIVATE | MAP_POPULATE, 0); + KUNIT_ASSERT_NE_MSG(test, uaddr, 0, "Could not create userspace mm"); + + /* We passed MAP_POPULATE so a PMD should already be allocated */ + pmdp = pmd_off(current->mm, uaddr); + KUNIT_ASSERT_NOT_NULL_MSG(test, pmdp, "Failed to get PMD"); + + write_pgtable(pmd, pmdp); +} + +static struct kunit_case kpkeys_hardened_pgtables_test_cases[] = { + KUNIT_CASE(write_direct_map_pgtables), + KUNIT_CASE(write_kernel_image_pud), + KUNIT_CASE(write_kernel_vmalloc_pte), + KUNIT_CASE(write_vmemmap_pmd), + KUNIT_CASE(write_user_pmd), + {} +}; + +static struct kunit_suite kpkeys_hardened_pgtables_test_suite = { + .name = "kpkeys_hardened_pgtables", + .test_cases = kpkeys_hardened_pgtables_test_cases, +}; +kunit_test_suite(kpkeys_hardened_pgtables_test_suite); + +MODULE_DESCRIPTION("Tests for the kpkeys_hardened_pgtables feature"); +MODULE_LICENSE("GPL"); diff --git a/security/Kconfig.hardening b/security/Kconfig.hardening index bb75b580b7f1..ad6152961f8e 100644 --- a/security/Kconfig.hardening +++ b/security/Kconfig.hardening @@ -283,6 +283,18 @@ config KPKEYS_HARDENED_PGTABLES This option has no effect if the system does not support kernel pkeys. +config KPKEYS_HARDENED_PGTABLES_KUNIT_TEST + bool "KUnit tests for kpkeys_hardened_pgtables" if !KUNIT_ALL_TESTS + depends on KPKEYS_HARDENED_PGTABLES + depends on KUNIT=y + default KUNIT_ALL_TESTS + help + Enable this option to check that the kpkeys_hardened_pgtables feature + functions as intended, i.e. prevents arbitrary writes to user and + kernel page tables. + + If unsure, say N. + endmenu config CC_HAS_RANDSTRUCT -- 2.51.2