Coverage Report

Created: 2026-02-14 07:22

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
7.02k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
54
7.02k
    FuzzedDataProvider fdp(data, size);
55
7.02k
    bool checkOnly = fdp.ConsumeBool();
56
7.02k
    uint8_t index = fdp.ConsumeIntegralInRange<uint8_t>(0, static_cast<uint8_t>(sizeof(types) / sizeof(types[0]) - 1));
57
58
7.02k
    D2SimpleParser simpleParser;
59
7.02k
    D2CfgContextPtr ctxPtr(new D2CfgContext());
60
7.02k
    D2ParserContext ctx;
61
7.02k
    ElementPtr elem;
62
63
    // Generate random parsing mode
64
7.02k
    D2ParserContext::ParserType type = types[index];
65
66
7.02k
    const std::string payload = fdp.ConsumeRemainingBytesAsString();
67
68
    // Target context parseString
69
7.02k
    try {
70
7.02k
        ctx.parseString(payload, type);
71
7.02k
    } catch (const isc::Exception&) {
72
        // Slient exceptions
73
6.66k
    }
74
75
    // Parse payload to JSON
76
7.02k
    try {
77
7.02k
        elem = Element::fromJSON(payload);
78
7.02k
    } catch (...) {
79
        // If failed to parse the payload, early exit
80
3.98k
        return 0;
81
3.98k
    }
82
83
    // Target SimpleParser
84
3.04k
    try {
85
3.04k
        D2SimpleParser::setAllDefaults(elem);
86
3.04k
        simpleParser.parse(ctxPtr, elem, checkOnly);
87
3.04k
    } catch (const isc::Exception&) {
88
        // Slient exceptions
89
2.62k
    }
90
91
    // Prepare buffer
92
3.04k
    InputBuffer buf(reinterpret_cast<const uint8_t*>(payload.data()), payload.size());
93
94
    // Target NameChangeRequest::fromtFormat
95
3.04k
    try {
96
3.04k
        NameChangeRequest::fromFormat(NameChangeFormat::FMT_JSON, buf);
97
3.04k
    } catch (const isc::Exception&) {
98
        // Slient exceptions
99
3.04k
    }
100
101
    // Target Message fromWire
102
3.04k
    try {
103
3.04k
        Message msg(Message::PARSE);
104
3.04k
        msg.fromWire(buf);
105
3.04k
    } catch (const isc::Exception&) {
106
        // Slient exceptions
107
3.03k
    }
108
109
    // Target D2UpdateMessage fromWire (Inbound)
110
3.04k
    try {
111
3.04k
        TSIGKey tsigkey("fuzz_key:fuzz_key");
112
3.04k
        TSIGContext tsigctx(tsigkey);
113
3.04k
        D2UpdateMessage message(D2UpdateMessage::INBOUND);
114
3.04k
        message.fromWire(payload.data(), payload.size(), &tsigctx) ;
115
3.04k
    } catch (const isc::Exception&) {
116
        // Slient exceptions
117
3.04k
    }
118
119
    // Target D2UpdateMessage fromWire (Outbound)
120
3.04k
    try {
121
3.04k
        TSIGKey tsigkey("fuzz_key:fuzz_key");
122
3.04k
        TSIGContext tsigctx(tsigkey);
123
3.04k
        D2UpdateMessage message(D2UpdateMessage::OUTBOUND);
124
3.04k
        message.fromWire(payload.data(), payload.size(), &tsigctx);
125
3.04k
    } catch (const isc::Exception&) {
126
        // Slient exceptions
127
3.04k
    }
128
129
3.04k
    return 0;
130
3.04k
}