/src/suricata7/src/tests/fuzz/fuzz_decodepcapfile.c
Line | Count | Source |
1 | | /** |
2 | | * @file |
3 | | * @author Philippe Antoine <contact@catenacyber.fr> |
4 | | * fuzz target for TMM_DECODEPCAPFILE |
5 | | */ |
6 | | |
7 | | #include "suricata-common.h" |
8 | | #include "suricata.h" |
9 | | #include "app-layer-detect-proto.h" |
10 | | #include "defrag.h" |
11 | | #include "tm-modules.h" |
12 | | #include "tm-threads.h" |
13 | | #include "source-pcap-file.h" |
14 | | #include "util-unittest-helper.h" |
15 | | #include "conf-yaml-loader.h" |
16 | | #include "util-time.h" |
17 | | #include "util-conf.h" |
18 | | |
19 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
20 | | |
21 | | static int initialized = 0; |
22 | | SCInstance surifuzz; |
23 | | |
24 | | const char configNoChecksum[] = "\ |
25 | | %YAML 1.1\n\ |
26 | | ---\n\ |
27 | | pcap-file:\n\ |
28 | | \n\ |
29 | | checksum-checks: no\n\ |
30 | | "; |
31 | | |
32 | | ThreadVars *tv; |
33 | | DecodeThreadVars *dtv; |
34 | | SC_ATOMIC_EXTERN(unsigned int, engine_stage); |
35 | | |
36 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
37 | 21.9k | { |
38 | 21.9k | void *ptv = NULL; |
39 | | |
40 | 21.9k | if (initialized == 0) { |
41 | | //Redirects logs to /dev/null |
42 | 2 | setenv("SC_LOG_OP_IFACE", "file", 0); |
43 | 2 | setenv("SC_LOG_FILE", "/dev/null", 0); |
44 | | |
45 | 2 | InitGlobal(); |
46 | 2 | run_mode = RUNMODE_PCAP_FILE; |
47 | | |
48 | | //redirect logs to /tmp |
49 | 2 | ConfigSetLogDirectory("/tmp/"); |
50 | | //disables checksums validation for fuzzing |
51 | 2 | if (ConfYamlLoadString(configNoChecksum, strlen(configNoChecksum)) != 0) { |
52 | 0 | abort(); |
53 | 0 | } |
54 | | |
55 | 2 | PostConfLoadedSetup(&surifuzz); |
56 | | |
57 | 2 | RunModeInitializeThreadSettings(); |
58 | 2 | TimeModeSetOffline(); |
59 | 2 | PcapFileGlobalInit(); |
60 | | |
61 | 2 | tv = TmThreadCreatePacketHandler("fuzz", |
62 | 2 | "packetpool", "packetpool", |
63 | 2 | "packetpool", "packetpool", |
64 | 2 | "pktacqloop"); |
65 | 2 | if (tv == NULL) { |
66 | 0 | return 0; |
67 | 0 | } |
68 | 2 | TmModule *tm_module = TmModuleGetByName("ReceivePcapFile"); |
69 | 2 | if (tm_module == NULL) { |
70 | 0 | return 0; |
71 | 0 | } |
72 | 2 | TmSlotSetFuncAppend(tv, tm_module, "/tmp/fuzz.pcap"); |
73 | 2 | tm_module = TmModuleGetByName("DecodePcapFile"); |
74 | 2 | if (tm_module == NULL) { |
75 | 0 | return 0; |
76 | 0 | } |
77 | 2 | TmSlotSetFuncAppend(tv, tm_module, NULL); |
78 | 2 | tmm_modules[TMM_DECODEPCAPFILE].ThreadInit(tv, NULL, (void **) &dtv); |
79 | 2 | (void)SC_ATOMIC_SET(tv->tm_slots->slot_next->slot_data, dtv); |
80 | | |
81 | 2 | extern uint16_t max_pending_packets; |
82 | 2 | max_pending_packets = 128; |
83 | 2 | PacketPoolInit(); |
84 | 2 | SC_ATOMIC_SET(engine_stage, SURICATA_RUNTIME); |
85 | | |
86 | 2 | initialized = 1; |
87 | 2 | } |
88 | | |
89 | | //rewrite buffer to a file as libpcap does not have buffer inputs |
90 | 21.9k | if (TestHelperBufferToFile("/tmp/fuzz.pcap", data, size) < 0) { |
91 | 0 | return 0; |
92 | 0 | } |
93 | | |
94 | 21.9k | if (tmm_modules[TMM_RECEIVEPCAPFILE].ThreadInit(tv, "/tmp/fuzz.pcap", &ptv) == TM_ECODE_OK && ptv != NULL) { |
95 | 18.0k | suricata_ctl_flags = 0; |
96 | 18.0k | tmm_modules[TMM_RECEIVEPCAPFILE].PktAcqLoop(tv, ptv, tv->tm_slots); |
97 | 18.0k | tmm_modules[TMM_RECEIVEPCAPFILE].ThreadDeinit(tv, ptv); |
98 | 18.0k | } |
99 | | |
100 | 21.9k | return 0; |
101 | 21.9k | } |