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 SC_ATOMIC_EXTERN(unsigned int, engine_stage);
35 
36 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
37 {
38  void *ptv = NULL;
39 
40  if (initialized == 0) {
41  //Redirects logs to /dev/null
42  setenv("SC_LOG_OP_IFACE", "file", 0);
43  setenv("SC_LOG_FILE", "/dev/null", 0);
44 
45  InitGlobal();
47 
48  //redirect logs to /tmp
49  ConfigSetLogDirectory("/tmp/");
50  //disables checksums validation for fuzzing
52  abort();
53  }
54 
56 
60 
62  "packetpool", "packetpool",
63  "packetpool", "packetpool",
64  "pktacqloop");
65  if (tv == NULL) {
66  return 0;
67  }
68  TmModule *tm_module = TmModuleGetByName("ReceivePcapFile");
69  if (tm_module == NULL) {
70  return 0;
71  }
72  TmSlotSetFuncAppend(tv, tm_module, "/tmp/fuzz.pcap");
73  tm_module = TmModuleGetByName("DecodePcapFile");
74  if (tm_module == NULL) {
75  return 0;
76  }
77  TmSlotSetFuncAppend(tv, tm_module, NULL);
78  tmm_modules[TMM_DECODEPCAPFILE].ThreadInit(tv, NULL, (void **) &dtv);
79  (void)SC_ATOMIC_SET(tv->tm_slots->slot_next->slot_data, dtv);
80 
81  extern uint16_t max_pending_packets;
82  max_pending_packets = 128;
84  SC_ATOMIC_SET(engine_stage, SURICATA_RUNTIME);
85 
86  initialized = 1;
87  }
88 
89  //rewrite buffer to a file as libpcap does not have buffer inputs
90  if (TestHelperBufferToFile("/tmp/fuzz.pcap", data, size) < 0) {
91  return 0;
92  }
93 
94  if (tmm_modules[TMM_RECEIVEPCAPFILE].ThreadInit(tv, "/tmp/fuzz.pcap", &ptv) == TM_ECODE_OK && ptv != NULL) {
98  }
99 
100  return 0;
101 }
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:1036
SC_ATOMIC_SET
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:386
tm-modules.h
SURICATA_RUNTIME
@ SURICATA_RUNTIME
Definition: suricata.h:96
configNoChecksum
const char configNoChecksum[]
Definition: fuzz_decodepcapfile.c:24
util-unittest-helper.h
TmModule_::PktAcqLoop
TmEcode(* PktAcqLoop)(ThreadVars *, void *, void *)
Definition: tm-modules.h:55
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
PacketPoolInit
void PacketPoolInit(void)
Definition: tmqh-packetpool.c:262
TmModule_::ThreadDeinit
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: tm-modules.h:50
TmModuleGetByName
TmModule * TmModuleGetByName(const char *name)
get a tm module ptr by name
Definition: tm-modules.c:53
app-layer-detect-proto.h
RunModeInitializeThreadSettings
void RunModeInitializeThreadSettings(void)
Definition: runmodes.c:983
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
SC_ATOMIC_EXTERN
SC_ATOMIC_EXTERN(unsigned int, engine_stage)
ConfYamlLoadString
int ConfYamlLoadString(const char *string, size_t len)
Load configuration from a YAML string.
Definition: conf-yaml-loader.c:511
PostConfLoadedSetup
int PostConfLoadedSetup(SCInstance *suri)
Definition: suricata.c:2632
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
max_pending_packets
uint16_t max_pending_packets
Definition: suricata.c:186
defrag.h
setenv
void setenv(const char *name, const char *value, int overwrite)
TmModule_
Definition: tm-modules.h:44
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:642
util-conf.h
TimeModeSetOffline
void TimeModeSetOffline(void)
Definition: util-time.c:105
suricata-common.h
run_mode
int run_mode
Definition: suricata.c:175
TmModule_::ThreadInit
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:48
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
ConfigSetLogDirectory
TmEcode ConfigSetLogDirectory(const char *name)
Definition: util-conf.c:33
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:686
PcapFileGlobalInit
void PcapFileGlobalInit(void)
Definition: source-pcap-file.c:139
suricata.h
SCInstance_
Definition: suricata.h:123
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:2864
RUNMODE_PCAP_FILE
@ RUNMODE_PCAP_FILE
Definition: runmodes.h:30
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Definition: fuzz_decodepcapfile.c:36
suricata_ctl_flags
volatile uint8_t suricata_ctl_flags
Definition: suricata.c:172