Skip to content

Commit 406ad00

Browse files
committed
Adding Fuzzing Targets
1 parent 64f827b commit 406ad00

File tree

4 files changed

+115
-20
lines changed

4 files changed

+115
-20
lines changed

src/credentials/tests/FuzzChipCertPW.cpp

+37-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ using namespace fuzztest;
1313

1414
void ChipCertFuzzer(const std::vector<std::uint8_t> & bytes)
1515
{
16-
1716
ByteSpan span(bytes.data(), bytes.size());
1817

1918
{
@@ -58,6 +57,43 @@ void ChipCertFuzzer(const std::vector<std::uint8_t> & bytes)
5857
MutableByteSpan outCert(outCertBuf);
5958
(void) ConvertChipCertToX509Cert(span, outCert);
6059
}
60+
61+
{
62+
// TODO: #34352 To Move this to a Fixture once Errors related to FuzzTest Fixtures are resolved
63+
ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR);
64+
ByteSpan span(bytes.data(), bytes.size());
65+
ValidateChipRCAC(span);
66+
chip::Platform::MemoryShutdown();
67+
}
6168
}
6269

6370
FUZZ_TEST(ChipCert, ChipCertFuzzer).WithDomains(Arbitrary<std::vector<std::uint8_t>>());
71+
72+
// The Property function for DecodeChipCertFuzzer, The FUZZ_TEST Macro will call this function.
73+
void DecodeChipCertFuzzer(const std::vector<std::uint8_t> & bytes, BitFlags<CertDecodeFlags> aDecodeFlag)
74+
{
75+
ByteSpan span(bytes.data(), bytes.size());
76+
77+
// TODO: #34352 To Move this to a Fixture once Errors related to FuzzTest Fixtures are resolved
78+
ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR);
79+
80+
ChipCertificateData certData;
81+
(void) DecodeChipCert(span, certData, aDecodeFlag);
82+
83+
chip::Platform::MemoryShutdown();
84+
}
85+
86+
// This function allows us to fuzz using one of three CertDecodeFlags flags; by using FuzzTests's `ElementOf` API, we define an
87+
// input domain by explicitly enumerating the set of values in it More Info:
88+
// https://github.com/google/fuzztest/blob/main/doc/domains-reference.md#elementof-domains-element-of
89+
auto AnyCertDecodeFlag()
90+
{
91+
92+
constexpr BitFlags<CertDecodeFlags> NullDecodeFlag;
93+
constexpr BitFlags<CertDecodeFlags> GenTBSHashFlag(CertDecodeFlags::kGenerateTBSHash);
94+
constexpr BitFlags<CertDecodeFlags> TrustAnchorFlag(CertDecodeFlags::kIsTrustAnchor);
95+
96+
return ElementOf<CertDecodeFlags>({ NullDecodeFlag, GenTBSHashFlag, TrustAnchorFlag });
97+
}
98+
99+
FUZZ_TEST(ChipCert, DecodeChipCertFuzzer).WithDomains(Arbitrary<std::vector<std::uint8_t>>(), AnyCertDecodeFlag());

src/lib/dnssd/minimal_mdns/tests/FuzzPacketParsingPW.cpp

+62-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <lib/dnssd/minimal_mdns/RecordData.h>
99

1010
using namespace fuzztest;
11+
using namespace std;
1112

1213
namespace {
1314

@@ -66,4 +67,64 @@ void PacketParserFuzz(const std::vector<std::uint8_t> & bytes)
6667
mdns::Minimal::ParsePacket(packet, &delegate);
6768
}
6869

69-
FUZZ_TEST(MinimalmDNS, PacketParserFuzz).WithDomains(Arbitrary<std::vector<std::uint8_t>>());
70+
class TxtRecordAccumulator : public TxtRecordDelegate
71+
{
72+
public:
73+
using DataType = vector<pair<string, string>>;
74+
75+
void OnRecord(const BytesRange & name, const BytesRange & value) override
76+
{
77+
mData.push_back(make_pair(AsString(name), AsString(value)));
78+
}
79+
80+
DataType & Data() { return mData; }
81+
const DataType & Data() const { return mData; }
82+
83+
private:
84+
DataType mData;
85+
86+
static string AsString(const BytesRange & range)
87+
{
88+
return string(reinterpret_cast<const char *>(range.Start()), reinterpret_cast<const char *>(range.End()));
89+
}
90+
};
91+
92+
void TxtResponderFuzz2(const std::vector<std::uint8_t> & aRecord)
93+
{
94+
95+
bool equal_sign_present = false;
96+
auto equal_sign_pos = aRecord.end();
97+
98+
// This test is only giving a set of values, it can be gives more
99+
vector<uint8_t> prefixedRecord{ static_cast<uint8_t>(aRecord.size()) };
100+
101+
prefixedRecord.insert(prefixedRecord.end(), aRecord.begin(), aRecord.end());
102+
103+
TxtRecordAccumulator accumulator;
104+
105+
// The Function under Test, Check that the function does not Crash
106+
ParseTxtRecord(BytesRange(prefixedRecord.data(), (&prefixedRecord.back() + 1)), &accumulator);
107+
108+
for (auto it = aRecord.begin(); it != aRecord.end(); it++)
109+
{
110+
// if this is first `=` found in the fuzzed record
111+
if ('=' == static_cast<char>(*it) && false == equal_sign_present)
112+
{
113+
equal_sign_present = true;
114+
equal_sign_pos = it;
115+
}
116+
}
117+
118+
// The Fuzzed Input (record) needs to have at least two characters in order for ParseTxtRecord to do something
119+
if (aRecord.size() > 1)
120+
{
121+
if (true == equal_sign_present)
122+
{
123+
std::string input_record_value(equal_sign_pos + 1, aRecord.end());
124+
EXPECT_EQ(accumulator.Data().at(0).second, input_record_value);
125+
}
126+
}
127+
}
128+
129+
FUZZ_TEST(MinimalmDNS, TxtResponderFuzz2).WithDomains(Arbitrary<vector<uint8_t>>().WithMaxSize(254));
130+
FUZZ_TEST(MinimalmDNS, PacketParserFuzz).WithDomains(Arbitrary<vector<uint8_t>>());

