Closed benehalo closed 7 months ago
This is a garbadge-in garbadge-out problem. You provide an invalid ppm for encoding which contains elements are are not within the promised precision of the ppm.
Won't fix. Missing input sanitation, this has to be done in the bitmap hook which is outside of the library, specifically cmd/bitmaphook.cpp if needed - this would add a single comparison per pixel which is at this time considered too expensive and not worth the hassle. Note that the decoder shall be robust, but the encoder operates on a garbadge-in garbadge-out basis.
Dear All,
This bug was found on Ubuntu 20.04 64-bit & libjpeg was checked out from the master branch of GitHub repository. Its commit is 3d391f06c80b6662d4524d8297e84528b009b774 (Mon Jan 15 07:47:50 2024 +0100).
libjpeg was built with ASAN using clang-14. The compile command was:
To reproduce:
Download and unzip the attached zip archive, and get PoCs
Bug Analysis
The cause of the bug is the lack of proper range checking for the index
*r
when it is used or assigned. As a result, the following code in ycbcrtrafo.cpp:220 triggers an index out-of-bounds errorSee the GDB analysis below for details
GDB Analysis
When
*r
is used as an index to access arraym_plEncodingLUT[0]
, an out-of-bounds error occursBy backtracking, I find where
m_plEncodingLUT[0]
is allocated heap memoryAs the size of
LONG
is 4,bytesize / sizeof(LONG) = 8196
, which is smaller than the aforementioned index*r = 13621
.Therefore, a heap-buffer-overflow (even SEGV when
*r
is big enough) occurs.ASAN says
While checked with ASAN, this problem might trigger 3 different crashes (heap-buffer-overflow / SEGV / unknown-crash), depending on the extent of the array index out-of-bounds
POC
poc-array-out-of-bounds.zip