/src/suricata8/src/tests/fuzz/fuzz_sigpcap_aware.c
Line | Count | Source |
1 | | /** |
2 | | * @file |
3 | | * @author Philippe Antoine <contact@catenacyber.fr> |
4 | | * fuzz target for AppLayerProtoDetectGetProto |
5 | | */ |
6 | | |
7 | | #include "suricata-common.h" |
8 | | #include "source-pcap-file.h" |
9 | | #include "detect-engine.h" |
10 | | #include "util-classification-config.h" |
11 | | #include "util-reference-config.h" |
12 | | #include "app-layer.h" |
13 | | #include "tm-queuehandlers.h" |
14 | | #include "util-cidr.h" |
15 | | #include "util-profiling.h" |
16 | | #include "util-proto-name.h" |
17 | | #include "detect-engine-tag.h" |
18 | | #include "detect-engine-threshold.h" |
19 | | #include "host-bit.h" |
20 | | #include "ippair-bit.h" |
21 | | #include "app-layer-htp.h" |
22 | | #include "detect-fast-pattern.h" |
23 | | #include "util-unittest-helper.h" |
24 | | #include "conf-yaml-loader.h" |
25 | | #include "pkt-var.h" |
26 | | #include "flow-util.h" |
27 | | #include "flow-worker.h" |
28 | | #include "tm-modules.h" |
29 | | #include "tmqh-packetpool.h" |
30 | | #include "util-conf.h" |
31 | | #include "packet.h" |
32 | | |
33 | | #include <fuzz_pcap.h> |
34 | | |
35 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
36 | | |
37 | | static int initialized = 0; |
38 | | ThreadVars *tv = NULL; |
39 | | DecodeThreadVars *dtv; |
40 | | // FlowWorkerThreadData |
41 | | void *fwd; |
42 | | SCInstance surifuzz; |
43 | | SC_ATOMIC_EXTERN(unsigned int, engine_stage); |
44 | | |
45 | | extern const char *configNoChecksum; |
46 | | |
47 | | static void SigGenerateAware(const uint8_t *data, size_t size, char *r, size_t *len) |
48 | 44.8k | { |
49 | 44.8k | *len = snprintf(r, 511, "alert ip any any -> any any ("); |
50 | 3.04M | for (size_t i = 0; i + 1 < size && *len < 511; i++) { |
51 | 2.99M | if (data[i] & 0x80) { |
52 | 75.0k | size_t off = (data[i] & 0x7F + ((data[i + 1] & 0xF) << 7)) % (DETECT_TBLSIZE); |
53 | 75.0k | if (sigmatch_table[off].flags & SIGMATCH_NOOPT || |
54 | 57.7k | ((data[i + 1] & 0x80) && sigmatch_table[off].flags & SIGMATCH_OPTIONAL_OPT)) { |
55 | 18.1k | *len += snprintf(r + *len, 511 - *len, "; %s;", sigmatch_table[off].name); |
56 | 56.8k | } else { |
57 | 56.8k | *len += snprintf(r + *len, 511 - *len, "; %s:", sigmatch_table[off].name); |
58 | 56.8k | } |
59 | 75.0k | i++; |
60 | 2.92M | } else { |
61 | 2.92M | r[*len] = data[i]; |
62 | 2.92M | *len = *len + 1; |
63 | 2.92M | } |
64 | 2.99M | } |
65 | 44.8k | if (*len < 511) { |
66 | 43.8k | *len += snprintf(r + *len, 511 - *len, ")"); |
67 | 43.8k | } else { |
68 | 925 | r[511] = 0; |
69 | 925 | *len = 511; |
70 | 925 | } |
71 | 44.8k | } |
72 | | |
73 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
74 | 10.4k | { |
75 | 10.4k | FPC_buffer_t pkts; |
76 | 10.4k | const u_char *pkt; |
77 | 10.4k | struct pcap_pkthdr header; |
78 | 10.4k | int r; |
79 | 10.4k | Packet *p; |
80 | 10.4k | size_t pos; |
81 | 10.4k | size_t pcap_cnt = 0; |
82 | | |
83 | 10.4k | if (initialized == 0) { |
84 | | // Redirects logs to /dev/null |
85 | 1 | setenv("SC_LOG_OP_IFACE", "file", 0); |
86 | 1 | setenv("SC_LOG_FILE", "/dev/null", 0); |
87 | | |
88 | 1 | InitGlobal(); |
89 | | |
90 | 1 | GlobalsInitPreConfig(); |
91 | 1 | SCRunmodeSet(RUNMODE_PCAP_FILE); |
92 | | // redirect logs to /tmp |
93 | 1 | ConfigSetLogDirectory("/tmp/"); |
94 | | // disables checksums validation for fuzzing |
95 | 1 | if (SCConfYamlLoadString(configNoChecksum, strlen(configNoChecksum)) != 0) { |
96 | 0 | abort(); |
97 | 0 | } |
98 | | // do not load rules before reproducible DetectEngineReload |
99 | 1 | remove("/tmp/fuzz.rules"); |
100 | 1 | surifuzz.sig_file = strdup("/tmp/fuzz.rules"); |
101 | 1 | surifuzz.sig_file_exclusive = 1; |
102 | | // loads rules after init |
103 | 1 | surifuzz.delayed_detect = 1; |
104 | | |
105 | 1 | PostConfLoadedSetup(&surifuzz); |
106 | 1 | PreRunPostPrivsDropInit(SCRunmodeGet()); |
107 | 1 | PostConfLoadedDetectSetup(&surifuzz); |
108 | | |
109 | 1 | tv = ThreadVarsAlloc(); |
110 | 1 | tv->flow_queue = FlowQueueNew(); |
111 | 1 | if (tv->flow_queue == NULL) |
112 | 0 | abort(); |
113 | 1 | dtv = DecodeThreadVarsAlloc(tv); |
114 | 1 | DecodeRegisterPerfCounters(dtv, tv); |
115 | 1 | tmm_modules[TMM_FLOWWORKER].ThreadInit(tv, NULL, &fwd); |
116 | 1 | StatsSetupPrivate(tv); |
117 | | |
118 | 1 | extern uint32_t max_pending_packets; |
119 | 1 | max_pending_packets = 128; |
120 | 1 | PacketPoolInit(); |
121 | 1 | SC_ATOMIC_SET(engine_stage, SURICATA_RUNTIME); |
122 | 1 | initialized = 1; |
123 | 1 | } |
124 | | |
125 | 10.4k | if (size < 1 + FPC0_HEADER_LEN) { |
126 | 3 | return 0; |
127 | 3 | } |
128 | 456k | for (pos = 0; pos < size - FPC0_HEADER_LEN; pos++) { |
129 | 456k | if (data[pos] == 0) { |
130 | 10.4k | break; |
131 | 10.4k | } |
132 | 456k | } |
133 | | // initialize FPC with the buffer |
134 | 10.4k | if (FPC_init(&pkts, data + pos + 1, size - pos - 1) < 0) { |
135 | 41 | return 0; |
136 | 41 | } |
137 | | |
138 | | // dump signatures to a file so as to reuse SigLoadSignatures |
139 | 10.4k | char sigaware[512]; |
140 | 10.4k | size_t len; |
141 | 10.4k | SigGenerateAware(data, pos + 1, sigaware, &len); |
142 | 10.4k | if (TestHelperBufferToFile(surifuzz.sig_file, (uint8_t *)sigaware, len) < 0) { |
143 | 0 | return 0; |
144 | 0 | } |
145 | | |
146 | 10.4k | if (DetectEngineReload(&surifuzz) < 0) { |
147 | 0 | return 0; |
148 | 0 | } |
149 | 10.4k | DetectEngineThreadCtx *old_det_ctx = FlowWorkerGetDetectCtxPtr(fwd); |
150 | | |
151 | 10.4k | DetectEngineCtx *de_ctx = DetectEngineGetCurrent(); |
152 | 10.4k | de_ctx->ref_cnt--; |
153 | 10.4k | DetectEngineThreadCtx *new_det_ctx = DetectEngineThreadCtxInitForReload(tv, de_ctx, 1); |
154 | 10.4k | FlowWorkerReplaceDetectCtx(fwd, new_det_ctx); |
155 | | |
156 | 10.4k | DetectEngineThreadCtxDeinit(NULL, old_det_ctx); |
157 | | |
158 | | // loop over packets |
159 | 10.4k | r = FPC_next(&pkts, &header, &pkt); |
160 | 10.4k | p = PacketGetFromAlloc(); |
161 | 10.4k | if (r <= 0 || header.ts.tv_sec >= INT_MAX - 3600) { |
162 | 210 | goto bail; |
163 | 210 | } |
164 | 10.2k | p->pkt_src = PKT_SRC_WIRE; |
165 | 10.2k | p->ts = SCTIME_FROM_TIMEVAL(&header.ts); |
166 | 10.2k | p->datalink = pkts.datalink; |
167 | 1.02M | while (r > 0) { |
168 | 1.02M | if (PacketCopyData(p, pkt, header.caplen) == 0) { |
169 | | // DecodePcapFile |
170 | 1.02M | TmEcode ecode = tmm_modules[TMM_DECODEPCAPFILE].Func(tv, p, dtv); |
171 | 1.02M | if (ecode == TM_ECODE_FAILED) { |
172 | 0 | break; |
173 | 0 | } |
174 | 1.02M | Packet *extra_p = PacketDequeueNoLock(&tv->decode_pq); |
175 | 1.02M | while (extra_p != NULL) { |
176 | 147 | PacketFreeOrRelease(extra_p); |
177 | 147 | extra_p = PacketDequeueNoLock(&tv->decode_pq); |
178 | 147 | } |
179 | 1.02M | tmm_modules[TMM_FLOWWORKER].Func(tv, p, fwd); |
180 | 1.02M | extra_p = PacketDequeueNoLock(&tv->decode_pq); |
181 | 1.02M | while (extra_p != NULL) { |
182 | 0 | PacketFreeOrRelease(extra_p); |
183 | 0 | extra_p = PacketDequeueNoLock(&tv->decode_pq); |
184 | 0 | } |
185 | 1.02M | } |
186 | 1.02M | r = FPC_next(&pkts, &header, &pkt); |
187 | 1.02M | if (r <= 0 || header.ts.tv_sec >= INT_MAX - 3600) { |
188 | 10.2k | goto bail; |
189 | 10.2k | } |
190 | 1.01M | PacketRecycle(p); |
191 | 1.01M | p->pkt_src = PKT_SRC_WIRE; |
192 | 1.01M | p->ts = SCTIME_FROM_TIMEVAL(&header.ts); |
193 | 1.01M | p->datalink = pkts.datalink; |
194 | 1.01M | pcap_cnt++; |
195 | 1.01M | p->pcap_cnt = pcap_cnt; |
196 | 1.01M | } |
197 | 10.4k | bail: |
198 | 10.4k | PacketFree(p); |
199 | 10.4k | FlowReset(); |
200 | | |
201 | 10.4k | return 0; |
202 | 10.2k | } |