src/lib/format/tests/FuzzPayloadDecoderPW.cpp

+5-18
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ using namespace chip::TLV;
3434
using namespace chip::TLVMeta;
3535
using namespace fuzztest;
3636

37-
void RunDecodePW(const std::vector<std::uint8_t> & bytes, chip::Protocols::Id mProtocol, uint8_t mMessageType)
37+
// The Property Function; The FUZZ_TEST macro will call this function, with the fuzzed input domains
38+
void RunDecodeFuzz(const std::vector<std::uint8_t> & bytes, chip::Protocols::Id mProtocol, uint8_t mMessageType)
3839
{
3940

4041
PayloadDecoderInitParams params;
@@ -56,29 +57,15 @@ void RunDecodePW(const std::vector<std::uint8_t> & bytes, chip::Protocols::Id mP
5657
{
5758
// Nothing to do ...
5859
}
59-
60-
// TODO: remove
61-
// PRINT BYTES: To check the combination of bytes being printed
62-
// std::cout << "bytes: ";
63-
// for (const auto& byte : bytes) {
64-
// std::cout << static_cast<int>(byte) << " ";
65-
// }
66-
// std::cout <<std::endl << std::endl;
67-
68-
// printing Protocol IDs
69-
// std::cout << "protocol ID: " << mProtocol.GetProtocolId()<<std::endl;
70-
71-
// printing mMessageType
72-
// std::cout << "mMessageType: " << mMessageType << std::endl;
7360
}
7461

75-
// This allows us to fuzz test with all the combinations of protocols
76-
auto ProtocolIDs()
62+
// This allows us to create a FuzzTest "Input Domain" to fuzz test with all the combinations of protocols.
63+
auto AnyProtocolID()
7764
{
7865
return ElementOf({ chip::Protocols::SecureChannel::Id, chip::Protocols::InteractionModel::Id, chip::Protocols::BDX::Id,
7966
chip::Protocols::UserDirectedCommissioning::Id });
8067
}
8168

82-
FUZZ_TEST(PayloadDecoder, RunDecodePW).WithDomains(Arbitrary<std::vector<std::uint8_t>>(), ProtocolIDs(), Arbitrary<uint8_t>());
69+
FUZZ_TEST(PayloadDecoder, RunDecodeFuzz).WithDomains(Arbitrary<std::vector<std::uint8_t>>(), AnyProtocolID(), Arbitrary<uint8_t>());
8370

8471
} // namespace

src/setup_payload/tests/FuzzBase38PW.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <pw_fuzzer/fuzztest.h>
66
#include <pw_unit_test/framework.h>
77

8+
#include "setup_payload/QRCodeSetupPayloadParser.h"
89
#include <setup_payload/Base38Decode.h>
910
#include <setup_payload/Base38Encode.h>
1011

@@ -54,9 +55,19 @@ void Base38RoundTripFuzz(const std::vector<uint8_t> & bytes)
5455
ASSERT_EQ(decodedData, bytes);
5556
}
5657

58+
void FuzzQRCodeSetupPayloadParser(const std::string & s)
59+
{
60+
chip::Platform::MemoryInit();
61+
62+
SetupPayload payload;
63+
QRCodeSetupPayloadParser(s).populatePayload(payload);
64+
}
65+
5766
// The invocation of the FuzzTest
5867
FUZZ_TEST(Base38Decoder, Base38DecodeFuzz).WithDomains(Arbitrary<std::vector<uint8_t>>());
5968

6069
// Max size of the vector is defined as 306 since that will give an outputSizeNeeded of 511 which is less than the required
6170
// kMaxOutputSize
6271
FUZZ_TEST(Base38Decoder, Base38RoundTripFuzz).WithDomains(Arbitrary<std::vector<uint8_t>>().WithMaxSize(306));
72+
73+
FUZZ_TEST(Base38Decoder, FuzzQRCodeSetupPayloadParser).WithDomains(Arbitrary<std::string>());

0 commit comments

Comments
 (0)