From 5c8d90afc8d5484aa26d7657eb5c046cb0dbcb25 Mon Sep 17 00:00:00 2001 From: Gurvinder Singh Date: Fri, 15 Jan 2010 15:23:26 +0100 Subject: [PATCH] memory leak fixes --- src/alert-unified-alert.c | 13 ++++------ src/alert-unified-log.c | 8 +++--- src/alert-unified2-alert.c | 13 ++++------ src/app-layer-detect-proto.c | 43 ++++++++++++++++++++++++-------- src/app-layer-detect-proto.h | 2 ++ src/counters.c | 1 + src/detect-parse.c | 4 +-- src/stream-tcp-reassemble.c | 7 ++++-- src/stream-tcp-reassemble.h | 1 + src/stream-tcp.c | 12 +++++---- src/suricata.c | 1 + src/util-classification-config.c | 8 ++++-- 12 files changed, 72 insertions(+), 41 deletions(-) diff --git a/src/alert-unified-alert.c b/src/alert-unified-alert.c index da17a202f0..4da9ad5e79 100644 --- a/src/alert-unified-alert.c +++ b/src/alert-unified-alert.c @@ -250,11 +250,6 @@ TmEcode AlertUnifiedAlertThreadDeinit(ThreadVars *t, void *data) return TM_ECODE_OK; error: - /* clear memory */ - if (aun != NULL) { - memset(aun, 0, sizeof(AlertUnifiedAlertThread)); - free(aun); - } return TM_ECODE_FAILED; } @@ -296,6 +291,7 @@ LogFileCtx *AlertUnifiedAlertInitCtx(ConfNode *conf) * */ int AlertUnifiedAlertOpenFileCtx(LogFileCtx *file_ctx, const char *prefix) { + int ret = 0; char *filename = NULL; if (file_ctx->filename != NULL) filename = file_ctx->filename; @@ -319,10 +315,11 @@ int AlertUnifiedAlertOpenFileCtx(LogFileCtx *file_ctx, const char *prefix) if (file_ctx->fp == NULL) { SCLogError(SC_ERR_FOPEN, "ERROR: failed to open %s: %s", filename, strerror(errno)); - return -1; + ret = -1; } - - return 0; + if (filename != NULL) + free(filename); + return ret; } #ifdef UNITTESTS diff --git a/src/alert-unified-log.c b/src/alert-unified-log.c index ae63143780..fc524a032e 100644 --- a/src/alert-unified-log.c +++ b/src/alert-unified-log.c @@ -312,6 +312,7 @@ LogFileCtx *AlertUnifiedLogInitCtx(ConfNode *conf) * */ int AlertUnifiedLogOpenFileCtx(LogFileCtx *file_ctx, const char *prefix) { + int ret = 0; char *filename = NULL; if (file_ctx->filename != NULL) filename = file_ctx->filename; @@ -335,10 +336,11 @@ int AlertUnifiedLogOpenFileCtx(LogFileCtx *file_ctx, const char *prefix) if (file_ctx->fp == NULL) { SCLogError(SC_ERR_FOPEN, "ERROR: failed to open %s: %s", filename, strerror(errno)); - return -1; + ret = -1; } - - return 0; + if (filename != NULL) + free(filename); + return ret; } #ifdef UNITTESTS diff --git a/src/alert-unified2-alert.c b/src/alert-unified2-alert.c index 3a882555ff..d0f284c945 100644 --- a/src/alert-unified2-alert.c +++ b/src/alert-unified2-alert.c @@ -536,11 +536,6 @@ TmEcode Unified2AlertThreadDeinit(ThreadVars *t, void *data) return TM_ECODE_OK; error: - /* clear memory */ - if (aun != NULL) { - memset(aun, 0, sizeof(Unified2AlertThread)); - free(aun); - } return TM_ECODE_FAILED; } @@ -583,6 +578,7 @@ LogFileCtx *Unified2AlertInitCtx(ConfNode *conf) * */ int Unified2AlertOpenFileCtx(LogFileCtx *file_ctx, const char *prefix) { + int ret = 0; char *filename = NULL; if (file_ctx->filename != NULL) filename = file_ctx->filename; @@ -606,10 +602,11 @@ int Unified2AlertOpenFileCtx(LogFileCtx *file_ctx, const char *prefix) if (file_ctx->fp == NULL) { SCLogError(SC_ERR_FOPEN, "ERROR: failed to open %s: %s", filename, strerror(errno)); - return -1; + ret = -1; } - - return 0; + if (filename != NULL) + free(filename); + return ret; } diff --git a/src/app-layer-detect-proto.c b/src/app-layer-detect-proto.c index 8b0ac47849..bd0c80cf88 100644 --- a/src/app-layer-detect-proto.c +++ b/src/app-layer-detect-proto.c @@ -70,11 +70,18 @@ void AlpProtoInit(AlpProtoDetectCtx *ctx) { ctx->toclient.id = 0; } -void AlpProtoDestroy(AlpProtoDetectCtx *ctx) { +void AlpProtoTestDestroy(AlpProtoDetectCtx *ctx) { mpm_table[ctx->toserver.mpm_ctx.mpm_type].DestroyCtx(&ctx->toserver.mpm_ctx); mpm_table[ctx->toclient.mpm_ctx.mpm_type].DestroyCtx(&ctx->toclient.mpm_ctx); } +void AlpProtoDestroy() { + SCEnter(); + mpm_table[alp_proto_ctx.toserver.mpm_ctx.mpm_type].DestroyCtx(&alp_proto_ctx.toserver.mpm_ctx); + mpm_table[alp_proto_ctx.toclient.mpm_ctx.mpm_type].DestroyCtx(&alp_proto_ctx.toclient.mpm_ctx); + SCReturn; +} + /** \brief Add a proto detection string to the detection ctx. * \param ctx The detection ctx * \param ip_proto The IP proto (TCP, UDP, etc) @@ -129,6 +136,20 @@ void AlpProtoFinalizeThread(AlpProtoDetectCtx *ctx, AlpProtoDetectThreadCtx *tct } } +void AlpProtoDeFinalize2Thread(AlpProtoDetectThreadCtx *tctx) { + if (alp_proto_ctx.toclient.id > 0) { + mpm_table[alp_proto_ctx.toclient.mpm_ctx.mpm_type].DestroyThreadCtx + (&alp_proto_ctx.toclient.mpm_ctx, &tctx->toclient.mpm_ctx); + /* XXX GS any idea why it is invalid free ?*/ + //PmqFree(&tctx->toclient.pmq); + } + if (alp_proto_ctx.toserver.id > 0) { + mpm_table[alp_proto_ctx.toserver.mpm_ctx.mpm_type].DestroyThreadCtx + (&alp_proto_ctx.toserver.mpm_ctx, &tctx->toserver.mpm_ctx); + //PmqFree(&tctx->toserver.pmq); + } + +} /** \brief to be called by ReassemblyThreadInit * \todo this is a hack, we need a proper place to store the global ctx */ void AlpProtoFinalize2Thread(AlpProtoDetectThreadCtx *tctx) { @@ -468,7 +489,7 @@ int AlpDetectTest01(void) { } free(buf); - AlpProtoDestroy(&ctx); + AlpProtoTestDestroy(&ctx); return r; } @@ -502,7 +523,7 @@ int AlpDetectTest02(void) { r = 0; } - AlpProtoDestroy(&ctx); + AlpProtoTestDestroy(&ctx); return r; } @@ -547,7 +568,7 @@ int AlpDetectTest03(void) { r = 0; } - AlpProtoDestroy(&ctx); + AlpProtoTestDestroy(&ctx); return r; } @@ -580,7 +601,7 @@ int AlpDetectTest04(void) { r = 0; } - AlpProtoDestroy(&ctx); + AlpProtoTestDestroy(&ctx); return r; } @@ -626,7 +647,7 @@ int AlpDetectTest05(void) { r = 0; } - AlpProtoDestroy(&ctx); + AlpProtoTestDestroy(&ctx); return r; } @@ -671,7 +692,7 @@ int AlpDetectTest06(void) { r = 0; } - AlpProtoDestroy(&ctx); + AlpProtoTestDestroy(&ctx); return r; } @@ -704,7 +725,7 @@ int AlpDetectTest07(void) { r = 0; } - AlpProtoDestroy(&ctx); + AlpProtoTestDestroy(&ctx); return r; } @@ -748,7 +769,7 @@ int AlpDetectTest08(void) { r = 0; } - AlpProtoDestroy(&ctx); + AlpProtoTestDestroy(&ctx); return r; } @@ -789,7 +810,7 @@ int AlpDetectTest09(void) { r = 0; } - AlpProtoDestroy(&ctx); + AlpProtoTestDestroy(&ctx); return r; } @@ -826,7 +847,7 @@ int AlpDetectTest10(void) { r = 0; } - AlpProtoDestroy(&ctx); + AlpProtoTestDestroy(&ctx); return r; } diff --git a/src/app-layer-detect-proto.h b/src/app-layer-detect-proto.h index 3ee7b93d25..1307b110ee 100644 --- a/src/app-layer-detect-proto.h +++ b/src/app-layer-detect-proto.h @@ -22,6 +22,8 @@ void AppLayerDetectProtoThreadSpawn(void); void AlpDetectRegisterTests(void); void AlpProtoFinalize2Thread(AlpProtoDetectThreadCtx *); +void AlpProtoDeFinalize2Thread (AlpProtoDetectThreadCtx *); +void AlpProtoDestroy(void); #endif /* __APP_LAYER_DETECT_PROTO_H__ */ diff --git a/src/counters.c b/src/counters.c index 75e87ecbf3..fccf0ed683 100644 --- a/src/counters.c +++ b/src/counters.c @@ -325,6 +325,7 @@ static int SCPerfParseTBCounterInterval(SCPerfCounter *pc, char *interval) (pc->type_q->minutes * 60) + pc->type_q->seconds); TimeGet(&pc->type_q->ts); + free(regex); return 0; error: diff --git a/src/detect-parse.c b/src/detect-parse.c index b6a736960f..7cff5c1f59 100644 --- a/src/detect-parse.c +++ b/src/detect-parse.c @@ -542,10 +542,10 @@ int SigParse(DetectEngineCtx *de_ctx, Signature *s, char *sigstr, uint8_t addrs_ if (basics != NULL) { int i = 0; while (basics[i] != NULL) { - //free(basics[i]); + free(basics[i]); i++; } - //free(basics); + free(basics); } SCReturnInt(ret); diff --git a/src/stream-tcp-reassemble.c b/src/stream-tcp-reassemble.c index 47464d5e1b..951d7a919d 100644 --- a/src/stream-tcp-reassemble.c +++ b/src/stream-tcp-reassemble.c @@ -176,6 +176,7 @@ void StreamTcpReassembleFree(char quiet) TcpReassemblyThreadCtx *StreamTcpReassembleInitThreadCtx(void) { + SCEnter(); TcpReassemblyThreadCtx *ra_ctx = malloc(sizeof(TcpReassemblyThreadCtx)); if (ra_ctx == NULL) { return NULL; @@ -185,17 +186,19 @@ TcpReassemblyThreadCtx *StreamTcpReassembleInitThreadCtx(void) ra_ctx->stream_q = StreamMsgQueueGetNew(); AlpProtoFinalize2Thread(&ra_ctx->dp_ctx); - return ra_ctx; + SCReturnPtr(ra_ctx, "TcpReassemblyThreadCtx"); } void StreamTcpReassembleFreeThreadCtx(TcpReassemblyThreadCtx *ra_ctx) { + SCEnter(); if (ra_ctx->stream_q != NULL) StreamMsgQueueFree(ra_ctx->stream_q); ra_ctx->stream_q = NULL; - + AlpProtoDeFinalize2Thread(&ra_ctx->dp_ctx); free(ra_ctx); + SCReturn; } void PrintList2(TcpSegment *seg) diff --git a/src/stream-tcp-reassemble.h b/src/stream-tcp-reassemble.h index 33620cdc3d..0d3f6d7f5c 100644 --- a/src/stream-tcp-reassemble.h +++ b/src/stream-tcp-reassemble.h @@ -42,6 +42,7 @@ int StreamTcpReassembleInit(char); void StreamTcpReassembleFree(char); void StreamTcpReassembleRegisterTests(void); TcpReassemblyThreadCtx *StreamTcpReassembleInitThreadCtx(void); +void StreamTcpReassembleFreeThreadCtx(TcpReassemblyThreadCtx *); int StreamTcpReassembleProcessAppLayer(TcpReassemblyThreadCtx *); void StreamTcpCreateTestPacket(uint8_t *, uint8_t, uint8_t, uint8_t); diff --git a/src/stream-tcp.c b/src/stream-tcp.c index 4f97fade9b..146c39c802 100644 --- a/src/stream-tcp.c +++ b/src/stream-tcp.c @@ -2434,9 +2434,10 @@ TmEcode StreamTcp (ThreadVars *tv, Packet *p, void *data, PacketQueue *pq) TmEcode StreamTcpThreadInit(ThreadVars *tv, void *initdata, void **data) { + SCEnter(); StreamTcpThread *stt = malloc(sizeof(StreamTcpThread)); if (stt == NULL) { - return TM_ECODE_FAILED; + SCReturnInt(TM_ECODE_FAILED); } memset(stt, 0, sizeof(StreamTcpThread)); @@ -2451,15 +2452,16 @@ TmEcode StreamTcpThreadInit(ThreadVars *tv, void *initdata, void **data) /* init reassembly ctx */ stt->ra_ctx = StreamTcpReassembleInitThreadCtx(); if (stt->ra_ctx == NULL) - return TM_ECODE_FAILED; + SCReturnInt(TM_ECODE_FAILED); SCLogDebug("StreamTcp thread specific ctx online at %p, reassembly ctx %p", stt, stt->ra_ctx); - return TM_ECODE_OK; + SCReturnInt(TM_ECODE_OK); } TmEcode StreamTcpThreadDeinit(ThreadVars *tv, void *data) { + SCEnter(); StreamTcpThread *stt = (StreamTcpThread *)data; if (stt == NULL) { return TM_ECODE_OK; @@ -2468,13 +2470,13 @@ TmEcode StreamTcpThreadDeinit(ThreadVars *tv, void *data) /* XXX */ /* free reassembly ctx */ - + StreamTcpReassembleFreeThreadCtx(stt->ra_ctx); /* clear memory */ memset(stt, 0, sizeof(StreamTcpThread)); free(stt); - return TM_ECODE_OK; + SCReturnInt(TM_ECODE_OK); } void StreamTcpExitPrintStats(ThreadVars *tv, void *data) diff --git a/src/suricata.c b/src/suricata.c index d0c03bd548..a414dd12de 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -757,6 +757,7 @@ int main(int argc, char **argv) SigGroupCleanup(de_ctx); SigCleanSignatures(de_ctx); DetectEngineCtxFree(de_ctx); + AlpProtoDestroy(); RunModeShutDown(); OutputDeregisterAll(); diff --git a/src/util-classification-config.c b/src/util-classification-config.c index 48c5e22c2a..13953795a8 100644 --- a/src/util-classification-config.c +++ b/src/util-classification-config.c @@ -126,8 +126,12 @@ static inline int SCClassConfInitContext(DetectEngineCtx *de_ctx) /** * \brief Releases resources used by the Classification Config API. */ -static void SCClassConfDeInitContext(void) +static void SCClassConfDeInitContext(DetectEngineCtx *de_ctx) { + if (de_ctx->class_conf_ht != NULL) { + HashTableFree(de_ctx->class_conf_ht); + de_ctx->class_conf_ht = NULL; + } fclose(fd); default_file_path = SC_CLASS_CONF_DEF_CONF_FILEPATH; fd = NULL; @@ -447,7 +451,7 @@ void SCClassConfLoadClassficationConfigFile(DetectEngineCtx *de_ctx) } SCClassConfParseFile(de_ctx); - SCClassConfDeInitContext(); + SCClassConfDeInitContext(de_ctx); return; }