From a84f09136e675f7a080638beae86ec3ec8fb4f94 Mon Sep 17 00:00:00 2001 From: Eirik Aavitsland Date: Wed, 26 Aug 2020 09:04:35 +0200 Subject: Implement basic format check also for compressed svgs For uncompressed files, QSvgIOhandler::canRead() will reject any file that does not start out with a svg or xml tag. That rudimentary check was never done for compressed files (svgz). Implement the check during the decompressing itself, so that we can fail early and not waste time and memory decompressing potentially huge files that are anyway not valid svgs. Fixes: oss-fuzz-24611 Change-Id: I154efd8adafe7f09307e8b28a66b536539b1e4bd Reviewed-by: Robert Loehning Reviewed-by: Thiago Macieira (cherry picked from commit 93466dad6613085a5044a862a3a84a4eba6fcef9) Reviewed-by: Qt Cherry-pick Bot --- src/svg/qsvgtinydocument.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/svg/qsvgtinydocument.cpp b/src/svg/qsvgtinydocument.cpp index a202a25..d7a6c8e 100644 --- a/src/svg/qsvgtinydocument.cpp +++ b/src/svg/qsvgtinydocument.cpp @@ -71,13 +71,15 @@ QSvgTinyDocument::~QSvgTinyDocument() } #ifndef QT_NO_COMPRESS +static QByteArray qt_inflateSvgzDataFrom(QIODevice *device, bool doCheckContent = true); # ifdef QT_BUILD_INTERNAL -Q_AUTOTEST_EXPORT QByteArray qt_inflateGZipDataFrom(QIODevice *device); -# else -static QByteArray qt_inflateGZipDataFrom(QIODevice *device); +Q_AUTOTEST_EXPORT QByteArray qt_inflateGZipDataFrom(QIODevice *device) +{ + return qt_inflateSvgzDataFrom(device, false); // autotest wants unchecked result +} # endif -QByteArray qt_inflateGZipDataFrom(QIODevice *device) +static QByteArray qt_inflateSvgzDataFrom(QIODevice *device, bool doCheckContent) { if (!device) return QByteArray(); @@ -153,6 +155,17 @@ QByteArray qt_inflateGZipDataFrom(QIODevice *device) // it means we have to provide more data, so exit the loop here } while (!zlibStream.avail_out); + if (doCheckContent) { + // Quick format check, equivalent to QSvgIOHandler::canRead() + QByteArray buf = destination.left(8); + if (!buf.contains("(&contents)); - const QByteArray inflated = qt_inflateGZipDataFrom(&buffer); + const QByteArray inflated = qt_inflateSvgzDataFrom(&buffer); if (inflated.isNull()) return nullptr; return load(inflated); -- cgit v1.2.1