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 AppLayerProtoDetectGetProto
5  */
6 
7 
8 #include <pcap/pcap.h>
9 
10 #include "suricata-common.h"
11 #include "app-layer-detect-proto.h"
12 #include "defrag.h"
13 #include "tm-modules.h"
14 #include "tm-threads.h"
15 #include "source-pcap-file.h"
16 #include "util-unittest-helper.h"
17 #include "conf-yaml-loader.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 
81 
82  initialized = 1;
83  }
84 
85  //rewrite buffer to a file as libpcap does not have buffer inputs
86  if (UTHbufferToFile("/tmp/fuzz.pcap", data, size) < 0) {
87  return 0;
88  }
89 
90  if (tmm_modules[TMM_RECEIVEPCAPFILE].ThreadInit(tv, "/tmp/fuzz.pcap", &ptv) == TM_ECODE_OK && ptv != NULL) {
94  }
95 
96  return 0;
97 }
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:1041
SC_ATOMIC_SET
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:207
RunModeInitialize
void RunModeInitialize(void)
Definition: runmodes.c:907
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:78
PacketPoolInit
void PacketPoolInit(void)
Definition: tmqh-packetpool.c:370
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
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
ConfYamlLoadString
int ConfYamlLoadString(const char *string, size_t len)
Load configuration from a YAML string.
Definition: conf-yaml-loader.c:451
PostConfLoadedSetup
int PostConfLoadedSetup(SCInstance *suri)
Definition: suricata.c:2408
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:623
TimeModeSetOffline
void TimeModeSetOffline(void)
Definition: util-time.c:103
suricata-common.h
run_mode
int run_mode
Definition: suricata.c:201
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:31
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:622
suricata
SCInstance suricata
Definition: fuzz_decodepcapfile.c:22
UTHbufferToFile
int UTHbufferToFile(const char *name, const uint8_t *data, size_t size)
writes the contents of a buffer into a file
Definition: util-unittest-helper.c:946
SCInstance_
Definition: suricata.h:128
TmSlot_::slot_next
struct TmSlot_ * slot_next
Definition: tm-threads.h:61
InitGlobal
int InitGlobal(void)
Global initialization common to all runmodes.
Definition: suricata.c:2640
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:198