From 46c22c4c15b69e5f2f44d3ca308d36dc5f14c6f1 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 8e12d5c..3bd0064 100644 --- a/src/svg/qsvgtinydocument.cpp +++ b/src/svg/qsvgtinydocument.cpp @@ -73,13 +73,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(); @@ -155,6 +157,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