blob: f881c3e53da77eadd3c01e3ec54004e76d23988c [file] [log] [blame]
Nikhil Namjoshida6e5572023-03-13 10:52:53 -07001#include "bej_common_test.hpp"
kasunathe8946af2022-05-23 12:32:09 -07002#include "bej_decoder_json.hpp"
Brandon Kim07abbf82025-06-23 21:03:07 +00003#include "bej_encoder_json.hpp"
kasunathe8946af2022-05-23 12:32:09 -07004
kasunathe8946af2022-05-23 12:32:09 -07005#include <memory>
kasunathe8946af2022-05-23 12:32:09 -07006#include <string_view>
Brandon Kim07abbf82025-06-23 21:03:07 +00007#include <vector>
kasunathe8946af2022-05-23 12:32:09 -07008
9#include <gmock/gmock-matchers.h>
10#include <gmock/gmock.h>
11#include <gtest/gtest.h>
12
13namespace libbej
14{
15
kasunathe8946af2022-05-23 12:32:09 -070016struct BejDecoderTestParams
17{
18 const std::string testName;
19 const BejTestInputFiles inputFiles;
20};
21
Brian Maa46f9852024-09-27 16:57:20 +080022void PrintTo(const BejDecoderTestParams& params, std::ostream* os)
23{
24 *os << params.testName;
25}
26
kasunathe8946af2022-05-23 12:32:09 -070027using BejDecoderTest = testing::TestWithParam<BejDecoderTestParams>;
28
29const BejTestInputFiles driveOemTestFiles = {
30 .jsonFile = "../test/json/drive_oem.json",
31 .schemaDictionaryFile = "../test/dictionaries/drive_oem_dict.bin",
32 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
33 .errorDictionaryFile = "",
34 .encodedStreamFile = "../test/encoded/drive_oem_enc.bin",
35};
36
37const BejTestInputFiles circuitTestFiles = {
38 .jsonFile = "../test/json/circuit.json",
39 .schemaDictionaryFile = "../test/dictionaries/circuit_dict.bin",
40 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
41 .errorDictionaryFile = "",
42 .encodedStreamFile = "../test/encoded/circuit_enc.bin",
43};
44
45const BejTestInputFiles storageTestFiles = {
46 .jsonFile = "../test/json/storage.json",
47 .schemaDictionaryFile = "../test/dictionaries/storage_dict.bin",
48 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
49 .errorDictionaryFile = "",
50 .encodedStreamFile = "../test/encoded/storage_enc.bin",
51};
52
53const BejTestInputFiles dummySimpleTestFiles = {
54 .jsonFile = "../test/json/dummysimple.json",
55 .schemaDictionaryFile = "../test/dictionaries/dummy_simple_dict.bin",
56 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
57 .errorDictionaryFile = "",
58 .encodedStreamFile = "../test/encoded/dummy_simple_enc.bin",
59};
60
kasunathe8946af2022-05-23 12:32:09 -070061TEST_P(BejDecoderTest, Decode)
62{
63 const BejDecoderTestParams& test_case = GetParam();
64 auto inputsOrErr = loadInputs(test_case.inputFiles);
65 EXPECT_TRUE(inputsOrErr);
66
67 BejDictionaries dictionaries = {
68 .schemaDictionary = inputsOrErr->schemaDictionary,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +000069 .schemaDictionarySize = inputsOrErr->schemaDictionarySize,
kasunathe8946af2022-05-23 12:32:09 -070070 .annotationDictionary = inputsOrErr->annotationDictionary,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +000071 .annotationDictionarySize = inputsOrErr->annotationDictionarySize,
kasunathe8946af2022-05-23 12:32:09 -070072 .errorDictionary = inputsOrErr->errorDictionary,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +000073 .errorDictionarySize = inputsOrErr->errorDictionarySize,
kasunathe8946af2022-05-23 12:32:09 -070074 };
75
76 BejDecoderJson decoder;
77 EXPECT_THAT(decoder.decode(dictionaries, inputsOrErr->encodedStream), 0);
78 std::string decoded = decoder.getOutput();
79 nlohmann::json jsonDecoded = nlohmann::json::parse(decoded);
80
81 // Just comparing nlohmann::json types could lead to errors. It compares the
82 // byte values. So int64 and unit64 comparisons might be incorrect. Eg:
83 // bytes values for -5 and 18446744073709551611 are the same. So compare the
84 // string values.
85 EXPECT_TRUE(jsonDecoded.dump() == inputsOrErr->expectedJson.dump());
86}
87
Kasun Athukorala6fd03fb2025-07-15 05:19:39 +000088/**
89 * TODO: Add more test cases.
90 * - Test Enums inside array elemets
91 * - Array inside an array: is this a valid case?
92 * - Real numbers with exponent part
93 * - Every type inside an array.
94 */
95INSTANTIATE_TEST_SUITE_P(
96 , BejDecoderTest,
97 testing::ValuesIn<BejDecoderTestParams>({
98 {"DriveOEM", driveOemTestFiles},
99 {"Circuit", circuitTestFiles},
100 {"Storage", storageTestFiles},
101 {"DummySimple", dummySimpleTestFiles},
102 }),
103 [](const testing::TestParamInfo<BejDecoderTest::ParamType>& info) {
104 return info.param.testName;
105 });
106
Brandon Kim07abbf82025-06-23 21:03:07 +0000107TEST(BejDecoderSecurityTest, MaxOperationsLimit)
108{
109 auto inputsOrErr = loadInputs(dummySimpleTestFiles);
110 ASSERT_TRUE(inputsOrErr);
111
112 BejDictionaries dictionaries = {
113 .schemaDictionary = inputsOrErr->schemaDictionary,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000114 .schemaDictionarySize = inputsOrErr->schemaDictionarySize,
Brandon Kim07abbf82025-06-23 21:03:07 +0000115 .annotationDictionary = inputsOrErr->annotationDictionary,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000116 .annotationDictionarySize = inputsOrErr->annotationDictionarySize,
Brandon Kim07abbf82025-06-23 21:03:07 +0000117 .errorDictionary = inputsOrErr->errorDictionary,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000118 .errorDictionarySize = inputsOrErr->errorDictionarySize,
Brandon Kim07abbf82025-06-23 21:03:07 +0000119 };
120
121 // Each array element below consists of a set and two properties, resulting
122 // in 3 operations. 400,000 elements will result in 1,200,000 operations,
123 // which should exceed the limit of 1,000,000.
124 constexpr int numElements = 400000;
125
126 auto root = std::make_unique<RedfishPropertyParent>();
127 bejTreeInitSet(root.get(), "DummySimple");
128
129 auto childArray = std::make_unique<RedfishPropertyParent>();
130 bejTreeInitArray(childArray.get(), "ChildArrayProperty");
131 bejTreeLinkChildToParent(root.get(), childArray.get());
132
133 std::vector<std::unique_ptr<RedfishPropertyParent>> sets;
134 std::vector<std::unique_ptr<RedfishPropertyLeafBool>> bools;
135 std::vector<std::unique_ptr<RedfishPropertyLeafEnum>> enums;
136 sets.reserve(numElements);
137 bools.reserve(numElements);
138 enums.reserve(numElements);
139
140 for (int i = 0; i < numElements; ++i)
141 {
142 auto chArraySet = std::make_unique<RedfishPropertyParent>();
143 bejTreeInitSet(chArraySet.get(), nullptr);
144
145 auto chArraySetBool = std::make_unique<RedfishPropertyLeafBool>();
146 bejTreeAddBool(chArraySet.get(), chArraySetBool.get(), "AnotherBoolean",
147 true);
148
149 auto chArraySetLs = std::make_unique<RedfishPropertyLeafEnum>();
150 bejTreeAddEnum(chArraySet.get(), chArraySetLs.get(), "LinkStatus",
151 "NoLink");
152
153 bejTreeLinkChildToParent(childArray.get(), chArraySet.get());
154
155 sets.push_back(std::move(chArraySet));
156 bools.push_back(std::move(chArraySetBool));
157 enums.push_back(std::move(chArraySetLs));
158 }
159
160 libbej::BejEncoderJson encoder;
161 encoder.encode(&dictionaries, bejMajorSchemaClass, root.get());
162 std::vector<uint8_t> outputBuffer = encoder.getOutput();
163
164 BejDecoderJson decoder;
165 EXPECT_THAT(decoder.decode(dictionaries, std::span(outputBuffer)),
Brandon Kimef01d4c2025-06-23 21:13:04 +0000166 bejErrorNotSupported);
Brandon Kim07abbf82025-06-23 21:03:07 +0000167}
168
Brandon Kim9eb01172025-06-23 21:26:59 +0000169TEST(BejDecoderSecurityTest, RealWithTooManyLeadingZeros)
170{
171 auto inputsOrErr = loadInputs(dummySimpleTestFiles);
172 ASSERT_TRUE(inputsOrErr);
173
174 BejDictionaries dictionaries = {
175 .schemaDictionary = inputsOrErr->schemaDictionary,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000176 .schemaDictionarySize = inputsOrErr->schemaDictionarySize,
Brandon Kim9eb01172025-06-23 21:26:59 +0000177 .annotationDictionary = inputsOrErr->annotationDictionary,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000178 .annotationDictionarySize = inputsOrErr->annotationDictionarySize,
Brandon Kim9eb01172025-06-23 21:26:59 +0000179 .errorDictionary = inputsOrErr->errorDictionary,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000180 .errorDictionarySize = inputsOrErr->errorDictionarySize,
Brandon Kim9eb01172025-06-23 21:26:59 +0000181 };
182
183 auto root = std::make_unique<RedfishPropertyParent>();
184 bejTreeInitSet(root.get(), "DummySimple");
185
186 // 1.003 was randomely chosen
187 auto real = std::make_unique<RedfishPropertyLeafReal>();
188 bejTreeAddReal(root.get(), real.get(), "SampleRealProperty", 1.003);
189
190 libbej::BejEncoderJson encoder;
191 encoder.encode(&dictionaries, bejMajorSchemaClass, root.get());
192 std::vector<uint8_t> outputBuffer = encoder.getOutput();
193
194 // Manually tamper with the encoded stream to create the attack vector.
195 // We will find the `bejReal` property and overwrite its `zeroCount`.
196 // The property "SampleRealProperty" has sequence number 4. The encoded
197 // sequence number is `(4 << 1) | 0 = 8`. The nnint for 8 is `0x01, 0x08`.
198 const std::vector<uint8_t> realPropSeqNum = {0x01, 0x08};
199 auto it = std::search(outputBuffer.begin(), outputBuffer.end(),
200 realPropSeqNum.begin(), realPropSeqNum.end());
201 ASSERT_NE(it, outputBuffer.end()) << "Could not find bejReal property";
202
203 // The structure of a bejReal SFLV is: S(nnint) F(u8) L(nnint) V(...)
204 // The structure of V is: nnint(len(whole)), int(whole), nnint(zeroCount)...
205 // Find the start of the value (V) by skipping S, F, and L.
206 size_t sflvOffset = std::distance(outputBuffer.begin(), it);
207 const uint8_t* streamPtr = outputBuffer.data() + sflvOffset;
208 // Skip S
209 streamPtr += bejGetNnintSize(streamPtr);
210 // Skip F
211 streamPtr++;
212 // Skip L
213 const uint8_t* valuePtr = streamPtr + bejGetNnintSize(streamPtr);
214
215 // Find the start of zeroCount within V.
216 const uint8_t* zeroCountPtr = valuePtr + bejGetNnintSize(valuePtr);
217 zeroCountPtr += bejGetNnint(valuePtr); // Skip int(whole)
218
219 // The original zeroCount for 1.003 is 2. nnint(2) is `0x01, 0x02`.
220 // We replace it with a zeroCount of 101, which exceeds the limit.
221 // nnint(101) is `0x01, 101`. The size is the same (2 bytes), so we don't
222 // need to update the SFLV length field (L).
223 ASSERT_EQ(bejGetNnint(zeroCountPtr), 2);
224 size_t zeroCountOffset = zeroCountPtr - outputBuffer.data();
225 // nnint value for 101
226 outputBuffer[zeroCountOffset + 1] = 101;
227
228 BejDecoderJson decoder;
229 EXPECT_THAT(decoder.decode(dictionaries, std::span(outputBuffer)),
230 bejErrorInvalidSize);
231}
232
Kasun Athukorala485044b2025-07-15 00:59:36 +0000233TEST(BejDecoderSecurityTest, StringTooLong)
234{
235 auto inputsOrErr = loadInputs(dummySimpleTestFiles);
236 ASSERT_TRUE(inputsOrErr);
237
238 BejDictionaries dictionaries = {
239 .schemaDictionary = inputsOrErr->schemaDictionary,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000240 .schemaDictionarySize = inputsOrErr->schemaDictionarySize,
Kasun Athukorala485044b2025-07-15 00:59:36 +0000241 .annotationDictionary = inputsOrErr->annotationDictionary,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000242 .annotationDictionarySize = inputsOrErr->annotationDictionarySize,
Kasun Athukorala485044b2025-07-15 00:59:36 +0000243 .errorDictionary = inputsOrErr->errorDictionary,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000244 .errorDictionarySize = inputsOrErr->errorDictionarySize,
Kasun Athukorala485044b2025-07-15 00:59:36 +0000245 };
246
247 auto root = std::make_unique<RedfishPropertyParent>();
248 bejTreeInitSet(root.get(), "DummySimple");
249
250 // Create a string with a length greater than MAX_BEJ_STRING_LEN (65536).
251 std::string longString(65537, 'A');
252
253 auto stringProp = std::make_unique<RedfishPropertyLeafString>();
254 bejTreeAddString(root.get(), stringProp.get(), "Id", longString.c_str());
255
256 libbej::BejEncoderJson encoder;
257 encoder.encode(&dictionaries, bejMajorSchemaClass, root.get());
258 std::vector<uint8_t> outputBuffer = encoder.getOutput();
259
260 // The decoder should return an error because the string is too long.
261 BejDecoderJson decoder;
262 EXPECT_THAT(decoder.decode(dictionaries, std::span(outputBuffer)),
263 bejErrorInvalidSize);
264}
265
Kasun Athukorala6fd03fb2025-07-15 05:19:39 +0000266TEST(BejDecoderSecurityTest, ValueBeyondStreamLength)
267{
268 auto inputsOrErr = loadInputs(dummySimpleTestFiles);
269 ASSERT_TRUE(inputsOrErr);
270
271 BejDictionaries dictionaries = {
272 .schemaDictionary = inputsOrErr->schemaDictionary,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000273 .schemaDictionarySize = inputsOrErr->schemaDictionarySize,
Kasun Athukorala6fd03fb2025-07-15 05:19:39 +0000274 .annotationDictionary = inputsOrErr->annotationDictionary,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000275 .annotationDictionarySize = inputsOrErr->annotationDictionarySize,
Kasun Athukorala6fd03fb2025-07-15 05:19:39 +0000276 .errorDictionary = inputsOrErr->errorDictionary,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000277 .errorDictionarySize = inputsOrErr->errorDictionarySize,
Kasun Athukorala6fd03fb2025-07-15 05:19:39 +0000278 };
279
280 auto root = std::make_unique<RedfishPropertyParent>();
281 bejTreeInitSet(root.get(), "DummySimple");
282
283 auto intProp = std::make_unique<RedfishPropertyLeafInt>();
284 bejTreeAddInteger(root.get(), intProp.get(), "SampleIntegerProperty", 123);
285
286 libbej::BejEncoderJson encoder;
287 encoder.encode(&dictionaries, bejMajorSchemaClass, root.get());
288 std::vector<uint8_t> outputBuffer = encoder.getOutput();
289
290 // Tamper with the encoded stream to simulate a value extending beyond the
291 // stream length. This stream only has an integer 0x7b. The value before it
292 // is the length tuple.
293 outputBuffer[outputBuffer.size() - 2] = 0x05;
294
295 BejDecoderJson decoder;
296 EXPECT_THAT(decoder.decode(dictionaries, std::span(outputBuffer)),
297 bejErrorInvalidSize);
298}
kasunathe8946af2022-05-23 12:32:09 -0700299
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000300TEST(BejDecoderSecurityTest, InvalidSchemaDictionarySize)
301{
302 auto inputsOrErr = loadInputs(dummySimpleTestFiles);
303 ASSERT_TRUE(inputsOrErr);
304
305 BejDictionaries dictionaries = {
306 .schemaDictionary = inputsOrErr->schemaDictionary,
307 .schemaDictionarySize = 10,
308 .annotationDictionary = inputsOrErr->annotationDictionary,
309 .annotationDictionarySize = inputsOrErr->annotationDictionarySize,
310 .errorDictionary = inputsOrErr->errorDictionary,
311 .errorDictionarySize = inputsOrErr->errorDictionarySize,
312 };
313
314 BejDecoderJson decoder;
315 EXPECT_THAT(decoder.decode(dictionaries, inputsOrErr->encodedStream),
316 bejErrorInvalidSize);
317}
318
319TEST(BejDecoderSecurityTest, InvalidAnnotationDictionarySize)
320{
321 auto inputsOrErr = loadInputs(dummySimpleTestFiles);
322 ASSERT_TRUE(inputsOrErr);
323
324 BejDictionaries dictionaries = {
325 .schemaDictionary = inputsOrErr->schemaDictionary,
326 .schemaDictionarySize = inputsOrErr->schemaDictionarySize,
327 .annotationDictionary = inputsOrErr->annotationDictionary,
328 .annotationDictionarySize = 10,
329 .errorDictionary = inputsOrErr->errorDictionary,
330 .errorDictionarySize = inputsOrErr->errorDictionarySize,
331 };
332
333 BejDecoderJson decoder;
334 EXPECT_THAT(decoder.decode(dictionaries, inputsOrErr->encodedStream),
335 bejErrorInvalidSize);
336}
337
338TEST(BejDecoderSecurityTest, InvalidErrorDictionarySize)
339{
340 auto inputsOrErr = loadInputs(dummySimpleTestFiles);
341 ASSERT_TRUE(inputsOrErr);
342
343 BejDictionaries dictionaries = {
344 .schemaDictionary = inputsOrErr->schemaDictionary,
345 .schemaDictionarySize = inputsOrErr->schemaDictionarySize,
346 .annotationDictionary = inputsOrErr->annotationDictionary,
347 .annotationDictionarySize = inputsOrErr->annotationDictionarySize,
348 .errorDictionary = inputsOrErr->errorDictionary,
349 .errorDictionarySize = 10,
350 };
351
352 BejDecoderJson decoder;
353 EXPECT_THAT(decoder.decode(dictionaries, inputsOrErr->encodedStream),
354 bejErrorInvalidSize);
355}
356
kasunathe8946af2022-05-23 12:32:09 -0700357} // namespace libbej