Fix buffer overflow in XBM parser

Avoid parsing over the buffer limit, or interpreting non-hex
as hex.

This still leaves parsing of lines longer than 300 chars
unreliable

Change-Id: I1c57a7e530c4380f6f9040b2ec729ccd7dc7a5fb
Reviewed-by: Robert Loehning <robert.loehning@qt.io>
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
(cherry picked from commit c562c1fc19629fb505acd0f6380604840b634211)
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
This commit is contained in:
Allan Sandfeld Jensen 2020-07-23 11:48:48 +02:00
parent a2b1ab0e6e
commit 35ecd0b69d
2 changed files with 40 additions and 1 deletions

View File

@ -158,7 +158,9 @@ static bool read_xbm_body(QIODevice *device, int w, int h, QImage *outImage)
w = (w+7)/8; // byte width
while (y < h) { // for all encoded bytes...
if (p) { // p = "0x.."
if (p && p < (buf + readBytes - 3)) { // p = "0x.."
if (!isxdigit(p[2]) || !isxdigit(p[3]))
return false;
*b++ = hex2byte(p+2);
p += 2;
if (++x == w && ++y < h) {

View File

@ -168,6 +168,7 @@ private slots:
void devicePixelRatio();
void xpmBufferOverflow();
void xbmBufferHandling();
private:
QString prefix;
@ -2010,5 +2011,41 @@ void tst_QImageReader::xpmBufferOverflow()
QImageReader(":/images/oss-fuzz-23988.xpm").read();
}
void tst_QImageReader::xbmBufferHandling()
{
uint8_t original_buffer[256];
for (int i = 0; i < 256; ++i)
original_buffer[i] = i;
QImage image(original_buffer, 256, 8, QImage::Format_MonoLSB);
image.setColorTable({0xff000000, 0xffffffff});
QByteArray buffer;
{
QBuffer buf(&buffer);
QImageWriter writer(&buf, "xbm");
writer.write(image);
}
QCOMPARE(QImage::fromData(buffer, "xbm"), image);
auto i = buffer.indexOf(',');
buffer.insert(i + 1, " ");
QCOMPARE(QImage::fromData(buffer, "xbm"), image);
buffer.insert(i + 1, " ");
QCOMPARE(QImage::fromData(buffer, "xbm"), image);
buffer.insert(i + 1, " ");
#if 0 // Lines longer than 300 chars not supported currently
QCOMPARE(QImage::fromData(buffer, "xbm"), image);
#endif
i = buffer.lastIndexOf("\n ");
buffer.truncate(i + 1);
buffer.append(QByteArray(297, ' '));
buffer.append("0x");
// Only check we get no buffer overflow
QImage::fromData(buffer, "xbm");
}
QTEST_MAIN(tst_QImageReader)
#include "tst_qimagereader.moc"