public inbox for linux-arm-kernel@lists.infradead.org
 help / color / mirror / Atom feed
From: Dev Jain <dev.jain@arm.com>
To: catalin.marinas@arm.com, will@kernel.org
Cc: anshuman.khandual@arm.com, quic_zhenhuah@quicinc.com,
	ryan.roberts@arm.com, kevin.brodsky@arm.com,
	yangyicong@hisilicon.com, joey.gouly@arm.com,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, mark.rutland@arm.com,
	maz@kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH] arm64/mm: Fix use-after-free due to race between memory hotunplug and ptdump
Date: Mon, 28 Jul 2025 16:12:08 +0530	[thread overview]
Message-ID: <c1a9284b-972a-4474-9151-0a2ed8558b9e@arm.com> (raw)
In-Reply-To: <20250728103137.94726-1-dev.jain@arm.com>


On 28/07/25 4:01 pm, Dev Jain wrote:
> Memory hotunplug is done under the hotplug lock and ptdump walk is done
> under the init_mm.mmap_lock. Therefore, ptdump and hotunplug can run
> simultaneously without any synchronization. During hotunplug,
> free_empty_tables() is ultimately called to free up the pagetables.
> The following race can happen, where x denotes the level of the pagetable:
>
> CPU1					CPU2
> free_empty_pxd_table
> 					ptdump_walk_pgd()
> 					Get p(x+1)d table from pxd entry
> pxd_clear
> free_hotplug_pgtable_page(p(x+1)dp)
> 					Still using the p(x+1)d table
>
> which leads to a user-after-free.
>
> To solve this, we need to synchronize ptdump_walk_pgd() with
> free_hotplug_pgtable_page() in such a way that ptdump never takes a
> reference on a freed pagetable.
>
> Since this race is very unlikely to happen in practice, we do not want to
> penalize other code paths taking the init_mm mmap_lock. Therefore, we use
> static keys. ptdump will enable the static key - upon observing that,
> the free_empty_pxd_table() functions will get patched in with an
> mmap_read_lock/unlock sequence. A code comment explains in detail, how
> a combination of acquire semantics of static_branch_enable() and the
> barriers in __flush_tlb_kernel_pgtable() ensures that ptdump will never
> get a hold on the address of a freed pagetable - either ptdump will block
> the table freeing path due to write locking the mmap_lock, or, the nullity
> of the pxd entry will be observed by ptdump, therefore having no access to
> the isolated p(x+1)d pagetable.
>
> This bug was found by code inspection, as a result of working on [1].
> 1. https://lore.kernel.org/all/20250723161827.15802-1-dev.jain@arm.com/
>
> Cc: <stable@vger.kernel.org>
> Fixes: bbd6ec605c0f ("arm64/mm: Enable memory hot remove")
> Signed-off-by: Dev Jain <dev.jain@arm.com>
> ---

Immediately after posting, I guess the first objection which is going to
come is, why not just nest free_empty_tables() with mmap_read_lock/unlock.
Memory offlining obviously should not be a hot path so taking the read lock
should be fine I guess.



  reply	other threads:[~2025-07-28 11:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-28 10:31 [PATCH] arm64/mm: Fix use-after-free due to race between memory hotunplug and ptdump Dev Jain
2025-07-28 10:42 ` Dev Jain [this message]
2025-07-28 11:13 ` Ryan Roberts
2025-07-28 11:31   ` Dev Jain
2025-07-28 11:56     ` Ryan Roberts

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=c1a9284b-972a-4474-9151-0a2ed8558b9e@arm.com \
    --to=dev.jain@arm.com \
    --cc=anshuman.khandual@arm.com \
    --cc=catalin.marinas@arm.com \
    --cc=joey.gouly@arm.com \
    --cc=kevin.brodsky@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=quic_zhenhuah@quicinc.com \
    --cc=ryan.roberts@arm.com \
    --cc=stable@vger.kernel.org \
    --cc=will@kernel.org \
    --cc=yangyicong@hisilicon.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox