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.
This commit is contained in:
Janek Bevendorff authored and Janek Bevendorff committed 2026-11-01 13:53:41 +01:00
1 parent 0491ec6f06
commit 70e439eb5f
5 files changed
+24 -20

No files matched your search

+9 -9
View File
@@ -819,7 +819,7 @@ bool KeePass1Reader::parseGroupTreeState(const QByteArray& data)
return false;
}
int pos = 0;
qsizetype pos = 0;
auto num = Endian::bytesToSizedInt<quint32>(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<quint32>(data.mid(pos, 4), KeePass1::BYTEORDER);
pos += 4;
@@ -862,14 +862,14 @@ bool KeePass1Reader::parseCustomIcons4(const QByteArray& data)
QList<QUuid> iconUuids;
for (quint32 i = 0; i < numIcons; i++) {
if (data.size() < (pos + 4)) {
for (quint32 i = 0; i < numIcons; ++i) {
if (static_cast<quint64>(data.size()) < (static_cast<quint64>(pos) + 4)) {
return false;
}
auto iconSize = Endian::bytesToSizedInt<quint32>(data.mid(pos, 4), KeePass1::BYTEORDER);
pos += 4;
if (static_cast<quint32>(data.size()) < (pos + iconSize)) {
if (static_cast<quint64>(data.size()) < (pos + static_cast<quint64>(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<quint32>(data.size()) < (pos + numEntries * 20)) {
if (static_cast<quint64>(data.size()) < (pos + static_cast<quint64>(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<quint32>(data.size()) < (pos + numGroups * 8)) {
if (static_cast<quint64>(data.size()) < (pos + static_cast<quint64>(numGroups) * 8)) {
return false;
}
for (quint32 i = 0; i < numGroups; i++) {
for (quint32 i = 0; i < numGroups; ++i) {
auto groupId = Endian::bytesToSizedInt<quint32>(data.mid(pos, 4), KeePass1::BYTEORDER);
pos += 4;
+4 -4
View File
@@ -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];
}
+1 -1
View File
@@ -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;
+1 -1
View File
@@ -42,7 +42,7 @@ namespace
}
// Read export.data into memory
int bytes, bytesRead = 0;
qsizetype bytes, bytesRead = 0;
QByteArray data;
unzOpenCurrentFile(uf);
do {
+9 -5
View File
@@ -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<quint64>(std::numeric_limits<qsizetype>::max()) - randomBytes) {
m_errorStr = tr("Plain text length out of range: %1").arg(len);
return false;
}
const qsizetype clear_len = static_cast<qsizetype>(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<qsizetype>(len)) {
m_errorStr = tr("Expected %1 bytes of clear-text, found %2").arg(len, clearText.size());
return false;
}