From b931895901dea0ec338a0afc33d2eecc959fbd3c Mon Sep 17 00:00:00 2001 From: Pablo Rincon Date: Mon, 21 Jun 2010 18:56:37 +0200 Subject: [PATCH] Fixing flow cleanup and ctx initialization --- src/detect-engine-tag.c | 26 +++++++++++--------------- src/detect-parse.c | 7 +++++++ src/detect-tag.c | 12 +++++++++--- src/detect-tag.h | 1 + src/detect-tls-version.c | 3 ++- src/flow-util.h | 2 ++ 6 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/detect-engine-tag.c b/src/detect-engine-tag.c index b8f794bb59..6cb17e604b 100644 --- a/src/detect-engine-tag.c +++ b/src/detect-engine-tag.c @@ -130,22 +130,18 @@ void TagRestartCtx() { */ void TagHashInit(DetectTagHostCtx *tag_ctx) { - if (tag_ctx->tag_hash_table_ipv4 == NULL || - tag_ctx->tag_hash_table_ipv6 == NULL) { - - tag_ctx->tag_hash_table_ipv4 = HashListTableInit(TAG_HASH_SIZE, TagHashFunc, TagCompareFunc, TagFreeFunc); - if(tag_ctx->tag_hash_table_ipv4 == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, - "Tag: Failed to initialize ipv4 dst hash table."); - exit(EXIT_FAILURE); - } + tag_ctx->tag_hash_table_ipv4 = HashListTableInit(TAG_HASH_SIZE, TagHashFunc, TagCompareFunc, TagFreeFunc); + if(tag_ctx->tag_hash_table_ipv4 == NULL) { + SCLogError(SC_ERR_MEM_ALLOC, + "Tag: Failed to initialize ipv4 dst hash table."); + exit(EXIT_FAILURE); + } - tag_ctx->tag_hash_table_ipv6 = HashListTableInit(TAG_HASH_SIZE, TagHashFunc, TagCompareFunc, TagFreeFunc); - if(tag_ctx->tag_hash_table_ipv6 == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, - "Tag: Failed to initialize ipv4 src hash table."); - exit(EXIT_FAILURE); - } + tag_ctx->tag_hash_table_ipv6 = HashListTableInit(TAG_HASH_SIZE, TagHashFunc, TagCompareFunc, TagFreeFunc); + if(tag_ctx->tag_hash_table_ipv6 == NULL) { + SCLogError(SC_ERR_MEM_ALLOC, + "Tag: Failed to initialize ipv4 src hash table."); + exit(EXIT_FAILURE); } } diff --git a/src/detect-parse.c b/src/detect-parse.c index 506df7031c..4286ae6e47 100644 --- a/src/detect-parse.c +++ b/src/detect-parse.c @@ -988,6 +988,13 @@ void SigFree(Signature *s) { sm = nsm; } + sm = s->tmatch; + while (sm != NULL) { + nsm = sm->next; + SigMatchFree(sm); + sm = nsm; + } + DetectAddressHeadCleanup(&s->src); DetectAddressHeadCleanup(&s->dst); diff --git a/src/detect-tag.c b/src/detect-tag.c index 03c4093290..eaa7a76a30 100644 --- a/src/detect-tag.c +++ b/src/detect-tag.c @@ -110,6 +110,9 @@ int DetectTagFlowAdd(Packet *p, DetectTagDataEntry *tde) { uint16_t num_tags = 0; DetectTagDataEntry *iter = NULL; + if (p->flow == NULL) + return 1; + SCMutexLock(&p->flow->m); if (p->flow->tag_list == NULL) { @@ -184,7 +187,7 @@ int DetectTagMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx, Packet *p, Si if (td->direction == DETECT_TAG_DIR_SRC || td->direction == DETECT_TAG_DIR_DST) { SCLogDebug("Tagging Host with sid %"PRIu32":%"PRIu32"", s->id, s->gid); if (TagHashAddTag(tag_ctx, tde, p) == 1) - DetectTagDataEntryFree(tde); + SCFree(tde); } else { SCLogError(SC_ERR_INVALID_VALUE, "Error on direction of a tag keyword (not src nor dst)"); @@ -194,7 +197,7 @@ int DetectTagMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx, Packet *p, Si if (p->flow != NULL) { /* If it already exists it will be updated */ if (DetectTagFlowAdd(p, tde) == 1) - DetectTagDataEntryFree(tde); + SCFree(tde); } else { SCLogDebug("No flow to append the session tag"); } @@ -384,7 +387,8 @@ void DetectTagDataEntryFree(void *ptr) { DetectTagDataEntry *dte = (DetectTagDataEntry *)ptr; if (dte->next != NULL) DetectTagDataEntryFree(dte->next); - SCFree(ptr); + dte->next = NULL; + SCFree(dte); } } @@ -857,6 +861,8 @@ cleanup: UTHFreePackets(p, 7); if (det_ctx != NULL) DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); + if (result == 1 && f.tag_list != NULL) + DetectTagDataListFree(f.tag_list); if (de_ctx != NULL) { SigGroupCleanup(de_ctx); diff --git a/src/detect-tag.h b/src/detect-tag.h index 8e948f4aa6..67f2562f64 100644 --- a/src/detect-tag.h +++ b/src/detect-tag.h @@ -90,6 +90,7 @@ typedef struct DetectTagDataEntryList_ { DetectTagDataEntry *header_entry; Address addr; /**< Var used to store dst or src addr */ uint8_t ipv; /**< IP Version */ + SCMutex lock; }DetectTagDataEntryList; /* prototypes */ diff --git a/src/detect-tls-version.c b/src/detect-tls-version.c index 2b6d525f98..53f42cece6 100644 --- a/src/detect-tls-version.c +++ b/src/detect-tls-version.c @@ -541,6 +541,7 @@ end: } static int DetectTlsVersionTestDetect03(void) { + DetectEngineCtx *de_ctx = NULL; int result = 0; Flow f; uint8_t tlsbuf1[] = { 0x16 }; @@ -590,7 +591,7 @@ static int DetectTlsVersionTestDetect03(void) { ssn.toserver_smsg_head = stream_msg; ssn.toserver_smsg_tail = stream_msg; - DetectEngineCtx *de_ctx = DetectEngineCtxInit(); + de_ctx = DetectEngineCtxInit(); if (de_ctx == NULL) { goto end; } diff --git a/src/flow-util.h b/src/flow-util.h index f2c16aa75c..97536bf33b 100644 --- a/src/flow-util.h +++ b/src/flow-util.h @@ -80,6 +80,7 @@ (f)->alflags = 0; \ (f)->alproto = 0; \ DetectTagDataListFree((f)->tag_list); \ + (f)->tag_list = NULL; \ } while(0) #define FLOW_DESTROY(f) do { \ @@ -97,6 +98,7 @@ (f)->alflags = 0; \ (f)->alproto = 0; \ DetectTagDataListFree((f)->tag_list); \ + (f)->tag_list = NULL; \ } while(0) Flow *FlowAlloc(void);