From cda664a8c45e3b96243a91511fe2a4f39c096057 Mon Sep 17 00:00:00 2001 From: Gurvinder Singh Date: Sun, 30 May 2010 14:35:24 +0200 Subject: [PATCH] memroy leaks fixes in detection module, app layer and counters --- src/app-layer-detect-proto.c | 6 +++--- src/counters.c | 33 +++++++++++++++++++++----------- src/detect-classtype.c | 2 ++ src/detect-content.c | 6 +++++- src/detect-engine-iponly.c | 21 ++++++++++++++------ src/detect-engine-mpm.c | 10 ++++++---- src/detect-engine-port.c | 4 ++-- src/detect-engine-sigorder.c | 12 ++++++++++-- src/detect-engine.c | 5 +++++ src/detect-http-client-body.c | 36 +++++++++++++++++++++++++++-------- src/detect-http-cookie.c | 25 ++++++++++++++++++------ src/detect-http-header.c | 26 +++++++++++++++++++------ src/detect-http-method.c | 5 +++-- src/detect-parse.c | 29 +++++++++++++++++++++++++--- src/detect-uricontent.c | 25 +++++++++++++++++++++++- src/util-debug.c | 12 ++++++++---- src/util-host-os-info.c | 3 +++ src/util-mpm-b2g.c | 6 +++--- src/util-mpm.c | 1 - src/util-spm-bm.c | 18 ++++++++++++++++++ src/util-spm-bm.h | 2 +- 21 files changed, 223 insertions(+), 64 deletions(-) diff --git a/src/app-layer-detect-proto.c b/src/app-layer-detect-proto.c index c4febb5e86..f381fb8820 100644 --- a/src/app-layer-detect-proto.c +++ b/src/app-layer-detect-proto.c @@ -240,6 +240,7 @@ 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); + MpmPatternIdTableFreeHash(alp_proto_ctx.mpm_pattern_id_store); SCReturn; } @@ -266,13 +267,12 @@ 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); + 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); + PmqFree(&tctx->toserver.pmq); } } diff --git a/src/counters.c b/src/counters.c index a9b78616b1..62d1d68b6c 100644 --- a/src/counters.c +++ b/src/counters.c @@ -337,6 +337,10 @@ static void SCPerfInitOPCtx(void) */ static void SCPerfReleaseOPCtx() { + SCPerfClubTMInst *pctmi = NULL; + SCPerfClubTMInst *temp = NULL; + pctmi = sc_perf_op_ctx->pctmi; + if (sc_perf_op_ctx != NULL) { if (sc_perf_op_ctx->fp != NULL) fclose(sc_perf_op_ctx->fp); @@ -344,14 +348,16 @@ static void SCPerfReleaseOPCtx() if (sc_perf_op_ctx->file != NULL) SCFree(sc_perf_op_ctx->file); - if (sc_perf_op_ctx->pctmi != NULL) { - if (sc_perf_op_ctx->pctmi->tm_name != NULL) - SCFree(sc_perf_op_ctx->pctmi->tm_name); + while (pctmi != NULL) { + if (pctmi->tm_name != NULL) + SCFree(pctmi->tm_name); - if (sc_perf_op_ctx->pctmi->head != NULL) - SCFree(sc_perf_op_ctx->pctmi->head); + if (pctmi->head != NULL) + SCFree(pctmi->head); - SCFree(sc_perf_op_ctx->pctmi); + temp = pctmi->next; + SCFree(pctmi); + pctmi = temp; } SCFree(sc_perf_op_ctx); @@ -494,7 +500,8 @@ static int SCPerfParseTBCounterInterval(SCPerfCounter *pc, char *interval) pcre_extra *regex_study = NULL; int opts = 0; const char *ep = NULL; - const char *str_ptr = NULL; + const char *str_ptr1 = NULL; + const char *str_ptr2 = NULL; int eo = 0; int ret = 0; int res = 0; @@ -522,20 +529,20 @@ static int SCPerfParseTBCounterInterval(SCPerfCounter *pc, char *interval) } for (i = 1; i < ret; i += 2) { - res = pcre_get_substring((char *)interval, ov, 30, i, &str_ptr); + res = pcre_get_substring((char *)interval, ov, 30, i, &str_ptr1); if (res < 0) { SCLogInfo("SCPerfParseTBCounterInterval:pcre_get_substring failed"); goto error; } - temp_value = atoi(str_ptr); + temp_value = atoi(str_ptr1); - res = pcre_get_substring((char *)interval, ov, 30, i + 1, &str_ptr); + res = pcre_get_substring((char *)interval, ov, 30, i + 1, &str_ptr2); if (res < 0) { SCLogInfo("SCPerfParseTBCounterInterval:pcre_get_substring failed"); goto error; } - switch (*str_ptr) { + switch (*str_ptr2) { case 'h': if (temp_value < 0 || temp_value > 24) { SCLogInfo("Invalid timebased counter interval"); @@ -571,10 +578,14 @@ static int SCPerfParseTBCounterInterval(SCPerfCounter *pc, char *interval) pc->type_q->total_secs = ((pc->type_q->hours * 60 * 60) + (pc->type_q->minutes * 60) + pc->type_q->seconds); + if (str_ptr1 != NULL) SCFree((char *)str_ptr1); + if (str_ptr2 != NULL) SCFree((char *)str_ptr2); SCFree(regex); return 0; error: + if (str_ptr1 != NULL) SCFree((char *)str_ptr1); + if (str_ptr2 != NULL) SCFree((char *)str_ptr2); return -1; } diff --git a/src/detect-classtype.c b/src/detect-classtype.c index 6e4b4a3297..cfea93f77d 100644 --- a/src/detect-classtype.c +++ b/src/detect-classtype.c @@ -180,9 +180,11 @@ static int DetectClasstypeSetup(DetectEngineCtx *de_ctx, Signature *s, char *raw if (s->prio == -1) s->prio = ct->priority; + pcre_free_substring(parsed_ct_name); return 0; error: + if (parsed_ct_name != NULL) pcre_free_substring(parsed_ct_name); return -1; } diff --git a/src/detect-content.c b/src/detect-content.c index aa54a2d63f..4c72a3114c 100644 --- a/src/detect-content.c +++ b/src/detect-content.c @@ -449,15 +449,19 @@ error: * \param cd pointer to DetectCotentData */ void DetectContentFree(void *ptr) { + SCEnter(); DetectContentData *cd = (DetectContentData *)ptr; if (cd == NULL) - return; + SCReturn; if (cd->content != NULL) SCFree(cd->content); + BoyerMooreCtxDeInit(cd->bm_ctx); + SCFree(cd); + SCReturn; } #ifdef UNITTESTS /* UNITTESTS */ diff --git a/src/detect-engine-iponly.c b/src/detect-engine-iponly.c index 75525d54ad..faf63d4f29 100644 --- a/src/detect-engine-iponly.c +++ b/src/detect-engine-iponly.c @@ -64,14 +64,15 @@ * \retval IPOnlyCIDRItem address of the new instance */ IPOnlyCIDRItem *IPOnlyCIDRItemNew() { + SCEnter(); IPOnlyCIDRItem *item = NULL; item = SCMalloc(sizeof(IPOnlyCIDRItem)); if (item == NULL) - return NULL; + SCReturnPtr(NULL, "NULL"); memset(item, 0, sizeof(IPOnlyCIDRItem)); - return item; + SCReturnPtr(item, "IPOnlyCIDRItem"); } /** @@ -128,7 +129,7 @@ IPOnlyCIDRItem *IPOnlyCIDRItemInsert(IPOnlyCIDRItem *head, /* The first element */ if (head == NULL) { - SCLogDebug("Head is NULL"); + SCLogDebug("Head is NULL to insert item (%p)",item); return item; } @@ -137,7 +138,7 @@ IPOnlyCIDRItem *IPOnlyCIDRItemInsert(IPOnlyCIDRItem *head, return head; } - SCLogDebug("Inserting %u ", item->netmask); + SCLogDebug("Inserting item(%p)->netmast %u head %p", item, item->netmask, head); prev = item; while (prev != NULL) { @@ -158,12 +159,15 @@ IPOnlyCIDRItem *IPOnlyCIDRItemInsert(IPOnlyCIDRItem *head, * \param tmphead Pointer to the list */ void IPOnlyCIDRListFree(IPOnlyCIDRItem *tmphead) { + SCEnter(); uint32_t i = 0; IPOnlyCIDRItem *it, *next = NULL; - if (tmphead == NULL) + if (tmphead == NULL) { + SCLogDebug("temphead is NULL"); return; + } it = tmphead; next = it->next; @@ -171,12 +175,13 @@ void IPOnlyCIDRListFree(IPOnlyCIDRItem *tmphead) { while (it != NULL) { i++; SCFree(it); - SCLogDebug("Item %"PRIu32" removed", i); + SCLogDebug("Item(%p) %"PRIu32" removed\n", it, i); it = next; if (next != NULL) next = next->next; } + SCReturn; } /** @@ -860,6 +865,9 @@ void IPOnlyPrint(DetectEngineCtx *de_ctx, DetectEngineIPOnlyCtx *io_ctx) { */ void IPOnlyDeinit(DetectEngineCtx *de_ctx, DetectEngineIPOnlyCtx *io_ctx) { + if (io_ctx == NULL) + return; + if (io_ctx->tree_ipv4src != NULL) SCRadixReleaseRadixTree(io_ctx->tree_ipv4src); @@ -874,6 +882,7 @@ void IPOnlyDeinit(DetectEngineCtx *de_ctx, DetectEngineIPOnlyCtx *io_ctx) { if (io_ctx->sig_init_array) SCFree(io_ctx->sig_init_array); + io_ctx->sig_init_array = NULL; } diff --git a/src/detect-engine-mpm.c b/src/detect-engine-mpm.c index a6a71376b7..737af45f86 100644 --- a/src/detect-engine-mpm.c +++ b/src/detect-engine-mpm.c @@ -962,9 +962,11 @@ static uint32_t MpmPatternIdHashFunc(HashTable *ht, void *p, uint16_t len) { /** \brief free a MpmPatternIdTableElmt */ static void MpmPatternIdTableElmtFree(void *e) { + SCEnter(); MpmPatternIdTableElmt *c = (MpmPatternIdTableElmt *)e; - free(c->pattern); - free(e); + SCFree(c->pattern); + SCFree(c); + SCReturn; } /** \brief alloc initialize the MpmPatternIdHash */ @@ -1048,7 +1050,7 @@ uint32_t DetectContentGetId(MpmPatternIdStore *ht, DetectContentData *co) { } if (e != NULL) - free(e); + MpmPatternIdTableElmtFree(e); SCReturnUInt(id); } @@ -1097,7 +1099,7 @@ uint32_t DetectUricontentGetId(MpmPatternIdStore *ht, DetectUricontentData *co) } if (e != NULL) - free(e); + MpmPatternIdTableElmtFree(e); SCReturnUInt(id); } diff --git a/src/detect-engine-port.c b/src/detect-engine-port.c index 1e354b62bb..cdcdb8a878 100644 --- a/src/detect-engine-port.c +++ b/src/detect-engine-port.c @@ -1460,7 +1460,7 @@ void DetectPortFreeFunc(void *p) { */ int DetectPortDpHashInit(DetectEngineCtx *de_ctx) { de_ctx->dport_hash_table = HashListTableInit(PORT_HASH_SIZE, - DetectPortHashFunc, DetectPortCompareFunc, NULL); + DetectPortHashFunc, DetectPortCompareFunc, DetectPortFreeFunc); if (de_ctx->dport_hash_table == NULL) goto error; @@ -1525,7 +1525,7 @@ DetectPort *DetectPortDpHashLookup(DetectEngineCtx *de_ctx, DetectPort *p) { */ int DetectPortSpHashInit(DetectEngineCtx *de_ctx) { de_ctx->sport_hash_table = HashListTableInit(PORT_HASH_SIZE, - DetectPortHashFunc, DetectPortCompareFunc, NULL); + DetectPortHashFunc, DetectPortCompareFunc, DetectPortFreeFunc); if (de_ctx->sport_hash_table == NULL) goto error; diff --git a/src/detect-engine-sigorder.c b/src/detect-engine-sigorder.c index bd5cb0c61d..1795b20270 100644 --- a/src/detect-engine-sigorder.c +++ b/src/detect-engine-sigorder.c @@ -904,7 +904,9 @@ void SCSigSignatureOrderingModuleCleanup(DetectEngineCtx *de_ctx) { SCSigOrderFunc *funcs = NULL; SCSigSignatureWrapper *sigw = NULL; + SCSigSignatureWrapper *prev = NULL; void *temp = NULL; + uint8_t i; /* clean the memory alloted to the signature ordering funcs */ funcs = de_ctx->sc_sig_order_funcs; @@ -918,9 +920,15 @@ void SCSigSignatureOrderingModuleCleanup(DetectEngineCtx *de_ctx) /* clean the memory alloted to the signature wrappers */ sigw = de_ctx->sc_sig_sig_wrapper; while (sigw != NULL) { - temp = sigw; + prev = sigw; sigw = sigw->next; - SCFree(temp); + for (i = 0; i < SC_RADIX_USER_DATA_MAX; i++) { + if (prev->user[i] != NULL) { + SCFree(prev->user[i]); + } + } + SCFree(prev->user); + SCFree(prev); } de_ctx->sc_sig_sig_wrapper = NULL; diff --git a/src/detect-engine.c b/src/detect-engine.c index ea90f8b8e8..46cb37ed9a 100644 --- a/src/detect-engine.c +++ b/src/detect-engine.c @@ -535,6 +535,11 @@ TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data) { PatternMatchThreadDestroy(&det_ctx->mtc, det_ctx->de_ctx->mpm_matcher); PatternMatchThreadDestroy(&det_ctx->mtcu, det_ctx->de_ctx->mpm_matcher); + PmqFree(&det_ctx->pmq); + + if (det_ctx->de_state_sig_array != NULL) + SCFree(det_ctx->de_state_sig_array); + SCFree(det_ctx); return TM_ECODE_OK; diff --git a/src/detect-http-client-body.c b/src/detect-http-client-body.c index 446afeadb0..a64a098682 100644 --- a/src/detect-http-client-body.c +++ b/src/detect-http-client-body.c @@ -52,6 +52,7 @@ int DetectHttpClientBodyMatch(ThreadVars *t, DetectEngineThreadCtx *det_ctx, SigMatch *m); int DetectHttpClientBodySetup(DetectEngineCtx *, Signature *, char *); void DetectHttpClientBodyRegisterTests(void); +void DetectHttpClientBodyFree(void *); /** * \brief Registers the keyword handlers for the "http_client_body" keyword. @@ -63,7 +64,7 @@ void DetectHttpClientBodyRegister(void) sigmatch_table[DETECT_AL_HTTP_CLIENT_BODY].AppLayerMatch = DetectHttpClientBodyMatch; sigmatch_table[DETECT_AL_HTTP_CLIENT_BODY].alproto = ALPROTO_HTTP; sigmatch_table[DETECT_AL_HTTP_CLIENT_BODY].Setup = DetectHttpClientBodySetup; - sigmatch_table[DETECT_AL_HTTP_CLIENT_BODY].Free = NULL; + sigmatch_table[DETECT_AL_HTTP_CLIENT_BODY].Free = DetectHttpClientBodyFree; sigmatch_table[DETECT_AL_HTTP_CLIENT_BODY].RegisterTests = DetectHttpClientBodyRegisterTests; sigmatch_table[DETECT_AL_HTTP_CLIENT_BODY].flags |= SIGMATCH_PAYLOAD ; @@ -248,17 +249,36 @@ int DetectHttpClientBodySetup(DetectEngineCtx *de_ctx, Signature *s, char *arg) return 0; error: - if (hcbd != NULL) { - if (hcbd->content != NULL) - SCFree(hcbd->content); - SCFree(hcbd); - } - if(nm != NULL) - SCFree(sm); + if (hcbd != NULL) + DetectHttpClientBodyFree(hcbd); + + if(nm != NULL) + SCFree(nm); return -1; } +/** + * \brief The function to free the http_client_body data. + * + * \param ptr Pointer to the http_client_body. + */ +void DetectHttpClientBodyFree(void *ptr) +{ + SCEnter(); + DetectHttpClientBodyData *hcbd = (DetectHttpClientBodyData *)ptr; + if (hcbd == NULL) + SCReturn; + + if (hcbd->content != NULL) + SCFree(hcbd->content); + + BoyerMooreCtxDeInit(hcbd->bm_ctx); + SCFree(hcbd); + + SCReturn; +} + /************************************Unittests*********************************/ #ifdef UNITTESTS diff --git a/src/detect-http-cookie.c b/src/detect-http-cookie.c index 6a93004b6b..acbc4734cd 100644 --- a/src/detect-http-cookie.c +++ b/src/detect-http-cookie.c @@ -54,6 +54,7 @@ int DetectHttpCookieMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx, SigMatch *m); static int DetectHttpCookieSetup (DetectEngineCtx *, Signature *, char *); void DetectHttpCookieRegisterTests(void); +void DetectHttpCookieFree(void *); /** * \brief Registration function for keyword: http_cookie @@ -64,7 +65,7 @@ void DetectHttpCookieRegister (void) { sigmatch_table[DETECT_AL_HTTP_COOKIE].AppLayerMatch = DetectHttpCookieMatch; sigmatch_table[DETECT_AL_HTTP_COOKIE].alproto = ALPROTO_HTTP; sigmatch_table[DETECT_AL_HTTP_COOKIE].Setup = DetectHttpCookieSetup; - sigmatch_table[DETECT_AL_HTTP_COOKIE].Free = NULL; + sigmatch_table[DETECT_AL_HTTP_COOKIE].Free = DetectHttpCookieFree; sigmatch_table[DETECT_AL_HTTP_COOKIE].RegisterTests = DetectHttpCookieRegisterTests; sigmatch_table[DETECT_AL_HTTP_COOKIE].flags |= SIGMATCH_PAYLOAD; @@ -164,6 +165,21 @@ end: SCReturnInt(ret); } +/** + * \brief this function clears the memory of http_cookie modifier keyword + * + * \param ptr Pointer to the Detection Cookie data + */ +void DetectHttpCookieFree(void *ptr) +{ + DetectHttpCookieData *hcd = (DetectHttpCookieData *)ptr; + if (hcd == NULL) + return; + if (hcd->data != NULL) + SCFree(hcd->data); + SCFree(hcd); +} + /** * \brief this function setups the http_cookie modifier keyword used in the rule * @@ -244,6 +260,7 @@ static int DetectHttpCookieSetup (DetectEngineCtx *de_ctx, Signature *s, char *s /* free the old content sigmatch, the content pattern memory * is taken over by the new sigmatch */ + BoyerMooreCtxDeInit(((DetectContentData *)pm->ctx)->bm_ctx); SCFree(pm->ctx); SCFree(pm); @@ -258,11 +275,7 @@ static int DetectHttpCookieSetup (DetectEngineCtx *de_ctx, Signature *s, char *s s->alproto = ALPROTO_HTTP; return 0; error: - if (hd != NULL) { - if (hd->data != NULL) - SCFree(hd->data); - SCFree(hd); - } + if (hd != NULL) DetectHttpCookieFree(hd); if(sm !=NULL) SCFree(sm); return -1; } diff --git a/src/detect-http-header.c b/src/detect-http-header.c index e7ec29b0c0..df8623cebf 100644 --- a/src/detect-http-header.c +++ b/src/detect-http-header.c @@ -53,6 +53,7 @@ int DetectHttpHeaderMatch(ThreadVars *t, DetectEngineThreadCtx *det_ctx, SigMatch *m); int DetectHttpHeaderSetup(DetectEngineCtx *, Signature *, char *); void DetectHttpHeaderRegisterTests(void); +void DetectHttpHeaderFree(void *); /** * \brief Registers the keyword handlers for the "http_header" keyword. @@ -64,7 +65,7 @@ void DetectHttpHeaderRegister(void) sigmatch_table[DETECT_AL_HTTP_HEADER].AppLayerMatch = DetectHttpHeaderMatch; sigmatch_table[DETECT_AL_HTTP_HEADER].alproto = ALPROTO_HTTP; sigmatch_table[DETECT_AL_HTTP_HEADER].Setup = DetectHttpHeaderSetup; - sigmatch_table[DETECT_AL_HTTP_HEADER].Free = NULL; + sigmatch_table[DETECT_AL_HTTP_HEADER].Free = DetectHttpHeaderFree; sigmatch_table[DETECT_AL_HTTP_HEADER].RegisterTests = DetectHttpHeaderRegisterTests; sigmatch_table[DETECT_AL_HTTP_HEADER].flags |= SIGMATCH_PAYLOAD ; @@ -141,6 +142,21 @@ int DetectHttpHeaderMatch(ThreadVars *t, DetectEngineThreadCtx *det_ctx, SCReturnInt(result); } +/** + * \brief this function clears the memory of http_header modifier keyword + * + * \param ptr Pointer to the Detection Header Data + */ +void DetectHttpHeaderFree(void *ptr) +{ + DetectHttpHeaderData *hd = (DetectHttpHeaderData *)ptr; + if (hd == NULL) + return; + if (hd->content != NULL) + SCFree(hd->content); + SCFree(hd); +} + /** * \brief The setup function for the http_header keyword for a signature. * @@ -226,6 +242,7 @@ int DetectHttpHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, char *arg) /* free the old content sigmatch, the content pattern memory * is taken over by the new sigmatch */ + BoyerMooreCtxDeInit(((DetectContentData *)sm->ctx)->bm_ctx); SCFree(sm->ctx); SCFree(sm); @@ -238,11 +255,8 @@ int DetectHttpHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, char *arg) return 0; error: - if (hcbd != NULL) { - if (hcbd->content != NULL) - SCFree(hcbd->content); - SCFree(hcbd); - } + if (hcbd != NULL) + DetectHttpHeaderFree(hcbd); if(nm != NULL) SCFree(sm); diff --git a/src/detect-http-method.c b/src/detect-http-method.c index cb7c9d5591..c5bfa326a0 100644 --- a/src/detect-http-method.c +++ b/src/detect-http-method.c @@ -149,7 +149,6 @@ static int DetectHttpMethodSetup(DetectEngineCtx *de_ctx, Signature *s, char *st { SCEnter(); DetectHttpMethodData *data = NULL; - SigMatch *sm = NULL; bstr *method; /** new sig match to replace previous content */ SigMatch *nm = NULL; @@ -216,6 +215,7 @@ static int DetectHttpMethodSetup(DetectEngineCtx *de_ctx, Signature *s, char *st /* free the old content sigmatch, the memory for the pattern * is taken over by our new sigmatch */ + BoyerMooreCtxDeInit(((DetectContentData *)pm->ctx)->bm_ctx); SCFree(pm->ctx); SCFree(pm); @@ -232,7 +232,8 @@ static int DetectHttpMethodSetup(DetectEngineCtx *de_ctx, Signature *s, char *st error: if (data != NULL) DetectHttpMethodFree(data); - if (sm != NULL) SCFree(sm); + if (nm->ctx != NULL) DetectHttpMethodFree(nm); + if (nm != NULL) SCFree(nm); SCReturnInt(-1); } diff --git a/src/detect-parse.c b/src/detect-parse.c index 5791fc90d9..81f240906c 100644 --- a/src/detect-parse.c +++ b/src/detect-parse.c @@ -436,7 +436,7 @@ static int SigParseOptions(DetectEngineCtx *de_ctx, Signature *s, char *optstr) if (optvalue) pcre_free_substring(optvalue); if (optstr) SCFree(optstr); //if (optmore) pcre_free_substring(optmore); - if (arr != NULL) SCFree(arr); + if (arr != NULL) pcre_free_substring_list(arr); return SigParseOptions(de_ctx, s, optmore); } @@ -444,7 +444,7 @@ static int SigParseOptions(DetectEngineCtx *de_ctx, Signature *s, char *optstr) if (optvalue) pcre_free_substring(optvalue); if (optmore) pcre_free_substring(optmore); if (optstr) SCFree(optstr); - if (arr != NULL) SCFree(arr); + if (arr != NULL) pcre_free_substring_list(arr); return 0; error: @@ -452,7 +452,7 @@ error: if (optvalue) pcre_free_substring(optvalue); if (optmore) pcre_free_substring(optmore); if (optstr) SCFree(optstr); - if (arr != NULL) SCFree(arr); + if (arr != NULL) pcre_free_substring_list(arr); return -1; } @@ -783,6 +783,14 @@ void SigFree(Signature *s) { if (s == NULL) return; + /* XXX GS there seems to be a bug in the IPOnlyCIDR list, which causes + system abort. */ + /*if (s->CidrDst != NULL) + IPOnlyCIDRListFree(s->CidrDst); + + if (s->CidrSrc != NULL) + IPOnlyCIDRListFree(s->CidrSrc);*/ + SigMatch *sm = s->match, *nsm; while (sm != NULL) { nsm = sm->next; @@ -796,6 +804,21 @@ void SigFree(Signature *s) { SigMatchFree(sm); sm = nsm; } + + sm = s->umatch; + while (sm != NULL) { + nsm = sm->next; + SigMatchFree(sm); + sm = nsm; + } + + sm = s->amatch; + while (sm != NULL) { + nsm = sm->next; + SigMatchFree(sm); + sm = nsm; + } + DetectAddressHeadCleanup(&s->src); DetectAddressHeadCleanup(&s->dst); diff --git a/src/detect-uricontent.c b/src/detect-uricontent.c index 6014f3ca68..4ba07e2c38 100644 --- a/src/detect-uricontent.c +++ b/src/detect-uricontent.c @@ -59,6 +59,7 @@ void HttpUriRegisterTests(void); int DetectAppLayerUricontentMatch (ThreadVars *, DetectEngineThreadCtx *, Flow *, uint8_t , void *, Signature *, SigMatch *); +void DetectUricontentFree(void *); /** * \brief Registration function for uricontent: keyword @@ -69,7 +70,7 @@ void DetectUricontentRegister (void) sigmatch_table[DETECT_URICONTENT].AppLayerMatch = NULL; sigmatch_table[DETECT_URICONTENT].Match = NULL; sigmatch_table[DETECT_URICONTENT].Setup = DetectUricontentSetup; - sigmatch_table[DETECT_URICONTENT].Free = NULL; + sigmatch_table[DETECT_URICONTENT].Free = DetectUricontentFree; sigmatch_table[DETECT_URICONTENT].RegisterTests = HttpUriRegisterTests; sigmatch_table[DETECT_URICONTENT].alproto = ALPROTO_HTTP; @@ -85,6 +86,27 @@ uint32_t DetectUricontentMaxId(DetectEngineCtx *de_ctx) return MpmPatternIdStoreGetMaxId(de_ctx->mpm_pattern_id_store); } +/** + * \brief this function will Free memory associated with DetectUricontentData + * + * \param cd pointer to DetectUricotentData + */ +void DetectUricontentFree(void *ptr) { + SCEnter(); + DetectUricontentData *cd = (DetectUricontentData *)ptr; + + if (cd == NULL) + SCReturn; + + if (cd->uricontent != NULL) + SCFree(cd->uricontent); + + BoyerMooreCtxDeInit(cd->bm_ctx); + + SCFree(cd); + SCReturn; +} + /** * \brief Helper function to print a DetectContentData */ @@ -342,6 +364,7 @@ int DetectUricontentSetup (DetectEngineCtx *de_ctx, Signature *s, char *contents error: if (cd) SCFree(cd); + if (sm != NULL) SCFree(sm); SCReturnInt(-1); } diff --git a/src/util-debug.c b/src/util-debug.c index 807688d19d..0b009d7fc5 100644 --- a/src/util-debug.c +++ b/src/util-debug.c @@ -538,6 +538,11 @@ static inline SCLogOPIfaceCtx *SCLogInitFileOPIface(const char *file, { SCLogOPIfaceCtx *iface_ctx = SCLogAllocLogOPIfaceCtx(); + if (iface_ctx == NULL) { + SCLogError(SC_ERR_FATAL, "Fatal error encountered in SCLogInitFileOPIface. Exiting..."); + exit(EXIT_FAILURE); + } + iface_ctx->iface = SC_LOG_OP_IFACE_FILE; if (file != NULL && @@ -577,11 +582,10 @@ static inline SCLogOPIfaceCtx *SCLogInitConsoleOPIface(const char *log_format, { SCLogOPIfaceCtx *iface_ctx = SCLogAllocLogOPIfaceCtx(); - if ( (iface_ctx = SCMalloc(sizeof(SCLogOPIfaceCtx))) == NULL) { + if (iface_ctx == NULL) { SCLogError(SC_ERR_FATAL, "Fatal error encountered in SCLogInitConsoleOPIface. Exiting..."); exit(EXIT_FAILURE); } - memset(iface_ctx, 0, sizeof(SCLogOPIfaceCtx)); iface_ctx->iface = SC_LOG_OP_IFACE_CONSOLE; @@ -632,11 +636,10 @@ static inline SCLogOPIfaceCtx *SCLogInitSyslogOPIface(int facility, { SCLogOPIfaceCtx *iface_ctx = SCLogAllocLogOPIfaceCtx(); - if ( (iface_ctx = SCMalloc(sizeof(SCLogOPIfaceCtx))) == NULL) { + if ( iface_ctx == NULL) { SCLogError(SC_ERR_FATAL, "Fatal error encountered in SCLogInitSyslogOPIface. Exiting..."); exit(EXIT_FAILURE); } - memset(iface_ctx, 0, sizeof(SCLogOPIfaceCtx)); iface_ctx->iface = SC_LOG_OP_IFACE_SYSLOG; @@ -1171,6 +1174,7 @@ void SCLogLoadConfig(void) SCLogInitLogModule(sc_lid); //exit(1); /* \todo Can we free sc_lid now? */ + if (sc_lid != NULL) SCFree(sc_lid); } /** diff --git a/src/util-host-os-info.c b/src/util-host-os-info.c index 1354c12c02..9bf1590ea4 100644 --- a/src/util-host-os-info.c +++ b/src/util-host-os-info.c @@ -304,6 +304,9 @@ int SCHInfoAddHostOSInfo(char *host_os, char *host_os_ip_range, int is_ipv4) SCHInfoAddHostOSInfo(host_os, ip_str_rem, is_ipv4); } + if (ip_str != NULL) SCFree(ip_str); + if (ipv4_addr != NULL) SCFree(ipv4_addr); + if (ipv6_addr != NULL) SCFree(ipv6_addr); return *user_data; } diff --git a/src/util-mpm-b2g.c b/src/util-mpm-b2g.c index 4df1dac0da..803dd5467d 100644 --- a/src/util-mpm-b2g.c +++ b/src/util-mpm-b2g.c @@ -150,12 +150,12 @@ static void B2gHashFree(MpmCtx *mpm_ctx, B2gHashItem *hi) { if (hi == NULL) return; - B2gHashItem *t = hi->nxt; - B2gHashFree(mpm_ctx, t); - mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= sizeof(B2gHashItem); + B2gHashItem *t = hi->nxt; SCFree(hi); + + B2gHashFree(mpm_ctx, t); } static inline void memcpy_tolower(uint8_t *d, uint8_t *s, uint16_t len) { diff --git a/src/util-mpm.c b/src/util-mpm.c index 3dec2e0f86..1b4af75d5e 100644 --- a/src/util-mpm.c +++ b/src/util-mpm.c @@ -149,7 +149,6 @@ void PmqFree(PatternMatcherQueue *pmq) { return; PmqCleanup(pmq); - SCFree(pmq); } /** diff --git a/src/util-spm-bm.c b/src/util-spm-bm.c index 3dfa6b92e4..4e859f1ced 100644 --- a/src/util-spm-bm.c +++ b/src/util-spm-bm.c @@ -85,6 +85,24 @@ BmCtx *BoyerMooreCtxInit(uint8_t *needle, uint32_t needle_len) { return new; } +/** + * \brief Free the memory allocated to Booyer More context. + * + * \param bmCtx pointer to the Context for the pattern + */ +void BoyerMooreCtxDeInit(BmCtx *bmctx) +{ + SCEnter(); + if (bmctx == NULL) + SCReturn; + + if (bmctx->bmGs != NULL) + SCFree(bmctx->bmGs); + + SCFree(bmctx); + + SCReturn; +} /** * \brief Array setup function for bad characters that split the pattern * Remember that the result array should be the length of ALPHABET_SIZE diff --git a/src/util-spm-bm.h b/src/util-spm-bm.h index 1d44ce07f3..19efddff6b 100644 --- a/src/util-spm-bm.h +++ b/src/util-spm-bm.h @@ -48,6 +48,6 @@ void PreBmBcNocase(const uint8_t *x, int32_t m, int32_t *bmBc); void BoyerMooreSuffixesNocase(const uint8_t *x, int32_t m, int32_t *suff); void PreBmGsNocase(const uint8_t *x, int32_t m, int32_t *bmGs); uint8_t *BoyerMooreNocase(uint8_t *x, int32_t m, uint8_t *y, int32_t n, int32_t *bmGs, int32_t *bmBc); - +void BoyerMooreCtxDeInit(BmCtx *); #endif /* __UTIL_SPM_BM__ */