suricata
fuzz_decodepcapfile.c
Go to the documentation of this file.
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"
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;
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 
34 
35 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
36 {
37  void *ptv = NULL;
38 
39  if (initialized == 0) {
40  //Redirects logs to /dev/null
41  setenv("SC_LOG_OP_IFACE", "file", 0);
42  setenv("SC_LOG_FILE", "/dev/null", 0);
43 
44  InitGlobal();
46 
47  //redirect logs to /tmp
48  ConfigSetLogDirectory("/tmp/");
49  //disables checksums validation for fuzzing
51  abort();
52  }
53 
55 
59 
61  "packetpool", "packetpool",
62  "packetpool", "packetpool",
63  "pktacqloop");
64  if (tv == NULL) {
65  return 0;
66  }
67  TmModule *tm_module = TmModuleGetByName("ReceivePcapFile");
68  if (tm_module == NULL) {
69  return 0;
70  }
71  TmSlotSetFuncAppend(tv, tm_module, "/tmp/fuzz.pcap");
72  tm_module = TmModuleGetByName("DecodePcapFile");
73  if (tm_module == NULL) {
74  return 0;
75  }
76  TmSlotSetFuncAppend(tv, tm_module, NULL);
77  tmm_modules[TMM_DECODEPCAPFILE].ThreadInit(tv, NULL, (void **) &dtv);
78  (void)SC_ATOMIC_SET(tv->tm_slots->slot_next->slot_data, dtv);
79 
80  extern intmax_t max_pending_packets;
81  max_pending_packets = 128;
83 
84  initialized = 1;
85  }
86 
87  //rewrite buffer to a file as libpcap does not have buffer inputs
88  if (TestHelperBufferToFile("/tmp/fuzz.pcap", data, size) < 0) {
89  return 0;
90  }
91 
92  if (tmm_modules[TMM_RECEIVEPCAPFILE].ThreadInit(tv, "/tmp/fuzz.pcap", &ptv) == TM_ECODE_OK && ptv != NULL) {
96  }
97 
98  return 0;
99 }
tm-threads.h
source-pcap-file.h
TmThreadCreatePacketHandler
ThreadVars * TmThreadCreatePacketHandler(const char *name, const char *inq_name, const char *inqh_name, const char *outq_name, const char *outqh_name, const char *slots)
Creates and returns a TV instance for a Packet Processing Thread. This function doesn't support custo...
Definition: tm-threads.c:1026
SC_ATOMIC_SET
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:387
RunModeInitialize
void RunModeInitialize(void)
Definition: runmodes.c:967
tm-modules.h
configNoChecksum
const char configNoChecksum[]
Definition: fuzz_decodepcapfile.c:24
util-unittest-helper.h
TmModule_::PktAcqLoop
TmEcode(* PktAcqLoop)(ThreadVars *, void *, void *)
Definition: tm-modules.h:54
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:82
PacketPoolInit
void PacketPoolInit(void)
Definition: tmqh-packetpool.c:284
TmModule_::ThreadDeinit
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: tm-modules.h:49
TmModuleGetByName
TmModule * TmModuleGetByName(const char *name)
get a tm module ptr by name
Definition: tm-modules.c:53
app-layer-detect-proto.h
surifuzz
SCInstance surifuzz
Definition: fuzz_decodepcapfile.c:22
ThreadVars_::tm_slots
struct TmSlot_ * tm_slots
Definition: threadvars.h:95
TMM_RECEIVEPCAPFILE
@ TMM_RECEIVEPCAPFILE
Definition: tm-threads-common.h:39
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
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:103
util-time.h
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:2563
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
defrag.h
setenv
void setenv(const char *name, const char *value, int overwrite)
TmModule_
Definition: tm-modules.h:43
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:33
TmSlotSetFuncAppend
void TmSlotSetFuncAppend(ThreadVars *tv, TmModule *tm, const void *data)
Appends a new entry to the slots.
Definition: tm-threads.c:633
util-conf.h
TimeModeSetOffline
void TimeModeSetOffline(void)
Definition: util-time.c:105
suricata-common.h
run_mode
int run_mode
Definition: suricata.c:164
PcapFileGlobalInit
void PcapFileGlobalInit()
Definition: source-pcap-file.c:138
TmModule_::ThreadInit
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:47
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
ConfigSetLogDirectory
TmEcode ConfigSetLogDirectory(const char *name)
Definition: util-conf.c:32
max_pending_packets
int max_pending_packets
Definition: suricata.c:175
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:659
suricata.h
SCInstance_
Definition: suricata.h:122
TmSlot_::slot_next
struct TmSlot_ * slot_next
Definition: tm-threads.h:62
InitGlobal
int InitGlobal(void)
Global initialization common to all runmodes.
Definition: suricata.c:2782
RUNMODE_PCAP_FILE
@ RUNMODE_PCAP_FILE
Definition: runmodes.h:30
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Definition: fuzz_decodepcapfile.c:35
suricata_ctl_flags
volatile uint8_t suricata_ctl_flags
Definition: suricata.c:161