From 70e439eb5ffaae59e9df7ad645f0680d2b94e657 Mon Sep 17 00:00:00 2001 From: Janek Bevendorff Date: Sat, 31 Oct 2026 17:34:24 +0100 Subject: [PATCH] Fix potential integer overflows in importers This is a fixup to #13682 (thanks @tikipiya). I also found potential overflows in the OPVault importer, which are now fixed. These overflows are mostly relevant on 32-bit systems only, where Qt's decision to use signed ints for all size types comes to bite you. Small API change: The size parameter of KeePass2RandomStream::randomBytes was widened from int to qsizetype, which is the more appropriate type anyway. --- src/format/KeePass1Reader.cpp | 18 +++++++++--------- src/format/KeePass2RandomStream.cpp | 8 ++++---- src/format/KeePass2RandomStream.h | 2 +- src/format/OPUXReader.cpp | 2 +- src/format/OpData01.cpp | 14 +++++++++----- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/format/KeePass1Reader.cpp b/src/format/KeePass1Reader.cpp index c718e96d5..f785b5b9e 100644 --- a/src/format/KeePass1Reader.cpp +++ b/src/format/KeePass1Reader.cpp @@ -819,7 +819,7 @@ bool KeePass1Reader::parseGroupTreeState(const QByteArray& data) return false; } - int pos = 0; + qsizetype pos = 0; auto num = Endian::bytesToSizedInt(data.mid(pos, 4), KeePass1::BYTEORDER); pos += 4; @@ -849,7 +849,7 @@ bool KeePass1Reader::parseCustomIcons4(const QByteArray& data) return false; } - int pos = 0; + qsizetype pos = 0; auto numIcons = Endian::bytesToSizedInt(data.mid(pos, 4), KeePass1::BYTEORDER); pos += 4; @@ -862,14 +862,14 @@ bool KeePass1Reader::parseCustomIcons4(const QByteArray& data) QList iconUuids; - for (quint32 i = 0; i < numIcons; i++) { - if (data.size() < (pos + 4)) { + for (quint32 i = 0; i < numIcons; ++i) { + if (static_cast(data.size()) < (static_cast(pos) + 4)) { return false; } auto iconSize = Endian::bytesToSizedInt(data.mid(pos, 4), KeePass1::BYTEORDER); pos += 4; - if (static_cast(data.size()) < (pos + iconSize)) { + if (static_cast(data.size()) < (pos + static_cast(iconSize))) { return false; } QByteArray icon = data.mid(pos, iconSize); @@ -880,11 +880,11 @@ bool KeePass1Reader::parseCustomIcons4(const QByteArray& data) m_db->metadata()->addCustomIcon(uuid, icon); } - if (static_cast(data.size()) < (pos + numEntries * 20)) { + if (static_cast(data.size()) < (pos + static_cast(numEntries) * 20)) { return false; } - for (quint32 i = 0; i < numEntries; i++) { + for (quint32 i = 0; i < numEntries; ++i) { QByteArray entryUuid = data.mid(pos, 16); pos += 16; @@ -896,11 +896,11 @@ bool KeePass1Reader::parseCustomIcons4(const QByteArray& data) } } - if (static_cast(data.size()) < (pos + numGroups * 8)) { + if (static_cast(data.size()) < (pos + static_cast(numGroups) * 8)) { return false; } - for (quint32 i = 0; i < numGroups; i++) { + for (quint32 i = 0; i < numGroups; ++i) { auto groupId = Endian::bytesToSizedInt(data.mid(pos, 4), KeePass1::BYTEORDER); pos += 4; diff --git a/src/format/KeePass2RandomStream.cpp b/src/format/KeePass2RandomStream.cpp index da6df20ad..6b79b342b 100644 --- a/src/format/KeePass2RandomStream.cpp +++ b/src/format/KeePass2RandomStream.cpp @@ -40,11 +40,11 @@ bool KeePass2RandomStream::init(SymmetricCipher::Mode mode, const QByteArray& ke return false; } -QByteArray KeePass2RandomStream::randomBytes(int size, bool* ok) +QByteArray KeePass2RandomStream::randomBytes(qsizetype size, bool* ok) { QByteArray result; - int bytesRemaining = size; + qsizetype bytesRemaining = size; while (bytesRemaining > 0) { if (m_buffer.size() == m_offset) { @@ -77,7 +77,7 @@ QByteArray KeePass2RandomStream::process(const QByteArray& data, bool* ok) QByteArray result; result.resize(data.size()); - for (int i = 0; i < data.size(); i++) { + for (qsizetype i = 0; i < data.size(); i++) { result[i] = data[i] ^ randomData[i]; } @@ -93,7 +93,7 @@ bool KeePass2RandomStream::processInPlace(QByteArray& data) return false; } - for (int i = 0; i < data.size(); i++) { + for (qsizetype i = 0; i < data.size(); i++) { data[i] = data[i] ^ randomData[i]; } diff --git a/src/format/KeePass2RandomStream.h b/src/format/KeePass2RandomStream.h index 7d48bd266..d92b8b288 100644 --- a/src/format/KeePass2RandomStream.h +++ b/src/format/KeePass2RandomStream.h @@ -26,7 +26,7 @@ public: KeePass2RandomStream() = default; bool init(SymmetricCipher::Mode mode, const QByteArray& key); - QByteArray randomBytes(int size, bool* ok); + QByteArray randomBytes(qsizetype size, bool* ok); QByteArray process(const QByteArray& data, bool* ok); Q_REQUIRED_RESULT bool processInPlace(QByteArray& data); QString errorString() const; diff --git a/src/format/OPUXReader.cpp b/src/format/OPUXReader.cpp index 925587573..d383dcac1 100644 --- a/src/format/OPUXReader.cpp +++ b/src/format/OPUXReader.cpp @@ -42,7 +42,7 @@ namespace } // Read export.data into memory - int bytes, bytesRead = 0; + qsizetype bytes, bytesRead = 0; QByteArray data; unzOpenCurrentFile(uf); do { diff --git a/src/format/OpData01.cpp b/src/format/OpData01.cpp index a51ff8fdb..4ab515a65 100644 --- a/src/format/OpData01.cpp +++ b/src/format/OpData01.cpp @@ -53,7 +53,7 @@ bool OpData01::decode(const QByteArray& data, const QByteArray& key, const QByte /*! * The next 8 bytes contain the length in bytes of the plaintext as a little endian unsigned 64 bit integer. */ - qlonglong len; + quint64 len; in >> len; /*! @@ -79,13 +79,17 @@ bool OpData01::decode(const QByteArray& data, const QByteArray& key, const QByte * to the plaintext. Otherwise, between 1 and 15 (inclusive) bytes of random data are prepended to the plaintext * to achieve an even multiple of blocks. */ - const int blockSize = cipher.blockSize(cipher.mode()); - int randomBytes = blockSize - (len % blockSize); + const qsizetype blockSize = cipher.blockSize(cipher.mode()); + qsizetype randomBytes = blockSize - (len % blockSize); if (randomBytes == 0) { // add random block randomBytes = blockSize; } - qlonglong clear_len = len + randomBytes; + if (len > static_cast(std::numeric_limits::max()) - randomBytes) { + m_errorStr = tr("Plain text length out of range: %1").arg(len); + return false; + } + const qsizetype clear_len = static_cast(len) + randomBytes; QByteArray qbaCT(clear_len, '\0'); in.readRawData(qbaCT.data(), clear_len); @@ -115,7 +119,7 @@ bool OpData01::decode(const QByteArray& data, const QByteArray& key, const QByte // Remove random bytes const QByteArray& clearText = qbaCT.mid(randomBytes); - if (clearText.size() != len) { + if (clearText.size() != static_cast(len)) { m_errorStr = tr("Expected %1 bytes of clear-text, found %2").arg(len, clearText.size()); return false; }