suricata
fuzz_iprep.c
Go to the documentation of this file.
1 /**
2  * @file
3  * @author Philippe Antoine <contact@catenacyber.fr>
4  */
5 
6 #include "suricata-common.h"
7 #include "detect-engine.h"
8 #include "detect-engine-build.h"
9 #include "detect-parse.h"
10 #include "util-fmemopen.h"
11 #include "reputation.h"
12 #include "util-unittest-helper.h"
13 #include "tmqh-packetpool.h"
14 #include "util-conf.h"
15 
16 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
17 
19 static int initialized = 0;
22 
23 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
24 {
25  if (initialized == 0) {
26  // Redirects logs to /dev/null
27  setenv("SC_LOG_OP_IFACE", "file", 0);
28  setenv("SC_LOG_FILE", "/dev/null", 0);
29 
30  InitGlobal();
31 
33  // redirect logs to /tmp
34  ConfigSetLogDirectory("/tmp/");
35 
39 
40  extern uint32_t max_pending_packets;
41  max_pending_packets = 128;
44  p = UTHBuildPacket((uint8_t *)"fuzzfuzz", 8, IPPROTO_TCP);
45  p->alerts.cnt = 0;
46  p->action = 0;
47  initialized = 1;
48  }
49 
50  FILE *fd = NULL;
51  size_t kw_len = 0;
52  size_t split2 = 0;
53  while (kw_len < size && data[kw_len] != ';') {
54  kw_len++;
55  }
56  if (kw_len >= size) {
57  return 0;
58  }
59  split2 = kw_len + 1;
60  while (split2 < size && data[split2] != 0) {
61  split2++;
62  }
63  if (split2 >= size) {
64  return 0;
65  }
66  const uint8_t *category_data = data + kw_len + 1;
67  size_t category_len = split2 - kw_len - 1;
68  const uint8_t *ipreplist_data = data + split2 + 1;
69  size_t ipreplist_len = size - split2 - 1;
70 
71  /* Build the full signature string */
72  char sig_buf[DETECT_MAX_RULE_SIZE];
73  size_t sig_len = strlcat(sig_buf, "alert ip any any -> any any (iprep:", sizeof(sig_buf));
74  if (sig_len + kw_len >= DETECT_MAX_RULE_SIZE) {
75  return 0;
76  }
77  memcpy(sig_buf + sig_len, data, kw_len);
78  sig_len += kw_len;
79  sig_len += strlcat(sig_buf + sig_len, "; sid:1;)", sizeof(sig_buf) - sig_len);
80 
81  /* ------------------------------------------------------------------ *
82  * Per-iteration setup, mirroring DetectIPRepTest01
83  * ------------------------------------------------------------------ */
84 
86  de_ctx->flags |= DE_QUIET;
87  DetectEngineThreadCtx *det_ctx = NULL;
88  DetectEngineThreadCtxInit(th_v, (void *)de_ctx, (void *)&det_ctx);
89 
90  /* Reset the global srep_version so that SRepLoadCatFileFromFD (which
91  * asserts version == 0) can be called, and so that CIDR reputation
92  * entries written with version 0 are visible to the matcher
93  * (de_ctx->srep_version also stays 0 because SRepInit returns early
94  * when no reputation config is present). */
96 
97  /* Load the fuzz-controlled reputation categories file (if any) */
98  fd = SCFmemopen((void *)category_data, category_len, "r");
99  if (fd != NULL) {
100  (void)SRepLoadCatFileFromFD(fd);
101  fclose(fd);
102  }
103 
104  /* Load fixed CIDR networks into the per-de_ctx reputation tree */
105  fd = SCFmemopen((void *)ipreplist_data, ipreplist_len, "r");
106  if (fd != NULL) {
108  fclose(fd);
109  }
110 
111  Signature *sig = DetectEngineAppendSig(de_ctx, sig_buf);
112  if (sig == NULL) {
113  DetectEngineThreadCtxDeinit(th_v, (void *)det_ctx);
115  return 0;
116  }
118  SigMatchSignatures(th_v, de_ctx, det_ctx, p);
119  DetectEngineThreadCtxDeinit(th_v, (void *)det_ctx);
121 
122  return 0;
123 }
detect-engine.h
surifuzz
SCInstance surifuzz
Definition: fuzz_iprep.c:18
util-fmemopen.h
PacketAlerts_::cnt
uint16_t cnt
Definition: decode.h:288
Packet_::action
uint8_t action
Definition: decode.h:622
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Definition: fuzz_iprep.c:23
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:973
th_v
ThreadVars * th_v
Definition: fuzz_iprep.c:20
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2715
DE_QUIET
#define DE_QUIET
Definition: detect.h:330
UTHBuildPacket
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
Definition: util-unittest-helper.c:243
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:2971
GlobalsInitPreConfig
void GlobalsInitPreConfig(void)
Definition: suricata.c:404
p
Packet * p
Definition: fuzz_iprep.c:21
SCFmemopen
#define SCFmemopen
Definition: util-fmemopen.h:52
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3580
Packet_::alerts
PacketAlerts alerts
Definition: decode.h:635
tmqh-packetpool.h
util-unittest-helper.h
PacketPoolInit
void PacketPoolInit(void)
Definition: tmqh-packetpool.c:235
ThreadVarsAlloc
ThreadVars * ThreadVarsAlloc(void)
Allocate a new ThreadVars structure.
Definition: threadvars.c:28
SCRunmodeGet
SCRunMode SCRunmodeGet(void)
Get the current run mode.
Definition: suricata.c:301
PreRunPostPrivsDropInit
void PreRunPostPrivsDropInit(const int runmode)
Definition: suricata.c:2373
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1291
strlcat
size_t strlcat(char *, const char *src, size_t siz)
Definition: util-strlcatu.c:45
DetectEngineCtx_::srepCIDR_ctx
SRepCIDRTree * srepCIDR_ctx
Definition: detect.h:989
SRepLoadCatFileFromFD
int SRepLoadCatFileFromFD(FILE *fp)
Definition: reputation.c:368
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3461
PostConfLoadedSetup
int PostConfLoadedSetup(SCInstance *suri)
Definition: suricata.c:2805
Packet_
Definition: decode.h:514
PostConfLoadedDetectSetup
void PostConfLoadedDetectSetup(SCInstance *suri)
Definition: suricata.c:2700
detect-engine-build.h
DETECT_MAX_RULE_SIZE
#define DETECT_MAX_RULE_SIZE
Definition: detect.h:46
reputation.h
SRepLoadFileFromFD
int SRepLoadFileFromFD(SRepCIDRTree *cidr_ctx, FILE *fp)
Definition: reputation.c:424
setenv
void setenv(const char *name, const char *value, int overwrite)
max_pending_packets
uint32_t max_pending_packets
Definition: suricata.c:187
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2274
util-conf.h
suricata-common.h
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Definition: detect-engine.c:3706
ConfigSetLogDirectory
TmEcode ConfigSetLogDirectory(const char *name)
Definition: util-conf.c:33
detect-parse.h
Signature_
Signature container.
Definition: detect.h:675
SRepResetVersion
void SRepResetVersion(void)
Definition: reputation.c:64
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2676
SCInstance_
Definition: suricata.h:134
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:975
InitGlobal
int InitGlobal(void)
Global initialization common to all runmodes.
Definition: suricata.c:3054