Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: 徐晗翔 <xuhx@hust.edu.cn>,
	muchun.song@linux.dev, osalvador@suse.de,
	akpm@linux-foundation.org
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Ackerley Tng <ackerleytng@google.com>
Subject: 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)
Date: Wed, 25 Nov 2026 13:05:04 +0100	[thread overview]
Message-ID: <3b7d2d65-be96-4b63-9de6-7303d2de641b@kernel.org> (raw)
In-Reply-To: <566ddeb4.2ad04.1a202302fe1.Coremail.xuhx@hust.edu.cn>

Hi Ackerley,

does your series [1] actually fix this? What's the status of that?

[1]
https://lore.kernel.org/linux-mm/20260916-hugetlb-subpool-always-track-used-v3-0-38aae9b5ccdd@google.com/

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:
> 
> * 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


-- 
Cheers,

David


      parent reply	other threads:[~2026-11-25 12:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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)
2026-11-25 12:05     ` David Hildenbrand (Arm) [this message]

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=3b7d2d65-be96-4b63-9de6-7303d2de641b@kernel.org \
    --to=david@kernel.org \
    --cc=ackerleytng@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=muchun.song@linux.dev \
    --cc=osalvador@suse.de \
    --cc=xuhx@hust.edu.cn \
    /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