suricata
fuzz_applayerparserparse.c
Go to the documentation of this file.
1 /**
2  * @file
3  * @author Philippe Antoine <contact@catenacyber.fr>
4  * fuzz target for AppLayerParserParse
5  */
6 
7 #include "suricata-common.h"
8 #include "suricata.h"
10 #include "flow-util.h"
11 #include "app-layer-parser.h"
12 #include "util-unittest-helper.h"
13 #include "util-byte.h"
14 #include "conf-yaml-loader.h"
15 #include "util-conf.h"
16 
17 #define HEADER_LEN 6
18 
19 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
20 int LLVMFuzzerInitialize(int *argc, char ***argv);
21 
23 
24 extern const char *configNoChecksum;
25 
26 /* input buffer is structured this way :
27  * 6 bytes header,
28  * then sequence of buffers separated by magic bytes 01 D5 CA 7A */
29 
30 /* The 6 bytes header is
31  * alproto
32  * proto
33  * source port (uint16_t)
34  * destination port (uint16_t) */
35 
36 const uint8_t separator[] = {0x01, 0xD5, 0xCA, 0x7A};
39 char *target_suffix = NULL;
40 SC_ATOMIC_EXTERN(unsigned int, engine_stage);
41 
42 int LLVMFuzzerInitialize(int *argc, char ***argv)
43 {
44  target_suffix = strrchr((*argv)[0], '_');
45  // else
46  if (!target_suffix) {
47  target_suffix = getenv("FUZZ_APPLAYER");
48  }
49  return 0;
50 }
51 
52 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
53 {
54  Flow * f;
55  TcpSession ssn;
56  const uint8_t * albuffer;
57  uint8_t * alnext;
58  size_t alsize;
59  // used to find under and overflows
60  // otherwise overflows do not fail as they read the next packet
61  uint8_t * isolatedBuffer;
62 
63  if (alp_tctx == NULL) {
64  //Redirects logs to /dev/null
65  setenv("SC_LOG_OP_IFACE", "file", 0);
66  setenv("SC_LOG_FILE", "/dev/null", 0);
67 
68  InitGlobal();
71 
72  //redirect logs to /tmp
73  ConfigSetLogDirectory("/tmp/");
74  // disables checksums validation for fuzzing
76  abort();
77  }
78 
81  SC_ATOMIC_SET(engine_stage, SURICATA_RUNTIME);
82  if (target_suffix != NULL) {
83  AppProto applayer = StringToAppProto(target_suffix + 1);
84  if (applayer != ALPROTO_UNKNOWN) {
85  forceLayer = applayer;
86  printf("Forcing %s=%" PRIu16 "\n", AppProtoToString(forceLayer), forceLayer);
87  }
88  }
89  // http is the output name, but we want to fuzz HTTP1
90  if (forceLayer == ALPROTO_HTTP) {
92  }
93  }
94 
95  if (size < HEADER_LEN) {
96  return 0;
97  }
98 
99  if (data[0] >= g_alproto_max) {
100  return 0;
101  }
102  //no UTHBuildFlow to have storage
103  f = FlowAlloc();
104  if (f == NULL) {
105  return 0;
106  }
107  f->flags |= FLOW_IPV4;
108  f->src.addr_data32[0] = 0x01020304;
109  f->dst.addr_data32[0] = 0x05060708;
110  f->sp = (uint16_t)((data[2] << 8) | data[3]);
111  f->dp = (uint16_t)((data[4] << 8) | data[5]);
112  f->proto = data[1];
113  memset(&ssn, 0, sizeof(TcpSession));
114  f->protoctx = &ssn;
116  if (forceLayer > 0) {
117  f->alproto = forceLayer;
118  } else {
119  f->alproto = data[0];
120  }
121 
122  FLOWLOCK_WRLOCK(f);
123  /*
124  * We want to fuzz multiple calls to AppLayerParserParse
125  * because some parts of the code are only reached after
126  * multiple packets (in SMTP for example).
127  * So we treat our input as a list of buffers with magic separator.
128  */
129  albuffer = data + HEADER_LEN;
130  alsize = size - HEADER_LEN;
131  uint8_t flags = STREAM_START;
132  int flip = 0;
133  alnext = memmem(albuffer, alsize, separator, 4);
134  while (alnext) {
135  if (flip) {
136  flags |= STREAM_TOCLIENT;
137  flags &= ~(STREAM_TOSERVER);
138  flip = 0;
139  } else {
140  flags |= STREAM_TOSERVER;
141  flags &= ~(STREAM_TOCLIENT);
142  flip = 1;
143  }
144 
145  if (alnext != albuffer) {
146  // only if we have some data
147  isolatedBuffer = malloc(alnext - albuffer);
148  if (isolatedBuffer == NULL) {
149  goto bail;
150  }
151  memcpy(isolatedBuffer, albuffer, alnext - albuffer);
152  (void) AppLayerParserParse(NULL, alp_tctx, f, f->alproto, flags, isolatedBuffer, alnext - albuffer);
153  free(isolatedBuffer);
154  if (FlowChangeProto(f)) {
155  // exits if a protocol change is requested
156  alsize = 0;
157  break;
158  }
159  flags &= ~(STREAM_START);
160  if (f->alparser &&
161  (((flags & STREAM_TOSERVER) != 0 &&
163  ((flags & STREAM_TOCLIENT) != 0 &&
165  //no final chunk
166  alsize = 0;
167  break;
168  }
169 
170  AppLayerParserTransactionsCleanup(f, flags & (STREAM_TOSERVER | STREAM_TOCLIENT));
171  }
172  alsize -= alnext - albuffer + 4;
173  albuffer = alnext + 4;
174  if (alsize == 0) {
175  break;
176  }
177  alnext = memmem(albuffer, alsize, separator, 4);
178  }
179  if (alsize > 0 ) {
180  if (flip) {
181  flags |= STREAM_TOCLIENT;
182  flags &= ~(STREAM_TOSERVER);
183  flip = 0;
184  } else {
185  flags |= STREAM_TOSERVER;
186  flags &= ~(STREAM_TOCLIENT);
187  flip = 1;
188  }
189  flags |= STREAM_EOF;
190  isolatedBuffer = malloc(alsize);
191  if (isolatedBuffer == NULL) {
192  goto bail;
193  }
194  memcpy(isolatedBuffer, albuffer, alsize);
195  (void) AppLayerParserParse(NULL, alp_tctx, f, f->alproto, flags, isolatedBuffer, alsize);
196  free(isolatedBuffer);
197  }
198 
199 bail:
200  FLOWLOCK_UNLOCK(f);
201  FlowFree(f);
202 
203  return 0;
204 }
util-byte.h
surifuzz
SCInstance surifuzz
Definition: fuzz_applayerparserparse.c:37
flow-util.h
SC_ATOMIC_SET
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:386
AppLayerParserTransactionsCleanup
void AppLayerParserTransactionsCleanup(Flow *f, const uint8_t pkt_dir)
remove obsolete (inspected and logged) transactions
Definition: app-layer-parser.c:882
Flow_::proto
uint8_t proto
Definition: flow.h:379
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:85
LLVMFuzzerInitialize
int LLVMFuzzerInitialize(int *argc, char ***argv)
Definition: fuzz_applayerparserparse.c:42
configNoChecksum
const char * configNoChecksum
Definition: confyaml.c:1
Flow_
Flow data structure.
Definition: flow.h:357
Flow_::protomap
uint8_t protomap
Definition: flow.h:446
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:40
SCRunmodeSet
void SCRunmodeSet(int run_mode)
Set the current run mode.
Definition: suricata.c:265
GlobalsInitPreConfig
void GlobalsInitPreConfig(void)
Definition: suricata.c:358
Flow_::dp
Port dp
Definition: flow.h:373
HEADER_LEN
#define HEADER_LEN
Definition: fuzz_applayerparserparse.c:17
APP_LAYER_PARSER_EOF_TS
#define APP_LAYER_PARSER_EOF_TS
Definition: app-layer-parser.h:39
Flow_::protoctx
void * protoctx
Definition: flow.h:442
FLOW_IPV4
#define FLOW_IPV4
Definition: flow.h:99
util-unittest-helper.h
FLOWLOCK_UNLOCK
#define FLOWLOCK_UNLOCK(fb)
Definition: flow.h:274
Flow_::alparser
AppLayerParserState * alparser
Definition: flow.h:476
Flow_::dst
FlowAddress dst
Definition: flow.h:360
app-layer-detect-proto.h
separator
const uint8_t separator[]
Definition: fuzz_applayerparserparse.c:36
g_alproto_max
AppProto g_alproto_max
Definition: app-layer-protos.c:29
alp_tctx
AppLayerParserThreadCtx * alp_tctx
Definition: fuzz_applayerparserparse.c:22
StringToAppProto
AppProto StringToAppProto(const char *proto_name)
Maps a string to its ALPROTO_* equivalent.
Definition: app-layer-protos.c:60
FLOWLOCK_WRLOCK
#define FLOWLOCK_WRLOCK(fb)
Definition: flow.h:271
SURICATA_RUNTIME
@ SURICATA_RUNTIME
Definition: suricata.h:96
ConfYamlLoadString
int ConfYamlLoadString(const char *string, size_t len)
Load configuration from a YAML string.
Definition: conf-yaml-loader.c:523
PostConfLoadedSetup
int PostConfLoadedSetup(SCInstance *suri)
Definition: suricata.c:2619
app-layer-parser.h
FlowGetProtoMapping
uint8_t FlowGetProtoMapping(uint8_t proto)
Function to map the protocol to the defined FLOW_PROTO_* enumeration.
Definition: flow-util.c:98
conf-yaml-loader.h
APP_LAYER_PARSER_EOF_TC
#define APP_LAYER_PARSER_EOF_TC
Definition: app-layer-parser.h:40
setenv
void setenv(const char *name, const char *value, int overwrite)
Flow_::src
FlowAddress src
Definition: flow.h:360
target_suffix
char * target_suffix
Definition: fuzz_applayerparserparse.c:39
AppLayerParserThreadCtxAlloc
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol's parser thread context.
Definition: app-layer-parser.c:285
util-conf.h
flags
uint8_t flags
Definition: decode-gre.h:0
AppLayerParserParse
int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow *f, AppProto alproto, uint8_t flags, const uint8_t *input, uint32_t input_len)
Definition: app-layer-parser.c:1274
suricata-common.h
FlowFree
void FlowFree(Flow *f)
cleanup & free the memory of a flow
Definition: flow-util.c:83
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
ConfigSetLogDirectory
TmEcode ConfigSetLogDirectory(const char *name)
Definition: util-conf.c:33
Flow_::flags
uint32_t flags
Definition: flow.h:422
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:76
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
suricata.h
SCInstance_
Definition: suricata.h:123
SC_ATOMIC_EXTERN
SC_ATOMIC_EXTERN(unsigned int, engine_stage)
AppLayerParserThreadCtx_
Definition: app-layer-parser.c:58
InitGlobal
int InitGlobal(void)
Global initialization common to all runmodes.
Definition: suricata.c:2851
FlowChangeProto
int FlowChangeProto(Flow *f)
Check if change proto flag is set for flow.
Definition: flow.c:204
Flow_::sp
Port sp
Definition: flow.h:362
TcpSession_
Definition: stream-tcp-private.h:283
FlowAlloc
Flow * FlowAlloc(void)
allocate a flow
Definition: flow-util.c:55
AppLayerParserStateIssetFlag
uint16_t AppLayerParserStateIssetFlag(AppLayerParserState *pstate, uint16_t flag)
Definition: app-layer-parser.c:1793
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:451
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Definition: fuzz_applayerparserparse.c:52
RUNMODE_PCAP_FILE
@ RUNMODE_PCAP_FILE
Definition: runmodes.h:30
forceLayer
AppProto forceLayer
Definition: fuzz_applayerparserparse.c:38