suricata
fuzz_applayerprotodetectgetproto.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 "suricata-common.h"
9 #include "suricata.h"
10 #include "app-layer-detect-proto.h"
11 #include "flow-util.h"
12 #include "app-layer.h"
13 #include "util-unittest-helper.h"
14 #include "conf-yaml-loader.h"
15 
16 #define HEADER_LEN 6
17 
18 //rule of thumb constant, so as not to timeout target
19 #define PROTO_DETECT_MAX_LEN 1024
20 
21 extern const char *configNoChecksum;
22 
23 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
24 
26 SC_ATOMIC_EXTERN(unsigned int, engine_stage);
27 
28 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
29 {
30  Flow *f;
31  TcpSession ssn;
32  bool reverse;
33 
34  if (alpd_tctx == NULL) {
35  //global init
36  InitGlobal();
39  abort();
40  }
41  MpmTableSetup();
42  SpmTableSetup();
44  AppLayerSetup();
46  SC_ATOMIC_SET(engine_stage, SURICATA_RUNTIME);
47  }
48 
49  if (size < HEADER_LEN) {
50  return 0;
51  }
52 
53  f = TestHelperBuildFlow(AF_INET, "1.2.3.4", "5.6.7.8", (uint16_t)((data[2] << 8) | data[3]),
54  (uint16_t)((data[4] << 8) | data[5]));
55  if (f == NULL) {
56  return 0;
57  }
58  f->proto = data[1];
59  memset(&ssn, 0, sizeof(TcpSession));
60  f->protoctx = &ssn;
62 
63  uint8_t flags = STREAM_TOCLIENT;
64  if (data[0] & STREAM_TOSERVER) {
65  flags = STREAM_TOSERVER;
66  }
68  alpd_tctx, f, data + HEADER_LEN, size - HEADER_LEN, f->proto, flags, &reverse);
69  FlowFree(f);
70 
71  return 0;
72 }
RUNMODE_UNITTEST
@ RUNMODE_UNITTEST
Definition: runmodes.h:40
flow-util.h
SC_ATOMIC_SET
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:386
Flow_::proto
uint8_t proto
Definition: flow.h:378
HEADER_LEN
#define HEADER_LEN
Definition: fuzz_applayerprotodetectgetproto.c:16
Flow_
Flow data structure.
Definition: flow.h:356
Flow_::protomap
uint8_t protomap
Definition: flow.h:450
SCRunmodeSet
void SCRunmodeSet(int run_mode)
Set the current run mode.
Definition: suricata.c:265
Flow_::protoctx
void * protoctx
Definition: flow.h:446
util-unittest-helper.h
AppLayerSetup
int AppLayerSetup(void)
Setup the app layer.
Definition: app-layer.c:1032
app-layer-detect-proto.h
AppLayerProtoDetectThreadCtx_
The app layer protocol detection thread context.
Definition: app-layer-detect-proto.c:174
EngineModeSetIDS
void EngineModeSetIDS(void)
Definition: suricata.c:245
SpmTableSetup
void SpmTableSetup(void)
Definition: util-spm.c:122
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Definition: fuzz_applayerprotodetectgetproto.c:28
SURICATA_RUNTIME
@ SURICATA_RUNTIME
Definition: suricata.h:96
alpd_tctx
AppLayerProtoDetectThreadCtx * alpd_tctx
Definition: fuzz_applayerprotodetectgetproto.c:25
ConfYamlLoadString
int ConfYamlLoadString(const char *string, size_t len)
Load configuration from a YAML string.
Definition: conf-yaml-loader.c:523
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
MpmTableSetup
void MpmTableSetup(void)
Definition: util-mpm.c:225
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
FlowFree
void FlowFree(Flow *f)
cleanup & free the memory of a flow
Definition: flow-util.c:83
SC_ATOMIC_EXTERN
SC_ATOMIC_EXTERN(unsigned int, engine_stage)
configNoChecksum
const char * configNoChecksum
Definition: confyaml.c:1
AppLayerProtoDetectGetProto
AppProto AppLayerProtoDetectGetProto(AppLayerProtoDetectThreadCtx *tctx, Flow *f, const uint8_t *buf, uint32_t buflen, uint8_t ipproto, uint8_t flags, bool *reverse_flow)
Returns the app layer protocol given a buffer.
Definition: app-layer-detect-proto.c:1396
AppLayerProtoDetectGetCtxThread
AppLayerProtoDetectThreadCtx * AppLayerProtoDetectGetCtxThread(void)
Inits and returns an app layer protocol detection thread context.
Definition: app-layer-detect-proto.c:1957
suricata.h
TestHelperBuildFlow
Flow * TestHelperBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
Definition: util-unittest-helper.c:52
InitGlobal
int InitGlobal(void)
Global initialization common to all runmodes.
Definition: suricata.c:2856
TcpSession_
Definition: stream-tcp-private.h:283
app-layer.h