T1Lib Buffer Overflow Vulnerability T1Lib Vulnerability Description PHP and T1Lib Exploitation Possibility Workaround Credits T1Lib t1lib is a library written in C which implements functions for generating bitmaps from Adobe Type 1 fonts. It uses most internal parts of the Type 1 rasterizer provided with X11R5 and newer but tries avoid the disadvantages known from that rasterizer. Although most people would use the rasterizer under X11, having X11 is not necessary at all. The rasterizer is implemented as a shared or static library to be linked to the programs that want to use Type 1 fonts. In addition to this library there is a test-program called xglyph which, as the name indicates, requires X11. It can be used to show most of the features of the library. xglyph can be run out of the box without doing any X11 configuration like setting resources. Vulnerability Description there exists a buffer overflow vulnerability char * intT1_Env_GetCompletePath() of t1lib/t1env.c which caused by unchecked boundary in strcat(pathbuf, StrippedName); pathbuf is a global function defined as static char pathbuf[2048]; and so is allocated in Heap memory. therefore we have a buffer overflow situation by feeding the first argument of intT1_Env_GetCompletePath() which is char *filename, a big string. PHP and T1Lib t1lib is used in many application, perhaps the most popular one is PHP 5.x and 4.x. when PHP is configured with '--with-t1lib=[PATH]' then t1lib is staticly linked with PHP. the shared object which contains the vulnerable function is libt1.so.5. then every enabled PHP function which related to t1lib could be a target, we found imagepsloadfont() which accepts a filename, which then calls the affected t1lib function. Exploitation Possibility Linux (slackware 10.2 - kernel 2.4.31 - php 4.4.7) : with small values (<70000) when trying to derefrence edx in : testb [edx], 0xff we get a segmentation fault due the invalid memory address edx points to. edx is somehow controled by us and is actually related to the size of string. anyway when increase the size of input we die at : mov DWORD PTR [ecx+edx+12],eax checking the registers we see that eax is directly controled by our input but ecx and edx remain the same with different size of inputs. so memory overwrite seems to be impossible in this situation too. so does code execution. Windows (2003 sever - no service pack - php 5.2.2) : with values about 61800 byts of input and higher we get access violation at: php_gd2!phpi_get_le_gd+0x7feab : movsd ds:0115dd0c=44444444 trying to read from where edi points to. by changing the size of input it indicates that we have no control over edi so we access violate whenever we use strings with the sizes over 61800. this time we try with size of 61000, we have direct control over eax, edi and esi somehow we die at: php_gd2!phpi_get_le_gd+0x76e85 : mov eax, [eax+esi-0x98] trying to read from eax+esi-0x98. so we might pass this instruction and reach code execution. no furthur honey in this case. when we try with 7814 bytes we die at: ntdll!RtlDeleteCriticalSection+0xf : mov eax, [ebx+0x10] controlling eax and ebx with the same value, so if you have some good memory A which A+10 points to somewhere good and also A itself is some kind of exception handler this could be a way to go. (I know its unlikely :p) but anyway you can corrupt memory. like somewhere in safe_mode function handling etc etc. Workaround Don't use t1lib till a patch or a newer version comes out. in PHP you can disable functions which use t1lib. or compile again without t1lib support. Credits Vulnerability Discovery : Hamid Ebadi ebadi@bugtraq.ir Vulnetability Research : Kaveh Razavi razavi@bugtraq.ir all the other people contributing in www.bugtraq.ir , Alireza Danesh, Mohammad Mozaffari and others... ps. no more time to research since yesterday somebody reported a bug in PHP regarding imagepsloadfont() which we now know it is not a PHP one.