summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2020-03-07 07:38:51 -0800
committerThiago Macieira <thiago.macieira@intel.com>2020-04-10 14:45:53 -0300
commit07e8390b455b9aa81b7472b401fcaf5dfd48d3c1 (patch)
treed0b7858eccf37390fd39a54cdc75dc00a35ff491 /tests
parentbe28eb44a8097830e303d3de777d7a57e04fb879 (diff)
QCborValue::fromCbor: Apply a recursion limit to decoding
A simple 16k file can produce deep enough recursion in Qt to cause stack overflow. So prevent that. I tested 4096 recursions just fine on my Linux system (8 MB stack), but decided 1024 was sufficient, as this code will also be run on embedded systems that could have smaller stacks. [ChangeLog][QtCore][QCborValue] fromCbor() now limits decoding to at most 1024 nested maps, arrays, and tags to prevent stack overflows. This should be sufficient for most uses of CBOR. An API to limit further or to relax the limit will be provided in 5.15. Meanwhile, if decoding more is required, QCborStreamReader can be used (note that each level of map and array allocates memory). Change-Id: Iaa63461109844e978376fffd15fa0fbefbf607a2 Reviewed-by: Lars Knoll <lars.knoll@qt.io> (cherry picked from commit 02d595946faa7a21f6aa4109227f7e90db20ae7a) Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp b/tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp
index 9d453bd38e..0a2b39c93e 100644
--- a/tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp
+++ b/tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp
@@ -101,6 +101,8 @@ private slots:
void fromCborStreamReaderIODevice();
void validation_data();
void validation();
+ void recursionLimit_data();
+ void recursionLimit();
void toDiagnosticNotation_data();
void toDiagnosticNotation();
};
@@ -1581,6 +1583,58 @@ void tst_QCborValue::validation()
}
}
+void tst_QCborValue::recursionLimit_data()
+{
+ constexpr int RecursionAttempts = 4096;
+ QTest::addColumn<QByteArray>("data");
+ QByteArray arrays(RecursionAttempts, char(0x81));
+ QByteArray _arrays(RecursionAttempts, char(0x9f));
+ QByteArray maps(RecursionAttempts, char(0xa1));
+ QByteArray _maps(RecursionAttempts, char(0xbf));
+ QByteArray tags(RecursionAttempts, char(0xc0));
+
+ QTest::newRow("array-nesting-too-deep") << arrays;
+ QTest::newRow("_array-nesting-too-deep") << _arrays;
+ QTest::newRow("map-nesting-too-deep") << maps;
+ QTest::newRow("_map-nesting-too-deep") << _maps;
+ QTest::newRow("tag-nesting-too-deep") << tags;
+
+ QByteArray mixed(5 * RecursionAttempts, Qt::Uninitialized);
+ char *ptr = mixed.data();
+ for (int i = 0; i < RecursionAttempts; ++i) {
+ quint8 type = qBound(quint8(QCborStreamReader::Array), quint8(i & 0x80), quint8(QCborStreamReader::Tag));
+ quint8 additional_info = i & 0x1f;
+ if (additional_info == 0x1f)
+ (void)additional_info; // leave it
+ else if (additional_info > 0x1a)
+ additional_info = 0x1a;
+ else if (additional_info < 1)
+ additional_info = 1;
+
+ *ptr++ = type | additional_info;
+ if (additional_info == 0x18) {
+ *ptr++ = uchar(i);
+ } else if (additional_info == 0x19) {
+ qToBigEndian(ushort(i), ptr);
+ ptr += 2;
+ } else if (additional_info == 0x1a) {
+ qToBigEndian(uint(i), ptr);
+ ptr += 4;
+ }
+ }
+
+ QTest::newRow("mixed-nesting-too-deep") << mixed;
+}
+
+void tst_QCborValue::recursionLimit()
+{
+ QFETCH(QByteArray, data);
+
+ QCborParserError error;
+ QCborValue decoded = QCborValue::fromCbor(data, &error);
+ QCOMPARE(error.error, QCborError::NestingTooDeep);
+}
+
void tst_QCborValue::toDiagnosticNotation_data()
{
QTest::addColumn<QCborValue>("v");