From: Victor Julien Date: Thu, 25 Feb 2021 19:06:40 +0000 (+0100) Subject: detect: track base id for xform buffers X-Git-Tag: suricata-6.0.2~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d4705a896a73c5eba47383b6cf77d6d2b6a29998;p=thirdparty%2Fsuricata.git detect: track base id for xform buffers Buffers with transforms are based on the non-transformed "base" buffer, with a new ID assigned and the transform callbacks added. This patch stores the id of the original buffer in the new buffer inspect and prefilter structures. This way the buffers with and without transforms can share some of the logic are progression of file and body inspection trackers. Related tickets: #4361 #4199 #3616 (cherry picked from commit 975062cf401f79c00abf728d923c65aabd143af2) --- diff --git a/src/detect-engine-mpm.c b/src/detect-engine-mpm.c index 5c99e3386a..2ea28e7775 100644 --- a/src/detect-engine-mpm.c +++ b/src/detect-engine-mpm.c @@ -116,6 +116,7 @@ void DetectAppLayerMpmRegister2(const char *name, snprintf(am->pname, sizeof(am->pname), "%s", am->name); am->direction = direction; am->sm_list = sm_list; + am->sm_list_base = sm_list; am->priority = priority; am->type = DETECT_BUFFER_MPM_TYPE_APP; @@ -155,6 +156,7 @@ void DetectAppLayerMpmRegisterByParentId(DetectEngineCtx *de_ctx, am->name = t->name; am->direction = t->direction; am->sm_list = id; // use new id + am->sm_list_base = t->sm_list; am->type = DETECT_BUFFER_MPM_TYPE_APP; am->PrefilterRegisterWithListId = t->PrefilterRegisterWithListId; am->app_v2.GetData = t->app_v2.GetData; @@ -350,6 +352,7 @@ void DetectPktMpmRegisterByParentId(DetectEngineCtx *de_ctx, am->name = t->name; snprintf(am->pname, sizeof(am->pname), "%s#%d", am->name, id); am->sm_list = id; // use new id + am->sm_list_base = t->sm_list; am->type = DETECT_BUFFER_MPM_TYPE_PKT; am->PrefilterRegisterWithListId = t->PrefilterRegisterWithListId; am->pkt_v1.GetData = t->pkt_v1.GetData; diff --git a/src/detect-engine.c b/src/detect-engine.c index c6bca16cef..3a41868fb5 100644 --- a/src/detect-engine.c +++ b/src/detect-engine.c @@ -150,6 +150,7 @@ void DetectPktInspectEngineRegister(const char *name, "failed to register inspect engine %s: %s", name, strerror(errno)); } new_engine->sm_list = sm_list; + new_engine->sm_list_base = sm_list; new_engine->v1.Callback = Callback; new_engine->v1.GetData = GetPktData; @@ -270,6 +271,7 @@ void DetectAppLayerInspectEngineRegister2(const char *name, new_engine->alproto = alproto; new_engine->dir = direction; new_engine->sm_list = sm_list; + new_engine->sm_list_base = sm_list; new_engine->progress = progress; new_engine->v2.Callback = Callback2; new_engine->v2.GetData = GetData; @@ -302,6 +304,7 @@ static void DetectAppLayerInspectEngineCopy( new_engine->alproto = t->alproto; new_engine->dir = t->dir; new_engine->sm_list = new_list; /* use new list id */ + new_engine->sm_list_base = sm_list; new_engine->progress = t->progress; new_engine->Callback = t->Callback; new_engine->v2 = t->v2; @@ -334,6 +337,7 @@ static void DetectAppLayerInspectEngineCopyListToDetectCtx(DetectEngineCtx *de_c new_engine->alproto = t->alproto; new_engine->dir = t->dir; new_engine->sm_list = t->sm_list; + new_engine->sm_list_base = t->sm_list; new_engine->progress = t->progress; new_engine->Callback = t->Callback; new_engine->v2 = t->v2; @@ -367,6 +371,7 @@ static void DetectPktInspectEngineCopy( exit(EXIT_FAILURE); } new_engine->sm_list = new_list; /* use new list id */ + new_engine->sm_list_base = sm_list; new_engine->v1 = t->v1; new_engine->v1.transforms = transforms; /* assign transforms */ @@ -396,6 +401,7 @@ static void DetectPktInspectEngineCopyListToDetectCtx(DetectEngineCtx *de_ctx) exit(EXIT_FAILURE); } new_engine->sm_list = t->sm_list; + new_engine->sm_list_base = t->sm_list; new_engine->v1 = t->v1; if (de_ctx->pkt_inspect_engines == NULL) { @@ -435,6 +441,7 @@ static void AppendStreamInspectEngine(Signature *s, SigMatchData *stream, int di new_engine->dir = direction; new_engine->stream = true; new_engine->sm_list = DETECT_SM_LIST_PMATCH; + new_engine->sm_list_base = DETECT_SM_LIST_PMATCH; new_engine->smd = stream; new_engine->Callback = DetectEngineInspectStream; new_engine->progress = 0; @@ -504,6 +511,7 @@ int DetectEngineAppInspectionEngine2Signature(DetectEngineCtx *de_ctx, Signature } new_engine->sm_list = e->sm_list; + new_engine->sm_list_base = e->sm_list_base; new_engine->smd = ptrs[new_engine->sm_list]; new_engine->v1 = e->v1; SCLogDebug("sm_list %d new_engine->v1 %p/%p/%p", @@ -567,6 +575,7 @@ int DetectEngineAppInspectionEngine2Signature(DetectEngineCtx *de_ctx, Signature new_engine->alproto = t->alproto; new_engine->dir = t->dir; new_engine->sm_list = t->sm_list; + new_engine->sm_list_base = t->sm_list_base; new_engine->smd = ptrs[new_engine->sm_list]; new_engine->Callback = t->Callback; new_engine->progress = t->progress; @@ -1487,6 +1496,7 @@ static int DetectEnginePktInspectionAppend(Signature *s, InspectionBufferPktInsp return -1; e->sm_list = list_id; + e->sm_list_base = list_id; e->v1.Callback = Callback; e->smd = data; diff --git a/src/detect.h b/src/detect.h index c1fc715ca0..6d510b7f51 100644 --- a/src/detect.h +++ b/src/detect.h @@ -403,9 +403,10 @@ typedef struct DetectEngineAppInspectionEngine_ { AppProto alproto; uint8_t dir; uint8_t id; /**< per sig id used in state keeping */ - uint16_t mpm:1; - uint16_t stream:1; - uint16_t sm_list:14; + bool mpm; + bool stream; + uint16_t sm_list; + uint16_t sm_list_base; /**< base buffer being transformed */ int16_t progress; /* \retval 0 No match. Don't discontinue matching yet. We need more data. @@ -460,8 +461,9 @@ typedef InspectionBuffer *(*InspectionBufferGetPktDataPtr)( typedef struct DetectEnginePktInspectionEngine { SigMatchData *smd; - uint16_t mpm:1; - uint16_t sm_list:15; + bool mpm; + uint16_t sm_list; + uint16_t sm_list_base; struct { InspectionBufferGetPktDataPtr GetData; InspectionBufferPktInspectFunc Callback; @@ -612,7 +614,8 @@ typedef struct DetectBufferMpmRegistery_ { const char *name; char pname[32]; /**< name used in profiling */ int direction; /**< SIG_FLAG_TOSERVER or SIG_FLAG_TOCLIENT */ - int sm_list; + int16_t sm_list; + int16_t sm_list_base; int priority; int id; /**< index into this array and result arrays */ enum DetectBufferMpmType type;