All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH mm-hotfixes v4 2/4] x86/mm/pat: acquire init_mm write lock to avoid UAF
Date: Thu, 16 Jul 2026 21:53:06 +0000	[thread overview]
Message-ID: <20260716215307.14A6E1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260716-series-vmap-race-fix-v4-2-8c108c4317df@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The patch adds `mmap_write_lock(&init_mm)` to prevent `ptdump` from encountering a Use-After-Free when `cpa_collapse_large_pages` frees kernel page tables. However, it fails to protect lockless kernel page table walkers like `vmalloc_to_page` and `lookup_address`, leaving them vulnerable to the exact same UAF.
--

--- Patch [2]: [PATCH mm-hotfixes v4 2/4] x86/mm/pat: acquire init_mm write lock to avoid UAF ---
commit 5b395a03ec3db496bc431613dd4a4fe93dcd87d9
Author: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Subject: x86/mm/pat: acquire init_mm write lock to avoid UAF

This patch adds an mmap write lock acquisition around kernel page table
collapsing in the x86 PAT code. It aims to prevent use-after-free issues
when concurrent walkers like ptdump traverse page tables while they are
being freed.

> diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
> index d023a40a1e034..d1e63f7d267ff 100644
> --- a/arch/x86/mm/pat/set_memory.c
> +++ b/arch/x86/mm/pat/set_memory.c

[ ... ]

> @@ -442,6 +443,18 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa)
>  	}
>  }
>  
> +static void cpa_collapse_large_pages(struct cpa_data *cpa)
> +{
> +	/*
> +	 * Take the mmap write lock on init_mm to:
> +	 * - Avoid a use-after-free if raced by ptdump (which takes its own
> +	 *   write lock on init_mm).
> +	 * - Serialise concurrent CPA walkers.
> +	 */
> +	scoped_guard(mmap_write_lock, &init_mm)
> +		__cpa_collapse_large_pages(cpa);

[Severity: High]
This is a pre-existing issue, but does taking the mmap write lock on init_mm
protect against lockless kernel page table walkers?

While this prevents a race with ptdump, functions like vmalloc_to_page() or
lookup_address() can still walk the kernel page tables without holding the
init_mm mmap lock.

If a lockless walker reaches the PMD level just as
__cpa_collapse_large_pages() collapses the huge page and calls
pagetable_free(), couldn't the walker encounter a use-after-free when it
calls pte_offset_kernel() on the freed page table?

For example, a concurrent call to vmalloc_to_page() could see:

CPU1
__cpa_collapse_large_pages()
    pagetable_free(ptdesc); /* freed immediately, no RCU grace period */

CPU2
vmalloc_to_page()
    ptep = pte_offset_kernel(pmd, addr); /* UAF on freed page table */
    pte = ptep_get(ptep);

Does this mean the page tables still need to be freed via RCU to ensure
these lockless walkers don't dereference freed memory?

> +}
> +
>  static void cpa_flush(struct cpa_data *cpa, int cache)
>  {
>  	unsigned int i;

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260716-series-vmap-race-fix-v4-0-8c108c4317df@kernel.org?part=2

  reply	other threads:[~2026-07-16 21:53 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-16 21:31 [PATCH mm-hotfixes v4 0/4] mm: fix UAF caused by race between ptdump and vmap pgtable freeing Lorenzo Stoakes (ARM)
2026-07-16 21:31 ` [PATCH mm-hotfixes v4 1/4] mm/vmalloc: acquire init_mm lock on huge vmap to avoid ptdump UAF Lorenzo Stoakes (ARM)
2026-07-16 21:31 ` [PATCH mm-hotfixes v4 2/4] x86/mm/pat: acquire init_mm write lock to avoid UAF Lorenzo Stoakes (ARM)
2026-07-16 21:53   ` sashiko-bot [this message]
2026-07-17  3:46   ` David CARLIER
2026-07-17  7:47     ` Lorenzo Stoakes (ARM)
2026-07-17  8:05       ` David CARLIER
2026-07-17 11:30   ` Will Deacon
2026-07-17 11:45     ` Lorenzo Stoakes (ARM)
2026-07-17 11:40   ` Lorenzo Stoakes (ARM)
2026-07-16 21:31 ` [PATCH mm-hotfixes v4 3/4] mm/ptdump: always stabilise against page table freeing using init_mm Lorenzo Stoakes (ARM)
2026-07-16 21:43   ` sashiko-bot
2026-07-16 21:31 ` [PATCH mm-hotfixes v4 4/4] arm64: remove redundant concurrent ptdump UAF mitigation Lorenzo Stoakes (ARM)
2026-07-16 23:41 ` [PATCH mm-hotfixes v4 0/4] mm: fix UAF caused by race between ptdump and vmap pgtable freeing Andrew Morton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260716215307.14A6E1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=ljs@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.