Read_jpg_copy_loop代码段
在处理JPEG 图像文件时,有漏洞的函数会处理图像尺寸。下面是有漏洞的代码的伪代码:
width = rect->right - rect->bottom;
height = rect->top - rect->left;
allocated_address = __wrap_malloc(width*height*cinfo->output_components);// output_scanline;
if ( (unsigned int)output_scanline >= cinfo->output_height )
break;
//reads one line from the file into the cinfo buffer
jpeg_read_scanlines(cinfo, line_buffer, 1);
if ( output_scanline >= Rect->left && output_scanline < Rect->top )
{
memcpy(allocated_address + bytes_copied , line_buffer, width*output_component);// <--Oops
bytes_copied += width * output_component;
}
}
其中:
_wrap_malloc 函数会根据图像尺寸的3个参数来分配内存块。Width和height 都是16位的整数(uint16_t)。
cinfo->output_component 告诉我们有多少个字节用来表示每个像素。变量的值分别代表不同的意思,1表示Greyscale、3表示RGB、4表示RGB + Alpha\CMYK等。
除了height和width外,output_component 也可以完全被攻击者控制。因为在分析的过程中并不会与文件中的其他数据进行验证。
__warp_malloc 希望其参数在32位的寄存器中进行处理。也就是说如果分配的大小超过 (2^32) 字节,那么就可以引发整数溢出。
分配的大小是通过图像的width乘 height再乘以 output_components 得到的。因为没有检查,那么一旦被攻击者所控制,进一步滥用后就会引发整数溢出。
_wrap_malloc(width * height * cinfo->output_components);// <---- Integer overflow
然后缓存被传递给memcpy,引发基于堆的缓存溢出。
分配后,memcpy 函数会被调用,然后复制图像数据到分配的内存中。
复制的过程是一行一行进行的:
memcpy(allocated_address + bytes_copied ,line_buffer, width*output_component);//<--Oops
size (width*output_component)数据也会被复制 height 次。