fuzz: make targets more resitant to allocation failures

pull/8891/head
Philippe Antoine 2 years ago
parent 3247e39f0c
commit 5fb0b3b8cb

@ -36,6 +36,7 @@ AppLayerParserThreadCtx *alp_tctx = NULL;
const uint8_t separator[] = {0x01, 0xD5, 0xCA, 0x7A}; const uint8_t separator[] = {0x01, 0xD5, 0xCA, 0x7A};
SCInstance surifuzz; SCInstance surifuzz;
AppProto forceLayer = 0; AppProto forceLayer = 0;
SC_ATOMIC_EXTERN(unsigned int, engine_stage);
int LLVMFuzzerInitialize(int *argc, char ***argv) int LLVMFuzzerInitialize(int *argc, char ***argv)
{ {
@ -75,10 +76,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
// otherwise overflows do not fail as they read the next packet // otherwise overflows do not fail as they read the next packet
uint8_t * isolatedBuffer; uint8_t * isolatedBuffer;
if (size < HEADER_LEN) {
return 0;
}
if (alp_tctx == NULL) { if (alp_tctx == NULL) {
//Redirects logs to /dev/null //Redirects logs to /dev/null
setenv("SC_LOG_OP_IFACE", "file", 0); setenv("SC_LOG_OP_IFACE", "file", 0);
@ -97,6 +94,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
PostConfLoadedSetup(&surifuzz); PostConfLoadedSetup(&surifuzz);
alp_tctx = AppLayerParserThreadCtxAlloc(); alp_tctx = AppLayerParserThreadCtxAlloc();
SC_ATOMIC_SET(engine_stage, SURICATA_RUNTIME);
}
if (size < HEADER_LEN) {
return 0;
} }
if (data[0] >= ALPROTO_MAX) { if (data[0] >= ALPROTO_MAX) {
@ -149,7 +151,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
// only if we have some data // only if we have some data
isolatedBuffer = malloc(alnext - albuffer); isolatedBuffer = malloc(alnext - albuffer);
if (isolatedBuffer == NULL) { if (isolatedBuffer == NULL) {
return 0; goto bail;
} }
memcpy(isolatedBuffer, albuffer, alnext - albuffer); memcpy(isolatedBuffer, albuffer, alnext - albuffer);
(void) AppLayerParserParse(NULL, alp_tctx, f, f->alproto, flags, isolatedBuffer, alnext - albuffer); (void) AppLayerParserParse(NULL, alp_tctx, f, f->alproto, flags, isolatedBuffer, alnext - albuffer);
@ -192,13 +194,14 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
flags |= STREAM_EOF; flags |= STREAM_EOF;
isolatedBuffer = malloc(alsize); isolatedBuffer = malloc(alsize);
if (isolatedBuffer == NULL) { if (isolatedBuffer == NULL) {
return 0; goto bail;
} }
memcpy(isolatedBuffer, albuffer, alsize); memcpy(isolatedBuffer, albuffer, alsize);
(void) AppLayerParserParse(NULL, alp_tctx, f, f->alproto, flags, isolatedBuffer, alsize); (void) AppLayerParserParse(NULL, alp_tctx, f, f->alproto, flags, isolatedBuffer, alsize);
free(isolatedBuffer); free(isolatedBuffer);
} }
bail:
FLOWLOCK_UNLOCK(f); FLOWLOCK_UNLOCK(f);
FlowFree(f); FlowFree(f);

@ -23,6 +23,7 @@
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
AppLayerProtoDetectThreadCtx *alpd_tctx = NULL; AppLayerProtoDetectThreadCtx *alpd_tctx = NULL;
SC_ATOMIC_EXTERN(unsigned int, engine_stage);
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{ {
@ -32,10 +33,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
AppProto alproto; AppProto alproto;
AppProto alproto2; AppProto alproto2;
if (size < HEADER_LEN) {
return 0;
}
if (alpd_tctx == NULL) { if (alpd_tctx == NULL) {
//global init //global init
InitGlobal(); InitGlobal();
@ -50,6 +47,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
AppLayerParserSetup(); AppLayerParserSetup();
AppLayerParserRegisterProtocolParsers(); AppLayerParserRegisterProtocolParsers();
alpd_tctx = AppLayerProtoDetectGetCtxThread(); alpd_tctx = AppLayerProtoDetectGetCtxThread();
SC_ATOMIC_SET(engine_stage, SURICATA_RUNTIME);
}
if (size < HEADER_LEN) {
return 0;
} }
f = TestHelperBuildFlow(AF_INET, "1.2.3.4", "5.6.7.8", (uint16_t)((data[2] << 8) | data[3]), f = TestHelperBuildFlow(AF_INET, "1.2.3.4", "5.6.7.8", (uint16_t)((data[2] << 8) | data[3]),

@ -31,6 +31,7 @@ pcap-file:\n\
ThreadVars *tv; ThreadVars *tv;
DecodeThreadVars *dtv; DecodeThreadVars *dtv;
SC_ATOMIC_EXTERN(unsigned int, engine_stage);
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{ {
@ -80,6 +81,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
extern uint16_t max_pending_packets; extern uint16_t max_pending_packets;
max_pending_packets = 128; max_pending_packets = 128;
PacketPoolInit(); PacketPoolInit();
SC_ATOMIC_SET(engine_stage, SURICATA_RUNTIME);
initialized = 1; initialized = 1;
} }

@ -40,6 +40,7 @@ DecodeThreadVars *dtv;
// FlowWorkerThreadData // FlowWorkerThreadData
void *fwd; void *fwd;
SCInstance surifuzz; SCInstance surifuzz;
SC_ATOMIC_EXTERN(unsigned int, engine_stage);
#include "confyaml.c" #include "confyaml.c"
@ -103,6 +104,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
return 0; return 0;
} }
SC_ATOMIC_SET(engine_stage, SURICATA_RUNTIME);
initialized = 1; initialized = 1;
} }
@ -117,7 +119,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
// loop over packets // loop over packets
r = FPC_next(&pkts, &header, &pkt); r = FPC_next(&pkts, &header, &pkt);
p = PacketGetFromAlloc(); p = PacketGetFromAlloc();
if (r <= 0 || header.ts.tv_sec >= INT_MAX - 3600) { if (p == NULL || r <= 0 || header.ts.tv_sec >= INT_MAX - 3600) {
goto bail; goto bail;
} }
p->ts = SCTIME_FROM_TIMEVAL(&header.ts); p->ts = SCTIME_FROM_TIMEVAL(&header.ts);
@ -154,7 +156,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
p->pkt_src = PKT_SRC_WIRE; p->pkt_src = PKT_SRC_WIRE;
} }
bail: bail:
if (p != NULL) {
PacketFree(p); PacketFree(p);
}
FlowReset(); FlowReset();
return 0; return 0;

