3 # Wireshark - Network traffic analyzer
4 # By Gerald Combs <gerald@wireshark.org>
5 # Copyright 1998 Gerald Combs
7 # SPDX-License-Identifier: GPL-2.0-or-later
10 # List of dissectors compiled below, which should be turned off.
11 # This is done to avoid single fuzzer (like IP) to call UDP protocols, which can go back to IP, and so on..
12 # While doing so might find some bugs, but it's likely to be the problem for too big corpus in oss-fuzzer
13 # (see: https://github.com/google/oss-fuzz/issues/1087).
14 # + udplite - it's sharing most of code with UDP.
15 set(FUZZ_DISABLED_DISSECTORS ip udp udplite ospf bgp dhcp json)
17 set(FUZZ_DISSECTORS ip)
18 set(FUZZ_IP_PROTO_DISSECTORS udp ospf)
20 set(FUZZ_TCP_PORT_DISSECTORS bgp)
21 # list(APPEND FUZZ_TCP_PORT_DISSECTORS bzr) # disabled, cause of known problem.
22 # list(APPEND FUZZ_TCP_PORT_DISSECTORS echo) # disabled, too simple.
24 set(FUZZ_UDP_PORT_DISSECTORS dns dhcp)
25 # list(FUZZ_UDP_PORT_DISSECTORS bfd) # disabled, too simple.
27 set(FUZZ_MEDIA_TYPE_DISSECTORS json)
36 if("$ENV{LIB_FUZZING_ENGINE}" STREQUAL "")
37 message(FATAL_ERROR "LIB_FUZZING_ENGINE is not set!")
39 list(APPEND fuzzshark_LIBS $ENV{LIB_FUZZING_ENGINE})
44 set(FUZZ_LINK_FLAGS "${WS_LINK_FLAGS}")
46 set(FUZZ_LINK_FLAGS "${FUZZ_LINK_FLAGS} -fsanitize=fuzzer")
49 # libFuzzingEngine.a is not position independent, so cannot use -pie.
50 set(FUZZ_LINK_FLAGS "${FUZZ_LINK_FLAGS} -no-pie")
53 # Convert the list of disabled dissectors from a;b;c -> "a", "b", "c"
54 # for use in fuzzshark.c (macro)
55 string(REGEX REPLACE "([^;]+)" "\"\\1\"" FUZZ_DISABLED_DISSECTORS_MACRO "${FUZZ_DISABLED_DISSECTORS}")
56 string(REPLACE ";" ", " FUZZ_DISABLED_DISSECTORS_MACRO "${FUZZ_DISABLED_DISSECTORS_MACRO}")
58 # Targets that are build via all-fuzzers:
59 # - fuzzshark: a non-specific fuzz target, configurable through env vars (requires BUILD_fuzzshark)
60 # - fuzzshark_<target>: fuzz target for a specific dissector target.
61 # - fuzzshark_<table>-<target>: fuzz target for a specific dissector via a dissector table.
62 add_custom_target(all-fuzzers)
64 function(fuzzshark_set_common_options fuzzer_name)
65 # Sanitizers require a C++ runtime, so use a C++ linker.
66 set_target_properties(${fuzzer_name} PROPERTIES
68 LINK_FLAGS "${FUZZ_LINK_FLAGS}"
71 target_link_libraries(${fuzzer_name} ${fuzzshark_LIBS})
72 add_dependencies(all-fuzzers ${fuzzer_name})
76 if(NOT (ENABLE_FUZZER OR OSS_FUZZ))
77 # libFuzzer includes a main routine that enables fuzzing. If
78 # support for fuzzing was not enabled, add a small standalone
79 # target that can be used to test-compile fuzzshark.c.
80 list(APPEND fuzzshark_FILES StandaloneFuzzTargetMain.c)
82 add_executable(fuzzshark ${fuzzshark_FILES})
83 fuzzshark_set_common_options(fuzzshark)
86 # Create a new dissector fuzzer target.
87 # If <dissector_table> is empty, <name> will be called directly.
88 # If <dissector_table> is non-empty, a dissector with filter name <name> will be
89 # looked up in dissector table <dissector_table>.
90 function(generate_fuzzer dissector_table name)
91 if(NOT (ENABLE_FUZZER OR OSS_FUZZ))
95 if(dissector_table STREQUAL "")
96 set(fuzzer_name fuzzshark_${name})
98 # "ip.proto" and "udp" -> "ip_proto-udp"
99 set(fuzzer_name fuzzshark_${dissector_table}-${name})
100 string(REPLACE "." "_" fuzzer_name ${fuzzer_name})
103 add_executable(${fuzzer_name} EXCLUDE_FROM_ALL ${fuzzshark_FILES})
104 fuzzshark_set_common_options(${fuzzer_name})
105 target_compile_definitions(${fuzzer_name} PRIVATE
106 FUZZ_DISSECTOR_LIST=${FUZZ_DISABLED_DISSECTORS_MACRO}
107 FUZZ_DISSECTOR_TARGET="${name}"
109 if(NOT dissector_table STREQUAL "")
110 target_compile_definitions(${fuzzer_name} PRIVATE
111 FUZZ_DISSECTOR_TABLE="${dissector_table}")
115 # Add fuzzer targets for every dissector in list FUZZ_<table-var>_DISSECTORS,
116 # where <table-var> changes a <table> such as "ip.proto" into "IP_PROTO".
117 function(add_table_fuzzers table)
118 string(REPLACE "." "_" table_var ${table})
119 string(TOUPPER "${table_var}" table_var)
120 foreach(dissector IN LISTS FUZZ_${table_var}_DISSECTORS)
121 generate_fuzzer(${table} ${dissector})
125 foreach(dissector IN LISTS FUZZ_DISSECTORS)
126 generate_fuzzer("" ${dissector})
129 add_table_fuzzers("ip.proto")
130 add_table_fuzzers("tcp.port")
131 add_table_fuzzers("udp.port")
132 add_table_fuzzers("media_type")
135 # Editor modelines - https://www.wireshark.org/tools/modelines.html
140 # indent-tabs-mode: t
143 # vi: set shiftwidth=8 tabstop=8 noexpandtab:
144 # :indentSize=8:tabSize=8:noTabs=false: