.
D 2007-10-09T16:18:22
J assignee pvgoran
J cmimetype text/plain
J comment Hi\sTcl\steam,\n\n\nRecently\sa\scolleague\s(Tavis\sOrmandy)\sand\sI\suncovered\ssome\sbugs\sin\sPostgresql's\sregular\sexpression\sengine.\s\sIt\sturns\sout\sthat\sthey're\susing\scode\sderived\sfrom\sTcl.\s\sWith\sTom\sLane's\shelp,\swe\shave\ssome\stentative\spatches,\sbut\sfirst\sthe\sbugs\s:-)\n\n\nThe\sout\sof\sband\sread\sin\sthe\sUCS-4-enabled\sbuild\s--\s[CVE-2007-4769]\n(the\snumeric\svalue\smust\sbe\s>\sINT_MAX\sand\s<=\sUINT_MAX)\n\n\s\sselect\stext\s(E'\\\\'\s||\s'3161573148')\s~\s(E'\\\\'\s||\s'3161573148');\n\n\s\sregexp\s{\\3161573148}\s{\\3161573148};\n\n(The\scode\slocation\sapplies\sto\spostgres\nBackreferences\sover\sINT_MAX\sslip\sthrough\sthe\scheck\sfor\sa\ssingle-digit\sdue\sto\na\ssignedness\serror\sin\sregcomp.c's\slexescape()\snear\sline\s948.\s\sv->numsubexp\nis\sa\ssigned\sinteger\sand\sc\sis\sunsigned.\s\sThe\scast\sto\sa\ssigned\sint\savoids\nwarnings\sbut\smeans\sthat\sany\sc\s>\sINT_MAX\swill\spass\sthe\stest\sagainst\sthe\ndefault\snsubexp\svalue\sof\s10.\n\nThis\smistake\sallows\sindexing\sinto\sv->subs\sin\sregcomp.c's\sparsebranch()\snear\nline\s948\sto\sbe\scontrolled.\s\sHowever,\sthis\sis\sjust\sa\sNULL\stest.\s\sIf\sthe\svalue\ngiven\sindexes\sout\sof\svalid\smemory,\sa\ssegmentation\sfault\swill\soccur\sand\sthe\nchild\sprocess\swill\sdie.\n\n\nAn\sinfinite\sloop\s--\s[CVE-2007-4772]\\\n\s\sregexp\s{($|^)*}\s{x}\n\nTom\sLane\sprovided\san\sanalysis:\n"""\n\sThe\sproblem\sseems\sto\soccur\swhen\nan\sNFA\sstate\shas\sa\sregular\s^-constraint\sas\san\soutgoing\sarc,\splus\sboth\n^-constraint\sand\s$-constraint\soutgoing\sarcs\sthat\sloop\sback\sto\sitself.\nThe\slatter\stwo\sshould\sbe\sdestroyed\sas\suseless,\sbut\sthe\scode\sdidn't\sget\nrid\sof\sthe\s$-constraint\ssoon\senough\sand\sinstead\stransferred\sit\sto\sthe\nnew\sstate\sthat\swas\sgenerated\sas\spart\sof\spushing\sthe\sregular\s^-constraint\nback\sto\sthis\sstate's\spredecessor\sstates.\s\sThis\sthen\sre-created\sa\nsituation\sthat\sneeded\sanother\siteration\sof\spullback().\n\nThe\sproposed\sfix\sdeals\swith\sthis\sby\schanging\sthe\sorder\sof\soperations:\nuseless\sarcs\sare\sdestroyed\sin\sa\sseparate\spass\sbefore\swe\sdo\sthe\spull()\ncalls.\n\nI\smade\sa\ssimilar\schange\sin\spushfwd(),\swhich\shas\seffectively\sidentical\nlogic.\s\sThis\spart\sof\sthe\spatch\sis\snot\sneeded\sto\sfix\sthe\sparticular\ntest\scases\swe\shave,\sand\sI'm\snot\scertain\sit\scan\sbe\sa\sproblem\sonce\npullback()\shas\sgotten\srid\sof\s^-constraints.\s\sBut\sit\sseems\slike\sa\sgood\nidea\sto\skeep\sthe\slogic\sin\ssync,\sand\smaybe\sthere\sare\scases\swhere\sit's\nnecessary\sfor\spushfwd()\sto\sprotect\sitself.\n\nThe\spatches\spass\sthe\sPostgres\sand\sTcl\sregression\stests,\sbut\sI'd\ssure\nlike\sto\shave\ssomeone\swho\sunderstands\sthis\sregex\scode\slook\sat\sthem.\nIn\sparticular\sI'm\snot\sentirely\ssure\swhether\sit's\sOK\sto\sflush\s*all*\nnon-PLAIN\scircular\sarcs.\n"""\n\n\nAn\sexpensive\sregular\sexpression:\n\nregexp\s{(x{200}){200}$y}\s{x}\n\n\nAgain\swith\sexcellent\sanalysis\sfrom\sTom\sLane:\n"""\nThis\sis\snot\sas\sslow\sas\syour\soriginal\s(about\s2m\srather\sthan\s15m\son\nmy\smachine),\sbut\sit\sillustrates\sthe\sbasic\sissue:\sthe\sdoubly\snested\niteration\sgenerates\s200*200\s=\s40000\sNFA\sstates,\sall\sof\swhich\sare\nlater\sfound\sto\sbe\sdead\sbecause\sthe\strailing\s"$y"\scannot\spossibly\nmatch\sanything,\sand\sthen\swhen\swe\stry\sto\sclean\sthem\sup\swe\sspend\nO(N^2)\stime\sat\sit\sbecause\sthey're\sall\son\sthe\ssame\scolorchain.\n\nAFAICS\sthe\sonly\suse\sof\sthe\scolorchain\sdata\sstructure\sis\sin\sokcolors(),\nwhich\scould\sprobably\sbe\srewritten\sto\sdo\sone\siteration\sover\sall\sthe\sNFA's\nstates/arcs.\s\sSo\sI'm\stempted\sto\spropose\sjust\sgetting\srid\sof\sthe\ncolorchain\slists.\s\sBut\sI'd\s*really*\swant\sto\stalk\sto\ssomeone\swho\sknows\nthis\scode\sbetter,\sfirst.\s\sThere\smight\sbe\scases\swhere\sthe\scolorchains\nare\sessential\sfor\sperformance.\n"""\n\n\nIt'd\sbe\sgreat\sif\swe\scould\schat\sabout\sthis\smore,\sbut\sI\swasn't\ssure\sthe\sbest\splace\sto\semail.\s\sI\sknow\sthat\sTom\sLane\swould\sappreciate\sany\sadditional\sinsight\sbefore\spatching\spostgresql.\s\sTom\scurrently\shas\sa\sfew\spatches\sexplicitly\sfor\spostgres,\sbut\sthey\sappear\sthat\sthey'll\sbe\spretty\sportable.\s\sHere\sare\sthe\srelevant\saddresses:\n\nWill\sDrewry\s<[email protected]>\s\nTavis\sOrmandy\s<[email protected]>\nTom\sLane\s<[email protected]>\nPostgresql\sSecurity\s<[email protected]>\n\n\nThanks!\nwill\n
J foundin obsolete:\s8.5b1
J is_private 1
J priority 5\sMedium
J resolution None
J status Open
J submitter gwad
J subsystem 43.\sRegexp
J title Out\sof\sband\sread\sand\sexpensive/looping\soperations
J type Bug
K 1810264fffffffffffffffffffffffffffffffff
U gwad
Z 30c9a159d4d74c2c277c255fdaa3498c