@ -40,6 +40,7 @@ DecodeThreadVars *dtv;
//FlowWorkerThreadData //FlowWorkerThreadData
void *fwd; void *fwd;
SCInstance surifuzz; SCInstance surifuzz;
SC_ATOMIC_EXTERN(unsigned int, engine_stage);
#include "confyaml.c" #include "confyaml.c"
@ -92,6 +93,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
extern uint16_t max_pending_packets; extern uint16_t max_pending_packets;
max_pending_packets = 128; max_pending_packets = 128;
PacketPoolInit(); PacketPoolInit();
SC_ATOMIC_SET(engine_stage, SURICATA_RUNTIME);
initialized = 1; initialized = 1;
} }

@ -40,6 +40,7 @@ DecodeThreadVars *dtv;
// FlowWorkerThreadData // FlowWorkerThreadData
void *fwd; void *fwd;
SCInstance surifuzz; SCInstance surifuzz;
SC_ATOMIC_EXTERN(unsigned int, engine_stage);
#include "confyaml.c" #include "confyaml.c"
@ -118,6 +119,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
extern uint16_t max_pending_packets; extern uint16_t max_pending_packets;
max_pending_packets = 128; max_pending_packets = 128;
PacketPoolInit(); PacketPoolInit();
SC_ATOMIC_SET(engine_stage, SURICATA_RUNTIME);
initialized = 1; initialized = 1;
} }

Loading…
Cancel
Save