suricata
fuzz_sigpcap_aware.c
Go to the documentation of this file.
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"
11 #include "util-reference-config.h"
12 #include "app-layer.h"
13 #include "tm-queuehandlers.h"
14 #include "util-cidr.h"
15 #include "util-proto-name.h"
16 #include "detect-engine-tag.h"
18 #include "host-bit.h"
19 #include "ippair-bit.h"
20 #include "app-layer-htp.h"
21 #include "detect-fast-pattern.h"
22 #include "util-unittest-helper.h"
23 #include "conf-yaml-loader.h"
24 #include "pkt-var.h"
25 #include "flow-util.h"
26 
27 #include <fuzz_pcap.h>
28 
29 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
30 
31 static int initialized = 0;
34 // FlowWorkerThreadData
35 void *fwd;
37 
38 #include "confyaml.c"
39 
40 static void SigGenereateAware(const uint8_t *data, size_t size, char *r, size_t *len)
41 {
42  *len = snprintf(r, 511, "alert ip any any -> any any (");
43  for (size_t i = 0; i + 1 < size && *len < 511; i++) {
44  if (data[i] & 0x80) {
45  size_t off = (data[i] & 0x7F + ((data[i + 1] & 0xF) << 7)) %
46  (sizeof(sigmatch_table) / sizeof(SigTableElmt));
47  if (sigmatch_table[off].flags & SIGMATCH_NOOPT ||
48  ((data[i + 1] & 0x80) && sigmatch_table[off].flags & SIGMATCH_OPTIONAL_OPT)) {
49  *len += snprintf(r + *len, 511 - *len, "; %s;", sigmatch_table[off].name);
50  } else {
51  *len += snprintf(r + *len, 511 - *len, "; %s:", sigmatch_table[off].name);
52  }
53  i++;
54  } else {
55  r[*len] = data[i];
56  *len = *len + 1;
57  }
58  }
59  if (*len < 511) {
60  *len += snprintf(r + *len, 511 - *len, ")");
61  } else {
62  r[511] = 0;
63  *len = 511;
64  }
65 }
66 
67 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
68 {
69  FPC_buffer_t pkts;
70  const u_char *pkt;
71  struct pcap_pkthdr header;
72  int r;
73  Packet *p;
74  size_t pos;
75  size_t pcap_cnt = 0;
76 
77  if (initialized == 0) {
78  // Redirects logs to /dev/null
79  setenv("SC_LOG_OP_IFACE", "file", 0);
80  setenv("SC_LOG_FILE", "/dev/null", 0);
81 
82  InitGlobal();
83 
86  // redirect logs to /tmp
87  ConfigSetLogDirectory("/tmp/");
88  // disables checksums validation for fuzzing
90  abort();
91  }
92  // do not load rules before reproducible DetectEngineReload
93  remove("/tmp/fuzz.rules");
94  surifuzz.sig_file = strdup("/tmp/fuzz.rules");
96  // loads rules after init
98 
102 
103  memset(&tv, 0, sizeof(tv));
105  if (tv.flow_queue == NULL)
106  abort();
111 
112  extern intmax_t max_pending_packets;
113  max_pending_packets = 128;
114  PacketPoolInit();
115  initialized = 1;
116  }
117 
118  if (size < 1 + FPC0_HEADER_LEN) {
119  return 0;
120  }
121  for (pos = 0; pos < size - FPC0_HEADER_LEN; pos++) {
122  if (data[pos] == 0) {
123  break;
124  }
125  }
126  // initialize FPC with the buffer
127  if (FPC_init(&pkts, data + pos + 1, size - pos - 1) < 0) {
128  return 0;
129  }
130 
131  // dump signatures to a file so as to reuse SigLoadSignatures
132  char sigaware[512];
133  size_t len;
134  SigGenereateAware(data, pos + 1, sigaware, &len);
135  if (TestHelperBufferToFile(surifuzz.sig_file, (uint8_t *)sigaware, len) < 0) {
136  return 0;
137  }
138 
139  if (DetectEngineReload(&surifuzz) < 0) {
140  return 0;
141  }
143 
145  de_ctx->ref_cnt--;
147  FlowWorkerReplaceDetectCtx(fwd, new_det_ctx);
148 
149  DetectEngineThreadCtxDeinit(NULL, old_det_ctx);
150 
151  // loop over packets
152  r = FPC_next(&pkts, &header, &pkt);
153  p = PacketGetFromAlloc();
154  p->pkt_src = PKT_SRC_WIRE;
155  p->ts.tv_sec = header.ts.tv_sec;
156  p->ts.tv_usec = header.ts.tv_usec % 1000000;
157  p->datalink = pkts.datalink;
158  while (r > 0) {
159  if (PacketCopyData(p, pkt, header.caplen) == 0) {
160  // DecodePcapFile
162  if (ecode == TM_ECODE_FAILED) {
163  break;
164  }
165  Packet *extra_p = PacketDequeueNoLock(&tv.decode_pq);
166  while (extra_p != NULL) {
167  PacketFreeOrRelease(extra_p);
168  extra_p = PacketDequeueNoLock(&tv.decode_pq);
169  }
171  extra_p = PacketDequeueNoLock(&tv.decode_pq);
172  while (extra_p != NULL) {
173  PacketFreeOrRelease(extra_p);
174  extra_p = PacketDequeueNoLock(&tv.decode_pq);
175  }
176  }
177  r = FPC_next(&pkts, &header, &pkt);
178  PACKET_RECYCLE(p);
179  p->pkt_src = PKT_SRC_WIRE;
180  p->ts.tv_sec = header.ts.tv_sec;
181  p->ts.tv_usec = header.ts.tv_usec % 1000000;
182  p->datalink = pkts.datalink;
183  pcap_cnt++;
184  p->pcap_cnt = pcap_cnt;
185  }
186  PacketFree(p);
187  FlowReset();
188 
189  return 0;
190 }
ThreadVars_::flow_queue
struct FlowQueue_ * flow_queue
Definition: threadvars.h:133
len
uint8_t len
Definition: app-layer-dnp3.h:2
detect-engine.h
DetectEngineThreadCtxInitForReload
DetectEngineThreadCtx * DetectEngineThreadCtxInitForReload(ThreadVars *tv, DetectEngineCtx *new_de_ctx, int mt)
Definition: detect-engine.c:3211
PacketFreeOrRelease
void PacketFreeOrRelease(Packet *p)
Return a packet to where it was allocated.
Definition: decode.c:194
flow-util.h
fwd
void * fwd
Definition: fuzz_sigpcap_aware.c:35
PacketCopyData
int PacketCopyData(Packet *p, const uint8_t *pktdata, uint32_t pktlen)
Copy data to Packet payload and set packet length.
Definition: decode.c:295
source-pcap-file.h
DetectEngineCtx_::ref_cnt
uint32_t ref_cnt
Definition: detect.h:931
Packet_::pcap_cnt
uint64_t pcap_cnt
Definition: decode.h:603
ippair-bit.h
tv
ThreadVars tv
Definition: fuzz_sigpcap_aware.c:32
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1244
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:794
DetectEngineGetCurrent
DetectEngineCtx * DetectEngineGetCurrent(void)
Definition: detect-engine.c:3601
FlowReset
void FlowReset(void)
Definition: flow.c:652
StatsSetupPrivate
int StatsSetupPrivate(ThreadVars *tv)
Definition: counters.c:1211
GlobalsInitPreConfig
void GlobalsInitPreConfig(void)
Definition: suricata.c:375
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:83
util-unittest-helper.h
PacketPoolInit
void PacketPoolInit(void)
Definition: tmqh-packetpool.c:302
Packet_::datalink
int datalink
Definition: decode.h:616
DecodeRegisterPerfCounters
void DecodeRegisterPerfCounters(DecodeThreadVars *dtv, ThreadVars *tv)
Definition: decode.c:523
util-cidr.h
app-layer-htp.h
PreRunPostPrivsDropInit
void PreRunPostPrivsDropInit(const int runmode)
Definition: suricata.c:2172
PKT_SRC_WIRE
@ PKT_SRC_WIRE
Definition: decode.h:51
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1043
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Definition: fuzz_sigpcap_aware.c:67
PacketDequeueNoLock
Packet * PacketDequeueNoLock(PacketQueueNoLock *qnl)
Definition: packet-queue.c:206
util-reference-config.h
SCInstance_::delayed_detect
int delayed_detect
Definition: suricata.h:148
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
TmModule_::Func
TmEcode(* Func)(ThreadVars *, Packet *, void *)
Definition: tm-modules.h:52
SigTableElmt
struct SigTableElmt_ SigTableElmt
element in sigmatch type table.
PacketFree
void PacketFree(Packet *p)
Return a malloced packet.
Definition: decode.c:134
TestHelperBufferToFile
int TestHelperBufferToFile(const char *name, const uint8_t *data, size_t size)
writes the contents of a buffer into a file
Definition: util-unittest-helper.c:101
ConfYamlLoadString
int ConfYamlLoadString(const char *string, size_t len)
Load configuration from a YAML string.
Definition: conf-yaml-loader.c:487
PostConfLoadedSetup
int PostConfLoadedSetup(SCInstance *suri)
Definition: suricata.c:2584
detect-engine-tag.h
Packet_
Definition: decode.h:442
PostConfLoadedDetectSetup
void PostConfLoadedDetectSetup(SCInstance *suri)
Definition: suricata.c:2466
tmm_modules
TmModule tmm_modules[TMM_SIZE]
Definition: tm-modules.c:33
conf-yaml-loader.h
TMM_DECODEPCAPFILE
@ TMM_DECODEPCAPFILE
Definition: tm-threads-common.h:41
confyaml.c
TmEcode
TmEcode
Definition: tm-threads-common.h:81
util-proto-name.h
setenv
void setenv(const char *name, const char *value, int overwrite)
TMM_FLOWWORKER
@ TMM_FLOWWORKER
Definition: tm-threads-common.h:34
tm-queuehandlers.h
FlowWorkerGetDetectCtxPtr
void * FlowWorkerGetDetectCtxPtr(void *flow_worker)
Definition: flow-worker.c:615
detect-fast-pattern.h
flags
uint8_t flags
Definition: decode-gre.h:0
Packet_::ts
struct timeval ts
Definition: decode.h:485
suricata-common.h
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
Definition: detect-engine.c:3358
configNoChecksum
const char configNoChecksum[]
Definition: confyaml.c:1
run_mode
int run_mode
Definition: suricata.c:205
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
TmModule_::ThreadInit
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:47
util-classification-config.h
dtv
DecodeThreadVars * dtv
Definition: fuzz_sigpcap_aware.c:33
SCInstance_::sig_file
char * sig_file
Definition: suricata.h:128
SIGMATCH_OPTIONAL_OPT
#define SIGMATCH_OPTIONAL_OPT
Definition: detect.h:1445
ConfigSetLogDirectory
TmEcode ConfigSetLogDirectory(const char *name)
Definition: util-conf.c:30
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:173
SCInstance_::sig_file_exclusive
int sig_file_exclusive
Definition: suricata.h:129
Packet_::pkt_src
uint8_t pkt_src
Definition: decode.h:588
max_pending_packets
int max_pending_packets
Definition: suricata.c:216
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:672
DecodeThreadVarsAlloc
DecodeThreadVars * DecodeThreadVarsAlloc(ThreadVars *tv)
Alloc and setup DecodeThreadVars.
Definition: decode.c:684
ThreadVars_::decode_pq
PacketQueueNoLock decode_pq
Definition: threadvars.h:110
FlowQueueNew
FlowQueue * FlowQueueNew()
Definition: flow-queue.c:36
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1436
DetectEngineReload
int DetectEngineReload(const SCInstance *suri)
Reload the detection engine.
Definition: detect-engine.c:4428
SCInstance_
Definition: suricata.h:123
InitGlobal
int InitGlobal(void)
Global initialization common to all runmodes.
Definition: suricata.c:2803
RUNMODE_PCAP_FILE
@ RUNMODE_PCAP_FILE
Definition: runmodes.h:30
surifuzz
SCInstance surifuzz
Definition: fuzz_sigpcap_aware.c:36
PACKET_RECYCLE
#define PACKET_RECYCLE(p)
Definition: decode.h:884
host-bit.h
detect-engine-threshold.h
FlowWorkerReplaceDetectCtx
void FlowWorkerReplaceDetectCtx(void *flow_worker, void *detect_ctx)
Definition: flow-worker.c:608
app-layer.h