Coverage Report

Created: 2025-11-16 07:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/kea-fuzzer/fuzz_d2.cc
Line
Count
Source
1
// Copyright (C) 2025 Ada Logcis Ltd.
2
//
3
// This Source Code Form is subject to the terms of the Mozilla Public
4
// License, v. 2.0. If a copy of the MPL was not distributed with this
5
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
////////////////////////////////////////////////////////////////////////////////
7
// Copyright (C) 2025 Ada Logcis Ltd.
8
//
9
// This Source Code Form is subject to the terms of the Mozilla Public
10
// License, v. 2.0. If a copy of the MPL was not distributed with this
11
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
12
////////////////////////////////////////////////////////////////////////////////
13
#include <config.h>
14
#include <fuzzer/FuzzedDataProvider.h>
15
16
#include <d2/parser_context.h>
17
#include <d2srv/d2_cfg_mgr.h>
18
#include <d2srv/d2_simple_parser.h>
19
#include <d2srv/d2_update_message.h>
20
#include <dhcp_ddns/ncr_msg.h>
21
#include <dns/message.h>
22
#include <dns/name.h>
23
#include <dns/tsig.h>
24
#include <dns/tsigkey.h>
25
26
#include <cc/data.h>
27
#include <exceptions/exceptions.h>
28
29
#include <cstddef>
30
#include <cstdint>
31
#include <string>
32
#include <memory>
33
34
using namespace isc::d2;
35
using namespace isc::data;
36
using namespace isc::dhcp_ddns;
37
using namespace isc::dns;
38
using namespace isc::util;
39
40
static const D2ParserContext::ParserType types[] = {
41
    D2ParserContext::PARSER_JSON,
42
    D2ParserContext::PARSER_DHCPDDNS,
43
    D2ParserContext::PARSER_SUB_DHCPDDNS,
44
    D2ParserContext::PARSER_TSIG_KEY,
45
    D2ParserContext::PARSER_TSIG_KEYS,
46
    D2ParserContext::PARSER_DDNS_DOMAIN,
47
    D2ParserContext::PARSER_DDNS_DOMAINS,
48
    D2ParserContext::PARSER_DNS_SERVER,
49
    D2ParserContext::PARSER_DNS_SERVERS,
50
    D2ParserContext::PARSER_HOOKS_LIBRARY
51
};
52
53
4.46k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
54
4.46k
    FuzzedDataProvider fdp(data, size);
55
4.46k
    bool checkOnly = fdp.ConsumeBool();
56
4.46k
    uint8_t index = fdp.ConsumeIntegralInRange<uint8_t>(0, static_cast<uint8_t>(sizeof(types) / sizeof(types[0]) - 1));
57
58
4.46k
    D2SimpleParser simpleParser;
59
4.46k
    D2CfgContextPtr ctxPtr(new D2CfgContext());
60
4.46k
    D2ParserContext ctx;
61
4.46k
    ElementPtr elem;
62
63
    // Generate random parsing mode
64
4.46k
    D2ParserContext::ParserType type = types[index];
65
66
4.46k
    const std::string payload = fdp.ConsumeRemainingBytesAsString();
67
68
    // Target context parseString
69
4.46k
    try {
70
4.46k
        ctx.parseString(payload, type);
71
4.46k
    } catch (const isc::Exception&) {
72
        // Slient exceptions
73
4.31k
    }
74
75
    // Parse payload to JSON
76
4.46k
    try {
77
4.46k
        elem = Element::fromJSON(payload);
78
4.46k
    } catch (...) {
79
        // If failed to parse the payload, early exit
80
2.48k
        return 0;
81
2.48k
    }
82
83
    // Target SimpleParser
84
1.97k
    try {
85
1.97k
        D2SimpleParser::setAllDefaults(elem);
86
1.97k
        simpleParser.parse(ctxPtr, elem, checkOnly);
87
1.97k
    } catch (const isc::Exception&) {
88
        // Slient exceptions
89
1.82k
    }
90
91
    // Prepare buffer
92
1.97k
    InputBuffer buf(reinterpret_cast<const uint8_t*>(payload.data()), payload.size());
93
94
    // Target NameChangeRequest::fromtFormat
95
1.97k
    try {
96
1.97k
        NameChangeRequest::fromFormat(NameChangeFormat::FMT_JSON, buf);
97
1.97k
    } catch (const isc::Exception&) {
98
        // Slient exceptions
99
1.97k
    }
100
101
    // Target Message fromWire
102
1.97k
    try {
103
1.97k
        Message msg(Message::PARSE);
104
1.97k
        msg.fromWire(buf);
105
1.97k
    } catch (const isc::Exception&) {
106
        // Slient exceptions
107
1.97k
    }
108
109
    // Target D2UpdateMessage fromWire (Inbound)
110
1.97k
    try {
111
1.97k
        TSIGKey tsigkey("fuzz_key:fuzz_key");
112
1.97k
        TSIGContext tsigctx(tsigkey);
113
1.97k
        D2UpdateMessage message(D2UpdateMessage::INBOUND);
114
1.97k
        message.fromWire(payload.data(), payload.size(), &tsigctx) ;
115
1.97k
    } catch (const isc::Exception&) {
116
        // Slient exceptions
117
1.97k
    }
118
119
    // Target D2UpdateMessage fromWire (Outbound)
120
1.97k
    try {
121
1.97k
        TSIGKey tsigkey("fuzz_key:fuzz_key");
122
1.97k
        TSIGContext tsigctx(tsigkey);
123
1.97k
        D2UpdateMessage message(D2UpdateMessage::OUTBOUND);
124
1.97k
        message.fromWire(payload.data(), payload.size(), &tsigctx);
125
1.97k
    } catch (const isc::Exception&) {
126
        // Slient exceptions
127
1.97k
    }
128
129
1.97k
    return 0;
130
1.97k
}